Debian Codegraph @

trees/btree/btree_test.go

raw symbols SCIP
1 // Copyright (c) 2015, Emir Pasic. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package btree
6
7 import (
8 "encoding/json"
9 "slices"
10 "strings"
11 "testing"
12 )
13
14 func TestBTreeGet1(t *testing.T) {
15 tree := New[int, string](3)
16 tree.Put(1, "a")
17 tree.Put(2, "b")
18 tree.Put(3, "c")
19 tree.Put(4, "d")
20 tree.Put(5, "e")
21 tree.Put(6, "f")
22 tree.Put(7, "g")
23
24 tests := [][]interface{}{
25 {0, "", false},
26 {1, "a", true},
27 {2, "b", true},
28 {3, "c", true},
29 {4, "d", true},
30 {5, "e", true},
31 {6, "f", true},
32 {7, "g", true},
33 {8, "", false},
34 }
35
36 for _, test := range tests {
37 if value, found := tree.Get(test[0].(int)); value != test[1] || found != test[2] {
38 t.Errorf("Got %v,%v expected %v,%v", value, found, test[1], test[2])
39 }
40 }
41 }
42
43 func TestBTreeGet2(t *testing.T) {
44 tree := New[int, string](3)
45 tree.Put(7, "g")
46 tree.Put(9, "i")
47 tree.Put(10, "j")
48 tree.Put(6, "f")
49 tree.Put(3, "c")
50 tree.Put(4, "d")
51 tree.Put(5, "e")
52 tree.Put(8, "h")
53 tree.Put(2, "b")
54 tree.Put(1, "a")
55
56 tests := [][]interface{}{
57 {0, "", false},
58 {1, "a", true},
59 {2, "b", true},
60 {3, "c", true},
61 {4, "d", true},
62 {5, "e", true},
63 {6, "f", true},
64 {7, "g", true},
65 {8, "h", true},
66 {9, "i", true},
67 {10, "j", true},
68 {11, "", false},
69 }
70
71 for _, test := range tests {
72 if value, found := tree.Get(test[0].(int)); value != test[1] || found != test[2] {
73 t.Errorf("Got %v,%v expected %v,%v", value, found, test[1], test[2])
74 }
75 }
76 }
77
78 func TestBTreeGet3(t *testing.T) {
79 tree := New[int, string](3)
80
81 if actualValue := tree.Size(); actualValue != 0 {
82 t.Errorf("Got %v expected %v", actualValue, 0)
83 }
84
85 if actualValue := tree.GetNode(2).Size(); actualValue != 0 {
86 t.Errorf("Got %v expected %v", actualValue, 0)
87 }
88
89 tree.Put(1, "x") // 1->x
90 tree.Put(2, "b") // 1->x, 2->b (in order)
91 tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
92 tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
93 tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
94 tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
95 tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
96 tree.Put(7, "g") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f, 7->g (in order)
97
98 // BTree
99 // 1
100 // 2
101 // 3
102 // 4
103 // 5
104 // 6
105 // 7
106
107 if actualValue := tree.Size(); actualValue != 7 {
108 t.Errorf("Got %v expected %v", actualValue, 7)
109 }
110
111 if actualValue := tree.GetNode(2).Size(); actualValue != 3 {
112 t.Errorf("Got %v expected %v", actualValue, 3)
113 }
114
115 if actualValue := tree.GetNode(4).Size(); actualValue != 7 {
116 t.Errorf("Got %v expected %v", actualValue, 7)
117 }
118
119 if actualValue := tree.GetNode(8).Size(); actualValue != 0 {
120 t.Errorf("Got %v expected %v", actualValue, 0)
121 }
122 }
123
124 func TestBTreePut1(t *testing.T) {
125 // https://upload.wikimedia.org/wikipedia/commons/3/33/B_tree_insertion_example.png
126 tree := New[int, int](3)
127 assertValidTree(t, tree, 0)
128
129 tree.Put(1, 0)
130 assertValidTree(t, tree, 1)
131 assertValidTreeNode(t, tree.Root, 1, 0, []int{1}, false)
132
133 tree.Put(2, 1)
134 assertValidTree(t, tree, 2)
135 assertValidTreeNode(t, tree.Root, 2, 0, []int{1, 2}, false)
136
137 tree.Put(3, 2)
138 assertValidTree(t, tree, 3)
139 assertValidTreeNode(t, tree.Root, 1, 2, []int{2}, false)
140 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
141 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{3}, true)
142
143 tree.Put(4, 2)
144 assertValidTree(t, tree, 4)
145 assertValidTreeNode(t, tree.Root, 1, 2, []int{2}, false)
146 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
147 assertValidTreeNode(t, tree.Root.Children[1], 2, 0, []int{3, 4}, true)
148
149 tree.Put(5, 2)
150 assertValidTree(t, tree, 5)
151 assertValidTreeNode(t, tree.Root, 2, 3, []int{2, 4}, false)
152 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
153 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{3}, true)
154 assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{5}, true)
155
156 tree.Put(6, 2)
157 assertValidTree(t, tree, 6)
158 assertValidTreeNode(t, tree.Root, 2, 3, []int{2, 4}, false)
159 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
160 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{3}, true)
161 assertValidTreeNode(t, tree.Root.Children[2], 2, 0, []int{5, 6}, true)
162
163 tree.Put(7, 2)
164 assertValidTree(t, tree, 7)
165 assertValidTreeNode(t, tree.Root, 1, 2, []int{4}, false)
166 assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{2}, true)
167 assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{6}, true)
168 assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{1}, true)
169 assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{3}, true)
170 assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{5}, true)
171 assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{7}, true)
172 }
173
174 func TestBTreePut2(t *testing.T) {
175 tree := New[int, int](4)
176 assertValidTree(t, tree, 0)
177
178 tree.Put(0, 0)
179 assertValidTree(t, tree, 1)
180 assertValidTreeNode(t, tree.Root, 1, 0, []int{0}, false)
181
182 tree.Put(2, 2)
183 assertValidTree(t, tree, 2)
184 assertValidTreeNode(t, tree.Root, 2, 0, []int{0, 2}, false)
185
186 tree.Put(1, 1)
187 assertValidTree(t, tree, 3)
188 assertValidTreeNode(t, tree.Root, 3, 0, []int{0, 1, 2}, false)
189
190 tree.Put(1, 1)
191 assertValidTree(t, tree, 3)
192 assertValidTreeNode(t, tree.Root, 3, 0, []int{0, 1, 2}, false)
193
194 tree.Put(3, 3)
195 assertValidTree(t, tree, 4)
196 assertValidTreeNode(t, tree.Root, 1, 2, []int{1}, false)
197 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{0}, true)
198 assertValidTreeNode(t, tree.Root.Children[1], 2, 0, []int{2, 3}, true)
199
200 tree.Put(4, 4)
201 assertValidTree(t, tree, 5)
202 assertValidTreeNode(t, tree.Root, 1, 2, []int{1}, false)
203 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{0}, true)
204 assertValidTreeNode(t, tree.Root.Children[1], 3, 0, []int{2, 3, 4}, true)
205
206 tree.Put(5, 5)
207 assertValidTree(t, tree, 6)
208 assertValidTreeNode(t, tree.Root, 2, 3, []int{1, 3}, false)
209 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{0}, true)
210 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{2}, true)
211 assertValidTreeNode(t, tree.Root.Children[2], 2, 0, []int{4, 5}, true)
212 }
213
214 func TestBTreePut3(t *testing.T) {
215 // http://www.geeksforgeeks.org/b-tree-set-1-insert-2/
216 tree := New[int, int](6)
217 assertValidTree(t, tree, 0)
218
219 tree.Put(10, 0)
220 assertValidTree(t, tree, 1)
221 assertValidTreeNode(t, tree.Root, 1, 0, []int{10}, false)
222
223 tree.Put(20, 1)
224 assertValidTree(t, tree, 2)
225 assertValidTreeNode(t, tree.Root, 2, 0, []int{10, 20}, false)
226
227 tree.Put(30, 2)
228 assertValidTree(t, tree, 3)
229 assertValidTreeNode(t, tree.Root, 3, 0, []int{10, 20, 30}, false)
230
231 tree.Put(40, 3)
232 assertValidTree(t, tree, 4)
233 assertValidTreeNode(t, tree.Root, 4, 0, []int{10, 20, 30, 40}, false)
234
235 tree.Put(50, 4)
236 assertValidTree(t, tree, 5)
237 assertValidTreeNode(t, tree.Root, 5, 0, []int{10, 20, 30, 40, 50}, false)
238
239 tree.Put(60, 5)
240 assertValidTree(t, tree, 6)
241 assertValidTreeNode(t, tree.Root, 1, 2, []int{30}, false)
242 assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{10, 20}, true)
243 assertValidTreeNode(t, tree.Root.Children[1], 3, 0, []int{40, 50, 60}, true)
244
245 tree.Put(70, 6)
246 assertValidTree(t, tree, 7)
247 assertValidTreeNode(t, tree.Root, 1, 2, []int{30}, false)
248 assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{10, 20}, true)
249 assertValidTreeNode(t, tree.Root.Children[1], 4, 0, []int{40, 50, 60, 70}, true)
250
251 tree.Put(80, 7)
252 assertValidTree(t, tree, 8)
253 assertValidTreeNode(t, tree.Root, 1, 2, []int{30}, false)
254 assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{10, 20}, true)
255 assertValidTreeNode(t, tree.Root.Children[1], 5, 0, []int{40, 50, 60, 70, 80}, true)
256
257 tree.Put(90, 8)
258 assertValidTree(t, tree, 9)
259 assertValidTreeNode(t, tree.Root, 2, 3, []int{30, 60}, false)
260 assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{10, 20}, true)
261 assertValidTreeNode(t, tree.Root.Children[1], 2, 0, []int{40, 50}, true)
262 assertValidTreeNode(t, tree.Root.Children[2], 3, 0, []int{70, 80, 90}, true)
263 }
264
265 func TestBTreePut4(t *testing.T) {
266 tree := New[int, *struct{}](3)
267 assertValidTree(t, tree, 0)
268
269 tree.Put(6, nil)
270 assertValidTree(t, tree, 1)
271 assertValidTreeNode(t, tree.Root, 1, 0, []int{6}, false)
272
273 tree.Put(5, nil)
274 assertValidTree(t, tree, 2)
275 assertValidTreeNode(t, tree.Root, 2, 0, []int{5, 6}, false)
276
277 tree.Put(4, nil)
278 assertValidTree(t, tree, 3)
279 assertValidTreeNode(t, tree.Root, 1, 2, []int{5}, false)
280 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{4}, true)
281 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{6}, true)
282
283 tree.Put(3, nil)
284 assertValidTree(t, tree, 4)
285 assertValidTreeNode(t, tree.Root, 1, 2, []int{5}, false)
286 assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{3, 4}, true)
287 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{6}, true)
288
289 tree.Put(2, nil)
290 assertValidTree(t, tree, 5)
291 assertValidTreeNode(t, tree.Root, 2, 3, []int{3, 5}, false)
292 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{2}, true)
293 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{4}, true)
294 assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{6}, true)
295
296 tree.Put(1, nil)
297 assertValidTree(t, tree, 6)
298 assertValidTreeNode(t, tree.Root, 2, 3, []int{3, 5}, false)
299 assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{1, 2}, true)
300 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{4}, true)
301 assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{6}, true)
302
303 tree.Put(0, nil)
304 assertValidTree(t, tree, 7)
305 assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
306 assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{1}, true)
307 assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
308 assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{0}, true)
309 assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{2}, true)
310 assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
311 assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
312
313 tree.Put(-1, nil)
314 assertValidTree(t, tree, 8)
315 assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
316 assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{1}, true)
317 assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
318 assertValidTreeNode(t, tree.Root.Children[0].Children[0], 2, 0, []int{-1, 0}, true)
319 assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{2}, true)
320 assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
321 assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
322
323 tree.Put(-2, nil)
324 assertValidTree(t, tree, 9)
325 assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
326 assertValidTreeNode(t, tree.Root.Children[0], 2, 3, []int{-1, 1}, true)
327 assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
328 assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{-2}, true)
329 assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{0}, true)
330 assertValidTreeNode(t, tree.Root.Children[0].Children[2], 1, 0, []int{2}, true)
331 assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
332 assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
333
334 tree.Put(-3, nil)
335 assertValidTree(t, tree, 10)
336 assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
337 assertValidTreeNode(t, tree.Root.Children[0], 2, 3, []int{-1, 1}, true)
338 assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{5}, true)
339 assertValidTreeNode(t, tree.Root.Children[0].Children[0], 2, 0, []int{-3, -2}, true)
340 assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{0}, true)
341 assertValidTreeNode(t, tree.Root.Children[0].Children[2], 1, 0, []int{2}, true)
342 assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{4}, true)
343 assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{6}, true)
344
345 tree.Put(-4, nil)
346 assertValidTree(t, tree, 11)
347 assertValidTreeNode(t, tree.Root, 2, 3, []int{-1, 3}, false)
348 assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{-3}, true)
349 assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{1}, true)
350 assertValidTreeNode(t, tree.Root.Children[2], 1, 2, []int{5}, true)
351 assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{-4}, true)
352 assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{-2}, true)
353 assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{0}, true)
354 assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{2}, true)
355 assertValidTreeNode(t, tree.Root.Children[2].Children[0], 1, 0, []int{4}, true)
356 assertValidTreeNode(t, tree.Root.Children[2].Children[1], 1, 0, []int{6}, true)
357 }
358
359 func TestBTreeRemove1(t *testing.T) {
360 // empty
361 tree := New[int, int](3)
362 tree.Remove(1)
363 assertValidTree(t, tree, 0)
364 }
365
366 func TestBTreeRemove2(t *testing.T) {
367 // leaf node (no underflow)
368 tree := New[int, *struct{}](3)
369 tree.Put(1, nil)
370 tree.Put(2, nil)
371
372 tree.Remove(1)
373 assertValidTree(t, tree, 1)
374 assertValidTreeNode(t, tree.Root, 1, 0, []int{2}, false)
375
376 tree.Remove(2)
377 assertValidTree(t, tree, 0)
378 }
379
380 func TestBTreeRemove3(t *testing.T) {
381 // merge with right (underflow)
382 {
383 tree := New[int, *struct{}](3)
384 tree.Put(1, nil)
385 tree.Put(2, nil)
386 tree.Put(3, nil)
387
388 tree.Remove(1)
389 assertValidTree(t, tree, 2)
390 assertValidTreeNode(t, tree.Root, 2, 0, []int{2, 3}, false)
391 }
392 // merge with left (underflow)
393 {
394 tree := New[int, *struct{}](3)
395 tree.Put(1, nil)
396 tree.Put(2, nil)
397 tree.Put(3, nil)
398
399 tree.Remove(3)
400 assertValidTree(t, tree, 2)
401 assertValidTreeNode(t, tree.Root, 2, 0, []int{1, 2}, false)
402 }
403 }
404
405 func TestBTreeRemove4(t *testing.T) {
406 // rotate left (underflow)
407 tree := New[int, *struct{}](3)
408 tree.Put(1, nil)
409 tree.Put(2, nil)
410 tree.Put(3, nil)
411 tree.Put(4, nil)
412
413 assertValidTree(t, tree, 4)
414 assertValidTreeNode(t, tree.Root, 1, 2, []int{2}, false)
415 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
416 assertValidTreeNode(t, tree.Root.Children[1], 2, 0, []int{3, 4}, true)
417
418 tree.Remove(1)
419 assertValidTree(t, tree, 3)
420 assertValidTreeNode(t, tree.Root, 1, 2, []int{3}, false)
421 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{2}, true)
422 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{4}, true)
423 }
424
425 func TestBTreeRemove5(t *testing.T) {
426 // rotate right (underflow)
427 tree := New[int, *struct{}](3)
428 tree.Put(1, nil)
429 tree.Put(2, nil)
430 tree.Put(3, nil)
431 tree.Put(0, nil)
432
433 assertValidTree(t, tree, 4)
434 assertValidTreeNode(t, tree.Root, 1, 2, []int{2}, false)
435 assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{0, 1}, true)
436 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{3}, true)
437
438 tree.Remove(3)
439 assertValidTree(t, tree, 3)
440 assertValidTreeNode(t, tree.Root, 1, 2, []int{1}, false)
441 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{0}, true)
442 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{2}, true)
443 }
444
445 func TestBTreeRemove6(t *testing.T) {
446 // root height reduction after a series of underflows on right side
447 // use simulator: https://www.cs.usfca.edu/~galles/visualization/BTree.html
448 tree := New[int, *struct{}](3)
449 tree.Put(1, nil)
450 tree.Put(2, nil)
451 tree.Put(3, nil)
452 tree.Put(4, nil)
453 tree.Put(5, nil)
454 tree.Put(6, nil)
455 tree.Put(7, nil)
456
457 assertValidTree(t, tree, 7)
458 assertValidTreeNode(t, tree.Root, 1, 2, []int{4}, false)
459 assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{2}, true)
460 assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{6}, true)
461 assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{1}, true)
462 assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{3}, true)
463 assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{5}, true)
464 assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{7}, true)
465
466 tree.Remove(7)
467 assertValidTree(t, tree, 6)
468 assertValidTreeNode(t, tree.Root, 2, 3, []int{2, 4}, false)
469 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{1}, true)
470 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{3}, true)
471 assertValidTreeNode(t, tree.Root.Children[2], 2, 0, []int{5, 6}, true)
472 }
473
474 func TestBTreeRemove7(t *testing.T) {
475 // root height reduction after a series of underflows on left side
476 // use simulator: https://www.cs.usfca.edu/~galles/visualization/BTree.html
477 tree := New[int, *struct{}](3)
478 tree.Put(1, nil)
479 tree.Put(2, nil)
480 tree.Put(3, nil)
481 tree.Put(4, nil)
482 tree.Put(5, nil)
483 tree.Put(6, nil)
484 tree.Put(7, nil)
485
486 assertValidTree(t, tree, 7)
487 assertValidTreeNode(t, tree.Root, 1, 2, []int{4}, false)
488 assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{2}, true)
489 assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{6}, true)
490 assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{1}, true)
491 assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{3}, true)
492 assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{5}, true)
493 assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{7}, true)
494
495 tree.Remove(1) // series of underflows
496 assertValidTree(t, tree, 6)
497 assertValidTreeNode(t, tree.Root, 2, 3, []int{4, 6}, false)
498 assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{2, 3}, true)
499 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{5}, true)
500 assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{7}, true)
501
502 // clear all remaining
503 tree.Remove(2)
504 assertValidTree(t, tree, 5)
505 assertValidTreeNode(t, tree.Root, 2, 3, []int{4, 6}, false)
506 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{3}, true)
507 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{5}, true)
508 assertValidTreeNode(t, tree.Root.Children[2], 1, 0, []int{7}, true)
509
510 tree.Remove(3)
511 assertValidTree(t, tree, 4)
512 assertValidTreeNode(t, tree.Root, 1, 2, []int{6}, false)
513 assertValidTreeNode(t, tree.Root.Children[0], 2, 0, []int{4, 5}, true)
514 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{7}, true)
515
516 tree.Remove(4)
517 assertValidTree(t, tree, 3)
518 assertValidTreeNode(t, tree.Root, 1, 2, []int{6}, false)
519 assertValidTreeNode(t, tree.Root.Children[0], 1, 0, []int{5}, true)
520 assertValidTreeNode(t, tree.Root.Children[1], 1, 0, []int{7}, true)
521
522 tree.Remove(5)
523 assertValidTree(t, tree, 2)
524 assertValidTreeNode(t, tree.Root, 2, 0, []int{6, 7}, false)
525
526 tree.Remove(6)
527 assertValidTree(t, tree, 1)
528 assertValidTreeNode(t, tree.Root, 1, 0, []int{7}, false)
529
530 tree.Remove(7)
531 assertValidTree(t, tree, 0)
532 }
533
534 func TestBTreeRemove8(t *testing.T) {
535 // use simulator: https://www.cs.usfca.edu/~galles/visualization/BTree.html
536 tree := New[int, *struct{}](3)
537 tree.Put(1, nil)
538 tree.Put(2, nil)
539 tree.Put(3, nil)
540 tree.Put(4, nil)
541 tree.Put(5, nil)
542 tree.Put(6, nil)
543 tree.Put(7, nil)
544 tree.Put(8, nil)
545 tree.Put(9, nil)
546
547 assertValidTree(t, tree, 9)
548 assertValidTreeNode(t, tree.Root, 1, 2, []int{4}, false)
549 assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{2}, true)
550 assertValidTreeNode(t, tree.Root.Children[1], 2, 3, []int{6, 8}, true)
551 assertValidTreeNode(t, tree.Root.Children[0].Children[0], 1, 0, []int{1}, true)
552 assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{3}, true)
553 assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{5}, true)
554 assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{7}, true)
555 assertValidTreeNode(t, tree.Root.Children[1].Children[2], 1, 0, []int{9}, true)
556
557 tree.Remove(1)
558 assertValidTree(t, tree, 8)
559 assertValidTreeNode(t, tree.Root, 1, 2, []int{6}, false)
560 assertValidTreeNode(t, tree.Root.Children[0], 1, 2, []int{4}, true)
561 assertValidTreeNode(t, tree.Root.Children[1], 1, 2, []int{8}, true)
562 assertValidTreeNode(t, tree.Root.Children[0].Children[0], 2, 0, []int{2, 3}, true)
563 assertValidTreeNode(t, tree.Root.Children[0].Children[1], 1, 0, []int{5}, true)
564 assertValidTreeNode(t, tree.Root.Children[1].Children[0], 1, 0, []int{7}, true)
565 assertValidTreeNode(t, tree.Root.Children[1].Children[1], 1, 0, []int{9}, true)
566 }
567
568 func TestBTreeRemove9(t *testing.T) {
569 const max = 1000
570 orders := []int{3, 4, 5, 6, 7, 8, 9, 10, 20, 100, 500, 1000, 5000, 10000}
571 for _, order := range orders {
572
573 tree := New[int, int](order)
574
575 {
576 for i := 1; i <= max; i++ {
577 tree.Put(i, i)
578 }
579 assertValidTree(t, tree, max)
580
581 for i := 1; i <= max; i++ {
582 if _, found := tree.Get(i); !found {
583 t.Errorf("Not found %v", i)
584 }
585 }
586
587 for i := 1; i <= max; i++ {
588 tree.Remove(i)
589 }
590 assertValidTree(t, tree, 0)
591 }
592
593 {
594 for i := max; i > 0; i-- {
595 tree.Put(i, i)
596 }
597 assertValidTree(t, tree, max)
598
599 for i := max; i > 0; i-- {
600 if _, found := tree.Get(i); !found {
601 t.Errorf("Not found %v", i)
602 }
603 }
604
605 for i := max; i > 0; i-- {
606 tree.Remove(i)
607 }
608 assertValidTree(t, tree, 0)
609 }
610 }
611 }
612
613 func TestBTreeHeight(t *testing.T) {
614 tree := New[int, int](3)
615 if actualValue, expectedValue := tree.Height(), 0; actualValue != expectedValue {
616 t.Errorf("Got %v expected %v", actualValue, expectedValue)
617 }
618
619 tree.Put(1, 0)
620 if actualValue, expectedValue := tree.Height(), 1; actualValue != expectedValue {
621 t.Errorf("Got %v expected %v", actualValue, expectedValue)
622 }
623
624 tree.Put(2, 1)
625 if actualValue, expectedValue := tree.Height(), 1; actualValue != expectedValue {
626 t.Errorf("Got %v expected %v", actualValue, expectedValue)
627 }
628
629 tree.Put(3, 2)
630 if actualValue, expectedValue := tree.Height(), 2; actualValue != expectedValue {
631 t.Errorf("Got %v expected %v", actualValue, expectedValue)
632 }
633
634 tree.Put(4, 2)
635 if actualValue, expectedValue := tree.Height(), 2; actualValue != expectedValue {
636 t.Errorf("Got %v expected %v", actualValue, expectedValue)
637 }
638
639 tree.Put(5, 2)
640 if actualValue, expectedValue := tree.Height(), 2; actualValue != expectedValue {
641 t.Errorf("Got %v expected %v", actualValue, expectedValue)
642 }
643
644 tree.Put(6, 2)
645 if actualValue, expectedValue := tree.Height(), 2; actualValue != expectedValue {
646 t.Errorf("Got %v expected %v", actualValue, expectedValue)
647 }
648
649 tree.Put(7, 2)
650 if actualValue, expectedValue := tree.Height(), 3; actualValue != expectedValue {
651 t.Errorf("Got %v expected %v", actualValue, expectedValue)
652 }
653
654 tree.Remove(1)
655 tree.Remove(2)
656 tree.Remove(3)
657 tree.Remove(4)
658 tree.Remove(5)
659 tree.Remove(6)
660 tree.Remove(7)
661 if actualValue, expectedValue := tree.Height(), 0; actualValue != expectedValue {
662 t.Errorf("Got %v expected %v", actualValue, expectedValue)
663 }
664 }
665
666 func TestBTreeLeftAndRight(t *testing.T) {
667 tree := New[int, string](3)
668
669 if actualValue := tree.Left(); actualValue != nil {
670 t.Errorf("Got %v expected %v", actualValue, nil)
671 }
672 if actualValue := tree.Right(); actualValue != nil {
673 t.Errorf("Got %v expected %v", actualValue, nil)
674 }
675
676 tree.Put(1, "a")
677 tree.Put(5, "e")
678 tree.Put(6, "f")
679 tree.Put(7, "g")
680 tree.Put(3, "c")
681 tree.Put(4, "d")
682 tree.Put(1, "x") // overwrite
683 tree.Put(2, "b")
684
685 if actualValue, expectedValue := tree.LeftKey(), 1; actualValue != expectedValue {
686 t.Errorf("Got %v expected %v", actualValue, expectedValue)
687 }
688 if actualValue, expectedValue := tree.LeftValue(), "x"; actualValue != expectedValue {
689 t.Errorf("Got %v expected %v", actualValue, expectedValue)
690 }
691
692 if actualValue, expectedValue := tree.RightKey(), 7; actualValue != expectedValue {
693 t.Errorf("Got %v expected %v", actualValue, expectedValue)
694 }
695 if actualValue, expectedValue := tree.RightValue(), "g"; actualValue != expectedValue {
696 t.Errorf("Got %v expected %v", actualValue, expectedValue)
697 }
698 }
699
700 func TestBTreeIteratorValuesAndKeys(t *testing.T) {
701 tree := New[int, string](4)
702 tree.Put(4, "d")
703 tree.Put(5, "e")
704 tree.Put(6, "f")
705 tree.Put(3, "c")
706 tree.Put(1, "a")
707 tree.Put(7, "g")
708 tree.Put(2, "b")
709 tree.Put(1, "x") // override
710 if actualValue, expectedValue := tree.Keys(), []int{1, 2, 3, 4, 5, 6, 7}; !slices.Equal(actualValue, expectedValue) {
711 t.Errorf("Got %v expected %v", actualValue, expectedValue)
712 }
713 if actualValue, expectedValue := tree.Values(), []string{"x", "b", "c", "d", "e", "f", "g"}; !slices.Equal(actualValue, expectedValue) {
714 t.Errorf("Got %v expected %v", actualValue, expectedValue)
715 }
716 if actualValue := tree.Size(); actualValue != 7 {
717 t.Errorf("Got %v expected %v", actualValue, 7)
718 }
719 }
720
721 func TestBTreeIteratorNextOnEmpty(t *testing.T) {
722 tree := New[int, string](3)
723 it := tree.Iterator()
724 for it.Next() {
725 t.Errorf("Shouldn't iterate on empty tree")
726 }
727 }
728
729 func TestBTreeIteratorPrevOnEmpty(t *testing.T) {
730 tree := New[int, string](3)
731 it := tree.Iterator()
732 for it.Prev() {
733 t.Errorf("Shouldn't iterate on empty tree")
734 }
735 }
736
737 func TestBTreeIterator1Next(t *testing.T) {
738 tree := New[int, string](3)
739 tree.Put(5, "e")
740 tree.Put(6, "f")
741 tree.Put(7, "g")
742 tree.Put(3, "c")
743 tree.Put(4, "d")
744 tree.Put(1, "x")
745 tree.Put(2, "b")
746 tree.Put(1, "a") //overwrite
747 it := tree.Iterator()
748 count := 0
749 for it.Next() {
750 count++
751 key := it.Key()
752 if actualValue, expectedValue := key, count; actualValue != expectedValue {
753 t.Errorf("Got %v expected %v", actualValue, expectedValue)
754 }
755 }
756 if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
757 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
758 }
759 }
760
761 func TestBTreeIterator1Prev(t *testing.T) {
762 tree := New[int, string](3)
763 tree.Put(5, "e")
764 tree.Put(6, "f")
765 tree.Put(7, "g")
766 tree.Put(3, "c")
767 tree.Put(4, "d")
768 tree.Put(1, "x")
769 tree.Put(2, "b")
770 tree.Put(1, "a") //overwrite
771 it := tree.Iterator()
772 for it.Next() {
773 }
774 countDown := tree.size
775 for it.Prev() {
776 key := it.Key()
777 if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
778 t.Errorf("Got %v expected %v", actualValue, expectedValue)
779 }
780 countDown--
781 }
782 if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
783 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
784 }
785 }
786
787 func TestBTreeIterator2Next(t *testing.T) {
788 tree := New[int, string](3)
789 tree.Put(3, "c")
790 tree.Put(1, "a")
791 tree.Put(2, "b")
792 it := tree.Iterator()
793 count := 0
794 for it.Next() {
795 count++
796 key := it.Key()
797 if actualValue, expectedValue := key, count; actualValue != expectedValue {
798 t.Errorf("Got %v expected %v", actualValue, expectedValue)
799 }
800 }
801 if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
802 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
803 }
804 }
805
806 func TestBTreeIterator2Prev(t *testing.T) {
807 tree := New[int, string](3)
808 tree.Put(3, "c")
809 tree.Put(1, "a")
810 tree.Put(2, "b")
811 it := tree.Iterator()
812 for it.Next() {
813 }
814 countDown := tree.size
815 for it.Prev() {
816 key := it.Key()
817 if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
818 t.Errorf("Got %v expected %v", actualValue, expectedValue)
819 }
820 countDown--
821 }
822 if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
823 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
824 }
825 }
826
827 func TestBTreeIterator3Next(t *testing.T) {
828 tree := New[int, string](3)
829 tree.Put(1, "a")
830 it := tree.Iterator()
831 count := 0
832 for it.Next() {
833 count++
834 key := it.Key()
835 if actualValue, expectedValue := key, count; actualValue != expectedValue {
836 t.Errorf("Got %v expected %v", actualValue, expectedValue)
837 }
838 }
839 if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
840 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
841 }
842 }
843
844 func TestBTreeIterator3Prev(t *testing.T) {
845 tree := New[int, string](3)
846 tree.Put(1, "a")
847 it := tree.Iterator()
848 for it.Next() {
849 }
850 countDown := tree.size
851 for it.Prev() {
852 key := it.Key()
853 if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
854 t.Errorf("Got %v expected %v", actualValue, expectedValue)
855 }
856 countDown--
857 }
858 if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
859 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
860 }
861 }
862
863 func TestBTreeIterator4Next(t *testing.T) {
864 tree := New[int, int](3)
865 tree.Put(13, 5)
866 tree.Put(8, 3)
867 tree.Put(17, 7)
868 tree.Put(1, 1)
869 tree.Put(11, 4)
870 tree.Put(15, 6)
871 tree.Put(25, 9)
872 tree.Put(6, 2)
873 tree.Put(22, 8)
874 tree.Put(27, 10)
875 it := tree.Iterator()
876 count := 0
877 for it.Next() {
878 count++
879 value := it.Value()
880 if actualValue, expectedValue := value, count; actualValue != expectedValue {
881 t.Errorf("Got %v expected %v", actualValue, expectedValue)
882 }
883 }
884 if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
885 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
886 }
887 }
888
889 func TestBTreeIterator4Prev(t *testing.T) {
890 tree := New[int, int](3)
891 tree.Put(13, 5)
892 tree.Put(8, 3)
893 tree.Put(17, 7)
894 tree.Put(1, 1)
895 tree.Put(11, 4)
896 tree.Put(15, 6)
897 tree.Put(25, 9)
898 tree.Put(6, 2)
899 tree.Put(22, 8)
900 tree.Put(27, 10)
901 it := tree.Iterator()
902 count := tree.Size()
903 for it.Next() {
904 }
905 for it.Prev() {
906 value := it.Value()
907 if actualValue, expectedValue := value, count; actualValue != expectedValue {
908 t.Errorf("Got %v expected %v", actualValue, expectedValue)
909 }
910 count--
911 }
912 if actualValue, expectedValue := count, 0; actualValue != expectedValue {
913 t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
914 }
915 }
916
917 func TestBTreeIteratorBegin(t *testing.T) {
918 tree := New[int, string](3)
919 tree.Put(3, "c")
920 tree.Put(1, "a")
921 tree.Put(2, "b")
922 it := tree.Iterator()
923
924 if it.node != nil {
925 t.Errorf("Got %v expected %v", it.node, nil)
926 }
927
928 it.Begin()
929
930 if it.node != nil {
931 t.Errorf("Got %v expected %v", it.node, nil)
932 }
933
934 for it.Next() {
935 }
936
937 it.Begin()
938
939 if it.node != nil {
940 t.Errorf("Got %v expected %v", it.node, nil)
941 }
942
943 it.Next()
944 if key, value := it.Key(), it.Value(); key != 1 || value != "a" {
945 t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")
946 }
947 }
948
949 func TestBTreeIteratorEnd(t *testing.T) {
950 tree := New[int, string](3)
951 it := tree.Iterator()
952
953 if it.node != nil {
954 t.Errorf("Got %v expected %v", it.node, nil)
955 }
956
957 it.End()
958 if it.node != nil {
959 t.Errorf("Got %v expected %v", it.node, nil)
960 }
961
962 tree.Put(3, "c")
963 tree.Put(1, "a")
964 tree.Put(2, "b")
965 it.End()
966 if it.node != nil {
967 t.Errorf("Got %v expected %v", it.node, nil)
968 }
969
970 it.Prev()
971 if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
972 t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
973 }
974 }
975
976 func TestBTreeIteratorFirst(t *testing.T) {
977 tree := New[int, string](3)
978 tree.Put(3, "c")
979 tree.Put(1, "a")
980 tree.Put(2, "b")
981 it := tree.Iterator()
982 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
983 t.Errorf("Got %v expected %v", actualValue, expectedValue)
984 }
985 if key, value := it.Key(), it.Value(); key != 1 || value != "a" {
986 t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")
987 }
988 }
989
990 func TestBTreeIteratorLast(t *testing.T) {
991 tree := New[int, string](3)
992 tree.Put(3, "c")
993 tree.Put(1, "a")
994 tree.Put(2, "b")
995 it := tree.Iterator()
996 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
997 t.Errorf("Got %v expected %v", actualValue, expectedValue)
998 }
999 if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
1000 t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
1001 }
1002 }
1003
1004 func TestBTreeSearch(t *testing.T) {
1005 {
1006 tree := New[int, int](3)
1007 tree.Root = &Node[int, int]{Entries: []*Entry[int, int]{}, Children: make([]*Node[int, int], 0)}
1008 tests := [][]interface{}{
1009 {0, 0, false},
1010 }
1011 for _, test := range tests {
1012 index, found := tree.search(tree.Root, test[0].(int))
1013 if actualValue, expectedValue := index, test[1]; actualValue != expectedValue {
1014 t.Errorf("Got %v expected %v", actualValue, expectedValue)
1015 }
1016 if actualValue, expectedValue := found, test[2]; actualValue != expectedValue {
1017 t.Errorf("Got %v expected %v", actualValue, expectedValue)
1018 }
1019 }
1020 }
1021 {
1022 tree := New[int, int](3)
1023 tree.Root = &Node[int, int]{Entries: []*Entry[int, int]{{2, 0}, {4, 1}, {6, 2}}, Children: []*Node[int, int]{}}
1024 tests := [][]interface{}{
1025 {0, 0, false},
1026 {1, 0, false},
1027 {2, 0, true},
1028 {3, 1, false},
1029 {4, 1, true},
1030 {5, 2, false},
1031 {6, 2, true},
1032 {7, 3, false},
1033 }
1034 for _, test := range tests {
1035 index, found := tree.search(tree.Root, test[0].(int))
1036 if actualValue, expectedValue := index, test[1]; actualValue != expectedValue {
1037 t.Errorf("Got %v expected %v", actualValue, expectedValue)
1038 }
1039 if actualValue, expectedValue := found, test[2]; actualValue != expectedValue {
1040 t.Errorf("Got %v expected %v", actualValue, expectedValue)
1041 }
1042 }
1043 }
1044 }
1045
1046 func assertValidTree[K comparable, V any](t *testing.T, tree *Tree[K, V], expectedSize int) {
1047 if actualValue, expectedValue := tree.size, expectedSize; actualValue != expectedValue {
1048 t.Errorf("Got %v expected %v for tree size", actualValue, expectedValue)
1049 }
1050 }
1051
1052 func assertValidTreeNode[K comparable, V any](t *testing.T, node *Node[K, V], expectedEntries int, expectedChildren int, keys []K, hasParent bool) {
1053 if actualValue, expectedValue := node.Parent != nil, hasParent; actualValue != expectedValue {
1054 t.Errorf("Got %v expected %v for hasParent", actualValue, expectedValue)
1055 }
1056 if actualValue, expectedValue := len(node.Entries), expectedEntries; actualValue != expectedValue {
1057 t.Errorf("Got %v expected %v for entries size", actualValue, expectedValue)
1058 }
1059 if actualValue, expectedValue := len(node.Children), expectedChildren; actualValue != expectedValue {
1060 t.Errorf("Got %v expected %v for children size", actualValue, expectedValue)
1061 }
1062 for i, key := range keys {
1063 if actualValue, expectedValue := node.Entries[i].Key, key; actualValue != expectedValue {
1064 t.Errorf("Got %v expected %v for key", actualValue, expectedValue)
1065 }
1066 }
1067 }
1068
1069 func TestBTreeIteratorNextTo(t *testing.T) {
1070 // Sample seek function, i.e. string starting with "b"
1071 seek := func(index int, value string) bool {
1072 return strings.HasSuffix(value, "b")
1073 }
1074
1075 // NextTo (empty)
1076 {
1077 tree := New[int, string](3)
1078 it := tree.Iterator()
1079 for it.NextTo(seek) {
1080 t.Errorf("Shouldn't iterate on empty tree")
1081 }
1082 }
1083
1084 // NextTo (not found)
1085 {
1086 tree := New[int, string](3)
1087 tree.Put(0, "xx")
1088 tree.Put(1, "yy")
1089 it := tree.Iterator()
1090 for it.NextTo(seek) {
1091 t.Errorf("Shouldn't iterate on empty tree")
1092 }
1093 }
1094
1095 // NextTo (found)
1096 {
1097 tree := New[int, string](3)
1098 tree.Put(2, "cc")
1099 tree.Put(0, "aa")
1100 tree.Put(1, "bb")
1101 it := tree.Iterator()
1102 it.Begin()
1103 if !it.NextTo(seek) {
1104 t.Errorf("Shouldn't iterate on empty tree")
1105 }
1106 if index, value := it.Key(), it.Value(); index != 1 || value != "bb" {
1107 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
1108 }
1109 if !it.Next() {
1110 t.Errorf("Should go to first element")
1111 }
1112 if index, value := it.Key(), it.Value(); index != 2 || value != "cc" {
1113 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
1114 }
1115 if it.Next() {
1116 t.Errorf("Should not go past last element")
1117 }
1118 }
1119 }
1120
1121 func TestBTreeIteratorPrevTo(t *testing.T) {
1122 // Sample seek function, i.e. string starting with "b"
1123 seek := func(index int, value string) bool {
1124 return strings.HasSuffix(value, "b")
1125 }
1126
1127 // PrevTo (empty)
1128 {
1129 tree := New[int, string](3)
1130 it := tree.Iterator()
1131 it.End()
1132 for it.PrevTo(seek) {
1133 t.Errorf("Shouldn't iterate on empty tree")
1134 }
1135 }
1136
1137 // PrevTo (not found)
1138 {
1139 tree := New[int, string](3)
1140 tree.Put(0, "xx")
1141 tree.Put(1, "yy")
1142 it := tree.Iterator()
1143 it.End()
1144 for it.PrevTo(seek) {
1145 t.Errorf("Shouldn't iterate on empty tree")
1146 }
1147 }
1148
1149 // PrevTo (found)
1150 {
1151 tree := New[int, string](3)
1152 tree.Put(2, "cc")
1153 tree.Put(0, "aa")
1154 tree.Put(1, "bb")
1155 it := tree.Iterator()
1156 it.End()
1157 if !it.PrevTo(seek) {
1158 t.Errorf("Shouldn't iterate on empty tree")
1159 }
1160 if index, value := it.Key(), it.Value(); index != 1 || value != "bb" {
1161 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
1162 }
1163 if !it.Prev() {
1164 t.Errorf("Should go to first element")
1165 }
1166 if index, value := it.Key(), it.Value(); index != 0 || value != "aa" {
1167 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
1168 }
1169 if it.Prev() {
1170 t.Errorf("Should not go before first element")
1171 }
1172 }
1173 }
1174
1175 func TestBTreeSerialization(t *testing.T) {
1176 tree := New[string, string](3)
1177 tree.Put("c", "3")
1178 tree.Put("b", "2")
1179 tree.Put("a", "1")
1180
1181 var err error
1182 assert := func() {
1183 if actualValue, expectedValue := tree.Size(), 3; actualValue != expectedValue {
1184 t.Errorf("Got %v expected %v", actualValue, expectedValue)
1185 }
1186 if actualValue, expectedValue := tree.Keys(), []string{"a", "b", "c"}; !slices.Equal(actualValue, expectedValue) {
1187 t.Errorf("Got %v expected %v", actualValue, expectedValue)
1188 }
1189 if actualValue, expectedValue := tree.Values(), []string{"1", "2", "3"}; !slices.Equal(actualValue, expectedValue) {
1190 t.Errorf("Got %v expected %v", actualValue, expectedValue)
1191 }
1192 if err != nil {
1193 t.Errorf("Got error %v", err)
1194 }
1195 }
1196
1197 assert()
1198
1199 bytes, err := tree.ToJSON()
1200 assert()
1201
1202 err = tree.FromJSON(bytes)
1203 assert()
1204
1205 bytes, err = json.Marshal([]interface{}{"a", "b", "c", tree})
1206 if err != nil {
1207 t.Errorf("Got error %v", err)
1208 }
1209
1210 intTree := New[string, int](3)
1211 err = json.Unmarshal([]byte(`{"a":1,"b":2}`), intTree)
1212 if err != nil {
1213 t.Errorf("Got error %v", err)
1214 }
1215 if actualValue, expectedValue := intTree.Size(), 2; actualValue != expectedValue {
1216 t.Errorf("Got %v expected %v", actualValue, expectedValue)
1217 }
1218 if actualValue, expectedValue := intTree.Keys(), []string{"a", "b"}; !slices.Equal(actualValue, expectedValue) {
1219 t.Errorf("Got %v expected %v", actualValue, expectedValue)
1220 }
1221 if actualValue, expectedValue := intTree.Values(), []int{1, 2}; !slices.Equal(actualValue, expectedValue) {
1222 t.Errorf("Got %v expected %v", actualValue, expectedValue)
1223 }
1224 }
1225
1226 func TestBTreeString(t *testing.T) {
1227 c := New[string, int](3)
1228 c.Put("a", 1)
1229 if !strings.HasPrefix(c.String(), "BTree") {
1230 t.Errorf("String should start with container name")
1231 }
1232 }
1233
1234 func benchmarkGet(b *testing.B, tree *Tree[int, struct{}], size int) {
1235 for i := 0; i < b.N; i++ {
1236 for n := 0; n < size; n++ {
1237 tree.Get(n)
1238 }
1239 }
1240 }
1241
1242 func benchmarkPut(b *testing.B, tree *Tree[int, struct{}], size int) {
1243 for i := 0; i < b.N; i++ {
1244 for n := 0; n < size; n++ {
1245 tree.Put(n, struct{}{})
1246 }
1247 }
1248 }
1249
1250 func benchmarkRemove(b *testing.B, tree *Tree[int, struct{}], size int) {
1251 for i := 0; i < b.N; i++ {
1252 for n := 0; n < size; n++ {
1253 tree.Remove(n)
1254 }
1255 }
1256 }
1257
1258 func BenchmarkBTreeGet100(b *testing.B) {
1259 b.StopTimer()
1260 size := 100
1261 tree := New[int, struct{}](128)
1262 for n := 0; n < size; n++ {
1263 tree.Put(n, struct{}{})
1264 }
1265 b.StartTimer()
1266 benchmarkGet(b, tree, size)
1267 }
1268
1269 func BenchmarkBTreeGet1000(b *testing.B) {
1270 b.StopTimer()
1271 size := 1000
1272 tree := New[int, struct{}](128)
1273 for n := 0; n < size; n++ {
1274 tree.Put(n, struct{}{})
1275 }
1276 b.StartTimer()
1277 benchmarkGet(b, tree, size)
1278 }
1279
1280 func BenchmarkBTreeGet10000(b *testing.B) {
1281 b.StopTimer()
1282 size := 10000
1283 tree := New[int, struct{}](128)
1284 for n := 0; n < size; n++ {
1285 tree.Put(n, struct{}{})
1286 }
1287 b.StartTimer()
1288 benchmarkGet(b, tree, size)
1289 }
1290
1291 func BenchmarkBTreeGet100000(b *testing.B) {
1292 b.StopTimer()
1293 size := 100000
1294 tree := New[int, struct{}](128)
1295 for n := 0; n < size; n++ {
1296 tree.Put(n, struct{}{})
1297 }
1298 b.StartTimer()
1299 benchmarkGet(b, tree, size)
1300 }
1301
1302 func BenchmarkBTreePut100(b *testing.B) {
1303 b.StopTimer()
1304 size := 100
1305 tree := New[int, struct{}](128)
1306 b.StartTimer()
1307 benchmarkPut(b, tree, size)
1308 }
1309
1310 func BenchmarkBTreePut1000(b *testing.B) {
1311 b.StopTimer()
1312 size := 1000
1313 tree := New[int, struct{}](128)
1314 for n := 0; n < size; n++ {
1315 tree.Put(n, struct{}{})
1316 }
1317 b.StartTimer()
1318 benchmarkPut(b, tree, size)
1319 }
1320
1321 func BenchmarkBTreePut10000(b *testing.B) {
1322 b.StopTimer()
1323 size := 10000
1324 tree := New[int, struct{}](128)
1325 for n := 0; n < size; n++ {
1326 tree.Put(n, struct{}{})
1327 }
1328 b.StartTimer()
1329 benchmarkPut(b, tree, size)
1330 }
1331
1332 func BenchmarkBTreePut100000(b *testing.B) {
1333 b.StopTimer()
1334 size := 100000
1335 tree := New[int, struct{}](128)
1336 for n := 0; n < size; n++ {
1337 tree.Put(n, struct{}{})
1338 }
1339 b.StartTimer()
1340 benchmarkPut(b, tree, size)
1341 }
1342
1343 func BenchmarkBTreeRemove100(b *testing.B) {
1344 b.StopTimer()
1345 size := 100
1346 tree := New[int, struct{}](128)
1347 for n := 0; n < size; n++ {
1348 tree.Put(n, struct{}{})
1349 }
1350 b.StartTimer()
1351 benchmarkRemove(b, tree, size)
1352 }
1353
1354 func BenchmarkBTreeRemove1000(b *testing.B) {
1355 b.StopTimer()
1356 size := 1000
1357 tree := New[int, struct{}](128)
1358 for n := 0; n < size; n++ {
1359 tree.Put(n, struct{}{})
1360 }
1361 b.StartTimer()
1362 benchmarkRemove(b, tree, size)
1363 }
1364
1365 func BenchmarkBTreeRemove10000(b *testing.B) {
1366 b.StopTimer()
1367 size := 10000
1368 tree := New[int, struct{}](128)
1369 for n := 0; n < size; n++ {
1370 tree.Put(n, struct{}{})
1371 }
1372 b.StartTimer()
1373 benchmarkRemove(b, tree, size)
1374 }
1375
1376 func BenchmarkBTreeRemove100000(b *testing.B) {
1377 b.StopTimer()
1378 size := 100000
1379 tree := New[int, struct{}](128)
1380 for n := 0; n < size; n++ {
1381 tree.Put(n, struct{}{})
1382 }
1383 b.StartTimer()
1384 benchmarkRemove(b, tree, size)
1385 }