Skip to content

Commit

Permalink
Contributes to TheAlgorithms#9943 for interpolation search
Browse files Browse the repository at this point in the history
  • Loading branch information
HaidarTech committed Feb 17, 2024
1 parent 5d6846b commit 318b89a
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions searches/interpolation_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,39 @@ def __assert_sorted(collection):
print(f"{target} found at positions: {result}")
else:
print("Not found")

import unittest


class TestInterpolationSearch(unittest.TestCase):
def test_interpolation_search_existing_element(self):
"""
Test interpolation search for an existing element.
"""
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
self.assertEqual(interpolation_search(array, 9), 8) # Index of 9 is 8

def test_interpolation_search_missing_element(self):
"""
Test interpolation search for a missing element.
"""
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
self.assertIsNone(interpolation_search(array, 10)) # 10 is not in the array

def test_interpolation_search_by_recursion_existing_element(self):
"""
Test interpolation search via recursion for an existing element.
"""
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
self.assertEqual(
interpolation_search_by_recursion(array, 9, 0, 8), 8
) # Index of 9 is 8

def test_interpolation_search_by_recursion_missing_element(self):
"""
Test interpolation search via recursion for a missing element.
"""
array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
self.assertIsNone(
interpolation_search_by_recursion(array, 10, 0, 8)
) # 10 is not in the array

0 comments on commit 318b89a

Please sign in to comment.