def test_insert_withValidArgumentAndDynamicInsertionDeletionInterface_shouldInsertIntervalIntoTree(self): expected = set() tree = DynamicInsertionDeletion(SegmentTree) for i in range(1, 1000): tree.insert(Interval(i, i)) expected.add(Interval(i, i)) self.assertEqual(i, len(tree)) self.assertEqual(expected, tree.elements())
def test_query_withEmptyTreeAndDynamicInsertionDeletionInterface_shouldReturnEmptySet(self): tree = DynamicInsertionDeletion(SegmentTree) for _ in range(500): a = random.uniform(0, 1000) self.assertEqual(set(), tree.query(a)) for i in range(500): point = random.uniform(0, 1000) a = Interval(i, i) tree.insert(a) tree.delete(a) self.assertEqual(set(), tree.query(point))
def test_query_withPointNotInAnyIntervalAndDynamicInsertionDeletionInterface_shouldReturnEmptySet(self): leftmost = None rightmost = None tree = DynamicInsertionDeletion(SegmentTree) for i in range(500): a = Interval(random.uniform(0, i), random.uniform(i, 1000)) if leftmost is None or a.min < leftmost: leftmost = a.min if rightmost is None or a.max > rightmost: rightmost = a.max tree.insert(a) self.assertEqual(set(), tree.query(leftmost - 1)) self.assertEqual(set(), tree.query(rightmost + 1))
def test_delete_withIntervalInTreeAndDynamicInsertionDeletionInterface_shouldDeleteIntervalFromTree(self): tree = DynamicInsertionDeletion(SegmentTree) expected = set() for i in range(1, 500): expected.add(Interval(i, i)) tree.insert(Interval(i, i)) for i in range(1, 500): tree.delete(Interval(i, i)) expected.remove(Interval(i, i)) self.assertEqual(499 - i, len(tree)) self.assertEqual(expected, tree.elements())
def test_query_withPointInSomeIntervalsAndDynamicInsertionDeletionInterface_shouldReturnSetWithIntervalsThatContainQueryPoint(self): expected = set() point = random.uniform(0, 1000) tree = DynamicInsertionDeletion(SegmentTree) for i in range(500): a = Interval(i, 1000) b = Interval(random.uniform(0, 1000), random.uniform(0, 1000)) if point in a: expected.add(a) if point in b: expected.add(b) tree.insert(a) tree.insert(b) self.assertEqual(expected, tree.query(point)) while len(expected) > 0: x = expected.pop() tree.delete(x) self.assertEqual(expected, tree.query(point))
def test_insert_withNoneTypeArgumentAndDynamicInsertionDeletionInterface_shouldRaiseValueError( self): tree = DynamicInsertionDeletion(IntervalTree) with self.assertRaises(ValueError): tree.insert(None)