def assertRandomItems(self, *, total, take_factor, seed): import random ls = list(range(total)) rng = random.Random(seed) rng.shuffle(ls) ls_all = ls[:] del ls[int(total * take_factor):] r = RangeTree(min=0, max=total - 1) for pass_nr in range(4): self.assertEqual(r.is_empty(), True) if pass_nr == 0: pass elif pass_nr == 1: ls.reverse() elif pass_nr == 2: ls.sort() elif pass_nr == 3: ls.reverse() for i in ls: r.take(i) items_a = set(ls) items_b = set(range(1, total + 1)) - items_a for i in sorted(items_a): self.assertEqual(r.has(i), True) for i in sorted(items_b): self.assertEqual(r.has(i), False) # test retake for i in ls_all: test = r.retake(i) self.assertEqual(test, i not in ls) for i in ls_all: r.release(i)
def test_complex(self): """ Test complex pairs """ r = RangeTree(min=-10, max=11) # 2 passes on the same data structure. for _ in range(2): self.assertEqual(r.is_empty(), True) for i in (-10, 10, 11): r.take(i) self.assertEqual(r.is_empty(), False) self.assertEqual(list(r.range_iter()), [(-10, -10), (10, 11)]) for i in (-8, -7, 8): r.take(i) self.assertEqual(list(r.range_iter()), [(-10, -10), (-8, -7), (8, 8), (10, 11)]) for i in (-9, 9): r.take(i) self.assertEqual(list(r.range_iter()), [(-10, -7), (8, 11)]) for i in (-9, 9): r.release(i) self.assertEqual(list(r.range_iter()), [(-10, -10), (-8, -7), (8, 8), (10, 11)]) for i in (8, 10, 11): r.release(i) self.assertEqual(list(r.range_iter()), [(-10, -10), (-8, -7)]) for i in (-10, -8, -7): r.release(i) # empty for next pass self.assertEqual(list(r.range_iter()), [])
def test_many(self): r = RangeTree(min=0, max=9) # 2 passes to be sure for _ in range(2): self.assertEqual(r.is_empty(), True) ls = list(range(0, 10, 2)) for i in ls: r.take(i) self.assertEqual(r.is_empty(), False) self.assertEqual(r.has(ls[0] - 0), True) self.assertEqual(r.has(ls[0] - 1), False) self.assertEqual(r.has(ls[-1] + 0), True) self.assertEqual(r.has(ls[-1] + 1), False) self.assertEqual( list(r.range_iter()), [(0, 0), (2, 2), (4, 4), (6, 6), (8, 8)], ) r.release(ls.pop(0)) r.release(ls.pop(-1)) r.take(3) r.take(5) self.assertEqual(list(r.range_iter()), [(2, 6)]) r.release(2) r.release(6) self.assertEqual(list(r.range_iter()), [(3, 5)]) r.release(4) self.assertEqual(list(r.range_iter()), [(3, 3), (5, 5)]) for i in (3, 5): r.release(i) self.assertEqual(list(r.range_iter()), [])