-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeTests.py
133 lines (107 loc) · 4.24 KB
/
nodeTests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import unittest
from node import Node, parser
testTrees = [
[5, [3, [2], [5]], [7, [1], [0, [2], [8, [5]]]]],
[5, [3, [2, [1, [0]]]]],
[52, [20, [10]], [15]],
[5]
]
averageTestCases = [
3.8,
2.2,
24.25,
5
]
medianTestCases = [
4,
2,
17.5,
5
]
sumTestCases = [
38,
11,
97,
5
]
strTestCases = [
"5\n \t3\n \t\t2\n \t\t5\n \t7\n \t\t1\n \t\t0\n \t\t\t2\n \t\t\t8\n \t\t\t\t5\n".replace(" ",
""),
"5\n \t3\n \t\t2\n \t\t\t1\n \t\t\t\t0\n".replace(" ", ""),
"52\n \t20\n \t\t10\n \t15\n".replace(" ", ""),
"5\n"
]
class TestNode(unittest.TestCase):
def setUp(self):
self.root = Node(1)
def test_node(self):
self.assertEqual(self.root.value, 1)
self.assertEqual(len(self.root.children), 0)
def test_node_wrong_type_error(self):
with self.assertRaises(TypeError):
self.root=Node("a")
def test_add_child_as_node(self):
self.root.add_child(Node(2))
self.root.add_child(Node(3))
self.assertEqual(self.root.value, 1)
self.assertEqual(len(self.root.children), 2)
self.assertEqual(self.root.children[0].value, 2)
self.assertEqual(self.root.children[1].value, 3)
self.assertEqual(len(self.root.children[0].children), 0)
self.assertEqual(len(self.root.children[1].children), 0)
def test_add_child_as_int(self):
self.root.add_child(2)
self.assertEqual(len(self.root.children), 1)
self.assertEqual(self.root.children[0].value, 2)
self.assertEqual(len(self.root.children[0].children), 0)
def test_add_child_wrong_type_error(self):
with self.assertRaises(TypeError):
self.root.add_child("a")
def test_add_too_many_children(self):
self.root
for i in range(self.root.maxChildren):
self.root.add_child(Node(i))
with self.assertRaises(ValueError):
self.root.add_child(Node(0))
def test_parser(self):
testTree = [5, [3, [2], [5]], [7, [1], [0, [2], [8, [5]]]]]
self.root = parser(testTree)
self.assertEqual(self.root.value, 5)
self.assertEqual(self.root.children[0].value, 3)
self.assertEqual(self.root.children[0].children[0].value, 2)
self.assertEqual(self.root.children[0].children[1].value, 5)
self.assertEqual(self.root.children[1].value, 7)
self.assertEqual(self.root.children[1].children[0].value, 1)
self.assertEqual(self.root.children[1].children[1].value, 0)
self.assertEqual(self.root.children[1].children[1].children[0].value, 2)
self.assertEqual(self.root.children[1].children[1].children[1].value, 8)
self.assertEqual(self.root.children[1].children[1].children[1].children[0].value, 5)
def test_parser_singe_node(self):
testTree = [1]
self.assertEqual(self.root.value, 1)
self.assertEqual(len(self.root.children), 0)
def test_str(self):
for i in range(len(testTrees)):
self.root = parser(testTrees[i])
self.assertEqual(str(self.root), strTestCases[i])
def test_subtree_average(self):
for i in range(len(testTrees)):
self.root = parser(testTrees[i])
self.assertEqual(self.root.subtree_average(), averageTestCases[i])
def test_subtree_median(self):
for i in range(len(testTrees)):
self.root = parser(testTrees[i])
self.assertEqual(self.root.subtree_median(), medianTestCases[i])
def test_subtree_sum(self):
for i in range(len(testTrees)):
self.root = parser(testTrees[i])
self.assertEqual(self.root.subtree_sum(), sumTestCases[i])
def test_child_computation(self):
self.root=parser([52, [20, [10]], [15]])
child=self.root.children[0]
self.assertEqual(child.subtree_average(), 15)
self.assertEqual(child.subtree_median(), 15)
self.assertEqual(child.subtree_sum(), 30)
self.assertEqual(str(child), "20\n\t10\n")
if __name__ == "__main__":
unittest.main()