5 @INC = qw(. ../lib); require 'test.pl';
10 # these shouldn't hang
13 sort { for ($_ = 0;; $_++) {} } @a;
14 sort { while(1) {} } @a;
15 sort { while(1) { last; } } @a;
16 sort { while(0) { last; } } @a;
18 # Change 26011: Re: A surprising segfault
19 map scalar(sort(+())), ('')x68;
22 sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
23 sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 }
24 sub Backwards_other { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
26 my $upperfirst = 'A' lt 'a';
28 # Beware: in future this may become hairier because of possible
29 # collation complications: qw(A a B b) can be sorted at least as
30 # any of the following
37 # All the above orders make sense.
39 # That said, EBCDIC sorts all small letters first, as opposed
40 # to ASCII which sorts all big letters first.
42 @harry = ('dog','cat','x','Cain','Abel');
43 @george = ('gone','chased','yz','punished','Axed');
45 $x = join('', sort @harry);
46 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
48 cmp_ok($x,'eq',$expected,'upper first 1');
50 $x = join('', sort( Backwards @harry));
51 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
53 cmp_ok($x,'eq',$expected,'upper first 2');
55 $x = join('', sort( Backwards_stacked @harry));
56 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
58 cmp_ok($x,'eq',$expected,'upper first 3');
60 $x = join('', sort @george, 'to', @harry);
61 $expected = $upperfirst ?
62 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
63 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
65 cmp_ok($x,'eq',$expected,'upper first 4');
69 cmp_ok("@b",'eq',"",'reverse 1');
73 cmp_ok("@b",'eq',"1",'reverse 2');
77 cmp_ok("@b",'eq',"2 1",'reverse 3');
81 cmp_ok("@b",'eq',"3 2 1",'reverse 4');
85 cmp_ok("@b",'eq',"4 3 2 1",'reverse 5');
88 @b = sort {$a <=> $b;} @a;
89 cmp_ok("@b",'eq',"2 3 4 10",'sort numeric');
92 $x = join('', sort $sub @harry);
93 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
95 cmp_ok($x,'eq',$expected,'sorter sub name in var 1');
97 $sub = 'Backwards_stacked';
98 $x = join('', sort $sub @harry);
99 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
101 cmp_ok($x,'eq',$expected,'sorter sub name in var 2');
103 # literals, combinations
106 cmp_ok("@b",'eq','1 2 3 4','just sort');
109 @b = sort grep { $_ } (4,1,3,2);
110 cmp_ok("@b",'eq','1 2 3 4','grep then sort');
113 @b = sort map { $_ } (4,1,3,2);
114 cmp_ok("@b",'eq','1 2 3 4','map then sort');
117 @b = sort reverse (4,1,3,2);
118 cmp_ok("@b",'eq','1 2 3 4','reverse then sort');
122 sub twoface { no warnings 'redefine'; *twoface = sub { $a <=> $b }; &twoface }
123 eval { @b = sort twoface 4,1,3,2 };
124 cmp_ok("@b",'eq','1 2 3 4','redefine sort sub inside the sort sub');
127 eval { no warnings 'redefine'; *twoface = sub { &Backwards } };
128 ok(!$@,"redefining sort subs outside the sort \$@=[$@]");
130 eval { @b = sort twoface 4,1,3,2 };
131 cmp_ok("@b",'eq','4 3 2 1','twoface redefinition');
134 no warnings 'redefine';
135 *twoface = sub { *twoface = *Backwards_other; $a <=> $b };
138 eval { @b = sort twoface 4,1,9,5 };
139 ok(($@ eq "" && "@b" eq "1 4 5 9"),'redefinition should not take effect during the sort');
142 no warnings 'redefine';
144 eval 'sub twoface { $a <=> $b }';
145 die($@ eq "" ? "good\n" : "bad\n");
149 eval { @b = sort twoface 4,1 };
150 cmp_ok(substr($@,0,4), 'eq', 'good', 'twoface eval');
153 my @result = sort main'Backwards 'one', 'two';
155 cmp_ok($@,'eq','',q(old skool package));
158 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
159 my @result = sort 'one', 'two';
161 cmp_ok($@,'eq','',q(one is not a sub));
164 my $sortsub = \&Backwards;
165 my $sortglob = *Backwards;
166 my $sortglobr = \*Backwards;
167 my $sortname = 'Backwards';
168 @b = sort $sortsub 4,1,3,2;
169 cmp_ok("@b",'eq','4 3 2 1','sortname 1');
170 @b = sort $sortglob 4,1,3,2;
171 cmp_ok("@b",'eq','4 3 2 1','sortname 2');
172 @b = sort $sortname 4,1,3,2;
173 cmp_ok("@b",'eq','4 3 2 1','sortname 3');
174 @b = sort $sortglobr 4,1,3,2;
175 cmp_ok("@b",'eq','4 3 2 1','sortname 4');
179 my $sortsub = \&Backwards_stacked;
180 my $sortglob = *Backwards_stacked;
181 my $sortglobr = \*Backwards_stacked;
182 my $sortname = 'Backwards_stacked';
183 @b = sort $sortsub 4,1,3,2;
184 cmp_ok("@b",'eq','4 3 2 1','sortname 5');
185 @b = sort $sortglob 4,1,3,2;
186 cmp_ok("@b",'eq','4 3 2 1','sortname 6');
187 @b = sort $sortname 4,1,3,2;
188 cmp_ok("@b",'eq','4 3 2 1','sortname 7');
189 @b = sort $sortglobr 4,1,3,2;
190 cmp_ok("@b",'eq','4 3 2 1','sortname 8');
194 local $sortsub = \&Backwards;
195 local $sortglob = *Backwards;
196 local $sortglobr = \*Backwards;
197 local $sortname = 'Backwards';
198 @b = sort $sortsub 4,1,3,2;
199 cmp_ok("@b",'eq','4 3 2 1','sortname local 1');
200 @b = sort $sortglob 4,1,3,2;
201 cmp_ok("@b",'eq','4 3 2 1','sortname local 2');
202 @b = sort $sortname 4,1,3,2;
203 cmp_ok("@b",'eq','4 3 2 1','sortname local 3');
204 @b = sort $sortglobr 4,1,3,2;
205 cmp_ok("@b",'eq','4 3 2 1','sortname local 4');
209 local $sortsub = \&Backwards_stacked;
210 local $sortglob = *Backwards_stacked;
211 local $sortglobr = \*Backwards_stacked;
212 local $sortname = 'Backwards_stacked';
213 @b = sort $sortsub 4,1,3,2;
214 cmp_ok("@b",'eq','4 3 2 1','sortname local 5');
215 @b = sort $sortglob 4,1,3,2;
216 cmp_ok("@b",'eq','4 3 2 1','sortname local 6');
217 @b = sort $sortname 4,1,3,2;
218 cmp_ok("@b",'eq','4 3 2 1','sortname local 7');
219 @b = sort $sortglobr 4,1,3,2;
220 cmp_ok("@b",'eq','4 3 2 1','sortname local 8');
223 ## exercise sort builtins... ($a <=> $b already tested)
224 @a = ( 5, 19, 1996, 255, 90 );
226 my $dummy; # force blockness
229 cmp_ok("@b",'eq','1996 255 90 19 5','force blockness');
231 $x = join('', sort { $a cmp $b } @harry);
232 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
233 cmp_ok($x,'eq',$expected,'a cmp b');
235 $x = join('', sort { $b cmp $a } @harry);
236 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
237 cmp_ok($x,'eq',$expected,'b cmp a');
241 @b = sort { $a <=> $b } @a;
242 cmp_ok("@b",'eq','5 19 90 255 1996','integer a <=> b');
244 @b = sort { $b <=> $a } @a;
245 cmp_ok("@b",'eq','1996 255 90 19 5','integer b <=> a');
247 $x = join('', sort { $a cmp $b } @harry);
248 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
249 cmp_ok($x,'eq',$expected,'integer a cmp b');
251 $x = join('', sort { $b cmp $a } @harry);
252 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
253 cmp_ok($x,'eq',$expected,'integer b cmp a');
259 $x = join('', sort { $a <=> $b } 3, 1, 2);
260 cmp_ok($x,'eq','123',q(optimized-away comparison block doesn't take any other arguments away with it));
262 # test sorting in non-main package
264 @a = ( 5, 19, 1996, 255, 90 );
265 @b = sort { $b <=> $a } @a;
266 main::cmp_ok("@b",'eq','1996 255 90 19 5','not in main:: 1');
269 @b = sort main::Backwards_stacked @a;
270 main::cmp_ok("@b",'eq','90 5 255 1996 19','not in main:: 2');
273 # check if context for sort arguments is handled right
277 my $gimme = wantarray;
278 main::is($gimme,1,'wantarray 1');
282 my $m = sub { $a <=> $b };
284 sub cxt_one { sort $m test_if_list() }
286 sub cxt_two { sort { $a <=> $b } test_if_list() }
288 sub cxt_three { sort &test_if_list() }
292 my $gimme = wantarray;
293 main::is(!($gimme or !defined($gimme)),1,'wantarray 2');
298 $m = \&test_if_scalar;
299 sub cxt_four { sort $m 1,2 }
301 sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 }
303 sub cxt_six { sort test_if_scalar 1,2 }
306 # test against a reentrancy bug
309 sub compare { $a cmp $b }
310 sub reenter { my @force = sort compare qw/a b/ }
313 my($def, $init) = (0, 0);
315 $def = 1 if defined $Bar::a;
316 Bar::reenter() unless $init++;
319 main::cmp_ok("@b",'eq','1 2 3 4','reenter 1');
321 main::ok(!$def,'reenter 2');
326 sub routine { "one", "two" };
327 @a = sort(routine(1));
328 main::cmp_ok("@a",'eq',"one two",'bug id 19991001.003');
333 sub ok { main::cmp_ok($_[0],'eq',$_[1],$_[2]);
334 # print "not " unless $_[0] eq $_[1];
335 # print "ok $test - $_[2]\n";
336 # print "#[$_[0]] ne [$_[1]]\n" unless $_[0] eq $_[1];
340 # check for in-place optimisation of @a = sort @a
344 @g = (3,2,1); $r1 = \$g[2]; @g = sort @g; $r2 = \$g[0];
345 ok "$r1-@g", "$r2-1 2 3", "inplace sort of global";
347 @a = qw(b a c); $r1 = \$a[1]; @a = sort @a; $r2 = \$a[0];
348 ok "$r1-@a", "$r2-a b c", "inplace sort of lexical";
350 @g = (2,3,1); $r1 = \$g[1]; @g = sort { $b <=> $a } @g; $r2 = \$g[0];
351 ok "$r1-@g", "$r2-3 2 1", "inplace reversed sort of global";
354 $r1 = \$g[1]; @g = sort { $a<$b?1:$a>$b?-1:0 } @g; $r2 = \$g[0];
355 ok "$r1-@g", "$r2-3 2 1", "inplace custom sort of global";
357 sub mysort { $b cmp $a };
358 @a = qw(b c a); $r1 = \$a[1]; @a = sort mysort @a; $r2 = \$a[0];
359 ok "$r1-@a", "$r2-c b a", "inplace sort with function of lexical";
363 tie @t, 'Tie::StdArray';
365 @t = qw(b c a); @t = sort @t;
366 ok "@t", "a b c", "inplace sort of tied array";
368 @t = qw(b c a); @t = sort mysort @t;
369 ok "@t", "c b a", "inplace sort of tied array with function";
371 # [perl #29790] don't optimise @a = ('a', sort @a) !
373 @g = (3,2,1); @g = ('0', sort @g);
374 ok "@g", "0 1 2 3", "un-inplace sort of global";
375 @g = (3,2,1); @g = (sort(@g),'4');
376 ok "@g", "1 2 3 4", "un-inplace sort of global 2";
378 @a = qw(b a c); @a = ('x', sort @a);
379 ok "@a", "x a b c", "un-inplace sort of lexical";
380 @a = qw(b a c); @a = ((sort @a), 'x');
381 ok "@a", "a b c x", "un-inplace sort of lexical 2";
383 @g = (2,3,1); @g = ('0', sort { $b <=> $a } @g);
384 ok "@g", "0 3 2 1", "un-inplace reversed sort of global";
385 @g = (2,3,1); @g = ((sort { $b <=> $a } @g),'4');
386 ok "@g", "3 2 1 4", "un-inplace reversed sort of global 2";
388 @g = (2,3,1); @g = ('0', sort { $a<$b?1:$a>$b?-1:0 } @g);
389 ok "@g", "0 3 2 1", "un-inplace custom sort of global";
390 @g = (2,3,1); @g = ((sort { $a<$b?1:$a>$b?-1:0 } @g),'4');
391 ok "@g", "3 2 1 4", "un-inplace custom sort of global 2";
393 @a = qw(b c a); @a = ('x', sort mysort @a);
394 ok "@a", "x c b a", "un-inplace sort with function of lexical";
395 @a = qw(b c a); @a = ((sort mysort @a),'x');
396 ok "@a", "c b a x", "un-inplace sort with function of lexical 2";
399 # Test optimisations of reversed sorts. As we now guarantee stability by
400 # default, # optimisations which do not provide this are bogus.
404 use overload (qw("" stringify 0+ numify fallback 1));
407 bless [$_[1], $_[2]], $_[0];
410 sub stringify { $_[0]->[0] }
412 sub numify { $_[0]->[1] }
417 map {new Oscalar $_, $count++} qw(A A A B B B C C C);
420 my @input = &generate;
421 my @output = sort @input;
422 ok join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", "Simple stable sort";
425 @input = sort @input;
426 ok join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
427 "Simple stable in place sort";
429 # This won't be very interesting
431 @output = sort {$a <=> $b} @input;
432 ok "@output", "A A A B B B C C C", 'stable $a <=> $b sort';
435 @output = sort {$a cmp $b} @input;
436 ok join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", 'stable $a cmp $b sort';
439 @input = sort {$a cmp $b} @input;
440 ok join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
441 'stable $a cmp $b in place sort';
444 @output = sort {$b cmp $a} @input;
445 ok join(" ", map {0+$_} @output), "6 7 8 3 4 5 0 1 2", 'stable $b cmp $a sort';
448 @input = sort {$b cmp $a} @input;
449 ok join(" ", map {0+$_} @input), "6 7 8 3 4 5 0 1 2",
450 'stable $b cmp $a in place sort';
453 @output = reverse sort @input;
454 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", "Reversed stable sort";
457 @input = reverse sort @input;
458 ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
459 "Reversed stable in place sort";
462 my $output = reverse sort @input;
463 ok $output, "CCCBBBAAA", "Reversed stable sort in scalar context";
467 @output = reverse sort {$a cmp $b} @input;
468 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
469 'reversed stable $a cmp $b sort';
472 @input = reverse sort {$a cmp $b} @input;
473 ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
474 'revesed stable $a cmp $b in place sort';
477 $output = reverse sort {$a cmp $b} @input;
478 ok $output, "CCCBBBAAA", 'Reversed stable $a cmp $b sort in scalar context';
481 @output = reverse sort {$b cmp $a} @input;
482 ok join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
483 'reversed stable $b cmp $a sort';
486 @input = reverse sort {$b cmp $a} @input;
487 ok join(" ", map {0+$_} @input), "2 1 0 5 4 3 8 7 6",
488 'revesed stable $b cmp $a in place sort';
491 $output = reverse sort {$b cmp $a} @input;
492 ok $output, "AAABBBCCC", 'Reversed stable $b cmp $a sort in scalar context';
495 # Something complex enough to defeat any constant folding optimiser
500 @output = reverse sort {stuff || $a cmp $b} @input;
501 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
502 'reversed stable complex sort';
505 @input = reverse sort {stuff || $a cmp $b} @input;
506 ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
507 'revesed stable complex in place sort';
510 $output = reverse sort {stuff || $a cmp $b } @input;
511 ok $output, "CCCBBBAAA", 'Reversed stable complex sort in scalar context';
517 @output = sortr &generate;
518 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
519 'reversed stable sort return list context';
520 $output = sortr &generate;
521 ok $output, "CCCBBBAAA",
522 'reversed stable sort return scalar context';
525 reverse sort {$a cmp $b} @_;
528 @output = sortcmpr &generate;
529 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
530 'reversed stable $a cmp $b sort return list context';
531 $output = sortcmpr &generate;
532 ok $output, "CCCBBBAAA",
533 'reversed stable $a cmp $b sort return scalar context';
536 reverse sort {$b cmp $a} @_;
539 @output = sortcmprba &generate;
540 ok join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
541 'reversed stable $b cmp $a sort return list context';
542 $output = sortcmprba &generate;
543 ok $output, "AAABBBCCC",
544 'reversed stable $b cmp $a sort return scalar context';
547 reverse sort {stuff || $a cmp $b} @_;
550 @output = sortcmpr &generate;
551 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
552 'reversed stable complex sort return list context';
553 $output = sortcmpr &generate;
554 ok $output, "CCCBBBAAA",
555 'reversed stable complex sort return scalar context';
557 # And now with numbers
561 map {new Oscalar $count++, $_} 0, 0, 0, 1, 1, 1, 2, 2, 2;
564 # This won't be very interesting
566 @output = sort {$a cmp $b} @input;
567 ok "@output", "A B C D E F G H I", 'stable $a cmp $b sort';
570 @output = sort {$a <=> $b} @input;
571 ok "@output", "A B C D E F G H I", 'stable $a <=> $b sort';
574 @input = sort {$a <=> $b} @input;
575 ok "@input", "A B C D E F G H I", 'stable $a <=> $b in place sort';
578 @output = sort {$b <=> $a} @input;
579 ok "@output", "G H I D E F A B C", 'stable $b <=> $a sort';
582 @input = sort {$b <=> $a} @input;
583 ok "@input", "G H I D E F A B C", 'stable $b <=> $a in place sort';
585 # test that optimized {$b cmp $a} and {$b <=> $a} remain stable
586 # (new in 5.9) without overloading
588 @b = sort { $b <=> $a } @input = qw/5first 6first 5second 6second/;
589 ok "@b" , "6first 6second 5first 5second", "optimized {$b <=> $a} without overloading" ;
590 @input = sort {$b <=> $a} @input;
591 ok "@input" , "6first 6second 5first 5second","inline optimized {$b <=> $a} without overloading" ;
594 # These two are actually doing string cmp on 0 1 and 2
596 @output = reverse sort @input;
597 ok "@output", "I H G F E D C B A", "Reversed stable sort";
600 @input = reverse sort @input;
601 ok "@input", "I H G F E D C B A", "Reversed stable in place sort";
604 $output = reverse sort @input;
605 ok $output, "IHGFEDCBA", "Reversed stable sort in scalar context";
608 @output = reverse sort {$a <=> $b} @input;
609 ok "@output", "I H G F E D C B A", 'reversed stable $a <=> $b sort';
612 @input = reverse sort {$a <=> $b} @input;
613 ok "@input", "I H G F E D C B A", 'revesed stable $a <=> $b in place sort';
616 $output = reverse sort {$a <=> $b} @input;
617 ok $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort in scalar context';
620 @output = reverse sort {$b <=> $a} @input;
621 ok "@output", "C B A F E D I H G", 'reversed stable $b <=> $a sort';
624 @input = reverse sort {$b <=> $a} @input;
625 ok "@input", "C B A F E D I H G", 'revesed stable $b <=> $a in place sort';
628 $output = reverse sort {$b <=> $a} @input;
629 ok $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort in scalar context';
632 @output = reverse sort {stuff || $a <=> $b} @input;
633 ok "@output", "I H G F E D C B A", 'reversed stable complex sort';
636 @input = reverse sort {stuff || $a <=> $b} @input;
637 ok "@input", "I H G F E D C B A", 'revesed stable complex in place sort';
640 $output = reverse sort {stuff || $a <=> $b} @input;
641 ok $output, "IHGFEDCBA", 'reversed stable complex sort in scalar context';
644 reverse sort {$a <=> $b} @_;
647 @output = sortnumr &generate1;
648 ok "@output", "I H G F E D C B A",
649 'reversed stable $a <=> $b sort return list context';
650 $output = sortnumr &generate1;
651 ok $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort return scalar context';
654 reverse sort {$b <=> $a} @_;
657 @output = sortnumrba &generate1;
658 ok "@output", "C B A F E D I H G",
659 'reversed stable $b <=> $a sort return list context';
660 $output = sortnumrba &generate1;
661 ok $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort return scalar context';
664 reverse sort {stuff || $a <=> $b} @_;
667 @output = sortnumrq &generate1;
668 ok "@output", "I H G F E D C B A",
669 'reversed stable complex sort return list context';
670 $output = sortnumrq &generate1;
671 ok $output, "IHGFEDCBA", 'reversed stable complex sort return scalar context';
673 @output = reverse (sort(qw(C A B)), 0);
674 ok "@output", "0 C B A", 'reversed sort with trailing argument';
676 @output = reverse (0, sort(qw(C A B)));
677 ok "@output", "C B A 0", 'reversed sort with leading argument';
679 eval { @output = sort {goto sub {}} 1,2; };
680 $fail_msg = q(Can't goto subroutine outside a subroutine);
681 main::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr outside subr');
685 sub goto_sub {goto sub{}}
686 eval { @output = sort goto_sub 1,2; };
687 $fail_msg = q(Can't goto subroutine from a sort sub);
688 main::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr from a sort sub');
692 eval { @output = sort {goto label} 1,2; };
693 $fail_msg = q(Can't "goto" out of a pseudo block);
694 main::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 1');
698 sub goto_label {goto label}
699 label: eval { @output = sort goto_label 1,2; };
700 $fail_msg = q(Can't "goto" out of a pseudo block);
701 main::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 2');
705 sub self_immolate {undef &self_immolate; $a<=>$b}
706 eval { @output = sort self_immolate 1,2,3 };
707 $fail_msg = q(Can't undef active subroutine);
708 main::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'undef active subr');
717 if (!defined($n)) { # No arg means we're being called by sort()
720 if ($n<5) { rec($n+1); }
721 else { () = sort rec 1,2; }
723 $failed = 1 if !defined $n;
727 main::ok(!$failed, "sort from active sub");
730 # $a and $b are set in the package the sort() is called from,
731 # *not* the package the sort sub is in. This is longstanding
732 # de facto behaviour that shouldn't be broken.
735 () = sort OtherPack::foo 1,2,3,4;
741 $answer = "something was unexpectedly defined or undefined" if
742 defined($a) || defined($b) || !defined($main::a) || !defined($main::b);
743 $main::a <=> $main::b;
747 main::cmp_ok($answer,'eq','good','sort subr called from other package');
750 # Bug 36430 - sort called in package2 while a
751 # sort in package1 is active should set $package2::a/b.
754 my @list = sort { A::min(@$a) <=> A::min(@$b) }
755 [3, 1, 5], [2, 4], [0];
757 main::cmp_ok($answer,'eq','good','bug 36430');
762 $answer = '$a and/or $b are not defined ' if !defined($a) || !defined($b);
768 # Bug 7567 - an array shouldn't be modifiable while it's being
770 eval { @a=(1..8); @a = sort { @a = (0) } @a; };
772 $fail_msg = q(Modification of a read-only value attempted);
773 main::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'bug 7567');
777 # Sorting shouldn't increase the refcount of a sub
778 sub foo {(1+$a) <=> (1+$b)}
779 my $refcnt = &Internals::SvREFCNT(\&foo);
780 @output = sort foo 3,7,9;
782 ok($refcnt, &Internals::SvREFCNT(\&foo), "sort sub refcnt");
783 $fail_msg = q(Modification of a read-only value attempted);
784 # Sorting a read-only array in-place shouldn't be allowed
785 my @readonly = (1..10);
786 Internals::SvREADONLY(@readonly, 1);
787 eval { @readonly = sort @readonly; };
788 main::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'in-place sort of read-only array');
793 # Using return() should be okay even in a deeper context
794 @b = sort {while (1) {return ($a <=> $b)} } 1..10;
795 ok("@b", "1 2 3 4 5 6 7 8 9 10", "return within loop");
797 # Using return() should be okay even if there are other items
798 # on the stack at the time.
799 @b = sort {$_ = ($a<=>$b) + do{return $b<=> $a}} 1..10;
800 ok("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack");
802 # As above, but with a sort sub rather than a sort block.
803 sub ret_with_stacked { $_ = ($a<=>$b) + do {return $b <=> $a} }
804 @b = sort ret_with_stacked 1..10;
805 ok("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack");