Browse code
generator modulates
Greg Wiley authored on 04/05/2017 18:07:30
Showing 2 changed files
Showing 2 changed files
... | ... |
@@ -14,12 +14,25 @@ |
14 | 14 |
|
15 | 15 |
#include <string> |
16 | 16 |
#include <functional> |
17 |
+#include <sstream> |
|
18 |
+#include <iomanip> |
|
19 |
+ |
|
20 |
+#include "sys_stdlib.h" |
|
17 | 21 |
|
18 | 22 |
using generator = std::function<std::string()>; |
19 | 23 |
|
20 |
-inline generator make_generator() |
|
24 |
+inline std::string token_from_int(int x) { |
|
25 |
+ int v = x % 1000000; |
|
26 |
+ std::ostringstream is; |
|
27 |
+ is << std::setfill('0') << std::setw(6)<< v; |
|
28 |
+ return is.str(); |
|
29 |
+ } |
|
30 |
+ |
|
31 |
+inline generator make_generator(const stdlib &stdlib) |
|
21 | 32 |
{ |
22 |
- return [] {return std::string ("234567");}; |
|
33 |
+ return [stdlib] { |
|
34 |
+ return token_from_int(stdlib.rand()); |
|
35 |
+ }; |
|
23 | 36 |
} |
24 | 37 |
|
25 | 38 |
#endif |
... | ... |
@@ -8,16 +8,40 @@ |
8 | 8 |
* file. If not, you will find a copy in the "LICENSE" file |
9 | 9 |
* at https://github.com/cjdev/dual-control. |
10 | 10 |
*/ |
11 |
- |
|
11 |
+#include <memory> |
|
12 | 12 |
#include <algorithm> |
13 |
- |
|
13 |
+#include <initializer_list> |
|
14 |
+#include <vector> |
|
14 | 15 |
#include "generator.h" |
16 |
+#include "sys_stdlib.h" |
|
15 | 17 |
#include "test_util.h" |
16 | 18 |
|
19 |
+class fake_stdlib : public stdlib_ifc { |
|
20 |
+ |
|
21 |
+ private: |
|
22 |
+ std::vector<int> samples_; |
|
23 |
+ mutable std::vector<int>::iterator current_; |
|
24 |
+ public: |
|
25 |
+ fake_stdlib( const std::initializer_list<int> &samples) |
|
26 |
+ : samples_(samples.begin(), samples.end()), |
|
27 |
+ current_(samples_.begin()) {} |
|
28 |
+ int rand() const override { |
|
29 |
+ if (current_ != samples_.end()) { |
|
30 |
+ auto rval = *current_; |
|
31 |
+ current_ += 1; |
|
32 |
+ return rval; |
|
33 |
+ } |
|
34 |
+ return 0; |
|
35 |
+ } |
|
36 |
+}; |
|
37 |
+ |
|
17 | 38 |
int six_digits() |
18 | 39 |
{ |
19 | 40 |
// given |
20 |
- generator generator = make_generator(); |
|
41 |
+ std::initializer_list<int> samples { 1, 2, 3 }; |
|
42 |
+ auto test_stdlib = std::make_shared<fake_stdlib>(samples); |
|
43 |
+ stdlib stdlib(test_stdlib); |
|
44 |
+ generator generator = make_generator(stdlib); |
|
21 | 45 |
|
22 | 46 |
// when |
23 | 47 |
auto actual = generator(); |
... | ... |
@@ -30,9 +54,26 @@ int six_digits() |
30 | 54 |
succeed(); |
31 | 55 |
} |
32 | 56 |
|
57 |
+int modulated_source_modulates_tokens() { |
|
58 |
+ // given |
|
59 |
+ std::initializer_list<int> samples { 1, 2, 3 }; |
|
60 |
+ auto test_stdlib = std::make_shared<fake_stdlib>(samples); |
|
61 |
+ stdlib stdlib(test_stdlib); |
|
62 |
+ generator generator = make_generator(stdlib); |
|
63 |
+ |
|
64 |
+ // when |
|
65 |
+ auto actual1 = generator(); |
|
66 |
+ auto actual2 = generator(); |
|
67 |
+ |
|
68 |
+ // then |
|
69 |
+ check (actual1 != actual2, "samples should be different"); |
|
70 |
+ succeed(); |
|
71 |
+} |
|
72 |
+ |
|
33 | 73 |
int run_tests() |
34 | 74 |
{ |
35 | 75 |
test (six_digits); |
76 |
+ test (modulated_source_modulates_tokens); |
|
36 | 77 |
succeed(); |
37 | 78 |
} |
38 | 79 |
|