changeset 7:db1216d380b3

Added a set_rotor() that takes a string for convenience.
author Brian Neal <bgneal@gmail.com>
date Fri, 29 Jun 2012 20:08:10 -0500
parents 3370383116db
children b90a41f0cd94
files enigma/machine.h enigma/tests/test_machine.t.h
diffstat 2 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/enigma/machine.h	Tue Jun 26 21:22:42 2012 -0500
+++ b/enigma/machine.h	Fri Jun 29 20:08:10 2012 -0500
@@ -65,6 +65,25 @@
          rotors[3]->set_display(c3);
       }
 
+      // Set the rotor display (starting position) using a string; the
+      // string length must match the number of rotors in use or a
+      // enigma_machine_error exception will be thrown:
+      void set_display(const std::string& val)
+      {
+         if (val.size() == 3 && rotors.size() == 3)
+         {
+            set_display(val[0], val[1], val[2]);
+         }
+         else if (val.size() == 4 && rotors.size() == 4)
+         {
+            set_display(val[0], val[1], val[2], val[3]);
+         }
+         else
+         {
+            throw enigma_machine_error("set_display invalid size");
+         }
+      }
+
       // return the rotor display (starting position) as a string
       std::string get_display() const
       {
--- a/enigma/tests/test_machine.t.h	Tue Jun 26 21:22:42 2012 -0500
+++ b/enigma/tests/test_machine.t.h	Fri Jun 29 20:08:10 2012 -0500
@@ -14,6 +14,25 @@
 using namespace enigma;
 
 
+class display_suite : public CxxTest::TestSuite
+{
+public:
+
+   void test_set_display3()
+   {
+      enigma_machine m({"II", "IV", "V"}, {}, "B");
+      TS_ASSERT_THROWS(m.set_display("ABCD"), enigma_machine_error);
+      TS_ASSERT_THROWS_NOTHING(m.set_display("ABC"));
+   }
+
+   void test_set_display4()
+   {
+      enigma_machine m({"Gamma", "II", "IV", "V"}, {}, "B-Thin");
+      TS_ASSERT_THROWS(m.set_display("BCD"), enigma_machine_error);
+      TS_ASSERT_THROWS_NOTHING(m.set_display("ABCD"));
+   }
+};
+
 class stepping_test_suite : public CxxTest::TestSuite
 {
 public: