From 151a820c72161aacde623b444ab1f137689a36f5 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Sun, 17 Mar 2019 11:53:40 -0500 Subject: Rabin-Karp string search done! Not too bad to convert Java to Python. Converting the for loop and playing indexing is interesting, good exercise. --- tests/hash_substringTest.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/hash_substringTest.py (limited to 'tests/hash_substringTest.py') diff --git a/tests/hash_substringTest.py b/tests/hash_substringTest.py new file mode 100644 index 0000000..4c8fd5c --- /dev/null +++ b/tests/hash_substringTest.py @@ -0,0 +1,31 @@ +import unittest + +from sources.hash_substring import get_occurrences, rabin_karp + + +class MyTestCase(unittest.TestCase): + + def test1(self): + result = get_occurrences('aba', 'abacaba') + result = rabin_karp('aba', 'abacaba') + self.assertEqual(len(result), 2) + self.assertEqual(result[0], 0) + self.assertEqual(result[1], 4) + + def test2(self): + result = get_occurrences('Test', 'testTesttesT') + result = rabin_karp('Test', 'testTesttesT') + self.assertEqual(len(result), 1) + self.assertEqual(result[0], 4) + + def test3(self): + result = get_occurrences('aaaaa', 'baaaaaaa') + result = rabin_karp('aaaaa', 'baaaaaaa') + self.assertEqual(len(result), 3) + self.assertEqual(result[0], 1) + self.assertEqual(result[1], 2) + self.assertEqual(result[2], 3) + + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.3