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