11 # these shouldn't hang
14 sort { for ($_ = 0;; $_++) {} } @a;
15 sort { while(1) {} } @a;
16 sort { while(1) { last; } } @a;
17 sort { while(0) { last; } } @a;
19 # Change 26011: Re: A surprising segfault
20 map scalar(sort(+())), ('')x68;
23 sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
24 sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 }
25 sub Backwards_other { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
27 my $upperfirst = 'A' lt 'a';
29 # Beware: in future this may become hairier because of possible
30 # collation complications: qw(A a B b) can be sorted at least as
31 # any of the following
38 # All the above orders make sense.
40 # That said, EBCDIC sorts all small letters first, as opposed
41 # to ASCII which sorts all big letters first.
43 @harry = ('dog','cat','x','Cain','Abel');
44 @george = ('gone','chased','yz','punished','Axed');
46 $x = join('', sort @harry);
47 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
49 cmp_ok($x,'eq',$expected,'upper first 1');
51 $x = join('', sort( Backwards @harry));
52 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
54 cmp_ok($x,'eq',$expected,'upper first 2');
56 $x = join('', sort( Backwards_stacked @harry));
57 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
59 cmp_ok($x,'eq',$expected,'upper first 3');
61 $x = join('', sort @george, 'to', @harry);
62 $expected = $upperfirst ?
63 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
64 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
66 cmp_ok($x,'eq',$expected,'upper first 4');
70 cmp_ok("@b",'eq',"",'reverse 1');
74 cmp_ok("@b",'eq',"1",'reverse 2');
78 cmp_ok("@b",'eq',"2 1",'reverse 3');
82 cmp_ok("@b",'eq',"3 2 1",'reverse 4');
86 cmp_ok("@b",'eq',"4 3 2 1",'reverse 5');
89 @b = sort {$a <=> $b;} @a;
90 cmp_ok("@b",'eq',"2 3 4 10",'sort numeric');
93 $x = join('', sort $sub @harry);
94 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
96 cmp_ok($x,'eq',$expected,'sorter sub name in var 1');
98 $sub = 'Backwards_stacked';
99 $x = join('', sort $sub @harry);
100 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
102 cmp_ok($x,'eq',$expected,'sorter sub name in var 2');
104 # literals, combinations
107 cmp_ok("@b",'eq','1 2 3 4','just sort');
110 @b = sort grep { $_ } (4,1,3,2);
111 cmp_ok("@b",'eq','1 2 3 4','grep then sort');
114 @b = sort map { $_ } (4,1,3,2);
115 cmp_ok("@b",'eq','1 2 3 4','map then sort');
118 @b = sort reverse (4,1,3,2);
119 cmp_ok("@b",'eq','1 2 3 4','reverse then sort');
123 sub twoface { no warnings 'redefine'; *twoface = sub { $a <=> $b }; &twoface }
124 eval { @b = sort twoface 4,1,3,2 };
125 cmp_ok("@b",'eq','1 2 3 4','redefine sort sub inside the sort sub');
128 eval { no warnings 'redefine'; *twoface = sub { &Backwards } };
129 ok(!$@,"redefining sort subs outside the sort \$@=[$@]");
131 eval { @b = sort twoface 4,1,3,2 };
132 cmp_ok("@b",'eq','4 3 2 1','twoface redefinition');
135 no warnings 'redefine';
136 *twoface = sub { *twoface = *Backwards_other; $a <=> $b };
139 eval { @b = sort twoface 4,1,9,5 };
140 ok(($@ eq "" && "@b" eq "1 4 5 9"),'redefinition should not take effect during the sort');
143 no warnings 'redefine';
145 eval 'sub twoface { $a <=> $b }';
146 die($@ eq "" ? "good\n" : "bad\n");
150 eval { @b = sort twoface 4,1 };
151 cmp_ok(substr($@,0,4), 'eq', 'good', 'twoface eval');
154 my @result = sort main'Backwards 'one', 'two';
156 cmp_ok($@,'eq','',q(old skool package));
159 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
160 my @result = sort 'one', 'two';
162 cmp_ok($@,'eq','',q(one is not a sub));
165 my $sortsub = \&Backwards;
166 my $sortglob = *Backwards;
167 my $sortglobr = \*Backwards;
168 my $sortname = 'Backwards';
169 @b = sort $sortsub 4,1,3,2;
170 cmp_ok("@b",'eq','4 3 2 1','sortname 1');
171 @b = sort $sortglob 4,1,3,2;
172 cmp_ok("@b",'eq','4 3 2 1','sortname 2');
173 @b = sort $sortname 4,1,3,2;
174 cmp_ok("@b",'eq','4 3 2 1','sortname 3');
175 @b = sort $sortglobr 4,1,3,2;
176 cmp_ok("@b",'eq','4 3 2 1','sortname 4');
180 my $sortsub = \&Backwards_stacked;
181 my $sortglob = *Backwards_stacked;
182 my $sortglobr = \*Backwards_stacked;
183 my $sortname = 'Backwards_stacked';
184 @b = sort $sortsub 4,1,3,2;
185 cmp_ok("@b",'eq','4 3 2 1','sortname 5');
186 @b = sort $sortglob 4,1,3,2;
187 cmp_ok("@b",'eq','4 3 2 1','sortname 6');
188 @b = sort $sortname 4,1,3,2;
189 cmp_ok("@b",'eq','4 3 2 1','sortname 7');
190 @b = sort $sortglobr 4,1,3,2;
191 cmp_ok("@b",'eq','4 3 2 1','sortname 8');
195 local $sortsub = \&Backwards;
196 local $sortglob = *Backwards;
197 local $sortglobr = \*Backwards;
198 local $sortname = 'Backwards';
199 @b = sort $sortsub 4,1,3,2;
200 cmp_ok("@b",'eq','4 3 2 1','sortname local 1');
201 @b = sort $sortglob 4,1,3,2;
202 cmp_ok("@b",'eq','4 3 2 1','sortname local 2');
203 @b = sort $sortname 4,1,3,2;
204 cmp_ok("@b",'eq','4 3 2 1','sortname local 3');
205 @b = sort $sortglobr 4,1,3,2;
206 cmp_ok("@b",'eq','4 3 2 1','sortname local 4');
210 local $sortsub = \&Backwards_stacked;
211 local $sortglob = *Backwards_stacked;
212 local $sortglobr = \*Backwards_stacked;
213 local $sortname = 'Backwards_stacked';
214 @b = sort $sortsub 4,1,3,2;
215 cmp_ok("@b",'eq','4 3 2 1','sortname local 5');
216 @b = sort $sortglob 4,1,3,2;
217 cmp_ok("@b",'eq','4 3 2 1','sortname local 6');
218 @b = sort $sortname 4,1,3,2;
219 cmp_ok("@b",'eq','4 3 2 1','sortname local 7');
220 @b = sort $sortglobr 4,1,3,2;
221 cmp_ok("@b",'eq','4 3 2 1','sortname local 8');
224 ## exercise sort builtins... ($a <=> $b already tested)
225 @a = ( 5, 19, 1996, 255, 90 );
227 my $dummy; # force blockness
230 cmp_ok("@b",'eq','1996 255 90 19 5','force blockness');
232 $x = join('', sort { $a cmp $b } @harry);
233 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
234 cmp_ok($x,'eq',$expected,'a cmp b');
236 $x = join('', sort { $b cmp $a } @harry);
237 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
238 cmp_ok($x,'eq',$expected,'b cmp a');
242 @b = sort { $a <=> $b } @a;
243 cmp_ok("@b",'eq','5 19 90 255 1996','integer a <=> b');
245 @b = sort { $b <=> $a } @a;
246 cmp_ok("@b",'eq','1996 255 90 19 5','integer b <=> a');
248 $x = join('', sort { $a cmp $b } @harry);
249 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
250 cmp_ok($x,'eq',$expected,'integer a cmp b');
252 $x = join('', sort { $b cmp $a } @harry);
253 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
254 cmp_ok($x,'eq',$expected,'integer b cmp a');
260 $x = join('', sort { $a <=> $b } 3, 1, 2);
261 cmp_ok($x,'eq','123',q(optimized-away comparison block doesn't take any other arguments away with it));
263 # test sorting in non-main package
266 @a = ( 5, 19, 1996, 255, 90 );
267 @b = sort { $b <=> $a } @a;
268 ::cmp_ok("@b",'eq','1996 255 90 19 5','not in main:: 1');
270 @b = sort ::Backwards_stacked @a;
271 ::cmp_ok("@b",'eq','90 5 255 1996 19','not in main:: 2');
273 # check if context for sort arguments is handled right
275 my $gimme = wantarray;
276 ::is($gimme,1,'wantarray 1');
278 my $m = sub { $a <=> $b };
280 sub cxt_one { sort $m test_if_list() }
282 sub cxt_two { sort { $a <=> $b } test_if_list() }
284 sub cxt_three { sort &test_if_list() }
288 my $gimme = wantarray;
289 ::is(!($gimme or !defined($gimme)),1,'wantarray 2');
292 $m = \&test_if_scalar;
293 sub cxt_four { sort $m 1,2 }
295 sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 }
297 sub cxt_six { sort test_if_scalar 1,2 }
302 # test against a reentrancy bug
305 sub compare { $a cmp $b }
306 sub reenter { my @force = sort compare qw/a b/ }
309 my($def, $init) = (0, 0);
311 $def = 1 if defined $Bar::a;
312 Bar::reenter() unless $init++;
315 cmp_ok("@b",'eq','1 2 3 4','reenter 1');
317 ok(!$def,'reenter 2');
322 sub routine { "one", "two" };
323 @a = sort(routine(1));
324 cmp_ok("@a",'eq',"one two",'bug id 19991001.003');
328 # check for in-place optimisation of @a = sort @a
332 @g = (3,2,1); $r1 = \$g[2]; @g = sort @g; $r2 = \$g[0];
333 is "$r1-@g", "$r2-1 2 3", "inplace sort of global";
335 @a = qw(b a c); $r1 = \$a[1]; @a = sort @a; $r2 = \$a[0];
336 is "$r1-@a", "$r2-a b c", "inplace sort of lexical";
338 @g = (2,3,1); $r1 = \$g[1]; @g = sort { $b <=> $a } @g; $r2 = \$g[0];
339 is "$r1-@g", "$r2-3 2 1", "inplace reversed sort of global";
342 $r1 = \$g[1]; @g = sort { $a<$b?1:$a>$b?-1:0 } @g; $r2 = \$g[0];
343 is "$r1-@g", "$r2-3 2 1", "inplace custom sort of global";
345 sub mysort { $b cmp $a };
346 @a = qw(b c a); $r1 = \$a[1]; @a = sort mysort @a; $r2 = \$a[0];
347 is "$r1-@a", "$r2-c b a", "inplace sort with function of lexical";
351 tie @t, 'Tie::StdArray';
353 @t = qw(b c a); @t = sort @t;
354 is "@t", "a b c", "inplace sort of tied array";
356 @t = qw(b c a); @t = sort mysort @t;
357 is "@t", "c b a", "inplace sort of tied array with function";
359 # [perl #29790] don't optimise @a = ('a', sort @a) !
361 @g = (3,2,1); @g = ('0', sort @g);
362 is "@g", "0 1 2 3", "un-inplace sort of global";
363 @g = (3,2,1); @g = (sort(@g),'4');
364 is "@g", "1 2 3 4", "un-inplace sort of global 2";
366 @a = qw(b a c); @a = ('x', sort @a);
367 is "@a", "x a b c", "un-inplace sort of lexical";
368 @a = qw(b a c); @a = ((sort @a), 'x');
369 is "@a", "a b c x", "un-inplace sort of lexical 2";
371 @g = (2,3,1); @g = ('0', sort { $b <=> $a } @g);
372 is "@g", "0 3 2 1", "un-inplace reversed sort of global";
373 @g = (2,3,1); @g = ((sort { $b <=> $a } @g),'4');
374 is "@g", "3 2 1 4", "un-inplace reversed sort of global 2";
376 @g = (2,3,1); @g = ('0', sort { $a<$b?1:$a>$b?-1:0 } @g);
377 is "@g", "0 3 2 1", "un-inplace custom sort of global";
378 @g = (2,3,1); @g = ((sort { $a<$b?1:$a>$b?-1:0 } @g),'4');
379 is "@g", "3 2 1 4", "un-inplace custom sort of global 2";
381 @a = qw(b c a); @a = ('x', sort mysort @a);
382 is "@a", "x c b a", "un-inplace sort with function of lexical";
383 @a = qw(b c a); @a = ((sort mysort @a),'x');
384 is "@a", "c b a x", "un-inplace sort with function of lexical 2";
386 # RT#54758. Git 62b40d2474e7487e6909e1872b6bccdf812c6818
388 my @m; push @m, 0 for 1 .. 1024; $#m; @m = sort @m;
389 ::pass("in-place sorting segfault");
392 # Test optimisations of reversed sorts. As we now guarantee stability by
393 # default, # optimisations which do not provide this are bogus.
397 use overload (qw("" stringify 0+ numify fallback 1));
400 bless [$_[1], $_[2]], $_[0];
403 sub stringify { $_[0]->[0] }
405 sub numify { $_[0]->[1] }
410 map {new Oscalar $_, $count++} qw(A A A B B B C C C);
413 my @input = &generate;
414 my @output = sort @input;
415 is join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", "Simple stable sort";
418 @input = sort @input;
419 is join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
420 "Simple stable in place sort";
422 # This won't be very interesting
424 @output = sort {$a <=> $b} @input;
425 is "@output", "A A A B B B C C C", 'stable $a <=> $b sort';
428 @output = sort {$a cmp $b} @input;
429 is join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", 'stable $a cmp $b sort';
432 @input = sort {$a cmp $b} @input;
433 is join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
434 'stable $a cmp $b in place sort';
437 @output = sort {$b cmp $a} @input;
438 is join(" ", map {0+$_} @output), "6 7 8 3 4 5 0 1 2", 'stable $b cmp $a sort';
441 @input = sort {$b cmp $a} @input;
442 is join(" ", map {0+$_} @input), "6 7 8 3 4 5 0 1 2",
443 'stable $b cmp $a in place sort';
446 @output = reverse sort @input;
447 is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", "Reversed stable sort";
450 @input = reverse sort @input;
451 is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
452 "Reversed stable in place sort";
455 my $output = reverse sort @input;
456 is $output, "CCCBBBAAA", "Reversed stable sort in scalar context";
460 @output = reverse sort {$a cmp $b} @input;
461 is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
462 'reversed stable $a cmp $b sort';
465 @input = reverse sort {$a cmp $b} @input;
466 is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
467 'revesed stable $a cmp $b in place sort';
470 $output = reverse sort {$a cmp $b} @input;
471 is $output, "CCCBBBAAA", 'Reversed stable $a cmp $b sort in scalar context';
474 @output = reverse sort {$b cmp $a} @input;
475 is join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
476 'reversed stable $b cmp $a sort';
479 @input = reverse sort {$b cmp $a} @input;
480 is join(" ", map {0+$_} @input), "2 1 0 5 4 3 8 7 6",
481 'revesed stable $b cmp $a in place sort';
484 $output = reverse sort {$b cmp $a} @input;
485 is $output, "AAABBBCCC", 'Reversed stable $b cmp $a sort in scalar context';
488 # Something complex enough to defeat any constant folding optimiser
493 @output = reverse sort {stuff || $a cmp $b} @input;
494 is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
495 'reversed stable complex sort';
498 @input = reverse sort {stuff || $a cmp $b} @input;
499 is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
500 'revesed stable complex in place sort';
503 $output = reverse sort {stuff || $a cmp $b } @input;
504 is $output, "CCCBBBAAA", 'Reversed stable complex sort in scalar context';
510 @output = sortr &generate;
511 is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
512 'reversed stable sort return list context';
513 $output = sortr &generate;
514 is $output, "CCCBBBAAA",
515 'reversed stable sort return scalar context';
518 reverse sort {$a cmp $b} @_;
521 @output = sortcmpr &generate;
522 is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
523 'reversed stable $a cmp $b sort return list context';
524 $output = sortcmpr &generate;
525 is $output, "CCCBBBAAA",
526 'reversed stable $a cmp $b sort return scalar context';
529 reverse sort {$b cmp $a} @_;
532 @output = sortcmprba &generate;
533 is join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
534 'reversed stable $b cmp $a sort return list context';
535 $output = sortcmprba &generate;
536 is $output, "AAABBBCCC",
537 'reversed stable $b cmp $a sort return scalar context';
540 reverse sort {stuff || $a cmp $b} @_;
543 @output = sortcmpr &generate;
544 is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
545 'reversed stable complex sort return list context';
546 $output = sortcmpr &generate;
547 is $output, "CCCBBBAAA",
548 'reversed stable complex sort return scalar context';
550 # And now with numbers
554 map {new Oscalar $count++, $_} 0, 0, 0, 1, 1, 1, 2, 2, 2;
557 # This won't be very interesting
559 @output = sort {$a cmp $b} @input;
560 is "@output", "A B C D E F G H I", 'stable $a cmp $b sort';
563 @output = sort {$a <=> $b} @input;
564 is "@output", "A B C D E F G H I", 'stable $a <=> $b sort';
567 @input = sort {$a <=> $b} @input;
568 is "@input", "A B C D E F G H I", 'stable $a <=> $b in place sort';
571 @output = sort {$b <=> $a} @input;
572 is "@output", "G H I D E F A B C", 'stable $b <=> $a sort';
575 @input = sort {$b <=> $a} @input;
576 is "@input", "G H I D E F A B C", 'stable $b <=> $a in place sort';
578 # test that optimized {$b cmp $a} and {$b <=> $a} remain stable
579 # (new in 5.9) without overloading
581 @b = sort { $b <=> $a } @input = qw/5first 6first 5second 6second/;
582 is "@b" , "6first 6second 5first 5second", "optimized {$b <=> $a} without overloading" ;
583 @input = sort {$b <=> $a} @input;
584 is "@input" , "6first 6second 5first 5second","inline optimized {$b <=> $a} without overloading" ;
587 # These two are actually doing string cmp on 0 1 and 2
589 @output = reverse sort @input;
590 is "@output", "I H G F E D C B A", "Reversed stable sort";
593 @input = reverse sort @input;
594 is "@input", "I H G F E D C B A", "Reversed stable in place sort";
597 $output = reverse sort @input;
598 is $output, "IHGFEDCBA", "Reversed stable sort in scalar context";
601 @output = reverse sort {$a <=> $b} @input;
602 is "@output", "I H G F E D C B A", 'reversed stable $a <=> $b sort';
605 @input = reverse sort {$a <=> $b} @input;
606 is "@input", "I H G F E D C B A", 'revesed stable $a <=> $b in place sort';
609 $output = reverse sort {$a <=> $b} @input;
610 is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort in scalar context';
613 @output = reverse sort {$b <=> $a} @input;
614 is "@output", "C B A F E D I H G", 'reversed stable $b <=> $a sort';
617 @input = reverse sort {$b <=> $a} @input;
618 is "@input", "C B A F E D I H G", 'revesed stable $b <=> $a in place sort';
621 $output = reverse sort {$b <=> $a} @input;
622 is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort in scalar context';
625 @output = reverse sort {stuff || $a <=> $b} @input;
626 is "@output", "I H G F E D C B A", 'reversed stable complex sort';
629 @input = reverse sort {stuff || $a <=> $b} @input;
630 is "@input", "I H G F E D C B A", 'revesed stable complex in place sort';
633 $output = reverse sort {stuff || $a <=> $b} @input;
634 is $output, "IHGFEDCBA", 'reversed stable complex sort in scalar context';
637 reverse sort {$a <=> $b} @_;
640 @output = sortnumr &generate1;
641 is "@output", "I H G F E D C B A",
642 'reversed stable $a <=> $b sort return list context';
643 $output = sortnumr &generate1;
644 is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort return scalar context';
647 reverse sort {$b <=> $a} @_;
650 @output = sortnumrba &generate1;
651 is "@output", "C B A F E D I H G",
652 'reversed stable $b <=> $a sort return list context';
653 $output = sortnumrba &generate1;
654 is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort return scalar context';
657 reverse sort {stuff || $a <=> $b} @_;
660 @output = sortnumrq &generate1;
661 is "@output", "I H G F E D C B A",
662 'reversed stable complex sort return list context';
663 $output = sortnumrq &generate1;
664 is $output, "IHGFEDCBA", 'reversed stable complex sort return scalar context';
666 @output = reverse (sort(qw(C A B)), 0);
667 is "@output", "0 C B A", 'reversed sort with trailing argument';
669 @output = reverse (0, sort(qw(C A B)));
670 is "@output", "C B A 0", 'reversed sort with leading argument';
672 eval { @output = sort {goto sub {}} 1,2; };
673 $fail_msg = q(Can't goto subroutine outside a subroutine);
674 cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr outside subr');
678 sub goto_sub {goto sub{}}
679 eval { @output = sort goto_sub 1,2; };
680 $fail_msg = q(Can't goto subroutine from a sort sub);
681 cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr from a sort sub');
685 eval { @output = sort {goto label} 1,2; };
686 $fail_msg = q(Can't "goto" out of a pseudo block);
687 cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 1');
691 sub goto_label {goto label}
692 label: eval { @output = sort goto_label 1,2; };
693 $fail_msg = q(Can't "goto" out of a pseudo block);
694 cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 2');
698 sub self_immolate {undef &self_immolate; $a<=>$b}
699 eval { @output = sort self_immolate 1,2,3 };
700 $fail_msg = q(Can't undef active subroutine);
701 cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'undef active subr');
704 for(1,2) # We run this twice, to make sure sort does not lower the ref
705 { # count. See bug 71076.
710 if (!defined($n)) { # No arg means we're being called by sort()
713 if ($n<5) { rec($n+1); }
714 else { () = sort rec 1,2; }
716 $failed = 1 if !defined $n;
720 ok(!$failed, "sort from active sub");
723 # $a and $b are set in the package the sort() is called from,
724 # *not* the package the sort sub is in. This is longstanding
725 # de facto behaviour that shouldn't be broken.
727 () = sort OtherPack::foo 1,2,3,4;
733 $answer = "something was unexpectedly defined or undefined" if
734 defined($a) || defined($b) || !defined($main::a) || !defined($main::b);
735 $main::a <=> $main::b;
739 cmp_ok($answer,'eq','good','sort subr called from other package');
742 # Bug 36430 - sort called in package2 while a
743 # sort in package1 is active should set $package2::a/b.
746 my @list = sort { A::min(@$a) <=> A::min(@$b) }
747 [3, 1, 5], [2, 4], [0];
749 cmp_ok($answer,'eq','good','bug 36430');
754 $answer = '$a and/or $b are not defined ' if !defined($a) || !defined($b);
762 # Bug 7567 - an array shouldn't be modifiable while it's being
765 eval { @a=(1..8); @a = sort { @a = (0) } @a; };
767 $fail_msg = q(Modification of a read-only value attempted);
768 cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'bug 7567');
772 local $TODO = "sort should make sure elements are not freed in the sort block";
773 eval { @nomodify_x=(1..8); our @copy = sort { @nomodify_x = (0) } (@nomodify_x, 3); };
778 # Sorting shouldn't increase the refcount of a sub
780 sub sportello {(1+$a) <=> (1+$b)}
781 my $refcnt = &Internals::SvREFCNT(\&sportello);
782 @output = sort sportello 3,7,9;
786 ::is($refcnt, &Internals::SvREFCNT(\&::sportello), "sort sub refcnt");
787 $fail_msg = q(Modification of a read-only value attempted);
788 # Sorting a read-only array in-place shouldn't be allowed
789 my @readonly = (1..10);
790 Internals::SvREADONLY(@readonly, 1);
791 eval { @readonly = sort @readonly; };
792 ::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'in-place sort of read-only array');
797 # Using return() should be okay even in a deeper context
798 @b = sort {while (1) {return ($a <=> $b)} } 1..10;
799 is("@b", "1 2 3 4 5 6 7 8 9 10", "return within loop");
801 # Using return() should be okay even if there are other items
802 # on the stack at the time.
803 @b = sort {$_ = ($a<=>$b) + do{return $b<=> $a}} 1..10;
804 is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack");
806 # As above, but with a sort sub rather than a sort block.
807 sub ret_with_stacked { $_ = ($a<=>$b) + do {return $b <=> $a} }
808 @b = sort ret_with_stacked 1..10;
809 is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack");
811 # Comparison code should be able to give result in non-integer representation.
812 sub cmp_as_string($$) { $_[0] < $_[1] ? "-1" : $_[0] == $_[1] ? "0" : "+1" }
813 @b = sort { cmp_as_string($a, $b) } (1,5,4,7,3,2,3);
814 is("@b", "1 2 3 3 4 5 7", "comparison result as string");
815 @b = sort cmp_as_string (1,5,4,7,3,2,3);
816 is("@b", "1 2 3 3 4 5 7", "comparison result as string");