github.com/alexandrestein/gods@v1.0.1/maps/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 "github.com/alexandrestein/gods/utils" 10 "testing" 11 ) 12 13 func TestMapPut(t *testing.T) { 14 m := NewWith(utils.IntComparator, utils.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(utils.IntComparator, utils.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(utils.IntComparator, utils.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(utils.StringComparator, utils.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(utils.StringComparator, utils.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(utils.StringComparator, utils.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(utils.StringComparator, utils.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(utils.StringComparator, utils.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(utils.StringComparator, utils.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(utils.StringComparator, utils.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(utils.StringComparator, utils.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(utils.StringComparator, utils.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(utils.IntComparator, utils.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(utils.IntComparator, utils.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(utils.IntComparator, utils.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(utils.IntComparator, utils.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 m := NewWithStringComparators() 478 m.Put("a", "1") 479 m.Put("b", "2") 480 m.Put("c", "3") 481 482 var err error 483 assert := func() { 484 if actualValue := m.Keys(); actualValue[0].(string) != "a" || actualValue[1].(string) != "b" || actualValue[2].(string) != "c" { 485 t.Errorf("Got %v expected %v", actualValue, "[a,b,c]") 486 } 487 if actualValue := m.Values(); actualValue[0].(string) != "1" || actualValue[1].(string) != "2" || actualValue[2].(string) != "3" { 488 t.Errorf("Got %v expected %v", actualValue, "[1,2,3]") 489 } 490 if actualValue, expectedValue := m.Size(), 3; actualValue != expectedValue { 491 t.Errorf("Got %v expected %v", actualValue, expectedValue) 492 } 493 if err != nil { 494 t.Errorf("Got error %v", err) 495 } 496 } 497 498 assert() 499 500 json, err := m.ToJSON() 501 assert() 502 503 err = m.FromJSON(json) 504 assert() 505 } 506 507 func benchmarkGet(b *testing.B, m *Map, size int) { 508 for i := 0; i < b.N; i++ { 509 for n := 0; n < size; n++ { 510 m.Get(n) 511 } 512 } 513 } 514 515 func benchmarkPut(b *testing.B, m *Map, size int) { 516 for i := 0; i < b.N; i++ { 517 for n := 0; n < size; n++ { 518 m.Put(n, n) 519 } 520 } 521 } 522 523 func benchmarkRemove(b *testing.B, m *Map, size int) { 524 for i := 0; i < b.N; i++ { 525 for n := 0; n < size; n++ { 526 m.Remove(n) 527 } 528 } 529 } 530 531 func BenchmarkTreeBidiMapGet100(b *testing.B) { 532 b.StopTimer() 533 size := 100 534 m := NewWithIntComparators() 535 for n := 0; n < size; n++ { 536 m.Put(n, n) 537 } 538 b.StartTimer() 539 benchmarkGet(b, m, size) 540 } 541 542 func BenchmarkTreeBidiMapGet1000(b *testing.B) { 543 b.StopTimer() 544 size := 1000 545 m := NewWithIntComparators() 546 for n := 0; n < size; n++ { 547 m.Put(n, n) 548 } 549 b.StartTimer() 550 benchmarkGet(b, m, size) 551 } 552 553 func BenchmarkTreeBidiMapGet10000(b *testing.B) { 554 b.StopTimer() 555 size := 10000 556 m := NewWithIntComparators() 557 for n := 0; n < size; n++ { 558 m.Put(n, n) 559 } 560 b.StartTimer() 561 benchmarkGet(b, m, size) 562 } 563 564 func BenchmarkTreeBidiMapGet100000(b *testing.B) { 565 b.StopTimer() 566 size := 100000 567 m := NewWithIntComparators() 568 for n := 0; n < size; n++ { 569 m.Put(n, n) 570 } 571 b.StartTimer() 572 benchmarkGet(b, m, size) 573 } 574 575 func BenchmarkTreeBidiMapPut100(b *testing.B) { 576 b.StopTimer() 577 size := 100 578 m := NewWithIntComparators() 579 b.StartTimer() 580 benchmarkPut(b, m, size) 581 } 582 583 func BenchmarkTreeBidiMapPut1000(b *testing.B) { 584 b.StopTimer() 585 size := 1000 586 m := NewWithIntComparators() 587 for n := 0; n < size; n++ { 588 m.Put(n, n) 589 } 590 b.StartTimer() 591 benchmarkPut(b, m, size) 592 } 593 594 func BenchmarkTreeBidiMapPut10000(b *testing.B) { 595 b.StopTimer() 596 size := 10000 597 m := NewWithIntComparators() 598 for n := 0; n < size; n++ { 599 m.Put(n, n) 600 } 601 b.StartTimer() 602 benchmarkPut(b, m, size) 603 } 604 605 func BenchmarkTreeBidiMapPut100000(b *testing.B) { 606 b.StopTimer() 607 size := 100000 608 m := NewWithIntComparators() 609 for n := 0; n < size; n++ { 610 m.Put(n, n) 611 } 612 b.StartTimer() 613 benchmarkPut(b, m, size) 614 } 615 616 func BenchmarkTreeBidiMapRemove100(b *testing.B) { 617 b.StopTimer() 618 size := 100 619 m := NewWithIntComparators() 620 for n := 0; n < size; n++ { 621 m.Put(n, n) 622 } 623 b.StartTimer() 624 benchmarkRemove(b, m, size) 625 } 626 627 func BenchmarkTreeBidiMapRemove1000(b *testing.B) { 628 b.StopTimer() 629 size := 1000 630 m := NewWithIntComparators() 631 for n := 0; n < size; n++ { 632 m.Put(n, n) 633 } 634 b.StartTimer() 635 benchmarkRemove(b, m, size) 636 } 637 638 func BenchmarkTreeBidiMapRemove10000(b *testing.B) { 639 b.StopTimer() 640 size := 10000 641 m := NewWithIntComparators() 642 for n := 0; n < size; n++ { 643 m.Put(n, n) 644 } 645 b.StartTimer() 646 benchmarkRemove(b, m, size) 647 } 648 649 func BenchmarkTreeBidiMapRemove100000(b *testing.B) { 650 b.StopTimer() 651 size := 100000 652 m := NewWithIntComparators() 653 for n := 0; n < size; n++ { 654 m.Put(n, n) 655 } 656 b.StartTimer() 657 benchmarkRemove(b, m, size) 658 }