def test_binary_search(): lst = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] item = 5 bSearch = BinarySearch(lst, item) bSearch.binary_search(lst, item) assert(bSearch.lst == 2)
def test_binary_search_one_item_list(): sorted_list = [1] found_result = BinarySearch.search(sorted_list, 1) not_found_result = BinarySearch.search(sorted_list, 2) assert found_result == 0 assert not_found_result is None
def test_binary_search(): sorted_list = [1, 2, 6, 74, 2434, 43232, 3243243] found_result = BinarySearch.search(sorted_list, 6) found_result_right_side = BinarySearch.search(sorted_list, 3243243) not_fount_result = BinarySearch.search(sorted_list, 224) assert found_result == 2 assert found_result_right_side == 6 assert not_fount_result is None
def test_binary_search_empty_list(): sorted_list = [] result = BinarySearch.search(sorted_list, 1) assert result is None
def test_missing_too_large(self): ar = [1, 2, 3, 4, 5, 6, 7, 8, 9] self.assertEqual(-1, BinarySearch.binary_search(ar, 0, len(ar) - 1, 11))
def test_sequence(self): ar = [1, 2, 3, 4, 5, 6, 7, 8, 9] self.assertEqual(2, BinarySearch.binary_search(ar, 0, len(ar) - 1, 3))
def testSequenceOutOfOrder(self): ar = [9, 8, 7, 6, 5, 4, 3, 2, 1] self.assertEqual(-1, BinarySearch.binary_search(ar, 0, len(ar) - 1, 3))
def test_empty_array(self): ar = [] self.assertEqual(-1, BinarySearch.binary_search(ar, 0, len(ar) - 1, 1))
def test_singleton_array(self): ar = [1] self.assertEqual(0, BinarySearch.binary_search(ar, 0, len(ar) - 1, 1))