/
splay_unit.py
249 lines (197 loc) · 7 KB
/
splay_unit.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import unittest
import pdb
from random import randint
from splay_tree import SplayTree
class TestSplayBasic(unittest.TestCase):
def setUp(self):
self.s = SplayTree()
#self.mockSplay()
def mockSplay(self):
self.s.splay = lambda a : None
def testConstructor(self):
"""Test if SplayTree can be constructed without raising an exception."""
SplayTree()
self.assertIsNotNone(self.s)
self.assertIsInstance(self.s, SplayTree)
def testInsert(self):
"""Test if SplayTree can be inserted into without raising an exception."""
for i in xrange(randint(50,150)):
self.s.insert(i, None)
def testInsertLength(self):
"""Test if insertions into a splay tree result in the correct length."""
num = randint(60,180)
for i in xrange(num):
self.s.insert(i, None)
self.assertEqual(len(self.s), num)
#try to insert duplicates
for i in xrange(num):
self.s.insert(i, None)
self.assertEqual(len(self.s), num)
def testRemove(self):
"""Test if a splay tree can be inserted into and removed without error."""
numIns = randint(70,200)
for i in xrange(numIns):
self.s.insert(i, None)
for i in xrange(numIns):
self.s.remove(i)
def testRemoveLengthSimple(self):
"""Test if a simple, insertion and removal result in the correct length."""
a = randint(-2147483648,2147483647)
self.s.insert(a, True)
self.assertEqual(len(self.s), 1)
self.s.remove(a)
self.assertEqual(len(self.s), 0)
def testRemoveLength(self):
"""Test if insertions into a splay tree result in the correct length.
Use a python set to deal with duplicate insertions.
"""
numIns = randint(60, 180)
numRem = (numIns/2) + randint(0, numIns/2)
ref = set()
refStatic = set()
for i in xrange(numIns):
a = randint(-2147483648,2147483647)
ref.add(a)
refStatic.add(a)
self.s.insert(a,True)
self.assertEqual(len(self.s), len(ref))
refLength = len(self.s)
#pdb.set_trace()
for i in xrange(numRem):
self.assertEqual(len(self.s), refLength)
refLength -=1
self.s.remove(ref.pop())
self.assertNotEqual(len(self.s), numIns)
self.assertEqual(len(self.s), len(ref))
for i in xrange(numRem): #try to re-remove items
self.s.remove(refStatic.pop())
def testFind(self):
"""Test if items inserted into the splay tree can be found again."""
N = randint(20,150)
s = SplayTree()
for i in xrange(N):
self.s.insert(i,1)
for i in xrange(N):
a = self.s.find(i)
self.assertTrue(a)
N-=a
self.assertEqual(N, 0)
def testContains(self):
"""Test if the number of items inserted into the tree can be re-found."""
N = randint(20,100)
for i in xrange(N):
self.s.insert(i,True)
N-=(i in self.s)
self.assertEqual(N,0)
class TestSplayDeep(TestSplayBasic):
"""Test some of the internal features of the splay tree.
This requires internal access to the members of SplayTree.
"""
def testMin(self):
"""Test the correctness of SplayTree.minNode."""
n = randint(50, 170)
l = []
for i in xrange(n):
a = randint(-2147483648,2147483647)
self.s.insert(a, a)
l.append(a)
self.assertIsNotNone(self.s.minNode(self.s._root))
self.assertEqual(min(l), self.s.minNode(self.s._root).value)
def testMax(self):
"""Test the correctness of SplayTree.maxNode."""
n = randint(50, 170)
l = []
for i in xrange(n):
a = randint(-2147483648,2147483647)
self.s.insert(a, a)
l.append(a)
self.assertIsNotNone(self.s.maxNode(self.s._root))
self.assertEqual(max(l), self.s.maxNode(self.s._root).value)
def testBinaryHelper(self):
"""Test the correctness of SplayTree.binaryHelper."""
self.mockSplay() #disable splaying
#test base-case empty
self.assertIsNone(self.s.binaryHelper(None, None))
self.assertIsNone(self.s.binaryHelper(None, self.s._root))
#test base-case
self.s.insert(0, None)
self.assertIsNotNone(self.s.binaryHelper(0, self.s._root))
#note: binary helper does not splay, but insert does
n = randint(2,2147483647)
self.s.insert(n, None)
self.s.insert(-n, None)
#find a node that is in the tree
self.assertIsNotNone(self.s.binaryHelper(n, self.s._root))
self.assertIsNotNone(self.s.binaryHelper(-n, self.s._root))
#find the exact node
self.assertEqual(self.s.binaryHelper(n, self.s._root), self.s._root.right)
self.assertEqual(self.s.binaryHelper(-n, self.s._root), self.s._root.left)
#find the parent of a non-existant node
#pdb.set_trace()
self.assertEqual(self.s.binaryHelper(-1, self.s._root), self.s._root.left)
self.assertEqual(self.s.binaryHelper(n-1, self.s._root), \
self.s._root.right)
#recursively find the parent of a non-existant node
self.assertEqual(self.s.binaryHelper(n+1, self.s._root), \
self.s._root.right)
self.assertEqual(self.s.binaryHelper(-n+1, self.s._root), \
self.s._root.left)
def testRemoveDeep(self):
"""Test if removing an item from the tree updates it correctly.
Do several rounds of inserts and removes and make sure that there are no
inconsistancies between a splay tree and a python set.
"""
ref = set()
#pdb.set_trace()
for j in xrange(randint(5, 50)):
N = randint(60, 170)
R = N/2 + randint(0, N/2) #remove over half the entries
for i in xrange(N): #add
a = randint(-2147483648,2147483647)
self.s.insert(a, True)
ref.add(a)
self.assertTrue(a in self.s)
self.assertTrue(a in ref)
#ensure both contain equal number of elements
#self.assertEqual(len(ref), len(self.s))
for i in xrange(R): #remove
a = ref.pop()
self.s.remove(a)
self.assertFalse(a in self.s)
self.assertFalse(a in ref)
#check no extra elements were accidentially deleted
for j in ref:
try:
self.assertTrue(j in self.s)
except Exception as e:
print "missing:", j
print self.s
print ref
raise e
#check consistancy twice
self.assertEqual(len(ref), len(self.s))
for i in ref:
self.assertIsNotNone(self.s.find(i))
self.assertEqual(len(ref), len(self.s))
for i in ref:
self.assertIsNotNone(self.s.find(i))
def testInsertDeep(self):
"""Test if parent/child relationships in the tree 'seem' ok."""
#insert
for i in xrange(randint(50, 180)):
self.s.insert(randint(-2147483648,2147483647), i)
#walk through the tree
self.assertIsNotNone(self.s._root)
self.assertIsNone(self.s._root.parent)
self.assertIsNotNone(self.s._root.left)
self.assertIsNotNone(self.s._root.right)
def traversalHelper(n):
if not n:
return
self.assertTrue((n.parent.left is n) or (n.parent.right is n))
traversalHelper(n.left)
traversalHelper(n.right)
traversalHelper(self.s._root.left)
traversalHelper(self.s._root.right)
if __name__ == "__main__":
unittest.main()