view enigma/tests/test_plugboard.t.h @ 8:b90a41f0cd94

Created enigma_machine::army_str() & navy_str() functions for logging.
author Brian Neal <bgneal@gmail.com>
date Fri, 29 Jun 2012 20:29:41 -0500
parents f4e25e6b76c3
children 7362965f53b1
line wrap: on
line source
// Copyright (C) 2012 by Brian Neal.
// This file is part of Cpp-Enigma, the Enigma Machine simulation.
// Cpp-Enigma is released under the MIT License (see License.txt).
//
// test_plugboard.t.h - Unit tests for the plugboard class.

#include <cxxtest/TestSuite.h>
#include "plugboard.h"

using namespace enigma;


class plugboard_test_suite : public CxxTest::TestSuite
{
public:

   void test_bad_settings()
   {
      // too many
      TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN OP QR ST UV"), plugboard_error);
      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"),
                       plugboard_error);

      // duplicate
      TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN OF QR ST"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("AB CD EF GH IJ KL MN FP QR ST"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("18/26 17/4 21/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error);

      // invalid
      TS_ASSERT_THROWS(plugboard("A2 CD EF GH IJ KL MN FP QR ST"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("AB CD EF *H IJ KL MN FP QR ST"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("ABCD EF GH IJKLMN OP"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("A-D EF GH OP"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("A"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("9"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("1*/26 17/4 21/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("18/26 17/4 2A/6 3/16 19/14 22/3 8/1 12/25"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("100/2"), plugboard_error);
      TS_ASSERT_THROWS(plugboard("T/C"), plugboard_error);
   }

   void test_valid_settings()
   {
      TS_ASSERT_THROWS_NOTHING(plugboard{});
      TS_ASSERT_THROWS_NOTHING(plugboard{""});
      TS_ASSERT_THROWS_NOTHING(plugboard(plugboard::pair_vector{}));
      TS_ASSERT_THROWS_NOTHING(plugboard{"AB CD EF GH IJ KL MN OP QR ST"});
      TS_ASSERT_THROWS_NOTHING(plugboard{"CD EF GH IJ KL MN OP QR ST"});
      TS_ASSERT_THROWS_NOTHING(plugboard{"EF GH IJ KL MN OP QR ST"});
      TS_ASSERT_THROWS_NOTHING(plugboard{" GH "});
      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"});
      TS_ASSERT_THROWS_NOTHING(plugboard{"18/26 17/4"});
      TS_ASSERT_THROWS_NOTHING(plugboard{" 18/26 "});
   }

   void test_default_wiring()
   {
      plugboard p;
      for (int i = 0; i < 26; ++i)
      {
         TS_ASSERT_EQUALS(i, p.signal(i));
      }
   }

   void test_wiring()
   {
      std::vector<std::string> settings{"AB CD EF GH IJ KL MN OP QR ST",
                  "1/2 3/4 5/6 7/8 9/10 11/12 13/14 15/16 17/18 19/20"};

      for (const auto& setting : settings)
      {
         plugboard p{setting};
         for (int n = 0; n < 26; ++n)
         {
            if (n < 20)
            {
               if (n % 2 == 0)
               {
                  TS_ASSERT_EQUALS(p.signal(n), n + 1);
               }
               else
               {
                  TS_ASSERT_EQUALS(p.signal(n), n - 1);
               }
            }
            else
            {
               TS_ASSERT_EQUALS(p.signal(n), n);
            }
         }
      }
   }

   void test_wiring2()
   {
      std::string stecker{"AV BS CG DL FU HZ IN KM OW RX"};
      plugboard p{stecker};
      plugboard::pair_vector pairs{p.get_pairs()};

      plugboard::pair_vector expected_pairs{
         {0, 21}, {1, 18}, {2, 6}, {3, 11}, {5, 20},
         {7, 25}, {8, 13}, {10, 12}, {14, 22}, {17, 23}
      };

      TS_ASSERT_EQUALS(expected_pairs, pairs);

      std::map<int, int> wiring;
      for (const auto& p : pairs)
      {
         wiring.insert(p);
         wiring.insert(std::make_pair(p.second, p.first));
      }

      for (int n = 0; n < 26; ++n)
      {
         auto iter = wiring.find(n);
         if (iter != wiring.end())
         {
            TS_ASSERT_EQUALS(p.signal(n), iter->second);
         }
         else
         {
            TS_ASSERT_EQUALS(p.signal(n), n);
         }
      }
   }

   void test_army_str()
   {
      std::string stecker{"AB CD EF GH IJ KL MN OP QR ST"};
      plugboard p{stecker};
      TS_ASSERT_EQUALS(stecker, p.army_str());
   }

   void test_navy_str()
   {
      std::string stecker{"1/2 3/4 5/6 7/8 9/10 11/12 13/14 15/16 17/18 19/20"};
      plugboard p{stecker};
      TS_ASSERT_EQUALS(stecker, p.navy_str());
   }
};