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_two_leaf_children(self): """ Test sum_leaves on a BTNode with two leaf children. """ t = BTNode(5, BTNode(4), BTNode(3)) self.assertEqual(sum_leaves(t), 7, "sum_leaves should sum all " + "of the leaves in the entire BTNode.")
def test_returns_int(self): """ Test sum_leaves to make sure it returns an int. """ return_type = type(sum_leaves(BTNode(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 BTNode. """ (t, expected) = tuples_to_tree(tuple_tree) actual = sum_leaves(t) self.assertEqual(actual, expected, ("test_sum_leaves on BTNode\n{}\nReturned {}" + " instead of {}.").format(tuple_tree, actual, expected))
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_none(self): """ Test sum_leaves on None. """ self.assertEqual(sum_leaves(None), 0, "sum_leaves on None should " + "return 0.")