bgneal@1
|
1 # Copyright (C) 2012 by Brian Neal.
|
bgneal@1
|
2 # This file is part of Py-Enigma, the Enigma Machine simulation.
|
bgneal@1
|
3 # Py-Enigma is released under the MIT License (see License.txt).
|
bgneal@1
|
4
|
bgneal@4
|
5 """test_rotor.py - Unit tests for the Rotor class for the Enigma simulation."""
|
bgneal@4
|
6
|
bgneal@1
|
7 import unittest
|
bgneal@2
|
8 import collections
|
bgneal@1
|
9 import string
|
bgneal@1
|
10
|
bgneal@6
|
11 from ..rotors.rotor import Rotor, ALPHA_LABELS, NUMERIC_LABELS
|
bgneal@6
|
12 from ..rotors import RotorError
|
bgneal@1
|
13
|
bgneal@1
|
14
|
bgneal@1
|
15 WIRING = 'EKMFLGDQVZNTOWYHXUSPAIBRCJ'
|
bgneal@1
|
16
|
bgneal@1
|
17
|
bgneal@1
|
18 class SimpleRotorTestCase(unittest.TestCase):
|
bgneal@1
|
19 """Basic tests to verify Rotor functionality"""
|
bgneal@1
|
20
|
bgneal@1
|
21 def test_bad_wiring(self):
|
bgneal@1
|
22
|
bgneal@1
|
23 self.assertRaises(RotorError, Rotor, 'I', '')
|
bgneal@1
|
24 self.assertRaises(RotorError, Rotor, 'I', 'ABC')
|
bgneal@1
|
25 self.assertRaises(RotorError, Rotor, 'I', '123')
|
bgneal@1
|
26
|
bgneal@1
|
27 w = string.punctuation[:26]
|
bgneal@1
|
28 self.assertRaises(RotorError, Rotor, 'I', w)
|
bgneal@1
|
29
|
bgneal@1
|
30 w = 'ABCD' * 7
|
bgneal@1
|
31 self.assertRaises(RotorError, Rotor, 'III', w[:26])
|
bgneal@1
|
32
|
bgneal@1
|
33 def test_bad_ring_setting(self):
|
bgneal@1
|
34
|
bgneal@1
|
35 self.assertRaises(RotorError, Rotor, 'I', WIRING, ring_setting=-1)
|
bgneal@1
|
36 self.assertRaises(RotorError, Rotor, 'I', WIRING, ring_setting=26)
|
bgneal@1
|
37 self.assertRaises(RotorError, Rotor, 'I', WIRING, ring_setting='A')
|
bgneal@1
|
38 self.assertRaises(RotorError, Rotor, 'I', WIRING, ring_setting=None)
|
bgneal@1
|
39
|
bgneal@1
|
40 def test_bad_stepping(self):
|
bgneal@1
|
41
|
bgneal@1
|
42 for alpha in True, False:
|
bgneal@1
|
43 self.assertRaises(RotorError, Rotor, 'I', WIRING,
|
bgneal@1
|
44 alpha_labels=alpha, stepping="0")
|
bgneal@1
|
45 self.assertRaises(RotorError, Rotor, 'I', WIRING,
|
bgneal@1
|
46 alpha_labels=alpha, stepping="A0")
|
bgneal@1
|
47 self.assertRaises(RotorError, Rotor, 'I', WIRING,
|
bgneal@1
|
48 alpha_labels=alpha, stepping=[1])
|
bgneal@1
|
49 self.assertRaises(RotorError, Rotor, 'I', WIRING,
|
bgneal@1
|
50 alpha_labels=alpha, stepping=['A', '%', '14'])
|
bgneal@1
|
51 self.assertRaises(RotorError, Rotor, 'I', WIRING,
|
bgneal@1
|
52 alpha_labels=alpha, stepping=('A', '%', '14'))
|
bgneal@1
|
53
|
bgneal@1
|
54 def test_alpha_display(self):
|
bgneal@1
|
55
|
bgneal@1
|
56 for r in range(26):
|
bgneal@1
|
57 rotor = Rotor('I', WIRING, ring_setting=r, alpha_labels=True)
|
bgneal@1
|
58 for s in ALPHA_LABELS:
|
bgneal@1
|
59 rotor.set_display(s)
|
bgneal@1
|
60 self.assertEqual(s, rotor.get_display())
|
bgneal@1
|
61
|
bgneal@1
|
62 def test_numeric_display(self):
|
bgneal@1
|
63
|
bgneal@1
|
64 for r in range(26):
|
bgneal@1
|
65 rotor = Rotor('I', WIRING, ring_setting=r, alpha_labels=False)
|
bgneal@1
|
66 for s in NUMERIC_LABELS:
|
bgneal@1
|
67 rotor.set_display(s)
|
bgneal@1
|
68 self.assertEqual(s, rotor.get_display())
|
bgneal@1
|
69
|
bgneal@1
|
70 def test_wiring(self):
|
bgneal@5
|
71 """Loop through all ring settings & rotor positions and test the
|
bgneal@5
|
72 wiring.
|
bgneal@5
|
73
|
bgneal@5
|
74 """
|
bgneal@3
|
75 for r in range(26):
|
bgneal@3
|
76 rotor = Rotor('I', WIRING, ring_setting=r, alpha_labels=True)
|
bgneal@1
|
77
|
bgneal@3
|
78 for n, d in enumerate(ALPHA_LABELS):
|
bgneal@3
|
79 rotor.set_display(d)
|
bgneal@1
|
80
|
bgneal@3
|
81 wiring = collections.deque(WIRING)
|
bgneal@3
|
82 wiring.rotate(r - n)
|
bgneal@2
|
83
|
bgneal@3
|
84 for i in range(26):
|
bgneal@3
|
85 output = rotor.signal_in(i)
|
bgneal@2
|
86
|
bgneal@3
|
87 expected = (ord(wiring[i]) - ord('A') + r - n) % 26
|
bgneal@3
|
88 self.assertEqual(output, expected)
|
bgneal@3
|
89
|
bgneal@3
|
90 output = rotor.signal_out(expected)
|
bgneal@3
|
91 self.assertEqual(output, i)
|