def test_huffman_tree_app_sentence(self): left = Tree('a', 1) right = Tree('p', 2) tree = Tree('', 3, left, right) expected = PriorityQueue([tree]) self.assertEqual(HuffmanTree("app").huffman_tree, expected)
def test_with_all_values(self): left = Tree('a', 3) right = Tree('b', 2) tree = Tree('', 5, left=left, right=right) self.assertEqual(tree.frequency, 5) self.assertEqual(tree.left.letter, 'a') self.assertEqual(tree.right.letter, 'b')
def deserialise(cls, data, baseObj): """ setting redundant values on proxy is fine as _extractMask() will remove them anyway """ proxy = cls(baseObj) for index, branchData in data["added"]: branch = Tree.fromDict(branchData) proxy.addChild(branch, index=index) proxy.value = data["value"]
def test_add_and_search(size, step): tree = Tree() for element in range(0, size, step): tree.add(element) for element in range(0, size, step): assert tree.search(element) for element in range(10000, 10010): assert not tree.search(element)
proxy.value = data["value"] """ # toughest scenario: baseObj proxy = Delta(baseObj) proxy["key"] = valueA baseObj["key"] = valueB proxy["key"] = ...? easiest way is to say any "set" command overrides the base """ # test for interfaces with the tree structure testTree = Tree("TestRoot") testTree("asdf").value = "firstKey" testTree("parent").value = "nonas" testTree("parent.childA").value = 930 testTree("parent.childA").extras["options"] = (930, "eyyy") #testTree("parent.childB").value = True # cannot do nested list inputs yet #testTree["parent.listEntry"] = "[eyyy, test, 4.3, '', 2e-10, False]" #testTree["parent.nestedList"] = "[eyyy, test, 4.3, '', [4e4, i, 33, True], 2e-10]" if __name__ == '__main__': pass
def test_with_default_values(self): tree = Tree('a', 3) self.assertEqual(tree.letter, 'a') self.assertEqual(tree.frequency, 3) self.assertEqual(tree.left, None) self.assertEqual(tree.right, None)
def test_add_and_order(size, step): tree = Tree() for element in range(0, size, step): tree.add(element) assert tree.inorder() == list(range(0, size, step))
def test_create_tree_list_5_elements_out_of_order(self): expected = [Tree('b', 9), Tree('c', 12), Tree('d', 13), Tree('e', 16), Tree('f', 45), Tree('a', 5)] actual = HuffmanTree().create_tree_list({'b': 9, 'c': 12, 'd': 13, 'e': 16, 'f': 45, 'a': 5}) self.assertEqual(actual, expected)
def test_huffman_tree_1_letter_sentence(self): expected = PriorityQueue([Tree('a', 1)]) self.assertEqual(HuffmanTree("a").huffman_tree, expected)
def tree_list(self, values): return list(map(lambda v: Tree('', v), values))
def test_create_tree_list_2_elements(self): expected = [Tree('a', 1), Tree('b', 2)] actual = HuffmanTree().create_tree_list({'a': 1, 'b': 2}) self.assertEqual(actual, expected)
def tree(self, value): return Tree('', value)
def test_median(self): self.assertAlmostEqual(Tree.median(root), 4.0) self.assertAlmostEqual(Tree.median(root.right), 3.5) self.assertAlmostEqual(Tree.median(root.left), 3.0) self.assertAlmostEqual(Tree.median(root.left.right), 5.0)
def test_mean(self): self.assertAlmostEqual(Tree.mean(root), 3.8) self.assertAlmostEqual(Tree.mean(root.left), 10 / 3) self.assertAlmostEqual(Tree.mean(root.right.left), 1) self.assertAlmostEqual(Tree.mean(root.right.right), 3.75)
def test_sum(self): self.assertAlmostEqual(Tree.sum(root), 38) self.assertAlmostEqual(Tree.sum(root.left), 10) self.assertAlmostEqual(Tree.sum(root.right), 23) self.assertAlmostEqual(Tree.sum(root.left.left), 2)
import unittest from main import Tree root = Tree(5) root.insert(3, right=False) root.left.insert(2, right=False) root.left.insert(5) root.insert(7) root.right.insert(0) root.right.insert(1, right=False) root.right.right.insert(2, right=False) root.right.right.insert(8) root.right.right.right.insert(5) print('Sum of the tree elements = ', root.sum()) print('Mean of the tree elements = ', root.mean()) print('Median of the tree elements = ', root.median()) class TestTree(unittest.TestCase): def test_sum(self): self.assertAlmostEqual(Tree.sum(root), 38) self.assertAlmostEqual(Tree.sum(root.left), 10) self.assertAlmostEqual(Tree.sum(root.right), 23) self.assertAlmostEqual(Tree.sum(root.left.left), 2) def test_mean(self): self.assertAlmostEqual(Tree.mean(root), 3.8) self.assertAlmostEqual(Tree.mean(root.left), 10 / 3) self.assertAlmostEqual(Tree.mean(root.right.left), 1) self.assertAlmostEqual(Tree.mean(root.right.right), 3.75)