Minor documentation updates
[szyfrow.git] / tests / test_vigenere.py
1 import pytest
2 import string
3
4 from szyfrow.vigenere import *
5 from szyfrow.support.utilities import *
6
7
8 def test_vigenere_encipher_message():
9 enciphered = vigenere_encipher('hello', 'abc')
10 expected = 'hfnlp'
11 assert enciphered == expected
12
13
14 def test_vigenere_decipher_message():
15 deciphered = vigenere_decipher('hfnlp', 'abc')
16 expected = 'hello'
17 assert deciphered == expected
18
19 def test_beaufort_encipher_message():
20 enciphered = beaufort_encipher('inhisjournaldatedtheidesofoctober', 'arcanaimperii')
21 expected = 'sevsvrusyrrxfayyxuteemazudmpjmmwr'
22 assert enciphered == expected
23
24 def test_beaufort_decipher_message():
25 deciphered = beaufort_encipher('sevsvrusyrrxfayyxuteemazudmpjmmwr', 'arcanaimperii')
26 expected = 'inhisjournaldatedtheidesofoctober'
27 assert deciphered == expected
28
29
30 def test_vigenere_keyword_break():
31 ciphertext = vigenere_encipher(sanitise('this is a test message for the vigenere decipherment'), 'cat')
32 expected_key = 'cat'
33 expected_score = -52.9472712
34
35 actual_key, actual_score = vigenere_keyword_break(ciphertext, wordlist=['cat', 'elephant', 'kangaroo'])
36 assert expected_key == actual_key
37 assert expected_score == pytest.approx(actual_score)
38
39 def test_vigenere_frequency_break():
40 ciphertext = vigenere_encipher(sanitise("It is time to " \
41 "run. She is ready and so am I. I stole Daniel's pocketbook this " \
42 "afternoon when he left his jacket hanging on the easel in the " \
43 "attic. I jump every time I hear a footstep on the stairs, " \
44 "certain that the theft has been discovered and that I will " \
45 "be caught. The SS officer visits less often now that he is " \
46 "sure"), 'florence')
47 expected_key = 'florence'
48 expected_score = -307.5473096
49 actual_key, actual_score = vigenere_frequency_break(ciphertext)
50 assert expected_key == actual_key
51 assert expected_score == pytest.approx(actual_score)
52
53
54 def test_beaufort_sub_break():
55
56 ciphertext = 'samwpplggnnmmyaazgympjapopnwiywwomwspgpjmefwmawx' \
57 'jafjhxwwwdigxshnlywiamhyshtasxptwueahhytjwsn'
58 expected_key = 0
59 expected_score = -117.4492
60 actual_key, actual_score = beaufort_sub_break(ciphertext)
61 assert expected_key == actual_key
62 assert expected_score == pytest.approx(actual_score)
63
64 ciphertext = 'eyprzjjzznxymrygryjqmqhznjrjjapenejznawngnnezgza' \
65 'dgndknaogpdjneadadazlhkhxkryevrronrmdjnndjlo'
66 expected_key = 17
67 expected_score = -114.9598
68 actual_key, actual_score = beaufort_sub_break(ciphertext)
69 assert expected_key == actual_key
70 assert expected_score == pytest.approx(actual_score)
71
72
73 def test_beaufort_frequency_break():
74 ciphertext = beaufort_encipher(sanitise("It is time to " \
75 "run. She is ready and so am I. I stole Daniel's pocketbook this " \
76 "afternoon when he left his jacket hanging on the easel in the " \
77 "attic. I jump every time I hear a footstep on the stairs, " \
78 "certain that the theft has been discovered and that I will " \
79 "be caught. The SS officer visits less often now that he is " \
80 "sure"), 'florence')
81 expected_key = 'florence'
82 expected_score = -307.5473096
83 actual_key, actual_score = beaufort_frequency_break(ciphertext)
84 assert expected_key == actual_key
85 assert expected_score == pytest.approx(actual_score)
86
87
88 def test_beaufort_variant_frequency_break():
89 ciphertext = beaufort_variant_encipher(sanitise("It is time to " \
90 "run. She is ready and so am I. I stole Daniel's pocketbook this " \
91 "afternoon when he left his jacket hanging on the easel in the " \
92 "attic. I jump every time I hear a footstep on the stairs, " \
93 "certain that the theft has been discovered and that I will " \
94 "be caught. The SS officer visits less often now that he is " \
95 "sure"), 'florence')
96 expected_key = 'florence'
97 expected_score = -307.5473096
98 actual_key, actual_score = beaufort_variant_frequency_break(ciphertext)
99 assert expected_key == actual_key
100 assert expected_score == pytest.approx(actual_score)