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