Mercurial > public > enigma
changeset 10:9ca7c7a93fc2
Added a test for "double stepping".
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 26 May 2012 21:43:03 -0500 |
parents | f6edbbd35b92 |
children | 845896830342 |
files | enigma/machine.py enigma/tests/test_enigma.py |
diffstat | 2 files changed, 43 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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.
--- /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) +