bgneal@3: // Copyright (C) 2012 by Brian Neal. bgneal@3: // This file is part of Cpp-Enigma, the Enigma Machine simulation. bgneal@3: // Cpp-Enigma is released under the MIT License (see License.txt). bgneal@3: // bgneal@3: // test_plugboard.t.h - Unit tests for the plugboard class. bgneal@3: bgneal@9: #include bgneal@3: #include bgneal@3: #include "plugboard.h" bgneal@3: bgneal@3: using namespace enigma; bgneal@3: bgneal@3: bgneal@3: class plugboard_test_suite : public CxxTest::TestSuite bgneal@3: { bgneal@3: public: bgneal@3: bgneal@3: void test_bad_settings() bgneal@3: { bgneal@3: // too many bgneal@3: TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN OP QR ST UV"), plugboard_error); bgneal@3: 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: plugboard_error); bgneal@3: bgneal@3: // duplicate bgneal@3: TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN OF QR ST"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN FP QR ST"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("18/26 17/4 21/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error); bgneal@3: bgneal@3: // invalid bgneal@3: TS_ASSERT_THROWS(plugboard("A2 CD EF GH IJ KL MN FP QR ST"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("AB CD EF *H IJ KL MN FP QR ST"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("ABCD EF GH IJKLMN OP"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("A-D EF GH OP"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("A"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("9"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("1*/26 17/4 21/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("18/26 17/4 2A/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("100/2"), plugboard_error); bgneal@3: TS_ASSERT_THROWS(plugboard("T/C"), plugboard_error); bgneal@3: } bgneal@3: bgneal@3: void test_valid_settings() bgneal@3: { bgneal@3: TS_ASSERT_THROWS_NOTHING(plugboard{}); bgneal@3: TS_ASSERT_THROWS_NOTHING(plugboard{""}); bgneal@3: TS_ASSERT_THROWS_NOTHING(plugboard(plugboard::pair_vector{})); bgneal@3: TS_ASSERT_THROWS_NOTHING(plugboard{"AB CD EF GH IJ KL MN OP QR ST"}); bgneal@3: TS_ASSERT_THROWS_NOTHING(plugboard{"CD EF GH IJ KL MN OP QR ST"}); bgneal@3: TS_ASSERT_THROWS_NOTHING(plugboard{"EF GH IJ KL MN OP QR ST"}); bgneal@3: TS_ASSERT_THROWS_NOTHING(plugboard{" GH "}); bgneal@3: 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: TS_ASSERT_THROWS_NOTHING(plugboard{"18/26 17/4"}); bgneal@3: TS_ASSERT_THROWS_NOTHING(plugboard{" 18/26 "}); bgneal@3: } bgneal@3: bgneal@3: void test_default_wiring() bgneal@3: { bgneal@3: plugboard p; bgneal@3: for (int i = 0; i < 26; ++i) bgneal@3: { bgneal@3: TS_ASSERT_EQUALS(i, p.signal(i)); bgneal@3: } bgneal@3: } bgneal@3: bgneal@3: void test_wiring() bgneal@3: { bgneal@3: std::vector settings{"AB CD EF GH IJ KL MN OP QR ST", bgneal@3: "1/2 3/4 5/6 7/8 9/10 11/12 13/14 15/16 17/18 19/20"}; bgneal@3: bgneal@3: for (const auto& setting : settings) bgneal@3: { bgneal@3: plugboard p{setting}; bgneal@3: for (int n = 0; n < 26; ++n) bgneal@3: { bgneal@3: if (n < 20) bgneal@3: { bgneal@3: if (n % 2 == 0) bgneal@3: { bgneal@3: TS_ASSERT_EQUALS(p.signal(n), n + 1); bgneal@3: } bgneal@3: else bgneal@3: { bgneal@3: TS_ASSERT_EQUALS(p.signal(n), n - 1); bgneal@3: } bgneal@3: } bgneal@3: else bgneal@3: { bgneal@3: TS_ASSERT_EQUALS(p.signal(n), n); bgneal@3: } bgneal@3: } bgneal@3: } bgneal@3: } bgneal@3: bgneal@3: void test_wiring2() bgneal@3: { bgneal@3: std::string stecker{"AV BS CG DL FU HZ IN KM OW RX"}; bgneal@3: plugboard p{stecker}; bgneal@3: plugboard::pair_vector pairs{p.get_pairs()}; bgneal@3: bgneal@3: plugboard::pair_vector expected_pairs{ bgneal@3: {0, 21}, {1, 18}, {2, 6}, {3, 11}, {5, 20}, bgneal@3: {7, 25}, {8, 13}, {10, 12}, {14, 22}, {17, 23} bgneal@3: }; bgneal@3: bgneal@3: TS_ASSERT_EQUALS(expected_pairs, pairs); bgneal@3: bgneal@3: std::map wiring; bgneal@3: for (const auto& p : pairs) bgneal@3: { bgneal@3: wiring.insert(p); bgneal@3: wiring.insert(std::make_pair(p.second, p.first)); bgneal@3: } bgneal@3: bgneal@3: for (int n = 0; n < 26; ++n) bgneal@3: { bgneal@3: auto iter = wiring.find(n); bgneal@3: if (iter != wiring.end()) bgneal@3: { bgneal@3: TS_ASSERT_EQUALS(p.signal(n), iter->second); bgneal@3: } bgneal@3: else bgneal@3: { bgneal@3: TS_ASSERT_EQUALS(p.signal(n), n); bgneal@3: } bgneal@3: } bgneal@3: } bgneal@3: bgneal@3: void test_army_str() bgneal@3: { bgneal@3: std::string stecker{"AB CD EF GH IJ KL MN OP QR ST"}; bgneal@3: plugboard p{stecker}; bgneal@3: TS_ASSERT_EQUALS(stecker, p.army_str()); bgneal@3: } bgneal@3: bgneal@3: void test_navy_str() bgneal@3: { bgneal@3: 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: plugboard p{stecker}; bgneal@3: TS_ASSERT_EQUALS(stecker, p.navy_str()); bgneal@3: } bgneal@9: bgneal@9: void test_get_wiring() bgneal@9: { bgneal@9: plugboard pb; bgneal@9: alpha_int_array w(pb.get_wiring()); bgneal@9: bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: TS_ASSERT_EQUALS(w[i], i); bgneal@9: } bgneal@9: bgneal@9: pb.connect(0, 1); bgneal@9: pb.connect(8, 20); bgneal@9: pb.connect(24, 25); bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: w[i] = i; bgneal@9: } bgneal@9: std::swap(w[0], w[1]); bgneal@9: std::swap(w[8], w[20]); bgneal@9: std::swap(w[24], w[25]); bgneal@9: bgneal@9: alpha_int_array w1(pb.get_wiring()); bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: TS_ASSERT_EQUALS(w[i], w1[i]); bgneal@9: } bgneal@9: } bgneal@9: bgneal@9: void test_set_wiring() bgneal@9: { bgneal@9: alpha_int_array w; bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: w[i] = i; bgneal@9: } bgneal@9: std::swap(w[0], w[1]); bgneal@9: std::swap(w[8], w[20]); bgneal@9: std::swap(w[24], w[25]); bgneal@9: bgneal@9: plugboard pb; bgneal@9: pb.set_wiring(w); bgneal@9: bgneal@9: alpha_int_array w2 = pb.get_wiring(); bgneal@9: TS_ASSERT_EQUALS(w, w2); bgneal@9: } bgneal@9: bgneal@9: void test_is_wired() bgneal@9: { bgneal@9: plugboard pb; bgneal@9: pb.connect(0, 25); bgneal@9: pb.connect(4, 18); bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: if (i == 0 || i == 25 || i == 4 || i == 18) bgneal@9: { bgneal@9: TS_ASSERT(pb.is_wired(i)); bgneal@9: } bgneal@9: else bgneal@9: { bgneal@9: TS_ASSERT(!pb.is_wired(i)); bgneal@9: } bgneal@9: } bgneal@9: } bgneal@9: bgneal@9: void test_is_free() bgneal@9: { bgneal@9: plugboard pb; bgneal@9: pb.connect(0, 25); bgneal@9: pb.connect(4, 18); bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: if (i == 0 || i == 25 || i == 4 || i == 18) bgneal@9: { bgneal@9: TS_ASSERT(!pb.is_free(i)); bgneal@9: } bgneal@9: else bgneal@9: { bgneal@9: TS_ASSERT(pb.is_free(i)); bgneal@9: } bgneal@9: } bgneal@9: } bgneal@9: bgneal@9: void test_disconnect() bgneal@9: { bgneal@9: plugboard pb; bgneal@9: pb.connect(0, 25); bgneal@9: pb.connect(4, 18); bgneal@9: pb.disconnect(0); bgneal@9: pb.disconnect(18); bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: TS_ASSERT(pb.is_free(i)); bgneal@9: } bgneal@9: } bgneal@9: bgneal@9: void test_disconnect2() bgneal@9: { bgneal@9: plugboard pb; bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: pb.disconnect(i); bgneal@9: } bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: TS_ASSERT(pb.is_free(i)); bgneal@9: } bgneal@9: } bgneal@9: bgneal@9: void test_connect() bgneal@9: { bgneal@9: plugboard pb; bgneal@9: pb.connect(2, 4); bgneal@9: pb.connect(17, 20); bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: if (i == 2 || i == 4 || i == 17 || i == 20) bgneal@9: { bgneal@9: TS_ASSERT(pb.is_wired(i)); bgneal@9: } bgneal@9: else bgneal@9: { bgneal@9: TS_ASSERT(pb.is_free(i)); bgneal@9: } bgneal@9: } bgneal@9: pb.connect(20, 25); bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: if (i == 2 || i == 4 || i == 25 || i == 20) bgneal@9: { bgneal@9: TS_ASSERT(pb.is_wired(i)); bgneal@9: } bgneal@9: else bgneal@9: { bgneal@9: TS_ASSERT(pb.is_free(i)); bgneal@9: } bgneal@9: } bgneal@9: } bgneal@9: bgneal@9: void test_is_connected() bgneal@9: { bgneal@9: plugboard pb; bgneal@9: pb.connect(5, 7); bgneal@9: pb.connect(1, 20); bgneal@9: TS_ASSERT(pb.is_connected(5, 7)); bgneal@9: TS_ASSERT(pb.is_connected(1, 20)); bgneal@9: bgneal@9: for (int i = 0; i < 26; ++i) bgneal@9: { bgneal@9: if (i == 7) bgneal@9: { bgneal@9: TS_ASSERT(pb.is_connected(5, i)); bgneal@9: } bgneal@9: else bgneal@9: { bgneal@9: TS_ASSERT(!pb.is_connected(5, i)); bgneal@9: } bgneal@9: if (i == 1) bgneal@9: { bgneal@9: TS_ASSERT(pb.is_connected(20, i)); bgneal@9: } bgneal@9: else bgneal@9: { bgneal@9: TS_ASSERT(!pb.is_connected(20, i)); bgneal@9: } bgneal@9: } bgneal@9: } bgneal@9: bgneal@3: };