def test_binary_search(self):
     array = [1, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 6]
     self.assertEqual(10, binary_search(array, 5))
     self.assertEqual(11, binary_search(array, 6))
     self.assertEqual(None, binary_search(array, 7))
     self.assertEqual(None, binary_search(array, -1))
     # Test binary_search_recur
     self.assertEqual(10, binary_search_recur(array, 0, 11, 5))
     self.assertEqual(11, binary_search_recur(array, 0, 11, 6))
     self.assertEqual(-1, binary_search_recur(array, 0, 11, 7))
     self.assertEqual(-1, binary_search_recur(array, 0, 11, -1))
Example #2
0
 def test_missing_elements(self):
     a_list = list(range(100))
     self.assertEqual(-1, binary_search(a_list, 100))
     self.assertEqual(-1, binary_search(a_list, -1))
     self.assertEqual(-1, binary_search(a_list, 150))
Example #3
0
 def test_present_elements(self):
     a_list = list(range(100))
     self.assertEqual(10, binary_search(a_list, 10))
     self.assertEqual(0, binary_search(a_list, 0))
     self.assertEqual(99, binary_search(a_list, 99))
Example #4
0
 def test_empty_list(self):
     self.assertEqual(-1, binary_search([], 5))
Example #5
0
def test_binary_search():
    result = binary_search(5, [1, 2, 3, 4, 5, 6, 7, 8])
    assert result is not None
    assert result == 5
from search.binary_search import binary_search


data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(binary_search(data, 3))
Example #7
0
    def test_exists1(self):
        array = self.generate_array()
        ind = binary_search(array, 12)

        self.assertTrue(ind == 2)
Example #8
0
    def test_not_exist4(self):
        array = self.generate_array()
        ind = binary_search(array, 17)

        self.assertTrue(ind == 4)