github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bcomparator/comparator_test.go (about) 1 package bcomparator 2 3 import ( 4 "math" 5 "reflect" 6 "testing" 7 ) 8 9 func TestStringComparator(t *testing.T) { 10 tests := []struct { 11 name string 12 s1 string 13 s2 string 14 want int 15 }{ 16 { 17 name: "eq", 18 s1: "", 19 s2: "", 20 want: 0, 21 }, 22 { 23 name: "eq", 24 s1: "123", 25 s2: "123", 26 want: 0, 27 }, 28 { 29 name: "eq", 30 s1: "abc", 31 s2: "abc", 32 want: 0, 33 }, 34 { 35 name: "gt", 36 s1: "1", 37 s2: "", 38 want: 1, 39 }, 40 { 41 name: "gt", 42 s1: "321", 43 s2: "123", 44 want: 1, 45 }, 46 { 47 name: "gt", 48 s1: "a", 49 s2: "", 50 want: 1, 51 }, 52 { 53 name: "gt", 54 s1: "cba", 55 s2: "abc", 56 want: 1, 57 }, 58 { 59 name: "lt", 60 s1: "", 61 s2: "1", 62 want: -1, 63 }, 64 { 65 name: "lt", 66 s1: "123", 67 s2: "321", 68 want: -1, 69 }, 70 { 71 name: "lt", 72 s1: "", 73 s2: "a", 74 want: -1, 75 }, 76 { 77 name: "lt", 78 s1: "abc", 79 s2: "cba", 80 want: -1, 81 }, 82 } 83 for _, tt := range tests { 84 t.Run(tt.name, func(t *testing.T) { 85 if got := StringComparator()(tt.s1, tt.s2); !reflect.DeepEqual(got, tt.want) { 86 t.Errorf("StringComparator() = %v, want %v", got, tt.want) 87 } 88 }) 89 } 90 } 91 92 func TestIntComparator(t *testing.T) { 93 tests := []struct { 94 name string 95 i1 int 96 i2 int 97 want int 98 }{ 99 { 100 name: "eq", 101 i1: 0, 102 i2: 0, 103 want: 0, 104 }, 105 { 106 name: "eq", 107 i1: math.MaxInt, 108 i2: math.MaxInt, 109 want: 0, 110 }, 111 { 112 name: "eq", 113 i1: math.MinInt, 114 i2: math.MinInt, 115 want: 0, 116 }, 117 { 118 name: "gt", 119 i1: 1, 120 i2: 0, 121 want: 1, 122 }, 123 { 124 name: "gt", 125 i1: math.MaxInt, 126 i2: 0, 127 want: 1, 128 }, 129 { 130 name: "gt", 131 i1: math.MaxInt, 132 i2: math.MinInt, 133 want: 1, 134 }, 135 { 136 name: "lt", 137 i1: 0, 138 i2: 1, 139 want: -1, 140 }, 141 { 142 name: "lt", 143 i1: 0, 144 i2: math.MaxInt, 145 want: -1, 146 }, 147 { 148 name: "lt", 149 i1: math.MinInt, 150 i2: math.MaxInt, 151 want: -1, 152 }, 153 } 154 for _, tt := range tests { 155 t.Run(tt.name, func(t *testing.T) { 156 if got := IntComparator()(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 157 t.Errorf("IntComparator() = %v, want %v", got, tt.want) 158 } 159 }) 160 } 161 } 162 163 func TestInt8Comparator(t *testing.T) { 164 tests := []struct { 165 name string 166 i1 int8 167 i2 int8 168 want int 169 }{ 170 { 171 name: "eq", 172 i1: 0, 173 i2: 0, 174 want: 0, 175 }, 176 { 177 name: "eq", 178 i1: math.MaxInt8, 179 i2: math.MaxInt8, 180 want: 0, 181 }, 182 { 183 name: "eq", 184 i1: math.MinInt8, 185 i2: math.MinInt8, 186 want: 0, 187 }, 188 { 189 name: "gt", 190 i1: 1, 191 i2: 0, 192 want: 1, 193 }, 194 { 195 name: "gt", 196 i1: math.MaxInt8, 197 i2: 0, 198 want: 1, 199 }, 200 { 201 name: "gt", 202 i1: math.MaxInt8, 203 i2: math.MinInt8, 204 want: 1, 205 }, 206 { 207 name: "lt", 208 i1: 0, 209 i2: 1, 210 want: -1, 211 }, 212 { 213 name: "lt", 214 i1: 0, 215 i2: math.MaxInt8, 216 want: -1, 217 }, 218 { 219 name: "lt", 220 i1: math.MinInt8, 221 i2: math.MaxInt8, 222 want: -1, 223 }, 224 } 225 for _, tt := range tests { 226 t.Run(tt.name, func(t *testing.T) { 227 if got := Int8Comparator()(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 228 t.Errorf("IntComparator() = %v, want %v", got, tt.want) 229 } 230 }) 231 } 232 } 233 234 func TestInt16Comparator(t *testing.T) { 235 tests := []struct { 236 name string 237 i1 int16 238 i2 int16 239 want int 240 }{ 241 { 242 name: "eq", 243 i1: 0, 244 i2: 0, 245 want: 0, 246 }, 247 { 248 name: "eq", 249 i1: math.MaxInt16, 250 i2: math.MaxInt16, 251 want: 0, 252 }, 253 { 254 name: "eq", 255 i1: math.MinInt16, 256 i2: math.MinInt16, 257 want: 0, 258 }, 259 { 260 name: "gt", 261 i1: 1, 262 i2: 0, 263 want: 1, 264 }, 265 { 266 name: "gt", 267 i1: math.MaxInt16, 268 i2: 0, 269 want: 1, 270 }, 271 { 272 name: "gt", 273 i1: math.MaxInt16, 274 i2: math.MinInt16, 275 want: 1, 276 }, 277 { 278 name: "lt", 279 i1: 0, 280 i2: 1, 281 want: -1, 282 }, 283 { 284 name: "lt", 285 i1: 0, 286 i2: math.MaxInt16, 287 want: -1, 288 }, 289 { 290 name: "lt", 291 i1: math.MinInt16, 292 i2: math.MaxInt16, 293 want: -1, 294 }, 295 } 296 for _, tt := range tests { 297 t.Run(tt.name, func(t *testing.T) { 298 if got := Int16Comparator()(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 299 t.Errorf("IntComparator() = %v, want %v", got, tt.want) 300 } 301 }) 302 } 303 } 304 305 func TestInt32Comparator(t *testing.T) { 306 tests := []struct { 307 name string 308 i1 int32 309 i2 int32 310 want int 311 }{ 312 { 313 name: "eq", 314 i1: 0, 315 i2: 0, 316 want: 0, 317 }, 318 { 319 name: "eq", 320 i1: math.MaxInt32, 321 i2: math.MaxInt32, 322 want: 0, 323 }, 324 { 325 name: "eq", 326 i1: math.MinInt32, 327 i2: math.MinInt32, 328 want: 0, 329 }, 330 { 331 name: "gt", 332 i1: 1, 333 i2: 0, 334 want: 1, 335 }, 336 { 337 name: "gt", 338 i1: math.MaxInt32, 339 i2: 0, 340 want: 1, 341 }, 342 { 343 name: "gt", 344 i1: math.MaxInt32, 345 i2: math.MinInt32, 346 want: 1, 347 }, 348 { 349 name: "lt", 350 i1: 0, 351 i2: 1, 352 want: -1, 353 }, 354 { 355 name: "lt", 356 i1: 0, 357 i2: math.MaxInt32, 358 want: -1, 359 }, 360 { 361 name: "lt", 362 i1: math.MinInt32, 363 i2: math.MaxInt32, 364 want: -1, 365 }, 366 } 367 for _, tt := range tests { 368 t.Run(tt.name, func(t *testing.T) { 369 if got := Int32Comparator()(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 370 t.Errorf("IntComparator() = %v, want %v", got, tt.want) 371 } 372 }) 373 } 374 } 375 376 func TestInt64Comparator(t *testing.T) { 377 tests := []struct { 378 name string 379 i1 int64 380 i2 int64 381 want int 382 }{ 383 { 384 name: "eq", 385 i1: 0, 386 i2: 0, 387 want: 0, 388 }, 389 { 390 name: "eq", 391 i1: math.MaxInt64, 392 i2: math.MaxInt64, 393 want: 0, 394 }, 395 { 396 name: "eq", 397 i1: math.MinInt64, 398 i2: math.MinInt64, 399 want: 0, 400 }, 401 { 402 name: "gt", 403 i1: 1, 404 i2: 0, 405 want: 1, 406 }, 407 { 408 name: "gt", 409 i1: math.MaxInt64, 410 i2: 0, 411 want: 1, 412 }, 413 { 414 name: "gt", 415 i1: math.MaxInt64, 416 i2: math.MinInt64, 417 want: 1, 418 }, 419 { 420 name: "lt", 421 i1: 0, 422 i2: 1, 423 want: -1, 424 }, 425 { 426 name: "lt", 427 i1: 0, 428 i2: math.MaxInt64, 429 want: -1, 430 }, 431 { 432 name: "lt", 433 i1: math.MinInt64, 434 i2: math.MaxInt64, 435 want: -1, 436 }, 437 } 438 for _, tt := range tests { 439 t.Run(tt.name, func(t *testing.T) { 440 if got := Int64Comparator()(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 441 t.Errorf("IntComparator() = %v, want %v", got, tt.want) 442 } 443 }) 444 } 445 } 446 447 func TestUintComparator(t *testing.T) { 448 tests := []struct { 449 name string 450 i1 uint 451 i2 uint 452 want int 453 }{ 454 { 455 name: "eq", 456 i1: 0, 457 i2: 0, 458 want: 0, 459 }, 460 { 461 name: "eq", 462 i1: math.MaxUint, 463 i2: math.MaxUint, 464 want: 0, 465 }, 466 { 467 name: "eq", 468 i1: 1, 469 i2: 1, 470 want: 0, 471 }, 472 { 473 name: "gt", 474 i1: 1, 475 i2: 0, 476 want: 1, 477 }, 478 { 479 name: "gt", 480 i1: math.MaxUint, 481 i2: 0, 482 want: 1, 483 }, 484 { 485 name: "gt", 486 i1: math.MaxUint, 487 i2: 1, 488 want: 1, 489 }, 490 { 491 name: "lt", 492 i1: 0, 493 i2: 1, 494 want: -1, 495 }, 496 { 497 name: "lt", 498 i1: 0, 499 i2: math.MaxUint, 500 want: -1, 501 }, 502 { 503 name: "lt", 504 i1: 1, 505 i2: math.MaxUint, 506 want: -1, 507 }, 508 } 509 for _, tt := range tests { 510 t.Run(tt.name, func(t *testing.T) { 511 if got := UintComparator()(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 512 t.Errorf("UintComparator() = %v, want %v", got, tt.want) 513 } 514 }) 515 } 516 } 517 518 func TestUint8Comparator(t *testing.T) { 519 tests := []struct { 520 name string 521 i1 uint8 522 i2 uint8 523 want int 524 }{ 525 { 526 name: "eq", 527 i1: 0, 528 i2: 0, 529 want: 0, 530 }, 531 { 532 name: "eq", 533 i1: math.MaxUint8, 534 i2: math.MaxUint8, 535 want: 0, 536 }, 537 { 538 name: "eq", 539 i1: 1, 540 i2: 1, 541 want: 0, 542 }, 543 { 544 name: "gt", 545 i1: 1, 546 i2: 0, 547 want: 1, 548 }, 549 { 550 name: "gt", 551 i1: math.MaxUint8, 552 i2: 0, 553 want: 1, 554 }, 555 { 556 name: "gt", 557 i1: math.MaxUint8, 558 i2: 1, 559 want: 1, 560 }, 561 { 562 name: "lt", 563 i1: 0, 564 i2: 1, 565 want: -1, 566 }, 567 { 568 name: "lt", 569 i1: 0, 570 i2: math.MaxUint8, 571 want: -1, 572 }, 573 { 574 name: "lt", 575 i1: 1, 576 i2: math.MaxUint8, 577 want: -1, 578 }, 579 } 580 for _, tt := range tests { 581 t.Run(tt.name, func(t *testing.T) { 582 if got := Uint8Comparator()(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 583 t.Errorf("UintComparator() = %v, want %v", got, tt.want) 584 } 585 }) 586 } 587 } 588 589 func TestUint16Comparator(t *testing.T) { 590 tests := []struct { 591 name string 592 i1 uint16 593 i2 uint16 594 want int 595 }{ 596 { 597 name: "eq", 598 i1: 0, 599 i2: 0, 600 want: 0, 601 }, 602 { 603 name: "eq", 604 i1: math.MaxUint16, 605 i2: math.MaxUint16, 606 want: 0, 607 }, 608 { 609 name: "eq", 610 i1: 1, 611 i2: 1, 612 want: 0, 613 }, 614 { 615 name: "gt", 616 i1: 1, 617 i2: 0, 618 want: 1, 619 }, 620 { 621 name: "gt", 622 i1: math.MaxUint16, 623 i2: 0, 624 want: 1, 625 }, 626 { 627 name: "gt", 628 i1: math.MaxUint16, 629 i2: 1, 630 want: 1, 631 }, 632 { 633 name: "lt", 634 i1: 0, 635 i2: 1, 636 want: -1, 637 }, 638 { 639 name: "lt", 640 i1: 0, 641 i2: math.MaxUint16, 642 want: -1, 643 }, 644 { 645 name: "lt", 646 i1: 1, 647 i2: math.MaxUint16, 648 want: -1, 649 }, 650 } 651 for _, tt := range tests { 652 t.Run(tt.name, func(t *testing.T) { 653 if got := Uint16Comparator()(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 654 t.Errorf("UintComparator() = %v, want %v", got, tt.want) 655 } 656 }) 657 } 658 } 659 660 func TestUint32Comparator(t *testing.T) { 661 tests := []struct { 662 name string 663 i1 uint32 664 i2 uint32 665 want int 666 }{ 667 { 668 name: "eq", 669 i1: 0, 670 i2: 0, 671 want: 0, 672 }, 673 { 674 name: "eq", 675 i1: math.MaxUint32, 676 i2: math.MaxUint32, 677 want: 0, 678 }, 679 { 680 name: "eq", 681 i1: 1, 682 i2: 1, 683 want: 0, 684 }, 685 { 686 name: "gt", 687 i1: 1, 688 i2: 0, 689 want: 1, 690 }, 691 { 692 name: "gt", 693 i1: math.MaxUint32, 694 i2: 0, 695 want: 1, 696 }, 697 { 698 name: "gt", 699 i1: math.MaxUint32, 700 i2: 1, 701 want: 1, 702 }, 703 { 704 name: "lt", 705 i1: 0, 706 i2: 1, 707 want: -1, 708 }, 709 { 710 name: "lt", 711 i1: 0, 712 i2: math.MaxUint32, 713 want: -1, 714 }, 715 { 716 name: "lt", 717 i1: 1, 718 i2: math.MaxUint32, 719 want: -1, 720 }, 721 } 722 for _, tt := range tests { 723 t.Run(tt.name, func(t *testing.T) { 724 if got := Uint32Comparator()(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 725 t.Errorf("UintComparator() = %v, want %v", got, tt.want) 726 } 727 }) 728 } 729 } 730 731 func TestUint64Comparator(t *testing.T) { 732 tests := []struct { 733 name string 734 i1 uint64 735 i2 uint64 736 want int 737 }{ 738 { 739 name: "eq", 740 i1: 0, 741 i2: 0, 742 want: 0, 743 }, 744 { 745 name: "eq", 746 i1: math.MaxUint64, 747 i2: math.MaxUint64, 748 want: 0, 749 }, 750 { 751 name: "eq", 752 i1: 1, 753 i2: 1, 754 want: 0, 755 }, 756 { 757 name: "gt", 758 i1: 1, 759 i2: 0, 760 want: 1, 761 }, 762 { 763 name: "gt", 764 i1: math.MaxUint64, 765 i2: 0, 766 want: 1, 767 }, 768 { 769 name: "gt", 770 i1: math.MaxUint64, 771 i2: 1, 772 want: 1, 773 }, 774 { 775 name: "lt", 776 i1: 0, 777 i2: 1, 778 want: -1, 779 }, 780 { 781 name: "lt", 782 i1: 0, 783 i2: math.MaxUint64, 784 want: -1, 785 }, 786 { 787 name: "lt", 788 i1: 1, 789 i2: math.MaxUint64, 790 want: -1, 791 }, 792 } 793 for _, tt := range tests { 794 t.Run(tt.name, func(t *testing.T) { 795 if got := Uint64Comparator()(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 796 t.Errorf("IntComparator() = %v, want %v", got, tt.want) 797 } 798 }) 799 } 800 } 801 802 func TestFloat32Comparator(t *testing.T) { 803 tests := []struct { 804 name string 805 f1 float32 806 f2 float32 807 want int 808 }{ 809 { 810 name: "eq", 811 f1: 0.1, 812 f2: 0.1, 813 want: 0, 814 }, 815 { 816 name: "eq", 817 f1: math.MaxFloat32, 818 f2: math.MaxFloat32, 819 want: 0, 820 }, 821 { 822 name: "eq", 823 f1: 0.0000000001, 824 f2: 0.0000000002, 825 want: 0, 826 }, 827 { 828 name: "gt", 829 f1: 1.11, 830 f2: 1.10, 831 want: 1, 832 }, 833 { 834 name: "gt", 835 f1: math.MaxFloat32, 836 f2: 1.11, 837 want: 1, 838 }, 839 { 840 name: "gt", 841 f1: math.MaxFloat32, 842 f2: 1.11, 843 want: 1, 844 }, 845 { 846 name: "lt", 847 f1: 1.10, 848 f2: 1.11, 849 want: -1, 850 }, 851 { 852 name: "lt", 853 f1: 1.11, 854 f2: math.MaxFloat32, 855 want: -1, 856 }, 857 { 858 name: "lt", 859 f1: 1.11, 860 f2: math.MaxFloat32, 861 want: -1, 862 }, 863 } 864 for _, tt := range tests { 865 t.Run(tt.name, func(t *testing.T) { 866 if got := Float32Comparator()(tt.f1, tt.f2); !reflect.DeepEqual(got, tt.want) { 867 t.Errorf("IntComparator() = %v, want %v", got, tt.want) 868 } 869 }) 870 } 871 } 872 873 func TestFloat64Comparator(t *testing.T) { 874 tests := []struct { 875 name string 876 f1 float64 877 f2 float64 878 want int 879 }{ 880 { 881 name: "eq", 882 f1: 0.1, 883 f2: 0.1, 884 want: 0, 885 }, 886 { 887 name: "eq", 888 f1: math.MaxFloat64, 889 f2: math.MaxFloat64, 890 want: 0, 891 }, 892 { 893 name: "eq", 894 f1: 0.0000000001, 895 f2: 0.0000000002, 896 want: 0, 897 }, 898 { 899 name: "gt", 900 f1: 1.11, 901 f2: 1.10, 902 want: 1, 903 }, 904 { 905 name: "gt", 906 f1: math.MaxFloat64, 907 f2: 1.11, 908 want: 1, 909 }, 910 { 911 name: "gt", 912 f1: math.MaxFloat64, 913 f2: 1.11, 914 want: 1, 915 }, 916 { 917 name: "lt", 918 f1: 1.10, 919 f2: 1.11, 920 want: -1, 921 }, 922 { 923 name: "lt", 924 f1: 1.11, 925 f2: math.MaxFloat64, 926 want: -1, 927 }, 928 { 929 name: "lt", 930 f1: 1.11, 931 f2: math.MaxFloat64, 932 want: -1, 933 }, 934 } 935 for _, tt := range tests { 936 t.Run(tt.name, func(t *testing.T) { 937 if got := Float64Comparator()(tt.f1, tt.f2); !reflect.DeepEqual(got, tt.want) { 938 t.Errorf("IntComparator() = %v, want %v", got, tt.want) 939 } 940 }) 941 } 942 } 943 944 func TestBoolComparator(t *testing.T) { 945 tests := []struct { 946 name string 947 b1 bool 948 b2 bool 949 want int 950 }{ 951 { 952 name: "eq", 953 b1: true, 954 b2: true, 955 want: 0, 956 }, 957 { 958 name: "eq", 959 b1: false, 960 b2: false, 961 want: 0, 962 }, 963 { 964 name: "gt", 965 b1: true, 966 b2: false, 967 want: 1, 968 }, 969 970 { 971 name: "lt", 972 b1: false, 973 b2: true, 974 want: -1, 975 }, 976 } 977 for _, tt := range tests { 978 t.Run(tt.name, func(t *testing.T) { 979 if got := BoolComparator()(tt.b1, tt.b2); !reflect.DeepEqual(got, tt.want) { 980 t.Errorf("IntComparator() = %v, want %v", got, tt.want) 981 } 982 }) 983 } 984 } 985 986 func TestReverseComparator(t *testing.T) { 987 tests := []struct { 988 name string 989 want int 990 i1, i2 int 991 }{ 992 { 993 name: "eq", 994 want: 0, 995 i1: 0, 996 i2: 0, 997 }, 998 { 999 name: "lt", 1000 want: -1, 1001 i1: 1, 1002 i2: 0, 1003 }, 1004 { 1005 name: "gt", 1006 want: 1, 1007 i1: 0, 1008 i2: 1, 1009 }, 1010 } 1011 for _, tt := range tests { 1012 t.Run(tt.name, func(t *testing.T) { 1013 if got := ReverseComparator(IntComparator())(tt.i1, tt.i2); !reflect.DeepEqual(got, tt.want) { 1014 t.Errorf("ReverseComparator() = %v, want %v", got, tt.want) 1015 } 1016 }) 1017 } 1018 }