def test_binary_search_nums_positive_and_negative_unsorted(self): self.assertEqual(binary_search([-12, -19, -20, 45, 91, 55], 45), 'Value was found within the list.') self.assertEqual(binary_search([-12, -19, -20, 45, 91, 55], 72), 'Value was not found within the list.') self.assertEqual(binary_search([-12, -19, -20, 45, 91, 55], -12), 'Value was found within the list.') self.assertEqual(binary_search([-12, -19, -20, 45, 91, 55], -72), 'Value was not found within the list.')
def test_binary_search(self): testlist = [ 0, 1, 2, 8, 13, 17, 19, 32, 42, ] self.assertEqual(b.binary_search(testlist, 3), False) self.assertEqual(b.binary_search(testlist, 13), True)
def test_binary_search_no_list(self): self.assertEqual(binary_search(None, 1), 'Nothing to search through.') self.assertEqual(binary_search([], 1), 'Nothing to search through.')
def test_binary_search_nums_positive_unsorted(self): self.assertEqual(binary_search([20, 12, 45, 19, 91, 55], 20), 'Value was found within the list.') self.assertEqual(binary_search([20, 12, 45, 19, 91, 55], 72), 'Value was not found within the list.')
def test_binary_search_nums_negative_unsorted(self): self.assertEqual(binary_search([-20, -12, -45, -19, -91, -55], -45), 'Value was found within the list.') self.assertEqual(binary_search([-20, -12, -45, -19, -91, -55], -72), 'Value was not found within the list.')
def test_binary_search_not_num_value(self): self.assertEqual(binary_search([0, 1, 2], 'a'), 'Can only search for numbers.') self.assertEqual(binary_search(['a', 'b', 'c'], 'a'), 'Can only search for numbers.')
def test_binary_search_single_value(self): self.assertEqual(binary_search([0], 0), 'Value was found within the list.') self.assertEqual(binary_search([1], 1), 'Value was found within the list.') self.assertEqual(binary_search([1], 2), 'Value was not found within the list.')
def test_binary_search_no_value(self): self.assertEqual(binary_search([1, 2, 3], None), 'Nothing to search for.')
def test_binary_search_not_nums_list(self): self.assertEqual(binary_search([0, 'a', 3], 3), 'Can only search through lists of just numbers.') self.assertEqual(binary_search(['a', 'b', 'c'], 0), 'Can only search through lists of just numbers.')
def test_search_failed(self): self.assertEqual(-1, binary_search([1,3,7,30,60,100,121], 42))
def test_search_empty(self): self.assertEqual(-1, binary_search([], 42))
def test_search_success_second_half(self): self.assertEqual(4, binary_search([1,3,9,16,42,69], 42))
def test_search_success_first_half(self): self.assertEqual(2, binary_search([1,4,42,57,67,100], 42))
import numpy as np if __name__ == "__main__": item = 3402 for j in range(0, 50): A = np.loadtxt('/home/misha/python/sorts/merge/1000000/{0}.txt'.format(j), dtype="int").tolist() A.sort() inter_time = 0 binary_time = 0 for i in range(0, 10): binary_start = time.time() binary_index, binary_k = binary_search(A, item) binary_end = time.time() binary_time += (binary_end - binary_start) for i in range(0, 10): inter_start = time.time() inter_index, inter_k = interpolation_search(A, item) inter_end = time.time() inter_time += (inter_end - inter_start) assert inter_index == binary_index print("{} | {} | {:f} | {} | {:f} | {} |".format(j, inter_k, float(inter_time), binary_k, float(binary_time), binary_index)) print("------------------------")