Mercurial > public > cpp-enigma
comparison enigma/tests/test_plugboard.t.h @ 3:f4e25e6b76c3
Created plugboard class and tests.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 23 Jun 2012 23:28:17 -0500 |
parents | |
children | 7362965f53b1 |
comparison
equal
deleted
inserted
replaced
2:713fa2a9ea9a | 3:f4e25e6b76c3 |
---|---|
1 // Copyright (C) 2012 by Brian Neal. | |
2 // This file is part of Cpp-Enigma, the Enigma Machine simulation. | |
3 // Cpp-Enigma is released under the MIT License (see License.txt). | |
4 // | |
5 // test_plugboard.t.h - Unit tests for the plugboard class. | |
6 | |
7 #include <cxxtest/TestSuite.h> | |
8 #include "plugboard.h" | |
9 | |
10 using namespace enigma; | |
11 | |
12 | |
13 class plugboard_test_suite : public CxxTest::TestSuite | |
14 { | |
15 public: | |
16 | |
17 void test_bad_settings() | |
18 { | |
19 // too many | |
20 TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN OP QR ST UV"), plugboard_error); | |
21 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"), | |
22 plugboard_error); | |
23 | |
24 // duplicate | |
25 TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN OF QR ST"), plugboard_error); | |
26 TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN FP QR ST"), plugboard_error); | |
27 TS_ASSERT_THROWS(plugboard("18/26 17/4 21/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error); | |
28 | |
29 // invalid | |
30 TS_ASSERT_THROWS(plugboard("A2 CD EF GH IJ KL MN FP QR ST"), plugboard_error); | |
31 TS_ASSERT_THROWS(plugboard("AB CD EF *H IJ KL MN FP QR ST"), plugboard_error); | |
32 TS_ASSERT_THROWS(plugboard("ABCD EF GH IJKLMN OP"), plugboard_error); | |
33 TS_ASSERT_THROWS(plugboard("A-D EF GH OP"), plugboard_error); | |
34 TS_ASSERT_THROWS(plugboard("A"), plugboard_error); | |
35 TS_ASSERT_THROWS(plugboard("9"), plugboard_error); | |
36 TS_ASSERT_THROWS(plugboard("1*/26 17/4 21/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error); | |
37 TS_ASSERT_THROWS(plugboard("18/26 17/4 2A/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error); | |
38 TS_ASSERT_THROWS(plugboard("100/2"), plugboard_error); | |
39 TS_ASSERT_THROWS(plugboard("T/C"), plugboard_error); | |
40 } | |
41 | |
42 void test_valid_settings() | |
43 { | |
44 TS_ASSERT_THROWS_NOTHING(plugboard{}); | |
45 TS_ASSERT_THROWS_NOTHING(plugboard{""}); | |
46 TS_ASSERT_THROWS_NOTHING(plugboard(plugboard::pair_vector{})); | |
47 TS_ASSERT_THROWS_NOTHING(plugboard{"AB CD EF GH IJ KL MN OP QR ST"}); | |
48 TS_ASSERT_THROWS_NOTHING(plugboard{"CD EF GH IJ KL MN OP QR ST"}); | |
49 TS_ASSERT_THROWS_NOTHING(plugboard{"EF GH IJ KL MN OP QR ST"}); | |
50 TS_ASSERT_THROWS_NOTHING(plugboard{" GH "}); | |
51 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"}); | |
52 TS_ASSERT_THROWS_NOTHING(plugboard{"18/26 17/4"}); | |
53 TS_ASSERT_THROWS_NOTHING(plugboard{" 18/26 "}); | |
54 } | |
55 | |
56 void test_default_wiring() | |
57 { | |
58 plugboard p; | |
59 for (int i = 0; i < 26; ++i) | |
60 { | |
61 TS_ASSERT_EQUALS(i, p.signal(i)); | |
62 } | |
63 } | |
64 | |
65 void test_wiring() | |
66 { | |
67 std::vector<std::string> settings{"AB CD EF GH IJ KL MN OP QR ST", | |
68 "1/2 3/4 5/6 7/8 9/10 11/12 13/14 15/16 17/18 19/20"}; | |
69 | |
70 for (const auto& setting : settings) | |
71 { | |
72 plugboard p{setting}; | |
73 for (int n = 0; n < 26; ++n) | |
74 { | |
75 if (n < 20) | |
76 { | |
77 if (n % 2 == 0) | |
78 { | |
79 TS_ASSERT_EQUALS(p.signal(n), n + 1); | |
80 } | |
81 else | |
82 { | |
83 TS_ASSERT_EQUALS(p.signal(n), n - 1); | |
84 } | |
85 } | |
86 else | |
87 { | |
88 TS_ASSERT_EQUALS(p.signal(n), n); | |
89 } | |
90 } | |
91 } | |
92 } | |
93 | |
94 void test_wiring2() | |
95 { | |
96 std::string stecker{"AV BS CG DL FU HZ IN KM OW RX"}; | |
97 plugboard p{stecker}; | |
98 plugboard::pair_vector pairs{p.get_pairs()}; | |
99 | |
100 plugboard::pair_vector expected_pairs{ | |
101 {0, 21}, {1, 18}, {2, 6}, {3, 11}, {5, 20}, | |
102 {7, 25}, {8, 13}, {10, 12}, {14, 22}, {17, 23} | |
103 }; | |
104 | |
105 TS_ASSERT_EQUALS(expected_pairs, pairs); | |
106 | |
107 std::map<int, int> wiring; | |
108 for (const auto& p : pairs) | |
109 { | |
110 wiring.insert(p); | |
111 wiring.insert(std::make_pair(p.second, p.first)); | |
112 } | |
113 | |
114 for (int n = 0; n < 26; ++n) | |
115 { | |
116 auto iter = wiring.find(n); | |
117 if (iter != wiring.end()) | |
118 { | |
119 TS_ASSERT_EQUALS(p.signal(n), iter->second); | |
120 } | |
121 else | |
122 { | |
123 TS_ASSERT_EQUALS(p.signal(n), n); | |
124 } | |
125 } | |
126 } | |
127 | |
128 void test_army_str() | |
129 { | |
130 std::string stecker{"AB CD EF GH IJ KL MN OP QR ST"}; | |
131 plugboard p{stecker}; | |
132 TS_ASSERT_EQUALS(stecker, p.army_str()); | |
133 } | |
134 | |
135 void test_navy_str() | |
136 { | |
137 std::string stecker{"1/2 3/4 5/6 7/8 9/10 11/12 13/14 15/16 17/18 19/20"}; | |
138 plugboard p{stecker}; | |
139 TS_ASSERT_EQUALS(stecker, p.navy_str()); | |
140 } | |
141 }; |