Peano
Loading...
Searching...
No Matches
test_splittrees_bottom_up_propagation.py
Go to the documentation of this file.
1import itertools
2import pytest
3
4from toolbox.loadbalancing.api.input.RegularGridGenerator import RegularGridGenerator
5import toolbox.loadbalancing.api.strategies.SplitTrees as split_strategy
6
7
8@pytest.mark.parametrize(
9 "dimensions,target_subtree,parent_coords,mixed_parent_coords",
10 [
11 (2, 1, (1, 1), (0, 0)),
12 (3, 2, (1, 1, 1), (0, 0, 0)),
13 ],
14)
16 dimensions, target_subtree, parent_coords, mixed_parent_coords
17):
18 """
19 Stage II ("Bottom-Up Propagation") invariant test in 2D and 3D.
20
21 If all existing children of a parent belong to the same subtree, then after
22 Stage II the parent has to be assigned to that subtree as well.
23 """
24 # Arrange: tree with 2 levels: L0 = 3^d, L1 = 9^d (children)
25 tree = RegularGridGenerator(name=f"pytest-tree-{dimensions}d", max_level=2, dimensions=dimensions)
26
27 parent_level = 0
28 child_level = 1
29
30 # Parent that should be promoted
31 assert tree._tree_arrays[parent_level][parent_coords] == (1, 0)
32
33 # Make all children of parent_coords belong to target_subtree
34 ranges = [range(3 * coord, 3 * coord + 3) for coord in parent_coords]
35 for child_coords in itertools.product(*ranges):
36 tree._tree_arrays[child_level][child_coords] = (1, target_subtree)
37
38 # Parent that should remain unchanged due to mixed children
39 assert tree._tree_arrays[parent_level][mixed_parent_coords] == (1, 0)
40
41 # Introduce mixed assignments under mixed_parent_coords:
42 # some children in target_subtree, at least one child still in subtree 0.
43 mixed_child_base = tuple(3 * coord for coord in mixed_parent_coords)
44 first_child = mixed_child_base
45 second_child = mixed_child_base[:-1] + (mixed_child_base[-1] + 1,)
46 third_child = mixed_child_base[:-1] + (mixed_child_base[-1] + 2,)
47
48 tree._tree_arrays[child_level][first_child] = (1, target_subtree)
49 tree._tree_arrays[child_level][second_child] = (1, target_subtree)
50 tree._tree_arrays[child_level][third_child] = (1, 0)
51
52 # Act: run Stage II only
53 split_strategy.propagate_bottom_up(tree)
54
55 # Assert: uniform-children parent is promoted
56 assert tree._tree_arrays[parent_level][parent_coords] == (1, target_subtree)
57
58 # Assert: mixed-children parent remains unchanged
59 assert tree._tree_arrays[parent_level][mixed_parent_coords] == (1, 0)
test_bottom_up_propagation_promotes_parent_if_all_children_same_subtree(dimensions, target_subtree, parent_coords, mixed_parent_coords)
Stage II ("Bottom-Up Propagation") invariant test in 2D and 3D.