Beispiel #1
0
 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))
Beispiel #2
0
 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))
Beispiel #3
0
 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))
Beispiel #4
0
 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())
Beispiel #5
0
 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)
Beispiel #6
0
    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))
Beispiel #7
0
    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())
Beispiel #8
0
    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())
Beispiel #9
0
    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
Beispiel #10
0
    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)
Beispiel #11
0
    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)
Beispiel #12
0
    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())
Beispiel #13
0
    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)
Beispiel #14
0
 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))
Beispiel #15
0
    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))
Beispiel #16
0
    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))
Beispiel #17
0
    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))
Beispiel #18
0
    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))
Beispiel #19
0
    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))
Beispiel #20
0
    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))
Beispiel #21
0
    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))
Beispiel #22
0
    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))
Beispiel #24
0
    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())
Beispiel #25
0
    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))
Beispiel #27
0
 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))
Beispiel #28
0
    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))
Beispiel #29
0
    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))
Beispiel #30
0
 def test_notEquals_withNotEqualIntervals_shouldReturnTrue(self):
     a = Interval(0, 1)
     b = Interval(2, 0)
     self.assertTrue(a != b)
     self.assertTrue(b != a)