github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/sets/treeset/treeset_test.go (about) 1 package treeset 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "reflect" 7 "strings" 8 "testing" 9 ) 10 11 func TestSetNew(t *testing.T) { 12 set := NewWithIntComparator(2, 1) 13 if actualValue := set.Size(); actualValue != 2 { 14 t.Errorf("Got %v expected %v", actualValue, 2) 15 } 16 values := set.Values() 17 if actualValue := values[0]; actualValue != 1 { 18 t.Errorf("Got %v expected %v", actualValue, 1) 19 } 20 if actualValue := values[1]; actualValue != 2 { 21 t.Errorf("Got %v expected %v", actualValue, 2) 22 } 23 } 24 25 func TestSetAdd(t *testing.T) { 26 set := NewWithIntComparator() 27 set.Add() 28 set.Add(1) 29 set.Add(2) 30 set.Add(2, 3) 31 set.Add() 32 if actualValue := set.Empty(); actualValue != false { 33 t.Errorf("Got %v expected %v", actualValue, false) 34 } 35 if actualValue := set.Size(); actualValue != 3 { 36 t.Errorf("Got %v expected %v", actualValue, 3) 37 } 38 if actualValue, expectedValue := set.Values(), []int{1, 2, 3}; !reflect.DeepEqual(actualValue, expectedValue) { 39 t.Errorf("Got %v expected %v", actualValue, expectedValue) 40 } 41 } 42 43 func TestSetContains(t *testing.T) { 44 set := NewWithIntComparator() 45 set.Add(3, 1, 2) 46 if actualValue := set.Contains(); actualValue != true { 47 t.Errorf("Got %v expected %v", actualValue, true) 48 } 49 if actualValue := set.Contains(1); actualValue != true { 50 t.Errorf("Got %v expected %v", actualValue, true) 51 } 52 if actualValue := set.Contains(1, 2, 3); actualValue != true { 53 t.Errorf("Got %v expected %v", actualValue, true) 54 } 55 if actualValue := set.Contains(1, 2, 3, 4); actualValue != false { 56 t.Errorf("Got %v expected %v", actualValue, false) 57 } 58 } 59 60 func TestSetRemove(t *testing.T) { 61 set := NewWithIntComparator() 62 set.Add(3, 1, 2) 63 set.Remove() 64 if actualValue := set.Size(); actualValue != 3 { 65 t.Errorf("Got %v expected %v", actualValue, 3) 66 } 67 set.Remove(1) 68 if actualValue := set.Size(); actualValue != 2 { 69 t.Errorf("Got %v expected %v", actualValue, 2) 70 } 71 set.Remove(3) 72 set.Remove(3) 73 set.Remove() 74 set.Remove(2) 75 if actualValue := set.Size(); actualValue != 0 { 76 t.Errorf("Got %v expected %v", actualValue, 0) 77 } 78 } 79 80 func TestSetEach(t *testing.T) { 81 set := NewWithStringComparator() 82 set.Add("c", "a", "b") 83 set.Each(func(index int, value string) { 84 switch index { 85 case 0: 86 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 87 t.Errorf("Got %v expected %v", actualValue, expectedValue) 88 } 89 case 1: 90 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 91 t.Errorf("Got %v expected %v", actualValue, expectedValue) 92 } 93 case 2: 94 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 95 t.Errorf("Got %v expected %v", actualValue, expectedValue) 96 } 97 default: 98 t.Errorf("Too many") 99 } 100 }) 101 } 102 103 func TestSetMap(t *testing.T) { 104 set := NewWithStringComparator() 105 set.Add("c", "a", "b") 106 mappedSet := set.Map(func(index int, value string) string { 107 return "mapped: " + value 108 }) 109 if actualValue, expectedValue := mappedSet.Contains("mapped: a", "mapped: b", "mapped: c"), true; actualValue != expectedValue { 110 t.Errorf("Got %v expected %v", actualValue, expectedValue) 111 } 112 if actualValue, expectedValue := mappedSet.Contains("mapped: a", "mapped: b", "mapped: x"), false; actualValue != expectedValue { 113 t.Errorf("Got %v expected %v", actualValue, expectedValue) 114 } 115 if mappedSet.Size() != 3 { 116 t.Errorf("Got %v expected %v", mappedSet.Size(), 3) 117 } 118 } 119 120 func TestSetSelect(t *testing.T) { 121 set := NewWithStringComparator() 122 set.Add("c", "a", "b") 123 selectedSet := set.Select(func(index int, value string) bool { 124 return value >= "a" && value <= "b" 125 }) 126 if actualValue, expectedValue := selectedSet.Contains("a", "b"), true; actualValue != expectedValue { 127 fmt.Println("A: ", selectedSet.Contains("b")) 128 t.Errorf("Got %v (%v) expected %v (%v)", actualValue, selectedSet.Values(), expectedValue, "[a b]") 129 } 130 if actualValue, expectedValue := selectedSet.Contains("a", "b", "c"), false; actualValue != expectedValue { 131 t.Errorf("Got %v (%v) expected %v (%v)", actualValue, selectedSet.Values(), expectedValue, "[a b]") 132 } 133 if selectedSet.Size() != 2 { 134 t.Errorf("Got %v expected %v", selectedSet.Size(), 3) 135 } 136 } 137 138 func TestSetAny(t *testing.T) { 139 set := NewWithStringComparator() 140 set.Add("c", "a", "b") 141 any := set.Any(func(index int, value string) bool { 142 return value == "c" 143 }) 144 if any != true { 145 t.Errorf("Got %v expected %v", any, true) 146 } 147 any = set.Any(func(index int, value string) bool { 148 return value == "x" 149 }) 150 if any != false { 151 t.Errorf("Got %v expected %v", any, false) 152 } 153 } 154 155 func TestSetAll(t *testing.T) { 156 set := NewWithStringComparator() 157 set.Add("c", "a", "b") 158 all := set.All(func(index int, value string) bool { 159 return value >= "a" && value <= "c" 160 }) 161 if all != true { 162 t.Errorf("Got %v expected %v", all, true) 163 } 164 all = set.All(func(index int, value string) bool { 165 return value >= "a" && value <= "b" 166 }) 167 if all != false { 168 t.Errorf("Got %v expected %v", all, false) 169 } 170 } 171 172 func TestSetFind(t *testing.T) { 173 set := NewWithStringComparator() 174 set.Add("c", "a", "b") 175 foundIndex, foundValue := set.Find(func(index int, value string) bool { 176 return value == "c" 177 }) 178 if foundValue != "c" || foundIndex != 2 { 179 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, "c", 2) 180 } 181 foundIndex, foundValue = set.Find(func(index int, value string) bool { 182 return value == "x" 183 }) 184 if foundValue != "" || foundIndex != -1 { 185 t.Errorf("Got %v at %v expected %v at %v", foundValue, foundIndex, nil, nil) 186 } 187 } 188 189 func TestSetChaining(t *testing.T) { 190 set := NewWithStringComparator() 191 set.Add("c", "a", "b") 192 } 193 194 func TestSetIteratorNextOnEmpty(t *testing.T) { 195 set := NewWithStringComparator() 196 it := set.Iterator() 197 for it.Next() { 198 t.Errorf("Shouldn't iterate on empty set") 199 } 200 } 201 202 func TestSetIteratorPrevOnEmpty(t *testing.T) { 203 set := NewWithStringComparator() 204 it := set.Iterator() 205 for it.Prev() { 206 t.Errorf("Shouldn't iterate on empty set") 207 } 208 } 209 210 func TestSetIteratorNext(t *testing.T) { 211 set := NewWithStringComparator() 212 set.Add("c", "a", "b") 213 it := set.Iterator() 214 count := 0 215 for it.Next() { 216 count++ 217 index := it.Index() 218 value := it.Value() 219 switch index { 220 case 0: 221 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 222 t.Errorf("Got %v expected %v", actualValue, expectedValue) 223 } 224 case 1: 225 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 226 t.Errorf("Got %v expected %v", actualValue, expectedValue) 227 } 228 case 2: 229 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 230 t.Errorf("Got %v expected %v", actualValue, expectedValue) 231 } 232 default: 233 t.Errorf("Too many") 234 } 235 if actualValue, expectedValue := index, count-1; actualValue != expectedValue { 236 t.Errorf("Got %v expected %v", actualValue, expectedValue) 237 } 238 } 239 if actualValue, expectedValue := count, 3; actualValue != expectedValue { 240 t.Errorf("Got %v expected %v", actualValue, expectedValue) 241 } 242 } 243 244 func TestSetIteratorPrev(t *testing.T) { 245 set := NewWithStringComparator() 246 set.Add("c", "a", "b") 247 it := set.Iterator() 248 for it.Prev() { 249 } 250 count := 0 251 for it.Next() { 252 count++ 253 index := it.Index() 254 value := it.Value() 255 switch index { 256 case 0: 257 if actualValue, expectedValue := value, "a"; actualValue != expectedValue { 258 t.Errorf("Got %v expected %v", actualValue, expectedValue) 259 } 260 case 1: 261 if actualValue, expectedValue := value, "b"; actualValue != expectedValue { 262 t.Errorf("Got %v expected %v", actualValue, expectedValue) 263 } 264 case 2: 265 if actualValue, expectedValue := value, "c"; actualValue != expectedValue { 266 t.Errorf("Got %v expected %v", actualValue, expectedValue) 267 } 268 default: 269 t.Errorf("Too many") 270 } 271 if actualValue, expectedValue := index, count-1; actualValue != expectedValue { 272 t.Errorf("Got %v expected %v", actualValue, expectedValue) 273 } 274 } 275 if actualValue, expectedValue := count, 3; actualValue != expectedValue { 276 t.Errorf("Got %v expected %v", actualValue, expectedValue) 277 } 278 } 279 280 func TestSetIteratorBegin(t *testing.T) { 281 set := NewWithStringComparator() 282 it := set.Iterator() 283 it.Begin() 284 set.Add("a", "b", "c") 285 for it.Next() { 286 } 287 it.Begin() 288 it.Next() 289 if index, value := it.Index(), it.Value(); index != 0 || value != "a" { 290 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") 291 } 292 } 293 294 func TestSetIteratorEnd(t *testing.T) { 295 set := NewWithStringComparator() 296 it := set.Iterator() 297 298 if index := it.Index(); index != -1 { 299 t.Errorf("Got %v expected %v", index, -1) 300 } 301 302 it.End() 303 if index := it.Index(); index != 0 { 304 t.Errorf("Got %v expected %v", index, 0) 305 } 306 307 set.Add("a", "b", "c") 308 it.End() 309 if index := it.Index(); index != set.Size() { 310 t.Errorf("Got %v expected %v", index, set.Size()) 311 } 312 313 it.Prev() 314 if index, value := it.Index(), it.Value(); index != set.Size()-1 || value != "c" { 315 t.Errorf("Got %v,%v expected %v,%v", index, value, set.Size()-1, "c") 316 } 317 } 318 319 func TestSetIteratorFirst(t *testing.T) { 320 set := NewWithStringComparator() 321 set.Add("a", "b", "c") 322 it := set.Iterator() 323 if actualValue, expectedValue := it.First(), true; actualValue != expectedValue { 324 t.Errorf("Got %v expected %v", actualValue, expectedValue) 325 } 326 if index, value := it.Index(), it.Value(); index != 0 || value != "a" { 327 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") 328 } 329 } 330 331 func TestSetIteratorLast(t *testing.T) { 332 set := NewWithStringComparator() 333 set.Add("a", "b", "c") 334 it := set.Iterator() 335 if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue { 336 t.Errorf("Got %v expected %v", actualValue, expectedValue) 337 } 338 if index, value := it.Index(), it.Value(); index != 2 || value != "c" { 339 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "c") 340 } 341 } 342 343 func TestSetIteratorNextTo(t *testing.T) { 344 // Sample seek function, i.e. string starting with "b" 345 seek := func(index int, value string) bool { 346 return strings.HasSuffix(value, "b") 347 } 348 349 // NextTo (empty) 350 { 351 set := NewWithStringComparator() 352 it := set.Iterator() 353 for it.NextTo(seek) { 354 t.Errorf("Shouldn't iterate on empty set") 355 } 356 } 357 358 // NextTo (not found) 359 { 360 set := NewWithStringComparator() 361 set.Add("xx", "yy") 362 it := set.Iterator() 363 for it.NextTo(seek) { 364 t.Errorf("Shouldn't iterate on empty set") 365 } 366 } 367 368 // NextTo (found) 369 { 370 set := NewWithStringComparator() 371 set.Add("aa", "bb", "cc") 372 it := set.Iterator() 373 it.Begin() 374 if !it.NextTo(seek) { 375 t.Errorf("Shouldn't iterate on empty set") 376 } 377 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" { 378 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") 379 } 380 if !it.Next() { 381 t.Errorf("Should go to first element") 382 } 383 if index, value := it.Index(), it.Value(); index != 2 || value != "cc" { 384 t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc") 385 } 386 if it.Next() { 387 t.Errorf("Should not go past last element") 388 } 389 } 390 } 391 392 func TestSetIteratorPrevTo(t *testing.T) { 393 // Sample seek function, i.e. string starting with "b" 394 seek := func(index int, value string) bool { 395 return strings.HasSuffix(value, "b") 396 } 397 398 // PrevTo (empty) 399 { 400 set := NewWithStringComparator() 401 it := set.Iterator() 402 it.End() 403 for it.PrevTo(seek) { 404 t.Errorf("Shouldn't iterate on empty set") 405 } 406 } 407 408 // PrevTo (not found) 409 { 410 set := NewWithStringComparator() 411 set.Add("xx", "yy") 412 it := set.Iterator() 413 it.End() 414 for it.PrevTo(seek) { 415 t.Errorf("Shouldn't iterate on empty set") 416 } 417 } 418 419 // PrevTo (found) 420 { 421 set := NewWithStringComparator() 422 set.Add("aa", "bb", "cc") 423 it := set.Iterator() 424 it.End() 425 if !it.PrevTo(seek) { 426 t.Errorf("Shouldn't iterate on empty set") 427 } 428 if index, value := it.Index(), it.Value(); index != 1 || value != "bb" { 429 t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb") 430 } 431 if !it.Prev() { 432 t.Errorf("Should go to first element") 433 } 434 if index, value := it.Index(), it.Value(); index != 0 || value != "aa" { 435 t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa") 436 } 437 if it.Prev() { 438 t.Errorf("Should not go before first element") 439 } 440 } 441 } 442 443 func TestSetSerialization(t *testing.T) { 444 set := NewWithStringComparator() 445 set.Add("a", "b", "c") 446 447 var err error 448 assert := func() { 449 if actualValue, expectedValue := set.Size(), 3; actualValue != expectedValue { 450 t.Errorf("Got %v expected %v", actualValue, expectedValue) 451 } 452 if actualValue := set.Contains("a", "b", "c"); actualValue != true { 453 t.Errorf("Got %v expected %v", actualValue, true) 454 } 455 if err != nil { 456 t.Errorf("Got error %v", err) 457 } 458 } 459 460 assert() 461 462 bytes, err := set.MarshalJSON() 463 assert() 464 465 err = set.UnmarshalJSON(bytes) 466 assert() 467 468 bytes, err = json.Marshal([]interface{}{"a", "b", "c", set}) 469 if err != nil { 470 t.Errorf("Got error %v", err) 471 } 472 473 err = json.Unmarshal([]byte(`["1","2","3"]`), &set) 474 if err != nil { 475 t.Errorf("Got error %v", err) 476 } 477 } 478 479 func TestSetString(t *testing.T) { 480 c := NewWithIntComparator() 481 c.Add(1) 482 if !strings.HasPrefix(c.String(), "TreeSet") { 483 t.Errorf("String should start with container name") 484 } 485 } 486 487 func TestSetIntersection(t *testing.T) { 488 489 set := NewWithStringComparator() 490 another := NewWithStringComparator() 491 492 intersection := set.Intersection(another) 493 if actualValue, expectedValue := intersection.Size(), 0; actualValue != expectedValue { 494 t.Errorf("Got %v expected %v", actualValue, expectedValue) 495 } 496 497 set.Add("a", "b", "c", "d") 498 another.Add("c", "d", "e", "f") 499 500 intersection = set.Intersection(another) 501 502 if actualValue, expectedValue := intersection.Size(), 2; actualValue != expectedValue { 503 t.Errorf("Got %v expected %v", actualValue, expectedValue) 504 } 505 if actualValue := intersection.Contains("c", "d"); actualValue != true { 506 t.Errorf("Got %v expected %v", actualValue, true) 507 } 508 } 509 510 func TestSetUnion(t *testing.T) { 511 512 set := NewWithStringComparator() 513 another := NewWithStringComparator() 514 515 union := set.Union(another) 516 if actualValue, expectedValue := union.Size(), 0; actualValue != expectedValue { 517 t.Errorf("Got %v expected %v", actualValue, expectedValue) 518 } 519 520 set.Add("a", "b", "c", "d") 521 another.Add("c", "d", "e", "f") 522 523 union = set.Union(another) 524 525 if actualValue, expectedValue := union.Size(), 6; actualValue != expectedValue { 526 t.Errorf("Got %v expected %v", actualValue, expectedValue) 527 } 528 if actualValue := union.Contains("a", "b", "c", "d", "e", "f"); actualValue != true { 529 t.Errorf("Got %v expected %v", actualValue, true) 530 } 531 } 532 533 func TestSetDifference(t *testing.T) { 534 535 set := NewWithStringComparator() 536 another := NewWithStringComparator() 537 538 difference := set.Difference(another) 539 if actualValue, expectedValue := difference.Size(), 0; actualValue != expectedValue { 540 t.Errorf("Got %v expected %v", actualValue, expectedValue) 541 } 542 543 set.Add("a", "b", "c", "d") 544 another.Add("c", "d", "e", "f") 545 546 difference = set.Difference(another) 547 548 if actualValue, expectedValue := difference.Size(), 2; actualValue != expectedValue { 549 t.Errorf("Got %v expected %v", actualValue, expectedValue) 550 } 551 if actualValue := difference.Contains("a", "b"); actualValue != true { 552 t.Errorf("Got %v expected %v", actualValue, true) 553 } 554 } 555 556 func benchmarkContains[E int](b *testing.B, set *Set[E], size int) { 557 for i := 0; i < b.N; i++ { 558 for n := 0; n < size; n++ { 559 set.Contains(E(n)) 560 } 561 } 562 } 563 564 func benchmarkAdd[E int](b *testing.B, set *Set[E], size int) { 565 for i := 0; i < b.N; i++ { 566 for n := 0; n < size; n++ { 567 set.Add(E(n)) 568 } 569 } 570 } 571 572 func benchmarkRemove[E int](b *testing.B, set *Set[E], size int) { 573 for i := 0; i < b.N; i++ { 574 for n := 0; n < size; n++ { 575 set.Remove(E(n)) 576 } 577 } 578 } 579 580 func BenchmarkTreeSetContains100(b *testing.B) { 581 b.StopTimer() 582 size := 100 583 set := NewWithIntComparator() 584 for n := 0; n < size; n++ { 585 set.Add(n) 586 } 587 b.StartTimer() 588 benchmarkContains(b, set, size) 589 } 590 591 func BenchmarkTreeSetContains1000(b *testing.B) { 592 b.StopTimer() 593 size := 1000 594 set := NewWithIntComparator() 595 for n := 0; n < size; n++ { 596 set.Add(n) 597 } 598 b.StartTimer() 599 benchmarkContains(b, set, size) 600 } 601 602 func BenchmarkTreeSetContains10000(b *testing.B) { 603 b.StopTimer() 604 size := 10000 605 set := NewWithIntComparator() 606 for n := 0; n < size; n++ { 607 set.Add(n) 608 } 609 b.StartTimer() 610 benchmarkContains(b, set, size) 611 } 612 613 func BenchmarkTreeSetContains100000(b *testing.B) { 614 b.StopTimer() 615 size := 100000 616 set := NewWithIntComparator() 617 for n := 0; n < size; n++ { 618 set.Add(n) 619 } 620 b.StartTimer() 621 benchmarkContains(b, set, size) 622 } 623 624 func BenchmarkTreeSetAdd100(b *testing.B) { 625 b.StopTimer() 626 size := 100 627 set := NewWithIntComparator() 628 b.StartTimer() 629 benchmarkAdd(b, set, size) 630 } 631 632 func BenchmarkTreeSetAdd1000(b *testing.B) { 633 b.StopTimer() 634 size := 1000 635 set := NewWithIntComparator() 636 for n := 0; n < size; n++ { 637 set.Add(n) 638 } 639 b.StartTimer() 640 benchmarkAdd(b, set, size) 641 } 642 643 func BenchmarkTreeSetAdd10000(b *testing.B) { 644 b.StopTimer() 645 size := 10000 646 set := NewWithIntComparator() 647 for n := 0; n < size; n++ { 648 set.Add(n) 649 } 650 b.StartTimer() 651 benchmarkAdd(b, set, size) 652 } 653 654 func BenchmarkTreeSetAdd100000(b *testing.B) { 655 b.StopTimer() 656 size := 100000 657 set := NewWithIntComparator() 658 for n := 0; n < size; n++ { 659 set.Add(n) 660 } 661 b.StartTimer() 662 benchmarkAdd(b, set, size) 663 } 664 665 func BenchmarkTreeSetRemove100(b *testing.B) { 666 b.StopTimer() 667 size := 100 668 set := NewWithIntComparator() 669 for n := 0; n < size; n++ { 670 set.Add(n) 671 } 672 b.StartTimer() 673 benchmarkRemove(b, set, size) 674 } 675 676 func BenchmarkTreeSetRemove1000(b *testing.B) { 677 b.StopTimer() 678 size := 1000 679 set := NewWithIntComparator() 680 for n := 0; n < size; n++ { 681 set.Add(n) 682 } 683 b.StartTimer() 684 benchmarkRemove(b, set, size) 685 } 686 687 func BenchmarkTreeSetRemove10000(b *testing.B) { 688 b.StopTimer() 689 size := 10000 690 set := NewWithIntComparator() 691 for n := 0; n < size; n++ { 692 set.Add(n) 693 } 694 b.StartTimer() 695 benchmarkRemove(b, set, size) 696 } 697 698 func BenchmarkTreeSetRemove100000(b *testing.B) { 699 b.StopTimer() 700 size := 100000 701 set := NewWithIntComparator() 702 for n := 0; n < size; n++ { 703 set.Add(n) 704 } 705 b.StartTimer() 706 benchmarkRemove(b, set, size) 707 }