-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
186 lines (161 loc) · 5.26 KB
/
tests.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from intervalt import *
import unittest
class IntervalTests(unittest.TestCase):
def test_create_interval(self):
i = Interval(4,5)
self.assertLess(i.left, i.right)
def test_create_interval_invalid(self):
self.assertRaises(InvalidInterval, lambda: Interval(5,4))
def test_check_overlap(self):
i1 = Interval(3,6)
i2 = Interval(4,5)
self.assertTrue(i1.check_overlap(i2))
def test_check_do_not_overlap(self):
i1 = Interval(3,3.5)
i2 = Interval(4,5)
self.assertTrue(i1.check_overlap(i2))
class NodeTests(unittest.TestCase):
def test_create_node(self):
i = Interval(4,5)
n = Node(i)
self.assertEqual(i, n.interval)
def test_add_get_left(self):
i1 = Interval(4,5)
n1 = Node(i1)
i2 = Interval(3,6)
n2 = Node(i2)
n1.right_node = n2
self.assertEqual(n1.right_node.interval, i2)
self.assertEqual(n1.maxval, i2.right)
self.assertEqual(n2.parent, n1)
def test_add_get_right(self):
i1 = Interval(4,5)
n1 = Node(i1)
i2 = Interval(3,6)
n2 = Node(i2)
n1.right_node = n2
self.assertEqual(n1.right_node.interval, i2)
self.assertEqual(n1.maxval, i2.right)
self.assertEqual(n2.parent, n1)
class IntervalTreeTests(unittest.TestCase):
def test_create_interval_tree(self):
tree = IntervalTree()
i1 = Interval(3,4)
n1 = Node(i1)
tree.root = n1
self.assertEqual(tree.root.interval, i1)
class IntervalBSTreeTests(unittest.TestCase):
def test_create_bs_interval_tree(self):
tree = IntervalBSTree()
i1 = Interval(3,4)
n1 = Node(i1)
tree.root = n1
self.assertEqual(tree.root.interval, i1)
def test_add_interval_node(self):
tree = IntervalBSTree()
i1 = Interval(3,4)
n1 = Node(i1)
i2 = Interval(4,5)
n2 = Node(i2)
tree.root = n1
tree.add(n2)
self.assertEqual(tree.root.right_node, n2)
def test_remove_interval_node_right(self):
tree = IntervalBSTree()
i1 = Interval(3,4)
n1 = Node(i1)
i2 = Interval(4,5)
n2 = Node(i2)
tree.root = n1
tree.add(n2)
self.assertEqual(tree.root.right_node, n2)
tree.remove(n2)
self.assertIsNone(tree.root.right_node)
def test_remove_interval_node(self):
tree = IntervalBSTree()
i1 = Interval(3,4)
n1 = Node(i1)
i2 = Interval(2,5)
n2 = Node(i2)
tree.root = n1
tree.add(n2)
self.assertEqual(tree.root.left_node, n2)
tree.remove(n2)
self.assertIsNone(tree.root.left_node)
def test_remove_interval_node_root(self):
tree = IntervalBSTree()
i1 = Interval(3,4)
n1 = Node(i1)
i2 = Interval(2,5)
n2 = Node(i2)
tree.root = n1
tree.add(n2)
self.assertEqual(tree.root.left_node, n2)
tree.remove(n1)
self.assertIsNone(tree.root.left_node)
def test_remove_interval_two_children(self):
tree = IntervalBSTree()
i1 = Interval(3,4)
n1 = Node(i1)
i2 = Interval(2,5)
n2 = Node(i2)
i3 = Interval(4,7)
n3 = Node(i1)
tree.root = n1
tree.add(n2)
tree.add(n3)
self.assertEqual(tree.root.left_node, n2)
self.assertEqual(tree.root.right_node, n3)
tree.remove(n1)
self.assertIsNone(tree.root.right_node)
def test_overlapping(self):
tree = IntervalBSTree()
i1 = Interval(3,4)
n1 = Node(i1)
i2 = Interval(2,5)
tree.root = n1
node = tree.check_overlap(i2)
self.assertEqual(node, i1)
class IntervalAVLTreeTests(unittest.TestCase):
NotImplemented
class IntervalRBTreeTests(unittest.TestCase):
NotImplemented
if bintrees_enabled:
class BintreesAdaptersTest(unittest.TestCase):
def test_bintrees_installed(self):
# How do you test this? TODO
pass
def test_bintrees_not_installed(self):
pass
def test_bstree(self):
# Not sure how this adapter should work
i1 = Interval(4,5)
n1 = Node(i1)
tree = IntervalBSTreeBintreesAdapter(n1)
i2 = Interval(3,6)
n2 = Node(i2)
tree.add(n2)
node = tree.check_overlap(Interval(4,7))
self.assertIn(node, [n1, n2])
def test_rbtree(self):
# Not sure how this adapter should work
i1 = Interval(4,5)
n1 = Node(i1)
tree = IntervalRBTreeBintreesAdapter(n1)
i2 = Interval(3,6)
n2 = Node(i2)
tree.add(n2)
node = tree.check_overlap(Interval(4,7))
self.assertIn(node, [n1, n2])
def test_avltree(self):
# Not sure how this adapter should work
i1 = Interval(4,5)
n1 = Node(i1)
tree = IntervalAVLTreeBintreesAdapter(n1)
i2 = Interval(3,6)
n2 = Node(i2)
tree.add(n2)
node = tree.check_overlap(Interval(4,7))
self.assertIn(node, [n1, n2])
if __name__ == "__main__":
unittest.main()