annotate enigma/tests/test_plugboard.t.h @ 4:2792ca4ffa84

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