def test_set(): # Set (completely replace) value at specified index. # BIT.__setitem__ should be O(logN). for length in intensities[INTENSITY]: bit, dummy = bit_dummy(gl(length), bf, ibf) # set 150 random elements. lst = gl(min(150, length)) for rand_value in lst: rand_pos = randint(0, len(bit) - 1) bit[rand_pos] = rand_value dummy[rand_pos] = rand_value for i in range(rand_pos, len(bit)): assert bit[i] == dummy[i] # check index error is raised correctly. b = BIT() with pytest.raises(IndexError): b[10] = None with pytest.raises(IndexError): b[0] = None # check that setitem can't work without # inverse function supplied: l = gl(1) b.append(l[-1]) with pytest.raises(TypeError): b[0] = l[-1]
def test__getitem__slice(): for length in intensities[INTENSITY]: lst = gl(length) bit1, bit2 = BIT(lst, bf, ibf), BIT(lst, bf, ibf) for _ in range(length // 2): index_a = randint(0, length - 2) index_b = randint(index_a, length - 1) # we really just need to check that we handle the slices right assert bit1[index_a:index_b] == bit2.range_sum(index_a, index_b) # check some edge cases assert bit1[:] == bit2.range_sum() assert bit1[:len(bit1) - 1] == bit2.range_sum(j=len(bit2) - 1) assert bit1[len(bit1) // 2:] == bit2.range_sum(i=len(bit2) // 2)
class test_01(unittest.TestCase): arr = [3, 2, -1, 6, 5, 4, -3, 3, 7, 2, 3] exp_bit = [0, 3, 5, -1, 10, 5, 9, -3, 19, 7, 9, 3] bit_arr = [0] * len(arr) bit = BIT(arr) def test_insert(self): self.assertEqual(self.bit._arr, self.exp_bit)
def test_pop(): # Pop from end and assert item popped is the same. for length in intensities[INTENSITY]: bit, dummy = bit_dummy(gl(length), bf, ibf) while bit: assert bit.pop() == dummy.pop() # check the same for random indices, make sure sum # until end is valid after pop. for length in intensities[INTENSITY]: bit, dummy = bit_dummy(gl(length), bf, ibf) while len(bit) > 1: rand_index = randint(0, len(bit) - 2) assert bit.pop(rand_index) == dummy.pop(rand_index) assert bit[-1] == dummy[-1] # case where inverse isn't defined and we raise with pytest.raises(TypeError): b = BIT([gl(1).pop()]) b.pop()
def test_layout_changes(): assert BIT.bit_layout([]) == BIT([], bf, ibf).original_layout() for length in intensities[INTENSITY]: lst = gl(length) assert lst == BIT(lst, bf, ibf).original_layout() # check that we can't build original layout without having an # inverse function defined: b = BIT([]) with pytest.raises(TypeError): b.original_layout()
def test_range_sum(): for length in intensities[INTENSITY]: bit, dummy = bit_dummy(gl(length), bf, ibf) for _ in range(length // 2): index_a = randint(0, length - 2) index_b = randint(index_a, length - 1) assert bit.range_sum(index_a, index_b) == dummy.range_sum(index_a, index_b) # check that we raise for some odd values with pytest.raises(TypeError): b = BIT(range(1)) b.range_sum() with pytest.raises(IndexError): b = BIT(range(10)) b.range_sum(8, 4)
def bit_dummy(lst, binop, inverse): """ Create instances of BIT and dummy version with given list, binary op and inverse binary op. """ return BIT(lst, binop, inverse), DummyPS(lst, binop, inverse)
def bit_dummy(lst, binop, inverse): return BIT(lst, binop, inverse), DummyPS(lst, binop, inverse)