Пример #1
0
 def test_is_empty(self):
     """Does is_empty() return True if tree is empty, False if not?"""
     self.assertFalse(self.tree.is_empty())
     # Try it on a tree that was nonempty, then was emptied out:
     self.tree._delete(self.first_child)
     self.tree._delete(self.root)
     self.assertTrue(self.tree.is_empty())
     # Try it on a new blank tree:
     new_tree = GeneralTree()
     self.assertTrue(new_tree.is_empty())
Пример #2
0
class TestPublicAccessors(unittest.TestCase):
    """Test that the main public accessor methods work for a simple test
    tree with 3 layers.
    """
    def setUp(self):
        self.tree = GeneralTree()
        self.root = self.tree._add_root("Root element")
        self.first_child = self.tree._add_child(self.root,
                                                "First child element")
        self.large_tree, self.positions = twelve_element_test_tree()

    def test_height(self):
        """Does the height method correctly return the height of various nodes
        in the large 12-element test tree?"""
        # height of root should be 3 (3 levels below it)
        self.assertEqual(self.large_tree.height(), 3)

        # height of position 2 should be 1:
        self.assertEqual(self.large_tree.height(self.positions[2]), 1)
        # height of position 3 should be 2:
        self.assertEqual(self.large_tree.height(self.positions[3]), 2)
        # height of a leaf should be 0:
        self.assertEqual(self.large_tree.height(self.positions[10]), 0)

    def test_depth(self):
        """Does the depth() method return the correct height for various nodes in
        the 12-element test tree?"""
        # depth of root should be 0
        self.assertEqual(self.large_tree.depth(self.large_tree.root()), 0)
        # depth of position 2 (first layer child) should be 1
        self.assertEqual(self.large_tree.depth(self.positions[2]), 1)
        # depth of position 8 (second layer child) should be 2
        self.assertEqual(self.large_tree.depth(self.positions[8]), 2)
        # depth of 12 (third layer child) should be 3
        self.assertEqual(self.large_tree.depth(self.positions[12]), 3)

    def test_root(self):
        """Does root() return a Position referencing the same node as
        the tree's root?"""
        self.assertEqual(self.tree.root(),
                         self.tree._make_position(self.tree._root))

    def test_is_root(self):
        """Does is_root() correctly return True when called on a Position
        that is root, and false when called on a Position that is not root?"""
        self.assertTrue(self.tree.is_root(self.root))
        self.assertFalse(self.tree.is_root(self.first_child))

    def test_parent(self):
        """Does parent() correctly return the Position object corresponding to
        the parent node of arg position when position is not root, and None
        when called on a Position that is root?"""
        self.assertEqual(self.tree.parent(self.first_child), self.root)
        self.assertIsNone(self.tree.parent(self.root))

    def test_is_leaf(self):
        """Does is_leaf() correctly return True when called on a position that
        has no children, and False when called on a position that has at least
        one child?"""
        self.assertTrue(self.tree.is_leaf(self.first_child))
        self.assertFalse(self.tree.is_leaf(self.root))

    def test_len(self):
        """Obvious-case tests for len(tree) where the method is being called
        in isolation, not used in combination with anything else."""
        # len() should be 2 with the 2 elements added to self.tree by setUp
        self.assertEqual(len(self.tree), 2)
        # len of a new tree should be 0
        new_tree = GeneralTree()
        self.assertEqual(len(new_tree), 0)
        # Add root and 5 children of root, len should be 6
        new_tree._add_root("NT root element")
        for i in range(5):
            new_tree._add_child(new_tree.root(), i)
        self.assertEqual(len(new_tree), 6)
        # Add 10 children to each of those children, len should be 56
        for child in new_tree.root(
        )._node._children:  # todo (the GeneralTree.children() iterator isn't implemented yet)
            child = new_tree._make_position(child)
            for i in range(10):
                latest_child = new_tree._add_child(child, i)  # For expediency
                # in using one leaf
                # below
        self.assertEqual(len(new_tree), 56)
        # Confirm the _delete method decrements length
        prior_length = len(new_tree)
        new_tree._delete(latest_child)
        self.assertEqual(len(new_tree), prior_length - 1)

    def test_is_empty(self):
        """Does is_empty() return True if tree is empty, False if not?"""
        self.assertFalse(self.tree.is_empty())
        # Try it on a tree that was nonempty, then was emptied out:
        self.tree._delete(self.first_child)
        self.tree._delete(self.root)
        self.assertTrue(self.tree.is_empty())
        # Try it on a new blank tree:
        new_tree = GeneralTree()
        self.assertTrue(new_tree.is_empty())

    def test_parenthesize(self):
        expected_string = '1 (2 (5), 3 (6 (12), 7, 8, 9), 4 (10, 11))'
        self.assertEqual(self.large_tree.parenthesize(self.large_tree.root()),
                         expected_string)