def test_search_not_found(self): self.DATA_SIZE = 1000 seed(0) self.data = sample(range(self.DATA_SIZE * 3), k=self.DATA_SIZE) self.data.sort() result = linear_search(self.data, self.DATA_SIZE * 4) self.assertFalse(result) result = recursive_binary_search(self.data, self.DATA_SIZE * 4) self.assertFalse(result) result = jump_search(self.data, self.DATA_SIZE * 4) self.assertFalse(result)
def test_search_not_found(): gen = make_data() data = next(gen) result = linear_search(data, DATA_SIZE * 4) assert not result result = binary_search(data, DATA_SIZE * 4) assert not result result = jump_search(data, DATA_SIZE * 4) assert not result
def test_search_at_middle(): gen = make_data() data = next(gen) result = linear_search(data, data[(DATA_SIZE // 2) - 1]) assert result result = binary_search(data, data[(DATA_SIZE // 2) - 1]) assert result result = jump_search(data, data[(DATA_SIZE // 2) - 1]) assert result
def test_search_at_beginning(): gen = make_data() data = next(gen) result = linear_search(data, data[0]) assert result result = binary_search(data, data[0]) assert result result = jump_search(data, data[0]) assert result
def test_search_at_middle(self): self.DATA_SIZE = 1000 seed(0) self.data = sample(range(self.DATA_SIZE * 3), k=self.DATA_SIZE) self.data.sort() result = linear_search(self.data, self.data[(self.DATA_SIZE // 2) - 1]) self.assertTrue(result) result = recursive_binary_search(self.data, self.data[(self.DATA_SIZE // 2) - 1]) self.assertTrue(result) result = jump_search(self.data, self.data[(self.DATA_SIZE // 2) - 1]) self.assertTrue(result)
def test_search_at_end(): gen = make_data() data = next(gen) start = time.perf_counter() result = binary_search(data, data[-1]) fastest = time.perf_counter() - start assert result start = time.perf_counter() result = linear_search(data, data[-1]) slowest = time.perf_counter() - start assert result assert fastest * 10000 < slowest * 10000 start = time.perf_counter() result = jump_search(data, data[-1]) fastest = time.perf_counter() - start assert result assert fastest * 10000 < slowest * 10000
def test_search_at_end(self): self.DATA_SIZE = 1000000 seed(0) self.data = sample(range(self.DATA_SIZE * 3), k=self.DATA_SIZE) self.data.sort() start = time.perf_counter() result = recursive_binary_search(self.data, self.data[-1]) fastest = time.perf_counter() - start self.assertTrue(result) start = time.perf_counter() result = linear_search(self.data, self.data[-1]) slowest = time.perf_counter() - start self.assertTrue(result) self.assertLess(fastest * 10000, slowest * 10000) start = time.perf_counter() result = jump_search(self.data, self.data[-1]) fastest = time.perf_counter() - start self.assertTrue(result) self.assertLess(fastest * 10000, slowest * 10000)