2014-04-07 22:32:31 -03:00
|
|
|
import unittest
|
|
|
|
|
|
2019-08-07 09:45:02 -04:00
|
|
|
from hamming import distance
|
2014-06-11 15:06:22 +02:00
|
|
|
|
2020-10-15 12:46:24 -04:00
|
|
|
# Tests adapted from `problem-specifications//canonical-data.json`
|
2017-03-20 16:42:22 +01:00
|
|
|
|
2014-04-07 22:32:31 -03:00
|
|
|
|
2019-07-29 23:42:12 -05:00
|
|
|
class HammingTest(unittest.TestCase):
|
2017-10-26 21:08:57 +05:30
|
|
|
def test_empty_strands(self):
|
2019-08-07 09:45:02 -04:00
|
|
|
self.assertEqual(distance("", ""), 0)
|
2017-10-26 21:08:57 +05:30
|
|
|
|
2019-01-08 20:03:03 +05:30
|
|
|
def test_single_letter_identical_strands(self):
|
2019-08-07 09:45:02 -04:00
|
|
|
self.assertEqual(distance("A", "A"), 0)
|
2014-04-07 22:32:31 -03:00
|
|
|
|
2019-01-08 20:03:03 +05:30
|
|
|
def test_single_letter_different_strands(self):
|
2019-08-07 09:45:02 -04:00
|
|
|
self.assertEqual(distance("G", "T"), 1)
|
2014-04-07 22:32:31 -03:00
|
|
|
|
2019-01-08 20:03:03 +05:30
|
|
|
def test_long_identical_strands(self):
|
2019-08-07 09:45:02 -04:00
|
|
|
self.assertEqual(distance("GGACTGAAATCTG", "GGACTGAAATCTG"), 0)
|
2016-11-24 14:42:42 +01:00
|
|
|
|
2019-01-08 20:03:03 +05:30
|
|
|
def test_long_different_strands(self):
|
2019-08-07 09:45:02 -04:00
|
|
|
self.assertEqual(distance("GGACGGATTCTG", "AGGACGGATTCT"), 9)
|
2016-11-24 14:42:42 +01:00
|
|
|
|
|
|
|
|
def test_disallow_first_strand_longer(self):
|
2017-12-12 18:11:43 +00:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-08-07 09:45:02 -04:00
|
|
|
distance("AATG", "AAA")
|
2016-11-24 14:42:42 +01:00
|
|
|
|
|
|
|
|
def test_disallow_second_strand_longer(self):
|
2017-12-12 18:11:43 +00:00
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-08-07 09:45:02 -04:00
|
|
|
distance("ATA", "AGTG")
|
2014-04-07 22:32:31 -03:00
|
|
|
|
2019-02-14 13:23:17 -02:00
|
|
|
def test_disallow_left_empty_strand(self):
|
|
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-08-07 09:45:02 -04:00
|
|
|
distance("", "G")
|
2019-02-14 13:23:17 -02:00
|
|
|
|
|
|
|
|
def test_disallow_right_empty_strand(self):
|
|
|
|
|
with self.assertRaisesWithMessage(ValueError):
|
2019-08-07 09:45:02 -04:00
|
|
|
distance("G", "")
|
2019-02-14 13:23:17 -02:00
|
|
|
|
2017-12-12 18:11:43 +00:00
|
|
|
# Utility functions
|
|
|
|
|
def assertRaisesWithMessage(self, exception):
|
|
|
|
|
return self.assertRaisesRegex(exception, r".+")
|
|
|
|
|
|
2014-04-07 22:32:31 -03:00
|
|
|
|
2019-07-29 23:42:12 -05:00
|
|
|
if __name__ == "__main__":
|
2014-04-07 22:32:31 -03:00
|
|
|
unittest.main()
|