Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
210CT-Coursework/BST_unit_test.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
118 lines (98 sloc)
3.9 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import BST | |
import unittest | |
class Tests(unittest.TestCase): | |
def test_insert_into_empty(self): | |
# Inserting into empty tree, should return a tree | |
self.assertNotEqual(BST.tree_insert(None,"A"), None) | |
def test_insert_duplicates(self): | |
tree = BST.tree_insert(None,"A") | |
BST.tree_insert(tree, "A") | |
node = BST.tree_find(tree, "A") | |
self.assertEqual(node.frequency, 2) | |
def test_goes_to_left(self): | |
tree = BST.tree_insert(None, "B") | |
BST.tree_insert(tree, "A") | |
node = BST.tree_find(tree,"A") | |
self.assertEqual(tree.left, node) | |
def test_goes_to_right(self): | |
tree = BST.tree_insert(None, "A") | |
BST.tree_insert(tree, "B") | |
node = BST.tree_find(tree,"B") | |
self.assertEqual(tree.right, node) | |
def test_delete_root(self): | |
tree = BST.tree_insert(None, "A") | |
empty = BST.tree_delete(tree, "A") | |
self.assertEqual(empty, None) | |
def test_delete_one_child_right_and_is_root(self): | |
tree = BST.tree_insert(None, "A") | |
BST.tree_insert(tree, "B") | |
tree = BST.tree_delete(tree, "A") | |
pre_order1 = BST.pre_order(tree) | |
equalTree = BST.tree_insert(None, "B") | |
pre_order2 = BST.pre_order(equalTree) | |
self.assertEqual(pre_order1,pre_order2) | |
def test_delete_one_child_left_and_is_root(self): | |
tree = BST.tree_insert(None, "B") | |
BST.tree_insert(tree, "A") | |
tree = BST.tree_delete(tree, "B") | |
pre_order1 = BST.pre_order(tree) | |
equalTree = BST.tree_insert(None, "A") | |
pre_order2 = BST.pre_order(equalTree) | |
self.assertEqual(pre_order1,pre_order2) | |
def test_delete_two_children_and_is_root(self): | |
tree = BST.tree_insert(None, "B") | |
BST.tree_insert(tree, "A") | |
BST.tree_insert(tree, "C") | |
tree = BST.tree_delete(tree, "B") | |
pre_order1 = BST.pre_order(tree) | |
equalTree = BST.tree_insert(None, "A") | |
BST.tree_insert(equalTree, "C") | |
pre_order2 = BST.pre_order(equalTree) | |
self.assertEqual(pre_order1,pre_order2) | |
def test_delete_one_child_right(self): | |
tree = BST.tree_insert(None, "A") | |
BST.tree_insert(tree, "B") | |
BST.tree_insert(tree, "C") | |
tree = BST.tree_delete(tree, "B") | |
pre_order1 = BST.pre_order(tree) | |
equalTree = BST.tree_insert(None, "A") | |
BST.tree_insert(equalTree,"C") | |
pre_order2 = BST.pre_order(equalTree) | |
self.assertEqual(pre_order1,pre_order2) | |
def test_delete_one_child_left(self): | |
tree = BST.tree_insert(None, "A") | |
BST.tree_insert(tree, "C") | |
BST.tree_insert(tree, "B") | |
tree = BST.tree_delete(tree, "C") | |
pre_order1 = BST.pre_order(tree) | |
equalTree = BST.tree_insert(None, "A") | |
BST.tree_insert(equalTree,"B") | |
pre_order2 = BST.pre_order(equalTree) | |
self.assertEqual(pre_order1,pre_order2) | |
def test_delete_two_children(self): | |
tree = BST.tree_insert(None, "A") | |
BST.tree_insert(tree, "C") | |
BST.tree_insert(tree, "B") | |
BST.tree_insert(tree, "D") | |
tree = BST.tree_delete(tree, "C") | |
pre_order1 = BST.pre_order(tree) | |
equalTree = BST.tree_insert(None, "A") | |
BST.tree_insert(equalTree,"B") | |
BST.tree_insert(equalTree,"D") | |
pre_order2 = BST.pre_order(equalTree) | |
self.assertEqual(pre_order1,pre_order2) | |
def test_find_when_not_in_tree(self): | |
tree = BST.tree_insert(None, "A") | |
node = BST.tree_find(tree, "B") | |
self.assertEqual(node, None) | |
def test_find(self): | |
tree = BST.tree_insert(None, "A") | |
BST.tree_insert(tree, "C") | |
BST.tree_insert(tree, "B") | |
BST.tree_insert(tree, "D") | |
BST.tree_insert(tree, "B") | |
node = BST.tree_find(tree, "B") | |
self.assertEqual(node.value, "B") | |
self.assertEqual(node.frequency, 2) | |
if __name__ == "__main__": | |
unittest.main() |