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