def test_one_left_child(self): """ Test sum_leaves on a BinaryTree with one left child. """ t = BinaryTree(1, BinaryTree(5)) self.assertNotEqual( sum_leaves(t), 1, "sum_leaves should not " + "count None or any BinaryTrees with children as" + " leaf nodes.") self.assertNotEqual( sum_leaves(t), 6, "sum_leaves should not " + "count None or any BinaryTrees with children as" + " leaf nodes.") self.assertEqual( sum_leaves(t), 5, "sum_leaves should return the " + "value of the leaf child when used on a BinaryTree " + "with a single leaf child.")
def test_one_right_child(self): """ Test sum_leaves on a BTNode with one right child """ t = BTNode(1, None, BTNode(5)) self.assertNotEqual( sum_leaves(t), 1, "sum_leaves should not " + "count None or any BTNodes with children as" + " leaf nodes.") self.assertNotEqual( sum_leaves(t), 6, "sum_leaves should not " + "count None or any BTNodes with children as" + " leaf nodes.") self.assertEqual( sum_leaves(t), 5, "sum_leaves should return the " + "data of the leaf child when used on a BTNode " + "with a single leaf child.")
def test_leaf(self): """ Test sum_leaves on a leaf. """ self.assertEqual( sum_leaves(BinaryTree(2)), 2, "sum_leaves should" + " return the leaf's value when used on a leaf.")
def test_leaf(self): """ Test sum_leaves on a leaf. """ self.assertEqual( sum_leaves(BTNode(2)), 2, "sum_leaves should" + " return the leaf's data when used on a leaf.")
def test_two_leaf_children(self): """ Test sum_leaves on a BinaryTree with two leaf children. """ t = BinaryTree(5, BinaryTree(4), BinaryTree(3)) self.assertEqual( sum_leaves(t), 7, "sum_leaves should sum all " + "of the leaves in the entire BinaryTree.")
def test_returns_int(self): """ Test sum_leaves to make sure it returns an int. """ return_type = type(sum_leaves(BinaryTree(1))) self.assertEqual( return_type, int, "sum_leaves should return type " + "int, but instead returned type {}.".format(return_type))
def test_sum_leaves(self, tuple_tree): """ Test sum_leaves on a randomly generated BinaryTree. """ (t, expected) = tuples_to_tree(tuple_tree) actual = sum_leaves(t) self.assertEqual(actual, expected, ("test_sum_leaves on BinaryTree\n{}\nReturned {}" + " instead of {}.").format(tuple_tree, actual, expected))
def test_none(self): """ Test sum_leaves on None. """ self.assertEqual(sum_leaves(None), 0, "sum_leaves on None should " + "return 0.")