def test_leaf_node(self): """ Test sum_internal on a leaf node. """ self.assertEqual( sum_internal(Tree(1)), 0, "Leaf nodes should " + "have 0 internal nodes to sum.")
def test_returns_int(self): """ Test sum_internal to make sure it returns an int. """ return_type = type(sum_internal(Tree(0, [Tree(1, [Tree(2)])]))) self.assertEqual( return_type, int, "sum_internal should return type" + "int, but instead returned type {}.".format(return_type))
def test_multiple_non_leaf_children(self): """ Test sum_internal 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_internal(t), 6, "All non-leaf nodes should be " + "summed together.")
def test_one_leaf_child(self): """ Test sum_internal on a tree containing only a root and its leaf child. """ t = Tree(1, [Tree(2)]) self.assertEqual( sum_internal(t), 1, "Leaf nodes should not be " + "included in the sum, while root nodes (with " + "children) should.")
def test_multiple_leaf_children(self): """ Test sum_internal on a tree containing only a root and its children which are all leaves. """ t = Tree(1, [Tree(2), Tree(3)]) self.assertEqual( sum_internal(t), 1, "Leaf nodes should not be " + "included in the sum, while root nodes (with " + "children) should.")
def test_sum_internal(self, value, children): """ Test the correctness of sum_internal against the solution. """ (children_list, internal_sum) = make_children_list(children) t = Tree(value, children_list) if children_list: internal_sum += value actual = sum_internal(t) self.assertEqual(actual, internal_sum, ("sum_internal on the Tree\n{}\nReturned {}" + " instead of {}.").format(t.__repr__(), actual, internal_sum))