コード例 #1
0
 def test_returns_int(self):
     """
     Test sum_leaves to make sure it returns an int.
     """
     return_type = type(sum_leaves(Tree(0, [Tree(1, [Tree(2)])])))
     self.assertEqual(
         return_type, int, "sum_leaves should return type" +
         "int, but instead returned type {}.".format(return_type))
コード例 #2
0
    def test_multiple_non_leaf_children(self):
        """
        Test sum_leaves on a tree containing only a root and its children
        which are all subtrees with children.
        """
        t = Tree(1, [Tree(2, [Tree(4), Tree(6)]), Tree(3, [Tree(5), Tree(7)])])

        self.assertEqual(sum_leaves(t), 22,
                         "All leaf nodes should be " + "summed together.")
コード例 #3
0
    def test_one_leaf_child(self):
        """
        Test sum_leaves on a tree containing only a root and its leaf child.
        """
        t = Tree(1, [Tree(2)])

        self.assertEqual(
            sum_leaves(t), 2, "Internal nodes should not be " +
            "included in the sum, while leaf nodes (with no " +
            "children) should.")
コード例 #4
0
    def test_multiple_leaf_children(self):
        """
        Test sum_leaves on a tree containing only a root and its children
        which are all leaves.
        """
        t = Tree(1, [Tree(2), Tree(3)])

        self.assertEqual(
            sum_leaves(t), 5, "Internal nodes should not be " +
            "included in the sum, while leaf nodes (with no " +
            "children) should.")
コード例 #5
0
    def test_sum_leaves(self, value, children):
        """
        Test the correctness of sum_leaves against the solution.
        """
        (children_list, leaves_sum) = make_children_list(children)
        t = Tree(value, children_list)

        if not children_list:
            leaves_sum += value

        actual = sum_leaves(t)
        self.assertEqual(actual, leaves_sum,
                         ("sum_leaves on the Tree\n{}\nReturned {}" +
                          " instead of {}.").format(t.__repr__(), actual,
                                                    leaves_sum))
コード例 #6
0
 def test_leaf_node(self):
     """
     Test sum_leaves on a leaf node.
     """
     self.assertEqual(sum_leaves(Tree(1)), 1,
                      "Leaf nodes should " + "return their value.")