1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
|
#!perl
BEGIN {
unshift @INC, 't';
require Config;
if (($Config::Config{'extensions'} !~ /\bB\b/) ){
print "1..0 # Skip -- Perl configured without B module\n";
exit 0;
}
}
use OptreeCheck;
plan tests => 38;
=head1 f_sort.t
Code test snippets here are adapted from `perldoc -f map`
=head1 Test Notes
# chunk: #!perl
#examples poached from perldoc -f sort
NOTE: name is no longer a required arg for checkOptree, as label is
synthesized out of others. HOWEVER, if the test-code has newlines in
it, the label must be overridden by an explicit name.
This is because t/TEST is quite particular about the test output it
processes, and multi-line labels violate its 1-line-per-test
expectations.
=for gentest
# chunk: # sort lexically
@articles = sort @files;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{@articles = sort @files; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 545 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*files] s
# 5 <1> rv2av[t4] lK/1
# 6 <@> sort lK
# 7 <0> pushmark s
# 8 <#> gv[*articles] s
# 9 <1> rv2av[t2] lKRM*/1
# a <2> aassign[t5] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 545 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*files) s
# 5 <1> rv2av[t2] lK/1
# 6 <@> sort lK
# 7 <0> pushmark s
# 8 <$> gv(*articles) s
# 9 <1> rv2av[t1] lKRM*/1
# a <2> aassign[t3] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # same thing, but with explicit sort routine
@articles = sort {$a cmp $b} @files;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{@articles = sort {$a cmp $b} @files; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*files] s
# 5 <1> rv2av[t7] lK/1
# 6 <@> sort lK
# 7 <0> pushmark s
# 8 <#> gv[*articles] s
# 9 <1> rv2av[t2] lKRM*/1
# a <2> aassign[t3] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*files) s
# 5 <1> rv2av[t3] lK/1
# 6 <@> sort lK
# 7 <0> pushmark s
# 8 <$> gv(*articles) s
# 9 <1> rv2av[t1] lKRM*/1
# a <2> aassign[t2] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # now case-insensitively
@articles = sort {uc($a) cmp uc($b)} @files;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{@articles = sort {uc($a) cmp uc($b)} @files; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*files] s
# 5 <1> rv2av[t9] lKM/1
# 6 <@> sort lKS*
# 7 <0> pushmark s
# 8 <#> gv[*articles] s
# 9 <1> rv2av[t2] lKRM*/1
# a <2> aassign[t10] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*files) s
# 5 <1> rv2av[t5] lKM/1
# 6 <@> sort lKS*
# 7 <0> pushmark s
# 8 <$> gv(*articles) s
# 9 <1> rv2av[t1] lKRM*/1
# a <2> aassign[t6] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # same thing in reversed order
@articles = sort {$b cmp $a} @files;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{@articles = sort {$b cmp $a} @files; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*files] s
# 5 <1> rv2av[t7] lK/1
# 6 <@> sort lK/DESC
# 7 <0> pushmark s
# 8 <#> gv[*articles] s
# 9 <1> rv2av[t2] lKRM*/1
# a <2> aassign[t3] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*files) s
# 5 <1> rv2av[t3] lK/1
# 6 <@> sort lK/DESC
# 7 <0> pushmark s
# 8 <$> gv(*articles) s
# 9 <1> rv2av[t1] lKRM*/1
# a <2> aassign[t2] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # sort numerically ascending
@articles = sort {$a <=> $b} @files;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{@articles = sort {$a <=> $b} @files; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*files] s
# 5 <1> rv2av[t7] lK/1
# 6 <@> sort lK/NUM
# 7 <0> pushmark s
# 8 <#> gv[*articles] s
# 9 <1> rv2av[t2] lKRM*/1
# a <2> aassign[t3] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*files) s
# 5 <1> rv2av[t3] lK/1
# 6 <@> sort lK/NUM
# 7 <0> pushmark s
# 8 <$> gv(*articles) s
# 9 <1> rv2av[t1] lKRM*/1
# a <2> aassign[t2] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # sort numerically descending
@articles = sort {$b <=> $a} @files;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{@articles = sort {$b <=> $a} @files; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 587 (eval 26):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*files] s
# 5 <1> rv2av[t7] lK/1
# 6 <@> sort lK/DESC,NUM
# 7 <0> pushmark s
# 8 <#> gv[*articles] s
# 9 <1> rv2av[t2] lKRM*/1
# a <2> aassign[t3] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*files) s
# 5 <1> rv2av[t3] lK/1
# 6 <@> sort lK/DESC,NUM
# 7 <0> pushmark s
# 8 <$> gv(*articles) s
# 9 <1> rv2av[t1] lKRM*/1
# a <2> aassign[t2] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # this sorts the %age hash by value instead of key
# using an in-line function
@eldest = sort { $age{$b} <=> $age{$a} } keys %age;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{@eldest = sort { $age{$b} <=> $age{$a} } keys %age; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 592 (eval 28):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*age] s
# 5 <1> rv2hv[t9] lKRM
# 6 <1> keys[t10] lKM/1
# 7 <@> sort lKS*
# 8 <0> pushmark s
# 9 <#> gv[*eldest] s
# a <1> rv2av[t2] lKRM*/1
# b <2> aassign[t11] KS/COM_AGG
# c <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*age) s
# 5 <1> rv2hv[t3] lKRM
# 6 <1> keys[t4] lKM/1
# 7 <@> sort lKS*
# 8 <0> pushmark s
# 9 <$> gv(*eldest) s
# a <1> rv2av[t1] lKRM*/1
# b <2> aassign[t5] KS/COM_AGG
# c <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # sort using explicit subroutine name
sub byage {
$age{$a} <=> $age{$b}; # presuming numeric
}
@sortedclass = sort byage @class;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{sub byage { $age{$a} <=> $age{$b}; } @sortedclass = sort byage @class; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 597 (eval 30):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> const[PV "byage"] s/BARE
# 5 <#> gv[*class] s
# 6 <1> rv2av[t4] lKM/1
# 7 <@> sort lKS
# 8 <0> pushmark s
# 9 <#> gv[*sortedclass] s
# a <1> rv2av[t2] lKRM*/1
# b <2> aassign[t5] KS/COM_AGG
# c <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> const(PV "byage") s/BARE
# 5 <$> gv(*class) s
# 6 <1> rv2av[t2] lKM/1
# 7 <@> sort lKS
# 8 <0> pushmark s
# 9 <$> gv(*sortedclass) s
# a <1> rv2av[t1] lKRM*/1
# b <2> aassign[t3] KS/COM_AGG
# c <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: sub backwards { $b cmp $a }
@harry = qw(dog cat x Cain Abel);
@george = qw(gone chased yz Punished Axed);
print sort @harry;
# prints AbelCaincatdogx
print sort backwards @harry;
# prints xdogcatCainAbel
print sort @george, 'to', @harry;
# prints AbelAxedCainPunishedcatchaseddoggonetoxyz
=cut
checkOptree(name => q{sort USERSUB LIST },
bcopts => q{-exec},
code => q{sub backwards { $b cmp $a }
@harry = qw(dog cat x Cain Abel);
@george = qw(gone chased yz Punished Axed);
print sort @harry; print sort backwards @harry;
print sort @george, 'to', @harry; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 602 (eval 32):2) v
# 2 <0> pushmark s
# 3 <$> const[PV "dog"] s
# 4 <$> const[PV "cat"] s
# 5 <$> const[PV "x"] s
# 6 <$> const[PV "Cain"] s
# 7 <$> const[PV "Abel"] s
# 8 <0> pushmark s
# 9 <#> gv[*harry] s
# a <1> rv2av[t2] lKRM*/1
# b <2> aassign[t3] vKS
# c <;> nextstate(main 602 (eval 32):3) v
# d <0> pushmark s
# e <$> const[PV "gone"] s
# f <$> const[PV "chased"] s
# g <$> const[PV "yz"] s
# h <$> const[PV "Punished"] s
# i <$> const[PV "Axed"] s
# j <0> pushmark s
# k <#> gv[*george] s
# l <1> rv2av[t5] lKRM*/1
# m <2> aassign[t6] vKS
# n <;> nextstate(main 602 (eval 32):4) v:{
# o <0> pushmark s
# p <0> pushmark s
# q <#> gv[*harry] s
# r <1> rv2av[t8] lK/1
# s <@> sort lK
# t <@> print vK
# u <;> nextstate(main 602 (eval 32):4) v:{
# v <0> pushmark s
# w <0> pushmark s
# x <$> const[PV "backwards"] s/BARE
# y <#> gv[*harry] s
# z <1> rv2av[t10] lKM/1
# 10 <@> sort lKS
# 11 <@> print vK
# 12 <;> nextstate(main 602 (eval 32):5) v:{
# 13 <0> pushmark s
# 14 <0> pushmark s
# 15 <#> gv[*george] s
# 16 <1> rv2av[t12] lK/1
# 17 <$> const[PV "to"] s
# 18 <#> gv[*harry] s
# 19 <1> rv2av[t14] lK/1
# 1a <@> sort lK
# 1b <@> print sK
# 1c <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 602 (eval 32):2) v
# 2 <0> pushmark s
# 3 <$> const(PV "dog") s
# 4 <$> const(PV "cat") s
# 5 <$> const(PV "x") s
# 6 <$> const(PV "Cain") s
# 7 <$> const(PV "Abel") s
# 8 <0> pushmark s
# 9 <$> gv(*harry) s
# a <1> rv2av[t1] lKRM*/1
# b <2> aassign[t2] vKS
# c <;> nextstate(main 602 (eval 32):3) v
# d <0> pushmark s
# e <$> const(PV "gone") s
# f <$> const(PV "chased") s
# g <$> const(PV "yz") s
# h <$> const(PV "Punished") s
# i <$> const(PV "Axed") s
# j <0> pushmark s
# k <$> gv(*george) s
# l <1> rv2av[t3] lKRM*/1
# m <2> aassign[t4] vKS
# n <;> nextstate(main 602 (eval 32):4) v:{
# o <0> pushmark s
# p <0> pushmark s
# q <$> gv(*harry) s
# r <1> rv2av[t5] lK/1
# s <@> sort lK
# t <@> print vK
# u <;> nextstate(main 602 (eval 32):4) v:{
# v <0> pushmark s
# w <0> pushmark s
# x <$> const(PV "backwards") s/BARE
# y <$> gv(*harry) s
# z <1> rv2av[t6] lKM/1
# 10 <@> sort lKS
# 11 <@> print vK
# 12 <;> nextstate(main 602 (eval 32):5) v:{
# 13 <0> pushmark s
# 14 <0> pushmark s
# 15 <$> gv(*george) s
# 16 <1> rv2av[t7] lK/1
# 17 <$> const(PV "to") s
# 18 <$> gv(*harry) s
# 19 <1> rv2av[t8] lK/1
# 1a <@> sort lK
# 1b <@> print sK
# 1c <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # inefficiently sort by descending numeric compare using
# the first integer after the first = sign, or the
# whole record case-insensitively otherwise
@new = @old[ sort {
$nums[$b] <=> $nums[$a]
|| $caps[$a] cmp $caps[$b]
} 0..$#old ];
=cut
=for gentest
# chunk: # same thing, but without any temps
@new = map { $_->[0] }
sort { $b->[1] <=> $a->[1]
|| $a->[2] cmp $b->[2]
} map { [$_, /=(\d+)/, uc($_)] } @old;
=cut
checkOptree(name => q{Compound sort/map Expression },
bcopts => q{-exec},
code => q{ @new = map { $_->[0] }
sort { $b->[1] <=> $a->[1] || $a->[2] cmp $b->[2] }
map { [$_, /=(\d+)/, uc($_)] } @old; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 609 (eval 34):3) v:{
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <0> pushmark s
# 5 <0> pushmark s
# 6 <#> gv[*old] s
# 7 <1> rv2av[t19] lKM/1
# 8 <@> mapstart lK
# 9 <|> mapwhile(other->a)[t20] lKM
# a <0> enter l
# b <;> nextstate(main 608 (eval 34):2) v:{
# c <0> pushmark s
# d <#> gvsv[*_] s
# e </> match(/"=(\\d+)"/) l
# f <#> gvsv[*_] s
# g <1> uc[t17] sK/1
# h <@> anonlist sK*/1
# i <@> leave lKP
# goto 9
# j <@> sort lKMS*
# k <@> mapstart lK
# l <|> mapwhile(other->m)[t26] lK
# m <+> multideref($_->[0]) sK
# goto l
# n <0> pushmark s
# o <#> gv[*new] s
# p <1> rv2av[t2] lKRM*/1
# q <2> aassign[t22] KS/COM_AGG
# r <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 609 (eval 34):3) v:{
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <0> pushmark s
# 5 <0> pushmark s
# 6 <$> gv(*old) s
# 7 <1> rv2av[t10] lKM/1
# 8 <@> mapstart lK
# 9 <|> mapwhile(other->a)[t11] lKM
# a <0> enter l
# b <;> nextstate(main 608 (eval 34):2) v:{
# c <0> pushmark s
# d <$> gvsv(*_) s
# e </> match(/"=(\\d+)"/) l
# f <$> gvsv(*_) s
# g <1> uc[t9] sK/1
# h <@> anonlist sK*/1
# i <@> leave lKP
# goto 9
# j <@> sort lKMS*
# k <@> mapstart lK
# l <|> mapwhile(other->m)[t12] lK
# m <+> multideref($_->[0]) sK
# goto l
# n <0> pushmark s
# o <$> gv(*new) s
# p <1> rv2av[t1] lKRM*/1
# q <2> aassign[t13] KS/COM_AGG
# r <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # using a prototype allows you to use any comparison subroutine
# as a sort subroutine (including other package's subroutines)
package other;
sub backwards ($$) { $_[1] cmp $_[0]; } # $a and $b are not set here
package main;
@new = sort other::backwards @old;
=cut
checkOptree(name => q{sort other::sub LIST },
bcopts => q{-exec},
code => q{package other; sub backwards ($$) { $_[1] cmp $_[0]; }
package main; @new = sort other::backwards @old; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 614 (eval 36):2) v:{
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> const[PV "other::backwards"] s/BARE
# 5 <#> gv[*old] s
# 6 <1> rv2av[t4] lKM/1
# 7 <@> sort lKS
# 8 <0> pushmark s
# 9 <#> gv[*new] s
# a <1> rv2av[t2] lKRM*/1
# b <2> aassign[t5] KS/COM_AGG
# c <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 614 (eval 36):2) v:{
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> const(PV "other::backwards") s/BARE
# 5 <$> gv(*old) s
# 6 <1> rv2av[t2] lKM/1
# 7 <@> sort lKS
# 8 <0> pushmark s
# 9 <$> gv(*new) s
# a <1> rv2av[t1] lKRM*/1
# b <2> aassign[t3] KS/COM_AGG
# c <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # repeat, condensed. $main::a and $b are unaffected
sub other::backwards ($$) { $_[1] cmp $_[0]; }
@new = sort other::backwards @old;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{sub other::backwards ($$) { $_[1] cmp $_[0]; } @new = sort other::backwards @old; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 619 (eval 38):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> const[PV "other::backwards"] s/BARE
# 5 <#> gv[*old] s
# 6 <1> rv2av[t4] lKM/1
# 7 <@> sort lKS
# 8 <0> pushmark s
# 9 <#> gv[*new] s
# a <1> rv2av[t2] lKRM*/1
# b <2> aassign[t5] KS/COM_AGG
# c <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> const(PV "other::backwards") s/BARE
# 5 <$> gv(*old) s
# 6 <1> rv2av[t2] lKM/1
# 7 <@> sort lKS
# 8 <0> pushmark s
# 9 <$> gv(*new) s
# a <1> rv2av[t1] lKRM*/1
# b <2> aassign[t3] KS/COM_AGG
# c <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # guarantee stability, regardless of algorithm
use sort 'stable';
@new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old;
=cut
my ($expect, $expect_nt) = (<<'EOT_EOT', <<'EONT_EONT');
# 1 <;> nextstate(main 656 (eval 40):1) v:%,{
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*old] s
# 5 <1> rv2av[t9] lKM/1
# 6 <@> sort lKS*
# 7 <0> pushmark s
# 8 <#> gv[*new] s
# 9 <1> rv2av[t2] lKRM*/1
# a <2> aassign[t14] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 578 (eval 15):1) v:%,{
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*old) s
# 5 <1> rv2av[t5] lKM/1
# 6 <@> sort lKS*
# 7 <0> pushmark s
# 8 <$> gv(*new) s
# 9 <1> rv2av[t1] lKRM*/1
# a <2> aassign[t6] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EONT_EONT
checkOptree(note => q{},
bcopts => q{-exec},
code => q{use sort 'stable'; @new = sort { substr($a, 3, 5) cmp substr($b, 3, 5) } @old; },
expect => $expect, expect_nt => $expect_nt);
=for gentest
# chunk: # you should have a good reason to do this!
@articles = sort {$FooPack::b <=> $FooPack::a} @files;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{@articles = sort {$FooPack::b <=> $FooPack::a} @files; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 667 (eval 44):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*files] s
# 5 <1> rv2av[t7] lKM/1
# 6 <@> sort lKS*
# 7 <0> pushmark s
# 8 <#> gv[*articles] s
# 9 <1> rv2av[t2] lKRM*/1
# a <2> aassign[t8] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*files) s
# 5 <1> rv2av[t3] lKM/1
# 6 <@> sort lKS*
# 7 <0> pushmark s
# 8 <$> gv(*articles) s
# 9 <1> rv2av[t1] lKRM*/1
# a <2> aassign[t4] KS/COM_AGG
# b <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # fancy
@result = sort { $a <=> $b } grep { $_ == $_ } @input;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{@result = sort { $a <=> $b } grep { $_ == $_ } @input; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 673 (eval 46):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <0> pushmark s
# 5 <#> gv[*input] s
# 6 <1> rv2av[t9] lKM/1
# 7 <@> grepstart lK
# 8 <|> grepwhile(other->9)[t10] lK
# 9 <#> gvsv[*_] s
# a <#> gvsv[*_] s
# b <2> eq sK/2
# goto 8
# c <@> sort lK/NUM
# d <0> pushmark s
# e <#> gv[*result] s
# f <1> rv2av[t2] lKRM*/1
# g <2> aassign[t3] KS/COM_AGG
# h <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 547 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <0> pushmark s
# 5 <$> gv(*input) s
# 6 <1> rv2av[t3] lKM/1
# 7 <@> grepstart lK
# 8 <|> grepwhile(other->9)[t4] lK
# 9 <$> gvsv(*_) s
# a <$> gvsv(*_) s
# b <2> eq sK/2
# goto 8
# c <@> sort lK/NUM
# d <0> pushmark s
# e <$> gv(*result) s
# f <1> rv2av[t1] lKRM*/1
# g <2> aassign[t2] KS/COM_AGG
# h <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # void return context sort
sort { $a <=> $b } @input;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{sort { $a <=> $b } @input; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 678 (eval 48):1) v
# 2 <0> pushmark s
# 3 <#> gv[*input] s
# 4 <1> rv2av[t5] lK/1
# 5 <@> sort K/NUM
# 6 <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v
# 2 <0> pushmark s
# 3 <$> gv(*input) s
# 4 <1> rv2av[t2] lK/1
# 5 <@> sort K/NUM
# 6 <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # more void context, propagating ?
sort { $a <=> $b } grep { $_ == $_ } @input;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{sort { $a <=> $b } grep { $_ == $_ } @input; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 684 (eval 50):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*input] s
# 5 <1> rv2av[t7] lKM/1
# 6 <@> grepstart lK
# 7 <|> grepwhile(other->8)[t8] lK
# 8 <#> gvsv[*_] s
# 9 <#> gvsv[*_] s
# a <2> eq sK/2
# goto 7
# b <@> sort K/NUM
# c <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 547 (eval 15):1) v
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*input) s
# 5 <1> rv2av[t2] lKM/1
# 6 <@> grepstart lK
# 7 <|> grepwhile(other->8)[t3] lK
# 8 <$> gvsv(*_) s
# 9 <$> gvsv(*_) s
# a <2> eq sK/2
# goto 7
# b <@> sort K/NUM
# c <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: # scalar return context sort
$s = sort { $a <=> $b } @input;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{$s = sort { $a <=> $b } @input; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 689 (eval 52):1) v:{
# 2 <0> pushmark s
# 3 <#> gv[*input] s
# 4 <1> rv2av[t6] lK/1
# 5 <@> sort sK/NUM
# 6 <#> gvsv[*s] s
# 7 <2> sassign sKS/2
# 8 <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 546 (eval 15):1) v:{
# 2 <0> pushmark s
# 3 <$> gv(*input) s
# 4 <1> rv2av[t2] lK/1
# 5 <@> sort sK/NUM
# 6 <$> gvsv(*s) s
# 7 <2> sassign sKS/2
# 8 <1> leavesub[1 ref] K/REFC,1
EONT_EONT
=for gentest
# chunk: $s = sort { $a <=> $b } grep { $_ == $_ } @input;
=cut
checkOptree(note => q{},
bcopts => q{-exec},
code => q{$s = sort { $a <=> $b } grep { $_ == $_ } @input; },
expect => <<'EOT_EOT', expect_nt => <<'EONT_EONT');
# 1 <;> nextstate(main 695 (eval 54):1) v:{
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <#> gv[*input] s
# 5 <1> rv2av[t8] lKM/1
# 6 <@> grepstart lK
# 7 <|> grepwhile(other->8)[t9] lK
# 8 <#> gvsv[*_] s
# 9 <#> gvsv[*_] s
# a <2> eq sK/2
# goto 7
# b <@> sort sK/NUM
# c <#> gvsv[*s] s
# d <2> sassign sKS/2
# e <1> leavesub[1 ref] K/REFC,1
EOT_EOT
# 1 <;> nextstate(main 547 (eval 15):1) v:{
# 2 <0> pushmark s
# 3 <0> pushmark s
# 4 <$> gv(*input) s
# 5 <1> rv2av[t2] lKM/1
# 6 <@> grepstart lK
# 7 <|> grepwhile(other->8)[t3] lK
# 8 <$> gvsv(*_) s
# 9 <$> gvsv(*_) s
# a <2> eq sK/2
# goto 7
# b <@> sort sK/NUM
# c <$> gvsv(*s) s
# d <2> sassign sKS/2
# e <1> leavesub[1 ref] K/REFC,1
EONT_EONT
|