Minor documentation updates
[szyfrow.git] / tests / test_pocket_enigma.py
1 import pytest
2 import string
3
4 from szyfrow.pocket_enigma import *
5 from szyfrow.support.utilities import *
6
7 @pytest.fixture
8 def pe():
9 return PocketEnigma(1, 'a')
10
11 def test_init(pe):
12 assert pe.wheel_map == [25, 4, 23, 10, 1, 7, 9, 5, 12, 6, 3, 17, 8, 14, 13, 21, 19, 11, 20, 16, 18, 15, 24, 2, 22, 0]
13 assert pe.position == 0
14
15 def test_wheel_map(pe):
16 assert pe.make_wheel_map(pe.wheel2) == [2, 3, 0, 1, 22, 8, 15, 12, 5, 10, 9, 13, 7, 11, 16, 6, 14, 25, 20, 21, 18, 19, 4, 24, 23, 17]
17
18 def test_validate_wheel_map(pe):
19 pe.validate_wheel_spec(pe.wheel2) == True
20
21 with pytest.raises(ValueError):
22 pe.validate_wheel_spec([])
23 with pytest.raises(ValueError):
24 pe.validate_wheel_spec([('a', 'b', 'c')]*13)
25 with pytest.raises(ValueError):
26 pe.validate_wheel_spec([('a', 'b')]*13)
27
28 def test_encipher_letter(pe):
29 pe.set_position('f')
30 assert pe.encipher_letter('k') == 'h'
31 assert pe.position == 6
32
33 def test_lookup(pe):
34 pe.set_position('f')
35 assert cat([pe.lookup(l) for l in string.ascii_lowercase]) == 'udhbfejcpgmokrliwntsayqzvx'
36 assert pe.lookup('A') == ''
37
38 def test_advance(pe):
39 pe.set_position('f')
40 assert pe.position == 5
41 pe.advance()
42 assert pe.position == 6
43
44 pe.set_position('y')
45 assert pe.position == 24
46 pe.advance()
47 assert pe.position == 25
48 pe.advance()
49 assert pe.position == 0
50
51 def test_encipher(pe):
52 pe.set_position('f')
53 assert pe.encipher('helloworld') == 'kjsglcjoqc'
54
55 pe.set_position('f')
56 assert pe.encipher('kjsglcjoqc') == 'helloworld'
57
58 assert pe.encipher('helloworld', starting_position = 'x') == 'egrekthnnf'
59
60 def test_set_position(pe):
61 assert pe.set_position('a') == 0
62 assert pe.set_position('m') == 12
63 assert pe.set_position('z') == 25
64
65 def test_break():
66 assert pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'h', 0) == ['a', 'f', 'q']
67 assert pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'he', 0) == ['a']
68 assert pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'll', 2) == ['a']
69 assert pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'l', 2) == ['a']
70 assert pocket_enigma_break_by_crib('kzpjlzmoga', 1, 'l', 3) == ['a', 'j', 'n']
71 assert pocket_enigma_break_by_crib('aaaaa', 1, 'l', 3) == []
72