def test_lengthOrder_withIntervalsWithNotEqualLength_shouldReturnCorrectComparator( self): for _ in range(1000): a = Interval(random.uniform(1, 200), random.uniform(1, 200)) b = Interval(random.uniform(1, 100), random.uniform(500, 1000)) self.assertEqual(-1, Interval.length_order(a, b)) self.assertEqual(1, Interval.length_order(b, a))
def test_hash_withNotEqualIntervals_shouldReturnNotEqualHashCodes(self): for _ in range(1000): a = Interval(random.uniform(1, 200), random.uniform(1, 200)) b = Interval(random.uniform(1, 100), random.uniform(500, 1000)) self.assertNotEqual(a, b) self.assertNotEqual(b, a) self.assertNotEqual(hash(a), hash(b))
def test_maxOrder_withIntervalsWithEqualMaxEndpointAndNotEqualMinEndpoint_shouldCompareByMinEndpoint( self): for _ in range(1000): a = Interval(random.uniform(1, 200), 1500) b = Interval(random.uniform(500, 1000), 1500) self.assertEqual(-1, Interval.max_order(a, b)) self.assertEqual(1, Interval.max_order(b, a))
def test_insert_withValidArgumentAndDynamicInsertionInterface_shouldInsertIntervalIntoTree(self): expected = set() tree = DynamicInsertion(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.all())
def test_contains_withPointNotInInterval_shouldReturnFalse(self): i = Interval(0, 1) for _ in range(1000): a = random.random() + 1.1 b = random.random() - 1 self.assertFalse(i.contains(a)) self.assertFalse(a in i) self.assertFalse(i.contains(b)) self.assertFalse(b in i)
def test_constructor_shouldCreateIntervalBetweenGivenEndpoints(self): i = Interval(0, 1) self.assertEqual(0, i.min) self.assertEqual(1, i.max) self.assertEqual("[0, 1]", str(i)) i = Interval(1, 0) self.assertEqual(0, i.min) self.assertEqual(1, i.max) self.assertEqual("[0, 1]", str(i))
def test_delete_withIntervalNotInTreeAndDynamicDeletionInterface_shouldNotAlterTree(self): intervals = set() for i in range(500): intervals.add(Interval(i, i)) tree = DynamicDeletion(SegmentTree, intervals) for i in range(500, 1000): tree.delete(Interval(i, i)) self.assertEqual(500, len(tree)) self.assertEqual(intervals, tree.all())
def test_delete_withIntervalInTreeAndDynamicDeletionInterface_shouldDeleteIntervalFromTree(self): intervals = set() for i in range(1, 500): intervals.add(Interval(i, i)) tree = DynamicDeletion(SegmentTree, intervals) for i in range(1, 500): tree.delete(Interval(i, i)) intervals.remove(Interval(i, i)) self.assertEqual(499 - i, len(tree)) self.assertEqual(intervals, tree.all())
def __build(self, points, a, b): if a + 1 == b: v = self.__Node(Interval(points[a], points[b])) return v m = (a + b) // 2 v = self.__Node(Interval(points[a], points[b])) v.left = self.__build(points, a, m) v.right = self.__build(points, m, b) return v
def test_contains_withPointInInterval_shouldReturnTrue(self): i = Interval(0, 1) self.assertTrue(i.contains(0)) self.assertTrue(i.contains(1)) self.assertTrue(0 in i) self.assertTrue(1 in i) for _ in range(1000): a = random.random() self.assertTrue(i.contains(a)) self.assertTrue(a in i)
def test_equals_withEqualIntervals_shouldReturnTrue(self): a = Interval(0, 1) b = Interval(1, 0) self.assertTrue(a == b) self.assertTrue(b == a) b = Interval(0, 1) self.assertTrue(a == b) self.assertTrue(b == a) b = a self.assertTrue(a == b) self.assertTrue(b == a)
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_notEquals_withEqualIntervals_shouldReturnFalse(self): a = Interval(0, 1) b = Interval(1, 0) self.assertFalse(a != b) self.assertFalse(b != a) b = Interval(0, 1) self.assertFalse(a != b) self.assertFalse(b != a) b = a self.assertFalse(a != b) self.assertFalse(b != a)
def test_query_withPointInSomeIntervalsAndDynamicInsertionInterface_shouldReturnSetWithIntervalsThatContainQueryPoint(self): tree = DynamicInsertion(SegmentTree) point = random.uniform(0, 1000) expected = set() 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))
def test_query_withPointInSomeIntervals_shouldReturnSetWithIntervalsThatContainQueryPoint(self): point = random.uniform(0, 1000) expected = set() intervals = list() for i in range(1000): 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) intervals.append(a) intervals.append(b) tree = IntervalTree(intervals) self.assertEqual(expected, tree.query(point))
def test_constructor_withNotEqualIntervals_shouldCreateTreeWithAllGivenIntervals(self): intervals = list() for i in range(1000): intervals.append(Interval(i, i)) tree = IntervalTree(intervals) self.assertEqual(1000, len(tree))
def test_hash_withEqualIntervals_shouldReturnEqualHashCodes(self): a = Interval(0, 1) b = Interval(0, 1) self.assertEqual(a, b) self.assertEqual(b, a) self.assertEqual(hash(a), hash(b)) b = Interval(1, 0) self.assertEqual(a, b) self.assertEqual(b, a) self.assertEqual(hash(a), hash(b)) b = a self.assertEqual(a, b) self.assertEqual(b, a) self.assertEqual(hash(a), hash(b))
def test_constructor_withCopiesOfOnlyOneInterval_shouldIgnoreCopiesAndConstructOneSizedTree(self): intervals = list() for _ in range(1000): intervals.append(Interval(1, 1)) tree = IntervalTree(intervals) self.assertEqual(1, len(tree))
def test_maxOrder_withEqualIntervals_shouldReturnZero(self): a = Interval(1, 0) b = Interval(1, 0) self.assertEqual(0, Interval.max_order(a, b)) self.assertEqual(0, Interval.max_order(b, a)) b = Interval(0, 1) self.assertEqual(0, Interval.max_order(a, b)) self.assertEqual(0, Interval.max_order(b, a)) b = a self.assertEqual(0, Interval.max_order(a, b)) self.assertEqual(0, Interval.max_order(b, a))
def test_lengthOrder_withEqualLengthIntervals_shouldReturnZero(self): a = Interval(1, 0) b = Interval(1, 0) self.assertEqual(0, Interval.length_order(a, b)) self.assertEqual(0, Interval.length_order(b, a)) b = Interval(0, 1) self.assertEqual(0, Interval.length_order(a, b)) self.assertEqual(0, Interval.length_order(b, a)) b = a self.assertEqual(0, Interval.length_order(a, b)) self.assertEqual(0, Interval.length_order(b, a))
def test_constructor_withDuplicatedIntervals_shouldIgnoreCopiesWhenBuildingTree(self): intervals = list() for i in range(500): for _ in range(10): intervals.append(Interval(i, i)) tree = IntervalTree(intervals) self.assertEqual(500, len(tree))
def test_query_withNoneTypeArgument_shouldRaiseValueError(self): intervals = list() for i in range(10): intervals.append(Interval(i, i)) tree = IntervalTree(intervals) with self.assertRaises(ValueError): tree.query(None)
def test_constructor_withCopiesOfOneFiveHundredIntervals_shouldIgnoreCopiesAndConstructFiveHundredSizedTree( self): intervals = list() for i in range(500): for _ in range(10): intervals.append(Interval(i, i)) tree = SegmentTree(intervals) self.assertEqual(500, len(tree))
def test_constructor_withDynamicDeletionInterfaceAndDuplicatedIntervalsInData_shouldRemoveDuplicatesAndCreateTree(self): intervals = list() for i in range(500): for _ in range(10): intervals.append(Interval(i, i)) tree = DynamicDeletion(SegmentTree, intervals) self.assertEqual(500, len(tree)) self.assertEqual(set(intervals), tree.all())
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_withPointInSomeIntervalsAndDynamicInsertionDeletionInterface_shouldReturnSetWithIntervalsThatContainQueryPoint( self): expected = set() point = random.uniform(0, 1000) tree = DynamicInsertionDeletion(IntervalTree) 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_query_withPointNotInAnyIntervalAndDynamicInsertionInterface_shouldReturnEmptySet(self): leftmost = None rightmost = None tree = DynamicInsertion(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_query_withPointNotInAnyInterval_shouldReturnEmptySet(self): leftmost = None rightmost = None intervals = list() for i in range(1000): 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 intervals.append(a) tree = IntervalTree(intervals) self.assertEqual(set(), tree.query(leftmost - 1)) self.assertEqual(set(), tree.query(rightmost + 1))
def test_intersects_withIntervalsThatDoNotIntersect_shouldReturnFalse( self): for _ in range(1000): # Interval in range [0, 1) a = Interval(random.uniform(0, 1), random.uniform(0, 1)) # Interval in range [1, 1000) b = Interval(random.uniform(1, 1000), random.uniform(1, 1000)) # Interval in range [-1000, -0.1) c = Interval(random.uniform(-0.1, -1000), random.uniform(-0.1, -1000)) self.assertFalse(a.intersects(b)) self.assertFalse(a.intersects(c)) self.assertFalse(b.intersects(c))
def test_notEquals_withNotEqualIntervals_shouldReturnTrue(self): a = Interval(0, 1) b = Interval(2, 0) self.assertTrue(a != b) self.assertTrue(b != a)