github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bslice/ordered_test.go (about) 1 package bslice 2 3 import ( 4 "github.com/stretchr/testify/assert" 5 "testing" 6 ) 7 8 func TestUnsafeOrderedBSlice_Compare(t *testing.T) { 9 type fields struct { 10 e []int 11 } 12 type args struct { 13 es []int 14 } 15 tests := []struct { 16 name string 17 fields fields 18 args args 19 want int 20 }{ 21 { 22 name: "nil", 23 fields: fields{ 24 e: nil, 25 }, 26 args: args{ 27 es: nil, 28 }, 29 want: 0, 30 }, 31 { 32 name: "", 33 fields: fields{ 34 e: []int{}, 35 }, 36 args: args{ 37 es: nil, 38 }, 39 want: 0, 40 }, 41 { 42 name: "", 43 fields: fields{ 44 e: nil, 45 }, 46 args: args{ 47 es: []int{}, 48 }, 49 want: 0, 50 }, 51 { 52 name: "", 53 fields: fields{ 54 e: []int{1}, 55 }, 56 args: args{ 57 es: []int{}, 58 }, 59 want: 1, 60 }, 61 { 62 name: "", 63 fields: fields{ 64 e: []int{}, 65 }, 66 args: args{ 67 es: []int{1}, 68 }, 69 want: -1, 70 }, 71 { 72 name: "", 73 fields: fields{ 74 e: []int{2, 2}, 75 }, 76 args: args{ 77 es: []int{2, 1, 1}, 78 }, 79 want: 1, 80 }, 81 { 82 name: "", 83 fields: fields{ 84 e: []int{2, 1, 1}, 85 }, 86 args: args{ 87 es: []int{2, 2}, 88 }, 89 want: -1, 90 }, 91 } 92 for _, tt := range tests { 93 t.Run(tt.name, func(t *testing.T) { 94 x := NewUnsafeOrderedBSliceBySlice(tt.fields.e) 95 assert.Equalf(t, tt.want, x.Compare(tt.args.es), "Compare(%v)", tt.args.es) 96 }) 97 } 98 } 99 100 func TestUnsafeOrderedBSlice_Sort(t *testing.T) { 101 type fields struct { 102 e []int 103 } 104 tests := []struct { 105 name string 106 fields fields 107 want []int 108 }{ 109 { 110 name: "nil", 111 fields: fields{ 112 e: nil, 113 }, 114 want: nil, 115 }, 116 { 117 name: "", 118 fields: fields{ 119 e: []int{}, 120 }, 121 want: []int{}, 122 }, 123 { 124 name: "", 125 fields: fields{ 126 e: []int{1, 2, 3}, 127 }, 128 want: []int{1, 2, 3}, 129 }, 130 { 131 name: "", 132 fields: fields{ 133 e: []int{2, 1, 3}, 134 }, 135 want: []int{1, 2, 3}, 136 }, 137 } 138 for _, tt := range tests { 139 t.Run(tt.name, func(t *testing.T) { 140 x := NewUnsafeOrderedBSliceBySlice(tt.fields.e) 141 x.Sort() 142 assert.Equalf(t, tt.want, x.ToMetaSlice(), "Sort") 143 }) 144 } 145 } 146 147 func TestUnsafeOrderedBSlice_IsSorted(t *testing.T) { 148 type fields struct { 149 e []int 150 } 151 tests := []struct { 152 name string 153 fields fields 154 want bool 155 }{ 156 { 157 name: "", 158 fields: fields{ 159 e: nil, 160 }, 161 want: true, 162 }, 163 { 164 name: "", 165 fields: fields{ 166 e: []int{}, 167 }, 168 want: true, 169 }, 170 { 171 name: "", 172 fields: fields{ 173 e: []int{1, 1, 1}, 174 }, 175 want: true, 176 }, 177 { 178 name: "", 179 fields: fields{ 180 e: []int{1, 2, 3}, 181 }, 182 want: true, 183 }, 184 { 185 name: "", 186 fields: fields{ 187 e: []int{2, 1, 3}, 188 }, 189 want: false, 190 }, 191 { 192 name: "", 193 fields: fields{ 194 e: []int{3, 2, 1}, 195 }, 196 want: false, 197 }, 198 } 199 for _, tt := range tests { 200 t.Run(tt.name, func(t *testing.T) { 201 x := NewUnsafeOrderedBSliceBySlice(tt.fields.e) 202 assert.Equalf(t, tt.want, x.IsSorted(), "IsSorted()") 203 }) 204 } 205 } 206 207 func TestUnsafeOrderedBSlice_BinarySearch(t *testing.T) { 208 type fields struct { 209 e []int 210 } 211 type args struct { 212 target int 213 } 214 tests := []struct { 215 name string 216 fields fields 217 args args 218 want int 219 want1 bool 220 }{ 221 { 222 name: "nil", 223 fields: fields{ 224 e: nil, 225 }, 226 args: args{ 227 target: 0, 228 }, 229 want: 0, 230 want1: false, 231 }, 232 { 233 name: "", 234 fields: fields{ 235 e: []int{}, 236 }, 237 args: args{ 238 target: 0, 239 }, 240 want: 0, 241 want1: false, 242 }, 243 { 244 name: "", 245 fields: fields{ 246 e: []int{1, 3, 5}, 247 }, 248 args: args{ 249 target: 0, 250 }, 251 want: 0, 252 want1: false, 253 }, 254 { 255 name: "", 256 fields: fields{ 257 e: []int{1, 3, 5}, 258 }, 259 args: args{ 260 target: 1, 261 }, 262 want: 0, 263 want1: true, 264 }, 265 { 266 name: "", 267 fields: fields{ 268 e: []int{1, 3, 5}, 269 }, 270 args: args{ 271 target: 2, 272 }, 273 want: 1, 274 want1: false, 275 }, 276 { 277 name: "", 278 fields: fields{ 279 e: []int{1, 3, 5}, 280 }, 281 args: args{ 282 target: 3, 283 }, 284 want: 1, 285 want1: true, 286 }, 287 { 288 name: "", 289 fields: fields{ 290 e: []int{1, 3, 5}, 291 }, 292 args: args{ 293 target: 4, 294 }, 295 want: 2, 296 want1: false, 297 }, 298 { 299 name: "", 300 fields: fields{ 301 e: []int{1, 3, 5}, 302 }, 303 args: args{ 304 target: 5, 305 }, 306 want: 2, 307 want1: true, 308 }, 309 { 310 name: "", 311 fields: fields{ 312 e: []int{1, 3, 5}, 313 }, 314 args: args{ 315 target: 6, 316 }, 317 want: 3, 318 want1: false, 319 }, 320 { 321 name: "", 322 fields: fields{ 323 e: []int{1, 3, 5, 7}, 324 }, 325 args: args{ 326 target: 0, 327 }, 328 want: 0, 329 want1: false, 330 }, 331 { 332 name: "", 333 fields: fields{ 334 e: []int{1, 3, 5, 7}, 335 }, 336 args: args{ 337 target: 1, 338 }, 339 want: 0, 340 want1: true, 341 }, 342 { 343 name: "", 344 fields: fields{ 345 e: []int{1, 3, 5, 7}, 346 }, 347 args: args{ 348 target: 2, 349 }, 350 want: 1, 351 want1: false, 352 }, 353 { 354 name: "", 355 fields: fields{ 356 e: []int{1, 3, 5, 7}, 357 }, 358 args: args{ 359 target: 3, 360 }, 361 want: 1, 362 want1: true, 363 }, 364 { 365 name: "", 366 fields: fields{ 367 e: []int{1, 3, 5, 7}, 368 }, 369 args: args{ 370 target: 4, 371 }, 372 want: 2, 373 want1: false, 374 }, 375 { 376 name: "", 377 fields: fields{ 378 e: []int{1, 3, 5, 7}, 379 }, 380 args: args{ 381 target: 5, 382 }, 383 want: 2, 384 want1: true, 385 }, 386 { 387 name: "", 388 fields: fields{ 389 e: []int{1, 3, 5, 7}, 390 }, 391 args: args{ 392 target: 6, 393 }, 394 want: 3, 395 want1: false, 396 }, 397 { 398 name: "", 399 fields: fields{ 400 e: []int{1, 3, 5, 7}, 401 }, 402 args: args{ 403 target: 7, 404 }, 405 want: 3, 406 want1: true, 407 }, 408 { 409 name: "", 410 fields: fields{ 411 e: []int{1, 3, 5, 7}, 412 }, 413 args: args{ 414 target: 8, 415 }, 416 want: 4, 417 want1: false, 418 }, 419 } 420 for _, tt := range tests { 421 t.Run(tt.name, func(t *testing.T) { 422 x := NewUnsafeOrderedBSliceBySlice(tt.fields.e) 423 got, got1 := x.BinarySearch(tt.args.target) 424 assert.Equalf(t, tt.want, got, "BinarySearch(%v)", tt.args.target) 425 assert.Equalf(t, tt.want1, got1, "BinarySearch(%v)", tt.args.target) 426 }) 427 } 428 } 429 430 func TestSafeOrderedBSlice_Compare(t *testing.T) { 431 type fields struct { 432 e []int 433 } 434 type args struct { 435 es []int 436 } 437 tests := []struct { 438 name string 439 fields fields 440 args args 441 want int 442 }{ 443 { 444 name: "nil", 445 fields: fields{ 446 e: nil, 447 }, 448 args: args{ 449 es: nil, 450 }, 451 want: 0, 452 }, 453 { 454 name: "", 455 fields: fields{ 456 e: []int{}, 457 }, 458 args: args{ 459 es: nil, 460 }, 461 want: 0, 462 }, 463 { 464 name: "", 465 fields: fields{ 466 e: nil, 467 }, 468 args: args{ 469 es: []int{}, 470 }, 471 want: 0, 472 }, 473 { 474 name: "", 475 fields: fields{ 476 e: []int{1}, 477 }, 478 args: args{ 479 es: []int{}, 480 }, 481 want: 1, 482 }, 483 { 484 name: "", 485 fields: fields{ 486 e: []int{}, 487 }, 488 args: args{ 489 es: []int{1}, 490 }, 491 want: -1, 492 }, 493 { 494 name: "", 495 fields: fields{ 496 e: []int{2, 2}, 497 }, 498 args: args{ 499 es: []int{2, 1, 1}, 500 }, 501 want: 1, 502 }, 503 { 504 name: "", 505 fields: fields{ 506 e: []int{2, 1, 1}, 507 }, 508 args: args{ 509 es: []int{2, 2}, 510 }, 511 want: -1, 512 }, 513 } 514 for _, tt := range tests { 515 t.Run(tt.name, func(t *testing.T) { 516 x := NewSafeOrderedBSliceBySlice(tt.fields.e) 517 assert.Equalf(t, tt.want, x.Compare(tt.args.es), "Compare(%v)", tt.args.es) 518 }) 519 } 520 } 521 522 func TestSafeOrderedBSlice_Sort(t *testing.T) { 523 type fields struct { 524 e []int 525 } 526 tests := []struct { 527 name string 528 fields fields 529 want []int 530 }{ 531 { 532 name: "nil", 533 fields: fields{ 534 e: nil, 535 }, 536 want: nil, 537 }, 538 { 539 name: "", 540 fields: fields{ 541 e: []int{}, 542 }, 543 want: []int{}, 544 }, 545 { 546 name: "", 547 fields: fields{ 548 e: []int{1, 2, 3}, 549 }, 550 want: []int{1, 2, 3}, 551 }, 552 { 553 name: "", 554 fields: fields{ 555 e: []int{2, 1, 3}, 556 }, 557 want: []int{1, 2, 3}, 558 }, 559 } 560 for _, tt := range tests { 561 t.Run(tt.name, func(t *testing.T) { 562 x := NewSafeOrderedBSliceBySlice(tt.fields.e) 563 x.Sort() 564 assert.Equalf(t, tt.want, x.ToMetaSlice(), "Sort") 565 }) 566 } 567 } 568 569 func TestSafeOrderedBSlice_IsSorted(t *testing.T) { 570 type fields struct { 571 e []int 572 } 573 tests := []struct { 574 name string 575 fields fields 576 want bool 577 }{ 578 { 579 name: "", 580 fields: fields{ 581 e: nil, 582 }, 583 want: true, 584 }, 585 { 586 name: "", 587 fields: fields{ 588 e: []int{}, 589 }, 590 want: true, 591 }, 592 { 593 name: "", 594 fields: fields{ 595 e: []int{1, 1, 1}, 596 }, 597 want: true, 598 }, 599 { 600 name: "", 601 fields: fields{ 602 e: []int{1, 2, 3}, 603 }, 604 want: true, 605 }, 606 { 607 name: "", 608 fields: fields{ 609 e: []int{2, 1, 3}, 610 }, 611 want: false, 612 }, 613 { 614 name: "", 615 fields: fields{ 616 e: []int{3, 2, 1}, 617 }, 618 want: false, 619 }, 620 } 621 for _, tt := range tests { 622 t.Run(tt.name, func(t *testing.T) { 623 x := NewSafeOrderedBSliceBySlice(tt.fields.e) 624 assert.Equalf(t, tt.want, x.IsSorted(), "IsSorted()") 625 }) 626 } 627 } 628 629 func TestSafeOrderedBSlice_BinarySearch(t *testing.T) { 630 type fields struct { 631 e []int 632 } 633 type args struct { 634 target int 635 } 636 tests := []struct { 637 name string 638 fields fields 639 args args 640 want int 641 want1 bool 642 }{ 643 { 644 name: "nil", 645 fields: fields{ 646 e: nil, 647 }, 648 args: args{ 649 target: 0, 650 }, 651 want: 0, 652 want1: false, 653 }, 654 { 655 name: "", 656 fields: fields{ 657 e: []int{}, 658 }, 659 args: args{ 660 target: 0, 661 }, 662 want: 0, 663 want1: false, 664 }, 665 { 666 name: "", 667 fields: fields{ 668 e: []int{1, 3, 5}, 669 }, 670 args: args{ 671 target: 0, 672 }, 673 want: 0, 674 want1: false, 675 }, 676 { 677 name: "", 678 fields: fields{ 679 e: []int{1, 3, 5}, 680 }, 681 args: args{ 682 target: 1, 683 }, 684 want: 0, 685 want1: true, 686 }, 687 { 688 name: "", 689 fields: fields{ 690 e: []int{1, 3, 5}, 691 }, 692 args: args{ 693 target: 2, 694 }, 695 want: 1, 696 want1: false, 697 }, 698 { 699 name: "", 700 fields: fields{ 701 e: []int{1, 3, 5}, 702 }, 703 args: args{ 704 target: 3, 705 }, 706 want: 1, 707 want1: true, 708 }, 709 { 710 name: "", 711 fields: fields{ 712 e: []int{1, 3, 5}, 713 }, 714 args: args{ 715 target: 4, 716 }, 717 want: 2, 718 want1: false, 719 }, 720 { 721 name: "", 722 fields: fields{ 723 e: []int{1, 3, 5}, 724 }, 725 args: args{ 726 target: 5, 727 }, 728 want: 2, 729 want1: true, 730 }, 731 { 732 name: "", 733 fields: fields{ 734 e: []int{1, 3, 5}, 735 }, 736 args: args{ 737 target: 6, 738 }, 739 want: 3, 740 want1: false, 741 }, 742 { 743 name: "", 744 fields: fields{ 745 e: []int{1, 3, 5, 7}, 746 }, 747 args: args{ 748 target: 0, 749 }, 750 want: 0, 751 want1: false, 752 }, 753 { 754 name: "", 755 fields: fields{ 756 e: []int{1, 3, 5, 7}, 757 }, 758 args: args{ 759 target: 1, 760 }, 761 want: 0, 762 want1: true, 763 }, 764 { 765 name: "", 766 fields: fields{ 767 e: []int{1, 3, 5, 7}, 768 }, 769 args: args{ 770 target: 2, 771 }, 772 want: 1, 773 want1: false, 774 }, 775 { 776 name: "", 777 fields: fields{ 778 e: []int{1, 3, 5, 7}, 779 }, 780 args: args{ 781 target: 3, 782 }, 783 want: 1, 784 want1: true, 785 }, 786 { 787 name: "", 788 fields: fields{ 789 e: []int{1, 3, 5, 7}, 790 }, 791 args: args{ 792 target: 4, 793 }, 794 want: 2, 795 want1: false, 796 }, 797 { 798 name: "", 799 fields: fields{ 800 e: []int{1, 3, 5, 7}, 801 }, 802 args: args{ 803 target: 5, 804 }, 805 want: 2, 806 want1: true, 807 }, 808 { 809 name: "", 810 fields: fields{ 811 e: []int{1, 3, 5, 7}, 812 }, 813 args: args{ 814 target: 6, 815 }, 816 want: 3, 817 want1: false, 818 }, 819 { 820 name: "", 821 fields: fields{ 822 e: []int{1, 3, 5, 7}, 823 }, 824 args: args{ 825 target: 7, 826 }, 827 want: 3, 828 want1: true, 829 }, 830 { 831 name: "", 832 fields: fields{ 833 e: []int{1, 3, 5, 7}, 834 }, 835 args: args{ 836 target: 8, 837 }, 838 want: 4, 839 want1: false, 840 }, 841 } 842 for _, tt := range tests { 843 t.Run(tt.name, func(t *testing.T) { 844 x := NewSafeOrderedBSliceBySlice(tt.fields.e) 845 got, got1 := x.BinarySearch(tt.args.target) 846 assert.Equalf(t, tt.want, got, "BinarySearch(%v)", tt.args.target) 847 assert.Equalf(t, tt.want1, got1, "BinarySearch(%v)", tt.args.target) 848 }) 849 } 850 }