4 from szyfrow
.polybius
import *
5 from szyfrow
.support
.utilities
import *
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')
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'
21 def test_grid_round_trip():
23 fgrid
= polybius_grid('elephant', 'abcde', 'abcde',
25 rgrid
= polybius_reverse_grid('elephant', 'abcde', 'abcde',
29 assert ltm
[l
] == rgrid
[fgrid
[l
]]
31 assert l
== rgrid
[fgrid
[l
]]
33 assert p
== fgrid
[rgrid
[p
]]
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'
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'
48 ct
= polybius_encipher('this is a test message for the ' \
49 'polybius decipherment', 'elephant', 'abcde', 'abcde', \
51 assert ct
== 'bbadccddccddaebbaaddbbceaaddddaecbaacadadcbbadaaacdaabedbcccdeddbeaabdccacadaadcceaababb'
53 ct
= polybius_encipher('this is a test message for the ' \
54 'polybius decipherment', 'elephant', 'abcde', 'abcde', \
56 assert ct
== 'bbdaccddccddeabbaaddbbecaaddddeabcaaacadcdbbdaaacaadbadecbccedddebaadbcccadaaacdecaaabbb'
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
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
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
)
84 trans_table
= ''.maketrans('j', 'i')
85 expected_score
= Ptrigrams(plaintext
.translate(trans_table
))
87 dictionary
= ['clearinghouse', 'computerising', 'counterclaims',
88 'housewarmings', 'intravenously', 'liquefactions', 'somersaulting',
89 'sportsmanlike', 'swashbuckling']
91 # dictionary = ['swashbuckling']
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
)
97 assert key
== expected_key
98 assert wrap
== expected_wrap
99 assert col_first
== expected_col_first
100 assert score
== pytest
.approx(expected_score
)