Browse code
format
Greg Wiley authored on 04/05/2017 21:22:26
Showing 11 changed files
Showing 11 changed files
- dual_control_tool.cc
- generator.h
- generator_test.cc
- sys_stdlib.cc
- sys_stdlib.h
- sys_time.cc
- sys_time.h
- sys_unistd.cc
- system.cc
- system.h
- system_test.cc
... | ... |
@@ -22,32 +22,35 @@ |
22 | 22 |
#include "system.h" |
23 | 23 |
#include "sys_fstream.h" |
24 | 24 |
|
25 |
-namespace { |
|
26 |
- class system init_system() { |
|
27 |
- stdlib stdlib(stdlib::get()); |
|
28 |
- sys_time time(sys_time::get()); |
|
29 |
- class system system(stdlib, time); |
|
30 |
- return system; |
|
31 |
- } |
|
32 |
- installer init_installer() { |
|
33 |
- fstreams fstreams(fstreams::create()); |
|
34 |
- tokens tokens(tokens::create(fstreams)); |
|
35 |
- pwd pwd(pwd::create()); |
|
36 |
- unistd unistd(unistd::create()); |
|
37 |
- directory directory(directory::create(unistd, pwd)); |
|
38 |
- stdlib stdlib(stdlib::get()); |
|
39 |
- generator generator{make_generator(stdlib)}; |
|
40 |
- installer installer(installer::create(tokens, unistd, |
|
41 |
- directory, generator)); |
|
25 |
+namespace |
|
26 |
+{ |
|
27 |
+class system init_system() |
|
28 |
+{ |
|
29 |
+ stdlib stdlib (stdlib::get()); |
|
30 |
+ sys_time time (sys_time::get()); |
|
31 |
+ class system system (stdlib, time); |
|
32 |
+ return system; |
|
33 |
+} |
|
34 |
+installer init_installer() |
|
35 |
+{ |
|
36 |
+ fstreams fstreams (fstreams::create()); |
|
37 |
+ tokens tokens (tokens::create (fstreams)); |
|
38 |
+ pwd pwd (pwd::create()); |
|
39 |
+ unistd unistd (unistd::create()); |
|
40 |
+ directory directory (directory::create (unistd, pwd)); |
|
41 |
+ stdlib stdlib (stdlib::get()); |
|
42 |
+ generator generator{make_generator (stdlib)}; |
|
43 |
+ installer installer (installer::create (tokens, unistd, |
|
44 |
+ directory, generator)); |
|
42 | 45 |
|
43 |
- return installer; |
|
44 |
- } |
|
46 |
+ return installer; |
|
47 |
+} |
|
45 | 48 |
} |
46 | 49 |
|
47 | 50 |
int main (int argc, char *argv[]) |
48 | 51 |
{ |
49 |
- class system system(init_system()); |
|
50 |
- installer tool(init_installer()); |
|
52 |
+ class system system (init_system()); |
|
53 |
+ installer tool (init_installer()); |
|
51 | 54 |
std::string generated_token = tool.install_token(); |
52 | 55 |
std::cout << generated_token << std::endl; |
53 | 56 |
return 0; |
... | ... |
@@ -22,17 +22,18 @@ |
22 | 22 |
|
23 | 23 |
using generator = std::function<std::string()>; |
24 | 24 |
|
25 |
-inline std::string token_from_int(int x) { |
|
26 |
- int v = std::abs(x % 1000000); |
|
27 |
- std::ostringstream is; |
|
28 |
- is << std::setfill('0') << std::setw(6)<< v; |
|
29 |
- return is.str(); |
|
30 |
- } |
|
31 |
- |
|
32 |
-inline generator make_generator(const stdlib &stdlib) |
|
25 |
+inline std::string token_from_int (int x) |
|
26 |
+{ |
|
27 |
+ int v = std::abs (x % 1000000); |
|
28 |
+ std::ostringstream is; |
|
29 |
+ is << std::setfill ('0') << std::setw (6)<< v; |
|
30 |
+ return is.str(); |
|
31 |
+} |
|
32 |
+ |
|
33 |
+inline generator make_generator (const stdlib &stdlib) |
|
33 | 34 |
{ |
34 | 35 |
return [stdlib] { |
35 |
- return token_from_int(stdlib.rand()); |
|
36 |
+ return token_from_int (stdlib.rand()); |
|
36 | 37 |
}; |
37 | 38 |
} |
38 | 39 |
|
... | ... |
@@ -18,22 +18,25 @@ |
18 | 18 |
#include "sys_stdlib.h" |
19 | 19 |
#include "test_util.h" |
20 | 20 |
|
21 |
-class fake_stdlib : public stdlib_ifc { |
|
22 |
- |
|
23 |
- private: |
|
24 |
- std::vector<int> samples_; |
|
25 |
- mutable std::vector<int>::iterator current_; |
|
26 |
- public: |
|
27 |
- fake_stdlib( const std::initializer_list<int> &samples) |
|
28 |
- : samples_(samples.begin(), samples.end()), |
|
29 |
- current_(samples_.begin()) {} |
|
30 |
- int rand() const override { |
|
31 |
- if (current_ != samples_.end()) { |
|
32 |
- auto rval = *current_; |
|
33 |
- current_ += 1; |
|
34 |
- return rval; |
|
35 |
- } |
|
36 |
- return 0; |
|
21 |
+class fake_stdlib : public stdlib_ifc |
|
22 |
+{ |
|
23 |
+ |
|
24 |
+private: |
|
25 |
+ std::vector<int> samples_; |
|
26 |
+ mutable std::vector<int>::iterator current_; |
|
27 |
+public: |
|
28 |
+ fake_stdlib ( const std::initializer_list<int> &samples) |
|
29 |
+ : samples_ (samples.begin(), samples.end()), |
|
30 |
+ current_ (samples_.begin()) {} |
|
31 |
+ int rand() const override |
|
32 |
+ { |
|
33 |
+ if (current_ != samples_.end()) { |
|
34 |
+ auto rval = *current_; |
|
35 |
+ current_ += 1; |
|
36 |
+ return rval; |
|
37 |
+ } |
|
38 |
+ |
|
39 |
+ return 0; |
|
37 | 40 |
} |
38 | 41 |
}; |
39 | 42 |
|
... | ... |
@@ -41,9 +44,9 @@ int six_digits() |
41 | 44 |
{ |
42 | 45 |
// given |
43 | 46 |
std::initializer_list<int> samples { 1 }; |
44 |
- auto test_stdlib = std::make_shared<fake_stdlib>(samples); |
|
45 |
- stdlib stdlib(test_stdlib); |
|
46 |
- generator generator = make_generator(stdlib); |
|
47 |
+ auto test_stdlib = std::make_shared<fake_stdlib> (samples); |
|
48 |
+ stdlib stdlib (test_stdlib); |
|
49 |
+ generator generator = make_generator (stdlib); |
|
47 | 50 |
|
48 | 51 |
// when |
49 | 52 |
auto actual = generator(); |
... | ... |
@@ -56,12 +59,13 @@ int six_digits() |
56 | 59 |
succeed(); |
57 | 60 |
} |
58 | 61 |
|
59 |
-int modulated_source_modulates_tokens() { |
|
62 |
+int modulated_source_modulates_tokens() |
|
63 |
+{ |
|
60 | 64 |
// given |
61 | 65 |
std::initializer_list<int> samples { 1, 2, 3 }; |
62 |
- auto test_stdlib = std::make_shared<fake_stdlib>(samples); |
|
63 |
- stdlib stdlib(test_stdlib); |
|
64 |
- generator generator = make_generator(stdlib); |
|
66 |
+ auto test_stdlib = std::make_shared<fake_stdlib> (samples); |
|
67 |
+ stdlib stdlib (test_stdlib); |
|
68 |
+ generator generator = make_generator (stdlib); |
|
65 | 69 |
|
66 | 70 |
// when |
67 | 71 |
auto actual1 = generator(); |
... | ... |
@@ -76,9 +80,9 @@ int int_max() |
76 | 80 |
{ |
77 | 81 |
// given |
78 | 82 |
std::initializer_list<int> samples { INT_MAX }; |
79 |
- auto test_stdlib = std::make_shared<fake_stdlib>(samples); |
|
80 |
- stdlib stdlib(test_stdlib); |
|
81 |
- generator generator = make_generator(stdlib); |
|
83 |
+ auto test_stdlib = std::make_shared<fake_stdlib> (samples); |
|
84 |
+ stdlib stdlib (test_stdlib); |
|
85 |
+ generator generator = make_generator (stdlib); |
|
82 | 86 |
|
83 | 87 |
// when |
84 | 88 |
auto actual = generator(); |
... | ... |
@@ -95,9 +99,9 @@ int int_min() |
95 | 99 |
{ |
96 | 100 |
// given |
97 | 101 |
std::initializer_list<int> samples { INT_MIN }; |
98 |
- auto test_stdlib = std::make_shared<fake_stdlib>(samples); |
|
99 |
- stdlib stdlib(test_stdlib); |
|
100 |
- generator generator = make_generator(stdlib); |
|
102 |
+ auto test_stdlib = std::make_shared<fake_stdlib> (samples); |
|
103 |
+ stdlib stdlib (test_stdlib); |
|
104 |
+ generator generator = make_generator (stdlib); |
|
101 | 105 |
|
102 | 106 |
// when |
103 | 107 |
auto actual = generator(); |
... | ... |
@@ -110,8 +114,6 @@ int int_min() |
110 | 114 |
succeed(); |
111 | 115 |
} |
112 | 116 |
|
113 |
- |
|
114 |
- |
|
115 | 117 |
int run_tests() |
116 | 118 |
{ |
117 | 119 |
test (six_digits); |
... | ... |
@@ -17,7 +17,8 @@ |
17 | 17 |
class stdlib_ifc |
18 | 18 |
{ |
19 | 19 |
public: |
20 |
- virtual void srand(unsigned int seed) const { |
|
20 |
+ virtual void srand (unsigned int seed) const |
|
21 |
+ { |
|
21 | 22 |
} |
22 | 23 |
virtual int rand() const |
23 | 24 |
{ |
... | ... |
@@ -34,8 +35,9 @@ private: |
34 | 35 |
public: |
35 | 36 |
stdlib (delegate delegate = std::make_shared<stdlib_ifc>()) : delegate_ |
36 | 37 |
(delegate) {} |
37 |
- void srand(unsigned int seed) const { |
|
38 |
- return delegate_->srand(seed); |
|
38 |
+ void srand (unsigned int seed) const |
|
39 |
+ { |
|
40 |
+ return delegate_->srand (seed); |
|
39 | 41 |
} |
40 | 42 |
int rand() const |
41 | 43 |
{ |
... | ... |
@@ -1,18 +1,34 @@ |
1 |
+/* Copyright (C) CJ Affiliate |
|
2 |
+ * |
|
3 |
+ * You may use, distribute and modify this code under the |
|
4 |
+ * terms of the GNU General Public License version 2 or |
|
5 |
+ * later. |
|
6 |
+ * |
|
7 |
+ * You should have received a copy of the license with this |
|
8 |
+ * file. If not, you will find a copy in the "LICENSE" file |
|
9 |
+ * at https://github.com/cjdev/dual-control. |
|
10 |
+ */ |
|
11 |
+ |
|
1 | 12 |
#include <memory> |
2 | 13 |
#include <ctime> |
3 | 14 |
|
4 | 15 |
#include "sys_time.h" |
5 | 16 |
|
6 |
-namespace { |
|
7 |
- class impl : public sys_time_ifc { |
|
8 |
- public: |
|
9 |
- time_t time(time_t *timer) const override{ |
|
10 |
- return ::time(timer); |
|
11 |
- } |
|
12 |
- }; |
|
17 |
+namespace |
|
18 |
+{ |
|
19 |
+class impl : public sys_time_ifc |
|
20 |
+{ |
|
21 |
+public: |
|
22 |
+ time_t time (time_t *timer) const override |
|
23 |
+ { |
|
24 |
+ return ::time (timer); |
|
25 |
+ } |
|
26 |
+}; |
|
13 | 27 |
} |
14 | 28 |
|
15 |
-const sys_time &sys_time::get() { |
|
16 |
- static sys_time singleton(std::make_shared<impl>()); |
|
29 |
+const sys_time &sys_time::get() |
|
30 |
+{ |
|
31 |
+ static sys_time singleton (std::make_shared<impl>()); |
|
17 | 32 |
return singleton; |
18 | 33 |
} |
34 |
+ |
... | ... |
@@ -1,27 +1,42 @@ |
1 |
+/* Copyright (C) CJ Affiliate |
|
2 |
+ * |
|
3 |
+ * You may use, distribute and modify this code under the |
|
4 |
+ * terms of the GNU General Public License version 2 or |
|
5 |
+ * later. |
|
6 |
+ * |
|
7 |
+ * You should have received a copy of the license with this |
|
8 |
+ * file. If not, you will find a copy in the "LICENSE" file |
|
9 |
+ * at https://github.com/cjdev/dual-control. |
|
10 |
+ */ |
|
11 |
+ |
|
1 | 12 |
#ifndef SYS_TIME_H_ |
2 | 13 |
#define SYS_TIME_H_ |
3 | 14 |
|
4 | 15 |
#include <ctime> |
5 | 16 |
#include <memory> |
6 | 17 |
|
7 |
-class sys_time_ifc { |
|
8 |
- public: |
|
9 |
- virtual time_t time(time_t *timer) const { |
|
10 |
- return 0; |
|
11 |
- } |
|
18 |
+class sys_time_ifc |
|
19 |
+{ |
|
20 |
+public: |
|
21 |
+ virtual time_t time (time_t *timer) const |
|
22 |
+ { |
|
23 |
+ return 0; |
|
24 |
+ } |
|
12 | 25 |
}; |
13 | 26 |
|
14 |
-class sys_time { |
|
15 |
- public: |
|
16 |
- using delegate = std::shared_ptr<sys_time_ifc>; |
|
17 |
- private: |
|
18 |
- delegate delegate_; |
|
19 |
- public: |
|
20 |
- sys_time(const delegate &delegate) : delegate_(delegate) {} |
|
21 |
- time_t time(time_t *timer) const { |
|
22 |
- return delegate_->time(timer); |
|
23 |
- } |
|
24 |
- static const sys_time &get(); |
|
27 |
+class sys_time |
|
28 |
+{ |
|
29 |
+public: |
|
30 |
+ using delegate = std::shared_ptr<sys_time_ifc>; |
|
31 |
+private: |
|
32 |
+ delegate delegate_; |
|
33 |
+public: |
|
34 |
+ sys_time (const delegate &delegate) : delegate_ (delegate) {} |
|
35 |
+ time_t time (time_t *timer) const |
|
36 |
+ { |
|
37 |
+ return delegate_->time (timer); |
|
38 |
+ } |
|
39 |
+ static const sys_time &get(); |
|
25 | 40 |
|
26 | 41 |
}; |
27 | 42 |
|
... | ... |
@@ -21,8 +21,9 @@ public: |
21 | 21 |
{ |
22 | 22 |
return ::sysconf (name); |
23 | 23 |
} |
24 |
- const char *getlogin() const override { |
|
25 |
- return ::getlogin(); |
|
24 |
+ const char *getlogin() const override |
|
25 |
+ { |
|
26 |
+ return ::getlogin(); |
|
26 | 27 |
} |
27 | 28 |
}; |
28 | 29 |
static unistd sys_unistd (unistd::delegate (new impl)); |
... | ... |
@@ -1,11 +1,23 @@ |
1 |
+/* Copyright (C) CJ Affiliate |
|
2 |
+ * |
|
3 |
+ * You may use, distribute and modify this code under the |
|
4 |
+ * terms of the GNU General Public License version 2 or |
|
5 |
+ * later. |
|
6 |
+ * |
|
7 |
+ * You should have received a copy of the license with this |
|
8 |
+ * file. If not, you will find a copy in the "LICENSE" file |
|
9 |
+ * at https://github.com/cjdev/dual-control. |
|
10 |
+ */ |
|
11 |
+ |
|
1 | 12 |
|
2 | 13 |
#include "system.h" |
3 | 14 |
#include "sys_stdlib.h" |
4 | 15 |
#include "sys_time.h" |
5 | 16 |
|
6 |
-system::system(const stdlib &stdlib, const sys_time &time) { |
|
7 |
- unsigned seed = static_cast<unsigned>(time.time(nullptr)); |
|
8 |
- stdlib.srand(seed); |
|
17 |
+system::system (const stdlib &stdlib, const sys_time &time) |
|
18 |
+{ |
|
19 |
+ unsigned seed = static_cast<unsigned> (time.time (nullptr)); |
|
20 |
+ stdlib.srand (seed); |
|
9 | 21 |
/* this makes it look like we're more random when the tool |
10 | 22 |
* is invoked. it isn't necessary and we shouldn't be using |
11 | 23 |
* rand() at all in the final version since it's kind of |
... | ... |
@@ -1,3 +1,14 @@ |
1 |
+/* Copyright (C) CJ Affiliate |
|
2 |
+ * |
|
3 |
+ * You may use, distribute and modify this code under the |
|
4 |
+ * terms of the GNU General Public License version 2 or |
|
5 |
+ * later. |
|
6 |
+ * |
|
7 |
+ * You should have received a copy of the license with this |
|
8 |
+ * file. If not, you will find a copy in the "LICENSE" file |
|
9 |
+ * at https://github.com/cjdev/dual-control. |
|
10 |
+ */ |
|
11 |
+ |
|
1 | 12 |
#ifndef SYSTEM_H_ |
2 | 13 |
#define SYSTEM_H_ |
3 | 14 |
|
... | ... |
@@ -6,10 +17,11 @@ |
6 | 17 |
#include "sys_time.h" |
7 | 18 |
#include "sys_stdlib.h" |
8 | 19 |
|
9 |
-class system { |
|
10 |
- public: |
|
11 |
- system(const stdlib &stdlib, const sys_time &time); |
|
20 |
+class system |
|
21 |
+{ |
|
22 |
+public: |
|
23 |
+ system (const stdlib &stdlib, const sys_time &time); |
|
12 | 24 |
}; |
13 | 25 |
|
14 |
- |
|
15 | 26 |
#endif |
27 |
+ |
... | ... |
@@ -1,3 +1,14 @@ |
1 |
+/* Copyright (C) CJ Affiliate |
|
2 |
+ * |
|
3 |
+ * You may use, distribute and modify this code under the |
|
4 |
+ * terms of the GNU General Public License version 2 or |
|
5 |
+ * later. |
|
6 |
+ * |
|
7 |
+ * You should have received a copy of the license with this |
|
8 |
+ * file. If not, you will find a copy in the "LICENSE" file |
|
9 |
+ * at https://github.com/cjdev/dual-control. |
|
10 |
+ */ |
|
11 |
+ |
|
1 | 12 |
#include <memory> |
2 | 13 |
#include <ctime> |
3 | 14 |
|
... | ... |
@@ -7,39 +18,42 @@ |
7 | 18 |
#include "sys_time.h" |
8 | 19 |
#include "sys_stdlib.h" |
9 | 20 |
|
10 |
-class mock_stdlib : public stdlib_ifc { |
|
11 |
- public: |
|
21 |
+class mock_stdlib : public stdlib_ifc |
|
22 |
+{ |
|
23 |
+public: |
|
12 | 24 |
mutable unsigned captured_seed {0}; |
13 |
- void srand(unsigned int seed) const { |
|
25 |
+ void srand (unsigned int seed) const |
|
26 |
+ { |
|
14 | 27 |
captured_seed = seed; |
15 | 28 |
} |
16 | 29 |
}; |
17 | 30 |
|
18 |
- |
|
19 |
-class fake_sys_time : public sys_time_ifc { |
|
20 |
- private: |
|
21 |
- unsigned value_; |
|
22 |
- public: |
|
23 |
- fake_sys_time(unsigned value) : value_(value) {} |
|
24 |
- time_t time(time_t *timer) const override { |
|
25 |
- return static_cast<time_t>(value_); |
|
26 |
- } |
|
31 |
+class fake_sys_time : public sys_time_ifc |
|
32 |
+{ |
|
33 |
+private: |
|
34 |
+ unsigned value_; |
|
35 |
+public: |
|
36 |
+ fake_sys_time (unsigned value) : value_ (value) {} |
|
37 |
+ time_t time (time_t *timer) const override |
|
38 |
+ { |
|
39 |
+ return static_cast<time_t> (value_); |
|
40 |
+ } |
|
27 | 41 |
}; |
28 | 42 |
|
29 |
-int initializes_random () { |
|
43 |
+int initializes_random () |
|
44 |
+{ |
|
30 | 45 |
// given |
31 |
- unsigned seed(20392); |
|
46 |
+ unsigned seed (20392); |
|
32 | 47 |
auto test_stdlib = std::make_shared<mock_stdlib>(); |
33 |
- stdlib stdlib(test_stdlib); |
|
34 |
- auto test_time = std::make_shared<fake_sys_time>(seed); |
|
35 |
- sys_time time(test_time); |
|
48 |
+ stdlib stdlib (test_stdlib); |
|
49 |
+ auto test_time = std::make_shared<fake_sys_time> (seed); |
|
50 |
+ sys_time time (test_time); |
|
36 | 51 |
|
37 | 52 |
// when |
38 |
- system system(stdlib, time); |
|
39 |
- |
|
53 |
+ system system (stdlib, time); |
|
40 | 54 |
|
41 | 55 |
// then |
42 |
- check(test_stdlib->captured_seed == seed, "seed not cpatured"); |
|
56 |
+ check (test_stdlib->captured_seed == seed, "seed not cpatured"); |
|
43 | 57 |
succeed(); |
44 | 58 |
} |
45 | 59 |
int run_tests() |