Beispiel #1
0
    def test_all_ones(self):
        SIZELIMIT = 1000
        CHECKS = 10
        for length in range(1, SIZELIMIT):

            mode = random.randrange(4)
            if mode == 0:
                bt = binaryindexedtree.BinaryIndexedTree([1] * length)
            else:
                bt = binaryindexedtree.BinaryIndexedTree(length)
                if mode == 1: p = 0
                elif mode == 2: p = 1
                elif mode == 3: p = random.random()
                else: raise AssertionError()
                for i in range(length):
                    if random.random() < p:
                        bt.add(i, 1)
                    else:
                        bt[i] = 1

            self.assertEqual(length, len(bt))
            self.assertEqual(length, bt.get_total())
            for _ in range(CHECKS):
                self.assertEqual(1, bt[random.randrange(length)])
                k = random.randrange(length + 1)
                self.assertEqual(k, bt.get_prefix_sum(k))
                start, end = BinaryIndexedTreeTest._rand_start_end(length)
                self.assertEqual(end - start, bt.get_range_sum(start, end))
Beispiel #2
0
    def test_array_constructor_randomly(self):
        TRIALS = 300
        SIZELIMIT = 10000
        CHECKS = 100
        for _ in range(TRIALS):

            length = random.randrange(SIZELIMIT)
            vals = [random.randrange(-1000, 1000) for _ in range(length)]
            cums = [0]
            for x in vals:
                cums.append(cums[-1] + x)

            bt = binaryindexedtree.BinaryIndexedTree(vals)
            self.assertEqual(length, len(bt))
            self.assertEqual(cums[length], bt.get_total())

            for _ in range(CHECKS):
                if length > 0:
                    k = random.randrange(length)
                    self.assertEqual(vals[k], bt[k])
                k = random.randrange(length + 1)
                self.assertEqual(cums[k], bt.get_prefix_sum(k))
                start, end = BinaryIndexedTreeTest._rand_start_end(length)
                self.assertEqual(cums[end] - cums[start],
                                 bt.get_range_sum(start, end))
Beispiel #3
0
    def test_add_and_set_randomly(self):
        TRIALS = 100
        SIZELIMIT = 3000
        OPERATIONS = 3000
        CHECKS = 100
        for _ in range(TRIALS):

            length = random.randrange(1, SIZELIMIT)
            if random.randrange(2) == 0:
                vals = [0] * length
                bt = binaryindexedtree.BinaryIndexedTree(length)
            else:
                vals = [random.randrange(-1000, 1000) for _ in range(length)]
                bt = binaryindexedtree.BinaryIndexedTree(vals)

            for _ in range(OPERATIONS):
                k = random.randrange(length)
                x = random.randrange(-1000, 1000)
                if random.randrange(2) == 0:
                    vals[k] += x
                    bt.add(k, x)
                else:
                    vals[k] = x
                    bt[k] = x

            cums = [0]
            for x in vals:
                cums.append(cums[-1] + x)

            for _ in range(CHECKS):
                k = random.randrange(length)
                self.assertEqual(vals[k], bt[k])
                k = random.randrange(length + 1)
                self.assertEqual(cums[k], bt.get_prefix_sum(k))
                start, end = BinaryIndexedTreeTest._rand_start_end(length)
                self.assertEqual(cums[end] - cums[start],
                                 bt.get_range_sum(start, end))
Beispiel #4
0
    def test_size_constructor(self):
        SIZELIMIT = 1000
        CHECKS = 10
        for length in range(SIZELIMIT):

            bt = binaryindexedtree.BinaryIndexedTree(length)
            self.assertEqual(length, len(bt))
            self.assertEqual(0, bt.get_total())

            for _ in range(CHECKS):
                if length > 0:
                    self.assertEqual(0, bt[random.randrange(length)])
                self.assertEqual(
                    0, bt.get_prefix_sum(random.randrange(length + 1)))
                start, end = BinaryIndexedTreeTest._rand_start_end(length)
                self.assertEqual(0, bt.get_range_sum(start, end))