def test_traverse_with_condition(self): """ Tests traversing a tree with a condition function. Only nodes that pass the condition are traversed. If the condition returns False at the parent level, the child nodes should not be traversed. :return: """ # Arrange nodes = dict() t = create_complex_tree(self, nodes) def condition(node: RTreeNode): return node.is_root or node.get_bounding_rect().max_x <= 10 # Act result = t.traverse(_yield_node, condition) # Assert # Note node "L2" should not be present, even though it passed the condition, since its parent I2 did not. self.assertEqual( [ nodes['R'], # Root nodes['I1'], # Intermediate child 1 nodes['L1'], # Leaf child 1 nodes['L2'] # Leaf child 2 ], list(result))
def test_query_rect_no_matches(self): """Tests query method with a Rect location returning no matches.""" # Arrange t = create_complex_tree(self) r = Rect(4, 5, 5, 7) # Act result = list(t.query(r)) # Assert self.assertEqual(0, len(result))
def test_query_point_list_multiple_matches(self): """Tests query method with a point location passed in as a list of 2 coordinates returning multiple matches.""" # Arrange t = create_complex_tree(self) loc = [1.5, 5.5] # Act result = list(t.query(loc)) # Assert self.assertCountEqual(['f', 'j'], [e.data for e in result])
def test_query_point_multiple_matches(self): """Tests query method with a Point location returning multiple matches.""" # Arrange t = create_complex_tree(self) loc = Point(1.5, 1.5) # Act result = list(t.query(loc)) # Assert self.assertCountEqual(['a', 'b'], [e.data for e in result])
def test_query_point_no_matches(self): """Tests query method with a Point location returning no matches.""" # Arrange t = create_complex_tree(self) loc = Point(5, 8) # Act result = list(t.query(loc)) # Assert self.assertEqual(0, len(result))
def test_query_nodes_rect_no_matches(self): """Tests query_nodes method with a Rect location returning no matches""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) loc = [4, 5, 6, 10] # Act result = list(t.query_nodes(loc)) # Assert self.assertEqual(0, len(result))
def test_query_point_on_border_matches(self): """Ensures that a point that is on the border (but not within) an entry MBR matches.""" # Arrange t = create_complex_tree(self) loc = Point(4, 4) # Act result = list(t.query(loc)) # Assert self.assertEqual(1, len(result)) self.assertEqual('c', result[0].data)
def test_query_point_tuple_single_match(self): """Tests query method with a point tuple location returning a single match.""" # Arrange t = create_complex_tree(self) loc = (8, 3) # Act result = list(t.query(loc)) # Assert self.assertEqual(1, len(result)) self.assertEqual('h', result[0].data)
def test_query_nodes_intermediate_levels_no_matches(self): """Tests query_nodes method with leaves=False (returning intermediate nodes), returning no matches""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) loc = (12, 12) # Act result = list(t.query_nodes(loc, leaves=False)) # Assert self.assertEqual(0, len(result))
def test_query_nodes_rect_single_match(self): """Tests query_nodes method with a Rect location returning a single match""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) loc = Rect(6, 4, 8, 6) # Act result = list(t.query_nodes(loc)) # Assert self.assertCountEqual([nodes['L3']], result)
def test_query_adjacent_rect_does_not_match(self): """ Ensures that a query for a rect that is adjacent to but does not intersect with an entry does not match. """ # Arrange t = create_complex_tree(self) r = Rect(5, 0, 9, 2) # Act result = list(t.query(r)) # Assert self.assertEqual(0, len(result))
def test_query_nodes_intermediate_levels_single_match(self): """Tests query_nodes method with leaves=False (returning intermediate nodes), returning a single match.""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) loc = (11, 10) # Act result = list(t.query_nodes(loc, leaves=False)) # Assert self.assertEqual(1, len(result)) self.assertEqual(t.root, result[0])
def test_query_nodes_rect_multiple_matches(self): """Tests query_nodes method with a Rect location returning multiple matches""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) L3, L4 = nodes['L3'], nodes['L4'] loc = (5, 0, 8, 1) # Act result = list(t.query_nodes(loc)) # Assert self.assertCountEqual([L3, L4], result)
def test_query_nodes_intermediate_levels_multiple_matches(self): """Tests query_nodes method with leaves=False (returning intermediate nodes), returning multiple matches.""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) R, I1, L1 = nodes['R'], nodes['I1'], nodes['L1'] loc = Rect(3, 9, 4, 10) # Act result = list(t.query_nodes(loc, leaves=False)) # Assert self.assertCountEqual([R, I1, L1], result)
def test_query_rect_overlap_multiple_matches(self): """ Tests query method with a Rect location returning a multiple matches. Rectangle overlaps but is not equal to any of the matched entries. """ # Arrange t = create_complex_tree(self) r = Rect(4, 3, 8, 5) # Act result = list(t.query(r)) # Assert self.assertCountEqual(['c', 'h'], [e.data for e in result])
def test_query_root_mbr_returnes_all_entries(self): """ Ensures that a query for a rect that matches the bounding rectangle of the root node returns all entries. """ # Arrange t = create_complex_tree(self) # Act result = list(t.query(t.root.get_bounding_rect())) # Assert self.assertCountEqual( ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], [e.data for e in result])
def test_query_rect_contains_multiple_matches(self): """ Tests query method with a Rect location returning multiple matches. Rectangle wholly contains the MBRs of all matched entries. """ # Arrange t = create_complex_tree(self) r = [5.5, 6, 12, 13.5] # Act result = list(t.query(r)) # Assert self.assertCountEqual(['d', 'e'], [e.data for e in result])
def test_query_rect_list_overlap_multiple_matches(self): """ Tests query method with a Rect location passed in as a list of coordinates, returning a multiple matches. Rectangle overlaps but is not equal to any of the matched entries. """ # Arrange t = create_complex_tree(self) r = [0.5, 6, 2, 7.5] # Act result = list(t.query(r)) # Assert self.assertCountEqual(['f', 'j'], [e.data for e in result])
def test_search_nodes_single_match(self): """Tests search_nodes method with leaves=True and a condition that results in a single leaf node matching""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) def condition(node: RTreeNode): return node.get_bounding_rect().intersects(Rect(0, 9, 1, 10)) # Act result = list(t.search_nodes(condition)) # Assert self.assertCountEqual([nodes['L1']], result)
def test_search_nodes_no_matches(self): """Tests search_nodes method with leaves=True and a condition that results in no leaf nodes matching""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) def condition(node: RTreeNode): return node.get_bounding_rect() == Rect(0, 5, 10, 10) # Act result = list(t.search_nodes(condition)) # Assert self.assertEqual(0, len(result))
def test_search_nodes_multiple_matches(self): """Tests search_nodes method with leaves=True and a condition that results in multiple leaf nodes matching""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) L1, L4 = nodes['L1'], nodes['L4'] def condition(node: RTreeNode): return node.get_bounding_rect().min_x == 0 # Act result = list(t.search_nodes(condition)) # Assert self.assertCountEqual([L1, L4], result)
def test_query_rect_equals_single_match(self): """ Tests query method with a Rect location returning a single match. Rectangle exactly matches the MBR of matched entry. """ # Arrange t = create_complex_tree(self) r = (2, 2, 6, 4) # Act result = list(t.query(r)) # Assert self.assertEqual(1, len(result)) self.assertEqual('c', result[0].data)
def test_query_rect_overlap_single_match(self): """ Tests query method with a Rect location returning a single match. Rectangle overlaps but is not equal to matched entry. """ # Arrange t = create_complex_tree(self) r = Rect(4, 3, 6, 5) # Act result = list(t.query(r)) # Assert self.assertEqual(1, len(result)) self.assertEqual('c', result[0].data)
def test_search_nodes_intermediate_single_match(self): """ Tests search_nodes method with leaves=False (return intermediate matches) and a condition that results in a single intermediate node matching. """ # Arrange nodes = dict() t = create_complex_tree(self, nodes) def condition(node: RTreeNode): return node.get_bounding_rect() == Rect(0, 0, 11, 10) # Act result = list(t.search_nodes(condition, leaves=False)) # Assert self.assertCountEqual([nodes['R']], result)
def test_search_nodes_intermediate_no_matches(self): """ Tests search_nodes method with leaves=False (return intermediate matches) and a condition that results in no leaf nodes matching. """ # Arrange nodes = dict() t = create_complex_tree(self, nodes) def condition(node: RTreeNode): return node.get_bounding_rect() == Rect(8, 7, 10, 9) # Act result = list(t.search_nodes(condition, leaves=False)) # Assert self.assertEqual(0, len(result))
def test_search_nodes_intermediate_multiple_matches(self): """ Tests search_nodes method with leaves=False (return intermediate matches) and a condition that results in multiple intermediate and leaf nodes matching. """ # Arrange nodes = dict() t = create_complex_tree(self, nodes) R, I2, L3, L4 = nodes['R'], nodes['I2'], nodes['L3'], nodes['L4'] def condition(node: RTreeNode): return node.get_bounding_rect().intersects(Rect(4, 1, 8, 3)) # Act result = list(t.search_nodes(condition, leaves=False)) # Assert self.assertCountEqual([R, I2, L3, L4], result)
def test_traverse_level_order(self): """Tests that nodes are traversed in level-order when calling traverse_level_order""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) def fn(n: RTreeNode, lvl: int): yield (lvl, n) # Act result = list(t.traverse_level_order(fn)) # Assert # Root node should be visited first level, node = result[0] self.assertEqual(0, level) self.assertEqual(nodes['R'], node) # Intermediate child 1 should be visited next level, node = result[1] self.assertEqual(1, level) self.assertEqual(nodes['I1'], node) # Intermediate child 2 should be visited next level, node = result[2] self.assertEqual(1, level) self.assertEqual(nodes['I2'], node) # Leaf child 1 should be visited next level, node = result[3] self.assertEqual(2, level) self.assertEqual(nodes['L1'], node) # Leaf child 2 should be visited next level, node = result[4] self.assertEqual(2, level) self.assertEqual(nodes['L2'], node) # Leaf child 3 should be visited next level, node = result[5] self.assertEqual(2, level) self.assertEqual(nodes['L3'], node) # Leaf child 4 should be visited last level, node = result[6] self.assertEqual(2, level) self.assertEqual(nodes['L4'], node) # Ensure there are no more results self.assertEqual(7, len(result))
def test_traverse_level_order_with_condition(self): """Tests traverse_level_order with a condition function.""" # Arrange nodes = dict() t = create_complex_tree(self, nodes) def fn(n: RTreeNode, lvl: int): yield (lvl, n) def condition(n: RTreeNode, lvl: int): mbr = n.get_bounding_rect() max_x = mbr.max_x max_y = mbr.max_y return lvl == 0 or max_x == 10 or max_y == 5 # Act result = list(t.traverse_level_order(fn, condition)) # Assert # Root node should be visited first level, node = result[0] self.assertEqual(0, level) self.assertEqual(nodes['R'], node) # Intermediate child 1 should be visited next level, node = result[1] self.assertEqual(1, level) self.assertEqual(nodes['I1'], node) # Intermediate child 2 should be visited next level, node = result[2] self.assertEqual(1, level) self.assertEqual(nodes['I2'], node) # Leaf child 2 should be visited next level, node = result[3] self.assertEqual(2, level) self.assertEqual(nodes['L2'], node) # Leaf child 3 should be visited next level, node = result[4] self.assertEqual(2, level) self.assertEqual(nodes['L3'], node) # Ensure there are no more results self.assertEqual(5, len(result))