X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=tests%2Ftest_affine.py;h=29ef05a395f3b61e70e5505be6e9d7bccd701842;hb=3350a462f460e81d96c587466f5b6a88cbba1f7e;hp=83d9090aee5284301abbd828e7f97da595b428c7;hpb=27c8005f6dea0026887b80a01b5f93a8f1b3c2b2;p=szyfrow.git diff --git a/tests/test_affine.py b/tests/test_affine.py index 83d9090..29ef05a 100644 --- a/tests/test_affine.py +++ b/tests/test_affine.py @@ -1,59 +1,58 @@ -import unittest +import pytest 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() +from szyfrow.affine import * +from szyfrow.support.utilities import * + + +def test_encipher_letter(): + for p, c in zip( + string.ascii_letters, + 'hknqtwzcfiloruxadgjmpsvybeHKNQTWZCFILORUXADGJMPSVYBE'): + assert affine_encipher_letter(p, 3, 5, True) == c + + for p, c in zip( + string.ascii_letters, + 'filoruxadgjmpsvybehknqtwzcFILORUXADGJMPSVYBEHKNQTWZC'): + assert affine_encipher_letter(p, 3, 5, False) == c + + +def test_decipher_letter(): + for p, c in zip( + string.ascii_letters, + 'hknqtwzcfiloruxadgjmpsvybeHKNQTWZCFILORUXADGJMPSVYBE'): + assert affine_decipher_letter(c, 3, 5, True) == p + + for p, c in zip( + string.ascii_letters, + 'filoruxadgjmpsvybehknqtwzcFILORUXADGJMPSVYBEHKNQTWZC'): + assert affine_decipher_letter(c, 3, 5, False) == p + +def test_encipher_message(): + enciphered = affine_encipher( + 'hours passed during which jerico tried every trick he could think of', + 15, 22, True) + expected = 'lmyfu bkuusd dyfaxw claol psfaom jfasd snsfg jfaoe ls omytd jlaxe mh' + assert enciphered == expected + + +def test_decipher_message(): + + deciphered = affine_decipher('lmyfu bkuusd dyfaxw claol psfaom jfasd snsfg jfaoe ls omytd jlaxe mh', + 15, 22, True) + expected = 'hours passed during which jerico tried every trick he could think of' + assert deciphered == expected + + +def test_break(): + 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) + assert expected_key == actual_key + assert expected_score == pytest.approx(actual_score)