1" Tests for the "sort()" function and for the ":sort" command. 2 3source check.vim 4 5func Compare1(a, b) abort 6 call sort(range(3), 'Compare2') 7 return a:a - a:b 8endfunc 9 10func Compare2(a, b) abort 11 return a:a - a:b 12endfunc 13 14func Test_sort_strings() 15 " numbers compared as strings 16 call assert_equal([1, 2, 3], sort([3, 2, 1])) 17 call assert_equal([13, 28, 3], sort([3, 28, 13])) 18 19 call assert_equal(['A', 'O', 'P', 'a', 'o', 'p', 'Ä', 'Ô', 'ä', 'ô', 'œ', 'œ'], 20 \ sort(['A', 'O', 'P', 'a', 'o', 'p', 'Ä', 'Ô', 'ä', 'ô', 'œ', 'œ'])) 21 22 call assert_equal(['A', 'a', 'o', 'O', 'p', 'P', 'Ä', 'Ô', 'ä', 'ô', 'œ', 'œ'], 23 \ sort(['A', 'a', 'o', 'O', 'œ', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'i')) 24 25 " This does not appear to work correctly on Mac. 26 if !has('mac') 27 " With the following locales, the accentuated letters are ordered 28 " similarly to the non-accentuated letters... 29 if v:collate =~? '^\(en\|es\|de\|fr\|it\|nl\).*\.utf-\?8$' 30 call assert_equal(['a', 'A', 'ä', 'Ä', 'o', 'O', 'ô', 'Ô', 'œ', 'œ', 'p', 'P'], 31 \ sort(['A', 'a', 'o', 'O', 'œ', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l')) 32 " ... whereas with a Swedish locale, the accentuated letters are ordered 33 " after Z. 34 elseif v:collate =~? '^sv.*utf-\?8$' 35 call assert_equal(['a', 'A', 'o', 'O', 'p', 'P', 'ä', 'Ä', 'œ', 'œ', 'ô', 'Ô'], 36 \ sort(['A', 'a', 'o', 'O', 'œ', 'œ', 'p', 'P', 'Ä', 'ä', 'ô', 'Ô'], 'l')) 37 endif 38 endif 39endfunc 40 41func Test_sort_null_string() 42 " null strings are sorted as empty strings. 43 call assert_equal(['', 'a', 'b'], sort(['b', test_null_string(), 'a'])) 44endfunc 45 46func Test_sort_numeric() 47 call assert_equal([1, 2, 3], sort([3, 2, 1], 'n')) 48 call assert_equal([3, 13, 28], sort([13, 28, 3], 'n')) 49 " strings are not sorted 50 call assert_equal(['13', '28', '3'], sort(['13', '28', '3'], 'n')) 51endfunc 52 53func Test_sort_numbers() 54 call assert_equal([3, 13, 28], sort([13, 28, 3], 'N')) 55 call assert_equal(['3', '13', '28'], sort(['13', '28', '3'], 'N')) 56endfunc 57 58func Test_sort_float() 59 CheckFeature float 60 call assert_equal([0.28, 3, 13.5], sort([13.5, 0.28, 3], 'f')) 61endfunc 62 63func Test_sort_nested() 64 " test ability to call sort() from a compare function 65 call assert_equal([1, 3, 5], sort([3, 1, 5], 'Compare1')) 66endfunc 67 68func Test_sort_default() 69 CheckFeature float 70 71 " docs say omitted, empty or zero argument sorts on string representation. 72 call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"])) 73 call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], '')) 74 call assert_equal(['2', 'A', 'AA', 'a', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], 0)) 75 call assert_equal(['2', 'A', 'a', 'AA', 1, 3.3], sort([3.3, 1, "2", "A", "a", "AA"], 1)) 76 call assert_fails('call sort([3.3, 1, "2"], 3)', "E474:") 77endfunc 78 79" Tests for the ":sort" command. 80func Test_sort_cmd() 81 let tests = [ 82 \ { 83 \ 'name' : 'Alphabetical sort', 84 \ 'cmd' : '%sort', 85 \ 'input' : [ 86 \ 'abc', 87 \ 'ab', 88 \ 'a', 89 \ 'a321', 90 \ 'a123', 91 \ 'a122', 92 \ 'b321', 93 \ 'b123', 94 \ 'c123d', 95 \ ' 123b', 96 \ 'c321d', 97 \ 'b322b', 98 \ 'b321', 99 \ 'b321b' 100 \ ], 101 \ 'expected' : [ 102 \ ' 123b', 103 \ 'a', 104 \ 'a122', 105 \ 'a123', 106 \ 'a321', 107 \ 'ab', 108 \ 'abc', 109 \ 'b123', 110 \ 'b321', 111 \ 'b321', 112 \ 'b321b', 113 \ 'b322b', 114 \ 'c123d', 115 \ 'c321d' 116 \ ] 117 \ }, 118 \ { 119 \ 'name' : 'Numeric sort', 120 \ 'cmd' : '%sort n', 121 \ 'input' : [ 122 \ 'abc', 123 \ 'ab', 124 \ 'a321', 125 \ 'a123', 126 \ 'a122', 127 \ 'a', 128 \ 'x-22', 129 \ 'b321', 130 \ 'b123', 131 \ '', 132 \ 'c123d', 133 \ '-24', 134 \ ' 123b', 135 \ 'c321d', 136 \ '0', 137 \ 'b322b', 138 \ 'b321', 139 \ 'b321b' 140 \ ], 141 \ 'expected' : [ 142 \ 'abc', 143 \ 'ab', 144 \ 'a', 145 \ '', 146 \ '-24', 147 \ 'x-22', 148 \ '0', 149 \ 'a122', 150 \ 'a123', 151 \ 'b123', 152 \ 'c123d', 153 \ ' 123b', 154 \ 'a321', 155 \ 'b321', 156 \ 'c321d', 157 \ 'b321', 158 \ 'b321b', 159 \ 'b322b' 160 \ ] 161 \ }, 162 \ { 163 \ 'name' : 'Hexadecimal sort', 164 \ 'cmd' : '%sort x', 165 \ 'input' : [ 166 \ 'abc', 167 \ 'ab', 168 \ 'a', 169 \ 'a321', 170 \ 'a123', 171 \ 'a122', 172 \ 'b321', 173 \ 'b123', 174 \ 'c123d', 175 \ ' 123b', 176 \ 'c321d', 177 \ 'b322b', 178 \ 'b321', 179 \ 'b321b' 180 \ ], 181 \ 'expected' : [ 182 \ 'a', 183 \ 'ab', 184 \ 'abc', 185 \ ' 123b', 186 \ 'a122', 187 \ 'a123', 188 \ 'a321', 189 \ 'b123', 190 \ 'b321', 191 \ 'b321', 192 \ 'b321b', 193 \ 'b322b', 194 \ 'c123d', 195 \ 'c321d' 196 \ ] 197 \ }, 198 \ { 199 \ 'name' : 'Alphabetical unique sort', 200 \ 'cmd' : '%sort u', 201 \ 'input' : [ 202 \ 'abc', 203 \ 'ab', 204 \ 'a', 205 \ 'a321', 206 \ 'a123', 207 \ 'a122', 208 \ 'b321', 209 \ 'b123', 210 \ 'c123d', 211 \ ' 123b', 212 \ 'c321d', 213 \ 'b322b', 214 \ 'b321', 215 \ 'b321b' 216 \ ], 217 \ 'expected' : [ 218 \ ' 123b', 219 \ 'a', 220 \ 'a122', 221 \ 'a123', 222 \ 'a321', 223 \ 'ab', 224 \ 'abc', 225 \ 'b123', 226 \ 'b321', 227 \ 'b321b', 228 \ 'b322b', 229 \ 'c123d', 230 \ 'c321d' 231 \ ] 232 \ }, 233 \ { 234 \ 'name' : 'Alphabetical reverse sort', 235 \ 'cmd' : '%sort!', 236 \ 'input' : [ 237 \ 'abc', 238 \ 'ab', 239 \ 'a', 240 \ 'a321', 241 \ 'a123', 242 \ 'a122', 243 \ 'b321', 244 \ 'b123', 245 \ 'c123d', 246 \ ' 123b', 247 \ 'c321d', 248 \ 'b322b', 249 \ 'b321', 250 \ 'b321b' 251 \ ], 252 \ 'expected' : [ 253 \ 'c321d', 254 \ 'c123d', 255 \ 'b322b', 256 \ 'b321b', 257 \ 'b321', 258 \ 'b321', 259 \ 'b123', 260 \ 'abc', 261 \ 'ab', 262 \ 'a321', 263 \ 'a123', 264 \ 'a122', 265 \ 'a', 266 \ ' 123b', 267 \ ] 268 \ }, 269 \ { 270 \ 'name' : 'Numeric reverse sort', 271 \ 'cmd' : '%sort! n', 272 \ 'input' : [ 273 \ 'abc', 274 \ 'ab', 275 \ 'a', 276 \ 'a321', 277 \ 'a123', 278 \ 'a122', 279 \ 'b321', 280 \ 'b123', 281 \ 'c123d', 282 \ ' 123b', 283 \ 'c321d', 284 \ 'b322b', 285 \ 'b321', 286 \ 'b321b' 287 \ ], 288 \ 'expected' : [ 289 \ 'b322b', 290 \ 'b321b', 291 \ 'b321', 292 \ 'c321d', 293 \ 'b321', 294 \ 'a321', 295 \ ' 123b', 296 \ 'c123d', 297 \ 'b123', 298 \ 'a123', 299 \ 'a122', 300 \ 'a', 301 \ 'ab', 302 \ 'abc' 303 \ ] 304 \ }, 305 \ { 306 \ 'name' : 'Unique reverse sort', 307 \ 'cmd' : 'sort! u', 308 \ 'input' : [ 309 \ 'abc', 310 \ 'ab', 311 \ 'a', 312 \ 'a321', 313 \ 'a123', 314 \ 'a122', 315 \ 'b321', 316 \ 'b123', 317 \ 'c123d', 318 \ ' 123b', 319 \ 'c321d', 320 \ 'b322b', 321 \ 'b321', 322 \ 'b321b' 323 \ ], 324 \ 'expected' : [ 325 \ 'c321d', 326 \ 'c123d', 327 \ 'b322b', 328 \ 'b321b', 329 \ 'b321', 330 \ 'b123', 331 \ 'abc', 332 \ 'ab', 333 \ 'a321', 334 \ 'a123', 335 \ 'a122', 336 \ 'a', 337 \ ' 123b', 338 \ ] 339 \ }, 340 \ { 341 \ 'name' : 'Octal sort', 342 \ 'cmd' : 'sort o', 343 \ 'input' : [ 344 \ 'abc', 345 \ 'ab', 346 \ 'a', 347 \ 'a321', 348 \ 'a123', 349 \ 'a122', 350 \ 'b321', 351 \ 'b123', 352 \ 'c123d', 353 \ ' 123b', 354 \ 'c321d', 355 \ 'b322b', 356 \ 'b321', 357 \ 'b321b', 358 \ '', 359 \ '' 360 \ ], 361 \ 'expected' : [ 362 \ 'abc', 363 \ 'ab', 364 \ 'a', 365 \ '', 366 \ '', 367 \ 'a122', 368 \ 'a123', 369 \ 'b123', 370 \ 'c123d', 371 \ ' 123b', 372 \ 'a321', 373 \ 'b321', 374 \ 'c321d', 375 \ 'b321', 376 \ 'b321b', 377 \ 'b322b' 378 \ ] 379 \ }, 380 \ { 381 \ 'name' : 'Reverse hexadecimal sort', 382 \ 'cmd' : 'sort! x', 383 \ 'input' : [ 384 \ 'abc', 385 \ 'ab', 386 \ 'a', 387 \ 'a321', 388 \ 'a123', 389 \ 'a122', 390 \ 'b321', 391 \ 'b123', 392 \ 'c123d', 393 \ ' 123b', 394 \ 'c321d', 395 \ 'b322b', 396 \ 'b321', 397 \ 'b321b', 398 \ '', 399 \ '' 400 \ ], 401 \ 'expected' : [ 402 \ 'c321d', 403 \ 'c123d', 404 \ 'b322b', 405 \ 'b321b', 406 \ 'b321', 407 \ 'b321', 408 \ 'b123', 409 \ 'a321', 410 \ 'a123', 411 \ 'a122', 412 \ ' 123b', 413 \ 'abc', 414 \ 'ab', 415 \ 'a', 416 \ '', 417 \ '' 418 \ ] 419 \ }, 420 \ { 421 \ 'name' : 'Alpha (skip first character) sort', 422 \ 'cmd' : 'sort/./', 423 \ 'input' : [ 424 \ 'abc', 425 \ 'ab', 426 \ 'a', 427 \ 'a321', 428 \ 'a123', 429 \ 'a122', 430 \ 'b321', 431 \ 'b123', 432 \ 'c123d', 433 \ ' 123b', 434 \ 'c321d', 435 \ 'b322b', 436 \ 'b321', 437 \ 'b321b', 438 \ '', 439 \ '' 440 \ ], 441 \ 'expected' : [ 442 \ 'a', 443 \ '', 444 \ '', 445 \ 'a122', 446 \ 'a123', 447 \ 'b123', 448 \ ' 123b', 449 \ 'c123d', 450 \ 'a321', 451 \ 'b321', 452 \ 'b321', 453 \ 'b321b', 454 \ 'c321d', 455 \ 'b322b', 456 \ 'ab', 457 \ 'abc' 458 \ ] 459 \ }, 460 \ { 461 \ 'name' : 'Alpha (skip first 2 characters) sort', 462 \ 'cmd' : 'sort/../', 463 \ 'input' : [ 464 \ 'abc', 465 \ 'ab', 466 \ 'a', 467 \ 'a321', 468 \ 'a123', 469 \ 'a122', 470 \ 'b321', 471 \ 'b123', 472 \ 'c123d', 473 \ ' 123b', 474 \ 'c321d', 475 \ 'b322b', 476 \ 'b321', 477 \ 'b321b', 478 \ '', 479 \ '' 480 \ ], 481 \ 'expected' : [ 482 \ 'ab', 483 \ 'a', 484 \ '', 485 \ '', 486 \ 'a321', 487 \ 'b321', 488 \ 'b321', 489 \ 'b321b', 490 \ 'c321d', 491 \ 'a122', 492 \ 'b322b', 493 \ 'a123', 494 \ 'b123', 495 \ ' 123b', 496 \ 'c123d', 497 \ 'abc' 498 \ ] 499 \ }, 500 \ { 501 \ 'name' : 'alpha, unique, skip first 2 characters', 502 \ 'cmd' : 'sort/../u', 503 \ 'input' : [ 504 \ 'abc', 505 \ 'ab', 506 \ 'a', 507 \ 'a321', 508 \ 'a123', 509 \ 'a122', 510 \ 'b321', 511 \ 'b123', 512 \ 'c123d', 513 \ ' 123b', 514 \ 'c321d', 515 \ 'b322b', 516 \ 'b321', 517 \ 'b321b', 518 \ '', 519 \ '' 520 \ ], 521 \ 'expected' : [ 522 \ 'ab', 523 \ 'a', 524 \ '', 525 \ 'a321', 526 \ 'b321', 527 \ 'b321b', 528 \ 'c321d', 529 \ 'a122', 530 \ 'b322b', 531 \ 'a123', 532 \ 'b123', 533 \ ' 123b', 534 \ 'c123d', 535 \ 'abc' 536 \ ] 537 \ }, 538 \ { 539 \ 'name' : 'numeric, skip first character', 540 \ 'cmd' : 'sort/./n', 541 \ 'input' : [ 542 \ 'abc', 543 \ 'ab', 544 \ 'a', 545 \ 'a321', 546 \ 'a123', 547 \ 'a122', 548 \ 'b321', 549 \ 'b123', 550 \ 'c123d', 551 \ ' 123b', 552 \ 'c321d', 553 \ 'b322b', 554 \ 'b321', 555 \ 'b321b', 556 \ '', 557 \ '' 558 \ ], 559 \ 'expected' : [ 560 \ 'abc', 561 \ 'ab', 562 \ 'a', 563 \ '', 564 \ '', 565 \ 'a122', 566 \ 'a123', 567 \ 'b123', 568 \ 'c123d', 569 \ ' 123b', 570 \ 'a321', 571 \ 'b321', 572 \ 'c321d', 573 \ 'b321', 574 \ 'b321b', 575 \ 'b322b' 576 \ ] 577 \ }, 578 \ { 579 \ 'name' : 'alpha, sort on first character', 580 \ 'cmd' : 'sort/./r', 581 \ 'input' : [ 582 \ 'abc', 583 \ 'ab', 584 \ 'a', 585 \ 'a321', 586 \ 'a123', 587 \ 'a122', 588 \ 'b321', 589 \ 'b123', 590 \ 'c123d', 591 \ ' 123b', 592 \ 'c321d', 593 \ 'b322b', 594 \ 'b321', 595 \ 'b321b', 596 \ '', 597 \ '' 598 \ ], 599 \ 'expected' : [ 600 \ '', 601 \ '', 602 \ ' 123b', 603 \ 'abc', 604 \ 'ab', 605 \ 'a', 606 \ 'a321', 607 \ 'a123', 608 \ 'a122', 609 \ 'b321', 610 \ 'b123', 611 \ 'b322b', 612 \ 'b321', 613 \ 'b321b', 614 \ 'c123d', 615 \ 'c321d' 616 \ ] 617 \ }, 618 \ { 619 \ 'name' : 'alpha, sort on first 2 characters', 620 \ 'cmd' : 'sort/../r', 621 \ 'input' : [ 622 \ 'abc', 623 \ 'ab', 624 \ 'a', 625 \ 'a321', 626 \ 'a123', 627 \ 'a122', 628 \ 'b321', 629 \ 'b123', 630 \ 'c123d', 631 \ ' 123b', 632 \ 'c321d', 633 \ 'b322b', 634 \ 'b321', 635 \ 'b321b', 636 \ '', 637 \ '' 638 \ ], 639 \ 'expected' : [ 640 \ 'a', 641 \ '', 642 \ '', 643 \ ' 123b', 644 \ 'a123', 645 \ 'a122', 646 \ 'a321', 647 \ 'abc', 648 \ 'ab', 649 \ 'b123', 650 \ 'b321', 651 \ 'b322b', 652 \ 'b321', 653 \ 'b321b', 654 \ 'c123d', 655 \ 'c321d' 656 \ ] 657 \ }, 658 \ { 659 \ 'name' : 'numeric, sort on first character', 660 \ 'cmd' : 'sort/./rn', 661 \ 'input' : [ 662 \ 'abc', 663 \ 'ab', 664 \ 'a', 665 \ 'a321', 666 \ 'a123', 667 \ 'a122', 668 \ 'b321', 669 \ 'b123', 670 \ 'c123d', 671 \ ' 123b', 672 \ 'c321d', 673 \ 'b322b', 674 \ 'b321', 675 \ 'b321b', 676 \ '', 677 \ '' 678 \ ], 679 \ 'expected' : [ 680 \ 'abc', 681 \ 'ab', 682 \ 'a', 683 \ 'a321', 684 \ 'a123', 685 \ 'a122', 686 \ 'b321', 687 \ 'b123', 688 \ 'c123d', 689 \ ' 123b', 690 \ 'c321d', 691 \ 'b322b', 692 \ 'b321', 693 \ 'b321b', 694 \ '', 695 \ '' 696 \ ] 697 \ }, 698 \ { 699 \ 'name' : 'alpha, skip past first digit', 700 \ 'cmd' : 'sort/\d/', 701 \ 'input' : [ 702 \ 'abc', 703 \ 'ab', 704 \ 'a', 705 \ 'a321', 706 \ 'a123', 707 \ 'a122', 708 \ 'b321', 709 \ 'b123', 710 \ 'c123d', 711 \ ' 123b', 712 \ 'c321d', 713 \ 'b322b', 714 \ 'b321', 715 \ 'b321b', 716 \ '', 717 \ '' 718 \ ], 719 \ 'expected' : [ 720 \ 'abc', 721 \ 'ab', 722 \ 'a', 723 \ '', 724 \ '', 725 \ 'a321', 726 \ 'b321', 727 \ 'b321', 728 \ 'b321b', 729 \ 'c321d', 730 \ 'a122', 731 \ 'b322b', 732 \ 'a123', 733 \ 'b123', 734 \ ' 123b', 735 \ 'c123d' 736 \ ] 737 \ }, 738 \ { 739 \ 'name' : 'alpha, sort on first digit', 740 \ 'cmd' : 'sort/\d/r', 741 \ 'input' : [ 742 \ 'abc', 743 \ 'ab', 744 \ 'a', 745 \ 'a321', 746 \ 'a123', 747 \ 'a122', 748 \ 'b321', 749 \ 'b123', 750 \ 'c123d', 751 \ ' 123b', 752 \ 'c321d', 753 \ 'b322b', 754 \ 'b321', 755 \ 'b321b', 756 \ '', 757 \ '' 758 \ ], 759 \ 'expected' : [ 760 \ 'abc', 761 \ 'ab', 762 \ 'a', 763 \ '', 764 \ '', 765 \ 'a123', 766 \ 'a122', 767 \ 'b123', 768 \ 'c123d', 769 \ ' 123b', 770 \ 'a321', 771 \ 'b321', 772 \ 'c321d', 773 \ 'b322b', 774 \ 'b321', 775 \ 'b321b' 776 \ ] 777 \ }, 778 \ { 779 \ 'name' : 'numeric, skip past first digit', 780 \ 'cmd' : 'sort/\d/n', 781 \ 'input' : [ 782 \ 'abc', 783 \ 'ab', 784 \ 'a', 785 \ 'a321', 786 \ 'a123', 787 \ 'a122', 788 \ 'b321', 789 \ 'b123', 790 \ 'c123d', 791 \ ' 123b', 792 \ 'c321d', 793 \ 'b322b', 794 \ 'b321', 795 \ 'b321b', 796 \ '', 797 \ '' 798 \ ], 799 \ 'expected' : [ 800 \ 'abc', 801 \ 'ab', 802 \ 'a', 803 \ '', 804 \ '', 805 \ 'a321', 806 \ 'b321', 807 \ 'c321d', 808 \ 'b321', 809 \ 'b321b', 810 \ 'a122', 811 \ 'b322b', 812 \ 'a123', 813 \ 'b123', 814 \ 'c123d', 815 \ ' 123b' 816 \ ] 817 \ }, 818 \ { 819 \ 'name' : 'numeric, sort on first digit', 820 \ 'cmd' : 'sort/\d/rn', 821 \ 'input' : [ 822 \ 'abc', 823 \ 'ab', 824 \ 'a', 825 \ 'a321', 826 \ 'a123', 827 \ 'a122', 828 \ 'b321', 829 \ 'b123', 830 \ 'c123d', 831 \ ' 123b', 832 \ 'c321d', 833 \ 'b322b', 834 \ 'b321', 835 \ 'b321b', 836 \ '', 837 \ '' 838 \ ], 839 \ 'expected' : [ 840 \ 'abc', 841 \ 'ab', 842 \ 'a', 843 \ '', 844 \ '', 845 \ 'a123', 846 \ 'a122', 847 \ 'b123', 848 \ 'c123d', 849 \ ' 123b', 850 \ 'a321', 851 \ 'b321', 852 \ 'c321d', 853 \ 'b322b', 854 \ 'b321', 855 \ 'b321b' 856 \ ] 857 \ }, 858 \ { 859 \ 'name' : 'alpha, skip past first 2 digits', 860 \ 'cmd' : 'sort/\d\d/', 861 \ 'input' : [ 862 \ 'abc', 863 \ 'ab', 864 \ 'a', 865 \ 'a321', 866 \ 'a123', 867 \ 'a122', 868 \ 'b321', 869 \ 'b123', 870 \ 'c123d', 871 \ ' 123b', 872 \ 'c321d', 873 \ 'b322b', 874 \ 'b321', 875 \ 'b321b', 876 \ '', 877 \ '' 878 \ ], 879 \ 'expected' : [ 880 \ 'abc', 881 \ 'ab', 882 \ 'a', 883 \ '', 884 \ '', 885 \ 'a321', 886 \ 'b321', 887 \ 'b321', 888 \ 'b321b', 889 \ 'c321d', 890 \ 'a122', 891 \ 'b322b', 892 \ 'a123', 893 \ 'b123', 894 \ ' 123b', 895 \ 'c123d' 896 \ ] 897 \ }, 898 \ { 899 \ 'name' : 'numeric, skip past first 2 digits', 900 \ 'cmd' : 'sort/\d\d/n', 901 \ 'input' : [ 902 \ 'abc', 903 \ 'ab', 904 \ 'a', 905 \ 'a321', 906 \ 'a123', 907 \ 'a122', 908 \ 'b321', 909 \ 'b123', 910 \ 'c123d', 911 \ ' 123b', 912 \ 'c321d', 913 \ 'b322b', 914 \ 'b321', 915 \ 'b321b', 916 \ '', 917 \ '' 918 \ ], 919 \ 'expected' : [ 920 \ 'abc', 921 \ 'ab', 922 \ 'a', 923 \ '', 924 \ '', 925 \ 'a321', 926 \ 'b321', 927 \ 'c321d', 928 \ 'b321', 929 \ 'b321b', 930 \ 'a122', 931 \ 'b322b', 932 \ 'a123', 933 \ 'b123', 934 \ 'c123d', 935 \ ' 123b' 936 \ ] 937 \ }, 938 \ { 939 \ 'name' : 'hexadecimal, skip past first 2 digits', 940 \ 'cmd' : 'sort/\d\d/x', 941 \ 'input' : [ 942 \ 'abc', 943 \ 'ab', 944 \ 'a', 945 \ 'a321', 946 \ 'a123', 947 \ 'a122', 948 \ 'b321', 949 \ 'b123', 950 \ 'c123d', 951 \ ' 123b', 952 \ 'c321d', 953 \ 'b322b', 954 \ 'b321', 955 \ 'b321b', 956 \ '', 957 \ '' 958 \ ], 959 \ 'expected' : [ 960 \ 'abc', 961 \ 'ab', 962 \ 'a', 963 \ '', 964 \ '', 965 \ 'a321', 966 \ 'b321', 967 \ 'b321', 968 \ 'a122', 969 \ 'a123', 970 \ 'b123', 971 \ 'b321b', 972 \ 'c321d', 973 \ 'b322b', 974 \ ' 123b', 975 \ 'c123d' 976 \ ] 977 \ }, 978 \ { 979 \ 'name' : 'alpha, sort on first 2 digits', 980 \ 'cmd' : 'sort/\d\d/r', 981 \ 'input' : [ 982 \ 'abc', 983 \ 'ab', 984 \ 'a', 985 \ 'a321', 986 \ 'a123', 987 \ 'a122', 988 \ 'b321', 989 \ 'b123', 990 \ 'c123d', 991 \ ' 123b', 992 \ 'c321d', 993 \ 'b322b', 994 \ 'b321', 995 \ 'b321b', 996 \ '', 997 \ '' 998 \ ], 999 \ 'expected' : [ 1000 \ 'abc', 1001 \ 'ab', 1002 \ 'a', 1003 \ '', 1004 \ '', 1005 \ 'a123', 1006 \ 'a122', 1007 \ 'b123', 1008 \ 'c123d', 1009 \ ' 123b', 1010 \ 'a321', 1011 \ 'b321', 1012 \ 'c321d', 1013 \ 'b322b', 1014 \ 'b321', 1015 \ 'b321b' 1016 \ ] 1017 \ }, 1018 \ { 1019 \ 'name' : 'numeric, sort on first 2 digits', 1020 \ 'cmd' : 'sort/\d\d/rn', 1021 \ 'input' : [ 1022 \ 'abc', 1023 \ 'ab', 1024 \ 'a', 1025 \ 'a321', 1026 \ 'a123', 1027 \ 'a122', 1028 \ 'b321', 1029 \ 'b123', 1030 \ 'c123d', 1031 \ ' 123b', 1032 \ 'c321d', 1033 \ 'b322b', 1034 \ 'b321', 1035 \ 'b321b', 1036 \ '', 1037 \ '' 1038 \ ], 1039 \ 'expected' : [ 1040 \ 'abc', 1041 \ 'ab', 1042 \ 'a', 1043 \ '', 1044 \ '', 1045 \ 'a123', 1046 \ 'a122', 1047 \ 'b123', 1048 \ 'c123d', 1049 \ ' 123b', 1050 \ 'a321', 1051 \ 'b321', 1052 \ 'c321d', 1053 \ 'b322b', 1054 \ 'b321', 1055 \ 'b321b' 1056 \ ] 1057 \ }, 1058 \ { 1059 \ 'name' : 'hexadecimal, sort on first 2 digits', 1060 \ 'cmd' : 'sort/\d\d/rx', 1061 \ 'input' : [ 1062 \ 'abc', 1063 \ 'ab', 1064 \ 'a', 1065 \ 'a321', 1066 \ 'a123', 1067 \ 'a122', 1068 \ 'b321', 1069 \ 'b123', 1070 \ 'c123d', 1071 \ ' 123b', 1072 \ 'c321d', 1073 \ 'b322b', 1074 \ 'b321', 1075 \ 'b321b', 1076 \ '', 1077 \ '' 1078 \ ], 1079 \ 'expected' : [ 1080 \ 'abc', 1081 \ 'ab', 1082 \ 'a', 1083 \ '', 1084 \ '', 1085 \ 'a123', 1086 \ 'a122', 1087 \ 'b123', 1088 \ 'c123d', 1089 \ ' 123b', 1090 \ 'a321', 1091 \ 'b321', 1092 \ 'c321d', 1093 \ 'b322b', 1094 \ 'b321', 1095 \ 'b321b' 1096 \ ] 1097 \ }, 1098 \ { 1099 \ 'name' : 'binary', 1100 \ 'cmd' : 'sort b', 1101 \ 'input' : [ 1102 \ '0b111000', 1103 \ '0b101100', 1104 \ '0b101001', 1105 \ '0b101001', 1106 \ '0b101000', 1107 \ '0b000000', 1108 \ '0b001000', 1109 \ '0b010000', 1110 \ '0b101000', 1111 \ '0b100000', 1112 \ '0b101010', 1113 \ '0b100010', 1114 \ '0b100100', 1115 \ '0b100010', 1116 \ '', 1117 \ '' 1118 \ ], 1119 \ 'expected' : [ 1120 \ '', 1121 \ '', 1122 \ '0b000000', 1123 \ '0b001000', 1124 \ '0b010000', 1125 \ '0b100000', 1126 \ '0b100010', 1127 \ '0b100010', 1128 \ '0b100100', 1129 \ '0b101000', 1130 \ '0b101000', 1131 \ '0b101001', 1132 \ '0b101001', 1133 \ '0b101010', 1134 \ '0b101100', 1135 \ '0b111000' 1136 \ ] 1137 \ }, 1138 \ { 1139 \ 'name' : 'binary with leading characters', 1140 \ 'cmd' : 'sort b', 1141 \ 'input' : [ 1142 \ '0b100010', 1143 \ '0b010000', 1144 \ ' 0b101001', 1145 \ 'b0b101100', 1146 \ '0b100010', 1147 \ ' 0b100100', 1148 \ 'a0b001000', 1149 \ '0b101000', 1150 \ '0b101000', 1151 \ 'a0b101001', 1152 \ 'ab0b100000', 1153 \ '0b101010', 1154 \ '0b000000', 1155 \ 'b0b111000', 1156 \ '', 1157 \ '' 1158 \ ], 1159 \ 'expected' : [ 1160 \ '', 1161 \ '', 1162 \ '0b000000', 1163 \ 'a0b001000', 1164 \ '0b010000', 1165 \ 'ab0b100000', 1166 \ '0b100010', 1167 \ '0b100010', 1168 \ ' 0b100100', 1169 \ '0b101000', 1170 \ '0b101000', 1171 \ ' 0b101001', 1172 \ 'a0b101001', 1173 \ '0b101010', 1174 \ 'b0b101100', 1175 \ 'b0b111000' 1176 \ ] 1177 \ }, 1178 \ { 1179 \ 'name' : 'alphabetical, sorted input', 1180 \ 'cmd' : 'sort', 1181 \ 'input' : [ 1182 \ 'a', 1183 \ 'b', 1184 \ 'c', 1185 \ ], 1186 \ 'expected' : [ 1187 \ 'a', 1188 \ 'b', 1189 \ 'c', 1190 \ ] 1191 \ }, 1192 \ { 1193 \ 'name' : 'alphabetical, sorted input, unique at end', 1194 \ 'cmd' : 'sort u', 1195 \ 'input' : [ 1196 \ 'aa', 1197 \ 'bb', 1198 \ 'cc', 1199 \ 'cc', 1200 \ ], 1201 \ 'expected' : [ 1202 \ 'aa', 1203 \ 'bb', 1204 \ 'cc', 1205 \ ] 1206 \ }, 1207 \ { 1208 \ 'name' : 'sort one line buffer', 1209 \ 'cmd' : 'sort', 1210 \ 'input' : [ 1211 \ 'single line' 1212 \ ], 1213 \ 'expected' : [ 1214 \ 'single line' 1215 \ ] 1216 \ }, 1217 \ { 1218 \ 'name' : 'sort ignoring case', 1219 \ 'cmd' : '%sort i', 1220 \ 'input' : [ 1221 \ 'BB', 1222 \ 'Cc', 1223 \ 'aa' 1224 \ ], 1225 \ 'expected' : [ 1226 \ 'aa', 1227 \ 'BB', 1228 \ 'Cc' 1229 \ ] 1230 \ }, 1231 \ ] 1232 1233 " With the following locales, the accentuated letters are ordered 1234 " similarly to the non-accentuated letters. 1235 " This does not appear to work on Mac 1236 if v:collate =~? '^\(en\|es\|de\|fr\|it\|nl\).*\.utf-\?8$' && !has('mac') 1237 let tests += [ 1238 \ { 1239 \ 'name' : 'sort with locale', 1240 \ 'cmd' : '%sort l', 1241 \ 'input' : [ 1242 \ 'A', 1243 \ 'E', 1244 \ 'O', 1245 \ 'À', 1246 \ 'È', 1247 \ 'É', 1248 \ 'Ô', 1249 \ 'Œ', 1250 \ 'Z', 1251 \ 'a', 1252 \ 'e', 1253 \ 'o', 1254 \ 'à', 1255 \ 'è', 1256 \ 'é', 1257 \ 'ô', 1258 \ 'œ', 1259 \ 'z' 1260 \ ], 1261 \ 'expected' : [ 1262 \ 'a', 1263 \ 'A', 1264 \ 'à', 1265 \ 'À', 1266 \ 'e', 1267 \ 'E', 1268 \ 'é', 1269 \ 'É', 1270 \ 'è', 1271 \ 'È', 1272 \ 'o', 1273 \ 'O', 1274 \ 'ô', 1275 \ 'Ô', 1276 \ 'œ', 1277 \ 'Œ', 1278 \ 'z', 1279 \ 'Z' 1280 \ ] 1281 \ }, 1282 \ ] 1283 endif 1284 if has('float') 1285 let tests += [ 1286 \ { 1287 \ 'name' : 'float', 1288 \ 'cmd' : 'sort f', 1289 \ 'input' : [ 1290 \ '1.234', 1291 \ '0.88', 1292 \ ' + 123.456', 1293 \ '1.15e-6', 1294 \ '-1.1e3', 1295 \ '-1.01e3', 1296 \ '', 1297 \ '' 1298 \ ], 1299 \ 'expected' : [ 1300 \ '', 1301 \ '', 1302 \ '-1.1e3', 1303 \ '-1.01e3', 1304 \ '1.15e-6', 1305 \ '0.88', 1306 \ '1.234', 1307 \ ' + 123.456' 1308 \ ] 1309 \ }, 1310 \ ] 1311 endif 1312 1313 for t in tests 1314 enew! 1315 call append(0, t.input) 1316 $delete _ 1317 setlocal nomodified 1318 execute t.cmd 1319 1320 call assert_equal(t.expected, getline(1, '$'), t.name) 1321 1322 " Previously, the ":sort" command would set 'modified' even if the buffer 1323 " contents did not change. Here, we check that this problem is fixed. 1324 if t.input == t.expected 1325 call assert_false(&modified, t.name . ': &mod is not correct') 1326 else 1327 call assert_true(&modified, t.name . ': &mod is not correct') 1328 endif 1329 endfor 1330 1331 " Needs atleast two lines for this test 1332 call setline(1, ['line1', 'line2']) 1333 call assert_fails('sort no', 'E474:') 1334 call assert_fails('sort c', 'E475:') 1335 call assert_fails('sort #pat%', 'E654:') 1336 call assert_fails('sort /\%(/', 'E53:') 1337 1338 enew! 1339endfunc 1340 1341func Test_sort_large_num() 1342 new 1343 a 1344-2147483648 1345-2147483647 1346 1347-1 13480 13491 1350-2147483646 13512147483646 13522147483647 13532147483647 1354-2147483648 1355abc 1356 1357. 1358 " Numerical sort. Non-numeric lines are ordered before numerical lines. 1359 " Ordering of non-numerical is stable. 1360 sort n 1361 call assert_equal(['', 1362 \ 'abc', 1363 \ '', 1364 \ '-2147483648', 1365 \ '-2147483648', 1366 \ '-2147483647', 1367 \ '-2147483646', 1368 \ '-1', 1369 \ '0', 1370 \ '1', 1371 \ '2147483646', 1372 \ '2147483647', 1373 \ '2147483647'], getline(1, '$')) 1374 bwipe! 1375 1376 new 1377 a 1378-9223372036854775808 1379-9223372036854775807 1380 1381-1 13820 13831 1384-9223372036854775806 13859223372036854775806 13869223372036854775807 13879223372036854775807 1388-9223372036854775808 1389abc 1390 1391. 1392 sort n 1393 call assert_equal(['', 1394 \ 'abc', 1395 \ '', 1396 \ '-9223372036854775808', 1397 \ '-9223372036854775808', 1398 \ '-9223372036854775807', 1399 \ '-9223372036854775806', 1400 \ '-1', 1401 \ '0', 1402 \ '1', 1403 \ '9223372036854775806', 1404 \ '9223372036854775807', 1405 \ '9223372036854775807'], getline(1, '$')) 1406 bwipe! 1407endfunc 1408 1409 1410func Test_sort_cmd_report() 1411 enew! 1412 call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3)) 1413 $delete _ 1414 setlocal nomodified 1415 let res = execute('%sort u') 1416 1417 call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0')) 1418 call assert_match("6 fewer lines", res) 1419 enew! 1420 call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3)) 1421 $delete _ 1422 setlocal nomodified report=10 1423 let res = execute('%sort u') 1424 1425 call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0')) 1426 call assert_equal("", res) 1427 enew! 1428 call append(0, repeat([1], 3) + repeat([2], 3) + repeat([3], 3)) 1429 $delete _ 1430 setl report&vim 1431 setlocal nomodified 1432 let res = execute('1g/^/%sort u') 1433 1434 call assert_equal([1,2,3], map(getline(1, '$'), 'v:val+0')) 1435 " the output comes from the :g command, not from the :sort 1436 call assert_match("6 fewer lines", res) 1437 enew! 1438endfunc 1439 1440" Test for a :sort command followed by another command 1441func Test_sort_followed_by_cmd() 1442 new 1443 let var = '' 1444 call setline(1, ['cc', 'aa', 'bb']) 1445 %sort | let var = "sortcmdtest" 1446 call assert_equal(var, "sortcmdtest") 1447 call assert_equal(['aa', 'bb', 'cc'], getline(1, '$')) 1448 " Test for :sort followed by a comment 1449 call setline(1, ['3b', '1c', '2a']) 1450 %sort /\d\+/ " sort alphabetically 1451 call assert_equal(['2a', '3b', '1c'], getline(1, '$')) 1452 close! 1453endfunc 1454 1455" Test for :sort using last search pattern 1456func Test_sort_last_search_pat() 1457 new 1458 let @/ = '\d\+' 1459 call setline(1, ['3b', '1c', '2a']) 1460 sort // 1461 call assert_equal(['2a', '3b', '1c'], getline(1, '$')) 1462 close! 1463endfunc 1464 1465" Test for :sort with no last search pattern 1466func Test_sort_with_no_last_search_pat() 1467 let lines =<< trim [SCRIPT] 1468 call setline(1, ['3b', '1c', '2a']) 1469 call assert_fails('sort //', 'E35:') 1470 call writefile(v:errors, 'Xresult') 1471 qall! 1472 [SCRIPT] 1473 call writefile(lines, 'Xscript') 1474 if RunVim([], [], '--clean -S Xscript') 1475 call assert_equal([], readfile('Xresult')) 1476 endif 1477 call delete('Xscript') 1478 call delete('Xresult') 1479endfunc 1480 1481" Test for retaining marks across a :sort 1482func Test_sort_with_marks() 1483 new 1484 call setline(1, ['cc', 'aa', 'bb']) 1485 call setpos("'c", [0, 1, 0, 0]) 1486 call setpos("'a", [0, 2, 0, 0]) 1487 call setpos("'b", [0, 3, 0, 0]) 1488 %sort 1489 call assert_equal(['aa', 'bb', 'cc'], getline(1, '$')) 1490 call assert_equal(2, line("'a")) 1491 call assert_equal(3, line("'b")) 1492 call assert_equal(1, line("'c")) 1493 close! 1494endfunc 1495 1496" vim: shiftwidth=2 sts=2 expandtab 1497