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)
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)
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)