Minor documentation updates
[szyfrow.git] / tests / test_affine.py
1 import pytest
2 import string
3
4 from szyfrow.affine import *
5 from szyfrow.support.utilities import *
6
7
8 def test_encipher_letter():
9 for p, c in zip(
10 string.ascii_letters,
11 'hknqtwzcfiloruxadgjmpsvybeHKNQTWZCFILORUXADGJMPSVYBE'):
12 assert affine_encipher_letter(p, 3, 5, True) == c
13
14 for p, c in zip(
15 string.ascii_letters,
16 'filoruxadgjmpsvybehknqtwzcFILORUXADGJMPSVYBEHKNQTWZC'):
17 assert affine_encipher_letter(p, 3, 5, False) == c
18
19
20 def test_decipher_letter():
21 for p, c in zip(
22 string.ascii_letters,
23 'hknqtwzcfiloruxadgjmpsvybeHKNQTWZCFILORUXADGJMPSVYBE'):
24 assert affine_decipher_letter(c, 3, 5, True) == p
25
26 for p, c in zip(
27 string.ascii_letters,
28 'filoruxadgjmpsvybehknqtwzcFILORUXADGJMPSVYBEHKNQTWZC'):
29 assert affine_decipher_letter(c, 3, 5, False) == p
30
31 def test_encipher_message():
32 enciphered = affine_encipher(
33 'hours passed during which jerico tried every trick he could think of',
34 15, 22, True)
35 expected = 'lmyfu bkuusd dyfaxw claol psfaom jfasd snsfg jfaoe ls omytd jlaxe mh'
36 assert enciphered == expected
37
38
39 def test_decipher_message():
40
41 deciphered = affine_decipher('lmyfu bkuusd dyfaxw claol psfaom jfasd snsfg jfaoe ls omytd jlaxe mh',
42 15, 22, True)
43 expected = 'hours passed during which jerico tried every trick he could think of'
44 assert deciphered == expected
45
46
47 def test_break():
48 ciphertext = '''lmyfu bkuusd dyfaxw claol psfaom jfasd snsfg jfaoe ls
49 omytd jlaxe mh jm bfmibj umis hfsul axubafkjamx. ls kffkxwsd jls
50 ofgbjmwfkiu olfmxmtmwaokttg jlsx ls kffkxwsd jlsi zg tsxwjl. jlsx
51 ls umfjsd jlsi zg hfsqysxog. ls dmmdtsd mx jls bats mh bkbsf. ls
52 bfmctsd kfmyxd jls lyj, mztanamyu xmc jm clm cku tmmeaxw kj lai
53 kxd clm ckuxj.'''
54 expected_key = (15, 22, True)
55 expected_score = -340.6011819
56 actual_key, actual_score = affine_break(ciphertext)
57 assert expected_key == actual_key
58 assert expected_score == pytest.approx(actual_score)