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