7 class LetterTransformerTest(unittest
.TestCase
):
10 lt
= LetterTransformer([('z', 'a')] + \
11 list(zip(string
.ascii_lowercase
, string
.ascii_lowercase
[1:])),
13 self
.assertEqual(lt
.forward_map
,
14 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0])
16 self
.assertEqual(lt
.backward_map
,
17 [25, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
18 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])
22 lt
= LetterTransformer(cat(collections
.OrderedDict
.fromkeys('zyxwc' + string
.ascii_lowercase
)))
23 self
.assertEqual(lt
.forward_map
,
24 [25, 24, 23, 22, 2, 0, 1, 3, 4, 5, 6, 7, 8, 9,
25 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21])
26 self
.assertEqual(lt
.backward_map
,
27 [5, 6, 4, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
28 17, 18, 19, 20, 21, 22, 23, 24, 25, 3, 2, 1, 0])
30 def test_transform(self
):
31 lt
= LetterTransformer(cat(collections
.OrderedDict
.fromkeys('zyxwc' + string
.ascii_lowercase
)))
32 self
.assertEqual(cat(lt
.forward(l
) for l
in string
.ascii_lowercase
),
33 'zyxwcabdefghijklmnopqrstuv')
34 self
.assertEqual(cat(lt
.backward(l
) for l
in string
.ascii_lowercase
),
35 'fgehijklmnopqrstuvwxyzdcba')
38 class PlugboardTest(unittest
.TestCase
):
40 self
.pb
= Plugboard('ua pf rq so ni ey bg hl tx zj'.upper())
43 self
.assertEqual(self
.pb
.forward_map
,
44 [20, 6, 2, 3, 24, 15, 1, 11, 13, 25, 10, 7, 12,
45 8, 18, 5, 17, 16, 14, 23, 0, 21, 22, 19, 4, 9])
46 self
.assertEqual(self
.pb
.forward_map
, self
.pb
.backward_map
)
48 def test_transform(self
):
49 self
.assertEqual(cat(self
.pb
.forward(l
)
50 for l
in string
.ascii_lowercase
),
51 'ugcdypblnzkhmisfrqoxavwtej')
52 self
.assertEqual(cat(self
.pb
.backward(l
)
53 for l
in string
.ascii_lowercase
),
54 'ugcdypblnzkhmisfrqoxavwtej')
57 class ReflectorTest(unittest
.TestCase
):
59 self
.ref
= Reflector(reflector_b_spec
)
62 self
.assertEqual(self
.ref
.forward_map
,
63 [24, 17, 20, 7, 16, 18, 11, 3, 15, 23, 13, 6, 14,
64 10, 12, 8, 4, 1, 5, 25, 2, 22, 21, 9, 0, 19])
65 self
.assertEqual(self
.ref
.forward_map
, self
.ref
.backward_map
)
67 def test_transform(self
):
68 self
.assertEqual(cat(self
.ref
.forward(l
)
69 for l
in string
.ascii_lowercase
),
70 'yruhqsldpxngokmiebfzcwvjat')
71 self
.assertEqual(cat(self
.ref
.backward(l
)
72 for l
in string
.ascii_lowercase
),
73 'yruhqsldpxngokmiebfzcwvjat')
76 class SimpleWheelTest(unittest
.TestCase
):
78 rotor_1_transform
= list(zip(string
.ascii_lowercase
,
79 'EKMFLGDQVZNTOWYHXUSPAIBRCJ'.lower()))
80 wheel_1
= SimpleWheel(rotor_1_transform
, raw_transform
=True)
81 self
.assertEqual(cat(wheel_1
.forward(l
)
82 for l
in string
.ascii_lowercase
),
83 'ekmflgdqvzntowyhxuspaibrcj')
84 self
.assertEqual(cat(wheel_1
.backward(l
)
85 for l
in string
.ascii_lowercase
),
86 'uwygadfpvzbeckmthxslrinqoj')
89 wheel_2
= SimpleWheel(wheel_ii_spec
)
90 self
.assertEqual(cat(wheel_2
.forward(l
)
91 for l
in string
.ascii_lowercase
),
92 'ajdksiruxblhwtmcqgznpyfvoe')
93 self
.assertEqual(cat(wheel_2
.backward(l
)
94 for l
in string
.ascii_lowercase
),
95 'ajpczwrlfbdkotyuqgenhxmivs')
97 def test_set_position(self
):
98 wheel_3
= SimpleWheel(wheel_iii_spec
)
99 wheel_3
.set_position('a')
100 self
.assertEqual(wheel_3
.position
, 0)
101 self
.assertEqual(wheel_3
.position_l
, 'a')
103 wheel_3
.set_position('f')
104 self
.assertEqual(wheel_3
.position
, 5)
105 self
.assertEqual(wheel_3
.position_l
, 'f')
108 self
.assertEqual(wheel_3
.position
, 6)
109 self
.assertEqual(wheel_3
.position_l
, 'g')
111 wheel_3
.set_position(12)
112 self
.assertEqual(wheel_3
.position
, 12)
113 self
.assertEqual(wheel_3
.position_l
, 'm')
116 self
.assertEqual(wheel_3
.position
, 13)
117 self
.assertEqual(wheel_3
.position_l
, 'n')
120 def test_advance(self
):
121 wheel_3
= SimpleWheel(wheel_iii_spec
)
122 wheel_3
.set_position('a')
124 self
.assertEqual(cat(wheel_3
.forward(l
)
125 for l
in string
.ascii_lowercase
),
126 'cegikboqswuymxdhvfzjltrpna')
127 self
.assertEqual(cat(wheel_3
.backward(l
)
128 for l
in string
.ascii_lowercase
),
129 'zfaobrcpdteumygxhwivkqjnls')
130 self
.assertEqual(wheel_3
.position
, 1)
131 self
.assertEqual(wheel_3
.position_l
, 'b')
133 for _
in range(24): wheel_3
.advance()
135 self
.assertEqual(wheel_3
.position
, 25)
136 self
.assertEqual(wheel_3
.position_l
, 'z')
138 self
.assertEqual(cat(wheel_3
.forward(l
)
139 for l
in string
.ascii_lowercase
),
140 'pcegikmdqsuywaozfjxhblnvtr')
141 self
.assertEqual(cat(wheel_3
.backward(l
)
142 for l
in string
.ascii_lowercase
),
143 'nubhcqdterfvgwoaizjykxmslp')
146 self
.assertEqual(wheel_3
.position
, 0)
147 self
.assertEqual(wheel_3
.position_l
, 'a')
150 self
.assertEqual(cat(wheel_3
.forward(l
)
151 for l
in string
.ascii_lowercase
),
152 'bdfhjlcprtxvznyeiwgakmusqo')
153 self
.assertEqual(cat(wheel_3
.backward(l
)
154 for l
in string
.ascii_lowercase
),
155 'tagbpcsdqeufvnzhyixjwlrkom')
158 class WheelTest(unittest
.TestCase
):
159 def test_init1(self
):
160 wheel
= Wheel(wheel_iii_spec
, wheel_iii_pegs
, position
='b',
162 self
.assertEqual(wheel
.position
, 1)
163 self
.assertEqual(wheel
.peg_positions
, [20])
164 self
.assertEqual(wheel
.position_l
, 'b')
167 self
.assertEqual(wheel
.position
, 2)
168 self
.assertEqual(wheel
.peg_positions
, [19])
169 self
.assertEqual(wheel
.position_l
, 'c')
171 def test_init2(self
):
172 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
174 self
.assertEqual(wheel
.position
, 25)
175 self
.assertIn(11, wheel
.peg_positions
)
176 self
.assertIn(24, wheel
.peg_positions
)
177 self
.assertEqual(wheel
.position_l
, 'b')
178 self
.assertEqual(cat(wheel
.forward(l
)
179 for l
in string
.ascii_lowercase
),
180 'xkqhwpvngzrcfoiaselbtymjdu')
181 self
.assertEqual(cat(wheel
.backward(l
)
182 for l
in string
.ascii_lowercase
),
183 'ptlyrmidoxbswhnfckquzgeavj')
185 def test_advance(self
):
186 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
190 self
.assertEqual(wheel
.position
, 0)
191 self
.assertIn(10, wheel
.peg_positions
)
192 self
.assertIn(23, wheel
.peg_positions
)
193 self
.assertEqual(wheel
.position_l
, 'c')
194 self
.assertEqual(cat(wheel
.forward(l
)
195 for l
in string
.ascii_lowercase
),
196 'jpgvoumfyqbenhzrdkasxlictw')
197 self
.assertEqual(cat(wheel
.backward(l
)
198 for l
in string
.ascii_lowercase
),
199 'skxqlhcnwarvgmebjptyfdzuio')
201 def test_advance_23(self
):
202 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
207 self
.assertEqual(wheel
.position
, 22)
208 self
.assertIn(1, wheel
.peg_positions
)
209 self
.assertIn(14, wheel
.peg_positions
)
210 self
.assertEqual(wheel
.position_l
, 'y')
211 self
.assertEqual(cat(wheel
.forward(l
)
212 for l
in string
.ascii_lowercase
),
213 'mgxantkzsyqjcufirldvhoewbp')
214 self
.assertEqual(cat(wheel
.backward(l
)
215 for l
in string
.ascii_lowercase
),
216 'dymswobuplgraevzkqifntxcjh')
218 def test_advance_24(self
):
219 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
224 self
.assertEqual(wheel
.position
, 23)
225 self
.assertIn(0, wheel
.peg_positions
)
226 self
.assertIn(13, wheel
.peg_positions
)
227 self
.assertEqual(wheel
.position_l
, 'z')
228 self
.assertEqual(cat(wheel
.forward(l
)
229 for l
in string
.ascii_lowercase
),
230 'fwzmsjyrxpibtehqkcugndvaol')
231 self
.assertEqual(cat(wheel
.backward(l
)
232 for l
in string
.ascii_lowercase
),
233 'xlrvnatokfqzduyjphemswbigc')
235 def test_advance_25(self
):
236 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
241 self
.assertEqual(wheel
.position
, 24)
242 self
.assertIn(25, wheel
.peg_positions
)
243 self
.assertIn(12, wheel
.peg_positions
)
244 self
.assertEqual(wheel
.position_l
, 'a')
245 self
.assertEqual(cat(wheel
.forward(l
)
246 for l
in string
.ascii_lowercase
),
247 'vylrixqwohasdgpjbtfmcuznke')
248 self
.assertEqual(cat(wheel
.backward(l
)
249 for l
in string
.ascii_lowercase
),
250 'kqumzsnjepyctxiogdlrvahfbw')
252 def test_advance_26(self
):
253 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
258 self
.assertEqual(wheel
.position
, 25)
259 self
.assertIn(24, wheel
.peg_positions
)
260 self
.assertIn(11, wheel
.peg_positions
)
261 self
.assertEqual(wheel
.position_l
, 'b')
262 self
.assertEqual(cat(wheel
.forward(l
)
263 for l
in string
.ascii_lowercase
),
264 'xkqhwpvngzrcfoiaselbtymjdu')
265 self
.assertEqual(cat(wheel
.backward(l
)
266 for l
in string
.ascii_lowercase
),
267 'ptlyrmidoxbswhnfckquzgeavj')
269 def test_advance_27(self
):
270 wheel
= Wheel(wheel_vi_spec
, wheel_vi_pegs
, position
='b',
275 self
.assertEqual(wheel
.position
, 0)
276 self
.assertIn(23, wheel
.peg_positions
)
277 self
.assertIn(10, wheel
.peg_positions
)
278 self
.assertEqual(wheel
.position_l
, 'c')
279 self
.assertEqual(cat(wheel
.forward(l
)
280 for l
in string
.ascii_lowercase
),
281 'jpgvoumfyqbenhzrdkasxlictw')
282 self
.assertEqual(cat(wheel
.backward(l
)
283 for l
in string
.ascii_lowercase
),
284 'skxqlhcnwarvgmebjptyfdzuio')
286 def test_set_position(self
):
287 wheel_3
= Wheel(wheel_iii_spec
, wheel_iii_pegs
, ring_setting
=3)
288 wheel_3
.set_position('a')
289 self
.assertEqual(wheel_3
.position
, 24)
290 self
.assertEqual(wheel_3
.position_l
, 'a')
291 self
.assertEqual(wheel_3
.peg_positions
, [21])
293 wheel_3
.set_position('z')
294 self
.assertEqual(wheel_3
.position
, 23)
295 self
.assertEqual(wheel_3
.position_l
, 'z')
296 self
.assertEqual(wheel_3
.peg_positions
, [22])
298 wheel_3
.set_position(26)
299 self
.assertEqual(wheel_3
.position
, 23)
300 self
.assertEqual(wheel_3
.position_l
, 'z')
301 self
.assertEqual(wheel_3
.peg_positions
, [22])
303 wheel_3
.set_position(27)
304 self
.assertEqual(wheel_3
.position
, 24)
305 self
.assertEqual(wheel_3
.position_l
, 'a')
306 self
.assertEqual(wheel_3
.peg_positions
, [21])
308 wheel_3
.set_position('f')
309 self
.assertEqual(wheel_3
.position
, 3)
310 self
.assertEqual(wheel_3
.position_l
, 'f')
311 self
.assertEqual(wheel_3
.peg_positions
, [16])
313 wheel_3
.set_position(6)
314 self
.assertEqual(wheel_3
.position
, 3)
315 self
.assertEqual(wheel_3
.position_l
, 'f')
316 self
.assertEqual(wheel_3
.peg_positions
, [16])
319 self
.assertEqual(wheel_3
.position
, 4)
320 self
.assertEqual(wheel_3
.position_l
, 'g')
321 self
.assertEqual(wheel_3
.peg_positions
, [15])
323 wheel_3
.set_position(12)
324 self
.assertEqual(wheel_3
.position
, 9)
325 self
.assertEqual(wheel_3
.position_l
, 'l')
326 self
.assertEqual(wheel_3
.peg_positions
, [10])
329 self
.assertEqual(wheel_3
.position
, 10)
330 self
.assertEqual(wheel_3
.position_l
, 'm')
331 self
.assertEqual(wheel_3
.peg_positions
, [9])
334 class EnigmaTest(unittest
.TestCase
):
337 self
.enigma
= Enigma(reflector_b_spec
,
338 wheel_i_spec
, wheel_i_pegs
,
339 wheel_ii_spec
, wheel_ii_pegs
,
340 wheel_iii_spec
, wheel_iii_pegs
,
344 # Setting sheet line 31 from http://www.codesandciphers.org.uk/enigma/enigma3.htm
345 # Enigma simulation settings are
346 # http://enigma.louisedade.co.uk/enigma.html?m3;b;b153;AFTX;AJEU;AU-BG-EY-FP-HL-IN-JZ-OS-QR-TX
347 self
.enigma31
= Enigma(reflector_b_spec
,
348 wheel_i_spec
, wheel_i_pegs
,
349 wheel_v_spec
, wheel_v_pegs
,
350 wheel_iii_spec
, wheel_iii_pegs
,
352 'ua pf rq so ni ey bg hl tx zj')
354 # Settings for Bletchley Park outreach department's Enigma
355 self
.enigma_bp
= Enigma(reflector_b_spec
,
356 wheel_i_spec
, wheel_i_pegs
,
357 wheel_iii_spec
, wheel_iii_pegs
,
358 wheel_ii_spec
, wheel_ii_pegs
,
360 'qm we ro tu zj ps dl fg')
363 def test_middle_advance(self
):
364 self
.enigma
.set_wheels('a', 'a', 't')
365 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 0, 19))
366 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'aat')
367 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [4], [2]))
368 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
369 'puvioztjdhxmlyeawsrgbcqknf')
371 self
.enigma
.advance()
372 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 0, 20))
373 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'aau')
374 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [4], [1]))
375 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
376 'baigpldqcowfyzjehvtsxrkumn')
378 self
.enigma
.advance()
379 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 0, 21))
380 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'aav')
381 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [4], [0]))
382 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
383 'mnvfydiwgzsoablrxpkutchqej')
385 self
.enigma
.advance()
386 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 1, 22))
387 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'abw')
388 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [3], [25]))
389 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
390 'ulfopcykswhbzvderqixanjtgm')
392 self
.enigma
.advance()
393 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 1, 23))
394 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'abx')
395 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [3], [24]))
396 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
397 'qmwftdyovursbzhxaklejicpgn')
399 self
.enigma
.advance()
400 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 1, 24))
401 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'aby')
402 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [3], [23]))
403 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
404 'oljmzxrvucybdqasngpwihtfke')
407 def test_double_advance(self
):
408 self
.enigma
.set_wheels('a', 'd', 't')
409 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 3, 19))
410 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'adt')
411 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [1], [2]))
412 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
413 'zcbpqxwsjiuonmldethrkygfva')
415 self
.enigma
.advance()
416 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 3, 20))
417 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'adu')
418 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [1], [1]))
419 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
420 'ehprawjbngotxikcsdqlzyfmvu')
422 self
.enigma
.advance()
423 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 3, 21))
424 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'adv')
425 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [1], [0]))
426 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
427 'eqzxarpihmnvjkwgbfuyslodtc')
429 self
.enigma
.advance()
430 self
.assertEqual(self
.enigma
.wheel_positions
, (0, 4, 22))
431 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'aew')
432 self
.assertEqual(self
.enigma
.peg_positions
, ([16], [0], [25]))
433 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
434 'qedcbtpluzmhkongavwfirsyxj')
436 self
.enigma
.advance()
437 self
.assertEqual(self
.enigma
.wheel_positions
, (1, 5, 23))
438 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'bfx')
439 self
.assertEqual(self
.enigma
.peg_positions
, ([15], [25], [24]))
440 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
441 'iwuedhsfazqxytvrkpgncoblmj')
443 self
.enigma
.advance()
444 self
.assertEqual(self
.enigma
.wheel_positions
, (1, 5, 24))
445 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'bfy')
446 self
.assertEqual(self
.enigma
.peg_positions
, ([15], [25], [23]))
447 self
.assertEqual(cat(self
.enigma
.lookup(l
) for l
in string
.ascii_lowercase
),
448 'baknstqzrmcxjdvygiefwoulph')
451 def test_simple_encipher(self
):
452 self
.enigma
.set_wheels('a', 'a', 'a')
453 ct
= self
.enigma
.encipher('testmessage')
454 self
.assertEqual(ct
, 'olpfhnvflyn')
456 self
.enigma
.set_wheels('a', 'd', 't')
457 ct
= self
.enigma
.encipher('testmessage')
458 self
.assertEqual(ct
, 'lawnjgpwjik')
460 self
.enigma
.set_wheels('b', 'd', 'q')
461 ct
= self
.enigma
.encipher('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
463 'kvmmwrlqlqsqpeugjrcxzwpfyiyybwloewrouvkpoztceuwtfjzqwpbqldttsr')
464 self
.assertEqual(cat(self
.enigma
.wheel_positions_l
), 'cha')
467 def test_advance_with_ring_settings(self
):
468 self
.enigma31
.set_wheels('j', 'e', 'u')
470 self
.enigma31
.advance()
471 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 11, 24))
472 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jev')
473 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [21], [0]))
474 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
475 'mvqjlyowkdieasgzcunxrbhtfp')
477 self
.enigma31
.advance()
478 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 12, 25))
479 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jfw')
480 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [20], [25]))
481 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
482 'sjolzuyvrbwdpxcmtiaqfhknge')
484 self
.enigma31
.advance()
485 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 12, 0))
486 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jfx')
487 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [20], [24]))
488 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
489 'qrxedkoywufmlvgsabpzjnicht')
491 self
.enigma31
.advance()
492 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 12, 1))
493 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jfy')
494 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [20], [23]))
495 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
496 'hpsukliagqefwvtbjxcodnmrzy')
498 self
.enigma31
.advance()
499 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 12, 2))
500 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jfz')
501 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [20], [22]))
502 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
503 'zevnbpyqowrtxdifhkulscjmga')
506 def test_advance_with_ring_settings_2(self
):
507 self
.enigma31
.set_wheels('i', 'd', 'z')
509 self
.enigma31
.advance()
510 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 3))
511 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ida')
512 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [21]))
513 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
514 'ikhpqrvcambzjondefwyxgsutl')
516 self
.enigma31
.advance()
517 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 4))
518 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idb')
519 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [20]))
520 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
521 'cdabskhgzwfmlqvunyexpojtri')
523 self
.enigma31
.advance()
524 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 5))
525 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idc')
526 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [19]))
527 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
528 'pcbwiqhgemyvjsuaftnroldzkx')
530 self
.enigma31
.advance()
531 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 6))
532 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idd')
533 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [18]))
534 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
535 'xcbfvdnouptmlghjzwykierasq')
537 self
.enigma31
.advance()
538 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 7))
539 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ide')
540 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [17]))
541 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
542 'xfvglbdynuseriwqpmkzjcoaht')
544 self
.enigma31
.advance()
545 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 8))
546 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idf')
547 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [16]))
548 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
549 'tfpqlbouynsewjgcdxkahzmriv')
551 self
.enigma31
.advance()
552 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 9))
553 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idg')
554 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [15]))
555 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
556 'cjaunvlwtbygzexrspqidfhokm')
558 self
.enigma31
.advance()
559 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 10))
560 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idh')
561 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [14]))
562 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
563 'yltxkrqvowebzpingfucshjdam')
565 self
.enigma31
.advance()
566 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 11))
567 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idi')
568 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [13]))
569 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
570 'myktluzrnxceaiqsohpdfwvjbg')
572 self
.enigma31
.advance()
573 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 12))
574 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idj')
575 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [12]))
576 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
577 'pynjrmiugdqxfcvakewzhoslbt')
579 self
.enigma31
.advance()
580 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 13))
581 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idk')
582 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [11]))
583 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
584 'mwvedyplnoxhaijgrqtszcbkfu')
586 self
.enigma31
.advance()
587 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 14))
588 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idl')
589 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [10]))
590 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
591 'qcbrfeutvoxpnmjladzhgiykws')
593 self
.enigma31
.advance()
594 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 15))
595 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idm')
596 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [9]))
597 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
598 'dnoahryetsmukbcvwfjilpqzgx')
600 self
.enigma31
.advance()
601 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 16))
602 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idn')
603 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [8]))
604 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
605 'nidcfehgbqsovalyjzkxwmutpr')
607 self
.enigma31
.advance()
608 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 17))
609 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ido')
610 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [7]))
611 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
612 'joifxdulcarhzpbntkwqgysevm')
614 self
.enigma31
.advance()
615 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 18))
616 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idp')
617 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [6]))
618 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
619 'ptnlsxvozmwdjchayuebrgkfqi')
621 self
.enigma31
.advance()
622 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 19))
623 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idq')
624 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [5]))
625 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
626 'slwopzqnmxybihdeguavrtcjkf')
628 self
.enigma31
.advance()
629 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 20))
630 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idr')
631 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [4]))
632 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
633 'hcbedwlamzogixkytsrqvufnpj')
635 self
.enigma31
.advance()
636 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 21))
637 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ids')
638 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [3]))
639 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
640 'odxbjwzrmelkisavuhnyqpfctg')
642 self
.enigma31
.advance()
643 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 22))
644 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idt')
645 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [2]))
646 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
647 'udgbfeclrwnhxksvtioqapjmzy')
649 self
.enigma31
.advance()
650 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 23))
651 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idu')
652 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [1]))
653 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
654 'nrdczqxmowvshaiufblypkjgte')
656 self
.enigma31
.advance()
657 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 10, 24))
658 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'idv')
659 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [22], [0]))
660 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
661 'hkifjdoacebqtzgulyvmpsxwrn')
663 self
.enigma31
.advance()
664 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 11, 25))
665 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'iew')
666 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [21], [25]))
667 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
668 'yptzuhofqvnmlkgbixwcejsrad')
670 self
.enigma31
.advance()
671 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 11, 0))
672 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'iex')
673 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [21], [24]))
674 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
675 'vkdcwhqfjibzsptngumoraeyxl')
677 self
.enigma31
.advance()
678 self
.assertEqual(self
.enigma31
.wheel_positions
, (3, 11, 1))
679 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'iey')
680 self
.assertEqual(self
.enigma31
.peg_positions
, ([8], [21], [23]))
681 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
682 'wenpbqrouxlkychdfgzvitajms')
684 def test_double_advance_with_ring_settings_2(self
):
685 self
.enigma31
.set_wheels('a', 'y', 't')
686 self
.assertEqual(self
.enigma31
.wheel_positions
, (21, 5, 22))
687 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ayt')
688 self
.assertEqual(self
.enigma31
.peg_positions
, ([16], [1], [2]))
690 self
.enigma31
.advance()
691 self
.assertEqual(self
.enigma31
.wheel_positions
, (21, 5, 23))
692 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ayu')
693 self
.assertEqual(self
.enigma31
.peg_positions
, ([16], [1], [1]))
695 self
.enigma31
.advance()
696 self
.assertEqual(self
.enigma31
.wheel_positions
, (21, 5, 24))
697 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'ayv')
698 self
.assertEqual(self
.enigma31
.peg_positions
, ([16], [1], [0]))
700 self
.enigma31
.advance()
701 self
.assertEqual(self
.enigma31
.wheel_positions
, (21, 6, 25))
702 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'azw')
703 self
.assertEqual(self
.enigma31
.peg_positions
, ([16], [0], [25]))
705 self
.enigma31
.advance()
706 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 7, 0))
707 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bax')
708 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [25], [24]))
710 self
.enigma31
.advance()
711 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 7, 1))
712 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bay')
713 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [25], [23]))
715 self
.enigma31
.set_wheels('a', 'z', 't')
716 self
.assertEqual(self
.enigma31
.wheel_positions
, (21, 6, 22))
717 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'azt')
718 self
.assertEqual(self
.enigma31
.peg_positions
, ([16], [0], [2]))
720 self
.enigma31
.advance()
721 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 7, 23))
722 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bau')
723 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [25], [1]))
725 self
.enigma31
.advance()
726 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 7, 24))
727 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bav')
728 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [25], [0]))
730 self
.enigma31
.advance()
731 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 8, 25))
732 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bbw')
733 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [24], [25]))
735 self
.enigma31
.advance()
736 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 8, 0))
737 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bbx')
738 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [24], [24]))
740 self
.enigma31
.advance()
741 self
.assertEqual(self
.enigma31
.wheel_positions
, (22, 8, 1))
742 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'bby')
743 self
.assertEqual(self
.enigma31
.peg_positions
, ([15], [24], [23]))
746 def test_encipher_with_ring(self
):
748 self
.enigma31
.set_wheels('i', 'z', 'd')
749 ct
= self
.enigma31
.encipher('verylongtestmessagewithanextrabitofmessageforgoodmeasure')
751 'apocwtjuikurcfivlozvhffkoacxufcekthcvodfqpxdjqyckdozlqki')
752 self
.assertEqual(self
.enigma31
.wheel_positions
, (4, 9, 10))
753 self
.assertEqual(cat(self
.enigma31
.wheel_positions_l
), 'jch')
754 self
.assertEqual(self
.enigma31
.peg_positions
, ([7], [23], [14]))
755 self
.assertEqual(cat(self
.enigma31
.lookup(l
) for l
in string
.ascii_lowercase
),
756 'mopnigfuesqwadbcktjrhylzvx')
758 self
.enigma31
.set_wheels('i', 'z', 'd')
759 pt
= self
.enigma31
.decipher('apocwtjuikurcfivlozvhffkoacxufcekthcvodfqpxdjqyckdozlqki')
761 'verylongtestmessagewithanextrabitofmessageforgoodmeasure')
763 if __name__
== '__main__':