github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/maps/treemap/treemap_test.go (about) 1 package treemap 2 3 import ( 4 "encoding/json" 5 "github.com/songzhibin97/go-baseutils/base/bcomparator" 6 "reflect" 7 "strings" 8 "testing" 9 ) 10 11 func TestMapPut(t *testing.T) { 12 m := NewWith[int, string](bcomparator.IntComparator()) 13 m.Put(5, "e") 14 m.Put(6, "f") 15 m.Put(7, "g") 16 m.Put(3, "c") 17 m.Put(4, "d") 18 m.Put(1, "x") 19 m.Put(2, "b") 20 m.Put(1, "a") //overwrite 21 22 if actualValue := m.Size(); actualValue != 7 { 23 t.Errorf("Got %v expected %v", actualValue, 7) 24 } 25 if actualValue, expectedValue := m.Keys(), []int{1, 2, 3, 4, 5, 6, 7}; !sameElements(actualValue, expectedValue) { 26 t.Errorf("Got %v expected %v", actualValue, expectedValue) 27 } 28 if actualValue, expectedValue := m.Values(), []string{"a", "b", "c", "d", "e", "f", "g"}; !sameElements(actualValue, expectedValue) { 29 t.Errorf("Got %v expected %v", actualValue, expectedValue) 30 } 31 32 // key,expectedValue,expectedFound 33 tests1 := [][]interface{}{ 34 {1, "a", true}, 35 {2, "b", true}, 36 {3, "c", true}, 37 {4, "d", true}, 38 {5, "e", true}, 39 {6, "f", true}, 40 {7, "g", true}, 41 {8, "", false}, 42 } 43 44 for _, test := range tests1 { 45 // retrievals 46 actualValue, actualFound := m.Get(test[0].(int)) 47 if actualValue != test[1] || actualFound != test[2] { 48 t.Errorf("Got %v expected %v", actualValue, test[1]) 49 } 50 } 51 } 52 53 func TestMapMin(t *testing.T) { 54 m := NewWithIntComparator[string]() 55 56 if k, v := m.Min(); k != 0 || v != "" { 57 t.Errorf("Got %v->%v expected %v->%v", k, v, nil, nil) 58 } 59 60 m.Put(5, "e") 61 m.Put(6, "f") 62 m.Put(7, "g") 63 m.Put(3, "c") 64 m.Put(4, "d") 65 m.Put(1, "x") 66 m.Put(2, "b") 67 m.Put(1, "a") //overwrite 68 69 actualKey, actualValue := m.Min() 70 expectedKey, expectedValue := 1, "a" 71 if actualKey != expectedKey { 72 t.Errorf("Got %v expected %v", actualKey, expectedKey) 73 } 74 if actualValue != expectedValue { 75 t.Errorf("Got %v expected %v", actualValue, expectedValue) 76 } 77 } 78 79 func TestMapMax(t *testing.T) { 80 m := NewWithIntComparator[string]() 81 82 if k, v := m.Max(); k != 0 || v != "" { 83 t.Errorf("Got %v->%v expected %v->%v", k, v, nil, nil) 84 } 85 86 m.Put(5, "e") 87 m.Put(6, "f") 88 m.Put(7, "g") 89 m.Put(3, "c") 90 m.Put(4, "d") 91 m.Put(1, "x") 92 m.Put(2, "b") 93 m.Put(1, "a") //overwrite 94 95 actualKey, actualValue := m.Max() 96 expectedKey, expectedValue := 7, "g" 97 if actualKey != expectedKey { 98 t.Errorf("Got %v expected %v", actualKey, expectedKey) 99 } 100 if actualValue != expectedValue { 101 t.Errorf("Got %v expected %v", actualValue, expectedValue) 102 } 103 } 104 105 func TestMapClear(t *testing.T) { 106 m := NewWithIntComparator[string]() 107 m.Put(5, "e") 108 m.Put(6, "f") 109 m.Put(7, "g") 110 m.Put(3, "c") 111 if actualValue, expectedValue := m.Size(), 4; actualValue != expectedValue { 112 t.Errorf("Got %v expected %v", actualValue, expectedValue) 113 } 114 m.Clear() 115 if actualValue, expectedValue := m.Size(), 0; actualValue != expectedValue { 116 t.Errorf("Got %v expected %v", actualValue, expectedValue) 117 } 118 } 119 120 func TestMapRemove(t *testing.T) { 121 m := NewWithIntComparator[string]() 122 m.Put(5, "e") 123 m.Put(6, "f") 124 m.Put(7, "g") 125 m.Put(3, "c") 126 m.Put(4, "d") 127 m.Put(1, "x") 128 m.Put(2, "b") 129 m.Put(1, "a") //overwrite 130 131 m.Remove(5) 132 m.Remove(6) 133 m.Remove(7) 134 m.Remove(8) 135 m.Remove(5) 136 137 if actualValue, expectedValue := m.Keys(), []int{1, 2, 3, 4}; !sameElements(actualValue, expectedValue) { 138 t.Errorf("Got %v expected %v", actualValue, expectedValue) 139 } 140 141 if actualValue, expectedValue := m.Values(), []string{"a", "b", "c", "d"}; !sameElements(actualValue, expectedValue) { 142 t.Errorf("Got %v expected %v", actualValue, expectedValue) 143 } 144 if actualValue := m.Size(); actualValue != 4 { 145 t.Errorf("Got %v expected %v", actualValue, 4) 146 } 147 148 tests2 := [][]interface{}{ 149 {1, "a", true}, 150 {2, "b", true}, 151 {3, "c", true}, 152 {4, "d", true}, 153 {5, "", false}, 154 {6, "", false}, 155 {7, "", false}, 156 {8, "", false}, 157 } 158 159 for _, test := range tests2 { 160 actualValue, actualFound := m.Get(test[0].(int)) 161 if actualValue != test[1] || actualFound != test[2] { 162 t.Errorf("Got %v expected %v", actualValue, test[1]) 163 } 164 } 165 166 m.Remove(1) 167 m.Remove(4) 168 m.Remove(2) 169 m.Remove(3) 170 m.Remove(2) 171 m.Remove(2) 172 173 if actualValue, expectedValue := m.Keys(), []int{}; !reflect.DeepEqual(actualValue, expectedValue) { 174 t.Errorf("Got %v expected %v", actualValue, expectedValue) 175 } 176 if actualValue, expectedValue := m.Values(), []string{}; !reflect.DeepEqual(actualValue, expectedValue) { 177 t.Errorf("Got %v expected %v", actualValue, expectedValue) 178 } 179 if actualValue := m.Size(); actualValue != 0 { 180 t.Errorf("Got %v expected %v", actualValue, 0) 181 } 182 if actualValue := m.Empty(); actualValue != true { 183 t.Errorf("Got %v expected %v", actualValue, true) 184 } 185 } 186 187 func TestMapFloor(t *testing.T) { 188 m := NewWithIntComparator[string]() 189 m.Put(7, "g") 190 m.Put(3, "c") 191 m.Put(1, "a") 192 193 // key,expectedKey,expectedValue,expectedFound 194 tests1 := [][]interface{}{ 195 {-1, 0, "", false}, 196 {0, 0, "", false}, 197 {1, 1, "a", true}, 198 {2, 1, "a", true}, 199 {3, 3, "c", true}, 200 {4, 3, "c", true}, 201 {7, 7, "g", true}, 202 {8, 7, "g", true}, 203 } 204 205 for _, test := range tests1 { 206 // retrievals 207 actualKey, actualValue := m.Floor(test[0].(int)) 208 actualFound := actualKey != 0 && actualValue != "" 209 if actualKey != test[1] || actualValue != test[2] || actualFound != test[3] { 210 t.Errorf("Got %v, %v, %v, expected %v, %v, %v", actualKey, actualValue, actualFound, test[1], test[2], test[3]) 211 } 212 } 213 } 214 215 func TestMapCeiling(t *testing.T) { 216 m := NewWithIntComparator[string]() 217 m.Put(7, "g") 218 m.Put(3, "c") 219 m.Put(1, "a") 220 221 // key,expectedKey,expectedValue,expectedFound 222 tests1 := [][]interface{}{ 223 {-1, 1, "a", true}, 224 {0, 1, "a", true}, 225 {1, 1, "a", true}, 226 {2, 3, "c", true}, 227 {3, 3, "c", true}, 228 {4, 7, "g", true}, 229 {7, 7, "g", true}, 230 {8, 0, "", false}, 231 } 232 233 for _, test := range tests1 { 234 // retrievals 235 actualKey, actualValue := m.Ceiling(test[0].(int)) 236 actualFound := actualKey != 0 && actualValue != "" 237 if actualKey != test[1] || actualValue != test[2] || actualFound != test[3] { 238 t.Errorf("Got %v, %v, %v, expected %v, %v, %v", actualKey, actualValue, actualFound, test[1], test[2], test[3]) 239 } 240 } 241 } 242 243 func sameElements[E any](a []E, b []E) bool { 244 if len(a) != len(b) { 245 return false 246 } 247 for _, av := range a { 248 found := false 249 for _, bv := range b { 250 if reflect.DeepEqual(av, bv) { 251 found = true 252 break 253 } 254 } 255 if !found { 256 return false 257 } 258 } 259 return true 260 } 261 262 func TestMapEach(t *testing.T) { 263 m := NewWithStringComparator[int]() 264 m.Put("c", 3) 265 m.Put("a", 1) 266 m.Put("b", 2) 267 count := 0 268 m.Each(func(key string, value int) { 269 count++ 270 if actualValue, expectedValue := count, value; actualValue != expectedValue { 271 t.Errorf("Got %v expected %v", actualValue, expectedValue) 272 } 273 switch value { 274 case 1: 275 if actualValue, expectedValue := key, "a"; actualValue != expectedValue { 276 t.Errorf("Got %v expected %v", actualValue, expectedValue) 277 } 278 case 2: 279 if actualValue, expectedValue := key, "b"; actualValue != expectedValue { 280 t.Errorf("Got %v expected %v", actualValue, expectedValue) 281 } 282 case 3: 283 if actualValue, expectedValue := key, "c"; actualValue != expectedValue { 284 t.Errorf("Got %v expected %v", actualValue, expectedValue) 285 } 286 default: 287 t.Errorf("Too many") 288 } 289 }) 290 } 291 292 func TestMapMap(t *testing.T) { 293 m := NewWithStringComparator[int]() 294 m.Put("c", 3) 295 m.Put("a", 1) 296 m.Put("b", 2) 297 mappedMap := m.Map(func(key1 string, value1 int) (key2 string, value2 int) { 298 return key1, value1 * value1 299 }) 300 if actualValue, _ := mappedMap.Get("a"); actualValue != 1 { 301 t.Errorf("Got %v expected %v", actualValue, "mapped: a") 302 } 303 if actualValue, _ := mappedMap.Get("b"); actualValue != 4 { 304 t.Errorf("Got %v expected %v", actualValue, "mapped: b") 305 } 306 if actualValue, _ := mappedMap.Get("c"); actualValue != 9 { 307 t.Errorf("Got %v expected %v", actualValue, "mapped: c") 308 } 309 if mappedMap.Size() != 3 { 310 t.Errorf("Got %v expected %v", mappedMap.Size(), 3) 311 } 312 } 313 314 func TestMapSelect(t *testing.T) { 315 m := NewWithStringComparator[int]() 316 m.Put("c", 3) 317 m.Put("a", 1) 318 m.Put("b", 2) 319 selectedMap := m.Select(func(key string, value int) bool { 320 return key >= "a" && key <= "b" 321 }) 322 if actualValue, _ := selectedMap.Get("a"); actualValue != 1 { 323 t.Errorf("Got %v expected %v", actualValue, "value: a") 324 } 325 if actualValue, _ := selectedMap.Get("b"); actualValue != 2 { 326 t.Errorf("Got %v expected %v", actualValue, "value: b") 327 } 328 if selectedMap.Size() != 2 { 329 t.Errorf("Got %v expected %v", selectedMap.Size(), 2) 330 } 331 } 332 333 func TestMapAny(t *testing.T) { 334 m := NewWithStringComparator[int]() 335 m.Put("c", 3) 336 m.Put("a", 1) 337 m.Put("b", 2) 338 any := m.Any(func(key string, value int) bool { 339 return value == 3 340 }) 341 if any != true { 342 t.Errorf("Got %v expected %v", any, true) 343 } 344 any = m.Any(func(key string, value int) bool { 345 return value == 4 346 }) 347 if any != false { 348 t.Errorf("Got %v expected %v", any, false) 349 } 350 } 351 352 func TestMapAll(t *testing.T) { 353 m := NewWithStringComparator[int]() 354 m.Put("c", 3) 355 m.Put("a", 1) 356 m.Put("b", 2) 357 all := m.All(func(key string, value int) bool { 358 return key >= "a" && key <= "c" 359 }) 360 if all != true { 361 t.Errorf("Got %v expected %v", all, true) 362 } 363 all = m.All(func(key string, value int) bool { 364 return key >= "a" && key <= "b" 365 }) 366 if all != false { 367 t.Errorf("Got %v expected %v", all, false) 368 } 369 } 370 371 func TestMapFind(t *testing.T) { 372 m := NewWithStringComparator[int]() 373 m.Put("c", 3) 374 m.Put("a", 1) 375 m.Put("b", 2) 376 foundKey, foundValue := m.Find(func(key string, value int) bool { 377 return key == "c" 378 }) 379 if foundKey != "c" || foundValue != 3 { 380 t.Errorf("Got %v -> %v expected %v -> %v", foundKey, foundValue, "c", 3) 381 } 382 foundKey, foundValue = m.Find(func(key string, value int) bool { 383 return key == "x" 384 }) 385 if foundKey != "" || foundValue != 0 { 386 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundKey, nil, nil) 387 } 388 } 389 390 func TestMapChaining(t *testing.T) { 391 m := NewWithStringComparator[int]() 392 m.Put("c", 3) 393 m.Put("a", 1) 394 m.Put("b", 2) 395 chainedMap := m.Select(func(key string, value int) bool { 396 return value > 1 397 }).Map(func(key string, value int) (string, int) { 398 return key + key, value * value 399 }) 400 if actualValue := chainedMap.Size(); actualValue != 2 { 401 t.Errorf("Got %v expected %v", actualValue, 2) 402 } 403 if actualValue, found := chainedMap.Get("aa"); actualValue != 0 || found { 404 t.Errorf("Got %v expected %v", actualValue, nil) 405 } 406 if actualValue, found := chainedMap.Get("bb"); actualValue != 4 || !found { 407 t.Errorf("Got %v expected %v", actualValue, 4) 408 } 409 if actualValue, found := chainedMap.Get("cc"); actualValue != 9 || !found { 410 t.Errorf("Got %v expected %v", actualValue, 9) 411 } 412 } 413 414 func TestMapIteratorNextOnEmpty(t *testing.T) { 415 m := NewWithStringComparator[int]() 416 it := m.Iterator() 417 it = m.Iterator() 418 for it.Next() { 419 t.Errorf("Shouldn't iterate on empty map") 420 } 421 } 422 423 func TestMapIteratorPrevOnEmpty(t *testing.T) { 424 m := NewWithStringComparator[int]() 425 it := m.Iterator() 426 it = m.Iterator() 427 for it.Prev() { 428 t.Errorf("Shouldn't iterate on empty map") 429 } 430 } 431 432 func TestMapIteratorNext(t *testing.T) { 433 m := NewWithStringComparator[int]() 434 m.Put("c", 3) 435 m.Put("a", 1) 436 m.Put("b", 2) 437 438 it := m.Iterator() 439 count := 0 440 for it.Next() { 441 count++ 442 key := it.Key() 443 value := it.Value() 444 switch key { 445 case "a": 446 if actualValue, expectedValue := value, 1; actualValue != expectedValue { 447 t.Errorf("Got %v expected %v", actualValue, expectedValue) 448 } 449 case "b": 450 if actualValue, expectedValue := value, 2; actualValue != expectedValue { 451 t.Errorf("Got %v expected %v", actualValue, expectedValue) 452 } 453 case "c": 454 if actualValue, expectedValue := value, 3; actualValue != expectedValue { 455 t.Errorf("Got %v expected %v", actualValue, expectedValue) 456 } 457 default: 458 t.Errorf("Too many") 459 } 460 if actualValue, expectedValue := value, count; actualValue != expectedValue { 461 t.Errorf("Got %v expected %v", actualValue, expectedValue) 462 } 463 } 464 if actualValue, expectedValue := count, 3; actualValue != expectedValue { 465 t.Errorf("Got %v expected %v", actualValue, expectedValue) 466 } 467 } 468 469 func TestMapIteratorPrev(t *testing.T) { 470 m := NewWithStringComparator[int]() 471 m.Put("c", 3) 472 m.Put("a", 1) 473 m.Put("b", 2) 474 475 it := m.Iterator() 476 for it.Next() { 477 } 478 countDown := m.Size() 479 for it.Prev() { 480 key := it.Key() 481 value := it.Value() 482 switch key { 483 case "a": 484 if actualValue, expectedValue := value, 1; actualValue != expectedValue { 485 t.Errorf("Got %v expected %v", actualValue, expectedValue) 486 } 487 case "b": 488 if actualValue, expectedValue := value, 2; actualValue != expectedValue { 489 t.Errorf("Got %v expected %v", actualValue, expectedValue) 490 } 491 case "c": 492 if actualValue, expectedValue := value, 3; actualValue != expectedValue { 493 t.Errorf("Got %v expected %v", actualValue, expectedValue) 494 } 495 default: 496 t.Errorf("Too many") 497 } 498 if actualValue, expectedValue := value, countDown; actualValue != expectedValue { 499 t.Errorf("Got %v expected %v", actualValue, expectedValue) 500 } 501 countDown-- 502 } 503 if actualValue, expectedValue := countDown, 0; actualValue != expectedValue { 504 t.Errorf("Got %v expected %v", actualValue, expectedValue) 505 } 506 } 507 508 func TestMapIteratorBegin(t *testing.T) { 509 m := NewWithIntComparator[string]() 510 it := m.Iterator() 511 it.Begin() 512 m.Put(3, "c") 513 m.Put(1, "a") 514 m.Put(2, "b") 515 for it.Next() { 516 } 517 it.Begin() 518 it.Next() 519 if key, value := it.Key(), it.Value(); key != 1 || value != "a" { 520 t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") 521 } 522 } 523 524 func TestMapIteratorEnd(t *testing.T) { 525 m := NewWithIntComparator[string]() 526 it := m.Iterator() 527 m.Put(3, "c") 528 m.Put(1, "a") 529 m.Put(2, "b") 530 it.End() 531 it.Prev() 532 if key, value := it.Key(), it.Value(); key != 3 || value != "c" { 533 t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") 534 } 535 } 536 537 func TestMapIteratorFirst(t *testing.T) { 538 m := NewWithIntComparator[string]() 539 m.Put(3, "c") 540 m.Put(1, "a") 541 m.Put(2, "b") 542 it := m.Iterator() 543 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { 544 t.Errorf("Got %v expected %v", actualValue, expectedValue) 545 } 546 if key, value := it.Key(), it.Value(); key != 1 || value != "a" { 547 t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") 548 } 549 } 550 551 func TestMapIteratorLast(t *testing.T) { 552 m := NewWithIntComparator[string]() 553 m.Put(3, "c") 554 m.Put(1, "a") 555 m.Put(2, "b") 556 it := m.Iterator() 557 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { 558 t.Errorf("Got %v expected %v", actualValue, expectedValue) 559 } 560 if key, value := it.Key(), it.Value(); key != 3 || value != "c" { 561 t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") 562 } 563 } 564 565 func TestMapIteratorNextTo(t *testing.T) { 566 // Sample seek function, i.e. string starting with "b" 567 seek := func(index int, value string) bool { 568 return strings.HasSuffix(value, "b") 569 } 570 571 // NextTo (empty) 572 { 573 m := NewWithIntComparator[string]() 574 it := m.Iterator() 575 for it.NextTo(seek) { 576 t.Errorf("Shouldn't iterate on empty map") 577 } 578 } 579 580 // NextTo (not found) 581 { 582 m := NewWithIntComparator[string]() 583 m.Put(0, "xx") 584 m.Put(1, "yy") 585 it := m.Iterator() 586 for it.NextTo(seek) { 587 t.Errorf("Shouldn't iterate on empty map") 588 } 589 } 590 591 // NextTo (found) 592 { 593 m := NewWithIntComparator[string]() 594 m.Put(0, "aa") 595 m.Put(1, "bb") 596 m.Put(2, "cc") 597 it := m.Iterator() 598 it.Begin() 599 if !it.NextTo(seek) { 600 t.Errorf("Shouldn't iterate on empty map") 601 } 602 if index, value := it.Key(), it.Value(); index != 1 || value != "bb" { 603 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") 604 } 605 if !it.Next() { 606 t.Errorf("Should go to first element") 607 } 608 if index, value := it.Key(), it.Value(); index != 2 || value != "cc" { 609 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc") 610 } 611 if it.Next() { 612 t.Errorf("Should not go past last element") 613 } 614 } 615 } 616 617 func TestMapIteratorPrevTo(t *testing.T) { 618 // Sample seek function, i.e. string starting with "b" 619 seek := func(index int, value string) bool { 620 return strings.HasSuffix(value, "b") 621 } 622 623 // PrevTo (empty) 624 { 625 m := NewWithIntComparator[string]() 626 it := m.Iterator() 627 it.End() 628 for it.PrevTo(seek) { 629 t.Errorf("Shouldn't iterate on empty map") 630 } 631 } 632 633 // PrevTo (not found) 634 { 635 m := NewWithIntComparator[string]() 636 m.Put(0, "xx") 637 m.Put(1, "yy") 638 it := m.Iterator() 639 it.End() 640 for it.PrevTo(seek) { 641 t.Errorf("Shouldn't iterate on empty map") 642 } 643 } 644 645 // PrevTo (found) 646 { 647 m := NewWithIntComparator[string]() 648 m.Put(0, "aa") 649 m.Put(1, "bb") 650 m.Put(2, "cc") 651 it := m.Iterator() 652 it.End() 653 if !it.PrevTo(seek) { 654 t.Errorf("Shouldn't iterate on empty map") 655 } 656 if index, value := it.Key(), it.Value(); index != 1 || value != "bb" { 657 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") 658 } 659 if !it.Prev() { 660 t.Errorf("Should go to first element") 661 } 662 if index, value := it.Key(), it.Value(); index != 0 || value != "aa" { 663 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa") 664 } 665 if it.Prev() { 666 t.Errorf("Should not go before first element") 667 } 668 } 669 } 670 671 func TestMapSerialization(t *testing.T) { 672 for i := 0; i < 10; i++ { 673 original := NewWithStringComparator[string]() 674 original.Put("d", "4") 675 original.Put("e", "5") 676 original.Put("c", "3") 677 original.Put("b", "2") 678 original.Put("a", "1") 679 680 assertSerialization(original, "A", t) 681 682 serialized, err := original.MarshalJSON() 683 if err != nil { 684 t.Errorf("Got error %v", err) 685 } 686 assertSerialization(original, "B", t) 687 688 deserialized := NewWithStringComparator[string]() 689 err = deserialized.UnmarshalJSON(serialized) 690 if err != nil { 691 t.Errorf("Got error %v", err) 692 } 693 assertSerialization(deserialized, "C", t) 694 } 695 696 m := NewWithStringComparator[float64]() 697 m.Put("a", 1.0) 698 m.Put("b", 2.0) 699 m.Put("c", 3.0) 700 _, err := json.Marshal([]interface{}{"a", "b", "c", m}) 701 if err != nil { 702 t.Errorf("Got error %v", err) 703 } 704 705 err = json.Unmarshal([]byte(`{"a":1,"b":2}`), &m) 706 if err != nil { 707 t.Errorf("Got error %v", err) 708 } 709 710 } 711 712 func TestMapString(t *testing.T) { 713 c := NewWithStringComparator[int]() 714 c.Put("a", 1) 715 if !strings.HasPrefix(c.String(), "TreeMap") { 716 t.Errorf("String should start with container name") 717 } 718 } 719 720 // noinspection GoBoolExpressions 721 func assertSerialization(m *Map[string, string], txt string, t *testing.T) { 722 if actualValue := m.Keys(); false || 723 actualValue[0] != "a" || 724 actualValue[1] != "b" || 725 actualValue[2] != "c" || 726 actualValue[3] != "d" || 727 actualValue[4] != "e" { 728 t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[a,b,c,d,e]") 729 } 730 if actualValue := m.Values(); false || 731 actualValue[0] != "1" || 732 actualValue[1] != "2" || 733 actualValue[2] != "3" || 734 actualValue[3] != "4" || 735 actualValue[4] != "5" { 736 t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[1,2,3,4,5]") 737 } 738 if actualValue, expectedValue := m.Size(), 5; actualValue != expectedValue { 739 t.Errorf("[%s] Got %v expected %v", txt, actualValue, expectedValue) 740 } 741 } 742 743 func benchmarkGet[K int, V struct{}](b *testing.B, m *Map[K, V], size int) { 744 for i := 0; i < b.N; i++ { 745 for n := 0; n < size; n++ { 746 m.Get(K(n)) 747 } 748 } 749 } 750 751 func benchmarkPut[K int, V struct{}](b *testing.B, m *Map[K, V], size int) { 752 for i := 0; i < b.N; i++ { 753 for n := 0; n < size; n++ { 754 m.Put(K(n), struct{}{}) 755 } 756 } 757 } 758 759 func benchmarkRemove[K int, V struct{}](b *testing.B, m *Map[K, V], size int) { 760 for i := 0; i < b.N; i++ { 761 for n := 0; n < size; n++ { 762 m.Remove(K(n)) 763 } 764 } 765 } 766 767 func BenchmarkTreeMapGet100(b *testing.B) { 768 b.StopTimer() 769 size := 100 770 m := NewWithIntComparator[struct{}]() 771 for n := 0; n < size; n++ { 772 m.Put(n, struct{}{}) 773 } 774 b.StartTimer() 775 benchmarkGet(b, m, size) 776 } 777 778 func BenchmarkTreeMapGet1000(b *testing.B) { 779 b.StopTimer() 780 size := 1000 781 m := NewWithIntComparator[struct{}]() 782 for n := 0; n < size; n++ { 783 m.Put(n, struct{}{}) 784 } 785 b.StartTimer() 786 benchmarkGet(b, m, size) 787 } 788 789 func BenchmarkTreeMapGet10000(b *testing.B) { 790 b.StopTimer() 791 size := 10000 792 m := NewWithIntComparator[struct{}]() 793 for n := 0; n < size; n++ { 794 m.Put(n, struct{}{}) 795 } 796 b.StartTimer() 797 benchmarkGet(b, m, size) 798 } 799 800 func BenchmarkTreeMapGet100000(b *testing.B) { 801 b.StopTimer() 802 size := 100000 803 m := NewWithIntComparator[struct{}]() 804 for n := 0; n < size; n++ { 805 m.Put(n, struct{}{}) 806 } 807 b.StartTimer() 808 benchmarkGet(b, m, size) 809 } 810 811 func BenchmarkTreeMapPut100(b *testing.B) { 812 b.StopTimer() 813 size := 100 814 m := NewWithIntComparator[struct{}]() 815 b.StartTimer() 816 benchmarkPut(b, m, size) 817 } 818 819 func BenchmarkTreeMapPut1000(b *testing.B) { 820 b.StopTimer() 821 size := 1000 822 m := NewWithIntComparator[struct{}]() 823 for n := 0; n < size; n++ { 824 m.Put(n, struct{}{}) 825 } 826 b.StartTimer() 827 benchmarkPut(b, m, size) 828 } 829 830 func BenchmarkTreeMapPut10000(b *testing.B) { 831 b.StopTimer() 832 size := 10000 833 m := NewWithIntComparator[struct{}]() 834 for n := 0; n < size; n++ { 835 m.Put(n, struct{}{}) 836 } 837 b.StartTimer() 838 benchmarkPut(b, m, size) 839 } 840 841 func BenchmarkTreeMapPut100000(b *testing.B) { 842 b.StopTimer() 843 size := 100000 844 m := NewWithIntComparator[struct{}]() 845 for n := 0; n < size; n++ { 846 m.Put(n, struct{}{}) 847 } 848 b.StartTimer() 849 benchmarkPut(b, m, size) 850 } 851 852 func BenchmarkTreeMapRemove100(b *testing.B) { 853 b.StopTimer() 854 size := 100 855 m := NewWithIntComparator[struct{}]() 856 for n := 0; n < size; n++ { 857 m.Put(n, struct{}{}) 858 } 859 b.StartTimer() 860 benchmarkRemove(b, m, size) 861 } 862 863 func BenchmarkTreeMapRemove1000(b *testing.B) { 864 b.StopTimer() 865 size := 1000 866 m := NewWithIntComparator[struct{}]() 867 for n := 0; n < size; n++ { 868 m.Put(n, struct{}{}) 869 } 870 b.StartTimer() 871 benchmarkRemove(b, m, size) 872 } 873 874 func BenchmarkTreeMapRemove10000(b *testing.B) { 875 b.StopTimer() 876 size := 10000 877 m := NewWithIntComparator[struct{}]() 878 for n := 0; n < size; n++ { 879 m.Put(n, struct{}{}) 880 } 881 b.StartTimer() 882 benchmarkRemove(b, m, size) 883 } 884 885 func BenchmarkTreeMapRemove100000(b *testing.B) { 886 b.StopTimer() 887 size := 100000 888 m := NewWithIntComparator[struct{}]() 889 for n := 0; n < size; n++ { 890 m.Put(n, struct{}{}) 891 } 892 b.StartTimer() 893 benchmarkRemove(b, m, size) 894 }