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@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@3: };