This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
a regex in STDOUT destructor coredumped because regex pad already
[perl5.git] / t / op / sort.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7 use warnings;
8 print "1..129\n";
9
10 # these shouldn't hang
11 {
12     no warnings;
13     sort { for ($_ = 0;; $_++) {} } @a;
14     sort { while(1) {}            } @a;
15     sort { while(1) { last; }     } @a;
16     sort { while(0) { last; }     } @a;
17 }
18
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
22 my $upperfirst = 'A' lt 'a';
23
24 # Beware: in future this may become hairier because of possible
25 # collation complications: qw(A a B b) can be sorted at least as
26 # any of the following
27 #
28 #       A a B b
29 #       A B a b
30 #       a b A B
31 #       a A b B
32 #
33 # All the above orders make sense.
34 #
35 # That said, EBCDIC sorts all small letters first, as opposed
36 # to ASCII which sorts all big letters first.
37
38 @harry = ('dog','cat','x','Cain','Abel');
39 @george = ('gone','chased','yz','punished','Axed');
40
41 $x = join('', sort @harry);
42 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
43 print "# 1: x = '$x', expected = '$expected'\n";
44 print ($x eq $expected ? "ok 1\n" : "not ok 1\n");
45
46 $x = join('', sort( Backwards @harry));
47 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
48 print "# 2: x = '$x', expected = '$expected'\n";
49 print ($x eq $expected ? "ok 2\n" : "not ok 2\n");
50
51 $x = join('', sort( Backwards_stacked @harry));
52 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
53 print "# 3: x = '$x', expected = '$expected'\n";
54 print ($x eq $expected ? "ok 3\n" : "not ok 3\n");
55
56 $x = join('', sort @george, 'to', @harry);
57 $expected = $upperfirst ?
58     'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
59     'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
60 print "# 4: x = '$x', expected = '$expected'\n";
61 print ($x eq $expected ?"ok 4\n":"not ok 4\n");
62
63 @a = ();
64 @b = reverse @a;
65 print ("@b" eq "" ? "ok 5\n" : "not ok 5 (@b)\n");
66
67 @a = (1);
68 @b = reverse @a;
69 print ("@b" eq "1" ? "ok 6\n" : "not ok 6 (@b)\n");
70
71 @a = (1,2);
72 @b = reverse @a;
73 print ("@b" eq "2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
74
75 @a = (1,2,3);
76 @b = reverse @a;
77 print ("@b" eq "3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
78
79 @a = (1,2,3,4);
80 @b = reverse @a;
81 print ("@b" eq "4 3 2 1" ? "ok 9\n" : "not ok 9 (@b)\n");
82
83 @a = (10,2,3,4);
84 @b = sort {$a <=> $b;} @a;
85 print ("@b" eq "2 3 4 10" ? "ok 10\n" : "not ok 10 (@b)\n");
86
87 $sub = 'Backwards';
88 $x = join('', sort $sub @harry);
89 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
90 print "# 11: x = $x, expected = '$expected'\n";
91 print ($x eq $expected ? "ok 11\n" : "not ok 11\n");
92
93 $sub = 'Backwards_stacked';
94 $x = join('', sort $sub @harry);
95 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
96 print "# 12: x = $x, expected = '$expected'\n";
97 print ($x eq $expected ? "ok 12\n" : "not ok 12\n");
98
99 # literals, combinations
100
101 @b = sort (4,1,3,2);
102 print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
103 print "# x = '@b'\n";
104
105 @b = sort grep { $_ } (4,1,3,2);
106 print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
107 print "# x = '@b'\n";
108
109 @b = sort map { $_ } (4,1,3,2);
110 print ("@b" eq '1 2 3 4' ? "ok 15\n" : "not ok 15\n");
111 print "# x = '@b'\n";
112
113 @b = sort reverse (4,1,3,2);
114 print ("@b" eq '1 2 3 4' ? "ok 16\n" : "not ok 16\n");
115 print "# x = '@b'\n";
116
117 # redefining sort sub inside the sort sub should fail
118 sub twoface { *twoface = sub { $a <=> $b }; &twoface }
119 eval { @b = sort twoface 4,1,3,2 };
120 print ($@ =~ /redefine active sort/ ? "ok 17\n" : "not ok 17\n");
121
122 # redefining sort subs outside the sort should not fail
123 eval { no warnings 'redefine'; *twoface = sub { &Backwards } };
124 print $@ ? "not ok 18\n" : "ok 18\n";
125
126 eval { @b = sort twoface 4,1,3,2 };
127 print ("@b" eq '4 3 2 1' ? "ok 19\n" : "not ok 19 |@b|\n");
128
129 {
130   no warnings 'redefine';
131   *twoface = sub { *twoface = *Backwards; $a <=> $b };
132 }
133 eval { @b = sort twoface 4,1 };
134 print ($@ =~ /redefine active sort/ ? "ok 20\n" : "not ok 20\n");
135
136 {
137   no warnings 'redefine';
138   *twoface = sub {
139                  eval 'sub twoface { $a <=> $b }';
140                  die($@ =~ /redefine active sort/ ? "ok 21\n" : "not ok 21\n");
141                  $a <=> $b;
142                };
143 }
144 eval { @b = sort twoface 4,1 };
145 print $@ ? "$@" : "not ok 21\n";
146
147 eval <<'CODE';
148     my @result = sort main'Backwards 'one', 'two';
149 CODE
150 print $@ ? "not ok 22\n# $@" : "ok 22\n";
151
152 eval <<'CODE';
153     # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
154     my @result = sort 'one', 'two';
155 CODE
156 print $@ ? "not ok 23\n# $@" : "ok 23\n";
157
158 {
159   my $sortsub = \&Backwards;
160   my $sortglob = *Backwards;
161   my $sortglobr = \*Backwards;
162   my $sortname = 'Backwards';
163   @b = sort $sortsub 4,1,3,2;
164   print ("@b" eq '4 3 2 1' ? "ok 24\n" : "not ok 24 |@b|\n");
165   @b = sort $sortglob 4,1,3,2;
166   print ("@b" eq '4 3 2 1' ? "ok 25\n" : "not ok 25 |@b|\n");
167   @b = sort $sortname 4,1,3,2;
168   print ("@b" eq '4 3 2 1' ? "ok 26\n" : "not ok 26 |@b|\n");
169   @b = sort $sortglobr 4,1,3,2;
170   print ("@b" eq '4 3 2 1' ? "ok 27\n" : "not ok 27 |@b|\n");
171 }
172
173 {
174   my $sortsub = \&Backwards_stacked;
175   my $sortglob = *Backwards_stacked;
176   my $sortglobr = \*Backwards_stacked;
177   my $sortname = 'Backwards_stacked';
178   @b = sort $sortsub 4,1,3,2;
179   print ("@b" eq '4 3 2 1' ? "ok 28\n" : "not ok 28 |@b|\n");
180   @b = sort $sortglob 4,1,3,2;
181   print ("@b" eq '4 3 2 1' ? "ok 29\n" : "not ok 29 |@b|\n");
182   @b = sort $sortname 4,1,3,2;
183   print ("@b" eq '4 3 2 1' ? "ok 30\n" : "not ok 30 |@b|\n");
184   @b = sort $sortglobr 4,1,3,2;
185   print ("@b" eq '4 3 2 1' ? "ok 31\n" : "not ok 31 |@b|\n");
186 }
187
188 {
189   local $sortsub = \&Backwards;
190   local $sortglob = *Backwards;
191   local $sortglobr = \*Backwards;
192   local $sortname = 'Backwards';
193   @b = sort $sortsub 4,1,3,2;
194   print ("@b" eq '4 3 2 1' ? "ok 32\n" : "not ok 32 |@b|\n");
195   @b = sort $sortglob 4,1,3,2;
196   print ("@b" eq '4 3 2 1' ? "ok 33\n" : "not ok 33 |@b|\n");
197   @b = sort $sortname 4,1,3,2;
198   print ("@b" eq '4 3 2 1' ? "ok 34\n" : "not ok 34 |@b|\n");
199   @b = sort $sortglobr 4,1,3,2;
200   print ("@b" eq '4 3 2 1' ? "ok 35\n" : "not ok 35 |@b|\n");
201 }
202
203 {
204   local $sortsub = \&Backwards_stacked;
205   local $sortglob = *Backwards_stacked;
206   local $sortglobr = \*Backwards_stacked;
207   local $sortname = 'Backwards_stacked';
208   @b = sort $sortsub 4,1,3,2;
209   print ("@b" eq '4 3 2 1' ? "ok 36\n" : "not ok 36 |@b|\n");
210   @b = sort $sortglob 4,1,3,2;
211   print ("@b" eq '4 3 2 1' ? "ok 37\n" : "not ok 37 |@b|\n");
212   @b = sort $sortname 4,1,3,2;
213   print ("@b" eq '4 3 2 1' ? "ok 38\n" : "not ok 38 |@b|\n");
214   @b = sort $sortglobr 4,1,3,2;
215   print ("@b" eq '4 3 2 1' ? "ok 39\n" : "not ok 39 |@b|\n");
216 }
217
218 ## exercise sort builtins... ($a <=> $b already tested)
219 @a = ( 5, 19, 1996, 255, 90 );
220 @b = sort {
221     my $dummy;          # force blockness
222     return $b <=> $a
223 } @a;
224 print ("@b" eq '1996 255 90 19 5' ? "ok 40\n" : "not ok 40\n");
225 print "# x = '@b'\n";
226 $x = join('', sort { $a cmp $b } @harry);
227 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
228 print ($x eq $expected ? "ok 41\n" : "not ok 41\n");
229 print "# x = '$x'; expected = '$expected'\n";
230 $x = join('', sort { $b cmp $a } @harry);
231 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
232 print ($x eq $expected ? "ok 42\n" : "not ok 42\n");
233 print "# x = '$x'; expected = '$expected'\n";
234 {
235     use integer;
236     @b = sort { $a <=> $b } @a;
237     print ("@b" eq '5 19 90 255 1996' ? "ok 43\n" : "not ok 43\n");
238     print "# x = '@b'\n";
239     @b = sort { $b <=> $a } @a;
240     print ("@b" eq '1996 255 90 19 5' ? "ok 44\n" : "not ok 44\n");
241     print "# x = '@b'\n";
242     $x = join('', sort { $a cmp $b } @harry);
243     $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
244     print ($x eq $expected ? "ok 45\n" : "not ok 45\n");
245     print "# x = '$x'; expected = '$expected'\n";
246     $x = join('', sort { $b cmp $a } @harry);
247     $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
248     print ($x eq $expected ? "ok 46\n" : "not ok 46\n");
249     print "# x = '$x'; expected = '$expected'\n";
250 }
251
252 # test that an optimized-away comparison block doesn't take any other
253 # arguments away with it
254 $x = join('', sort { $a <=> $b } 3, 1, 2);
255 print $x eq "123" ? "ok 47\n" : "not ok 47\n";
256
257 # test sorting in non-main package
258 package Foo;
259 @a = ( 5, 19, 1996, 255, 90 );
260 @b = sort { $b <=> $a } @a;
261 print ("@b" eq '1996 255 90 19 5' ? "ok 48\n" : "not ok 48\n");
262 print "# x = '@b'\n";
263
264 @b = sort main::Backwards_stacked @a;
265 print ("@b" eq '90 5 255 1996 19' ? "ok 49\n" : "not ok 49\n");
266 print "# x = '@b'\n";
267
268 # check if context for sort arguments is handled right
269
270 $test = 49;
271 sub test_if_list {
272     my $gimme = wantarray;
273     print "not " unless $gimme;
274     ++$test;
275     print "ok $test\n";
276 }
277 my $m = sub { $a <=> $b };
278
279 sub cxt_one { sort $m test_if_list() }
280 cxt_one();
281 sub cxt_two { sort { $a <=> $b } test_if_list() }
282 cxt_two();
283 sub cxt_three { sort &test_if_list() }
284 cxt_three();
285
286 sub test_if_scalar {
287     my $gimme = wantarray;
288     print "not " if $gimme or !defined($gimme);
289     ++$test;
290     print "ok $test\n";
291 }
292
293 $m = \&test_if_scalar;
294 sub cxt_four { sort $m 1,2 }
295 @x = cxt_four();
296 sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 }
297 @x = cxt_five();
298 sub cxt_six { sort test_if_scalar 1,2 }
299 @x = cxt_six();
300
301 # test against a reentrancy bug
302 {
303     package Bar;
304     sub compare { $a cmp $b }
305     sub reenter { my @force = sort compare qw/a b/ }
306 }
307 {
308     my($def, $init) = (0, 0);
309     @b = sort {
310         $def = 1 if defined $Bar::a;
311         Bar::reenter() unless $init++;
312         $a <=> $b
313     } qw/4 3 1 2/;
314     print ("@b" eq '1 2 3 4' ? "ok 56\n" : "not ok 56\n");
315     print "# x = '@b'\n";
316     print !$def ? "ok 57\n" : "not ok 57\n";
317 }
318
319 # Bug 19991001.003
320 {
321     sub routine { "one", "two" };
322     @a = sort(routine(1));
323     print "@a" eq "one two" ? "ok 58\n" : "not ok 58\n";
324 }
325
326
327 my $test = 59;
328 sub ok {
329     print "not " unless $_[0] eq $_[1];
330     print "ok $test - $_[2]\n";
331     print "#[$_[0]] ne [$_[1]]\n" unless $_[0] eq $_[1];
332     $test++;
333 }
334
335 # check for in-place optimisation of @a = sort @a
336 {
337     my ($r1,$r2,@a);
338     our @g;
339     @g = (3,2,1); $r1 = \$g[2]; @g = sort @g; $r2 = \$g[0];
340     ok "$r1-@g", "$r2-1 2 3", "inplace sort of global";
341
342     @a = qw(b a c); $r1 = \$a[1]; @a = sort @a; $r2 = \$a[0];
343     ok "$r1-@a", "$r2-a b c", "inplace sort of lexical";
344
345     @g = (2,3,1); $r1 = \$g[1]; @g = sort { $b <=> $a } @g; $r2 = \$g[0];
346     ok "$r1-@g", "$r2-3 2 1", "inplace reversed sort of global";
347
348     @g = (2,3,1);
349     $r1 = \$g[1]; @g = sort { $a<$b?1:$a>$b?-1:0 } @g; $r2 = \$g[0];
350     ok "$r1-@g", "$r2-3 2 1", "inplace custom sort of global";
351
352     sub mysort { $b cmp $a };
353     @a = qw(b c a); $r1 = \$a[1]; @a = sort mysort @a; $r2 = \$a[0];
354     ok "$r1-@a", "$r2-c b a", "inplace sort with function of lexical";
355
356     use Tie::Array;
357     my @t;
358     tie @t, 'Tie::StdArray';
359
360     @t = qw(b c a); @t = sort @t;
361     ok "@t", "a b c", "inplace sort of tied array";
362
363     @t = qw(b c a); @t = sort mysort @t;
364     ok "@t", "c b a", "inplace sort of tied array with function";
365
366     #  [perl #29790] don't optimise @a = ('a', sort @a) !
367
368     @g = (3,2,1); @g = ('0', sort @g);
369     ok "@g", "0 1 2 3", "un-inplace sort of global";
370     @g = (3,2,1); @g = (sort(@g),'4');
371     ok "@g", "1 2 3 4", "un-inplace sort of global 2";
372
373     @a = qw(b a c); @a = ('x', sort @a);
374     ok "@a", "x a b c", "un-inplace sort of lexical";
375     @a = qw(b a c); @a = ((sort @a), 'x');
376     ok "@a", "a b c x", "un-inplace sort of lexical 2";
377
378     @g = (2,3,1); @g = ('0', sort { $b <=> $a } @g);
379     ok "@g", "0 3 2 1", "un-inplace reversed sort of global";
380     @g = (2,3,1); @g = ((sort { $b <=> $a } @g),'4');
381     ok "@g", "3 2 1 4", "un-inplace reversed sort of global 2";
382
383     @g = (2,3,1); @g = ('0', sort { $a<$b?1:$a>$b?-1:0 } @g);
384     ok "@g", "0 3 2 1", "un-inplace custom sort of global";
385     @g = (2,3,1); @g = ((sort { $a<$b?1:$a>$b?-1:0 } @g),'4');
386     ok "@g", "3 2 1 4", "un-inplace custom sort of global 2";
387
388     @a = qw(b c a); @a = ('x', sort mysort @a);
389     ok "@a", "x c b a", "un-inplace sort with function of lexical";
390     @a = qw(b c a); @a = ((sort mysort @a),'x');
391     ok "@a", "c b a x", "un-inplace sort with function of lexical 2";
392 }
393
394 # Test optimisations of reversed sorts. As we now guarantee stability by
395 # default, # optimisations which do not provide this are bogus.
396
397 {
398     package Oscalar;
399     use overload (qw("" stringify 0+ numify fallback 1));
400
401     sub new {
402         bless [$_[1], $_[2]], $_[0];
403     }
404
405     sub stringify { $_[0]->[0] }
406
407     sub numify { $_[0]->[1] }
408 }
409
410 sub generate {
411     my $count = 0;
412     map {new Oscalar $_, $count++} qw(A A A B B B C C C);
413 }
414
415 my @input = &generate;
416 my @output = sort @input;
417 ok join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", "Simple stable sort";
418
419 @input = &generate;
420 @input = sort @input;
421 ok join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
422     "Simple stable in place sort";
423
424 # This won't be very interesting
425 @input = &generate;
426 @output = sort {$a <=> $b} @input;
427 ok "@output", "A A A B B B C C C", 'stable $a <=> $b sort';
428
429 @input = &generate;
430 @output = sort {$a cmp $b} @input;
431 ok join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", 'stable $a cmp $b sort';
432
433 @input = &generate;
434 @input = sort {$a cmp $b} @input;
435 ok join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
436     'stable $a cmp $b in place sort';
437
438 @input = &generate;
439 @output = sort {$b cmp $a} @input;
440 ok join(" ", map {0+$_} @output), "6 7 8 3 4 5 0 1 2", 'stable $b cmp $a sort';
441
442 @input = &generate;
443 @input = sort {$b cmp $a} @input;
444 ok join(" ", map {0+$_} @input), "6 7 8 3 4 5 0 1 2",
445     'stable $b cmp $a in place sort';
446
447 @input = &generate;
448 @output = reverse sort @input;
449 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", "Reversed stable sort";
450
451 @input = &generate;
452 @input = reverse sort @input;
453 ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
454     "Reversed stable in place sort";
455
456 @input = &generate;
457 my $output = reverse sort @input;
458 ok $output, "CCCBBBAAA", "Reversed stable sort in scalar context";
459
460
461 @input = &generate;
462 @output = reverse sort {$a cmp $b} @input;
463 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
464     'reversed stable $a cmp $b sort';
465
466 @input = &generate;
467 @input = reverse sort {$a cmp $b} @input;
468 ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
469     'revesed stable $a cmp $b in place sort';
470
471 @input = &generate;
472 $output = reverse sort {$a cmp $b} @input;
473 ok $output, "CCCBBBAAA", 'Reversed stable $a cmp $b sort in scalar context';
474
475 @input = &generate;
476 @output = reverse sort {$b cmp $a} @input;
477 ok join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
478     'reversed stable $b cmp $a sort';
479
480 @input = &generate;
481 @input = reverse sort {$b cmp $a} @input;
482 ok join(" ", map {0+$_} @input), "2 1 0 5 4 3 8 7 6",
483     'revesed stable $b cmp $a in place sort';
484
485 @input = &generate;
486 $output = reverse sort {$b cmp $a} @input;
487 ok $output, "AAABBBCCC", 'Reversed stable $b cmp $a sort in scalar context';
488
489 sub stuff {
490     # Something complex enough to defeat any constant folding optimiser
491     $$ - $$;
492 }
493
494 @input = &generate;
495 @output = reverse sort {stuff || $a cmp $b} @input;
496 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
497     'reversed stable complex sort';
498
499 @input = &generate;
500 @input = reverse sort {stuff || $a cmp $b} @input;
501 ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
502     'revesed stable complex in place sort';
503
504 @input = &generate;
505 $output = reverse sort {stuff || $a cmp $b } @input;
506 ok $output, "CCCBBBAAA", 'Reversed stable complex sort in scalar context';
507
508 sub sortr {
509     reverse sort @_;
510 }
511
512 @output = sortr &generate;
513 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
514     'reversed stable sort return list context';
515 $output = sortr &generate;
516 ok $output, "CCCBBBAAA",
517     'reversed stable sort return scalar context';
518
519 sub sortcmpr {
520     reverse sort {$a cmp $b} @_;
521 }
522
523 @output = sortcmpr &generate;
524 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
525     'reversed stable $a cmp $b sort return list context';
526 $output = sortcmpr &generate;
527 ok $output, "CCCBBBAAA",
528     'reversed stable $a cmp $b sort return scalar context';
529
530 sub sortcmprba {
531     reverse sort {$b cmp $a} @_;
532 }
533
534 @output = sortcmprba &generate;
535 ok join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
536     'reversed stable $b cmp $a sort return list context';
537 $output = sortcmprba &generate;
538 ok $output, "AAABBBCCC",
539 'reversed stable $b cmp $a sort return scalar context';
540
541 sub sortcmprq {
542     reverse sort {stuff || $a cmp $b} @_;
543 }
544
545 @output = sortcmpr &generate;
546 ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
547     'reversed stable complex sort return list context';
548 $output = sortcmpr &generate;
549 ok $output, "CCCBBBAAA",
550     'reversed stable complex sort return scalar context';
551
552 # And now with numbers
553
554 sub generate1 {
555     my $count = 'A';
556     map {new Oscalar $count++, $_} 0, 0, 0, 1, 1, 1, 2, 2, 2;
557 }
558
559 # This won't be very interesting
560 @input = &generate1;
561 @output = sort {$a cmp $b} @input;
562 ok "@output", "A B C D E F G H I", 'stable $a cmp $b sort';
563
564 @input = &generate1;
565 @output = sort {$a <=> $b} @input;
566 ok "@output", "A B C D E F G H I", 'stable $a <=> $b sort';
567
568 @input = &generate1;
569 @input = sort {$a <=> $b} @input;
570 ok "@input", "A B C D E F G H I", 'stable $a <=> $b in place sort';
571
572 @input = &generate1;
573 @output = sort {$b <=> $a} @input;
574 ok "@output", "G H I D E F A B C", 'stable $b <=> $a sort';
575
576 @input = &generate1;
577 @input = sort {$b <=> $a} @input;
578 ok "@input", "G H I D E F A B C", 'stable $b <=> $a in place sort';
579
580 # test that optimized {$b cmp $a} and {$b <=> $a} remain stable
581 # (new in 5.9) without overloading
582 { no warnings;
583 @b = sort { $b <=> $a } @input = qw/5first 6first 5second 6second/;
584 ok "@b" , "6first 6second 5first 5second", "optimized {$b <=> $a} without overloading" ;
585 @input = sort {$b <=> $a} @input;
586 ok "@input" , "6first 6second 5first 5second","inline optimized {$b <=> $a} without overloading" ;
587 };
588
589 # These two are actually doing string cmp on 0 1 and 2
590 @input = &generate1;
591 @output = reverse sort @input;
592 ok "@output", "I H G F E D C B A", "Reversed stable sort";
593
594 @input = &generate1;
595 @input = reverse sort @input;
596 ok "@input", "I H G F E D C B A", "Reversed stable in place sort";
597
598 @input = &generate1;
599 $output = reverse sort @input;
600 ok $output, "IHGFEDCBA", "Reversed stable sort in scalar context";
601
602 @input = &generate1;
603 @output = reverse sort {$a <=> $b} @input;
604 ok "@output", "I H G F E D C B A", 'reversed stable $a <=> $b sort';
605
606 @input = &generate1;
607 @input = reverse sort {$a <=> $b} @input;
608 ok "@input", "I H G F E D C B A", 'revesed stable $a <=> $b in place sort';
609
610 @input = &generate1;
611 $output = reverse sort {$a <=> $b} @input;
612 ok $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort in scalar context';
613
614 @input = &generate1;
615 @output = reverse sort {$b <=> $a} @input;
616 ok "@output", "C B A F E D I H G", 'reversed stable $b <=> $a sort';
617
618 @input = &generate1;
619 @input = reverse sort {$b <=> $a} @input;
620 ok "@input", "C B A F E D I H G", 'revesed stable $b <=> $a in place sort';
621
622 @input = &generate1;
623 $output = reverse sort {$b <=> $a} @input;
624 ok $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort in scalar context';
625
626 @input = &generate1;
627 @output = reverse sort {stuff || $a <=> $b} @input;
628 ok "@output", "I H G F E D C B A", 'reversed stable complex sort';
629
630 @input = &generate1;
631 @input = reverse sort {stuff || $a <=> $b} @input;
632 ok "@input", "I H G F E D C B A", 'revesed stable complex in place sort';
633
634 @input = &generate1;
635 $output = reverse sort {stuff || $a <=> $b} @input;
636 ok $output, "IHGFEDCBA", 'reversed stable complex sort in scalar context';
637
638 sub sortnumr {
639     reverse sort {$a <=> $b} @_;
640 }
641
642 @output = sortnumr &generate1;
643 ok "@output", "I H G F E D C B A",
644     'reversed stable $a <=> $b sort return list context';
645 $output = sortnumr &generate1;
646 ok $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort return scalar context';
647
648 sub sortnumrba {
649     reverse sort {$b <=> $a} @_;
650 }
651
652 @output = sortnumrba &generate1;
653 ok "@output", "C B A F E D I H G",
654     'reversed stable $b <=> $a sort return list context';
655 $output = sortnumrba &generate1;
656 ok $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort return scalar context';
657
658 sub sortnumrq {
659     reverse sort {stuff || $a <=> $b} @_;
660 }
661
662 @output = sortnumrq &generate1;
663 ok "@output", "I H G F E D C B A",
664     'reversed stable complex sort return list context';
665 $output = sortnumrq &generate1;
666 ok $output, "IHGFEDCBA", 'reversed stable complex sort return scalar context';
667
668 @output = reverse (sort(qw(C A B)), 0);
669 ok "@output", "0 C B A", 'reversed sort with trailing argument';
670
671 @output = reverse (0, sort(qw(C A B)));
672 ok "@output", "C B A 0", 'reversed sort with leading argument';