Tweaks for testing examples
authorNeil Smith <neil.git@njae.me.uk>
Thu, 5 Apr 2018 10:36:16 +0000 (11:36 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Thu, 5 Apr 2018 10:36:16 +0000 (11:36 +0100)
caesar-cipher.ods
support/utilities.py
test/test_affine.py [new file with mode: 0644]

index d7cfe16c64c3dc6eb9e9f723656e7ee95b4dbc13..4f4243d0b1df5b148209b32459f283e9ba3b6671 100644 (file)
Binary files a/caesar-cipher.ods and b/caesar-cipher.ods differ
index 3125a9436dbad1d8ec7738f7b347aeef8389fa30..7319621c1fbfdfa7cf650912d288401189482057 100644 (file)
@@ -192,4 +192,5 @@ def frequencies(text):
     return collections.Counter(c for c in text)
 
 if __name__ == "__main__":
-    import doctest
\ No newline at end of file
+    import doctest
+    doctest.testmod()
\ No newline at end of file
diff --git a/test/test_affine.py b/test/test_affine.py
new file mode 100644 (file)
index 0000000..83d9090
--- /dev/null
@@ -0,0 +1,59 @@
+import unittest
+import string 
+
+from cipher.affine import *
+from support.utilities import *
+
+class AffineTest(unittest.TestCase):
+
+    def test_encipher_letter(self):
+        for p, c in zip(
+                string.ascii_letters, 
+                'hknqtwzcfiloruxadgjmpsvybeHKNQTWZCFILORUXADGJMPSVYBE'):
+            self.assertEqual(affine_encipher_letter(p, 3, 5, True), c)
+
+        for p, c in zip(
+                string.ascii_letters, 
+                'filoruxadgjmpsvybehknqtwzcFILORUXADGJMPSVYBEHKNQTWZC'):
+            self.assertEqual(affine_encipher_letter(p, 3, 5, False), c)
+
+
+    def test_decipher_letter(self):
+        for p, c in zip(
+                string.ascii_letters, 
+                'hknqtwzcfiloruxadgjmpsvybeHKNQTWZCFILORUXADGJMPSVYBE'):
+            self.assertEqual(affine_decipher_letter(c, 3, 5, True), p)
+
+        for p, c in zip(
+                string.ascii_letters, 
+                'filoruxadgjmpsvybehknqtwzcFILORUXADGJMPSVYBEHKNQTWZC'):
+            self.assertEqual(affine_decipher_letter(c, 3, 5, False), p)
+
+    def test_encipher_message(self):
+        self.assertEqual(affine_encipher(
+                'hours passed during which jerico tried every trick he could think of', 
+                15, 22, True),
+            'lmyfu bkuusd dyfaxw claol psfaom jfasd snsfg jfaoe ls omytd jlaxe mh')
+
+
+    def test_decipher_message(self):
+        self.assertEqual(affine_decipher('lmyfu bkuusd dyfaxw claol psfaom jfasd snsfg jfaoe ls omytd jlaxe mh', 
+                    15, 22, True),
+            'hours passed during which jerico tried every trick he could think of')
+
+
+    def test_break(self):
+        ciphertext = '''lmyfu bkuusd dyfaxw claol psfaom jfasd snsfg jfaoe ls 
+          omytd jlaxe mh jm bfmibj umis hfsul axubafkjamx. ls kffkxwsd jls 
+          ofgbjmwfkiu olfmxmtmwaokttg jlsx ls kffkxwsd jlsi zg tsxwjl. jlsx 
+          ls umfjsd jlsi zg hfsqysxog. ls dmmdtsd mx jls bats mh bkbsf. ls 
+          bfmctsd kfmyxd jls lyj, mztanamyu xmc jm clm cku tmmeaxw kj lai 
+          kxd clm ckuxj.'''
+        expected_key = (15, 22, True)
+        expected_score = -340.6011819
+        actual_key, actual_score = affine_break(ciphertext)
+        self.assertEqual(expected_key, actual_key)
+        self.assertAlmostEqual(expected_score, actual_score)
+
+if __name__ == '__main__':
+    unittest.main()