mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-24 13:31:07 +00:00
b6c3fa8992
* fix endless loop bug, divide 0 bug and update description fix an endless bug, for example, if collection = [10,30,40,45,50,66,77,93], item = 67. fix divide 0 bug, when right=left it is not OK to point = left + ((item - sorted_collection[left]) * (right - left)) // (sorted_collection[right] - sorted_collection[left]) update 'sorted' to 'ascending sorted' in description to avoid confusion * delete swap files * delete 'address' and add input validation
94 lines
3.6 KiB
Python
94 lines
3.6 KiB
Python
import unittest
|
|
from interpolation_search import interpolation_search, interpolation_search_by_recursion
|
|
|
|
class Test_interpolation_search(unittest.TestCase):
|
|
def setUp(self):
|
|
# un-sorted case
|
|
self.collection1 = [5,3,4,6,7]
|
|
self.item1 = 4
|
|
# sorted case, result exists
|
|
self.collection2 = [10,30,40,45,50,66,77,93]
|
|
self.item2 = 66
|
|
# sorted case, result doesn't exist
|
|
self.collection3 = [10,30,40,45,50,66,77,93]
|
|
self.item3 = 67
|
|
# equal elements case, result exists
|
|
self.collection4 = [10,10,10,10,10]
|
|
self.item4 = 10
|
|
# equal elements case, result doesn't exist
|
|
self.collection5 = [10,10,10,10,10]
|
|
self.item5 = 3
|
|
# 1 element case, result exists
|
|
self.collection6 = [10]
|
|
self.item6 = 10
|
|
# 1 element case, result doesn't exists
|
|
self.collection7 = [10]
|
|
self.item7 = 1
|
|
|
|
def tearDown(self):
|
|
pass
|
|
|
|
def test_interpolation_search(self):
|
|
self.assertEqual(interpolation_search(self.collection1, self.item1), None)
|
|
|
|
self.assertEqual(interpolation_search(self.collection2, self.item2), self.collection2.index(self.item2))
|
|
|
|
self.assertEqual(interpolation_search(self.collection3, self.item3), None)
|
|
|
|
self.assertEqual(interpolation_search(self.collection4, self.item4), self.collection4.index(self.item4))
|
|
|
|
self.assertEqual(interpolation_search(self.collection5, self.item5), None)
|
|
|
|
self.assertEqual(interpolation_search(self.collection6, self.item6), self.collection6.index(self.item6))
|
|
|
|
self.assertEqual(interpolation_search(self.collection7, self.item7), None)
|
|
|
|
|
|
|
|
class Test_interpolation_search_by_recursion(unittest.TestCase):
|
|
def setUp(self):
|
|
# un-sorted case
|
|
self.collection1 = [5,3,4,6,7]
|
|
self.item1 = 4
|
|
# sorted case, result exists
|
|
self.collection2 = [10,30,40,45,50,66,77,93]
|
|
self.item2 = 66
|
|
# sorted case, result doesn't exist
|
|
self.collection3 = [10,30,40,45,50,66,77,93]
|
|
self.item3 = 67
|
|
# equal elements case, result exists
|
|
self.collection4 = [10,10,10,10,10]
|
|
self.item4 = 10
|
|
# equal elements case, result doesn't exist
|
|
self.collection5 = [10,10,10,10,10]
|
|
self.item5 = 3
|
|
# 1 element case, result exists
|
|
self.collection6 = [10]
|
|
self.item6 = 10
|
|
# 1 element case, result doesn't exists
|
|
self.collection7 = [10]
|
|
self.item7 = 1
|
|
|
|
def tearDown(self):
|
|
pass
|
|
|
|
def test_interpolation_search_by_recursion(self):
|
|
self.assertEqual(interpolation_search_by_recursion(self.collection1, self.item1, 0, len(self.collection1)-1), None)
|
|
|
|
self.assertEqual(interpolation_search_by_recursion(self.collection2, self.item2, 0, len(self.collection2)-1), self.collection2.index(self.item2))
|
|
|
|
self.assertEqual(interpolation_search_by_recursion(self.collection3, self.item3, 0, len(self.collection3)-1), None)
|
|
|
|
self.assertEqual(interpolation_search_by_recursion(self.collection4, self.item4, 0, len(self.collection4)-1), self.collection4.index(self.item4))
|
|
|
|
self.assertEqual(interpolation_search_by_recursion(self.collection5, self.item5, 0, len(self.collection5)-1), None)
|
|
|
|
self.assertEqual(interpolation_search_by_recursion(self.collection6, self.item6, 0, len(self.collection6)-1), self.collection6.index(self.item6))
|
|
|
|
self.assertEqual(interpolation_search_by_recursion(self.collection7, self.item7, 0, len(self.collection7)-1), None)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|