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