def test_complexity(self): best, _ = big_o( lambda arr: binary_search(arr, arr[randint(0, len(arr) - 1)]), lambda x: list(range(x)), n_repeats=1000) self.assertIsInstance(best, complexities.Logarithmic)
def test_binary_search_no_solution(self): def condition(n): return n >= 200 self.assertIsNone( binary_search.binary_search(condition=condition, initial_left=0, initial_right=100))
def test_binary_search_all_true(self): def condition(n): return True right = random.randint(0, 1000) self.assertEqual( binary_search.binary_search(condition=condition, initial_left=0, initial_right=right), 0, )
def test_binary_search(self): for _ in range(5): N = 10000 solution = random.randint(0, N) def condition(n): return n >= solution self.assertEqual( binary_search.binary_search(condition=condition, initial_left=0, initial_right=N), solution, )
def test_binary_search(self): arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] found = binary_search(5, arr, 0, len(arr)-1) self.assertTrue(found, '5 is in array') found = binary_search(5.5, arr, 0, len(arr)-1) self.assertFalse(found, '5.5 is not in array') found = binary_search(17, arr, 0, len(arr)-1) self.assertFalse(found, '17 is not in array') found = binary_search(15, arr, 0, len(arr)-1) self.assertTrue(found, '15 is in array') arr = [1,2,3,4,4,4,4,4,4,4,4,4,4,5,6,7] found = binary_search(2, arr, 0, len(arr)-1) self.assertTrue(found, '2 is in array') found = binary_search(6, arr, 0, len(arr)-1) self.assertTrue(found, '6 is in array')
def test_find(self): self.assertEqual(binary_search([1, 3, 5, 7, 9], 3), 1)
def test_not_find(self): self.assertEqual(binary_search([1, 2, 3, 4, 5], 6), None)
def test_binary_search_element_not_in_lst(self): self.assertEqual(binary_search([1, 2, 3], 4), -1)
def test_binary_search_with_wrong_input_search(self): with self.assertRaises(AssertionError): binary_search([1, 2, 3], "a")
def test_binary_search_wrong_input_lst(self): with self.assertRaises(AssertionError): binary_search(["a", "b", "c"], 2)