def test_copy(self): """ Try copying a tree and check the structure is preserved. """ tree = MutableTree( Node("root", Node("leaf1"), Node("A", Node("leaf2"), Node("C", Node("leaf5"), Node("leaf6") ), ), Node("B", Node("leaf4") ) )) # Copy it new_tree = tree.copy() # Check none of the nodes has been preserved old_ids = [id(n) for n in tree.postorder()] for new_node in new_tree.postorder(): self.assertNotIn(id(new_node), old_ids) # Check the two trees evaluate as equal self.assertEqual(tree, new_tree)
def test_postorder(self): """ Check postorder returns the right list of nodes. """ leaf1 = Node("leaf1") leaf2 = Node("leaf2") leaf4 = Node("leaf4") leaf5 = Node("leaf5") leaf6 = Node("leaf6") c_node = Node("C", leaf5, leaf6) b_node = Node("B", leaf4) a_node = Node("A", leaf2, c_node) root = Node("root", leaf1, a_node, b_node) tree = MutableTree(root) # Define the correct postorder manually correct_postorder = [ leaf1, leaf2, leaf5, leaf6, c_node, a_node, leaf4, b_node, root ] postorder = tree.postorder() # Check that these match self.assertEqual([id(n) for n in correct_postorder], \ [id(n) for n in postorder])
def test_immutable(self): """ Try converting a mutable tree to an immutable. """ tree = MutableTree( Node( "root", Node("leaf1"), Node("A", Node("leaf2"), Node("leaf3")), Node("B", Node("leaf4"), Node("C", Node("leaf5"), Node("leaf6"))))) im = tree.immutable()
def test_create_tree(self): """ Just make sure building a big tree causes no errors. """ tree = MutableTree( Node( "root", Node("leaf1"), Node("A", Node("leaf2"), Node("leaf3")), Node("B", Node("leaf4"), Node("C", Node("leaf5"), Node("leaf6")))))
def test_copy(self): """ Try copying a tree and check the structure is preserved. """ tree = MutableTree( Node( "root", Node("leaf1"), Node( "A", Node("leaf2"), Node("C", Node("leaf5"), Node("leaf6")), ), Node("B", Node("leaf4")))) # Copy it new_tree = tree.copy() # Check none of the nodes has been preserved old_ids = [id(n) for n in tree.postorder()] for new_node in new_tree.postorder(): self.assertNotIn(id(new_node), old_ids) # Check the two trees evaluate as equal self.assertEqual(tree, new_tree)
def test_immutable(self): """ Try converting a mutable tree to an immutable. """ tree = MutableTree( Node("root", Node("leaf1"), Node("A", Node("leaf2"), Node("leaf3") ), Node("B", Node("leaf4"), Node("C", Node("leaf5"), Node("leaf6") ) ) )) im = tree.immutable()
def test_string(self): """ Tries to get the string representation of a tree. It's not critical that the representation is correct, so we don't worry about that. """ tree = MutableTree( Node( "root", Node("leaf1"), Node("A", Node("leaf2"), Node("leaf3")), Node("B", Node("leaf4"), Node("C", Node("leaf5"), Node("leaf6"))))) str(tree)
def test_postorder(self): """ Check postorder returns the right list of nodes. """ leaf1 = Node("leaf1") leaf2 = Node("leaf2") leaf4 = Node("leaf4") leaf5 = Node("leaf5") leaf6 = Node("leaf6") c_node = Node("C", leaf5, leaf6) b_node = Node("B", leaf4) a_node = Node("A", leaf2, c_node) root = Node("root", leaf1, a_node, b_node) tree = MutableTree(root) # Define the correct postorder manually correct_postorder = [leaf1, leaf2, leaf5, leaf6, c_node, a_node, leaf4, b_node, root] postorder = tree.postorder() # Check that these match self.assertEqual([id(n) for n in correct_postorder], \ [id(n) for n in postorder])
def test_create(self): tree = MutableTree(Node("root")) self.assertEqual(tree.root.label, "root")