def test_init_bstree(): tree = BSTree(4) assert tree.key == 4 assert tree.parent is None assert tree.left is None assert tree.right is None assert tree.size() == 1
class TestingMyMethods(unittest.TestCase): """Now let's test the methods""" def setUp(self): self.my_bst = BSTree() self.filled_tree = BSTree() self.filled_tree.insert(5) self.filled_tree.insert(3) self.filled_tree.insert(6) self.filled_tree.insert(8) self.filled_tree.insert(11) self.filled_tree.insert(2) def test_the_contains_method_on_empty_tree(self): self.assertEqual(self.my_bst.contains(5), False) def test_contains_on_tree_that_doesnt_have_the_val(self): self.assertEqual(self.filled_tree.contains(1), False) self.assertEqual(self.filled_tree.contains(9), False) def test_contains_for_values_it_has(self): self.assertEqual(self.filled_tree.contains(8), True) self.assertEqual(self.filled_tree.contains(2), True) self.assertEqual(self.filled_tree.contains(5), True) self.assertEqual(self.filled_tree.contains(11), True) self.assertEqual(self.filled_tree.contains(6), True) self.assertEqual(self.filled_tree.contains(3), True) def test_size(self): self.assertEqual(self.my_bst.size(), 0) self.my_bst.insert(1) self.assertEqual(self.my_bst.size(), 1) self.assertEqual(self.filled_tree.size(), 6) self.filled_tree.insert(15) self.assertEqual(self.filled_tree.size(), 7) def test_the_depth_method(self): self.assertEqual(self.my_bst.depth(), 0) self.assertEqual(self.filled_tree.depth(), 4) self.filled_tree.insert(5422) self.assertEqual(self.filled_tree.depth(), 5) def test_balance(self): self.assertEqual(self.my_bst.balance(), 0) self.assertEqual(self.filled_tree.balance(), 1) self.filled_tree.insert(5422) self.assertEqual(self.filled_tree.balance(), 2)
def test_size(): tree = BSTree(4) assert tree.size() == 1 tree.insert(2) assert tree.size() == 2 tree.insert(6) assert tree.size() == 3 tree.insert(1) assert tree.size() == 4 tree.insert(3) assert tree.size() == 5 tree.insert(5) assert tree.size() == 6 tree.insert(7) assert tree.size() == 7
def test_insert_already_present(): tree = BSTree(4) tree.insert(4) assert tree.size() == 1