Beispiel #1
0
 def test_insert_and_remove(self, quadtree_bounds, max_depth):
     params = quadtree_bounds + (max_depth,)
     qt = Node(*params)
     rect = random_rectangle(quadtree_bounds)
     print 'Testing with {0}, {1}'.format(qt, rect)  # shown if test fails
     assert qt.direct_children == []
     assert qt.get_children() == []
     qt.insert(rect)
     assert qt.get_children() == [rect]
     qt.remove(rect)
     assert qt.direct_children == []
     assert qt.get_children() == []
Beispiel #2
0
 def test_insert_and_remove(self, quadtree_bounds, max_depth):
     params = quadtree_bounds + (max_depth, )
     qt = Node(*params)
     rect = random_rectangle(quadtree_bounds)
     print 'Testing with {0}, {1}'.format(qt, rect)  # shown if test fails
     assert qt.direct_children == []
     assert qt.get_children() == []
     qt.insert(rect)
     assert qt.get_children() == [rect]
     qt.remove(rect)
     assert qt.direct_children == []
     assert qt.get_children() == []
Beispiel #3
0
 def test_insert_small_rect_in_corner(self, max_depth, coords):
     # inserting small rectangle in top left corner should make the
     # quadtree perform maximum number of subdivisions
     qt = Node(0, 0, 1024, 1024, max_depth)
     assert qt._get_depth() == 0
     assert qt._get_number_of_nodes() == 1
     rect = Rectangle(*coords)
     qt.insert(rect)
     assert qt._get_depth() == max_depth
     assert qt._get_number_of_nodes() == 1 + max_depth * 4
     qt.remove(rect)
     assert qt._get_depth() == 0
     assert qt._get_number_of_nodes() == 1
Beispiel #4
0
 def test_insert_small_rect_in_corner(self, max_depth, coords):
     # inserting small rectangle in top left corner should make the
     # quadtree perform maximum number of subdivisions
     qt = Node(0, 0, 1024, 1024, max_depth)
     assert qt._get_depth() == 0
     assert qt._get_number_of_nodes() == 1
     rect = Rectangle(*coords)
     qt.insert(rect)
     assert qt._get_depth() == max_depth
     assert qt._get_number_of_nodes() == 1 + max_depth * 4
     qt.remove(rect)
     assert qt._get_depth() == 0
     assert qt._get_number_of_nodes() == 1
Beispiel #5
0
class Test_insert_and_remove_single_item_once:
    def setup_class(self):
        self.qt = Node(0, 0, 1000, 1000)
        self.rect = Rectangle(20, 20, 40, 40)

    def test_is_empty_after_initialization(self):
        assert self.qt.direct_children == []
        assert self.qt.get_children() == []

    def test_contains_rect_after_insert(self):
        self.qt.insert(self.rect)
        assert self.qt.get_children() == [self.rect]

    def test_is_empty_after_removing(self):
        self.qt.remove(self.rect)
        assert self.qt.direct_children == []
        assert self.qt.get_children() == []
Beispiel #6
0
class Test_insert_and_remove_single_item_once:
    def setup_class(self):
        self.qt = Node(0, 0, 1000, 1000)
        self.rect = Rectangle(20, 20, 40, 40)

    def test_is_empty_after_initialization(self):
        assert self.qt.direct_children == []
        assert self.qt.get_children() == []

    def test_contains_rect_after_insert(self):
        self.qt.insert(self.rect)
        assert self.qt.get_children() == [self.rect]

    def test_is_empty_after_removing(self):
        self.qt.remove(self.rect)
        assert self.qt.direct_children == []
        assert self.qt.get_children() == []
Beispiel #7
0
    def test(self, n_elems, quadtree_bounds, max_depth, removal_order):
        rects = [random_rectangle(quadtree_bounds) for _ in range(n_elems)]
        params = quadtree_bounds + (max_depth,)
        qt = Node(*params)
        [qt.insert(r) for r in rects]
        key = lambda r: r.bounds
        assert sorted(qt.get_children(), key=key) == sorted(rects, key=key)
        assert len(qt.get_children()) == n_elems

        pop_element = [
            lambda: rects.pop(0),
            lambda: rects.pop(),
            lambda: rects.pop(randint(0, len(rects) - 1)),
        ][removal_order]

        while rects:
            rect = pop_element()
            qt.remove(rect)
            assert sorted(qt.get_children(), key=key) == sorted(rects, key=key)

        assert qt.get_children() == []
Beispiel #8
0
    def test(self, n_elems, quadtree_bounds, max_depth, removal_order):
        rects = [random_rectangle(quadtree_bounds) for _ in range(n_elems)]
        params = quadtree_bounds + (max_depth, )
        qt = Node(*params)
        [qt.insert(r) for r in rects]
        key = lambda r: r.bounds
        assert sorted(qt.get_children(), key=key) == sorted(rects, key=key)
        assert len(qt.get_children()) == n_elems

        pop_element = [
            lambda: rects.pop(0),
            lambda: rects.pop(),
            lambda: rects.pop(randint(0,
                                      len(rects) - 1)),
        ][removal_order]

        while rects:
            rect = pop_element()
            qt.remove(rect)
            assert sorted(qt.get_children(), key=key) == sorted(rects, key=key)

        assert qt.get_children() == []
Beispiel #9
0
    def test_insert_two_at_different_locations(self):
        qt = Node(0, 0, 1000, 1000)
        rect1 = Rectangle(10, 10, 20, 20)
        rect2 = Rectangle(800, 800, 900, 900)

        qt.insert(rect1)
        qt.insert(rect2)
        assert qt.get_children() == [rect1, rect2]

        qt.remove(rect1)
        assert qt.get_children() == [rect2]

        qt.insert(rect1)
        assert qt.get_children() == [rect1, rect2]

        qt.remove(rect2)
        assert qt.get_children() == [rect1]

        qt.remove(rect1)
        assert qt.get_children() == []
        assert qt.direct_children == []
Beispiel #10
0
    def test_insert_two_at_different_locations(self):
        qt = Node(0, 0, 1000, 1000)
        rect1 = Rectangle(10, 10, 20, 20)
        rect2 = Rectangle(800, 800, 900, 900)

        qt.insert(rect1)
        qt.insert(rect2)
        assert qt.get_children() == [rect1, rect2]

        qt.remove(rect1)
        assert qt.get_children() == [rect2]

        qt.insert(rect1)
        assert qt.get_children() == [rect1, rect2]

        qt.remove(rect2)
        assert qt.get_children() == [rect1]

        qt.remove(rect1)
        assert qt.get_children() == []
        assert qt.direct_children == []