-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
588 lines (497 loc) · 18.4 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# Test cases for ImplementMe class.
# The mocked objects (and therefore expected output) may change
# at the point of evaluation, including into a more complex object,
# but the functionality tested by each test case will not.
# Your implementation should anticipate ways in which these mocks could
# be more complex.
#
# Three cases are not yet disclosed; they will be challenging combinations
# of existing test cases.
import unittest
from node import *
from index import *
from implement_me import ImplementMe
# Insert into an empty tree
class TestCase01(unittest.TestCase):
def test_insertion(self):
btree = Index([])
key = 99
expected_output = Index([Node()]*1)
expected_output.nodes[ 0 ] = Node(\
KeySet((99, -1)),\
PointerSet((0,0,0)))
self.assertEqual( expected_output, ImplementMe.InsertIntoIndex( btree, key ) )
# Insert existing key
class TestCase02(unittest.TestCase):
def test_insertion(self):
btree = Index([Node()]*1)
btree.nodes[ 0 ] = Node(\
KeySet((99, -1)),\
PointerSet((0,0,0)))
key = 99
expected_output = Index([Node()]*1)
expected_output.nodes[ 0 ] = Node(\
KeySet((99, -1)),\
PointerSet((0,0,0)))
self.assertEqual( expected_output, ImplementMe.InsertIntoIndex( btree, key ) )
# Insert into existing node that is not full
class TestCase03(unittest.TestCase):
def test_insertion(self):
btree = Index([Node()]*1)
btree.nodes[ 0 ] = Node(\
KeySet((87, -1)),\
PointerSet((0,0,0)))
key = 66
expected_output = Index([Node()]*1)
expected_output.nodes[ 0 ] = Node(\
KeySet((66, 87)),\
PointerSet((0,0,0)))
self.assertEqual( expected_output, ImplementMe.InsertIntoIndex( btree, key ) )
# Insert into full node.
class TestCase04(unittest.TestCase):
def test_insertion(self):
btree = Index([Node()]*1)
btree.nodes[ 0 ] = Node(\
KeySet((66, 99)),\
PointerSet((0,0,0)))
key = 87
expected_output = Index([Node()]*4)
expected_output.nodes[0] = Node(\
KeySet((87, -1)),\
PointerSet((1,2,0)))
expected_output.nodes[1] = Node(\
KeySet((66,-1)),\
PointerSet((0,0,2)))
expected_output.nodes[2]=Node(\
KeySet((87,99)),\
PointerSet((0,0,0)))
self.assertEqual( expected_output, ImplementMe.InsertIntoIndex( btree, key ) )
# Insert into full node with full parent, causing root split.
class TestCase05(unittest.TestCase):
def test_insertion(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((42, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((7,-1)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,87)),\
PointerSet((0,0,0)))
key = 99
expected_output = Index([Node()]*13)
expected_output.nodes[0] = Node(\
KeySet((66, -1)),\
PointerSet((1,2,0)))
expected_output.nodes[1] = Node(\
KeySet((42,-1)),\
PointerSet((4,5,0)))
expected_output.nodes[2]=Node(\
KeySet((87,-1)),\
PointerSet((7,8,0)))
expected_output.nodes[4]=Node(\
KeySet((7,-1)),\
PointerSet((0,0,5)))
expected_output.nodes[5]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,7)))
expected_output.nodes[7]=Node(\
KeySet((66,-1)),\
PointerSet((0,0,8)))
expected_output.nodes[8]=Node(\
KeySet((87,99)),\
PointerSet((0,0,0)))
self.assertEqual( expected_output, ImplementMe.InsertIntoIndex( btree, key ) )
# Insert into full node with full parent, but does not cause a root split.
# Note that only the path that should be affected has correct data (testing complexity)
# Linearisation forces copy of some nodes to new addresses
class TestCase06(unittest.TestCase):
def test_insertion(self):
btree = Index([Node()]*13)
btree.nodes[0] = Node(\
KeySet((7, -1)),\
PointerSet((1,2,0)))
btree.nodes[2]=Node(\
KeySet((27,66)),\
PointerSet((7,8,9)))
btree.nodes[6]=Node(\
KeySet((11,11)),\
PointerSet((0,0,90))) # Dummy data for test
btree.nodes[7]=Node(\
KeySet((7,9)),\
PointerSet((0,0,8)))
btree.nodes[8]=Node(\
KeySet((27,-1)),\
PointerSet((0,0,9)))
btree.nodes[9]=Node(\
KeySet((66,88)),\
PointerSet((0,0,0)))
key = 12
expected_output = Index([Node()]*13)
expected_output.nodes[0] = Node(\
KeySet((7, 27)),\
PointerSet((1,2,3)))
expected_output.nodes[2] = Node(\
KeySet((9,-1)),\
PointerSet((7,8,0)))
expected_output.nodes[3]=Node(\
KeySet((66,-1)),\
PointerSet((10,11,0)))
expected_output.nodes[6]=Node(\
KeySet((11,11)),\
PointerSet((0,0,90))) # Dummy data for test
expected_output.nodes[7]=Node(\
KeySet((7,-1)),\
PointerSet((0,0,8)))
expected_output.nodes[8]=Node(\
KeySet((9,12)),\
PointerSet((0,0,10)))
expected_output.nodes[10]=Node(\
KeySet((27,-1)),\
PointerSet((0,0,11)))
expected_output.nodes[11]=Node(\
KeySet((66,88)),\
PointerSet((0,0,0)))
self.assertEqual( expected_output, ImplementMe.InsertIntoIndex( btree, key ) )
# Insertion causes splits that propagates at least three times
# Note that only the path that should be affected has correct data (testing complexity)
# Linearisation forces copy of some nodes to new addresses
class TestCase07(unittest.TestCase):
def test_insertion(self):
btree = Index([Node()]*13)
btree.nodes[0] = Node(\
KeySet((7, 99)),\
PointerSet((1,2,0)))
btree.nodes[2]=Node(\
KeySet((27,66)),\
PointerSet((7,8,9)))
btree.nodes[7]=Node(\
KeySet((7,9)),\
PointerSet((0,0,8)))
btree.nodes[8]=Node(\
KeySet((27,-1)),\
PointerSet((0,0,9)))
btree.nodes[9]=Node(\
KeySet((66,88)),\
PointerSet((0,0,0)))
key = 12
expected_output = Index([Node()]*40)
expected_output.nodes[0] = Node(\
KeySet((27, -1)),\
PointerSet((1,2,0)))
expected_output.nodes[1] = Node(\
KeySet((7, -1)),\
PointerSet((4,5,0)))
expected_output.nodes[2] = Node(\
KeySet((99, -1)),\
PointerSet((7,8,0)))
expected_output.nodes[5] = Node(\
KeySet((9,-1)),\
PointerSet((16,17,0)))
expected_output.nodes[7]=Node(\
KeySet((66,-1)),\
PointerSet((22,23,0)))
expected_output.nodes[16]=Node(\
KeySet((7,-1)),\
PointerSet((0,0,17)))
expected_output.nodes[17]=Node(\
KeySet((9,12)),\
PointerSet((0,0,22)))
expected_output.nodes[22]=Node(\
KeySet((27,-1)),\
PointerSet((0,0,23)))
expected_output.nodes[23]=Node(\
KeySet((66,88)),\
PointerSet((0,0,0)))
self.assertEqual( expected_output, ImplementMe.InsertIntoIndex( btree, key ) )
# Boundary case: lookup smallest key in tree
# Fake data in last node to test complexity
class TestCase08(unittest.TestCase):
def test_lookup(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((42, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((9,-1)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,7)),\
PointerSet((0,0,0)))
key = 9
expected_output = True
self.assertEqual( expected_output, ImplementMe.LookupKeyInIndex( btree, key ) )
# Boundary case: lookup largest key in tree
# Fake data in first node to test complexity
class TestCase09(unittest.TestCase):
def test_lookup(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((42, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((7,99)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,87)),\
PointerSet((0,0,0)))
key = 87
expected_output = True
self.assertEqual( expected_output, ImplementMe.LookupKeyInIndex( btree, key ) )
# Lookup key outside range of tree's keys
# Fake data in middle leaf to test complexity
class TestCase10(unittest.TestCase):
def test_lookup(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((42, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((9,-1)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((7,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,99)),\
PointerSet((0,0,0)))
key = 7
expected_output = False
self.assertEqual( expected_output, ImplementMe.LookupKeyInIndex( btree, key ) )
# Lookup key within tree's range but not in tree
# Fake data in one leaf to test complexity
class TestCase11(unittest.TestCase):
def test_lookup(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((42, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((7,-1)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,9)),\
PointerSet((0,0,0)))
key = 9
expected_output = False
self.assertEqual( expected_output, ImplementMe.LookupKeyInIndex( btree, key ) )
# Lookup key strictly within the tree's range
class TestCase12(unittest.TestCase):
def test_lookup(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((41, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((7,-1)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,99)),\
PointerSet((0,0,0)))
key = 42
expected_output = True
self.assertEqual( expected_output, ImplementMe.LookupKeyInIndex( btree, key ) )
# Range query fully contained in one leaf node
# Fake data in other node to test complexity
class TestCase13(unittest.TestCase):
def test_range(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((42, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((7,68)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,99)),\
PointerSet((0,0,0)))
lower_bound = 66
upper_bound = 87
expected_output = [66]
self.assertEqual( expected_output, ImplementMe.RangeSearchInIndex( btree, lower_bound, upper_bound ) )
# Range query half-open to the left
# Fake data in one node to test complexity.
class TestCase14(unittest.TestCase):
def test_range(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((42, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((7,-1)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,9)),\
PointerSet((0,0,0)))
lower_bound = 0
upper_bound = 42
expected_output = [7]
self.assertEqual( expected_output, ImplementMe.RangeSearchInIndex( btree, lower_bound, upper_bound ) )
# Range query half-open to the right
# Fake data in one node to test complexity
class TestCase15(unittest.TestCase):
def test_range(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((42, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((7,68)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,87)),\
PointerSet((0,0,0)))
lower_bound = 42
upper_bound = 99
expected_output = [42,66,87]
self.assertEqual( expected_output, ImplementMe.RangeSearchInIndex( btree, lower_bound, upper_bound ) )
# Range query with matching upper and lower bound
# Key not in tree but found as fake data in a different node to test complexity
class TestCase16(unittest.TestCase):
def test_range(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((42, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((7,-1)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,7)),\
PointerSet((0,0,0)))
lower_bound = 7
upper_bound = 7
expected_output = []
self.assertEqual( expected_output, ImplementMe.RangeSearchInIndex( btree, lower_bound, upper_bound ) )
# Multi-leaf range query in middle of tree
# Fake data in first node to test complexity
class TestCase17(unittest.TestCase):
def test_range(self):
btree = Index([Node()]*4)
btree.nodes[0] = Node(\
KeySet((42, 66)),\
PointerSet((1,2,3)))
btree.nodes[1] = Node(\
KeySet((68,-1)),\
PointerSet((0,0,2)))
btree.nodes[2]=Node(\
KeySet((42,-1)),\
PointerSet((0,0,3)))
btree.nodes[3]=Node(\
KeySet((66,99)),\
PointerSet((0,0,0)))
lower_bound = 42
upper_bound = 87
expected_output = [42,66]
self.assertEqual( expected_output, ImplementMe.RangeSearchInIndex( btree, lower_bound, upper_bound ) )
# Lookup recently added key
class TestCase18(unittest.TestCase):
def test_unknown(self):
btree = Index([Node()]*13)
btree.nodes[0] = Node(\
KeySet((7, 99)),\
PointerSet((1,2,0)))
btree.nodes[2]=Node(\
KeySet((27,66)),\
PointerSet((7,8,9)))
btree.nodes[7]=Node(\
KeySet((7,9)),\
PointerSet((0,0,8)))
btree.nodes[8]=Node(\
KeySet((27,-1)),\
PointerSet((0,0,9)))
btree.nodes[9]=Node(\
KeySet((66,88)),\
PointerSet((0,0,0)))
key = 12
expected_output = True
self.assertEqual( expected_output, ImplementMe.LookupKeyInIndex(\
ImplementMe.InsertIntoIndex( btree, key ), key ) )
# Lookup range that includes recently added key
class TestCase19(unittest.TestCase):
def test_unknown(self):
btree = Index([Node()]*13)
btree.nodes[0] = Node(\
KeySet((7, 99)),\
PointerSet((1,2,0)))
btree.nodes[2]=Node(\
KeySet((27,66)),\
PointerSet((7,8,9)))
btree.nodes[7]=Node(\
KeySet((7,9)),\
PointerSet((0,0,8)))
btree.nodes[8]=Node(\
KeySet((27,-1)),\
PointerSet((0,0,9)))
btree.nodes[9]=Node(\
KeySet((66,88)),\
PointerSet((0,0,0)))
key = 12
lower_bound = 12
upper_bound = 66
expected_output = [12,27]
self.assertEqual( expected_output, ImplementMe.RangeSearchInIndex(\
ImplementMe.InsertIntoIndex( btree, key ), lower_bound, upper_bound ) )
# Lookup range with nearly matching lower and upper bound equal to recently added key
class TestCase20(unittest.TestCase):
def test_unknown(self):
btree = Index([Node()]*13)
btree.nodes[0] = Node(\
KeySet((7, 99)),\
PointerSet((1,2,0)))
btree.nodes[2]=Node(\
KeySet((27,66)),\
PointerSet((7,8,9)))
btree.nodes[7]=Node(\
KeySet((7,9)),\
PointerSet((0,0,8)))
btree.nodes[8]=Node(\
KeySet((27,-1)),\
PointerSet((0,0,9)))
btree.nodes[9]=Node(\
KeySet((66,88)),\
PointerSet((0,0,0)))
key = 12
lower_bound = 12
upper_bound = 13
expected_output = [12]
self.assertEqual( expected_output, ImplementMe.RangeSearchInIndex(\
ImplementMe.InsertIntoIndex( btree, key ), lower_bound, upper_bound ) )
# Freebie for grinding out a tough semester
# Look up a key in an empty tree
class TestCaseB1(unittest.TestCase):
def test_unknown(self):
btree = Index([Node()]*1)
key = 9
expected_output = False
self.assertEqual( expected_output, ImplementMe.LookupKeyInIndex( btree, key ) )
# Run all unit tests above.
unittest.main(argv=[''],verbosity=2, exit=False)