gitee.com/quant1x/gox@v1.21.2/util/treemap/treemap_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 treemap 6 7 import ( 8 "fmt" 9 "testing" 10 ) 11 12 func TestMapPut(t *testing.T) { 13 m := NewWithIntComparator() 14 m.Put(5, "e") 15 m.Put(6, "f") 16 m.Put(7, "g") 17 m.Put(3, "c") 18 m.Put(4, "d") 19 m.Put(1, "x") 20 m.Put(2, "b") 21 m.Put(1, "a") //overwrite 22 23 if actualValue := m.Size(); actualValue != 7 { 24 t.Errorf("Got %v expected %v", actualValue, 7) 25 } 26 if actualValue, expectedValue := m.Keys(), []interface{}{1, 2, 3, 4, 5, 6, 7}; !sameElements(actualValue, expectedValue) { 27 t.Errorf("Got %v expected %v", actualValue, expectedValue) 28 } 29 if actualValue, expectedValue := m.Values(), []interface{}{"a", "b", "c", "d", "e", "f", "g"}; !sameElements(actualValue, expectedValue) { 30 t.Errorf("Got %v expected %v", actualValue, expectedValue) 31 } 32 33 // key,expectedValue,expectedFound 34 tests1 := [][]interface{}{ 35 {1, "a", true}, 36 {2, "b", true}, 37 {3, "c", true}, 38 {4, "d", true}, 39 {5, "e", true}, 40 {6, "f", true}, 41 {7, "g", true}, 42 {8, nil, false}, 43 } 44 45 for _, test := range tests1 { 46 // retrievals 47 actualValue, actualFound := m.Get(test[0]) 48 if actualValue != test[1] || actualFound != test[2] { 49 t.Errorf("Got %v expected %v", actualValue, test[1]) 50 } 51 } 52 } 53 54 func TestMapRemove(t *testing.T) { 55 m := NewWithIntComparator() 56 m.Put(5, "e") 57 m.Put(6, "f") 58 m.Put(7, "g") 59 m.Put(3, "c") 60 m.Put(4, "d") 61 m.Put(1, "x") 62 m.Put(2, "b") 63 m.Put(1, "a") //overwrite 64 65 m.Remove(5) 66 m.Remove(6) 67 m.Remove(7) 68 m.Remove(8) 69 m.Remove(5) 70 71 if actualValue, expectedValue := m.Keys(), []interface{}{1, 2, 3, 4}; !sameElements(actualValue, expectedValue) { 72 t.Errorf("Got %v expected %v", actualValue, expectedValue) 73 } 74 75 if actualValue, expectedValue := m.Values(), []interface{}{"a", "b", "c", "d"}; !sameElements(actualValue, expectedValue) { 76 t.Errorf("Got %v expected %v", actualValue, expectedValue) 77 } 78 if actualValue := m.Size(); actualValue != 4 { 79 t.Errorf("Got %v expected %v", actualValue, 4) 80 } 81 82 tests2 := [][]interface{}{ 83 {1, "a", true}, 84 {2, "b", true}, 85 {3, "c", true}, 86 {4, "d", true}, 87 {5, nil, false}, 88 {6, nil, false}, 89 {7, nil, false}, 90 {8, nil, false}, 91 } 92 93 for _, test := range tests2 { 94 actualValue, actualFound := m.Get(test[0]) 95 if actualValue != test[1] || actualFound != test[2] { 96 t.Errorf("Got %v expected %v", actualValue, test[1]) 97 } 98 } 99 100 m.Remove(1) 101 m.Remove(4) 102 m.Remove(2) 103 m.Remove(3) 104 m.Remove(2) 105 m.Remove(2) 106 107 if actualValue, expectedValue := fmt.Sprintf("%s", m.Keys()), "[]"; actualValue != expectedValue { 108 t.Errorf("Got %v expected %v", actualValue, expectedValue) 109 } 110 if actualValue, expectedValue := fmt.Sprintf("%s", m.Values()), "[]"; actualValue != expectedValue { 111 t.Errorf("Got %v expected %v", actualValue, expectedValue) 112 } 113 if actualValue := m.Size(); actualValue != 0 { 114 t.Errorf("Got %v expected %v", actualValue, 0) 115 } 116 if actualValue := m.Empty(); actualValue != true { 117 t.Errorf("Got %v expected %v", actualValue, true) 118 } 119 } 120 121 func TestMapFloor(t *testing.T) { 122 m := NewWithIntComparator() 123 m.Put(7, "g") 124 m.Put(3, "c") 125 m.Put(1, "a") 126 127 // key,expectedKey,expectedValue,expectedFound 128 tests1 := [][]interface{}{ 129 {-1, nil, nil, false}, 130 {0, nil, nil, false}, 131 {1, 1, "a", true}, 132 {2, 1, "a", true}, 133 {3, 3, "c", true}, 134 {4, 3, "c", true}, 135 {7, 7, "g", true}, 136 {8, 7, "g", true}, 137 } 138 139 for _, test := range tests1 { 140 // retrievals 141 actualKey, actualValue := m.Floor(test[0]) 142 actualFound := actualKey != nil && actualValue != nil 143 if actualKey != test[1] || actualValue != test[2] || actualFound != test[3] { 144 t.Errorf("Got %v, %v, %v, expected %v, %v, %v", actualKey, actualValue, actualFound, test[1], test[2], test[3]) 145 } 146 } 147 } 148 149 func TestMapCeiling(t *testing.T) { 150 m := NewWithIntComparator() 151 m.Put(7, "g") 152 m.Put(3, "c") 153 m.Put(1, "a") 154 155 // key,expectedKey,expectedValue,expectedFound 156 tests1 := [][]interface{}{ 157 {-1, 1, "a", true}, 158 {0, 1, "a", true}, 159 {1, 1, "a", true}, 160 {2, 3, "c", true}, 161 {3, 3, "c", true}, 162 {4, 7, "g", true}, 163 {7, 7, "g", true}, 164 {8, nil, nil, false}, 165 } 166 167 for _, test := range tests1 { 168 // retrievals 169 actualKey, actualValue := m.Ceiling(test[0]) 170 actualFound := actualKey != nil && actualValue != nil 171 if actualKey != test[1] || actualValue != test[2] || actualFound != test[3] { 172 t.Errorf("Got %v, %v, %v, expected %v, %v, %v", actualKey, actualValue, actualFound, test[1], test[2], test[3]) 173 } 174 } 175 } 176 177 func sameElements(a []interface{}, b []interface{}) bool { 178 if len(a) != len(b) { 179 return false 180 } 181 for _, av := range a { 182 found := false 183 for _, bv := range b { 184 if av == bv { 185 found = true 186 break 187 } 188 } 189 if !found { 190 return false 191 } 192 } 193 return true 194 } 195 196 func TestMapEach(t *testing.T) { 197 m := NewWithStringComparator() 198 m.Put("c", 3) 199 m.Put("a", 1) 200 m.Put("b", 2) 201 count := 0 202 m.Each(func(key interface{}, value interface{}) { 203 count++ 204 if actualValue, expectedValue := count, value; actualValue != expectedValue { 205 t.Errorf("Got %v expected %v", actualValue, expectedValue) 206 } 207 switch value { 208 case 1: 209 if actualValue, expectedValue := key, "a"; actualValue != expectedValue { 210 t.Errorf("Got %v expected %v", actualValue, expectedValue) 211 } 212 case 2: 213 if actualValue, expectedValue := key, "b"; actualValue != expectedValue { 214 t.Errorf("Got %v expected %v", actualValue, expectedValue) 215 } 216 case 3: 217 if actualValue, expectedValue := key, "c"; actualValue != expectedValue { 218 t.Errorf("Got %v expected %v", actualValue, expectedValue) 219 } 220 default: 221 t.Errorf("Too many") 222 } 223 }) 224 } 225 226 func TestMapMap(t *testing.T) { 227 m := NewWithStringComparator() 228 m.Put("c", 3) 229 m.Put("a", 1) 230 m.Put("b", 2) 231 mappedMap := m.Map(func(key1 interface{}, value1 interface{}) (key2 interface{}, value2 interface{}) { 232 return key1, value1.(int) * value1.(int) 233 }) 234 if actualValue, _ := mappedMap.Get("a"); actualValue != 1 { 235 t.Errorf("Got %v expected %v", actualValue, "mapped: a") 236 } 237 if actualValue, _ := mappedMap.Get("b"); actualValue != 4 { 238 t.Errorf("Got %v expected %v", actualValue, "mapped: b") 239 } 240 if actualValue, _ := mappedMap.Get("c"); actualValue != 9 { 241 t.Errorf("Got %v expected %v", actualValue, "mapped: c") 242 } 243 if mappedMap.Size() != 3 { 244 t.Errorf("Got %v expected %v", mappedMap.Size(), 3) 245 } 246 } 247 248 func TestMapSelect(t *testing.T) { 249 m := NewWithStringComparator() 250 m.Put("c", 3) 251 m.Put("a", 1) 252 m.Put("b", 2) 253 selectedMap := m.Select(func(key interface{}, value interface{}) bool { 254 return key.(string) >= "a" && key.(string) <= "b" 255 }) 256 if actualValue, _ := selectedMap.Get("a"); actualValue != 1 { 257 t.Errorf("Got %v expected %v", actualValue, "value: a") 258 } 259 if actualValue, _ := selectedMap.Get("b"); actualValue != 2 { 260 t.Errorf("Got %v expected %v", actualValue, "value: b") 261 } 262 if selectedMap.Size() != 2 { 263 t.Errorf("Got %v expected %v", selectedMap.Size(), 2) 264 } 265 } 266 267 func TestMapAny(t *testing.T) { 268 m := NewWithStringComparator() 269 m.Put("c", 3) 270 m.Put("a", 1) 271 m.Put("b", 2) 272 any := m.Any(func(key interface{}, value interface{}) bool { 273 return value.(int) == 3 274 }) 275 if any != true { 276 t.Errorf("Got %v expected %v", any, true) 277 } 278 any = m.Any(func(key interface{}, value interface{}) bool { 279 return value.(int) == 4 280 }) 281 if any != false { 282 t.Errorf("Got %v expected %v", any, false) 283 } 284 } 285 286 func TestMapAll(t *testing.T) { 287 m := NewWithStringComparator() 288 m.Put("c", 3) 289 m.Put("a", 1) 290 m.Put("b", 2) 291 all := m.All(func(key interface{}, value interface{}) bool { 292 return key.(string) >= "a" && key.(string) <= "c" 293 }) 294 if all != true { 295 t.Errorf("Got %v expected %v", all, true) 296 } 297 all = m.All(func(key interface{}, value interface{}) bool { 298 return key.(string) >= "a" && key.(string) <= "b" 299 }) 300 if all != false { 301 t.Errorf("Got %v expected %v", all, false) 302 } 303 } 304 305 func TestMapFind(t *testing.T) { 306 m := NewWithStringComparator() 307 m.Put("c", 3) 308 m.Put("a", 1) 309 m.Put("b", 2) 310 foundKey, foundValue := m.Find(func(key interface{}, value interface{}) bool { 311 return key.(string) == "c" 312 }) 313 if foundKey != "c" || foundValue != 3 { 314 t.Errorf("Got %v -> %v expected %v -> %v", foundKey, foundValue, "c", 3) 315 } 316 foundKey, foundValue = m.Find(func(key interface{}, value interface{}) bool { 317 return key.(string) == "x" 318 }) 319 if foundKey != nil || foundValue != nil { 320 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundKey, nil, nil) 321 } 322 } 323 324 func TestMapChaining(t *testing.T) { 325 m := NewWithStringComparator() 326 m.Put("c", 3) 327 m.Put("a", 1) 328 m.Put("b", 2) 329 chainedMap := m.Select(func(key interface{}, value interface{}) bool { 330 return value.(int) > 1 331 }).Map(func(key interface{}, value interface{}) (interface{}, interface{}) { 332 return key.(string) + key.(string), value.(int) * value.(int) 333 }) 334 if actualValue := chainedMap.Size(); actualValue != 2 { 335 t.Errorf("Got %v expected %v", actualValue, 2) 336 } 337 if actualValue, found := chainedMap.Get("aa"); actualValue != nil || found { 338 t.Errorf("Got %v expected %v", actualValue, nil) 339 } 340 if actualValue, found := chainedMap.Get("bb"); actualValue != 4 || !found { 341 t.Errorf("Got %v expected %v", actualValue, 4) 342 } 343 if actualValue, found := chainedMap.Get("cc"); actualValue != 9 || !found { 344 t.Errorf("Got %v expected %v", actualValue, 9) 345 } 346 } 347 348 func TestMapIteratorNextOnEmpty(t *testing.T) { 349 m := NewWithStringComparator() 350 it := m.Iterator() 351 it = m.Iterator() 352 for it.Next() { 353 t.Errorf("Shouldn't iterate on empty map") 354 } 355 } 356 357 func TestMapIteratorPrevOnEmpty(t *testing.T) { 358 m := NewWithStringComparator() 359 it := m.Iterator() 360 it = m.Iterator() 361 for it.Prev() { 362 t.Errorf("Shouldn't iterate on empty map") 363 } 364 } 365 366 func TestMapIteratorNext(t *testing.T) { 367 m := NewWithStringComparator() 368 m.Put("c", 3) 369 m.Put("a", 1) 370 m.Put("b", 2) 371 372 it := m.Iterator() 373 count := 0 374 for it.Next() { 375 count++ 376 key := it.Key() 377 value := it.Value() 378 switch key { 379 case "a": 380 if actualValue, expectedValue := value, 1; actualValue != expectedValue { 381 t.Errorf("Got %v expected %v", actualValue, expectedValue) 382 } 383 case "b": 384 if actualValue, expectedValue := value, 2; actualValue != expectedValue { 385 t.Errorf("Got %v expected %v", actualValue, expectedValue) 386 } 387 case "c": 388 if actualValue, expectedValue := value, 3; actualValue != expectedValue { 389 t.Errorf("Got %v expected %v", actualValue, expectedValue) 390 } 391 default: 392 t.Errorf("Too many") 393 } 394 if actualValue, expectedValue := value, count; actualValue != expectedValue { 395 t.Errorf("Got %v expected %v", actualValue, expectedValue) 396 } 397 } 398 if actualValue, expectedValue := count, 3; actualValue != expectedValue { 399 t.Errorf("Got %v expected %v", actualValue, expectedValue) 400 } 401 } 402 403 func TestMapIteratorPrev(t *testing.T) { 404 m := NewWithStringComparator() 405 m.Put("c", 3) 406 m.Put("a", 1) 407 m.Put("b", 2) 408 409 it := m.Iterator() 410 for it.Next() { 411 } 412 countDown := m.Size() 413 for it.Prev() { 414 key := it.Key() 415 value := it.Value() 416 switch key { 417 case "a": 418 if actualValue, expectedValue := value, 1; actualValue != expectedValue { 419 t.Errorf("Got %v expected %v", actualValue, expectedValue) 420 } 421 case "b": 422 if actualValue, expectedValue := value, 2; actualValue != expectedValue { 423 t.Errorf("Got %v expected %v", actualValue, expectedValue) 424 } 425 case "c": 426 if actualValue, expectedValue := value, 3; actualValue != expectedValue { 427 t.Errorf("Got %v expected %v", actualValue, expectedValue) 428 } 429 default: 430 t.Errorf("Too many") 431 } 432 if actualValue, expectedValue := value, countDown; actualValue != expectedValue { 433 t.Errorf("Got %v expected %v", actualValue, expectedValue) 434 } 435 countDown-- 436 } 437 if actualValue, expectedValue := countDown, 0; actualValue != expectedValue { 438 t.Errorf("Got %v expected %v", actualValue, expectedValue) 439 } 440 } 441 442 func TestMapIteratorBegin(t *testing.T) { 443 m := NewWithIntComparator() 444 it := m.Iterator() 445 it.Begin() 446 m.Put(3, "c") 447 m.Put(1, "a") 448 m.Put(2, "b") 449 for it.Next() { 450 } 451 it.Begin() 452 it.Next() 453 if key, value := it.Key(), it.Value(); key != 1 || value != "a" { 454 t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") 455 } 456 } 457 458 func TestMapTreeIteratorEnd(t *testing.T) { 459 m := NewWithIntComparator() 460 it := m.Iterator() 461 m.Put(3, "c") 462 m.Put(1, "a") 463 m.Put(2, "b") 464 it.End() 465 it.Prev() 466 if key, value := it.Key(), it.Value(); key != 3 || value != "c" { 467 t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") 468 } 469 } 470 471 func TestMapIteratorFirst(t *testing.T) { 472 m := NewWithIntComparator() 473 m.Put(3, "c") 474 m.Put(1, "a") 475 m.Put(2, "b") 476 it := m.Iterator() 477 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { 478 t.Errorf("Got %v expected %v", actualValue, expectedValue) 479 } 480 if key, value := it.Key(), it.Value(); key != 1 || value != "a" { 481 t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") 482 } 483 } 484 485 func TestMapIteratorLast(t *testing.T) { 486 m := NewWithIntComparator() 487 m.Put(3, "c") 488 m.Put(1, "a") 489 m.Put(2, "b") 490 it := m.Iterator() 491 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { 492 t.Errorf("Got %v expected %v", actualValue, expectedValue) 493 } 494 if key, value := it.Key(), it.Value(); key != 3 || value != "c" { 495 t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c") 496 } 497 } 498 499 func TestMapSerialization(t *testing.T) { 500 for i := 0; i < 10; i++ { 501 original := NewWithStringComparator() 502 original.Put("d", "4") 503 original.Put("e", "5") 504 original.Put("c", "3") 505 original.Put("b", "2") 506 original.Put("a", "1") 507 508 assertSerialization(original, "A", t) 509 510 serialized, err := original.ToJSON() 511 if err != nil { 512 t.Errorf("Got error %v", err) 513 } 514 assertSerialization(original, "B", t) 515 516 deserialized := NewWithStringComparator() 517 err = deserialized.FromJSON(serialized) 518 if err != nil { 519 t.Errorf("Got error %v", err) 520 } 521 assertSerialization(deserialized, "C", t) 522 } 523 } 524 525 // noinspection GoBoolExpressions 526 func assertSerialization(m *Map, txt string, t *testing.T) { 527 if actualValue := m.Keys(); false || 528 actualValue[0].(string) != "a" || 529 actualValue[1].(string) != "b" || 530 actualValue[2].(string) != "c" || 531 actualValue[3].(string) != "d" || 532 actualValue[4].(string) != "e" { 533 t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[a,b,c,d,e]") 534 } 535 if actualValue := m.Values(); false || 536 actualValue[0].(string) != "1" || 537 actualValue[1].(string) != "2" || 538 actualValue[2].(string) != "3" || 539 actualValue[3].(string) != "4" || 540 actualValue[4].(string) != "5" { 541 t.Errorf("[%s] Got %v expected %v", txt, actualValue, "[1,2,3,4,5]") 542 } 543 if actualValue, expectedValue := m.Size(), 5; actualValue != expectedValue { 544 t.Errorf("[%s] Got %v expected %v", txt, actualValue, expectedValue) 545 } 546 } 547 548 func benchmarkGet(b *testing.B, m *Map, size int) { 549 for i := 0; i < b.N; i++ { 550 for n := 0; n < size; n++ { 551 m.Get(n) 552 } 553 } 554 } 555 556 func benchmarkPut(b *testing.B, m *Map, size int) { 557 for i := 0; i < b.N; i++ { 558 for n := 0; n < size; n++ { 559 m.Put(n, struct{}{}) 560 } 561 } 562 } 563 564 func benchmarkRemove(b *testing.B, m *Map, size int) { 565 for i := 0; i < b.N; i++ { 566 for n := 0; n < size; n++ { 567 m.Remove(n) 568 } 569 } 570 } 571 572 func BenchmarkTreeMapGet100(b *testing.B) { 573 b.StopTimer() 574 size := 100 575 m := NewWithIntComparator() 576 for n := 0; n < size; n++ { 577 m.Put(n, struct{}{}) 578 } 579 b.StartTimer() 580 benchmarkGet(b, m, size) 581 } 582 583 func BenchmarkTreeMapGet1000(b *testing.B) { 584 b.StopTimer() 585 size := 1000 586 m := NewWithIntComparator() 587 for n := 0; n < size; n++ { 588 m.Put(n, struct{}{}) 589 } 590 b.StartTimer() 591 benchmarkGet(b, m, size) 592 } 593 594 func BenchmarkTreeMapGet10000(b *testing.B) { 595 b.StopTimer() 596 size := 10000 597 m := NewWithIntComparator() 598 for n := 0; n < size; n++ { 599 m.Put(n, struct{}{}) 600 } 601 b.StartTimer() 602 benchmarkGet(b, m, size) 603 } 604 605 func BenchmarkTreeMapGet100000(b *testing.B) { 606 b.StopTimer() 607 size := 100000 608 m := NewWithIntComparator() 609 for n := 0; n < size; n++ { 610 m.Put(n, struct{}{}) 611 } 612 b.StartTimer() 613 benchmarkGet(b, m, size) 614 } 615 616 func BenchmarkTreeMapPut100(b *testing.B) { 617 b.StopTimer() 618 size := 100 619 m := NewWithIntComparator() 620 b.StartTimer() 621 benchmarkPut(b, m, size) 622 } 623 624 func BenchmarkTreeMapPut1000(b *testing.B) { 625 b.StopTimer() 626 size := 1000 627 m := NewWithIntComparator() 628 for n := 0; n < size; n++ { 629 m.Put(n, struct{}{}) 630 } 631 b.StartTimer() 632 benchmarkPut(b, m, size) 633 } 634 635 func BenchmarkTreeMapPut10000(b *testing.B) { 636 b.StopTimer() 637 size := 10000 638 m := NewWithIntComparator() 639 for n := 0; n < size; n++ { 640 m.Put(n, struct{}{}) 641 } 642 b.StartTimer() 643 benchmarkPut(b, m, size) 644 } 645 646 func BenchmarkTreeMapPut100000(b *testing.B) { 647 b.StopTimer() 648 size := 100000 649 m := NewWithIntComparator() 650 for n := 0; n < size; n++ { 651 m.Put(n, struct{}{}) 652 } 653 b.StartTimer() 654 benchmarkPut(b, m, size) 655 } 656 657 func BenchmarkTreeMapRemove100(b *testing.B) { 658 b.StopTimer() 659 size := 100 660 m := NewWithIntComparator() 661 for n := 0; n < size; n++ { 662 m.Put(n, struct{}{}) 663 } 664 b.StartTimer() 665 benchmarkRemove(b, m, size) 666 } 667 668 func BenchmarkTreeMapRemove1000(b *testing.B) { 669 b.StopTimer() 670 size := 1000 671 m := NewWithIntComparator() 672 for n := 0; n < size; n++ { 673 m.Put(n, struct{}{}) 674 } 675 b.StartTimer() 676 benchmarkRemove(b, m, size) 677 } 678 679 func BenchmarkTreeMapRemove10000(b *testing.B) { 680 b.StopTimer() 681 size := 10000 682 m := NewWithIntComparator() 683 for n := 0; n < size; n++ { 684 m.Put(n, struct{}{}) 685 } 686 b.StartTimer() 687 benchmarkRemove(b, m, size) 688 } 689 690 func BenchmarkTreeMapRemove100000(b *testing.B) { 691 b.StopTimer() 692 size := 100000 693 m := NewWithIntComparator() 694 for n := 0; n < size; n++ { 695 m.Put(n, struct{}{}) 696 } 697 b.StartTimer() 698 benchmarkRemove(b, m, size) 699 }