Commit | Line | Data |
---|---|---|
a687059c LW |
1 | #!./perl |
2 | ||
9c007264 JH |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
5823dc9f MS |
5 | @INC = qw(. ../lib); |
6 | require 'test.pl'; | |
9c007264 | 7 | } |
9f1b1f2d | 8 | use warnings; |
a46b39a8 | 9 | plan( tests => 171 ); |
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 |
23 | sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 } |
24 | sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 } | |
9850bf21 | 25 | sub Backwards_other { $a lt $b ? 1 : $a gt $b ? -1 : 0 } |
a687059c | 26 | |
9d116dd7 JH |
27 | my $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 | |
49 | cmp_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 | |
54 | cmp_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 | |
59 | cmp_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 |
66 | cmp_ok($x,'eq',$expected,'upper first 4'); |
67 | $" = ' '; | |
03a14243 LW |
68 | @a = (); |
69 | @b = reverse @a; | |
f34362ee | 70 | cmp_ok("@b",'eq',"",'reverse 1'); |
03a14243 LW |
71 | |
72 | @a = (1); | |
73 | @b = reverse @a; | |
f34362ee | 74 | cmp_ok("@b",'eq',"1",'reverse 2'); |
03a14243 LW |
75 | |
76 | @a = (1,2); | |
77 | @b = reverse @a; | |
f34362ee | 78 | cmp_ok("@b",'eq',"2 1",'reverse 3'); |
03a14243 LW |
79 | |
80 | @a = (1,2,3); | |
81 | @b = reverse @a; | |
f34362ee | 82 | cmp_ok("@b",'eq',"3 2 1",'reverse 4'); |
03a14243 LW |
83 | |
84 | @a = (1,2,3,4); | |
85 | @b = reverse @a; | |
f34362ee | 86 | cmp_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 | 90 | cmp_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 | |
96 | cmp_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 | |
102 | cmp_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 |
107 | cmp_ok("@b",'eq','1 2 3 4','just sort'); |
108 | ||
cd5de442 GS |
109 | |
110 | @b = sort grep { $_ } (4,1,3,2); | |
f34362ee DL |
111 | cmp_ok("@b",'eq','1 2 3 4','grep then sort'); |
112 | ||
cd5de442 GS |
113 | |
114 | @b = sort map { $_ } (4,1,3,2); | |
f34362ee DL |
115 | cmp_ok("@b",'eq','1 2 3 4','map then sort'); |
116 | ||
cd5de442 GS |
117 | |
118 | @b = sort reverse (4,1,3,2); | |
f34362ee DL |
119 | cmp_ok("@b",'eq','1 2 3 4','reverse then sort'); |
120 | ||
121 | ||
7bac28a0 | 122 | |
9850bf21 | 123 | sub twoface { no warnings 'redefine'; *twoface = sub { $a <=> $b }; &twoface } |
7bac28a0 | 124 | eval { @b = sort twoface 4,1,3,2 }; |
f34362ee DL |
125 | cmp_ok("@b",'eq','1 2 3 4','redefine sort sub inside the sort sub'); |
126 | ||
7bac28a0 | 127 | |
9f1b1f2d | 128 | eval { no warnings 'redefine'; *twoface = sub { &Backwards } }; |
f34362ee | 129 | ok(!$@,"redefining sort subs outside the sort \$@=[$@]"); |
7bac28a0 | 130 | |
131 | eval { @b = sort twoface 4,1,3,2 }; | |
f34362ee | 132 | cmp_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 | 139 | eval { @b = sort twoface 4,1,9,5 }; |
f34362ee | 140 | ok(($@ 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 | 150 | eval { @b = sort twoface 4,1 }; |
f34362ee | 151 | cmp_ok(substr($@,0,4), 'eq', 'good', 'twoface eval'); |
15f0808c GS |
152 | |
153 | eval <<'CODE'; | |
9f1b1f2d | 154 | my @result = sort main'Backwards 'one', 'two'; |
15f0808c | 155 | CODE |
f34362ee | 156 | cmp_ok($@,'eq','',q(old skool package)); |
15f0808c GS |
157 | |
158 | eval <<'CODE'; | |
159 | # "sort 'one', 'two'" should not try to parse "'one" as a sort sub | |
160 | my @result = sort 'one', 'two'; | |
161 | CODE | |
f34362ee | 162 | cmp_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 |
230 | cmp_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 |
234 | cmp_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 |
238 | cmp_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 | 261 | cmp_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 | ||
408 | sub generate { | |
409 | my $count = 0; | |
410 | map {new Oscalar $_, $count++} qw(A A A B B B C C C); | |
411 | } | |
412 | ||
413 | my @input = &generate; | |
414 | my @output = sort @input; | |
5823dc9f | 415 | is 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 | 419 | is 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 | 425 | is "@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 | 429 | is 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 | 433 | is 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 | 438 | is 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 | 442 | is 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 | 447 | is 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 | 451 | is 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; |
455 | my $output = reverse sort @input; | |
5823dc9f | 456 | is $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 | 461 | is 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 | 466 | is 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 | 471 | is $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 | 475 | is 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 | 480 | is 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 | 485 | is $output, "AAABBBCCC", 'Reversed stable $b cmp $a sort in scalar context'; |
75dd5fa4 | 486 | |
7e7a548e NC |
487 | sub 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 | 494 | is 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 | 499 | is 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 | 504 | is $output, "CCCBBBAAA", 'Reversed stable complex sort in scalar context'; |
7e7a548e | 505 | |
a1824f2a NC |
506 | sub sortr { |
507 | reverse sort @_; | |
508 | } | |
509 | ||
510 | @output = sortr &generate; | |
5823dc9f | 511 | is 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 | 514 | is $output, "CCCBBBAAA", |
a1824f2a NC |
515 | 'reversed stable sort return scalar context'; |
516 | ||
517 | sub sortcmpr { | |
518 | reverse sort {$a cmp $b} @_; | |
519 | } | |
520 | ||
521 | @output = sortcmpr &generate; | |
5823dc9f | 522 | is 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 | 525 | is $output, "CCCBBBAAA", |
a1824f2a NC |
526 | 'reversed stable $a cmp $b sort return scalar context'; |
527 | ||
528 | sub sortcmprba { | |
529 | reverse sort {$b cmp $a} @_; | |
530 | } | |
531 | ||
532 | @output = sortcmprba &generate; | |
5823dc9f | 533 | is 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 | 536 | is $output, "AAABBBCCC", |
a1824f2a | 537 | 'reversed stable $b cmp $a sort return scalar context'; |
eb209983 | 538 | |
7e7a548e NC |
539 | sub sortcmprq { |
540 | reverse sort {stuff || $a cmp $b} @_; | |
541 | } | |
542 | ||
543 | @output = sortcmpr &generate; | |
5823dc9f | 544 | is 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 | 547 | is $output, "CCCBBBAAA", |
7e7a548e NC |
548 | 'reversed stable complex sort return scalar context'; |
549 | ||
eb209983 NC |
550 | # And now with numbers |
551 | ||
552 | sub 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 | 560 | is "@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 | 564 | is "@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 | 568 | is "@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 | 572 | is "@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 | 576 | is "@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 | 582 | is "@b" , "6first 6second 5first 5second", "optimized {$b <=> $a} without overloading" ; |
59608b94 | 583 | @input = sort {$b <=> $a} @input; |
5823dc9f | 584 | is "@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 | 590 | is "@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 | 594 | is "@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 | 598 | is $output, "IHGFEDCBA", "Reversed stable sort in scalar context"; |
75dd5fa4 NC |
599 | |
600 | @input = &generate1; | |
eb209983 | 601 | @output = reverse sort {$a <=> $b} @input; |
5823dc9f | 602 | is "@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 | 606 | is "@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 | 610 | is $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 | 614 | is "@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 | 618 | is "@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 | 622 | is $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 | 626 | is "@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 | 630 | is "@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 | 634 | is $output, "IHGFEDCBA", 'reversed stable complex sort in scalar context'; |
a1824f2a NC |
635 | |
636 | sub sortnumr { | |
637 | reverse sort {$a <=> $b} @_; | |
638 | } | |
639 | ||
640 | @output = sortnumr &generate1; | |
5823dc9f | 641 | is "@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 | 644 | is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort return scalar context'; |
a1824f2a NC |
645 | |
646 | sub sortnumrba { | |
647 | reverse sort {$b <=> $a} @_; | |
648 | } | |
649 | ||
650 | @output = sortnumrba &generate1; | |
5823dc9f | 651 | is "@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 | 654 | is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort return scalar context'; |
7e7a548e NC |
655 | |
656 | sub sortnumrq { | |
657 | reverse sort {stuff || $a <=> $b} @_; | |
658 | } | |
659 | ||
660 | @output = sortnumrq &generate1; | |
5823dc9f | 661 | is "@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 | 664 | is $output, "IHGFEDCBA", 'reversed stable complex sort return scalar context'; |
c093edd0 NC |
665 | |
666 | @output = reverse (sort(qw(C A B)), 0); | |
5823dc9f | 667 | is "@output", "0 C B A", 'reversed sort with trailing argument'; |
c093edd0 NC |
668 | |
669 | @output = reverse (0, sort(qw(C A B))); | |
5823dc9f | 670 | is "@output", "C B A 0", 'reversed sort with leading argument'; |
9850bf21 RH |
671 | |
672 | eval { @output = sort {goto sub {}} 1,2; }; | |
f34362ee | 673 | $fail_msg = q(Can't goto subroutine outside a subroutine); |
5823dc9f | 674 | cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr outside subr'); |
f34362ee DL |
675 | |
676 | ||
9850bf21 RH |
677 | |
678 | sub goto_sub {goto sub{}} | |
679 | eval { @output = sort goto_sub 1,2; }; | |
f34362ee | 680 | $fail_msg = q(Can't goto subroutine from a sort sub); |
5823dc9f | 681 | cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr from a sort sub'); |
f34362ee DL |
682 | |
683 | ||
9850bf21 RH |
684 | |
685 | eval { @output = sort {goto label} 1,2; }; | |
f34362ee | 686 | $fail_msg = q(Can't "goto" out of a pseudo block); |
5823dc9f | 687 | cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 1'); |
f34362ee DL |
688 | |
689 | ||
9850bf21 RH |
690 | |
691 | sub goto_label {goto label} | |
692 | label: eval { @output = sort goto_label 1,2; }; | |
f34362ee | 693 | $fail_msg = q(Can't "goto" out of a pseudo block); |
5823dc9f | 694 | cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 2'); |
f34362ee DL |
695 | |
696 | ||
9850bf21 RH |
697 | |
698 | sub self_immolate {undef &self_immolate; $a<=>$b} | |
699 | eval { @output = sort self_immolate 1,2,3 }; | |
f34362ee | 700 | $fail_msg = q(Can't undef active subroutine); |
5823dc9f | 701 | cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'undef active subr'); |
f34362ee DL |
702 | |
703 | ||
8a27a13e FC |
704 | for(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 | 726 | my $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 | 739 | cmp_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"; | |
39984de3 FC |
773 | eval { @nomodify_x=(1..8); |
774 | our @copy = sort { undef @nomodify_x; 1 } (@nomodify_x, 3); }; | |
84df657f GG |
775 | is($@, ""); |
776 | } | |
777 | ||
9850bf21 RH |
778 | |
779 | # Sorting shouldn't increase the refcount of a sub | |
5823dc9f | 780 | { |
7f4622ec RGS |
781 | sub sportello {(1+$a) <=> (1+$b)} |
782 | my $refcnt = &Internals::SvREFCNT(\&sportello); | |
783 | @output = sort sportello 3,7,9; | |
5823dc9f MS |
784 | |
785 | { | |
7f4622ec RGS |
786 | package Doc; |
787 | ::is($refcnt, &Internals::SvREFCNT(\&::sportello), "sort sub refcnt"); | |
5823dc9f MS |
788 | $fail_msg = q(Modification of a read-only value attempted); |
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; }; | |
793 | ::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'in-place sort of read-only array'); | |
794 | } | |
795 | } | |
f34362ee | 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; | |
5823dc9f | 800 | is("@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; | |
5823dc9f | 805 | is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack"); |
d7507f74 RH |
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; | |
5823dc9f | 810 | is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack"); |
93e19c0f Z |
811 | |
812 | # Comparison code should be able to give result in non-integer representation. | |
813 | sub cmp_as_string($$) { $_[0] < $_[1] ? "-1" : $_[0] == $_[1] ? "0" : "+1" } | |
814 | @b = sort { cmp_as_string($a, $b) } (1,5,4,7,3,2,3); | |
815 | is("@b", "1 2 3 3 4 5 7", "comparison result as string"); | |
816 | @b = sort cmp_as_string (1,5,4,7,3,2,3); | |
817 | is("@b", "1 2 3 3 4 5 7", "comparison result as string"); | |
bdbefedf DM |
818 | |
819 | # RT #34604: sort didn't honour overloading if the overloaded elements | |
820 | # were retrieved via tie | |
821 | ||
822 | { | |
823 | package RT34604; | |
824 | ||
825 | sub TIEHASH { bless { | |
826 | p => bless({ val => 2 }), | |
827 | q => bless({ val => 1 }), | |
828 | } | |
829 | } | |
830 | sub FETCH { $_[0]{$_[1] } } | |
831 | ||
832 | my $cc = 0; | |
833 | sub compare { $cc++; $_[0]{val} cmp $_[1]{val} } | |
834 | my $cs = 0; | |
835 | sub str { $cs++; $_[0]{val} } | |
836 | ||
837 | use overload 'cmp' => \&compare, '""' => \&str; | |
838 | ||
839 | package main; | |
840 | ||
841 | tie my %h, 'RT34604'; | |
842 | my @sorted = sort @h{qw(p q)}; | |
843 | is($cc, 1, 'overload compare called once'); | |
844 | is("@sorted","1 2", 'overload sort result'); | |
845 | is($cs, 2, 'overload string called twice'); | |
846 | } | |
8f443ca6 GG |
847 | |
848 | fresh_perl_is('sub w ($$) {my ($l, my $r) = @_; my $v = \@_; undef @_; $l <=> $r}; print join q{ }, sort w 3, 1, 2, 0', | |
849 | '0 1 2 3', | |
850 | {stderr => 1, switches => ['-w']}, | |
851 | 'RT #72334'); | |
852 | ||
853 | fresh_perl_is('sub w ($$) {my ($l, my $r) = @_; my $v = \@_; undef @_; @_ = 0..2; $l <=> $r}; print join q{ }, sort w 3, 1, 2, 0', | |
854 | '0 1 2 3', | |
855 | {stderr => 1, switches => ['-w']}, | |
856 | 'RT #72334'); | |
133acf7b NC |
857 | |
858 | { | |
859 | my $count = 0; | |
860 | { | |
861 | package Counter; | |
862 | ||
863 | sub new { | |
864 | ++$count; | |
865 | bless []; | |
866 | } | |
867 | ||
868 | sub DESTROY { | |
869 | --$count; | |
870 | } | |
871 | } | |
872 | ||
873 | sub sorter ($$) { | |
874 | my ($l, $r) = @_; | |
875 | my $q = \@_; | |
876 | $l <=> $r; | |
877 | } | |
878 | ||
879 | is($count, 0, 'None before we start'); | |
880 | my @a = map { Counter->new() } 0..1; | |
881 | is($count, 2, '2 here'); | |
882 | ||
883 | my @b = sort sorter @a; | |
884 | ||
885 | is(scalar @b, 2); | |
886 | cmp_ok($b[0], '<', $b[1], 'sorted!'); | |
887 | ||
888 | is($count, 2, 'still the same 2 here'); | |
889 | ||
890 | @a = (); @b = (); | |
891 | ||
892 | is($count, 0, 'all gone'); | |
893 | } | |
8e7d0c4b FC |
894 | |
895 | # [perl #77930] The context stack may be reallocated during a sort, as a | |
896 | # result of deeply-nested (or not-so-deeply-nested) calls | |
897 | # from a custom sort subroutine. | |
898 | fresh_perl_is | |
899 | ' | |
900 | $sub = sub { | |
901 | local $count = $count+1; | |
902 | ()->$sub if $count < 1000; | |
903 | $a cmp $b | |
904 | }; | |
905 | () = sort $sub qw<a b c d e f g>; | |
906 | print "ok" | |
907 | ', | |
908 | 'ok', | |
909 | {}, | |
92c404cd | 910 | '[perl #77930] cx_stack reallocation during sort' |
8e7d0c4b | 911 | ; |
ad021bfb FC |
912 | |
913 | # [perl #76026] | |
914 | # Match vars should not leak from one sort sub call to the next | |
915 | { | |
916 | my $output = ''; | |
917 | sub soarter { | |
918 | $output .= $1; | |
919 | "Leakage" =~ /(.*)/; | |
920 | 1 | |
921 | } | |
922 | sub soarterdd($$) { | |
923 | $output .= $1; | |
924 | "Leakage" =~ /(.*)/; | |
925 | 1 | |
926 | } | |
927 | ||
928 | "Win" =~ /(.*)/; | |
929 | my @b = sort soarter 0..2; | |
930 | ||
931 | like $output, qr/^(?:Win)+\z/, | |
932 | "Match vars do not leak from one plain sort sub to the next"; | |
933 | ||
934 | $output = ''; | |
935 | ||
936 | "Win" =~ /(.*)/; | |
937 | @b = sort soarterdd 0..2; | |
938 | ||
939 | like $output, qr/^(?:Win)+\z/, | |
940 | 'Match vars do not leak from one $$ sort sub to the next'; | |
941 | } | |
f7bc00ea FC |
942 | |
943 | # [perl #30661] autoloading | |
944 | AUTOLOAD { $b <=> $a } | |
945 | sub stubbedsub; | |
946 | is join("", sort stubbedsub split//, '04381091'), '98431100', | |
947 | 'stubborn AUTOLOAD'; | |
948 | is join("", sort hopefullynonexistent split//, '04381091'), '98431100', | |
949 | 'AUTOLOAD without stub'; | |
950 | my $stubref = \&givemeastub; | |
951 | is join("", sort $stubref split//, '04381091'), '98431100', | |
952 | 'AUTOLOAD with stubref'; | |
a46b39a8 FC |
953 | |
954 | # [perl #90030] sort without arguments | |
5d4b936e | 955 | eval '@x = (sort); 1'; |
a46b39a8 FC |
956 | is $@, '', '(sort) does not die'; |
957 | is @x, 0, '(sort) returns empty list'; | |
5d4b936e | 958 | eval '@x = sort; 1'; |
a46b39a8 FC |
959 | is $@, '', 'sort; does not die'; |
960 | is @x, 0, 'sort; returns empty list'; | |
961 | eval '{@x = sort} 1'; | |
962 | is $@, '', '{sort} does not die'; | |
963 | is @x, 0, '{sort} returns empty list'; |