Minor documentation updates
[szyfrow.git] / tests / test_polybius.py
1 import pytest
2 import string
3
4 from szyfrow.polybius import *
5 from szyfrow.support.utilities import *
6
7 def test_grid():
8 grid = polybius_grid('a', 'abcde', 'abcde')
9 assert grid['x'] == ('e', 'c')
10 grid = polybius_grid('elephant', 'abcde', 'abcde')
11 assert grid['e'] == ('a', 'a')
12 assert grid['b'] == ('b', 'c')
13
14 def test_reverse_grid():
15 grid = polybius_reverse_grid('a', 'abcde', 'abcde')
16 assert grid['e', 'c'] == 'x'
17 grid = polybius_reverse_grid('elephant', 'abcde', 'abcde')
18 assert grid['a', 'a'] == 'e'
19 assert grid['b', 'c'] == 'b'
20
21 def test_grid_round_trip():
22 ltm = {'j': 'i'}
23 fgrid = polybius_grid('elephant', 'abcde', 'abcde',
24 letters_to_merge=ltm)
25 rgrid = polybius_reverse_grid('elephant', 'abcde', 'abcde',
26 letters_to_merge=ltm)
27 for l in fgrid:
28 if l in ltm:
29 assert ltm[l] == rgrid[fgrid[l]]
30 else:
31 assert l == rgrid[fgrid[l]]
32 for p in rgrid:
33 assert p == fgrid[rgrid[p]]
34
35
36 def test_polybius_flatten():
37 assert polybius_flatten(('a', 'b'), column_first=True) == 'ba'
38 assert polybius_flatten(('a', 'b'), column_first=False) == 'ab'
39
40
41 def test_encipher_message():
42 ct = polybius_encipher('this is a test message for the ' \
43 'polybius decipherment', 'elephant', \
44 [1, 2, 3, 4, 5], [1, 2, 3, 4, 5], \
45 wrap_alphabet=KeywordWrapAlphabet.from_last)
46 assert ct == '2214445544551522115522511155551543114252542214111352123234442355411135441314115451112122'
47
48 ct = polybius_encipher('this is a test message for the ' \
49 'polybius decipherment', 'elephant', 'abcde', 'abcde', \
50 column_first=False)
51 assert ct == 'bbadccddccddaebbaaddbbceaaddddaecbaacadadcbbadaaacdaabedbcccdeddbeaabdccacadaadcceaababb'
52
53 ct = polybius_encipher('this is a test message for the ' \
54 'polybius decipherment', 'elephant', 'abcde', 'abcde', \
55 column_first=True)
56 assert ct == 'bbdaccddccddeabbaaddbbecaaddddeabcaaacadcdbbdaaacaadbadecbccedddebaadbcccadaaacdecaaabbb'
57
58
59 def test_decipher_message():
60 plaintext = sanitise('''Whoever has made a voyage up the Hudson must
61 remember the Kaatskill mountains. They are a dismembered branch of
62 the great''')
63 for key in ['wink', 'elephant', 'swashbuckling']:
64 enciphered = polybius_encipher(plaintext, key, 'abcde', 'abcde')
65 deciphered = polybius_decipher(enciphered, key, 'abcde', 'abcde')
66 assert deciphered == plaintext
67
68
69 def test_cadenus_break():
70 plaintext = sanitise('''You will rejoice to hear that no disaster has accompanied the
71 commencement of an enterprise which you have regarded with such evil
72 forebodings. I arrived here yesterday, and my first task is to assure
73 my dear sister of my welfare and increasing confidence in the success
74 of my undertaking.''')
75 expected_key = 'swashbuckling'
76 expected_wrap = KeywordWrapAlphabet.from_last
77 expected_row_order = 'abcde'
78 expected_col_order = 'abcde'
79 expected_col_first = True
80 ciphertext = polybius_encipher(plaintext, expected_key,
81 expected_col_order, expected_row_order,
82 column_first=expected_col_first, wrap_alphabet=expected_wrap)
83
84 trans_table = ''.maketrans('j', 'i')
85 expected_score = Ptrigrams(plaintext.translate(trans_table))
86
87 dictionary = ['clearinghouse', 'computerising', 'counterclaims',
88 'housewarmings', 'intravenously', 'liquefactions', 'somersaulting',
89 'sportsmanlike', 'swashbuckling']
90
91 # dictionary = ['swashbuckling']
92
93 (key, wrap, col_order, row_order, col_first), score = polybius_break(ciphertext,
94 expected_col_order, expected_row_order,
95 wordlist=dictionary, fitness=Ptrigrams)
96
97 assert key == expected_key
98 assert wrap == expected_wrap
99 assert col_first == expected_col_first
100 assert score == pytest.approx(expected_score)