This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don’t LEAVE_with_name("evalcomp") for syntax errors
[perl5.git] / t / op / sort.t
CommitLineData
a687059c
LW
1#!./perl
2
9c007264
JH
3BEGIN {
4 chdir 't' if -d 't';
5823dc9f
MS
5 @INC = qw(. ../lib);
6 require 'test.pl';
9c007264 7}
9f1b1f2d 8use warnings;
f7bc00ea 9plan( tests => 165 );
a687059c 10
71a29c3c
GS
11# these shouldn't hang
12{
13 no warnings;
14 sort { for ($_ = 0;; $_++) {} } @a;
15 sort { while(1) {} } @a;
16 sort { while(1) { last; } } @a;
17 sort { while(0) { last; } } @a;
1937c63e
TS
18
19 # Change 26011: Re: A surprising segfault
20 map scalar(sort(+())), ('')x68;
71a29c3c
GS
21}
22
9f1b1f2d
GS
23sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
24sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 }
9850bf21 25sub Backwards_other { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
a687059c 26
9d116dd7
JH
27my $upperfirst = 'A' lt 'a';
28
29# Beware: in future this may become hairier because of possible
59608b94 30# collation complications: qw(A a B b) can be sorted at least as
9d116dd7
JH
31# any of the following
32#
33# A a B b
34# A B a b
35# a b A B
36# a A b B
37#
38# All the above orders make sense.
39#
40# That said, EBCDIC sorts all small letters first, as opposed
41# to ASCII which sorts all big letters first.
42
a687059c 43@harry = ('dog','cat','x','Cain','Abel');
2f52a358 44@george = ('gone','chased','yz','punished','Axed');
a687059c
LW
45
46$x = join('', sort @harry);
9d116dd7 47$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
f34362ee
DL
48
49cmp_ok($x,'eq',$expected,'upper first 1');
a687059c 50
9f1b1f2d 51$x = join('', sort( Backwards @harry));
9d116dd7 52$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
53
54cmp_ok($x,'eq',$expected,'upper first 2');
a687059c 55
9f1b1f2d 56$x = join('', sort( Backwards_stacked @harry));
43481408 57$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
58
59cmp_ok($x,'eq',$expected,'upper first 3');
43481408 60
a687059c 61$x = join('', sort @george, 'to', @harry);
9d116dd7
JH
62$expected = $upperfirst ?
63 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
64 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
03a14243 65
f34362ee
DL
66cmp_ok($x,'eq',$expected,'upper first 4');
67$" = ' ';
03a14243
LW
68@a = ();
69@b = reverse @a;
f34362ee 70cmp_ok("@b",'eq',"",'reverse 1');
03a14243
LW
71
72@a = (1);
73@b = reverse @a;
f34362ee 74cmp_ok("@b",'eq',"1",'reverse 2');
03a14243
LW
75
76@a = (1,2);
77@b = reverse @a;
f34362ee 78cmp_ok("@b",'eq',"2 1",'reverse 3');
03a14243
LW
79
80@a = (1,2,3);
81@b = reverse @a;
f34362ee 82cmp_ok("@b",'eq',"3 2 1",'reverse 4');
03a14243
LW
83
84@a = (1,2,3,4);
85@b = reverse @a;
f34362ee 86cmp_ok("@b",'eq',"4 3 2 1",'reverse 5');
55204971
LW
87
88@a = (10,2,3,4);
89@b = sort {$a <=> $b;} @a;
f34362ee 90cmp_ok("@b",'eq',"2 3 4 10",'sort numeric');
988174c1 91
9f1b1f2d 92$sub = 'Backwards';
988174c1 93$x = join('', sort $sub @harry);
9d116dd7 94$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
95
96cmp_ok($x,'eq',$expected,'sorter sub name in var 1');
43481408 97
9f1b1f2d 98$sub = 'Backwards_stacked';
43481408
GS
99$x = join('', sort $sub @harry);
100$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
101
102cmp_ok($x,'eq',$expected,'sorter sub name in var 2');
988174c1 103
cd5de442
GS
104# literals, combinations
105
106@b = sort (4,1,3,2);
f34362ee
DL
107cmp_ok("@b",'eq','1 2 3 4','just sort');
108
cd5de442
GS
109
110@b = sort grep { $_ } (4,1,3,2);
f34362ee
DL
111cmp_ok("@b",'eq','1 2 3 4','grep then sort');
112
cd5de442
GS
113
114@b = sort map { $_ } (4,1,3,2);
f34362ee
DL
115cmp_ok("@b",'eq','1 2 3 4','map then sort');
116
cd5de442
GS
117
118@b = sort reverse (4,1,3,2);
f34362ee
DL
119cmp_ok("@b",'eq','1 2 3 4','reverse then sort');
120
121
7bac28a0 122
9850bf21 123sub twoface { no warnings 'redefine'; *twoface = sub { $a <=> $b }; &twoface }
7bac28a0 124eval { @b = sort twoface 4,1,3,2 };
f34362ee
DL
125cmp_ok("@b",'eq','1 2 3 4','redefine sort sub inside the sort sub');
126
7bac28a0 127
9f1b1f2d 128eval { no warnings 'redefine'; *twoface = sub { &Backwards } };
f34362ee 129ok(!$@,"redefining sort subs outside the sort \$@=[$@]");
7bac28a0 130
131eval { @b = sort twoface 4,1,3,2 };
f34362ee 132cmp_ok("@b",'eq','4 3 2 1','twoface redefinition');
7bac28a0 133
9f1b1f2d
GS
134{
135 no warnings 'redefine';
9850bf21 136 *twoface = sub { *twoface = *Backwards_other; $a <=> $b };
9f1b1f2d 137}
f34362ee 138
9850bf21 139eval { @b = sort twoface 4,1,9,5 };
f34362ee 140ok(($@ eq "" && "@b" eq "1 4 5 9"),'redefinition should not take effect during the sort');
7bac28a0 141
9f1b1f2d
GS
142{
143 no warnings 'redefine';
144 *twoface = sub {
7bac28a0 145 eval 'sub twoface { $a <=> $b }';
f34362ee 146 die($@ eq "" ? "good\n" : "bad\n");
7bac28a0 147 $a <=> $b;
148 };
9f1b1f2d 149}
7bac28a0 150eval { @b = sort twoface 4,1 };
f34362ee 151cmp_ok(substr($@,0,4), 'eq', 'good', 'twoface eval');
15f0808c
GS
152
153eval <<'CODE';
9f1b1f2d 154 my @result = sort main'Backwards 'one', 'two';
15f0808c 155CODE
f34362ee 156cmp_ok($@,'eq','',q(old skool package));
15f0808c
GS
157
158eval <<'CODE';
159 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
160 my @result = sort 'one', 'two';
161CODE
f34362ee 162cmp_ok($@,'eq','',q(one is not a sub));
c6e96bcb
GS
163
164{
9f1b1f2d
GS
165 my $sortsub = \&Backwards;
166 my $sortglob = *Backwards;
167 my $sortglobr = \*Backwards;
168 my $sortname = 'Backwards';
c6e96bcb 169 @b = sort $sortsub 4,1,3,2;
f34362ee 170 cmp_ok("@b",'eq','4 3 2 1','sortname 1');
c6e96bcb 171 @b = sort $sortglob 4,1,3,2;
f34362ee 172 cmp_ok("@b",'eq','4 3 2 1','sortname 2');
c6e96bcb 173 @b = sort $sortname 4,1,3,2;
f34362ee 174 cmp_ok("@b",'eq','4 3 2 1','sortname 3');
62f274bf 175 @b = sort $sortglobr 4,1,3,2;
f34362ee 176 cmp_ok("@b",'eq','4 3 2 1','sortname 4');
43481408
GS
177}
178
179{
9f1b1f2d
GS
180 my $sortsub = \&Backwards_stacked;
181 my $sortglob = *Backwards_stacked;
182 my $sortglobr = \*Backwards_stacked;
183 my $sortname = 'Backwards_stacked';
43481408 184 @b = sort $sortsub 4,1,3,2;
f34362ee 185 cmp_ok("@b",'eq','4 3 2 1','sortname 5');
43481408 186 @b = sort $sortglob 4,1,3,2;
f34362ee 187 cmp_ok("@b",'eq','4 3 2 1','sortname 6');
43481408 188 @b = sort $sortname 4,1,3,2;
f34362ee 189 cmp_ok("@b",'eq','4 3 2 1','sortname 7');
43481408 190 @b = sort $sortglobr 4,1,3,2;
f34362ee 191 cmp_ok("@b",'eq','4 3 2 1','sortname 8');
c6e96bcb
GS
192}
193
194{
9f1b1f2d
GS
195 local $sortsub = \&Backwards;
196 local $sortglob = *Backwards;
197 local $sortglobr = \*Backwards;
198 local $sortname = 'Backwards';
c6e96bcb 199 @b = sort $sortsub 4,1,3,2;
f34362ee 200 cmp_ok("@b",'eq','4 3 2 1','sortname local 1');
c6e96bcb 201 @b = sort $sortglob 4,1,3,2;
f34362ee 202 cmp_ok("@b",'eq','4 3 2 1','sortname local 2');
c6e96bcb 203 @b = sort $sortname 4,1,3,2;
f34362ee 204 cmp_ok("@b",'eq','4 3 2 1','sortname local 3');
62f274bf 205 @b = sort $sortglobr 4,1,3,2;
f34362ee 206 cmp_ok("@b",'eq','4 3 2 1','sortname local 4');
43481408
GS
207}
208
209{
9f1b1f2d
GS
210 local $sortsub = \&Backwards_stacked;
211 local $sortglob = *Backwards_stacked;
212 local $sortglobr = \*Backwards_stacked;
213 local $sortname = 'Backwards_stacked';
43481408 214 @b = sort $sortsub 4,1,3,2;
f34362ee 215 cmp_ok("@b",'eq','4 3 2 1','sortname local 5');
43481408 216 @b = sort $sortglob 4,1,3,2;
f34362ee 217 cmp_ok("@b",'eq','4 3 2 1','sortname local 6');
43481408 218 @b = sort $sortname 4,1,3,2;
f34362ee 219 cmp_ok("@b",'eq','4 3 2 1','sortname local 7');
43481408 220 @b = sort $sortglobr 4,1,3,2;
f34362ee 221 cmp_ok("@b",'eq','4 3 2 1','sortname local 8');
c6e96bcb
GS
222}
223
9c007264
JH
224## exercise sort builtins... ($a <=> $b already tested)
225@a = ( 5, 19, 1996, 255, 90 );
5d4fa709
GS
226@b = sort {
227 my $dummy; # force blockness
228 return $b <=> $a
229} @a;
f34362ee
DL
230cmp_ok("@b",'eq','1996 255 90 19 5','force blockness');
231
9c007264
JH
232$x = join('', sort { $a cmp $b } @harry);
233$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
f34362ee
DL
234cmp_ok($x,'eq',$expected,'a cmp b');
235
9c007264
JH
236$x = join('', sort { $b cmp $a } @harry);
237$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
238cmp_ok($x,'eq',$expected,'b cmp a');
239
9c007264
JH
240{
241 use integer;
242 @b = sort { $a <=> $b } @a;
f34362ee
DL
243 cmp_ok("@b",'eq','5 19 90 255 1996','integer a <=> b');
244
9c007264 245 @b = sort { $b <=> $a } @a;
f34362ee
DL
246 cmp_ok("@b",'eq','1996 255 90 19 5','integer b <=> a');
247
9c007264
JH
248 $x = join('', sort { $a cmp $b } @harry);
249 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
f34362ee
DL
250 cmp_ok($x,'eq',$expected,'integer a cmp b');
251
9c007264
JH
252 $x = join('', sort { $b cmp $a } @harry);
253 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
254 cmp_ok($x,'eq',$expected,'integer b cmp a');
255
9c007264 256}
e507f050 257
f34362ee
DL
258
259
e507f050 260$x = join('', sort { $a <=> $b } 3, 1, 2);
f34362ee 261cmp_ok($x,'eq','123',q(optimized-away comparison block doesn't take any other arguments away with it));
e507f050 262
9c007264 263# test sorting in non-main package
5823dc9f
MS
264{
265 package Foo;
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');
8e3f9bdf 269
5823dc9f
MS
270 @b = sort ::Backwards_stacked @a;
271 ::cmp_ok("@b",'eq','90 5 255 1996 19','not in main:: 2');
f34362ee 272
5823dc9f
MS
273 # check if context for sort arguments is handled right
274 sub test_if_list {
275 my $gimme = wantarray;
276 ::is($gimme,1,'wantarray 1');
277 }
278 my $m = sub { $a <=> $b };
279
280 sub cxt_one { sort $m test_if_list() }
281 cxt_one();
282 sub cxt_two { sort { $a <=> $b } test_if_list() }
283 cxt_two();
284 sub cxt_three { sort &test_if_list() }
285 cxt_three();
286
287 sub test_if_scalar {
288 my $gimme = wantarray;
289 ::is(!($gimme or !defined($gimme)),1,'wantarray 2');
290 }
f34362ee 291
5823dc9f
MS
292 $m = \&test_if_scalar;
293 sub cxt_four { sort $m 1,2 }
294 @x = cxt_four();
295 sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 }
296 @x = cxt_five();
297 sub cxt_six { sort test_if_scalar 1,2 }
298 @x = cxt_six();
8e3f9bdf
GS
299}
300
8e664e10
GS
301
302# test against a reentrancy bug
303{
304 package Bar;
305 sub compare { $a cmp $b }
306 sub reenter { my @force = sort compare qw/a b/ }
307}
308{
309 my($def, $init) = (0, 0);
310 @b = sort {
311 $def = 1 if defined $Bar::a;
312 Bar::reenter() unless $init++;
313 $a <=> $b
314 } qw/4 3 1 2/;
5823dc9f 315 cmp_ok("@b",'eq','1 2 3 4','reenter 1');
f34362ee 316
5823dc9f 317 ok(!$def,'reenter 2');
8e664e10 318}
f0670693 319
f34362ee 320
f0670693
SC
321{
322 sub routine { "one", "two" };
323 @a = sort(routine(1));
5823dc9f 324 cmp_ok("@a",'eq',"one two",'bug id 19991001.003');
f0670693 325}
fe1bc4cf
DM
326
327
fe1bc4cf
DM
328# check for in-place optimisation of @a = sort @a
329{
330 my ($r1,$r2,@a);
331 our @g;
332 @g = (3,2,1); $r1 = \$g[2]; @g = sort @g; $r2 = \$g[0];
5823dc9f 333 is "$r1-@g", "$r2-1 2 3", "inplace sort of global";
fe1bc4cf
DM
334
335 @a = qw(b a c); $r1 = \$a[1]; @a = sort @a; $r2 = \$a[0];
5823dc9f 336 is "$r1-@a", "$r2-a b c", "inplace sort of lexical";
fe1bc4cf
DM
337
338 @g = (2,3,1); $r1 = \$g[1]; @g = sort { $b <=> $a } @g; $r2 = \$g[0];
5823dc9f 339 is "$r1-@g", "$r2-3 2 1", "inplace reversed sort of global";
fe1bc4cf
DM
340
341 @g = (2,3,1);
342 $r1 = \$g[1]; @g = sort { $a<$b?1:$a>$b?-1:0 } @g; $r2 = \$g[0];
5823dc9f 343 is "$r1-@g", "$r2-3 2 1", "inplace custom sort of global";
fe1bc4cf
DM
344
345 sub mysort { $b cmp $a };
346 @a = qw(b c a); $r1 = \$a[1]; @a = sort mysort @a; $r2 = \$a[0];
5823dc9f 347 is "$r1-@a", "$r2-c b a", "inplace sort with function of lexical";
fe1bc4cf
DM
348
349 use Tie::Array;
db7511db
DM
350 my @t;
351 tie @t, 'Tie::StdArray';
fe1bc4cf 352
db7511db 353 @t = qw(b c a); @t = sort @t;
5823dc9f 354 is "@t", "a b c", "inplace sort of tied array";
fe1bc4cf 355
db7511db 356 @t = qw(b c a); @t = sort mysort @t;
5823dc9f 357 is "@t", "c b a", "inplace sort of tied array with function";
db7511db
DM
358
359 # [perl #29790] don't optimise @a = ('a', sort @a) !
360
361 @g = (3,2,1); @g = ('0', sort @g);
5823dc9f 362 is "@g", "0 1 2 3", "un-inplace sort of global";
db7511db 363 @g = (3,2,1); @g = (sort(@g),'4');
5823dc9f 364 is "@g", "1 2 3 4", "un-inplace sort of global 2";
db7511db
DM
365
366 @a = qw(b a c); @a = ('x', sort @a);
5823dc9f 367 is "@a", "x a b c", "un-inplace sort of lexical";
db7511db 368 @a = qw(b a c); @a = ((sort @a), 'x');
5823dc9f 369 is "@a", "a b c x", "un-inplace sort of lexical 2";
db7511db
DM
370
371 @g = (2,3,1); @g = ('0', sort { $b <=> $a } @g);
5823dc9f 372 is "@g", "0 3 2 1", "un-inplace reversed sort of global";
db7511db 373 @g = (2,3,1); @g = ((sort { $b <=> $a } @g),'4');
5823dc9f 374 is "@g", "3 2 1 4", "un-inplace reversed sort of global 2";
db7511db
DM
375
376 @g = (2,3,1); @g = ('0', sort { $a<$b?1:$a>$b?-1:0 } @g);
5823dc9f 377 is "@g", "0 3 2 1", "un-inplace custom sort of global";
db7511db 378 @g = (2,3,1); @g = ((sort { $a<$b?1:$a>$b?-1:0 } @g),'4');
5823dc9f 379 is "@g", "3 2 1 4", "un-inplace custom sort of global 2";
db7511db
DM
380
381 @a = qw(b c a); @a = ('x', sort mysort @a);
5823dc9f 382 is "@a", "x c b a", "un-inplace sort with function of lexical";
db7511db 383 @a = qw(b c a); @a = ((sort mysort @a),'x');
5823dc9f 384 is "@a", "c b a x", "un-inplace sort with function of lexical 2";
aa6341cb
GG
385
386 # RT#54758. Git 62b40d2474e7487e6909e1872b6bccdf812c6818
8c1a9f82 387 no warnings 'void';
aa6341cb 388 my @m; push @m, 0 for 1 .. 1024; $#m; @m = sort @m;
5823dc9f 389 ::pass("in-place sorting segfault");
fe1bc4cf
DM
390}
391
eb209983
NC
392# Test optimisations of reversed sorts. As we now guarantee stability by
393# default, # optimisations which do not provide this are bogus.
fe1bc4cf 394
eb209983
NC
395{
396 package Oscalar;
397 use overload (qw("" stringify 0+ numify fallback 1));
398
399 sub new {
400 bless [$_[1], $_[2]], $_[0];
401 }
402
403 sub stringify { $_[0]->[0] }
404
405 sub numify { $_[0]->[1] }
406}
407
408sub generate {
409 my $count = 0;
410 map {new Oscalar $_, $count++} qw(A A A B B B C C C);
411}
412
413my @input = &generate;
414my @output = sort @input;
5823dc9f 415is join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", "Simple stable sort";
eb209983
NC
416
417@input = &generate;
418@input = sort @input;
5823dc9f 419is join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
eb209983
NC
420 "Simple stable in place sort";
421
422# This won't be very interesting
423@input = &generate;
424@output = sort {$a <=> $b} @input;
5823dc9f 425is "@output", "A A A B B B C C C", 'stable $a <=> $b sort';
eb209983
NC
426
427@input = &generate;
428@output = sort {$a cmp $b} @input;
5823dc9f 429is join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", 'stable $a cmp $b sort';
eb209983
NC
430
431@input = &generate;
432@input = sort {$a cmp $b} @input;
5823dc9f 433is join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
eb209983
NC
434 'stable $a cmp $b in place sort';
435
436@input = &generate;
437@output = sort {$b cmp $a} @input;
5823dc9f 438is join(" ", map {0+$_} @output), "6 7 8 3 4 5 0 1 2", 'stable $b cmp $a sort';
eb209983
NC
439
440@input = &generate;
441@input = sort {$b cmp $a} @input;
5823dc9f 442is join(" ", map {0+$_} @input), "6 7 8 3 4 5 0 1 2",
eb209983
NC
443 'stable $b cmp $a in place sort';
444
75dd5fa4 445@input = &generate;
eb209983 446@output = reverse sort @input;
5823dc9f 447is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", "Reversed stable sort";
eb209983
NC
448
449@input = &generate;
450@input = reverse sort @input;
5823dc9f 451is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
eb209983
NC
452 "Reversed stable in place sort";
453
75dd5fa4
NC
454@input = &generate;
455my $output = reverse sort @input;
5823dc9f 456is $output, "CCCBBBAAA", "Reversed stable sort in scalar context";
75dd5fa4 457
eb209983
NC
458
459@input = &generate;
460@output = reverse sort {$a cmp $b} @input;
5823dc9f 461is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
eb209983
NC
462 'reversed stable $a cmp $b sort';
463
464@input = &generate;
465@input = reverse sort {$a cmp $b} @input;
5823dc9f 466is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
eb209983
NC
467 'revesed stable $a cmp $b in place sort';
468
469@input = &generate;
7e7a548e 470$output = reverse sort {$a cmp $b} @input;
5823dc9f 471is $output, "CCCBBBAAA", 'Reversed stable $a cmp $b sort in scalar context';
75dd5fa4
NC
472
473@input = &generate;
eb209983 474@output = reverse sort {$b cmp $a} @input;
5823dc9f 475is join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
eb209983
NC
476 'reversed stable $b cmp $a sort';
477
478@input = &generate;
479@input = reverse sort {$b cmp $a} @input;
5823dc9f 480is join(" ", map {0+$_} @input), "2 1 0 5 4 3 8 7 6",
eb209983
NC
481 'revesed stable $b cmp $a in place sort';
482
75dd5fa4
NC
483@input = &generate;
484$output = reverse sort {$b cmp $a} @input;
5823dc9f 485is $output, "AAABBBCCC", 'Reversed stable $b cmp $a sort in scalar context';
75dd5fa4 486
7e7a548e
NC
487sub stuff {
488 # Something complex enough to defeat any constant folding optimiser
489 $$ - $$;
490}
491
492@input = &generate;
493@output = reverse sort {stuff || $a cmp $b} @input;
5823dc9f 494is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
7e7a548e
NC
495 'reversed stable complex sort';
496
497@input = &generate;
498@input = reverse sort {stuff || $a cmp $b} @input;
5823dc9f 499is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
7e7a548e
NC
500 'revesed stable complex in place sort';
501
502@input = &generate;
503$output = reverse sort {stuff || $a cmp $b } @input;
5823dc9f 504is $output, "CCCBBBAAA", 'Reversed stable complex sort in scalar context';
7e7a548e 505
a1824f2a
NC
506sub sortr {
507 reverse sort @_;
508}
509
510@output = sortr &generate;
5823dc9f 511is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
a1824f2a
NC
512 'reversed stable sort return list context';
513$output = sortr &generate;
5823dc9f 514is $output, "CCCBBBAAA",
a1824f2a
NC
515 'reversed stable sort return scalar context';
516
517sub sortcmpr {
518 reverse sort {$a cmp $b} @_;
519}
520
521@output = sortcmpr &generate;
5823dc9f 522is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
a1824f2a
NC
523 'reversed stable $a cmp $b sort return list context';
524$output = sortcmpr &generate;
5823dc9f 525is $output, "CCCBBBAAA",
a1824f2a
NC
526 'reversed stable $a cmp $b sort return scalar context';
527
528sub sortcmprba {
529 reverse sort {$b cmp $a} @_;
530}
531
532@output = sortcmprba &generate;
5823dc9f 533is join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
a1824f2a
NC
534 'reversed stable $b cmp $a sort return list context';
535$output = sortcmprba &generate;
5823dc9f 536is $output, "AAABBBCCC",
a1824f2a 537'reversed stable $b cmp $a sort return scalar context';
eb209983 538
7e7a548e
NC
539sub sortcmprq {
540 reverse sort {stuff || $a cmp $b} @_;
541}
542
543@output = sortcmpr &generate;
5823dc9f 544is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
7e7a548e
NC
545 'reversed stable complex sort return list context';
546$output = sortcmpr &generate;
5823dc9f 547is $output, "CCCBBBAAA",
7e7a548e
NC
548 'reversed stable complex sort return scalar context';
549
eb209983
NC
550# And now with numbers
551
552sub generate1 {
553 my $count = 'A';
554 map {new Oscalar $count++, $_} 0, 0, 0, 1, 1, 1, 2, 2, 2;
555}
556
557# This won't be very interesting
558@input = &generate1;
559@output = sort {$a cmp $b} @input;
5823dc9f 560is "@output", "A B C D E F G H I", 'stable $a cmp $b sort';
eb209983
NC
561
562@input = &generate1;
563@output = sort {$a <=> $b} @input;
5823dc9f 564is "@output", "A B C D E F G H I", 'stable $a <=> $b sort';
eb209983
NC
565
566@input = &generate1;
567@input = sort {$a <=> $b} @input;
5823dc9f 568is "@input", "A B C D E F G H I", 'stable $a <=> $b in place sort';
eb209983
NC
569
570@input = &generate1;
571@output = sort {$b <=> $a} @input;
5823dc9f 572is "@output", "G H I D E F A B C", 'stable $b <=> $a sort';
eb209983
NC
573
574@input = &generate1;
575@input = sort {$b <=> $a} @input;
5823dc9f 576is "@input", "G H I D E F A B C", 'stable $b <=> $a in place sort';
eb209983 577
59608b94
DN
578# test that optimized {$b cmp $a} and {$b <=> $a} remain stable
579# (new in 5.9) without overloading
580{ no warnings;
581@b = sort { $b <=> $a } @input = qw/5first 6first 5second 6second/;
5823dc9f 582is "@b" , "6first 6second 5first 5second", "optimized {$b <=> $a} without overloading" ;
59608b94 583@input = sort {$b <=> $a} @input;
5823dc9f 584is "@input" , "6first 6second 5first 5second","inline optimized {$b <=> $a} without overloading" ;
59608b94
DN
585};
586
eb209983 587# These two are actually doing string cmp on 0 1 and 2
75dd5fa4 588@input = &generate1;
eb209983 589@output = reverse sort @input;
5823dc9f 590is "@output", "I H G F E D C B A", "Reversed stable sort";
eb209983
NC
591
592@input = &generate1;
593@input = reverse sort @input;
5823dc9f 594is "@input", "I H G F E D C B A", "Reversed stable in place sort";
eb209983
NC
595
596@input = &generate1;
75dd5fa4 597$output = reverse sort @input;
5823dc9f 598is $output, "IHGFEDCBA", "Reversed stable sort in scalar context";
75dd5fa4
NC
599
600@input = &generate1;
eb209983 601@output = reverse sort {$a <=> $b} @input;
5823dc9f 602is "@output", "I H G F E D C B A", 'reversed stable $a <=> $b sort';
eb209983
NC
603
604@input = &generate1;
605@input = reverse sort {$a <=> $b} @input;
5823dc9f 606is "@input", "I H G F E D C B A", 'revesed stable $a <=> $b in place sort';
fe1bc4cf 607
eb209983 608@input = &generate1;
75dd5fa4 609$output = reverse sort {$a <=> $b} @input;
5823dc9f 610is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort in scalar context';
75dd5fa4
NC
611
612@input = &generate1;
eb209983 613@output = reverse sort {$b <=> $a} @input;
5823dc9f 614is "@output", "C B A F E D I H G", 'reversed stable $b <=> $a sort';
fe1bc4cf 615
eb209983
NC
616@input = &generate1;
617@input = reverse sort {$b <=> $a} @input;
5823dc9f 618is "@input", "C B A F E D I H G", 'revesed stable $b <=> $a in place sort';
75dd5fa4
NC
619
620@input = &generate1;
621$output = reverse sort {$b <=> $a} @input;
5823dc9f 622is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort in scalar context';
a1824f2a 623
7e7a548e
NC
624@input = &generate1;
625@output = reverse sort {stuff || $a <=> $b} @input;
5823dc9f 626is "@output", "I H G F E D C B A", 'reversed stable complex sort';
7e7a548e
NC
627
628@input = &generate1;
629@input = reverse sort {stuff || $a <=> $b} @input;
5823dc9f 630is "@input", "I H G F E D C B A", 'revesed stable complex in place sort';
7e7a548e
NC
631
632@input = &generate1;
633$output = reverse sort {stuff || $a <=> $b} @input;
5823dc9f 634is $output, "IHGFEDCBA", 'reversed stable complex sort in scalar context';
a1824f2a
NC
635
636sub sortnumr {
637 reverse sort {$a <=> $b} @_;
638}
639
640@output = sortnumr &generate1;
5823dc9f 641is "@output", "I H G F E D C B A",
a1824f2a
NC
642 'reversed stable $a <=> $b sort return list context';
643$output = sortnumr &generate1;
5823dc9f 644is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort return scalar context';
a1824f2a
NC
645
646sub sortnumrba {
647 reverse sort {$b <=> $a} @_;
648}
649
650@output = sortnumrba &generate1;
5823dc9f 651is "@output", "C B A F E D I H G",
a1824f2a
NC
652 'reversed stable $b <=> $a sort return list context';
653$output = sortnumrba &generate1;
5823dc9f 654is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort return scalar context';
7e7a548e
NC
655
656sub sortnumrq {
657 reverse sort {stuff || $a <=> $b} @_;
658}
659
660@output = sortnumrq &generate1;
5823dc9f 661is "@output", "I H G F E D C B A",
7e7a548e
NC
662 'reversed stable complex sort return list context';
663$output = sortnumrq &generate1;
5823dc9f 664is $output, "IHGFEDCBA", 'reversed stable complex sort return scalar context';
c093edd0
NC
665
666@output = reverse (sort(qw(C A B)), 0);
5823dc9f 667is "@output", "0 C B A", 'reversed sort with trailing argument';
c093edd0
NC
668
669@output = reverse (0, sort(qw(C A B)));
5823dc9f 670is "@output", "C B A 0", 'reversed sort with leading argument';
9850bf21
RH
671
672eval { @output = sort {goto sub {}} 1,2; };
f34362ee 673$fail_msg = q(Can't goto subroutine outside a subroutine);
5823dc9f 674cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr outside subr');
f34362ee
DL
675
676
9850bf21
RH
677
678sub goto_sub {goto sub{}}
679eval { @output = sort goto_sub 1,2; };
f34362ee 680$fail_msg = q(Can't goto subroutine from a sort sub);
5823dc9f 681cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr from a sort sub');
f34362ee
DL
682
683
9850bf21
RH
684
685eval { @output = sort {goto label} 1,2; };
f34362ee 686$fail_msg = q(Can't "goto" out of a pseudo block);
5823dc9f 687cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 1');
f34362ee
DL
688
689
9850bf21
RH
690
691sub goto_label {goto label}
692label: eval { @output = sort goto_label 1,2; };
f34362ee 693$fail_msg = q(Can't "goto" out of a pseudo block);
5823dc9f 694cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 2');
f34362ee
DL
695
696
9850bf21
RH
697
698sub self_immolate {undef &self_immolate; $a<=>$b}
699eval { @output = sort self_immolate 1,2,3 };
f34362ee 700$fail_msg = q(Can't undef active subroutine);
5823dc9f 701cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'undef active subr');
f34362ee
DL
702
703
8a27a13e
FC
704for(1,2) # We run this twice, to make sure sort does not lower the ref
705{ # count. See bug 71076.
9850bf21
RH
706 my $failed = 0;
707
708 sub rec {
709 my $n = shift;
710 if (!defined($n)) { # No arg means we're being called by sort()
711 return 1;
712 }
713 if ($n<5) { rec($n+1); }
714 else { () = sort rec 1,2; }
715
716 $failed = 1 if !defined $n;
717 }
718
719 rec(1);
5823dc9f 720 ok(!$failed, "sort from active sub");
9850bf21
RH
721}
722
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.
f34362ee 726my $answer = "good";
9850bf21
RH
727() = sort OtherPack::foo 1,2,3,4;
728
9da9462b
RGS
729{
730 package OtherPack;
731 no warnings 'once';
732 sub foo {
f34362ee 733 $answer = "something was unexpectedly defined or undefined" if
9da9462b
RGS
734 defined($a) || defined($b) || !defined($main::a) || !defined($main::b);
735 $main::a <=> $main::b;
736 }
737}
9850bf21 738
5823dc9f 739cmp_ok($answer,'eq','good','sort subr called from other package');
9850bf21
RH
740
741
742# Bug 36430 - sort called in package2 while a
743# sort in package1 is active should set $package2::a/b.
5823dc9f
MS
744{
745 my $answer = "good";
746 my @list = sort { A::min(@$a) <=> A::min(@$b) }
747 [3, 1, 5], [2, 4], [0];
748
749 cmp_ok($answer,'eq','good','bug 36430');
750
751 package A;
752 sub min {
753 my @list = sort {
754 $answer = '$a and/or $b are not defined ' if !defined($a) || !defined($b);
755 $a <=> $b;
756 } @_;
757 $list[0];
758 }
9850bf21
RH
759}
760
5823dc9f 761
9850bf21
RH
762# Bug 7567 - an array shouldn't be modifiable while it's being
763# sorted in-place.
5823dc9f
MS
764{
765 eval { @a=(1..8); @a = sort { @a = (0) } @a; };
f34362ee 766
5823dc9f
MS
767 $fail_msg = q(Modification of a read-only value attempted);
768 cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'bug 7567');
769}
f34362ee 770
84df657f
GG
771{
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); };
774 is($@, "");
775}
776
9850bf21
RH
777
778# Sorting shouldn't increase the refcount of a sub
5823dc9f 779{
7f4622ec
RGS
780 sub sportello {(1+$a) <=> (1+$b)}
781 my $refcnt = &Internals::SvREFCNT(\&sportello);
782 @output = sort sportello 3,7,9;
5823dc9f
MS
783
784 {
7f4622ec
RGS
785 package Doc;
786 ::is($refcnt, &Internals::SvREFCNT(\&::sportello), "sort sub refcnt");
5823dc9f
MS
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');
793 }
794}
f34362ee 795
9850bf21
RH
796
797# Using return() should be okay even in a deeper context
798@b = sort {while (1) {return ($a <=> $b)} } 1..10;
5823dc9f 799is("@b", "1 2 3 4 5 6 7 8 9 10", "return within loop");
d7507f74
RH
800
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;
5823dc9f 804is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack");
d7507f74
RH
805
806# As above, but with a sort sub rather than a sort block.
807sub ret_with_stacked { $_ = ($a<=>$b) + do {return $b <=> $a} }
808@b = sort ret_with_stacked 1..10;
5823dc9f 809is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack");
93e19c0f
Z
810
811# Comparison code should be able to give result in non-integer representation.
812sub 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);
814is("@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);
816is("@b", "1 2 3 3 4 5 7", "comparison result as string");
bdbefedf
DM
817
818# RT #34604: sort didn't honour overloading if the overloaded elements
819# were retrieved via tie
820
821{
822 package RT34604;
823
824 sub TIEHASH { bless {
825 p => bless({ val => 2 }),
826 q => bless({ val => 1 }),
827 }
828 }
829 sub FETCH { $_[0]{$_[1] } }
830
831 my $cc = 0;
832 sub compare { $cc++; $_[0]{val} cmp $_[1]{val} }
833 my $cs = 0;
834 sub str { $cs++; $_[0]{val} }
835
836 use overload 'cmp' => \&compare, '""' => \&str;
837
838 package main;
839
840 tie my %h, 'RT34604';
841 my @sorted = sort @h{qw(p q)};
842 is($cc, 1, 'overload compare called once');
843 is("@sorted","1 2", 'overload sort result');
844 is($cs, 2, 'overload string called twice');
845}
8f443ca6
GG
846
847fresh_perl_is('sub w ($$) {my ($l, my $r) = @_; my $v = \@_; undef @_; $l <=> $r}; print join q{ }, sort w 3, 1, 2, 0',
848 '0 1 2 3',
849 {stderr => 1, switches => ['-w']},
850 'RT #72334');
851
852fresh_perl_is('sub w ($$) {my ($l, my $r) = @_; my $v = \@_; undef @_; @_ = 0..2; $l <=> $r}; print join q{ }, sort w 3, 1, 2, 0',
853 '0 1 2 3',
854 {stderr => 1, switches => ['-w']},
855 'RT #72334');
133acf7b
NC
856
857{
858 my $count = 0;
859 {
860 package Counter;
861
862 sub new {
863 ++$count;
864 bless [];
865 }
866
867 sub DESTROY {
868 --$count;
869 }
870 }
871
872 sub sorter ($$) {
873 my ($l, $r) = @_;
874 my $q = \@_;
875 $l <=> $r;
876 }
877
878 is($count, 0, 'None before we start');
879 my @a = map { Counter->new() } 0..1;
880 is($count, 2, '2 here');
881
882 my @b = sort sorter @a;
883
884 is(scalar @b, 2);
885 cmp_ok($b[0], '<', $b[1], 'sorted!');
886
887 is($count, 2, 'still the same 2 here');
888
889 @a = (); @b = ();
890
891 is($count, 0, 'all gone');
892}
8e7d0c4b
FC
893
894# [perl #77930] The context stack may be reallocated during a sort, as a
895# result of deeply-nested (or not-so-deeply-nested) calls
896# from a custom sort subroutine.
897fresh_perl_is
898 '
899 $sub = sub {
900 local $count = $count+1;
901 ()->$sub if $count < 1000;
902 $a cmp $b
903 };
904 () = sort $sub qw<a b c d e f g>;
905 print "ok"
906 ',
907 'ok',
908 {},
92c404cd 909 '[perl #77930] cx_stack reallocation during sort'
8e7d0c4b 910;
ad021bfb
FC
911
912# [perl #76026]
913# Match vars should not leak from one sort sub call to the next
914{
915 my $output = '';
916 sub soarter {
917 $output .= $1;
918 "Leakage" =~ /(.*)/;
919 1
920 }
921 sub soarterdd($$) {
922 $output .= $1;
923 "Leakage" =~ /(.*)/;
924 1
925 }
926
927 "Win" =~ /(.*)/;
928 my @b = sort soarter 0..2;
929
930 like $output, qr/^(?:Win)+\z/,
931 "Match vars do not leak from one plain sort sub to the next";
932
933 $output = '';
934
935 "Win" =~ /(.*)/;
936 @b = sort soarterdd 0..2;
937
938 like $output, qr/^(?:Win)+\z/,
939 'Match vars do not leak from one $$ sort sub to the next';
940}
f7bc00ea
FC
941
942# [perl #30661] autoloading
943AUTOLOAD { $b <=> $a }
944sub stubbedsub;
945is join("", sort stubbedsub split//, '04381091'), '98431100',
946 'stubborn AUTOLOAD';
947is join("", sort hopefullynonexistent split//, '04381091'), '98431100',
948 'AUTOLOAD without stub';
949my $stubref = \&givemeastub;
950is join("", sort $stubref split//, '04381091'), '98431100',
951 'AUTOLOAD with stubref';