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;
19 sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
20 sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 }
21 sub Backwards_other { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
23 my $upperfirst = 'A' lt 'a';
25 # Beware: in future this may become hairier because of possible
26 # collation complications: qw(A a B b) can be sorted at least as
27 # any of the following
34 # All the above orders make sense.
36 # That said, EBCDIC sorts all small letters first, as opposed
37 # to ASCII which sorts all big letters first.
39 @harry = ('dog','cat','x','Cain','Abel');
40 @george = ('gone','chased','yz','punished','Axed');
42 $x = join('', sort @harry);
43 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
44 print "# 1: x = '$x', expected = '$expected'\n";
45 print ($x eq $expected ? "ok 1\n" : "not ok 1\n");
47 $x = join('', sort( Backwards @harry));
48 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
49 print "# 2: x = '$x', expected = '$expected'\n";
50 print ($x eq $expected ? "ok 2\n" : "not ok 2\n");
52 $x = join('', sort( Backwards_stacked @harry));
53 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
54 print "# 3: x = '$x', expected = '$expected'\n";
55 print ($x eq $expected ? "ok 3\n" : "not ok 3\n");
57 $x = join('', sort @george, 'to', @harry);
58 $expected = $upperfirst ?
59 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
60 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
61 print "# 4: x = '$x', expected = '$expected'\n";
62 print ($x eq $expected ?"ok 4\n":"not ok 4\n");
66 print ("@b" eq "" ? "ok 5\n" : "not ok 5 (@b)\n");
70 print ("@b" eq "1" ? "ok 6\n" : "not ok 6 (@b)\n");
74 print ("@b" eq "2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
78 print ("@b" eq "3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
82 print ("@b" eq "4 3 2 1" ? "ok 9\n" : "not ok 9 (@b)\n");
85 @b = sort {$a <=> $b;} @a;
86 print ("@b" eq "2 3 4 10" ? "ok 10\n" : "not ok 10 (@b)\n");
89 $x = join('', sort $sub @harry);
90 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
91 print "# 11: x = $x, expected = '$expected'\n";
92 print ($x eq $expected ? "ok 11\n" : "not ok 11\n");
94 $sub = 'Backwards_stacked';
95 $x = join('', sort $sub @harry);
96 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
97 print "# 12: x = $x, expected = '$expected'\n";
98 print ($x eq $expected ? "ok 12\n" : "not ok 12\n");
100 # literals, combinations
103 print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
104 print "# x = '@b'\n";
106 @b = sort grep { $_ } (4,1,3,2);
107 print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
108 print "# x = '@b'\n";
110 @b = sort map { $_ } (4,1,3,2);
111 print ("@b" eq '1 2 3 4' ? "ok 15\n" : "not ok 15\n");
112 print "# x = '@b'\n";
114 @b = sort reverse (4,1,3,2);
115 print ("@b" eq '1 2 3 4' ? "ok 16\n" : "not ok 16\n");
116 print "# x = '@b'\n";
118 # redefining sort sub inside the sort sub should not fail
119 sub twoface { no warnings 'redefine'; *twoface = sub { $a <=> $b }; &twoface }
120 eval { @b = sort twoface 4,1,3,2 };
121 print ($@ eq '' ? "ok 17\n" : "not ok 17\n");
123 # redefining sort subs outside the sort should also not fail
124 eval { no warnings 'redefine'; *twoface = sub { &Backwards } };
125 print $@ ? "not ok 18\n" : "ok 18\n";
127 eval { @b = sort twoface 4,1,3,2 };
128 print ("@b" eq '4 3 2 1' ? "ok 19\n" : "not ok 19 |@b|\n");
131 no warnings 'redefine';
132 *twoface = sub { *twoface = *Backwards_other; $a <=> $b };
134 # The redefinition should not take effect during the sort
135 eval { @b = sort twoface 4,1,9,5 };
136 print (($@ eq "" && "@b" eq "1 4 5 9") ? "ok 20\n" : "not ok 20 # $@|@b\n");
139 no warnings 'redefine';
141 eval 'sub twoface { $a <=> $b }';
142 die($@ eq "" ? "ok 21\n" : "not ok 21\n");
146 eval { @b = sort twoface 4,1 };
147 print($@ ? "$@" : "not ok 21 # $@\n");
150 my @result = sort main'Backwards 'one', 'two';
152 print $@ ? "not ok 22\n# $@" : "ok 22\n";
155 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
156 my @result = sort 'one', 'two';
158 print $@ ? "not ok 23\n# $@" : "ok 23\n";
161 my $sortsub = \&Backwards;
162 my $sortglob = *Backwards;
163 my $sortglobr = \*Backwards;
164 my $sortname = 'Backwards';
165 @b = sort $sortsub 4,1,3,2;
166 print ("@b" eq '4 3 2 1' ? "ok 24\n" : "not ok 24 |@b|\n");
167 @b = sort $sortglob 4,1,3,2;
168 print ("@b" eq '4 3 2 1' ? "ok 25\n" : "not ok 25 |@b|\n");
169 @b = sort $sortname 4,1,3,2;
170 print ("@b" eq '4 3 2 1' ? "ok 26\n" : "not ok 26 |@b|\n");
171 @b = sort $sortglobr 4,1,3,2;
172 print ("@b" eq '4 3 2 1' ? "ok 27\n" : "not ok 27 |@b|\n");
176 my $sortsub = \&Backwards_stacked;
177 my $sortglob = *Backwards_stacked;
178 my $sortglobr = \*Backwards_stacked;
179 my $sortname = 'Backwards_stacked';
180 @b = sort $sortsub 4,1,3,2;
181 print ("@b" eq '4 3 2 1' ? "ok 28\n" : "not ok 28 |@b|\n");
182 @b = sort $sortglob 4,1,3,2;
183 print ("@b" eq '4 3 2 1' ? "ok 29\n" : "not ok 29 |@b|\n");
184 @b = sort $sortname 4,1,3,2;
185 print ("@b" eq '4 3 2 1' ? "ok 30\n" : "not ok 30 |@b|\n");
186 @b = sort $sortglobr 4,1,3,2;
187 print ("@b" eq '4 3 2 1' ? "ok 31\n" : "not ok 31 |@b|\n");
191 local $sortsub = \&Backwards;
192 local $sortglob = *Backwards;
193 local $sortglobr = \*Backwards;
194 local $sortname = 'Backwards';
195 @b = sort $sortsub 4,1,3,2;
196 print ("@b" eq '4 3 2 1' ? "ok 32\n" : "not ok 32 |@b|\n");
197 @b = sort $sortglob 4,1,3,2;
198 print ("@b" eq '4 3 2 1' ? "ok 33\n" : "not ok 33 |@b|\n");
199 @b = sort $sortname 4,1,3,2;
200 print ("@b" eq '4 3 2 1' ? "ok 34\n" : "not ok 34 |@b|\n");
201 @b = sort $sortglobr 4,1,3,2;
202 print ("@b" eq '4 3 2 1' ? "ok 35\n" : "not ok 35 |@b|\n");
206 local $sortsub = \&Backwards_stacked;
207 local $sortglob = *Backwards_stacked;
208 local $sortglobr = \*Backwards_stacked;
209 local $sortname = 'Backwards_stacked';
210 @b = sort $sortsub 4,1,3,2;
211 print ("@b" eq '4 3 2 1' ? "ok 36\n" : "not ok 36 |@b|\n");
212 @b = sort $sortglob 4,1,3,2;
213 print ("@b" eq '4 3 2 1' ? "ok 37\n" : "not ok 37 |@b|\n");
214 @b = sort $sortname 4,1,3,2;
215 print ("@b" eq '4 3 2 1' ? "ok 38\n" : "not ok 38 |@b|\n");
216 @b = sort $sortglobr 4,1,3,2;
217 print ("@b" eq '4 3 2 1' ? "ok 39\n" : "not ok 39 |@b|\n");
220 ## exercise sort builtins... ($a <=> $b already tested)
221 @a = ( 5, 19, 1996, 255, 90 );
223 my $dummy; # force blockness
226 print ("@b" eq '1996 255 90 19 5' ? "ok 40\n" : "not ok 40\n");
227 print "# x = '@b'\n";
228 $x = join('', sort { $a cmp $b } @harry);
229 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
230 print ($x eq $expected ? "ok 41\n" : "not ok 41\n");
231 print "# x = '$x'; expected = '$expected'\n";
232 $x = join('', sort { $b cmp $a } @harry);
233 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
234 print ($x eq $expected ? "ok 42\n" : "not ok 42\n");
235 print "# x = '$x'; expected = '$expected'\n";
238 @b = sort { $a <=> $b } @a;
239 print ("@b" eq '5 19 90 255 1996' ? "ok 43\n" : "not ok 43\n");
240 print "# x = '@b'\n";
241 @b = sort { $b <=> $a } @a;
242 print ("@b" eq '1996 255 90 19 5' ? "ok 44\n" : "not ok 44\n");
243 print "# x = '@b'\n";
244 $x = join('', sort { $a cmp $b } @harry);
245 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
246 print ($x eq $expected ? "ok 45\n" : "not ok 45\n");
247 print "# x = '$x'; expected = '$expected'\n";
248 $x = join('', sort { $b cmp $a } @harry);
249 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
250 print ($x eq $expected ? "ok 46\n" : "not ok 46\n");
251 print "# x = '$x'; expected = '$expected'\n";
254 # test that an optimized-away comparison block doesn't take any other
255 # arguments away with it
256 $x = join('', sort { $a <=> $b } 3, 1, 2);
257 print $x eq "123" ? "ok 47\n" : "not ok 47\n";
259 # test sorting in non-main package
261 @a = ( 5, 19, 1996, 255, 90 );
262 @b = sort { $b <=> $a } @a;
263 print ("@b" eq '1996 255 90 19 5' ? "ok 48\n" : "not ok 48\n");
264 print "# x = '@b'\n";
266 @b = sort main::Backwards_stacked @a;
267 print ("@b" eq '90 5 255 1996 19' ? "ok 49\n" : "not ok 49\n");
268 print "# x = '@b'\n";
270 # check if context for sort arguments is handled right
274 my $gimme = wantarray;
275 print "not " unless $gimme;
279 my $m = sub { $a <=> $b };
281 sub cxt_one { sort $m test_if_list() }
283 sub cxt_two { sort { $a <=> $b } test_if_list() }
285 sub cxt_three { sort &test_if_list() }
289 my $gimme = wantarray;
290 print "not " if $gimme or !defined($gimme);
295 $m = \&test_if_scalar;
296 sub cxt_four { sort $m 1,2 }
298 sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 }
300 sub cxt_six { sort test_if_scalar 1,2 }
303 # test against a reentrancy bug
306 sub compare { $a cmp $b }
307 sub reenter { my @force = sort compare qw/a b/ }
310 my($def, $init) = (0, 0);
312 $def = 1 if defined $Bar::a;
313 Bar::reenter() unless $init++;
316 print ("@b" eq '1 2 3 4' ? "ok 56\n" : "not ok 56\n");
317 print "# x = '@b'\n";
318 print !$def ? "ok 57\n" : "not ok 57\n";
323 sub routine { "one", "two" };
324 @a = sort(routine(1));
325 print "@a" eq "one two" ? "ok 58\n" : "not ok 58\n";
331 print "not " unless $_[0] eq $_[1];
332 print "ok $test - $_[2]\n";
333 print "#[$_[0]] ne [$_[1]]\n" unless $_[0] eq $_[1];
337 # check for in-place optimisation of @a = sort @a
341 @g = (3,2,1); $r1 = \$g[2]; @g = sort @g; $r2 = \$g[0];
342 ok "$r1-@g", "$r2-1 2 3", "inplace sort of global";
344 @a = qw(b a c); $r1 = \$a[1]; @a = sort @a; $r2 = \$a[0];
345 ok "$r1-@a", "$r2-a b c", "inplace sort of lexical";
347 @g = (2,3,1); $r1 = \$g[1]; @g = sort { $b <=> $a } @g; $r2 = \$g[0];
348 ok "$r1-@g", "$r2-3 2 1", "inplace reversed sort of global";
351 $r1 = \$g[1]; @g = sort { $a<$b?1:$a>$b?-1:0 } @g; $r2 = \$g[0];
352 ok "$r1-@g", "$r2-3 2 1", "inplace custom sort of global";
354 sub mysort { $b cmp $a };
355 @a = qw(b c a); $r1 = \$a[1]; @a = sort mysort @a; $r2 = \$a[0];
356 ok "$r1-@a", "$r2-c b a", "inplace sort with function of lexical";
360 tie @t, 'Tie::StdArray';
362 @t = qw(b c a); @t = sort @t;
363 ok "@t", "a b c", "inplace sort of tied array";
365 @t = qw(b c a); @t = sort mysort @t;
366 ok "@t", "c b a", "inplace sort of tied array with function";
368 # [perl #29790] don't optimise @a = ('a', sort @a) !
370 @g = (3,2,1); @g = ('0', sort @g);
371 ok "@g", "0 1 2 3", "un-inplace sort of global";
372 @g = (3,2,1); @g = (sort(@g),'4');
373 ok "@g", "1 2 3 4", "un-inplace sort of global 2";
375 @a = qw(b a c); @a = ('x', sort @a);
376 ok "@a", "x a b c", "un-inplace sort of lexical";
377 @a = qw(b a c); @a = ((sort @a), 'x');
378 ok "@a", "a b c x", "un-inplace sort of lexical 2";
380 @g = (2,3,1); @g = ('0', sort { $b <=> $a } @g);
381 ok "@g", "0 3 2 1", "un-inplace reversed sort of global";
382 @g = (2,3,1); @g = ((sort { $b <=> $a } @g),'4');
383 ok "@g", "3 2 1 4", "un-inplace reversed sort of global 2";
385 @g = (2,3,1); @g = ('0', sort { $a<$b?1:$a>$b?-1:0 } @g);
386 ok "@g", "0 3 2 1", "un-inplace custom sort of global";
387 @g = (2,3,1); @g = ((sort { $a<$b?1:$a>$b?-1:0 } @g),'4');
388 ok "@g", "3 2 1 4", "un-inplace custom sort of global 2";
390 @a = qw(b c a); @a = ('x', sort mysort @a);
391 ok "@a", "x c b a", "un-inplace sort with function of lexical";
392 @a = qw(b c a); @a = ((sort mysort @a),'x');
393 ok "@a", "c b a x", "un-inplace sort with function of lexical 2";
396 # Test optimisations of reversed sorts. As we now guarantee stability by
397 # default, # optimisations which do not provide this are bogus.
401 use overload (qw("" stringify 0+ numify fallback 1));
404 bless [$_[1], $_[2]], $_[0];
407 sub stringify { $_[0]->[0] }
409 sub numify { $_[0]->[1] }
414 map {new Oscalar $_, $count++} qw(A A A B B B C C C);
417 my @input = &generate;
418 my @output = sort @input;
419 ok join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", "Simple stable sort";
422 @input = sort @input;
423 ok join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
424 "Simple stable in place sort";
426 # This won't be very interesting
428 @output = sort {$a <=> $b} @input;
429 ok "@output", "A A A B B B C C C", 'stable $a <=> $b sort';
432 @output = sort {$a cmp $b} @input;
433 ok join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", 'stable $a cmp $b sort';
436 @input = sort {$a cmp $b} @input;
437 ok join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
438 'stable $a cmp $b in place sort';
441 @output = sort {$b cmp $a} @input;
442 ok join(" ", map {0+$_} @output), "6 7 8 3 4 5 0 1 2", 'stable $b cmp $a sort';
445 @input = sort {$b cmp $a} @input;
446 ok join(" ", map {0+$_} @input), "6 7 8 3 4 5 0 1 2",
447 'stable $b cmp $a in place sort';
450 @output = reverse sort @input;
451 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", "Reversed stable sort";
454 @input = reverse sort @input;
455 ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
456 "Reversed stable in place sort";
459 my $output = reverse sort @input;
460 ok $output, "CCCBBBAAA", "Reversed stable sort in scalar context";
464 @output = reverse sort {$a cmp $b} @input;
465 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
466 'reversed stable $a cmp $b sort';
469 @input = reverse sort {$a cmp $b} @input;
470 ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
471 'revesed stable $a cmp $b in place sort';
474 $output = reverse sort {$a cmp $b} @input;
475 ok $output, "CCCBBBAAA", 'Reversed stable $a cmp $b sort in scalar context';
478 @output = reverse sort {$b cmp $a} @input;
479 ok join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
480 'reversed stable $b cmp $a sort';
483 @input = reverse sort {$b cmp $a} @input;
484 ok join(" ", map {0+$_} @input), "2 1 0 5 4 3 8 7 6",
485 'revesed stable $b cmp $a in place sort';
488 $output = reverse sort {$b cmp $a} @input;
489 ok $output, "AAABBBCCC", 'Reversed stable $b cmp $a sort in scalar context';
492 # Something complex enough to defeat any constant folding optimiser
497 @output = reverse sort {stuff || $a cmp $b} @input;
498 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
499 'reversed stable complex sort';
502 @input = reverse sort {stuff || $a cmp $b} @input;
503 ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
504 'revesed stable complex in place sort';
507 $output = reverse sort {stuff || $a cmp $b } @input;
508 ok $output, "CCCBBBAAA", 'Reversed stable complex sort in scalar context';
514 @output = sortr &generate;
515 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
516 'reversed stable sort return list context';
517 $output = sortr &generate;
518 ok $output, "CCCBBBAAA",
519 'reversed stable sort return scalar context';
522 reverse sort {$a cmp $b} @_;
525 @output = sortcmpr &generate;
526 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
527 'reversed stable $a cmp $b sort return list context';
528 $output = sortcmpr &generate;
529 ok $output, "CCCBBBAAA",
530 'reversed stable $a cmp $b sort return scalar context';
533 reverse sort {$b cmp $a} @_;
536 @output = sortcmprba &generate;
537 ok join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
538 'reversed stable $b cmp $a sort return list context';
539 $output = sortcmprba &generate;
540 ok $output, "AAABBBCCC",
541 'reversed stable $b cmp $a sort return scalar context';
544 reverse sort {stuff || $a cmp $b} @_;
547 @output = sortcmpr &generate;
548 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
549 'reversed stable complex sort return list context';
550 $output = sortcmpr &generate;
551 ok $output, "CCCBBBAAA",
552 'reversed stable complex sort return scalar context';
554 # And now with numbers
558 map {new Oscalar $count++, $_} 0, 0, 0, 1, 1, 1, 2, 2, 2;
561 # This won't be very interesting
563 @output = sort {$a cmp $b} @input;
564 ok "@output", "A B C D E F G H I", 'stable $a cmp $b sort';
567 @output = sort {$a <=> $b} @input;
568 ok "@output", "A B C D E F G H I", 'stable $a <=> $b sort';
571 @input = sort {$a <=> $b} @input;
572 ok "@input", "A B C D E F G H I", 'stable $a <=> $b in place sort';
575 @output = sort {$b <=> $a} @input;
576 ok "@output", "G H I D E F A B C", 'stable $b <=> $a sort';
579 @input = sort {$b <=> $a} @input;
580 ok "@input", "G H I D E F A B C", 'stable $b <=> $a in place sort';
582 # test that optimized {$b cmp $a} and {$b <=> $a} remain stable
583 # (new in 5.9) without overloading
585 @b = sort { $b <=> $a } @input = qw/5first 6first 5second 6second/;
586 ok "@b" , "6first 6second 5first 5second", "optimized {$b <=> $a} without overloading" ;
587 @input = sort {$b <=> $a} @input;
588 ok "@input" , "6first 6second 5first 5second","inline optimized {$b <=> $a} without overloading" ;
591 # These two are actually doing string cmp on 0 1 and 2
593 @output = reverse sort @input;
594 ok "@output", "I H G F E D C B A", "Reversed stable sort";
597 @input = reverse sort @input;
598 ok "@input", "I H G F E D C B A", "Reversed stable in place sort";
601 $output = reverse sort @input;
602 ok $output, "IHGFEDCBA", "Reversed stable sort in scalar context";
605 @output = reverse sort {$a <=> $b} @input;
606 ok "@output", "I H G F E D C B A", 'reversed stable $a <=> $b sort';
609 @input = reverse sort {$a <=> $b} @input;
610 ok "@input", "I H G F E D C B A", 'revesed stable $a <=> $b in place sort';
613 $output = reverse sort {$a <=> $b} @input;
614 ok $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort in scalar context';
617 @output = reverse sort {$b <=> $a} @input;
618 ok "@output", "C B A F E D I H G", 'reversed stable $b <=> $a sort';
621 @input = reverse sort {$b <=> $a} @input;
622 ok "@input", "C B A F E D I H G", 'revesed stable $b <=> $a in place sort';
625 $output = reverse sort {$b <=> $a} @input;
626 ok $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort in scalar context';
629 @output = reverse sort {stuff || $a <=> $b} @input;
630 ok "@output", "I H G F E D C B A", 'reversed stable complex sort';
633 @input = reverse sort {stuff || $a <=> $b} @input;
634 ok "@input", "I H G F E D C B A", 'revesed stable complex in place sort';
637 $output = reverse sort {stuff || $a <=> $b} @input;
638 ok $output, "IHGFEDCBA", 'reversed stable complex sort in scalar context';
641 reverse sort {$a <=> $b} @_;
644 @output = sortnumr &generate1;
645 ok "@output", "I H G F E D C B A",
646 'reversed stable $a <=> $b sort return list context';
647 $output = sortnumr &generate1;
648 ok $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort return scalar context';
651 reverse sort {$b <=> $a} @_;
654 @output = sortnumrba &generate1;
655 ok "@output", "C B A F E D I H G",
656 'reversed stable $b <=> $a sort return list context';
657 $output = sortnumrba &generate1;
658 ok $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort return scalar context';
661 reverse sort {stuff || $a <=> $b} @_;
664 @output = sortnumrq &generate1;
665 ok "@output", "I H G F E D C B A",
666 'reversed stable complex sort return list context';
667 $output = sortnumrq &generate1;
668 ok $output, "IHGFEDCBA", 'reversed stable complex sort return scalar context';
670 @output = reverse (sort(qw(C A B)), 0);
671 ok "@output", "0 C B A", 'reversed sort with trailing argument';
673 @output = reverse (0, sort(qw(C A B)));
674 ok "@output", "C B A 0", 'reversed sort with leading argument';
676 eval { @output = sort {goto sub {}} 1,2; };
677 print(($@ =~ /^Can't goto subroutine outside a subroutine/ ?
682 sub goto_sub {goto sub{}}
683 eval { @output = sort goto_sub 1,2; };
684 print(($@ =~ /^Can't goto subroutine from a sort sub/ ?
689 eval { @output = sort {goto label} 1,2; };
690 print(($@ =~ /^Can't "goto" out of a pseudo block/ ?
695 sub goto_label {goto label}
696 label: eval { @output = sort goto_label 1,2; };
697 print(($@ =~ /^Can't "goto" out of a pseudo block/ ?
702 sub self_immolate {undef &self_immolate; $a<=>$b}
703 eval { @output = sort self_immolate 1,2,3 };
704 print(($@ =~ /^Can't undef active subroutine/ ?
714 if (!defined($n)) { # No arg means we're being called by sort()
717 if ($n<5) { rec($n+1); }
718 else { () = sort rec 1,2; }
720 $failed = 1 if !defined $n;
724 print((!$failed ? "ok " : "not ok "), $test++, " - sort from active sub\n");
727 # $a and $b are set in the package the sort() is called from,
728 # *not* the package the sort sub is in. This is longstanding
729 # de facto behaviour that shouldn't be broken.
732 () = sort OtherPack::foo 1,2,3,4;
738 $answer = "not ok " if
739 defined($a) || defined($b) || !defined($main::a) || !defined($main::b);
740 $main::a <=> $main::b;
744 print $answer, $test++, "\n";
747 # Bug 36430 - sort called in package2 while a
748 # sort in package1 is active should set $package2::a/b.
751 my @list = sort { A::min(@$a) <=> A::min(@$b) }
752 [3, 1, 5], [2, 4], [0];
754 print $answer, $test++, "\n";
759 $answer = "not ok " if !defined($a) || !defined($b);
765 # Bug 7567 - an array shouldn't be modifiable while it's being
767 eval { @a=(1..8); @a = sort { @a = (0) } @a; };
769 print(($@ =~ /^Modification of a read-only value attempted/ ?
774 # Sorting shouldn't increase the refcount of a sub
775 sub foo {(1+$a) <=> (1+$b)}
776 my $refcnt = &Internals::SvREFCNT(\&foo);
777 @output = sort foo 3,7,9;
779 ok($refcnt, &Internals::SvREFCNT(\&foo), "sort sub refcnt");
781 # Sorting a read-only array in-place shouldn't be allowed
782 my @readonly = (1..10);
783 Internals::SvREADONLY(@readonly, 1);
784 eval { @readonly = sort @readonly; };
785 print(($@ =~ /^Modification of a read-only value attempted/ ?
790 # Using return() should be okay even in a deeper context
791 @b = sort {while (1) {return ($a <=> $b)} } 1..10;
792 ok("@b", "1 2 3 4 5 6 7 8 9 10", "return within loop");
794 # Using return() should be okay even if there are other items
795 # on the stack at the time.
796 @b = sort {$_ = ($a<=>$b) + do{return $b<=> $a}} 1..10;
797 ok("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack");
799 # As above, but with a sort sub rather than a sort block.
800 sub ret_with_stacked { $_ = ($a<=>$b) + do {return $b <=> $a} }
801 @b = sort ret_with_stacked 1..10;
802 ok("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack");