def test_operator(): a = [5, 4, 3, 2, 1] assert (bsearch(a, 5, op=operator.gt) == 0) assert (bsearch(a, 4, op=operator.gt) == 1) assert (bsearch(a, 3, op=operator.gt) == 2) assert (bsearch(a, 2, op=operator.gt) == 3) assert (bsearch(a, 1, op=operator.gt) == 4)
def advance(self, t): """ Advance to given timestamp (relative) as float, in seconds. """ # translate relative to absolute time t = long(1000 * (t + self.tStart)) key_a = bsearch(self.keys_a, t) entry_a = self.data_a[key_a] key_b = bsearch(self.keys_b, t) entry_b = self.data_b[key_b] self.buffer = (entry_a, entry_b)
def test_bsearch(): a = [0, 1, 2, 4, 8] assert (bsearch(a, -1) == 0) assert (bsearch(a, 0) == 0) assert (bsearch(a, 1) == 1) assert (bsearch(a, 2) == 2) assert (bsearch(a, 4) == 3) assert (bsearch(a, 8) == 4) assert (bsearch(a, 10) == 5)
def test_not_found(): with pytest.raises(ValueError): bsearch.bsearch([1, 2, 3], 0)
def timedBsearchRun(lst, num): start = time.clock() bsearch(lst, num) # num in lst end = time.clock() return end - start
def test_dup(): assert bsearch.bsearch([1, 2, 2, 2, 2, 2, 3, 3], 2) == 3
def test_empty(): with pytest.raises(ValueError): bsearch.bsearch([], 0)
def test_even(): assert bsearch.bsearch([1, 2, 3, 4, 5, 6, 7, 8], 2) == 1
def test_odd(): assert bsearch.bsearch([1, 2, 3, 4, 5, 6, 7], 2) == 1
def timedBsearchRun(lst, num): start = time.clock() bsearch(lst, num) # num in lst end = time.clock() return end - start
def test_0(self): a=[] a.sort() self.assertEqual( bsearch(a,0), 0)
def test_big_array(self): print("In big array test") a = [random.randint(1,10000) for _ in xrange(1000000)] a.sort() self.assertEqual(bsearch(a,20000),0)
def test_small_array(self): a=[12,23,56,78,9,-90,4] a.sort() self.assertEqual( bsearch(a,9), 1)
def test_bfs(self): import bsearch self.assertEqual(bsearch.bsearch(0, 10, lambda x: x >= 6), 6)