# HG changeset patch # User Brian Neal # Date 1338086583 18000 # Node ID 9ca7c7a93fc270ec3f211c4d33176e54fc9e21ef # Parent f6edbbd35b9277fc0fad20375a061b6703c0dd46 Added a test for "double stepping". diff -r f6edbbd35b92 -r 9ca7c7a93fc2 enigma/machine.py --- a/enigma/machine.py Sat May 26 20:05:07 2012 -0500 +++ b/enigma/machine.py Sat May 26 21:43:03 2012 -0500 @@ -57,6 +57,13 @@ for i, r in enumerate(range(start, self.rotor_count)): self.rotors[r].set_display(val[i]) + def get_display(self): + """Returns the operator display as a string.""" + + return "{}{}{}".format(self.rotors[-3].get_display(), + self.rotors[-2].get_display(), + self.rotors[-1].get_display()) + def key_press(self, key): """Simulate a front panel key press. diff -r f6edbbd35b92 -r 9ca7c7a93fc2 enigma/tests/test_enigma.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/enigma/tests/test_enigma.py Sat May 26 21:43:03 2012 -0500 @@ -0,0 +1,36 @@ +# Copyright (C) 2012 by Brian Neal. +# This file is part of Py-Enigma, the Enigma Machine simulation. +# Py-Enigma is released under the MIT License (see License.txt). + +"""Tests for the EnigmaMachine class.""" + +import unittest + +from ..rotors.factory import create_rotor, create_reflector +from ..machine import EnigmaMachine + + +class EnigmaMachineTestCase(unittest.TestCase): + + def test_double_stepping(self): + """Ensure the rotors step realistically by testing for a "double-step" + case. + + """ + # This example taken from + # http://users.telenet.be/d.rijmenants/en/enigmatech.htm + # in the section on "The Stepping Mechanism." + rotors = [] + rotors.append(create_rotor("III")) + rotors.append(create_rotor("II")) + rotors.append(create_rotor("I")) + + m = EnigmaMachine(rotors, create_reflector('B')) + + m.set_display('KDO') + + truth_data = ['KDP', 'KDQ', 'KER', 'LFS', 'LFT', 'LFU'] + for expected in truth_data: + m.key_press('A') + self.assertEqual(m.get_display(), expected) +