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