Пример #1
0
def test_tree_delitem():
    original_tpl = ("a", [
        (2, [
            (3, [("a", [
                (2, [
                    (3, []),
                ]),
            ])]),
        ]),
    ])
    mutated_tpl = ("a", [
        (2, [
            (3, [("a", [])]),
        ]),
    ])
    tree = Tree.from_tuple(original_tpl)

    # Check for default behavior
    del tree[(0, 0, 0, 0)]
    assert tree == Tree.from_tuple(mutated_tpl)

    # Check for IndexError when invalid index is presented
    with pytest.raises(IndexError):
        del tree[(0, 0, 0, 1)]

    # Check for IndexError when invalid type of index is presented
    with pytest.raises(TypeError):
        del tree["a"]

    # Check behavior for a frozen tree
    tree.freeze()
    with pytest.raises(AssertionError):
        del tree[(0, 0, 0)]
Пример #2
0
def test_default_usage():
    tpl = (1, [(2, []), (3, [])])
    tr = Tree(value=1,
              children=(
                  Tree(value=2, children=()),
                  Tree(value=3, children=()),
              ))
    assert tr == Tree.from_tuple(tpl)
Пример #3
0
def test_tree_emplace():
    tpl = (1, [(2, []), (4, [])])
    tree = Tree(value=1, children=(Tree(value=2, children=()), ))

    # Check for default behavior
    tree.emplace(4)
    assert tree == Tree.from_tuple(tpl)

    # Check emplace behavior for a frozen tree
    tree.freeze()
    with pytest.raises(AssertionError):
        tree.emplace(1)
Пример #4
0
def test_tree_str():
    tpl = (1, [
        (2, []),
    ])
    expected_str = "(1 (2))"
    tree = Tree.from_tuple(tpl)
    assert str(tree) == expected_str
Пример #5
0
def test_tree_len():
    original_tpl = ("a", [
        (2, []),
        (2, []),
    ])
    tree = Tree.from_tuple(original_tpl)
    assert len(original_tpl[1]) == len(tree)
Пример #6
0
def test_tree_bfs():
    tpl = ("a", [
        (2, [(3, [("a", []), ("b", [])])]),
    ])
    tpl_bfs_case = [
        '(a (2 (3 (a) (b))))', '(2 (3 (a) (b)))', '(3 (a) (b))', '(a)', '(b)'
    ]
    tpl_bfs_case_mirrored = [
        '(a (2 (3 (a) (b))))', '(2 (3 (a) (b)))', '(3 (a) (b))', '(b)', '(a)'
    ]
    tree = Tree.from_tuple(tpl)

    # Test case for basic bfs
    bfs_case = [str(t) for t in tree.bfs()]
    assert bfs_case == tpl_bfs_case

    # Test case for reversed bfs
    bfs_case_reversed = [str(t) for t in tree.bfs(reverse=True)]
    assert bfs_case_reversed == tpl_bfs_case[::-1]

    # Test case for mirrored bfs
    bfs_case_mirrored = [str(t) for t in tree.bfs(mirror=True)]
    assert bfs_case_mirrored == tpl_bfs_case_mirrored

    # Test case for both reversed and mirrored bfs
    bfs_case_mirrored_reversed = [
        str(t) for t in tree.bfs(reverse=True, mirror=True)
    ]
    assert bfs_case_mirrored_reversed == tpl_bfs_case_mirrored[::-1]
Пример #7
0
def test_tree_size():
    original_tpl = ("a", [(2, [("a", []), ("b", [])]), (3, [
        ("a", []),
    ])])
    tree = Tree.from_tuple(original_tpl)
    assert tree.size() == 6
    tree.freeze()
    assert tree.size() == 6
Пример #8
0
def test_tree_equals():
    tpl_f = (1, [(2, []), (3, [])])
    tpl_t = (1, [
        (2, []),
        (3, [
            (2, []),
        ]),
    ])

    # Check same trees for equality
    tree_1 = Tree.from_tuple(tpl_f)
    tree_2 = Tree.from_tuple(tpl_f)
    assert tree_1 == tree_2

    # Check different trees for equality
    tree_2 = Tree.from_tuple(tpl_t)
    assert tree_1 != tree_2
Пример #9
0
def test_tree_setitem():
    original_tpl = ("a", [
        (2, [
            (3, []),
        ]),
    ])
    mutated_tpl = ("a", [
        ("b", [
            ("c", []),
        ]),
    ])
    tree = Tree.from_tuple(original_tpl)
    tree[0] = Tree(value="b", children=(Tree(value="c", children=()), ))

    # Check for default behavior
    assert tree == Tree.from_tuple(mutated_tpl)

    # Check for IndexError when invalid index is presented
    with pytest.raises(IndexError):
        tree[4] = Tree(value="dd")

    # Check for IndexError when invalid type of index is presented
    with pytest.raises(TypeError):
        tree["a"] = Tree(value="a")

    # Check behavior for a frozen tree
    tree.freeze()
    with pytest.raises(AssertionError):
        tree[0] = Tree(value="a")
Пример #10
0
def test_tree_height():
    tpl = ("a", [
        (2, [
            (3, [("a", [
                (2, [
                    (3, []),
                ]),
            ])]),
        ]),
    ])
    tree = Tree.from_tuple(tpl)
    assert tree.height() == 6
    tree.freeze()
    assert tree.height() == 6
Пример #11
0
def test_tree_freeze():
    tpl = ("a", [
        (2, [
            (3, [("a", [
                (2, [
                    (3, []),
                ]),
            ])]),
        ]),
    ])
    tree = Tree.from_tuple(tpl)
    tree.freeze()
    assert tree._is_frozen == True
    assert tree[0]._is_frozen == True
    assert tree[(0, 0)]._is_frozen == True
Пример #12
0
def test_tree_from_tuple():
    tree_initialized = Tree(value="a",
                            children=(Tree(value=2,
                                           children=(Tree(
                                               value=3,
                                               children=(
                                                   Tree(value="a"),
                                                   Tree(value="b"),
                                               ),
                                           ), )), ))
    tree_from_tuple = Tree.from_tuple(("a", [
        (2, [(3, [("a", []), ("b", [])])]),
    ]))
    assert tree_from_tuple == tree_initialized
Пример #13
0
def test_tree_bfs_extended():
    def extract_result(bfs_result: object) -> Tuple[List[str], List[int]]:
        container = list(bfs_result) if not isinstance(bfs_result,
                                                       list) else bfs_result
        nodes_tuple_ = [str(t[0]) for t in container]
        nodes_positions_ = [t[1] for t in container]
        return nodes_tuple_, nodes_positions_

    tpl = ("a", [
        (2, [(3, [("a", []), ("b", [])])]),
    ])
    tpl_bfs_case_nodes = [
        '(a (2 (3 (a) (b))))', '(2 (3 (a) (b)))', '(3 (a) (b))', '(a)', '(b)'
    ]
    tpl_bfs_case_pos = [1, 2, 3, 4, 4]
    tree = Tree.from_tuple(tpl)

    # Test case for basic bfs_ex
    nodes_tuple, nodes_positions = extract_result(tree.bfs_ex())
    assert nodes_tuple == tpl_bfs_case_nodes
    assert nodes_positions == tpl_bfs_case_pos

    # Test case for specified depth of bfs
    nodes_tuple, nodes_positions = extract_result(tree.bfs_ex(depth=3))
    assert nodes_tuple == tpl_bfs_case_nodes[:3]
    assert nodes_positions == tpl_bfs_case_pos[:3]

    # Test case for specified depth of reversed bfs
    nodes_tuple, nodes_positions = extract_result(
        tree.bfs_ex(depth=3, reverse=True))
    assert nodes_tuple == tpl_bfs_case_nodes[-3:-6:-1]
    assert nodes_positions == tpl_bfs_case_pos[-3:-6:-1]

    # Test case for both reversed and mirrored bfs
    nodes_tuple, nodes_positions = extract_result(
        tree.bfs_ex(reverse=True, mirror=True))
    assert nodes_positions == tpl_bfs_case_pos[::-1]
    tpl_bfs_case_mirrored_nodes = [
        '(a)', '(b)', '(3 (a) (b))', '(2 (3 (a) (b)))', '(a (2 (3 (a) (b))))'
    ]
    assert nodes_tuple == tpl_bfs_case_mirrored_nodes
Пример #14
0
def test_tree_getitem():
    tpl = ("a", [
        (2, [
            (3, []),
        ]),
    ])
    tree = Tree.from_tuple(tpl)

    # Check for single indexing
    assert tree[0].value == tpl[1][0][0]

    # Check for deep indexing
    assert tree[(0, 0)].value == tpl[1][0][1][0][0]

    # Check for IndexError when invalid index is presented
    with pytest.raises(IndexError):
        _ = tree[1]

    # Check for TypeError when invalid type of index is presented
    with pytest.raises(TypeError):
        _ = tree["a"]
Пример #15
0
def test_tree_clone():
    tpl = ([1, 2], [
        (2, [
            (3, [("a", [
                (2, [
                    (3, []),
                ]),
            ])]),
        ]),
    ])
    tree = Tree.from_tuple(tpl)

    # Check behavior for shallow copy
    copied_tree = tree.clone()
    assert copied_tree == tree
    copied_tree.value.append(1)
    assert copied_tree == tree

    # Check behavior for deep copy
    copied_tree = tree.clone(deep=True)
    assert copied_tree == tree
    copied_tree.value.append(1)
    assert copied_tree != tree
Пример #16
0
def test_tree_unfreeze():
    tpl = ("a", [
        (2, [
            (3, [("a", [
                (2, [
                    (3, []),
                ]),
            ])]),
        ]),
    ])
    tree = Tree.from_tuple(tpl)

    # Check for unsafe way of in-place unfreeze
    tree.freeze()
    tree.unfreeze(unsafe=True)
    assert tree._is_frozen == False
    assert tree[(0, 0)]._is_frozen == False

    # Check for copying way of unfreeze
    tree.freeze()
    new_tree = tree.unfreeze()
    assert tree == new_tree
    assert tree._is_frozen == True
    assert new_tree._is_frozen == False
Пример #17
0
def test_tree_repr():
    tpl = (1, [
        (2, []),
    ])
    tree = Tree.from_tuple(tpl)
    assert eval(repr(tree)) == tree
Пример #18
0
def test_freeze_assert():
    with pytest.raises(AssertionError):
        hash(Tree(None))
Пример #19
0
def test_tree_hash():
    tpl = (1, [
        (2, []),
    ])
    tree = Tree.from_tuple(tpl)

    # Check behavior for non-frozen tree
    with pytest.raises(AssertionError):
        _h = hash(tree)

    # Check default behavior
    tree.freeze()
    assert isinstance(hash(tree), int)

    # Check hash equality of equal trees
    initialized_tree = Tree(value=1, children=(Tree(value=2), ))
    initialized_tree.freeze()
    assert hash(tree) == hash(initialized_tree)

    # Check hash equality of extended equal trees
    tree = Tree.from_tuple(tpl)
    tree.insert((0, 0), Tree(value=3))
    tree.freeze()
    initialized_tree = Tree.from_tuple(tpl)
    initialized_tree.append(Tree(value=3))
    initialized_tree.freeze()
    assert hash(tree) == hash(initialized_tree)
Пример #20
0
def test_tree_to_tuple():
    tpl = ("a", [
        (2, [(3, [("a", []), ("b", [])])]),
    ])
    assert Tree.from_tuple(tpl).to_tuple() == tpl
Пример #21
0
def main():
    t = Tree.from_tuple((1, [(5, []), (6, [('a', []), ('b', [])]), (7, [])]))
    g = Tree.from_tuple((1, [(5, []), (6, [('a', []), ('b', [])])]))

    print('1)', t)
    print('2)', g)
    print('3)', t == g)

    g.emplace(7)
    print('4)', g)
    print('5)', t == g)

    print('6)', g == g[()])
    print('7)', g[1])

    g[1, 0] = Tree(18)
    print('8)', g)

    del g[1, 1]
    print('9)', g)

    g.insert((1, 1), Tree('x'))
    print('10)', g)
    print('11)', len(g), g.size(), g.height())
    print('12)', g.value)

    print('13)', [x for x in g])
    print('14)', [
        Tree(value=5, children=()),
        Tree(value=6,
             children=(
                 Tree(value=18, children=()),
                 Tree(value='x', children=()),
             )),
        Tree(value=7, children=())
    ])

    test = (1, [(2, [(5, [(14, []), (15, []), (16, [(23, []), (24, [])])]),
                     (6, []), (7, [])]),
                (3, [(8, []),
                     (9, [(17, []), (18, []), (19, [(25, []), (26, [])])]),
                     (10, [])]),
                (4, [(11, []), (12, []),
                     (13, [(20, []), (21, []), (22, [(27, []), (28, [])])])])])
    tg = Tree.from_tuple(test)
    print()
    print('15)', *[x.value for x in tg.bfs()])
    print('16)', *[x.value for x in tg.bfs(reverse=True)])
    print('17)', *[x.value for x in tg.bfs(mirror=True)])
    print('18)', *[x.value for x in tg.bfs(reverse=True, mirror=True)])
    print()
    print('19)', *[x.value for x in tg.dfs()])
    print('20)', *[x.value for x in tg.dfs(reverse=True)])
    print('21)', *[x.value for x in tg.dfs(mirror=True)])
    print('22)', *[x.value for x in tg.dfs(reverse=True, mirror=True)])
    print()
    print('23)', *[x.value for x in tg.dfs()])
    print('24)', *[x.value for x in tg.dfs(post_order=True, mirror=True)])
    print('25)', *[x.value for x in tg.dfs(mirror=True)])
    print('26)', *[x.value for x in tg.dfs(post_order=True)])
    print()

    ctg = tg.clone()
    print('27)', tg == ctg)

    del ctg[0]
    print('28)', tg == ctg, *[x.value for x in ctg.bfs()])

    ctg[()].value = []
    ctg.freeze()
    ctg2 = ctg.unfreeze()
    print('29)', ctg2 == ctg)

    del ctg2[0]
    print('30)', *[x.value for x in ctg.bfs()])
    print('31)', *[x.value for x in ctg2.bfs()])

    ctg2[()].value.append(13)
    print('32)', *[x.value for x in ctg.bfs()])
    print('33)', *[x.value for x in ctg2.bfs()])
    print('34)', ctg2.to_tuple())

    print('35)', *[(t.value, d, i) for t, d, i in ctg2.bfs_ex(depth=3)])
    print(
        '36)', *[(t.value, d, i)
                 for t, d, i in ctg2.dfs_ex(depth=3, post_order=True)])
Пример #22
0
def test_tree_insert():
    # Check behavior for a single-layer insertion
    tpl = (1, [
        (2, []),
    ])
    tree = Tree(value=1)
    tree.insert(0, Tree(value=2))
    assert tree == Tree.from_tuple(tpl)

    # Check behavior for a deep insertion
    tpl = ("a", [(2, [("a", []), ("b", [])]), (3, [
        ("a", []),
    ])])
    tree = Tree(value="a",
                children=(
                    Tree(value=2, children=()),
                    Tree(value=3, children=()),
                ))
    node_a = Tree(value="a")
    node_b = Tree(value="b")
    tree.insert((0, 1), node_a)
    tree.insert((0, 1), node_b)
    tree.insert((1, 1), node_a)
    assert tree == Tree.from_tuple(tpl)

    # Check for IndexError when invalid index is presented
    with pytest.raises(IndexError):
        tree.insert((1, 2, 3, 4, 5), node_a)

    # Check for TypeError when invalid type of index is presented
    with pytest.raises(TypeError):
        tree.insert("b", node_a)

    # Check insert behavior for a frozen tree
    tree.freeze()
    with pytest.raises(AssertionError):
        tree.insert(0, node_a)