Minor documentation updates
[szyfrow.git] / tests / test_cadenus.py
1 import pytest
2 import string
3
4 from szyfrow.cadenus import *
5 from szyfrow.support.utilities import *
6
7 def test_keycolumn():
8 assert make_cadenus_keycolumn()['a'] == 0
9 assert make_cadenus_keycolumn()['b'] == 1
10 assert make_cadenus_keycolumn()['c'] == 2
11 assert make_cadenus_keycolumn()['v'] == 21
12 assert make_cadenus_keycolumn()['w'] == 21
13 assert make_cadenus_keycolumn()['z'] == 24
14 assert make_cadenus_keycolumn(doubled_letters='ij', start='b',
15 reverse=True)['a'] == 1
16 assert make_cadenus_keycolumn(doubled_letters='ij', start='b',
17 reverse=True)['b'] == 0
18 assert make_cadenus_keycolumn(doubled_letters='ij', start='b',
19 reverse=True)['c'] == 24
20 assert make_cadenus_keycolumn(doubled_letters='ij', start='b',
21 reverse=True)['i'] == 18
22 assert make_cadenus_keycolumn(doubled_letters='ij', start='b',
23 reverse=True)['j'] == 18
24 assert make_cadenus_keycolumn(doubled_letters='ij', start='b',
25 reverse=True)['v'] == 6
26 assert make_cadenus_keycolumn(doubled_letters='ij', start='b',
27 reverse=True)['z'] == 2
28
29
30 def test_encipher_message():
31 plaintext = sanitise('''Whoever has made a voyage up the Hudson must
32 remember the Kaatskill mountains. They are a dismembered branch of
33 the great''')
34 keycol = make_cadenus_keycolumn(doubled_letters='vw', start='a', reverse=True)
35 ciphertext = cadenus_encipher(plaintext, 'wink', keycol)
36 expected = 'antodeleeeuhrsidrbhmhdrrhnimefmthgeaetakseomehetyaasuvoyegrastmmuuaeenabbtpchehtarorikswosmvaleatned'
37 assert ciphertext == expected
38
39 plaintext = sanitise('''a severe limitation on the usefulness of the
40 cadenus is that every message must be a multiple of twenty-five
41 letters long''')
42 ciphertext = cadenus_encipher(plaintext, 'easy', keycol)
43 expected = 'systretomtattlusoatleeesfiyheasdfnmschbhneuvsnpmtofarenuseieeieltarlmentieetogevesitfaisltngeeuvowul'
44 assert ciphertext == expected
45
46
47 def test_decipher_message():
48 plaintext = sanitise('''Whoever has made a voyage up the Hudson must
49 remember the Kaatskill mountains. They are a dismembered branch of
50 the great''')
51 keycol = make_cadenus_keycolumn(doubled_letters='vw', start='a', reverse=True)
52 for key in ['wink', 'easy']:
53 enciphered = cadenus_encipher(plaintext, key, keycol)
54 deciphered = cadenus_decipher(enciphered, key, keycol)
55 assert deciphered == plaintext
56
57
58 def test_cadenus_break():
59 plaintext = sanitise('''You will rejoice to hear that no disaster has accompanied the
60 commencement of an enterprise which you have regarded with such evil
61 forebodings. I arrived here yesterday, and my first task is to assure
62 my dear sister of my welfare and increasing confidence in the success
63 of my undertaking.
64
65 I am already far north of London, and as I walk in the streets of
66 Petersburgh, I feel a cold northern breeze play upon my cheeks, which
67 braces my nerves and fills me with delight. Do you understand this
68 feeling? This breeze, which has travelled from the regions towards
69 which I am advancing, gives me a foretaste of those icy climes.
70 Inspirited by this wind of promise, my daydreams become more fervent
71 and vivid.
72
73 I try in vain to be persuaded that the pole is the seat of
74 frost and desolation; it ever presents itself to my imagination as the
75 region of beauty and delight. There, Margaret, the sun is for ever
76 visible, its broad disk just skirting the horizon and diffusing a
77 perpetual splendour. There—for with your leave, my sister, I will put
78 some trust in preceding navigators—there snow and frost are banished;
79 and, sailing over a calm sea, we may be wafted to a land surpassing in
80 wonders and in beauty every region hitherto discovered on the habitable
81 globe.
82
83 Its productions and features may be without example, as the
84 phenomena of the heavenly bodies undoubtedly are in those undiscovered
85 solitudes. What may not be expected in a country of eternal light? I
86 may there discover the wondrous power which attracts the needle and may
87 regulate a thousand celestial observations that require only this
88 voyage to render their seeming eccentricities consistent for ever. I
89 shall satiate my ardent curiosity with the sight of a part of the world
90 never before visited, and may tread a land never before imprinted by
91 the foot of man. These are my enticements, and they are sufficient to
92 conquer all fear of danger or death and to induce me to commence this
93 laborious voyage with the joy a child feels when he embarks in a little
94 boat, with his holiday mates, on an expedition of discovery up his
95 native river. '''
96 )
97 expected_key = 'swashbuckling'
98 chunk_len = len(transpositions_of(expected_key)) * 25
99 target = plaintext + pad(len(plaintext), chunk_len, 'a')
100 expected_score = Ptrigrams(target)
101 expected_keycol = make_cadenus_keycolumn(doubled_letters='vw', start='a',
102 reverse=True)
103
104 ciphertext = cadenus_encipher(plaintext, expected_key, expected_keycol)
105
106 dictionary = ['clearinghouse', 'computerising', 'counterclaims',
107 'housewarmings', 'intravenously', 'liquefactions', 'somersaulting',
108 'sportsmanlike', 'swashbuckling']
109
110 # dictionary = ['swashbuckling']
111
112 (key, keycol), score = cadenus_break(ciphertext, wordlist=dictionary,
113 fitness=Ptrigrams)
114
115 assert key == expected_key
116 assert keycol == expected_keycol
117 assert score == pytest.approx(expected_score)