bgneal@3
|
1 // Copyright (C) 2012 by Brian Neal.
|
bgneal@3
|
2 // This file is part of Cpp-Enigma, the Enigma Machine simulation.
|
bgneal@3
|
3 // Cpp-Enigma is released under the MIT License (see License.txt).
|
bgneal@3
|
4 //
|
bgneal@3
|
5 // test_plugboard.t.h - Unit tests for the plugboard class.
|
bgneal@3
|
6
|
bgneal@9
|
7 #include <algorithm>
|
bgneal@3
|
8 #include <cxxtest/TestSuite.h>
|
bgneal@3
|
9 #include "plugboard.h"
|
bgneal@3
|
10
|
bgneal@3
|
11 using namespace enigma;
|
bgneal@3
|
12
|
bgneal@3
|
13
|
bgneal@3
|
14 class plugboard_test_suite : public CxxTest::TestSuite
|
bgneal@3
|
15 {
|
bgneal@3
|
16 public:
|
bgneal@3
|
17
|
bgneal@3
|
18 void test_bad_settings()
|
bgneal@3
|
19 {
|
bgneal@3
|
20 // too many
|
bgneal@3
|
21 TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN OP QR ST UV"), plugboard_error);
|
bgneal@3
|
22 TS_ASSERT_THROWS(plugboard("18/26 17/4 21/6 3/16 19/14 22/7 8/1 12/25 5/9 10/15 2/20"),
|
bgneal@3
|
23 plugboard_error);
|
bgneal@3
|
24
|
bgneal@3
|
25 // duplicate
|
bgneal@3
|
26 TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN OF QR ST"), plugboard_error);
|
bgneal@3
|
27 TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN FP QR ST"), plugboard_error);
|
bgneal@3
|
28 TS_ASSERT_THROWS(plugboard("18/26 17/4 21/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error);
|
bgneal@3
|
29
|
bgneal@3
|
30 // invalid
|
bgneal@3
|
31 TS_ASSERT_THROWS(plugboard("A2 CD EF GH IJ KL MN FP QR ST"), plugboard_error);
|
bgneal@3
|
32 TS_ASSERT_THROWS(plugboard("AB CD EF *H IJ KL MN FP QR ST"), plugboard_error);
|
bgneal@3
|
33 TS_ASSERT_THROWS(plugboard("ABCD EF GH IJKLMN OP"), plugboard_error);
|
bgneal@3
|
34 TS_ASSERT_THROWS(plugboard("A-D EF GH OP"), plugboard_error);
|
bgneal@3
|
35 TS_ASSERT_THROWS(plugboard("A"), plugboard_error);
|
bgneal@3
|
36 TS_ASSERT_THROWS(plugboard("9"), plugboard_error);
|
bgneal@3
|
37 TS_ASSERT_THROWS(plugboard("1*/26 17/4 21/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error);
|
bgneal@3
|
38 TS_ASSERT_THROWS(plugboard("18/26 17/4 2A/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error);
|
bgneal@3
|
39 TS_ASSERT_THROWS(plugboard("100/2"), plugboard_error);
|
bgneal@3
|
40 TS_ASSERT_THROWS(plugboard("T/C"), plugboard_error);
|
bgneal@3
|
41 }
|
bgneal@3
|
42
|
bgneal@3
|
43 void test_valid_settings()
|
bgneal@3
|
44 {
|
bgneal@3
|
45 TS_ASSERT_THROWS_NOTHING(plugboard{});
|
bgneal@3
|
46 TS_ASSERT_THROWS_NOTHING(plugboard{""});
|
bgneal@3
|
47 TS_ASSERT_THROWS_NOTHING(plugboard(plugboard::pair_vector{}));
|
bgneal@3
|
48 TS_ASSERT_THROWS_NOTHING(plugboard{"AB CD EF GH IJ KL MN OP QR ST"});
|
bgneal@3
|
49 TS_ASSERT_THROWS_NOTHING(plugboard{"CD EF GH IJ KL MN OP QR ST"});
|
bgneal@3
|
50 TS_ASSERT_THROWS_NOTHING(plugboard{"EF GH IJ KL MN OP QR ST"});
|
bgneal@3
|
51 TS_ASSERT_THROWS_NOTHING(plugboard{" GH "});
|
bgneal@3
|
52 TS_ASSERT_THROWS_NOTHING(plugboard{"18/26 17/4 21/6 3/16 19/14 22/7 8/1 12/25 5/9 10/15"});
|
bgneal@3
|
53 TS_ASSERT_THROWS_NOTHING(plugboard{"18/26 17/4"});
|
bgneal@3
|
54 TS_ASSERT_THROWS_NOTHING(plugboard{" 18/26 "});
|
bgneal@3
|
55 }
|
bgneal@3
|
56
|
bgneal@3
|
57 void test_default_wiring()
|
bgneal@3
|
58 {
|
bgneal@3
|
59 plugboard p;
|
bgneal@3
|
60 for (int i = 0; i < 26; ++i)
|
bgneal@3
|
61 {
|
bgneal@3
|
62 TS_ASSERT_EQUALS(i, p.signal(i));
|
bgneal@3
|
63 }
|
bgneal@3
|
64 }
|
bgneal@3
|
65
|
bgneal@3
|
66 void test_wiring()
|
bgneal@3
|
67 {
|
bgneal@3
|
68 std::vector<std::string> settings{"AB CD EF GH IJ KL MN OP QR ST",
|
bgneal@3
|
69 "1/2 3/4 5/6 7/8 9/10 11/12 13/14 15/16 17/18 19/20"};
|
bgneal@3
|
70
|
bgneal@3
|
71 for (const auto& setting : settings)
|
bgneal@3
|
72 {
|
bgneal@3
|
73 plugboard p{setting};
|
bgneal@3
|
74 for (int n = 0; n < 26; ++n)
|
bgneal@3
|
75 {
|
bgneal@3
|
76 if (n < 20)
|
bgneal@3
|
77 {
|
bgneal@3
|
78 if (n % 2 == 0)
|
bgneal@3
|
79 {
|
bgneal@3
|
80 TS_ASSERT_EQUALS(p.signal(n), n + 1);
|
bgneal@3
|
81 }
|
bgneal@3
|
82 else
|
bgneal@3
|
83 {
|
bgneal@3
|
84 TS_ASSERT_EQUALS(p.signal(n), n - 1);
|
bgneal@3
|
85 }
|
bgneal@3
|
86 }
|
bgneal@3
|
87 else
|
bgneal@3
|
88 {
|
bgneal@3
|
89 TS_ASSERT_EQUALS(p.signal(n), n);
|
bgneal@3
|
90 }
|
bgneal@3
|
91 }
|
bgneal@3
|
92 }
|
bgneal@3
|
93 }
|
bgneal@3
|
94
|
bgneal@3
|
95 void test_wiring2()
|
bgneal@3
|
96 {
|
bgneal@3
|
97 std::string stecker{"AV BS CG DL FU HZ IN KM OW RX"};
|
bgneal@3
|
98 plugboard p{stecker};
|
bgneal@3
|
99 plugboard::pair_vector pairs{p.get_pairs()};
|
bgneal@3
|
100
|
bgneal@3
|
101 plugboard::pair_vector expected_pairs{
|
bgneal@3
|
102 {0, 21}, {1, 18}, {2, 6}, {3, 11}, {5, 20},
|
bgneal@3
|
103 {7, 25}, {8, 13}, {10, 12}, {14, 22}, {17, 23}
|
bgneal@3
|
104 };
|
bgneal@3
|
105
|
bgneal@3
|
106 TS_ASSERT_EQUALS(expected_pairs, pairs);
|
bgneal@3
|
107
|
bgneal@3
|
108 std::map<int, int> wiring;
|
bgneal@3
|
109 for (const auto& p : pairs)
|
bgneal@3
|
110 {
|
bgneal@3
|
111 wiring.insert(p);
|
bgneal@3
|
112 wiring.insert(std::make_pair(p.second, p.first));
|
bgneal@3
|
113 }
|
bgneal@3
|
114
|
bgneal@3
|
115 for (int n = 0; n < 26; ++n)
|
bgneal@3
|
116 {
|
bgneal@3
|
117 auto iter = wiring.find(n);
|
bgneal@3
|
118 if (iter != wiring.end())
|
bgneal@3
|
119 {
|
bgneal@3
|
120 TS_ASSERT_EQUALS(p.signal(n), iter->second);
|
bgneal@3
|
121 }
|
bgneal@3
|
122 else
|
bgneal@3
|
123 {
|
bgneal@3
|
124 TS_ASSERT_EQUALS(p.signal(n), n);
|
bgneal@3
|
125 }
|
bgneal@3
|
126 }
|
bgneal@3
|
127 }
|
bgneal@3
|
128
|
bgneal@3
|
129 void test_army_str()
|
bgneal@3
|
130 {
|
bgneal@3
|
131 std::string stecker{"AB CD EF GH IJ KL MN OP QR ST"};
|
bgneal@3
|
132 plugboard p{stecker};
|
bgneal@3
|
133 TS_ASSERT_EQUALS(stecker, p.army_str());
|
bgneal@3
|
134 }
|
bgneal@3
|
135
|
bgneal@3
|
136 void test_navy_str()
|
bgneal@3
|
137 {
|
bgneal@3
|
138 std::string stecker{"1/2 3/4 5/6 7/8 9/10 11/12 13/14 15/16 17/18 19/20"};
|
bgneal@3
|
139 plugboard p{stecker};
|
bgneal@3
|
140 TS_ASSERT_EQUALS(stecker, p.navy_str());
|
bgneal@3
|
141 }
|
bgneal@9
|
142
|
bgneal@9
|
143 void test_get_wiring()
|
bgneal@9
|
144 {
|
bgneal@9
|
145 plugboard pb;
|
bgneal@9
|
146 alpha_int_array w(pb.get_wiring());
|
bgneal@9
|
147
|
bgneal@9
|
148 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
149 {
|
bgneal@9
|
150 TS_ASSERT_EQUALS(w[i], i);
|
bgneal@9
|
151 }
|
bgneal@9
|
152
|
bgneal@9
|
153 pb.connect(0, 1);
|
bgneal@9
|
154 pb.connect(8, 20);
|
bgneal@9
|
155 pb.connect(24, 25);
|
bgneal@9
|
156 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
157 {
|
bgneal@9
|
158 w[i] = i;
|
bgneal@9
|
159 }
|
bgneal@9
|
160 std::swap(w[0], w[1]);
|
bgneal@9
|
161 std::swap(w[8], w[20]);
|
bgneal@9
|
162 std::swap(w[24], w[25]);
|
bgneal@9
|
163
|
bgneal@9
|
164 alpha_int_array w1(pb.get_wiring());
|
bgneal@9
|
165 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
166 {
|
bgneal@9
|
167 TS_ASSERT_EQUALS(w[i], w1[i]);
|
bgneal@9
|
168 }
|
bgneal@9
|
169 }
|
bgneal@9
|
170
|
bgneal@9
|
171 void test_set_wiring()
|
bgneal@9
|
172 {
|
bgneal@9
|
173 alpha_int_array w;
|
bgneal@9
|
174 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
175 {
|
bgneal@9
|
176 w[i] = i;
|
bgneal@9
|
177 }
|
bgneal@9
|
178 std::swap(w[0], w[1]);
|
bgneal@9
|
179 std::swap(w[8], w[20]);
|
bgneal@9
|
180 std::swap(w[24], w[25]);
|
bgneal@9
|
181
|
bgneal@9
|
182 plugboard pb;
|
bgneal@9
|
183 pb.set_wiring(w);
|
bgneal@9
|
184
|
bgneal@9
|
185 alpha_int_array w2 = pb.get_wiring();
|
bgneal@9
|
186 TS_ASSERT_EQUALS(w, w2);
|
bgneal@9
|
187 }
|
bgneal@9
|
188
|
bgneal@9
|
189 void test_is_wired()
|
bgneal@9
|
190 {
|
bgneal@9
|
191 plugboard pb;
|
bgneal@9
|
192 pb.connect(0, 25);
|
bgneal@9
|
193 pb.connect(4, 18);
|
bgneal@9
|
194 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
195 {
|
bgneal@9
|
196 if (i == 0 || i == 25 || i == 4 || i == 18)
|
bgneal@9
|
197 {
|
bgneal@9
|
198 TS_ASSERT(pb.is_wired(i));
|
bgneal@9
|
199 }
|
bgneal@9
|
200 else
|
bgneal@9
|
201 {
|
bgneal@9
|
202 TS_ASSERT(!pb.is_wired(i));
|
bgneal@9
|
203 }
|
bgneal@9
|
204 }
|
bgneal@9
|
205 }
|
bgneal@9
|
206
|
bgneal@9
|
207 void test_is_free()
|
bgneal@9
|
208 {
|
bgneal@9
|
209 plugboard pb;
|
bgneal@9
|
210 pb.connect(0, 25);
|
bgneal@9
|
211 pb.connect(4, 18);
|
bgneal@9
|
212 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
213 {
|
bgneal@9
|
214 if (i == 0 || i == 25 || i == 4 || i == 18)
|
bgneal@9
|
215 {
|
bgneal@9
|
216 TS_ASSERT(!pb.is_free(i));
|
bgneal@9
|
217 }
|
bgneal@9
|
218 else
|
bgneal@9
|
219 {
|
bgneal@9
|
220 TS_ASSERT(pb.is_free(i));
|
bgneal@9
|
221 }
|
bgneal@9
|
222 }
|
bgneal@9
|
223 }
|
bgneal@9
|
224
|
bgneal@9
|
225 void test_disconnect()
|
bgneal@9
|
226 {
|
bgneal@9
|
227 plugboard pb;
|
bgneal@9
|
228 pb.connect(0, 25);
|
bgneal@9
|
229 pb.connect(4, 18);
|
bgneal@9
|
230 pb.disconnect(0);
|
bgneal@9
|
231 pb.disconnect(18);
|
bgneal@9
|
232 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
233 {
|
bgneal@9
|
234 TS_ASSERT(pb.is_free(i));
|
bgneal@9
|
235 }
|
bgneal@9
|
236 }
|
bgneal@9
|
237
|
bgneal@9
|
238 void test_disconnect2()
|
bgneal@9
|
239 {
|
bgneal@9
|
240 plugboard pb;
|
bgneal@9
|
241 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
242 {
|
bgneal@9
|
243 pb.disconnect(i);
|
bgneal@9
|
244 }
|
bgneal@9
|
245 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
246 {
|
bgneal@9
|
247 TS_ASSERT(pb.is_free(i));
|
bgneal@9
|
248 }
|
bgneal@9
|
249 }
|
bgneal@9
|
250
|
bgneal@9
|
251 void test_connect()
|
bgneal@9
|
252 {
|
bgneal@9
|
253 plugboard pb;
|
bgneal@9
|
254 pb.connect(2, 4);
|
bgneal@9
|
255 pb.connect(17, 20);
|
bgneal@9
|
256 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
257 {
|
bgneal@9
|
258 if (i == 2 || i == 4 || i == 17 || i == 20)
|
bgneal@9
|
259 {
|
bgneal@9
|
260 TS_ASSERT(pb.is_wired(i));
|
bgneal@9
|
261 }
|
bgneal@9
|
262 else
|
bgneal@9
|
263 {
|
bgneal@9
|
264 TS_ASSERT(pb.is_free(i));
|
bgneal@9
|
265 }
|
bgneal@9
|
266 }
|
bgneal@9
|
267 pb.connect(20, 25);
|
bgneal@9
|
268 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
269 {
|
bgneal@9
|
270 if (i == 2 || i == 4 || i == 25 || i == 20)
|
bgneal@9
|
271 {
|
bgneal@9
|
272 TS_ASSERT(pb.is_wired(i));
|
bgneal@9
|
273 }
|
bgneal@9
|
274 else
|
bgneal@9
|
275 {
|
bgneal@9
|
276 TS_ASSERT(pb.is_free(i));
|
bgneal@9
|
277 }
|
bgneal@9
|
278 }
|
bgneal@9
|
279 }
|
bgneal@9
|
280
|
bgneal@9
|
281 void test_is_connected()
|
bgneal@9
|
282 {
|
bgneal@9
|
283 plugboard pb;
|
bgneal@9
|
284 pb.connect(5, 7);
|
bgneal@9
|
285 pb.connect(1, 20);
|
bgneal@9
|
286 TS_ASSERT(pb.is_connected(5, 7));
|
bgneal@9
|
287 TS_ASSERT(pb.is_connected(1, 20));
|
bgneal@9
|
288
|
bgneal@9
|
289 for (int i = 0; i < 26; ++i)
|
bgneal@9
|
290 {
|
bgneal@9
|
291 if (i == 7)
|
bgneal@9
|
292 {
|
bgneal@9
|
293 TS_ASSERT(pb.is_connected(5, i));
|
bgneal@9
|
294 }
|
bgneal@9
|
295 else
|
bgneal@9
|
296 {
|
bgneal@9
|
297 TS_ASSERT(!pb.is_connected(5, i));
|
bgneal@9
|
298 }
|
bgneal@9
|
299 if (i == 1)
|
bgneal@9
|
300 {
|
bgneal@9
|
301 TS_ASSERT(pb.is_connected(20, i));
|
bgneal@9
|
302 }
|
bgneal@9
|
303 else
|
bgneal@9
|
304 {
|
bgneal@9
|
305 TS_ASSERT(!pb.is_connected(20, i));
|
bgneal@9
|
306 }
|
bgneal@9
|
307 }
|
bgneal@9
|
308 }
|
bgneal@9
|
309
|
bgneal@10
|
310 void test_unplug_all()
|
bgneal@10
|
311 {
|
bgneal@10
|
312 plugboard pb;
|
bgneal@10
|
313 pb.connect(5, 7);
|
bgneal@10
|
314 pb.connect(1, 20);
|
bgneal@10
|
315 pb.connect(18, 24);
|
bgneal@10
|
316 pb.unplug_all();
|
bgneal@10
|
317 for (int i = 0; i < 26; ++i)
|
bgneal@10
|
318 {
|
bgneal@10
|
319 TS_ASSERT(pb.is_free(i));
|
bgneal@10
|
320 }
|
bgneal@10
|
321 pb.unplug_all();
|
bgneal@10
|
322 for (int i = 0; i < 26; ++i)
|
bgneal@10
|
323 {
|
bgneal@10
|
324 TS_ASSERT(pb.is_free(i));
|
bgneal@10
|
325 }
|
bgneal@10
|
326 }
|
bgneal@10
|
327
|
bgneal@3
|
328 };
|