This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add missing CUSTOMIZED entry for base.pm
[perl5.git] / t / op / sort.t
CommitLineData
a687059c 1#!./perl
d0664088 2$|=1;
a687059c 3
9c007264
JH
4BEGIN {
5 chdir 't' if -d 't';
ab4454fc 6 require './test.pl';
43ece5b1 7 set_up_inc('../lib');
9c007264 8}
9f1b1f2d 9use warnings;
dc9ef998 10plan(tests => 195);
a687059c 11
71a29c3c
GS
12# these shouldn't hang
13{
14 no warnings;
15 sort { for ($_ = 0;; $_++) {} } @a;
16 sort { while(1) {} } @a;
17 sort { while(1) { last; } } @a;
18 sort { while(0) { last; } } @a;
1937c63e
ST
19
20 # Change 26011: Re: A surprising segfault
21 map scalar(sort(+())), ('')x68;
71a29c3c
GS
22}
23
9f1b1f2d
GS
24sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
25sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 }
9850bf21 26sub Backwards_other { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
a687059c 27
9d116dd7
JH
28my $upperfirst = 'A' lt 'a';
29
30# Beware: in future this may become hairier because of possible
59608b94 31# collation complications: qw(A a B b) can be sorted at least as
9d116dd7
JH
32# any of the following
33#
34# A a B b
35# A B a b
36# a b A B
37# a A b B
38#
39# All the above orders make sense.
40#
41# That said, EBCDIC sorts all small letters first, as opposed
42# to ASCII which sorts all big letters first.
43
a687059c 44@harry = ('dog','cat','x','Cain','Abel');
2f52a358 45@george = ('gone','chased','yz','punished','Axed');
a687059c
LW
46
47$x = join('', sort @harry);
9d116dd7 48$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
f34362ee
DL
49
50cmp_ok($x,'eq',$expected,'upper first 1');
a687059c 51
9f1b1f2d 52$x = join('', sort( Backwards @harry));
9d116dd7 53$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
54
55cmp_ok($x,'eq',$expected,'upper first 2');
a687059c 56
9f1b1f2d 57$x = join('', sort( Backwards_stacked @harry));
43481408 58$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
59
60cmp_ok($x,'eq',$expected,'upper first 3');
43481408 61
a687059c 62$x = join('', sort @george, 'to', @harry);
9d116dd7
JH
63$expected = $upperfirst ?
64 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
65 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
03a14243 66
d0664088
KW
67my @initially_sorted = ( 0 .. 260,
68 0x3FF, 0x400, 0x401,
69 0x7FF, 0x800, 0x801,
70 0x3FFF, 0x4000, 0x4001,
71 0xFFFF, 0x10000, 0x10001,
72 );
73# It makes things easier below if there are an even number of elements in the
74# array.
75if (scalar(@initially_sorted) % 2 == 1) {
76 push @initially_sorted, $initially_sorted[-1] + 1;
77}
78
79# We convert to a chr(), but prepend a constant string to make sure things can
80# work on more than a single character.
81my $prefix = "a\xb6";
82my $prefix_len = length $prefix;
83
84my @chr_initially_sorted = @initially_sorted;
85$_ = $prefix . chr($_) for @chr_initially_sorted;
86
87# Create a very unsorted version by reversing it, and then pushing the same
88# code points again, but pair-wise reversed.
89my @initially_unsorted = reverse @chr_initially_sorted;
90for (my $i = 0; $i < @chr_initially_sorted - 1; $i += 2) {
91 push @initially_unsorted, $chr_initially_sorted[$i+1],
92 $chr_initially_sorted[$i];
93}
94
95# And, an all-UTF-8 version
96my @utf8_initialy_unsorted = @initially_unsorted;
97utf8::upgrade($_) for @utf8_initialy_unsorted;
98
99# Sort the non-UTF-8 version
100my @non_utf8_result = sort @initially_unsorted;
101my @wrongly_utf8;
102my $ordered_correctly = 1;
103for my $i (0 .. @chr_initially_sorted -1) {
104 if ( $chr_initially_sorted[$i] ne $non_utf8_result[2*$i]
105 || $chr_initially_sorted[$i] ne $non_utf8_result[2*$i+1])
106 {
107 $ordered_correctly = 0;
108 last;
109 }
110 push @wrongly_utf8, $i if $i < 256 && utf8::is_utf8($non_utf8_result[$i]);
111}
112if (! ok($ordered_correctly, "sort of non-utf8 list worked")) {
113 diag ("This should be in numeric order (with 2 instances of every code point):\n"
114 . join " ", map { sprintf "%02x", ord substr $_, $prefix_len, 1 } @non_utf8_result);
115}
116if (! is(@wrongly_utf8, 0,
117 "No elements were wrongly converted to utf8 in sorting"))
118{
119 diag "For code points " . join " ", @wrongly_utf8;
120}
121
122# And then the UTF-8 one
123my @wrongly_non_utf8;
124$ordered_correctly = 1;
125my @utf8_result = sort @utf8_initialy_unsorted;
126for my $i (0 .. @chr_initially_sorted -1) {
127 if ( $chr_initially_sorted[$i] ne $utf8_result[2*$i]
128 || $chr_initially_sorted[$i] ne $utf8_result[2*$i+1])
129 {
130 $ordered_correctly = 0;
131 last;
132 }
133 push @wrongly_non_utf8, $i unless utf8::is_utf8($utf8_result[$i]);
134}
135if (! ok($ordered_correctly, "sort of utf8 list worked")) {
136 diag ("This should be in numeric order (with 2 instances of every code point):\n"
137 . join " ", map { sprintf "%02x", ord substr $_, $prefix_len, 1 } @utf8_result);
138}
139if (! is(@wrongly_non_utf8, 0,
140 "No elements were wrongly converted from utf8 in sorting"))
141{
142 diag "For code points " . join " ", @wrongly_non_utf8;
143}
144
f34362ee
DL
145cmp_ok($x,'eq',$expected,'upper first 4');
146$" = ' ';
03a14243
LW
147@a = ();
148@b = reverse @a;
f34362ee 149cmp_ok("@b",'eq',"",'reverse 1');
03a14243
LW
150
151@a = (1);
152@b = reverse @a;
f34362ee 153cmp_ok("@b",'eq',"1",'reverse 2');
03a14243
LW
154
155@a = (1,2);
156@b = reverse @a;
f34362ee 157cmp_ok("@b",'eq',"2 1",'reverse 3');
03a14243
LW
158
159@a = (1,2,3);
160@b = reverse @a;
f34362ee 161cmp_ok("@b",'eq',"3 2 1",'reverse 4');
03a14243
LW
162
163@a = (1,2,3,4);
164@b = reverse @a;
f34362ee 165cmp_ok("@b",'eq',"4 3 2 1",'reverse 5');
55204971
LW
166
167@a = (10,2,3,4);
168@b = sort {$a <=> $b;} @a;
f34362ee 169cmp_ok("@b",'eq',"2 3 4 10",'sort numeric');
988174c1 170
9f1b1f2d 171$sub = 'Backwards';
988174c1 172$x = join('', sort $sub @harry);
9d116dd7 173$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
174
175cmp_ok($x,'eq',$expected,'sorter sub name in var 1');
43481408 176
9f1b1f2d 177$sub = 'Backwards_stacked';
43481408
GS
178$x = join('', sort $sub @harry);
179$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
180
181cmp_ok($x,'eq',$expected,'sorter sub name in var 2');
988174c1 182
cd5de442
GS
183# literals, combinations
184
185@b = sort (4,1,3,2);
f34362ee
DL
186cmp_ok("@b",'eq','1 2 3 4','just sort');
187
cd5de442
GS
188
189@b = sort grep { $_ } (4,1,3,2);
f34362ee
DL
190cmp_ok("@b",'eq','1 2 3 4','grep then sort');
191
cd5de442
GS
192
193@b = sort map { $_ } (4,1,3,2);
f34362ee
DL
194cmp_ok("@b",'eq','1 2 3 4','map then sort');
195
cd5de442
GS
196
197@b = sort reverse (4,1,3,2);
f34362ee
DL
198cmp_ok("@b",'eq','1 2 3 4','reverse then sort');
199
200
01b5ef50
FC
201@b = sort CORE::reverse (4,1,3,2);
202cmp_ok("@b",'eq','1 2 3 4','CORE::reverse then sort');
203
487e470d
FC
204eval { @b = sort CORE::revers (4,1,3,2); };
205like($@, qr/^Undefined sort subroutine "CORE::revers" called at /);
01b5ef50 206
7bac28a0 207
9850bf21 208sub twoface { no warnings 'redefine'; *twoface = sub { $a <=> $b }; &twoface }
7bac28a0 209eval { @b = sort twoface 4,1,3,2 };
f34362ee
DL
210cmp_ok("@b",'eq','1 2 3 4','redefine sort sub inside the sort sub');
211
7bac28a0 212
9f1b1f2d 213eval { no warnings 'redefine'; *twoface = sub { &Backwards } };
f34362ee 214ok(!$@,"redefining sort subs outside the sort \$@=[$@]");
7bac28a0
PP
215
216eval { @b = sort twoface 4,1,3,2 };
f34362ee 217cmp_ok("@b",'eq','4 3 2 1','twoface redefinition');
7bac28a0 218
9f1b1f2d
GS
219{
220 no warnings 'redefine';
9850bf21 221 *twoface = sub { *twoface = *Backwards_other; $a <=> $b };
9f1b1f2d 222}
f34362ee 223
9850bf21 224eval { @b = sort twoface 4,1,9,5 };
f34362ee 225ok(($@ eq "" && "@b" eq "1 4 5 9"),'redefinition should not take effect during the sort');
7bac28a0 226
9f1b1f2d
GS
227{
228 no warnings 'redefine';
229 *twoface = sub {
7bac28a0 230 eval 'sub twoface { $a <=> $b }';
f34362ee 231 die($@ eq "" ? "good\n" : "bad\n");
7bac28a0
PP
232 $a <=> $b;
233 };
9f1b1f2d 234}
7bac28a0 235eval { @b = sort twoface 4,1 };
f34362ee 236cmp_ok(substr($@,0,4), 'eq', 'good', 'twoface eval');
15f0808c
GS
237
238eval <<'CODE';
9f1b1f2d 239 my @result = sort main'Backwards 'one', 'two';
15f0808c 240CODE
f34362ee 241cmp_ok($@,'eq','',q(old skool package));
15f0808c
GS
242
243eval <<'CODE';
244 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
245 my @result = sort 'one', 'two';
246CODE
f34362ee 247cmp_ok($@,'eq','',q(one is not a sub));
c6e96bcb
GS
248
249{
9f1b1f2d
GS
250 my $sortsub = \&Backwards;
251 my $sortglob = *Backwards;
252 my $sortglobr = \*Backwards;
253 my $sortname = 'Backwards';
c6e96bcb 254 @b = sort $sortsub 4,1,3,2;
f34362ee 255 cmp_ok("@b",'eq','4 3 2 1','sortname 1');
c6e96bcb 256 @b = sort $sortglob 4,1,3,2;
f34362ee 257 cmp_ok("@b",'eq','4 3 2 1','sortname 2');
c6e96bcb 258 @b = sort $sortname 4,1,3,2;
f34362ee 259 cmp_ok("@b",'eq','4 3 2 1','sortname 3');
62f274bf 260 @b = sort $sortglobr 4,1,3,2;
f34362ee 261 cmp_ok("@b",'eq','4 3 2 1','sortname 4');
43481408
GS
262}
263
264{
9f1b1f2d
GS
265 my $sortsub = \&Backwards_stacked;
266 my $sortglob = *Backwards_stacked;
267 my $sortglobr = \*Backwards_stacked;
268 my $sortname = 'Backwards_stacked';
43481408 269 @b = sort $sortsub 4,1,3,2;
f34362ee 270 cmp_ok("@b",'eq','4 3 2 1','sortname 5');
43481408 271 @b = sort $sortglob 4,1,3,2;
f34362ee 272 cmp_ok("@b",'eq','4 3 2 1','sortname 6');
43481408 273 @b = sort $sortname 4,1,3,2;
f34362ee 274 cmp_ok("@b",'eq','4 3 2 1','sortname 7');
43481408 275 @b = sort $sortglobr 4,1,3,2;
f34362ee 276 cmp_ok("@b",'eq','4 3 2 1','sortname 8');
c6e96bcb
GS
277}
278
279{
9f1b1f2d
GS
280 local $sortsub = \&Backwards;
281 local $sortglob = *Backwards;
282 local $sortglobr = \*Backwards;
283 local $sortname = 'Backwards';
c6e96bcb 284 @b = sort $sortsub 4,1,3,2;
f34362ee 285 cmp_ok("@b",'eq','4 3 2 1','sortname local 1');
c6e96bcb 286 @b = sort $sortglob 4,1,3,2;
f34362ee 287 cmp_ok("@b",'eq','4 3 2 1','sortname local 2');
c6e96bcb 288 @b = sort $sortname 4,1,3,2;
f34362ee 289 cmp_ok("@b",'eq','4 3 2 1','sortname local 3');
62f274bf 290 @b = sort $sortglobr 4,1,3,2;
f34362ee 291 cmp_ok("@b",'eq','4 3 2 1','sortname local 4');
43481408
GS
292}
293
294{
9f1b1f2d
GS
295 local $sortsub = \&Backwards_stacked;
296 local $sortglob = *Backwards_stacked;
297 local $sortglobr = \*Backwards_stacked;
298 local $sortname = 'Backwards_stacked';
43481408 299 @b = sort $sortsub 4,1,3,2;
f34362ee 300 cmp_ok("@b",'eq','4 3 2 1','sortname local 5');
43481408 301 @b = sort $sortglob 4,1,3,2;
f34362ee 302 cmp_ok("@b",'eq','4 3 2 1','sortname local 6');
43481408 303 @b = sort $sortname 4,1,3,2;
f34362ee 304 cmp_ok("@b",'eq','4 3 2 1','sortname local 7');
43481408 305 @b = sort $sortglobr 4,1,3,2;
f34362ee 306 cmp_ok("@b",'eq','4 3 2 1','sortname local 8');
c6e96bcb
GS
307}
308
9c007264
JH
309## exercise sort builtins... ($a <=> $b already tested)
310@a = ( 5, 19, 1996, 255, 90 );
5d4fa709
GS
311@b = sort {
312 my $dummy; # force blockness
313 return $b <=> $a
314} @a;
f34362ee
DL
315cmp_ok("@b",'eq','1996 255 90 19 5','force blockness');
316
9c007264
JH
317$x = join('', sort { $a cmp $b } @harry);
318$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
f34362ee
DL
319cmp_ok($x,'eq',$expected,'a cmp b');
320
9c007264
JH
321$x = join('', sort { $b cmp $a } @harry);
322$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
323cmp_ok($x,'eq',$expected,'b cmp a');
324
9c007264
JH
325{
326 use integer;
327 @b = sort { $a <=> $b } @a;
f34362ee
DL
328 cmp_ok("@b",'eq','5 19 90 255 1996','integer a <=> b');
329
9c007264 330 @b = sort { $b <=> $a } @a;
f34362ee
DL
331 cmp_ok("@b",'eq','1996 255 90 19 5','integer b <=> a');
332
9c007264
JH
333 $x = join('', sort { $a cmp $b } @harry);
334 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
f34362ee
DL
335 cmp_ok($x,'eq',$expected,'integer a cmp b');
336
9c007264
JH
337 $x = join('', sort { $b cmp $a } @harry);
338 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
f34362ee
DL
339 cmp_ok($x,'eq',$expected,'integer b cmp a');
340
9c007264 341}
e507f050 342
f34362ee
DL
343
344
e507f050 345$x = join('', sort { $a <=> $b } 3, 1, 2);
f34362ee 346cmp_ok($x,'eq','123',q(optimized-away comparison block doesn't take any other arguments away with it));
e507f050 347
9c007264 348# test sorting in non-main package
5823dc9f
MS
349{
350 package Foo;
351 @a = ( 5, 19, 1996, 255, 90 );
352 @b = sort { $b <=> $a } @a;
353 ::cmp_ok("@b",'eq','1996 255 90 19 5','not in main:: 1');
8e3f9bdf 354
5823dc9f
MS
355 @b = sort ::Backwards_stacked @a;
356 ::cmp_ok("@b",'eq','90 5 255 1996 19','not in main:: 2');
f34362ee 357
5823dc9f
MS
358 # check if context for sort arguments is handled right
359 sub test_if_list {
360 my $gimme = wantarray;
361 ::is($gimme,1,'wantarray 1');
362 }
363 my $m = sub { $a <=> $b };
364
365 sub cxt_one { sort $m test_if_list() }
366 cxt_one();
367 sub cxt_two { sort { $a <=> $b } test_if_list() }
368 cxt_two();
369 sub cxt_three { sort &test_if_list() }
370 cxt_three();
e9d9e6f3
FC
371 sub cxt_three_anna_half { sort 0, test_if_list() }
372 cxt_three_anna_half();
5823dc9f
MS
373
374 sub test_if_scalar {
375 my $gimme = wantarray;
376 ::is(!($gimme or !defined($gimme)),1,'wantarray 2');
377 }
f34362ee 378
5823dc9f
MS
379 $m = \&test_if_scalar;
380 sub cxt_four { sort $m 1,2 }
381 @x = cxt_four();
382 sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 }
383 @x = cxt_five();
384 sub cxt_six { sort test_if_scalar 1,2 }
385 @x = cxt_six();
8e3f9bdf
GS
386}
387
8e664e10
GS
388
389# test against a reentrancy bug
390{
391 package Bar;
392 sub compare { $a cmp $b }
393 sub reenter { my @force = sort compare qw/a b/ }
394}
395{
396 my($def, $init) = (0, 0);
397 @b = sort {
398 $def = 1 if defined $Bar::a;
399 Bar::reenter() unless $init++;
400 $a <=> $b
401 } qw/4 3 1 2/;
5823dc9f 402 cmp_ok("@b",'eq','1 2 3 4','reenter 1');
f34362ee 403
5823dc9f 404 ok(!$def,'reenter 2');
8e664e10 405}
f0670693 406
f34362ee 407
f0670693
SC
408{
409 sub routine { "one", "two" };
410 @a = sort(routine(1));
ee95e30c 411 cmp_ok("@a",'eq',"one two",'bug id 19991001.003 (#1549)');
f0670693 412}
fe1bc4cf
DM
413
414
fe1bc4cf
DM
415# check for in-place optimisation of @a = sort @a
416{
417 my ($r1,$r2,@a);
418 our @g;
419 @g = (3,2,1); $r1 = \$g[2]; @g = sort @g; $r2 = \$g[0];
5823dc9f 420 is "$r1-@g", "$r2-1 2 3", "inplace sort of global";
fe1bc4cf
DM
421
422 @a = qw(b a c); $r1 = \$a[1]; @a = sort @a; $r2 = \$a[0];
5823dc9f 423 is "$r1-@a", "$r2-a b c", "inplace sort of lexical";
fe1bc4cf
DM
424
425 @g = (2,3,1); $r1 = \$g[1]; @g = sort { $b <=> $a } @g; $r2 = \$g[0];
5823dc9f 426 is "$r1-@g", "$r2-3 2 1", "inplace reversed sort of global";
fe1bc4cf
DM
427
428 @g = (2,3,1);
429 $r1 = \$g[1]; @g = sort { $a<$b?1:$a>$b?-1:0 } @g; $r2 = \$g[0];
5823dc9f 430 is "$r1-@g", "$r2-3 2 1", "inplace custom sort of global";
fe1bc4cf
DM
431
432 sub mysort { $b cmp $a };
433 @a = qw(b c a); $r1 = \$a[1]; @a = sort mysort @a; $r2 = \$a[0];
5823dc9f 434 is "$r1-@a", "$r2-c b a", "inplace sort with function of lexical";
fe1bc4cf
DM
435
436 use Tie::Array;
db7511db
DM
437 my @t;
438 tie @t, 'Tie::StdArray';
fe1bc4cf 439
db7511db 440 @t = qw(b c a); @t = sort @t;
5823dc9f 441 is "@t", "a b c", "inplace sort of tied array";
fe1bc4cf 442
db7511db 443 @t = qw(b c a); @t = sort mysort @t;
5823dc9f 444 is "@t", "c b a", "inplace sort of tied array with function";
db7511db
DM
445
446 # [perl #29790] don't optimise @a = ('a', sort @a) !
447
448 @g = (3,2,1); @g = ('0', sort @g);
5823dc9f 449 is "@g", "0 1 2 3", "un-inplace sort of global";
db7511db 450 @g = (3,2,1); @g = (sort(@g),'4');
5823dc9f 451 is "@g", "1 2 3 4", "un-inplace sort of global 2";
db7511db
DM
452
453 @a = qw(b a c); @a = ('x', sort @a);
5823dc9f 454 is "@a", "x a b c", "un-inplace sort of lexical";
db7511db 455 @a = qw(b a c); @a = ((sort @a), 'x');
5823dc9f 456 is "@a", "a b c x", "un-inplace sort of lexical 2";
db7511db
DM
457
458 @g = (2,3,1); @g = ('0', sort { $b <=> $a } @g);
5823dc9f 459 is "@g", "0 3 2 1", "un-inplace reversed sort of global";
db7511db 460 @g = (2,3,1); @g = ((sort { $b <=> $a } @g),'4');
5823dc9f 461 is "@g", "3 2 1 4", "un-inplace reversed sort of global 2";
db7511db
DM
462
463 @g = (2,3,1); @g = ('0', sort { $a<$b?1:$a>$b?-1:0 } @g);
5823dc9f 464 is "@g", "0 3 2 1", "un-inplace custom sort of global";
db7511db 465 @g = (2,3,1); @g = ((sort { $a<$b?1:$a>$b?-1:0 } @g),'4');
5823dc9f 466 is "@g", "3 2 1 4", "un-inplace custom sort of global 2";
db7511db
DM
467
468 @a = qw(b c a); @a = ('x', sort mysort @a);
5823dc9f 469 is "@a", "x c b a", "un-inplace sort with function of lexical";
db7511db 470 @a = qw(b c a); @a = ((sort mysort @a),'x');
5823dc9f 471 is "@a", "c b a x", "un-inplace sort with function of lexical 2";
aa6341cb
GG
472
473 # RT#54758. Git 62b40d2474e7487e6909e1872b6bccdf812c6818
8c1a9f82 474 no warnings 'void';
aa6341cb 475 my @m; push @m, 0 for 1 .. 1024; $#m; @m = sort @m;
5823dc9f 476 ::pass("in-place sorting segfault");
fe1bc4cf
DM
477}
478
eb209983
NC
479# Test optimisations of reversed sorts. As we now guarantee stability by
480# default, # optimisations which do not provide this are bogus.
fe1bc4cf 481
eb209983
NC
482{
483 package Oscalar;
484 use overload (qw("" stringify 0+ numify fallback 1));
485
486 sub new {
487 bless [$_[1], $_[2]], $_[0];
488 }
489
490 sub stringify { $_[0]->[0] }
491
492 sub numify { $_[0]->[1] }
493}
494
495sub generate {
496 my $count = 0;
497 map {new Oscalar $_, $count++} qw(A A A B B B C C C);
498}
499
500my @input = &generate;
501my @output = sort @input;
5823dc9f 502is join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", "Simple stable sort";
eb209983
NC
503
504@input = &generate;
505@input = sort @input;
5823dc9f 506is join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
eb209983
NC
507 "Simple stable in place sort";
508
509# This won't be very interesting
510@input = &generate;
511@output = sort {$a <=> $b} @input;
5823dc9f 512is "@output", "A A A B B B C C C", 'stable $a <=> $b sort';
eb209983
NC
513
514@input = &generate;
515@output = sort {$a cmp $b} @input;
5823dc9f 516is join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", 'stable $a cmp $b sort';
eb209983
NC
517
518@input = &generate;
519@input = sort {$a cmp $b} @input;
5823dc9f 520is join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8",
eb209983
NC
521 'stable $a cmp $b in place sort';
522
523@input = &generate;
524@output = sort {$b cmp $a} @input;
5823dc9f 525is join(" ", map {0+$_} @output), "6 7 8 3 4 5 0 1 2", 'stable $b cmp $a sort';
eb209983
NC
526
527@input = &generate;
528@input = sort {$b cmp $a} @input;
5823dc9f 529is join(" ", map {0+$_} @input), "6 7 8 3 4 5 0 1 2",
eb209983
NC
530 'stable $b cmp $a in place sort';
531
75dd5fa4 532@input = &generate;
eb209983 533@output = reverse sort @input;
5823dc9f 534is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", "Reversed stable sort";
eb209983
NC
535
536@input = &generate;
537@input = reverse sort @input;
5823dc9f 538is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
eb209983
NC
539 "Reversed stable in place sort";
540
75dd5fa4
NC
541@input = &generate;
542my $output = reverse sort @input;
5823dc9f 543is $output, "CCCBBBAAA", "Reversed stable sort in scalar context";
75dd5fa4 544
eb209983
NC
545
546@input = &generate;
547@output = reverse sort {$a cmp $b} @input;
5823dc9f 548is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
eb209983
NC
549 'reversed stable $a cmp $b sort';
550
551@input = &generate;
552@input = reverse sort {$a cmp $b} @input;
5823dc9f 553is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
eb209983
NC
554 'revesed stable $a cmp $b in place sort';
555
556@input = &generate;
7e7a548e 557$output = reverse sort {$a cmp $b} @input;
5823dc9f 558is $output, "CCCBBBAAA", 'Reversed stable $a cmp $b sort in scalar context';
75dd5fa4
NC
559
560@input = &generate;
eb209983 561@output = reverse sort {$b cmp $a} @input;
5823dc9f 562is join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
eb209983
NC
563 'reversed stable $b cmp $a sort';
564
565@input = &generate;
566@input = reverse sort {$b cmp $a} @input;
5823dc9f 567is join(" ", map {0+$_} @input), "2 1 0 5 4 3 8 7 6",
eb209983
NC
568 'revesed stable $b cmp $a in place sort';
569
75dd5fa4
NC
570@input = &generate;
571$output = reverse sort {$b cmp $a} @input;
5823dc9f 572is $output, "AAABBBCCC", 'Reversed stable $b cmp $a sort in scalar context';
75dd5fa4 573
7e7a548e
NC
574sub stuff {
575 # Something complex enough to defeat any constant folding optimiser
576 $$ - $$;
577}
578
579@input = &generate;
580@output = reverse sort {stuff || $a cmp $b} @input;
5823dc9f 581is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
7e7a548e
NC
582 'reversed stable complex sort';
583
584@input = &generate;
585@input = reverse sort {stuff || $a cmp $b} @input;
5823dc9f 586is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0",
7e7a548e
NC
587 'revesed stable complex in place sort';
588
589@input = &generate;
590$output = reverse sort {stuff || $a cmp $b } @input;
5823dc9f 591is $output, "CCCBBBAAA", 'Reversed stable complex sort in scalar context';
7e7a548e 592
a1824f2a
NC
593sub sortr {
594 reverse sort @_;
595}
596
597@output = sortr &generate;
5823dc9f 598is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
a1824f2a
NC
599 'reversed stable sort return list context';
600$output = sortr &generate;
5823dc9f 601is $output, "CCCBBBAAA",
a1824f2a
NC
602 'reversed stable sort return scalar context';
603
604sub sortcmpr {
605 reverse sort {$a cmp $b} @_;
606}
607
608@output = sortcmpr &generate;
5823dc9f 609is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
a1824f2a
NC
610 'reversed stable $a cmp $b sort return list context';
611$output = sortcmpr &generate;
5823dc9f 612is $output, "CCCBBBAAA",
a1824f2a
NC
613 'reversed stable $a cmp $b sort return scalar context';
614
615sub sortcmprba {
616 reverse sort {$b cmp $a} @_;
617}
618
619@output = sortcmprba &generate;
5823dc9f 620is join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6",
a1824f2a
NC
621 'reversed stable $b cmp $a sort return list context';
622$output = sortcmprba &generate;
5823dc9f 623is $output, "AAABBBCCC",
a1824f2a 624'reversed stable $b cmp $a sort return scalar context';
eb209983 625
7e7a548e
NC
626sub sortcmprq {
627 reverse sort {stuff || $a cmp $b} @_;
628}
629
630@output = sortcmpr &generate;
5823dc9f 631is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0",
7e7a548e
NC
632 'reversed stable complex sort return list context';
633$output = sortcmpr &generate;
5823dc9f 634is $output, "CCCBBBAAA",
7e7a548e
NC
635 'reversed stable complex sort return scalar context';
636
eb209983
NC
637# And now with numbers
638
639sub generate1 {
640 my $count = 'A';
641 map {new Oscalar $count++, $_} 0, 0, 0, 1, 1, 1, 2, 2, 2;
642}
643
644# This won't be very interesting
645@input = &generate1;
646@output = sort {$a cmp $b} @input;
5823dc9f 647is "@output", "A B C D E F G H I", 'stable $a cmp $b sort';
eb209983
NC
648
649@input = &generate1;
650@output = sort {$a <=> $b} @input;
5823dc9f 651is "@output", "A B C D E F G H I", 'stable $a <=> $b sort';
eb209983
NC
652
653@input = &generate1;
654@input = sort {$a <=> $b} @input;
5823dc9f 655is "@input", "A B C D E F G H I", 'stable $a <=> $b in place sort';
eb209983
NC
656
657@input = &generate1;
658@output = sort {$b <=> $a} @input;
5823dc9f 659is "@output", "G H I D E F A B C", 'stable $b <=> $a sort';
eb209983
NC
660
661@input = &generate1;
662@input = sort {$b <=> $a} @input;
5823dc9f 663is "@input", "G H I D E F A B C", 'stable $b <=> $a in place sort';
eb209983 664
59608b94
DN
665# test that optimized {$b cmp $a} and {$b <=> $a} remain stable
666# (new in 5.9) without overloading
667{ no warnings;
668@b = sort { $b <=> $a } @input = qw/5first 6first 5second 6second/;
5823dc9f 669is "@b" , "6first 6second 5first 5second", "optimized {$b <=> $a} without overloading" ;
59608b94 670@input = sort {$b <=> $a} @input;
5823dc9f 671is "@input" , "6first 6second 5first 5second","inline optimized {$b <=> $a} without overloading" ;
59608b94
DN
672};
673
eb209983 674# These two are actually doing string cmp on 0 1 and 2
75dd5fa4 675@input = &generate1;
eb209983 676@output = reverse sort @input;
5823dc9f 677is "@output", "I H G F E D C B A", "Reversed stable sort";
eb209983
NC
678
679@input = &generate1;
680@input = reverse sort @input;
5823dc9f 681is "@input", "I H G F E D C B A", "Reversed stable in place sort";
eb209983
NC
682
683@input = &generate1;
75dd5fa4 684$output = reverse sort @input;
5823dc9f 685is $output, "IHGFEDCBA", "Reversed stable sort in scalar context";
75dd5fa4
NC
686
687@input = &generate1;
eb209983 688@output = reverse sort {$a <=> $b} @input;
5823dc9f 689is "@output", "I H G F E D C B A", 'reversed stable $a <=> $b sort';
eb209983
NC
690
691@input = &generate1;
692@input = reverse sort {$a <=> $b} @input;
5823dc9f 693is "@input", "I H G F E D C B A", 'revesed stable $a <=> $b in place sort';
fe1bc4cf 694
eb209983 695@input = &generate1;
75dd5fa4 696$output = reverse sort {$a <=> $b} @input;
5823dc9f 697is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort in scalar context';
75dd5fa4
NC
698
699@input = &generate1;
eb209983 700@output = reverse sort {$b <=> $a} @input;
5823dc9f 701is "@output", "C B A F E D I H G", 'reversed stable $b <=> $a sort';
fe1bc4cf 702
eb209983
NC
703@input = &generate1;
704@input = reverse sort {$b <=> $a} @input;
5823dc9f 705is "@input", "C B A F E D I H G", 'revesed stable $b <=> $a in place sort';
75dd5fa4
NC
706
707@input = &generate1;
708$output = reverse sort {$b <=> $a} @input;
5823dc9f 709is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort in scalar context';
a1824f2a 710
7e7a548e
NC
711@input = &generate1;
712@output = reverse sort {stuff || $a <=> $b} @input;
5823dc9f 713is "@output", "I H G F E D C B A", 'reversed stable complex sort';
7e7a548e
NC
714
715@input = &generate1;
716@input = reverse sort {stuff || $a <=> $b} @input;
5823dc9f 717is "@input", "I H G F E D C B A", 'revesed stable complex in place sort';
7e7a548e
NC
718
719@input = &generate1;
720$output = reverse sort {stuff || $a <=> $b} @input;
5823dc9f 721is $output, "IHGFEDCBA", 'reversed stable complex sort in scalar context';
a1824f2a
NC
722
723sub sortnumr {
724 reverse sort {$a <=> $b} @_;
725}
726
727@output = sortnumr &generate1;
5823dc9f 728is "@output", "I H G F E D C B A",
a1824f2a
NC
729 'reversed stable $a <=> $b sort return list context';
730$output = sortnumr &generate1;
5823dc9f 731is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort return scalar context';
a1824f2a
NC
732
733sub sortnumrba {
734 reverse sort {$b <=> $a} @_;
735}
736
737@output = sortnumrba &generate1;
5823dc9f 738is "@output", "C B A F E D I H G",
a1824f2a
NC
739 'reversed stable $b <=> $a sort return list context';
740$output = sortnumrba &generate1;
5823dc9f 741is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort return scalar context';
7e7a548e
NC
742
743sub sortnumrq {
744 reverse sort {stuff || $a <=> $b} @_;
745}
746
747@output = sortnumrq &generate1;
5823dc9f 748is "@output", "I H G F E D C B A",
7e7a548e
NC
749 'reversed stable complex sort return list context';
750$output = sortnumrq &generate1;
5823dc9f 751is $output, "IHGFEDCBA", 'reversed stable complex sort return scalar context';
c093edd0
NC
752
753@output = reverse (sort(qw(C A B)), 0);
5823dc9f 754is "@output", "0 C B A", 'reversed sort with trailing argument';
c093edd0
NC
755
756@output = reverse (0, sort(qw(C A B)));
5823dc9f 757is "@output", "C B A 0", 'reversed sort with leading argument';
9850bf21
RH
758
759eval { @output = sort {goto sub {}} 1,2; };
f34362ee 760$fail_msg = q(Can't goto subroutine outside a subroutine);
5823dc9f 761cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr outside subr');
f34362ee
DL
762
763
9850bf21
RH
764
765sub goto_sub {goto sub{}}
766eval { @output = sort goto_sub 1,2; };
f34362ee 767$fail_msg = q(Can't goto subroutine from a sort sub);
5823dc9f 768cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr from a sort sub');
f34362ee
DL
769
770
9850bf21
RH
771
772eval { @output = sort {goto label} 1,2; };
f34362ee 773$fail_msg = q(Can't "goto" out of a pseudo block);
5823dc9f 774cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 1');
f34362ee
DL
775
776
9850bf21
RH
777
778sub goto_label {goto label}
779label: eval { @output = sort goto_label 1,2; };
f34362ee 780$fail_msg = q(Can't "goto" out of a pseudo block);
5823dc9f 781cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 2');
f34362ee
DL
782
783
9850bf21
RH
784
785sub self_immolate {undef &self_immolate; $a<=>$b}
786eval { @output = sort self_immolate 1,2,3 };
f34362ee 787$fail_msg = q(Can't undef active subroutine);
5823dc9f 788cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'undef active subr');
f34362ee
DL
789
790
8a27a13e
FC
791for(1,2) # We run this twice, to make sure sort does not lower the ref
792{ # count. See bug 71076.
9850bf21
RH
793 my $failed = 0;
794
795 sub rec {
796 my $n = shift;
797 if (!defined($n)) { # No arg means we're being called by sort()
798 return 1;
799 }
800 if ($n<5) { rec($n+1); }
801 else { () = sort rec 1,2; }
802
803 $failed = 1 if !defined $n;
804 }
805
806 rec(1);
5823dc9f 807 ok(!$failed, "sort from active sub");
9850bf21
RH
808}
809
810# $a and $b are set in the package the sort() is called from,
811# *not* the package the sort sub is in. This is longstanding
812# de facto behaviour that shouldn't be broken.
f34362ee 813my $answer = "good";
9850bf21
RH
814() = sort OtherPack::foo 1,2,3,4;
815
9da9462b
RGS
816{
817 package OtherPack;
818 no warnings 'once';
819 sub foo {
f34362ee 820 $answer = "something was unexpectedly defined or undefined" if
9da9462b
RGS
821 defined($a) || defined($b) || !defined($main::a) || !defined($main::b);
822 $main::a <=> $main::b;
823 }
824}
9850bf21 825
5823dc9f 826cmp_ok($answer,'eq','good','sort subr called from other package');
9850bf21
RH
827
828
829# Bug 36430 - sort called in package2 while a
830# sort in package1 is active should set $package2::a/b.
5823dc9f
MS
831{
832 my $answer = "good";
833 my @list = sort { A::min(@$a) <=> A::min(@$b) }
834 [3, 1, 5], [2, 4], [0];
835
836 cmp_ok($answer,'eq','good','bug 36430');
837
838 package A;
839 sub min {
840 my @list = sort {
841 $answer = '$a and/or $b are not defined ' if !defined($a) || !defined($b);
842 $a <=> $b;
843 } @_;
844 $list[0];
845 }
9850bf21
RH
846}
847
5823dc9f 848
9850bf21
RH
849# Bug 7567 - an array shouldn't be modifiable while it's being
850# sorted in-place.
5823dc9f
MS
851{
852 eval { @a=(1..8); @a = sort { @a = (0) } @a; };
f34362ee 853
5823dc9f
MS
854 $fail_msg = q(Modification of a read-only value attempted);
855 cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'bug 7567');
20d5dc23
FC
856 eval { @a=1..3 };
857 is $@, "", 'abrupt scope exit turns off readonliness';
5823dc9f 858}
f34362ee 859
a5f48505
DM
860# I commented out this TODO test because messing with FREEd scalars on the
861# stack can have all sorts of strange side-effects, not made safe by eval
862# - DAPM.
863#
864#{
865# local $TODO = "sort should make sure elements are not freed in the sort block";
866# eval { @nomodify_x=(1..8);
867# our @copy = sort { undef @nomodify_x; 1 } (@nomodify_x, 3); };
868# is($@, "");
869#}
84df657f 870
9850bf21
RH
871
872# Sorting shouldn't increase the refcount of a sub
5823dc9f 873{
7f4622ec
RGS
874 sub sportello {(1+$a) <=> (1+$b)}
875 my $refcnt = &Internals::SvREFCNT(\&sportello);
876 @output = sort sportello 3,7,9;
5823dc9f
MS
877
878 {
7f4622ec
RGS
879 package Doc;
880 ::is($refcnt, &Internals::SvREFCNT(\&::sportello), "sort sub refcnt");
5823dc9f
MS
881 $fail_msg = q(Modification of a read-only value attempted);
882 # Sorting a read-only array in-place shouldn't be allowed
883 my @readonly = (1..10);
884 Internals::SvREADONLY(@readonly, 1);
885 eval { @readonly = sort @readonly; };
886 ::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'in-place sort of read-only array');
887 }
888}
f34362ee 889
9850bf21
RH
890
891# Using return() should be okay even in a deeper context
892@b = sort {while (1) {return ($a <=> $b)} } 1..10;
5823dc9f 893is("@b", "1 2 3 4 5 6 7 8 9 10", "return within loop");
d7507f74
RH
894
895# Using return() should be okay even if there are other items
896# on the stack at the time.
897@b = sort {$_ = ($a<=>$b) + do{return $b<=> $a}} 1..10;
5823dc9f 898is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack");
d7507f74
RH
899
900# As above, but with a sort sub rather than a sort block.
901sub ret_with_stacked { $_ = ($a<=>$b) + do {return $b <=> $a} }
902@b = sort ret_with_stacked 1..10;
5823dc9f 903is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack");
93e19c0f
Z
904
905# Comparison code should be able to give result in non-integer representation.
906sub cmp_as_string($$) { $_[0] < $_[1] ? "-1" : $_[0] == $_[1] ? "0" : "+1" }
907@b = sort { cmp_as_string($a, $b) } (1,5,4,7,3,2,3);
908is("@b", "1 2 3 3 4 5 7", "comparison result as string");
909@b = sort cmp_as_string (1,5,4,7,3,2,3);
910is("@b", "1 2 3 3 4 5 7", "comparison result as string");
bdbefedf
DM
911
912# RT #34604: sort didn't honour overloading if the overloaded elements
913# were retrieved via tie
914
915{
916 package RT34604;
917
918 sub TIEHASH { bless {
919 p => bless({ val => 2 }),
920 q => bless({ val => 1 }),
921 }
922 }
923 sub FETCH { $_[0]{$_[1] } }
924
925 my $cc = 0;
926 sub compare { $cc++; $_[0]{val} cmp $_[1]{val} }
927 my $cs = 0;
928 sub str { $cs++; $_[0]{val} }
929
930 use overload 'cmp' => \&compare, '""' => \&str;
931
932 package main;
933
934 tie my %h, 'RT34604';
935 my @sorted = sort @h{qw(p q)};
936 is($cc, 1, 'overload compare called once');
937 is("@sorted","1 2", 'overload sort result');
938 is($cs, 2, 'overload string called twice');
939}
8f443ca6 940
f3106bc8 941fresh_perl_is('sub w ($$) {my ($l, $r) = @_; my $v = \@_; undef @_; $l <=> $r}; print join q{ }, sort w 3, 1, 2, 0',
8f443ca6
GG
942 '0 1 2 3',
943 {stderr => 1, switches => ['-w']},
944 'RT #72334');
945
f3106bc8 946fresh_perl_is('sub w ($$) {my ($l, $r) = @_; my $v = \@_; undef @_; @_ = 0..2; $l <=> $r}; print join q{ }, sort w 3, 1, 2, 0',
8f443ca6
GG
947 '0 1 2 3',
948 {stderr => 1, switches => ['-w']},
949 'RT #72334');
133acf7b
NC
950
951{
952 my $count = 0;
953 {
954 package Counter;
955
956 sub new {
957 ++$count;
958 bless [];
959 }
960
961 sub DESTROY {
962 --$count;
963 }
964 }
965
966 sub sorter ($$) {
967 my ($l, $r) = @_;
968 my $q = \@_;
969 $l <=> $r;
970 }
971
972 is($count, 0, 'None before we start');
973 my @a = map { Counter->new() } 0..1;
974 is($count, 2, '2 here');
975
976 my @b = sort sorter @a;
977
978 is(scalar @b, 2);
979 cmp_ok($b[0], '<', $b[1], 'sorted!');
980
981 is($count, 2, 'still the same 2 here');
982
983 @a = (); @b = ();
984
985 is($count, 0, 'all gone');
986}
8e7d0c4b
FC
987
988# [perl #77930] The context stack may be reallocated during a sort, as a
989# result of deeply-nested (or not-so-deeply-nested) calls
990# from a custom sort subroutine.
991fresh_perl_is
992 '
993 $sub = sub {
994 local $count = $count+1;
995 ()->$sub if $count < 1000;
996 $a cmp $b
997 };
998 () = sort $sub qw<a b c d e f g>;
999 print "ok"
1000 ',
1001 'ok',
1002 {},
92c404cd 1003 '[perl #77930] cx_stack reallocation during sort'
8e7d0c4b 1004;
ad021bfb
FC
1005
1006# [perl #76026]
1007# Match vars should not leak from one sort sub call to the next
1008{
1009 my $output = '';
1010 sub soarter {
1011 $output .= $1;
1012 "Leakage" =~ /(.*)/;
1013 1
1014 }
1015 sub soarterdd($$) {
1016 $output .= $1;
1017 "Leakage" =~ /(.*)/;
1018 1
1019 }
1020
1021 "Win" =~ /(.*)/;
1022 my @b = sort soarter 0..2;
1023
1024 like $output, qr/^(?:Win)+\z/,
1025 "Match vars do not leak from one plain sort sub to the next";
1026
1027 $output = '';
1028
1029 "Win" =~ /(.*)/;
1030 @b = sort soarterdd 0..2;
1031
1032 like $output, qr/^(?:Win)+\z/,
1033 'Match vars do not leak from one $$ sort sub to the next';
1034}
f7bc00ea
FC
1035
1036# [perl #30661] autoloading
1037AUTOLOAD { $b <=> $a }
1038sub stubbedsub;
1039is join("", sort stubbedsub split//, '04381091'), '98431100',
1040 'stubborn AUTOLOAD';
1041is join("", sort hopefullynonexistent split//, '04381091'), '98431100',
1042 'AUTOLOAD without stub';
1043my $stubref = \&givemeastub;
1044is join("", sort $stubref split//, '04381091'), '98431100',
1045 'AUTOLOAD with stubref';
a46b39a8
FC
1046
1047# [perl #90030] sort without arguments
5d4b936e 1048eval '@x = (sort); 1';
a46b39a8
FC
1049is $@, '', '(sort) does not die';
1050is @x, 0, '(sort) returns empty list';
5d4b936e 1051eval '@x = sort; 1';
a46b39a8
FC
1052is $@, '', 'sort; does not die';
1053is @x, 0, 'sort; returns empty list';
1054eval '{@x = sort} 1';
1055is $@, '', '{sort} does not die';
1056is @x, 0, '{sort} returns empty list';
a7fd8ef6
DM
1057
1058# this happened while the padrange op was being added. Sort blocks
1059# are executed in void context, and the padrange op was skipping pushing
1060# the item in void cx. The net result was that the return value was
1061# whatever was on the stack last.
1062
1063{
1064 my @a = sort {
1065 my $r = $a <=> $b;
1066 if ($r) {
1067 undef; # this got returned by mistake
1068 return $r
1069 }
1070 return 0;
1071 } 5,1,3,6,0;
1072 is "@a", "0 1 3 5 6", "padrange and void context";
1073}
2f43ddf1
FC
1074
1075# Fatal warnings an sort sub returning a non-number
1076# We need two evals, because the panic used to happen on scope exit.
1077eval { eval { use warnings FATAL => 'all'; () = sort { undef } 1,2 } };
1078is $@, "",
1079 'no panic/crash with fatal warnings when sort sub returns undef';
1080eval { eval { use warnings FATAL => 'all'; () = sort { "no thin" } 1,2 } };
1081is $@, "",
1082 'no panic/crash with fatal warnings when sort sub returns string';
1083sub notdef($$) { undef }
1084eval { eval { use warnings FATAL => 'all'; () = sort notdef 1,2 } };
1085is $@, "",
1086 'no panic/crash with fatal warnings when sort sub($$) returns undef';
1087sub yarn($$) { "no thinking aloud" }
1088eval { eval { use warnings FATAL => 'all'; () = sort yarn 1,2 } };
1089is $@, "",
1090 'no panic/crash with fatal warnings when sort sub($$) returns string';
f65493df
FC
1091
1092$#a = -1;
1093() = [sort { $a = 10; $b = 10; 0 } $#a, $#a];
1094is $#a, 10, 'sort block modifying $a and $b';
2d885586 1095
2d885586
FC
1096() = sort {
1097 is \$a, \$a, '[perl #78194] op return values passed to sort'; 0
1098} "${\''}", "${\''}";
8465ba45
FC
1099
1100package deletions {
1101 @_=sort { delete $deletions::{a}; delete $deletions::{b}; 3 } 1..3;
1102}
1103pass "no crash when sort block deletes *a and *b";
33411212
DM
1104
1105# make sure return args are always evaluated in scalar context
1106
1107{
1108 package Ret;
1109 no warnings 'void';
1110 sub f0 { }
1111 sub f1 { $b <=> $a, $a <=> $b }
1112 sub f2 { return ($b <=> $a, $a <=> $b) }
1113 sub f3 { for ($b <=> $a) { return ($b <=> $a, $a <=> $b) } }
1114
1115 {
1116 no warnings 'uninitialized';
1117 ::is (join('-', sort { () } 3,1,2,4), '3-1-2-4', "Ret: null blk");
1118 }
1119 ::is (join('-', sort { $b <=> $a, $a <=> $b } 3,1,2,4), '1-2-3-4', "Ret: blk");
1120 ::is (join('-', sort { for($b <=> $a) { return ($b <=> $a, $a <=> $b) } }
1121 3,1,2,4), '1-2-3-4', "Ret: blk ret");
1122 {
1123 no warnings 'uninitialized';
1124 ::is (join('-', sort f0 3,1,2,4), '3-1-2-4', "Ret: f0");
1125 }
1126 ::is (join('-', sort f1 3,1,2,4), '1-2-3-4', "Ret: f1");
1127 ::is (join('-', sort f2 3,1,2,4), '1-2-3-4', "Ret: f2");
1128 ::is (join('-', sort f3 3,1,2,4), '1-2-3-4', "Ret: f3");
1129}
dc9ef998
TC
1130
1131{
1132 @a = sort{ *a=0; 1} 0..1;
1133 pass "No crash when GP deleted out from under us [perl 124097]";
1134
1135 no warnings 'redefine';
1136 # some alternative non-solutions localized modifications to *a and *b
1137 sub a { 0 };
1138 @a = sort { *a = sub { 1 }; $a <=> $b } 0 .. 1;
1139 ok(a(), "*a wasn't localized inadvertantly");
1140}