# HG changeset patch # User Brian Neal # Date 1341018490 18000 # Node ID db1216d380b388af0f5b7c1fd437b9d3141a32c5 # Parent 3370383116dba65bb1b1e095b63561aea2a32688 Added a set_rotor() that takes a string for convenience. diff -r 3370383116db -r db1216d380b3 enigma/machine.h --- 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 { diff -r 3370383116db -r db1216d380b3 enigma/tests/test_machine.t.h --- 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: