Commit | Line | Data |
---|---|---|
a687059c | 1 | #!./perl |
d0664088 | 2 | $|=1; |
a687059c | 3 | |
9c007264 JH |
4 | BEGIN { |
5 | chdir 't' if -d 't'; | |
ab4454fc | 6 | require './test.pl'; |
43ece5b1 | 7 | set_up_inc('../lib'); |
9c007264 | 8 | } |
9f1b1f2d | 9 | use warnings; |
45c198c1 | 10 | plan(tests => 196); |
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 TS |
19 | |
20 | # Change 26011: Re: A surprising segfault | |
21 | map scalar(sort(+())), ('')x68; | |
71a29c3c GS |
22 | } |
23 | ||
9f1b1f2d GS |
24 | sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 } |
25 | sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 } | |
9850bf21 | 26 | sub Backwards_other { $a lt $b ? 1 : $a gt $b ? -1 : 0 } |
a687059c | 27 | |
9d116dd7 JH |
28 | my $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 | |
50 | cmp_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 | |
55 | cmp_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 | |
60 | cmp_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 |
67 | my @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. | |
75 | if (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. | |
81 | my $prefix = "a\xb6"; | |
82 | my $prefix_len = length $prefix; | |
83 | ||
84 | my @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. | |
89 | my @initially_unsorted = reverse @chr_initially_sorted; | |
90 | for (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 | |
96 | my @utf8_initialy_unsorted = @initially_unsorted; | |
97 | utf8::upgrade($_) for @utf8_initialy_unsorted; | |
98 | ||
99 | # Sort the non-UTF-8 version | |
100 | my @non_utf8_result = sort @initially_unsorted; | |
101 | my @wrongly_utf8; | |
102 | my $ordered_correctly = 1; | |
103 | for 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 | } | |
112 | if (! 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 | } | |
116 | if (! 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 | |
123 | my @wrongly_non_utf8; | |
124 | $ordered_correctly = 1; | |
125 | my @utf8_result = sort @utf8_initialy_unsorted; | |
126 | for 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 | } | |
135 | if (! 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 | } | |
139 | if (! 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 |
145 | cmp_ok($x,'eq',$expected,'upper first 4'); |
146 | $" = ' '; | |
03a14243 LW |
147 | @a = (); |
148 | @b = reverse @a; | |
f34362ee | 149 | cmp_ok("@b",'eq',"",'reverse 1'); |
03a14243 LW |
150 | |
151 | @a = (1); | |
152 | @b = reverse @a; | |
f34362ee | 153 | cmp_ok("@b",'eq',"1",'reverse 2'); |
03a14243 LW |
154 | |
155 | @a = (1,2); | |
156 | @b = reverse @a; | |
f34362ee | 157 | cmp_ok("@b",'eq',"2 1",'reverse 3'); |
03a14243 LW |
158 | |
159 | @a = (1,2,3); | |
160 | @b = reverse @a; | |
f34362ee | 161 | cmp_ok("@b",'eq',"3 2 1",'reverse 4'); |
03a14243 LW |
162 | |
163 | @a = (1,2,3,4); | |
164 | @b = reverse @a; | |
f34362ee | 165 | cmp_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 | 169 | cmp_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 | |
175 | cmp_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 | |
181 | cmp_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 |
186 | cmp_ok("@b",'eq','1 2 3 4','just sort'); |
187 | ||
cd5de442 GS |
188 | |
189 | @b = sort grep { $_ } (4,1,3,2); | |
f34362ee DL |
190 | cmp_ok("@b",'eq','1 2 3 4','grep then sort'); |
191 | ||
cd5de442 GS |
192 | |
193 | @b = sort map { $_ } (4,1,3,2); | |
f34362ee DL |
194 | cmp_ok("@b",'eq','1 2 3 4','map then sort'); |
195 | ||
cd5de442 GS |
196 | |
197 | @b = sort reverse (4,1,3,2); | |
f34362ee DL |
198 | cmp_ok("@b",'eq','1 2 3 4','reverse then sort'); |
199 | ||
200 | ||
01b5ef50 FC |
201 | @b = sort CORE::reverse (4,1,3,2); |
202 | cmp_ok("@b",'eq','1 2 3 4','CORE::reverse then sort'); | |
203 | ||
487e470d FC |
204 | eval { @b = sort CORE::revers (4,1,3,2); }; |
205 | like($@, qr/^Undefined sort subroutine "CORE::revers" called at /); | |
01b5ef50 | 206 | |
7bac28a0 | 207 | |
9850bf21 | 208 | sub twoface { no warnings 'redefine'; *twoface = sub { $a <=> $b }; &twoface } |
7bac28a0 | 209 | eval { @b = sort twoface 4,1,3,2 }; |
f34362ee DL |
210 | cmp_ok("@b",'eq','1 2 3 4','redefine sort sub inside the sort sub'); |
211 | ||
7bac28a0 | 212 | |
9f1b1f2d | 213 | eval { no warnings 'redefine'; *twoface = sub { &Backwards } }; |
f34362ee | 214 | ok(!$@,"redefining sort subs outside the sort \$@=[$@]"); |
7bac28a0 | 215 | |
216 | eval { @b = sort twoface 4,1,3,2 }; | |
f34362ee | 217 | cmp_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 | 224 | eval { @b = sort twoface 4,1,9,5 }; |
f34362ee | 225 | ok(($@ 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 | 232 | $a <=> $b; |
233 | }; | |
9f1b1f2d | 234 | } |
7bac28a0 | 235 | eval { @b = sort twoface 4,1 }; |
f34362ee | 236 | cmp_ok(substr($@,0,4), 'eq', 'good', 'twoface eval'); |
15f0808c GS |
237 | |
238 | eval <<'CODE'; | |
9f1b1f2d | 239 | my @result = sort main'Backwards 'one', 'two'; |
15f0808c | 240 | CODE |
f34362ee | 241 | cmp_ok($@,'eq','',q(old skool package)); |
15f0808c GS |
242 | |
243 | eval <<'CODE'; | |
244 | # "sort 'one', 'two'" should not try to parse "'one" as a sort sub | |
245 | my @result = sort 'one', 'two'; | |
246 | CODE | |
f34362ee | 247 | cmp_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 |
315 | cmp_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 |
319 | cmp_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 |
323 | cmp_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 | 346 | cmp_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]; | |
45c198c1 | 420 | is "$$r1-$$r2-@g", "1-1-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]; | |
45c198c1 | 423 | is "$$r1-$$r2-@a", "a-a-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]; | |
45c198c1 | 426 | is "$$r1-$$r2-@g", "3-3-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]; | |
45c198c1 | 430 | is "$$r1-$$r2-@g", "3-3-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]; | |
45c198c1 | 434 | is "$$r1-$$r2-@a", "c-c-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"); |
84721d61 DM |
477 | |
478 | # RT #39358 - array should be preserved during sort | |
479 | ||
480 | { | |
481 | my @aa = qw(b c a); | |
482 | my @copy; | |
483 | @aa = sort { @copy = @aa; $a cmp $b } @aa; | |
484 | is "@aa", "a b c", "RT 39358 - aa"; | |
485 | is "@copy", "b c a", "RT 39358 - copy"; | |
486 | } | |
45c198c1 DM |
487 | |
488 | # RT #128340: in-place sort incorrectly preserves element lvalue identity | |
489 | ||
490 | @a = (5, 4, 3); | |
491 | my $r = \$a[2]; | |
492 | @a = sort { $a <=> $b } @a; | |
493 | $$r = "z"; | |
494 | is ("@a", "3 4 5", "RT #128340"); | |
495 | ||
fe1bc4cf DM |
496 | } |
497 | ||
eb209983 NC |
498 | # Test optimisations of reversed sorts. As we now guarantee stability by |
499 | # default, # optimisations which do not provide this are bogus. | |
fe1bc4cf | 500 | |
eb209983 NC |
501 | { |
502 | package Oscalar; | |
503 | use overload (qw("" stringify 0+ numify fallback 1)); | |
504 | ||
505 | sub new { | |
506 | bless [$_[1], $_[2]], $_[0]; | |
507 | } | |
508 | ||
509 | sub stringify { $_[0]->[0] } | |
510 | ||
511 | sub numify { $_[0]->[1] } | |
512 | } | |
513 | ||
514 | sub generate { | |
515 | my $count = 0; | |
516 | map {new Oscalar $_, $count++} qw(A A A B B B C C C); | |
517 | } | |
518 | ||
519 | my @input = &generate; | |
520 | my @output = sort @input; | |
5823dc9f | 521 | is join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", "Simple stable sort"; |
eb209983 NC |
522 | |
523 | @input = &generate; | |
524 | @input = sort @input; | |
5823dc9f | 525 | is join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8", |
eb209983 NC |
526 | "Simple stable in place sort"; |
527 | ||
528 | # This won't be very interesting | |
529 | @input = &generate; | |
530 | @output = sort {$a <=> $b} @input; | |
5823dc9f | 531 | is "@output", "A A A B B B C C C", 'stable $a <=> $b sort'; |
eb209983 NC |
532 | |
533 | @input = &generate; | |
534 | @output = sort {$a cmp $b} @input; | |
5823dc9f | 535 | is join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", 'stable $a cmp $b sort'; |
eb209983 NC |
536 | |
537 | @input = &generate; | |
538 | @input = sort {$a cmp $b} @input; | |
5823dc9f | 539 | is join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8", |
eb209983 NC |
540 | 'stable $a cmp $b in place sort'; |
541 | ||
542 | @input = &generate; | |
543 | @output = sort {$b cmp $a} @input; | |
5823dc9f | 544 | is join(" ", map {0+$_} @output), "6 7 8 3 4 5 0 1 2", 'stable $b cmp $a sort'; |
eb209983 NC |
545 | |
546 | @input = &generate; | |
547 | @input = sort {$b cmp $a} @input; | |
5823dc9f | 548 | is join(" ", map {0+$_} @input), "6 7 8 3 4 5 0 1 2", |
eb209983 NC |
549 | 'stable $b cmp $a in place sort'; |
550 | ||
75dd5fa4 | 551 | @input = &generate; |
eb209983 | 552 | @output = reverse sort @input; |
5823dc9f | 553 | is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", "Reversed stable sort"; |
eb209983 NC |
554 | |
555 | @input = &generate; | |
556 | @input = reverse sort @input; | |
5823dc9f | 557 | is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0", |
eb209983 NC |
558 | "Reversed stable in place sort"; |
559 | ||
75dd5fa4 NC |
560 | @input = &generate; |
561 | my $output = reverse sort @input; | |
5823dc9f | 562 | is $output, "CCCBBBAAA", "Reversed stable sort in scalar context"; |
75dd5fa4 | 563 | |
eb209983 NC |
564 | |
565 | @input = &generate; | |
566 | @output = reverse sort {$a cmp $b} @input; | |
5823dc9f | 567 | is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", |
eb209983 NC |
568 | 'reversed stable $a cmp $b sort'; |
569 | ||
570 | @input = &generate; | |
571 | @input = reverse sort {$a cmp $b} @input; | |
5823dc9f | 572 | is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0", |
eb209983 NC |
573 | 'revesed stable $a cmp $b in place sort'; |
574 | ||
575 | @input = &generate; | |
7e7a548e | 576 | $output = reverse sort {$a cmp $b} @input; |
5823dc9f | 577 | is $output, "CCCBBBAAA", 'Reversed stable $a cmp $b sort in scalar context'; |
75dd5fa4 NC |
578 | |
579 | @input = &generate; | |
eb209983 | 580 | @output = reverse sort {$b cmp $a} @input; |
5823dc9f | 581 | is join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6", |
eb209983 NC |
582 | 'reversed stable $b cmp $a sort'; |
583 | ||
584 | @input = &generate; | |
585 | @input = reverse sort {$b cmp $a} @input; | |
5823dc9f | 586 | is join(" ", map {0+$_} @input), "2 1 0 5 4 3 8 7 6", |
eb209983 NC |
587 | 'revesed stable $b cmp $a in place sort'; |
588 | ||
75dd5fa4 NC |
589 | @input = &generate; |
590 | $output = reverse sort {$b cmp $a} @input; | |
5823dc9f | 591 | is $output, "AAABBBCCC", 'Reversed stable $b cmp $a sort in scalar context'; |
75dd5fa4 | 592 | |
7e7a548e NC |
593 | sub stuff { |
594 | # Something complex enough to defeat any constant folding optimiser | |
595 | $$ - $$; | |
596 | } | |
597 | ||
598 | @input = &generate; | |
599 | @output = reverse sort {stuff || $a cmp $b} @input; | |
5823dc9f | 600 | is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", |
7e7a548e NC |
601 | 'reversed stable complex sort'; |
602 | ||
603 | @input = &generate; | |
604 | @input = reverse sort {stuff || $a cmp $b} @input; | |
5823dc9f | 605 | is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0", |
7e7a548e NC |
606 | 'revesed stable complex in place sort'; |
607 | ||
608 | @input = &generate; | |
609 | $output = reverse sort {stuff || $a cmp $b } @input; | |
5823dc9f | 610 | is $output, "CCCBBBAAA", 'Reversed stable complex sort in scalar context'; |
7e7a548e | 611 | |
a1824f2a NC |
612 | sub sortr { |
613 | reverse sort @_; | |
614 | } | |
615 | ||
616 | @output = sortr &generate; | |
5823dc9f | 617 | is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", |
a1824f2a NC |
618 | 'reversed stable sort return list context'; |
619 | $output = sortr &generate; | |
5823dc9f | 620 | is $output, "CCCBBBAAA", |
a1824f2a NC |
621 | 'reversed stable sort return scalar context'; |
622 | ||
623 | sub sortcmpr { | |
624 | reverse sort {$a cmp $b} @_; | |
625 | } | |
626 | ||
627 | @output = sortcmpr &generate; | |
5823dc9f | 628 | is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", |
a1824f2a NC |
629 | 'reversed stable $a cmp $b sort return list context'; |
630 | $output = sortcmpr &generate; | |
5823dc9f | 631 | is $output, "CCCBBBAAA", |
a1824f2a NC |
632 | 'reversed stable $a cmp $b sort return scalar context'; |
633 | ||
634 | sub sortcmprba { | |
635 | reverse sort {$b cmp $a} @_; | |
636 | } | |
637 | ||
638 | @output = sortcmprba &generate; | |
5823dc9f | 639 | is join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6", |
a1824f2a NC |
640 | 'reversed stable $b cmp $a sort return list context'; |
641 | $output = sortcmprba &generate; | |
5823dc9f | 642 | is $output, "AAABBBCCC", |
a1824f2a | 643 | 'reversed stable $b cmp $a sort return scalar context'; |
eb209983 | 644 | |
7e7a548e NC |
645 | sub sortcmprq { |
646 | reverse sort {stuff || $a cmp $b} @_; | |
647 | } | |
648 | ||
649 | @output = sortcmpr &generate; | |
5823dc9f | 650 | is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", |
7e7a548e NC |
651 | 'reversed stable complex sort return list context'; |
652 | $output = sortcmpr &generate; | |
5823dc9f | 653 | is $output, "CCCBBBAAA", |
7e7a548e NC |
654 | 'reversed stable complex sort return scalar context'; |
655 | ||
eb209983 NC |
656 | # And now with numbers |
657 | ||
658 | sub generate1 { | |
659 | my $count = 'A'; | |
660 | map {new Oscalar $count++, $_} 0, 0, 0, 1, 1, 1, 2, 2, 2; | |
661 | } | |
662 | ||
663 | # This won't be very interesting | |
664 | @input = &generate1; | |
665 | @output = sort {$a cmp $b} @input; | |
5823dc9f | 666 | is "@output", "A B C D E F G H I", 'stable $a cmp $b sort'; |
eb209983 NC |
667 | |
668 | @input = &generate1; | |
669 | @output = sort {$a <=> $b} @input; | |
5823dc9f | 670 | is "@output", "A B C D E F G H I", 'stable $a <=> $b sort'; |
eb209983 NC |
671 | |
672 | @input = &generate1; | |
673 | @input = sort {$a <=> $b} @input; | |
5823dc9f | 674 | is "@input", "A B C D E F G H I", 'stable $a <=> $b in place sort'; |
eb209983 NC |
675 | |
676 | @input = &generate1; | |
677 | @output = sort {$b <=> $a} @input; | |
5823dc9f | 678 | is "@output", "G H I D E F A B C", 'stable $b <=> $a sort'; |
eb209983 NC |
679 | |
680 | @input = &generate1; | |
681 | @input = sort {$b <=> $a} @input; | |
5823dc9f | 682 | is "@input", "G H I D E F A B C", 'stable $b <=> $a in place sort'; |
eb209983 | 683 | |
59608b94 DN |
684 | # test that optimized {$b cmp $a} and {$b <=> $a} remain stable |
685 | # (new in 5.9) without overloading | |
686 | { no warnings; | |
687 | @b = sort { $b <=> $a } @input = qw/5first 6first 5second 6second/; | |
5823dc9f | 688 | is "@b" , "6first 6second 5first 5second", "optimized {$b <=> $a} without overloading" ; |
59608b94 | 689 | @input = sort {$b <=> $a} @input; |
5823dc9f | 690 | is "@input" , "6first 6second 5first 5second","inline optimized {$b <=> $a} without overloading" ; |
59608b94 DN |
691 | }; |
692 | ||
eb209983 | 693 | # These two are actually doing string cmp on 0 1 and 2 |
75dd5fa4 | 694 | @input = &generate1; |
eb209983 | 695 | @output = reverse sort @input; |
5823dc9f | 696 | is "@output", "I H G F E D C B A", "Reversed stable sort"; |
eb209983 NC |
697 | |
698 | @input = &generate1; | |
699 | @input = reverse sort @input; | |
5823dc9f | 700 | is "@input", "I H G F E D C B A", "Reversed stable in place sort"; |
eb209983 NC |
701 | |
702 | @input = &generate1; | |
75dd5fa4 | 703 | $output = reverse sort @input; |
5823dc9f | 704 | is $output, "IHGFEDCBA", "Reversed stable sort in scalar context"; |
75dd5fa4 NC |
705 | |
706 | @input = &generate1; | |
eb209983 | 707 | @output = reverse sort {$a <=> $b} @input; |
5823dc9f | 708 | is "@output", "I H G F E D C B A", 'reversed stable $a <=> $b sort'; |
eb209983 NC |
709 | |
710 | @input = &generate1; | |
711 | @input = reverse sort {$a <=> $b} @input; | |
5823dc9f | 712 | is "@input", "I H G F E D C B A", 'revesed stable $a <=> $b in place sort'; |
fe1bc4cf | 713 | |
eb209983 | 714 | @input = &generate1; |
75dd5fa4 | 715 | $output = reverse sort {$a <=> $b} @input; |
5823dc9f | 716 | is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort in scalar context'; |
75dd5fa4 NC |
717 | |
718 | @input = &generate1; | |
eb209983 | 719 | @output = reverse sort {$b <=> $a} @input; |
5823dc9f | 720 | is "@output", "C B A F E D I H G", 'reversed stable $b <=> $a sort'; |
fe1bc4cf | 721 | |
eb209983 NC |
722 | @input = &generate1; |
723 | @input = reverse sort {$b <=> $a} @input; | |
5823dc9f | 724 | is "@input", "C B A F E D I H G", 'revesed stable $b <=> $a in place sort'; |
75dd5fa4 NC |
725 | |
726 | @input = &generate1; | |
727 | $output = reverse sort {$b <=> $a} @input; | |
5823dc9f | 728 | is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort in scalar context'; |
a1824f2a | 729 | |
7e7a548e NC |
730 | @input = &generate1; |
731 | @output = reverse sort {stuff || $a <=> $b} @input; | |
5823dc9f | 732 | is "@output", "I H G F E D C B A", 'reversed stable complex sort'; |
7e7a548e NC |
733 | |
734 | @input = &generate1; | |
735 | @input = reverse sort {stuff || $a <=> $b} @input; | |
5823dc9f | 736 | is "@input", "I H G F E D C B A", 'revesed stable complex in place sort'; |
7e7a548e NC |
737 | |
738 | @input = &generate1; | |
739 | $output = reverse sort {stuff || $a <=> $b} @input; | |
5823dc9f | 740 | is $output, "IHGFEDCBA", 'reversed stable complex sort in scalar context'; |
a1824f2a NC |
741 | |
742 | sub sortnumr { | |
743 | reverse sort {$a <=> $b} @_; | |
744 | } | |
745 | ||
746 | @output = sortnumr &generate1; | |
5823dc9f | 747 | is "@output", "I H G F E D C B A", |
a1824f2a NC |
748 | 'reversed stable $a <=> $b sort return list context'; |
749 | $output = sortnumr &generate1; | |
5823dc9f | 750 | is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort return scalar context'; |
a1824f2a NC |
751 | |
752 | sub sortnumrba { | |
753 | reverse sort {$b <=> $a} @_; | |
754 | } | |
755 | ||
756 | @output = sortnumrba &generate1; | |
5823dc9f | 757 | is "@output", "C B A F E D I H G", |
a1824f2a NC |
758 | 'reversed stable $b <=> $a sort return list context'; |
759 | $output = sortnumrba &generate1; | |
5823dc9f | 760 | is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort return scalar context'; |
7e7a548e NC |
761 | |
762 | sub sortnumrq { | |
763 | reverse sort {stuff || $a <=> $b} @_; | |
764 | } | |
765 | ||
766 | @output = sortnumrq &generate1; | |
5823dc9f | 767 | is "@output", "I H G F E D C B A", |
7e7a548e NC |
768 | 'reversed stable complex sort return list context'; |
769 | $output = sortnumrq &generate1; | |
5823dc9f | 770 | is $output, "IHGFEDCBA", 'reversed stable complex sort return scalar context'; |
c093edd0 NC |
771 | |
772 | @output = reverse (sort(qw(C A B)), 0); | |
5823dc9f | 773 | is "@output", "0 C B A", 'reversed sort with trailing argument'; |
c093edd0 NC |
774 | |
775 | @output = reverse (0, sort(qw(C A B))); | |
5823dc9f | 776 | is "@output", "C B A 0", 'reversed sort with leading argument'; |
9850bf21 RH |
777 | |
778 | eval { @output = sort {goto sub {}} 1,2; }; | |
f34362ee | 779 | $fail_msg = q(Can't goto subroutine outside a subroutine); |
5823dc9f | 780 | cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr outside subr'); |
f34362ee DL |
781 | |
782 | ||
9850bf21 RH |
783 | |
784 | sub goto_sub {goto sub{}} | |
785 | eval { @output = sort goto_sub 1,2; }; | |
f34362ee | 786 | $fail_msg = q(Can't goto subroutine from a sort sub); |
5823dc9f | 787 | cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr from a sort sub'); |
f34362ee DL |
788 | |
789 | ||
9850bf21 RH |
790 | |
791 | eval { @output = sort {goto label} 1,2; }; | |
f34362ee | 792 | $fail_msg = q(Can't "goto" out of a pseudo block); |
5823dc9f | 793 | cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 1'); |
f34362ee DL |
794 | |
795 | ||
9850bf21 RH |
796 | |
797 | sub goto_label {goto label} | |
798 | label: eval { @output = sort goto_label 1,2; }; | |
f34362ee | 799 | $fail_msg = q(Can't "goto" out of a pseudo block); |
5823dc9f | 800 | cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 2'); |
f34362ee DL |
801 | |
802 | ||
9850bf21 RH |
803 | |
804 | sub self_immolate {undef &self_immolate; $a<=>$b} | |
805 | eval { @output = sort self_immolate 1,2,3 }; | |
f34362ee | 806 | $fail_msg = q(Can't undef active subroutine); |
5823dc9f | 807 | cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'undef active subr'); |
f34362ee DL |
808 | |
809 | ||
8a27a13e FC |
810 | for(1,2) # We run this twice, to make sure sort does not lower the ref |
811 | { # count. See bug 71076. | |
9850bf21 RH |
812 | my $failed = 0; |
813 | ||
814 | sub rec { | |
815 | my $n = shift; | |
816 | if (!defined($n)) { # No arg means we're being called by sort() | |
817 | return 1; | |
818 | } | |
819 | if ($n<5) { rec($n+1); } | |
820 | else { () = sort rec 1,2; } | |
821 | ||
822 | $failed = 1 if !defined $n; | |
823 | } | |
824 | ||
825 | rec(1); | |
5823dc9f | 826 | ok(!$failed, "sort from active sub"); |
9850bf21 RH |
827 | } |
828 | ||
829 | # $a and $b are set in the package the sort() is called from, | |
830 | # *not* the package the sort sub is in. This is longstanding | |
831 | # de facto behaviour that shouldn't be broken. | |
f34362ee | 832 | my $answer = "good"; |
9850bf21 RH |
833 | () = sort OtherPack::foo 1,2,3,4; |
834 | ||
9da9462b RGS |
835 | { |
836 | package OtherPack; | |
837 | no warnings 'once'; | |
838 | sub foo { | |
f34362ee | 839 | $answer = "something was unexpectedly defined or undefined" if |
9da9462b RGS |
840 | defined($a) || defined($b) || !defined($main::a) || !defined($main::b); |
841 | $main::a <=> $main::b; | |
842 | } | |
843 | } | |
9850bf21 | 844 | |
5823dc9f | 845 | cmp_ok($answer,'eq','good','sort subr called from other package'); |
9850bf21 RH |
846 | |
847 | ||
848 | # Bug 36430 - sort called in package2 while a | |
849 | # sort in package1 is active should set $package2::a/b. | |
5823dc9f MS |
850 | { |
851 | my $answer = "good"; | |
852 | my @list = sort { A::min(@$a) <=> A::min(@$b) } | |
853 | [3, 1, 5], [2, 4], [0]; | |
854 | ||
855 | cmp_ok($answer,'eq','good','bug 36430'); | |
856 | ||
857 | package A; | |
858 | sub min { | |
859 | my @list = sort { | |
860 | $answer = '$a and/or $b are not defined ' if !defined($a) || !defined($b); | |
861 | $a <=> $b; | |
862 | } @_; | |
863 | $list[0]; | |
864 | } | |
9850bf21 RH |
865 | } |
866 | ||
5823dc9f | 867 | |
f34362ee | 868 | |
a5f48505 DM |
869 | # I commented out this TODO test because messing with FREEd scalars on the |
870 | # stack can have all sorts of strange side-effects, not made safe by eval | |
871 | # - DAPM. | |
872 | # | |
873 | #{ | |
874 | # local $TODO = "sort should make sure elements are not freed in the sort block"; | |
875 | # eval { @nomodify_x=(1..8); | |
876 | # our @copy = sort { undef @nomodify_x; 1 } (@nomodify_x, 3); }; | |
877 | # is($@, ""); | |
878 | #} | |
84df657f | 879 | |
9850bf21 RH |
880 | |
881 | # Sorting shouldn't increase the refcount of a sub | |
5823dc9f | 882 | { |
7f4622ec RGS |
883 | sub sportello {(1+$a) <=> (1+$b)} |
884 | my $refcnt = &Internals::SvREFCNT(\&sportello); | |
885 | @output = sort sportello 3,7,9; | |
5823dc9f MS |
886 | |
887 | { | |
7f4622ec RGS |
888 | package Doc; |
889 | ::is($refcnt, &Internals::SvREFCNT(\&::sportello), "sort sub refcnt"); | |
5823dc9f MS |
890 | $fail_msg = q(Modification of a read-only value attempted); |
891 | # Sorting a read-only array in-place shouldn't be allowed | |
892 | my @readonly = (1..10); | |
893 | Internals::SvREADONLY(@readonly, 1); | |
894 | eval { @readonly = sort @readonly; }; | |
895 | ::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'in-place sort of read-only array'); | |
896 | } | |
897 | } | |
f34362ee | 898 | |
9850bf21 RH |
899 | |
900 | # Using return() should be okay even in a deeper context | |
901 | @b = sort {while (1) {return ($a <=> $b)} } 1..10; | |
5823dc9f | 902 | is("@b", "1 2 3 4 5 6 7 8 9 10", "return within loop"); |
d7507f74 RH |
903 | |
904 | # Using return() should be okay even if there are other items | |
905 | # on the stack at the time. | |
906 | @b = sort {$_ = ($a<=>$b) + do{return $b<=> $a}} 1..10; | |
5823dc9f | 907 | is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack"); |
d7507f74 RH |
908 | |
909 | # As above, but with a sort sub rather than a sort block. | |
910 | sub ret_with_stacked { $_ = ($a<=>$b) + do {return $b <=> $a} } | |
911 | @b = sort ret_with_stacked 1..10; | |
5823dc9f | 912 | is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack"); |
93e19c0f Z |
913 | |
914 | # Comparison code should be able to give result in non-integer representation. | |
915 | sub cmp_as_string($$) { $_[0] < $_[1] ? "-1" : $_[0] == $_[1] ? "0" : "+1" } | |
916 | @b = sort { cmp_as_string($a, $b) } (1,5,4,7,3,2,3); | |
917 | is("@b", "1 2 3 3 4 5 7", "comparison result as string"); | |
918 | @b = sort cmp_as_string (1,5,4,7,3,2,3); | |
919 | is("@b", "1 2 3 3 4 5 7", "comparison result as string"); | |
bdbefedf DM |
920 | |
921 | # RT #34604: sort didn't honour overloading if the overloaded elements | |
922 | # were retrieved via tie | |
923 | ||
924 | { | |
925 | package RT34604; | |
926 | ||
927 | sub TIEHASH { bless { | |
928 | p => bless({ val => 2 }), | |
929 | q => bless({ val => 1 }), | |
930 | } | |
931 | } | |
932 | sub FETCH { $_[0]{$_[1] } } | |
933 | ||
934 | my $cc = 0; | |
935 | sub compare { $cc++; $_[0]{val} cmp $_[1]{val} } | |
936 | my $cs = 0; | |
937 | sub str { $cs++; $_[0]{val} } | |
938 | ||
939 | use overload 'cmp' => \&compare, '""' => \&str; | |
940 | ||
941 | package main; | |
942 | ||
943 | tie my %h, 'RT34604'; | |
944 | my @sorted = sort @h{qw(p q)}; | |
945 | is($cc, 1, 'overload compare called once'); | |
946 | is("@sorted","1 2", 'overload sort result'); | |
947 | is($cs, 2, 'overload string called twice'); | |
948 | } | |
8f443ca6 | 949 | |
f3106bc8 | 950 | fresh_perl_is('sub w ($$) {my ($l, $r) = @_; my $v = \@_; undef @_; $l <=> $r}; print join q{ }, sort w 3, 1, 2, 0', |
8f443ca6 GG |
951 | '0 1 2 3', |
952 | {stderr => 1, switches => ['-w']}, | |
953 | 'RT #72334'); | |
954 | ||
f3106bc8 | 955 | fresh_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 |
956 | '0 1 2 3', |
957 | {stderr => 1, switches => ['-w']}, | |
958 | 'RT #72334'); | |
133acf7b NC |
959 | |
960 | { | |
961 | my $count = 0; | |
962 | { | |
963 | package Counter; | |
964 | ||
965 | sub new { | |
966 | ++$count; | |
967 | bless []; | |
968 | } | |
969 | ||
970 | sub DESTROY { | |
971 | --$count; | |
972 | } | |
973 | } | |
974 | ||
975 | sub sorter ($$) { | |
976 | my ($l, $r) = @_; | |
977 | my $q = \@_; | |
978 | $l <=> $r; | |
979 | } | |
980 | ||
981 | is($count, 0, 'None before we start'); | |
982 | my @a = map { Counter->new() } 0..1; | |
983 | is($count, 2, '2 here'); | |
984 | ||
985 | my @b = sort sorter @a; | |
986 | ||
987 | is(scalar @b, 2); | |
988 | cmp_ok($b[0], '<', $b[1], 'sorted!'); | |
989 | ||
990 | is($count, 2, 'still the same 2 here'); | |
991 | ||
992 | @a = (); @b = (); | |
993 | ||
994 | is($count, 0, 'all gone'); | |
995 | } | |
8e7d0c4b FC |
996 | |
997 | # [perl #77930] The context stack may be reallocated during a sort, as a | |
998 | # result of deeply-nested (or not-so-deeply-nested) calls | |
999 | # from a custom sort subroutine. | |
1000 | fresh_perl_is | |
1001 | ' | |
1002 | $sub = sub { | |
1003 | local $count = $count+1; | |
1004 | ()->$sub if $count < 1000; | |
1005 | $a cmp $b | |
1006 | }; | |
1007 | () = sort $sub qw<a b c d e f g>; | |
1008 | print "ok" | |
1009 | ', | |
1010 | 'ok', | |
1011 | {}, | |
92c404cd | 1012 | '[perl #77930] cx_stack reallocation during sort' |
8e7d0c4b | 1013 | ; |
ad021bfb FC |
1014 | |
1015 | # [perl #76026] | |
1016 | # Match vars should not leak from one sort sub call to the next | |
1017 | { | |
1018 | my $output = ''; | |
1019 | sub soarter { | |
1020 | $output .= $1; | |
1021 | "Leakage" =~ /(.*)/; | |
1022 | 1 | |
1023 | } | |
1024 | sub soarterdd($$) { | |
1025 | $output .= $1; | |
1026 | "Leakage" =~ /(.*)/; | |
1027 | 1 | |
1028 | } | |
1029 | ||
1030 | "Win" =~ /(.*)/; | |
1031 | my @b = sort soarter 0..2; | |
1032 | ||
1033 | like $output, qr/^(?:Win)+\z/, | |
1034 | "Match vars do not leak from one plain sort sub to the next"; | |
1035 | ||
1036 | $output = ''; | |
1037 | ||
1038 | "Win" =~ /(.*)/; | |
1039 | @b = sort soarterdd 0..2; | |
1040 | ||
1041 | like $output, qr/^(?:Win)+\z/, | |
1042 | 'Match vars do not leak from one $$ sort sub to the next'; | |
1043 | } | |
f7bc00ea FC |
1044 | |
1045 | # [perl #30661] autoloading | |
1046 | AUTOLOAD { $b <=> $a } | |
1047 | sub stubbedsub; | |
1048 | is join("", sort stubbedsub split//, '04381091'), '98431100', | |
1049 | 'stubborn AUTOLOAD'; | |
1050 | is join("", sort hopefullynonexistent split//, '04381091'), '98431100', | |
1051 | 'AUTOLOAD without stub'; | |
1052 | my $stubref = \&givemeastub; | |
1053 | is join("", sort $stubref split//, '04381091'), '98431100', | |
1054 | 'AUTOLOAD with stubref'; | |
a46b39a8 FC |
1055 | |
1056 | # [perl #90030] sort without arguments | |
5d4b936e | 1057 | eval '@x = (sort); 1'; |
a46b39a8 FC |
1058 | is $@, '', '(sort) does not die'; |
1059 | is @x, 0, '(sort) returns empty list'; | |
5d4b936e | 1060 | eval '@x = sort; 1'; |
a46b39a8 FC |
1061 | is $@, '', 'sort; does not die'; |
1062 | is @x, 0, 'sort; returns empty list'; | |
1063 | eval '{@x = sort} 1'; | |
1064 | is $@, '', '{sort} does not die'; | |
1065 | is @x, 0, '{sort} returns empty list'; | |
a7fd8ef6 DM |
1066 | |
1067 | # this happened while the padrange op was being added. Sort blocks | |
1068 | # are executed in void context, and the padrange op was skipping pushing | |
1069 | # the item in void cx. The net result was that the return value was | |
1070 | # whatever was on the stack last. | |
1071 | ||
1072 | { | |
1073 | my @a = sort { | |
1074 | my $r = $a <=> $b; | |
1075 | if ($r) { | |
1076 | undef; # this got returned by mistake | |
1077 | return $r | |
1078 | } | |
1079 | return 0; | |
1080 | } 5,1,3,6,0; | |
1081 | is "@a", "0 1 3 5 6", "padrange and void context"; | |
1082 | } | |
2f43ddf1 FC |
1083 | |
1084 | # Fatal warnings an sort sub returning a non-number | |
1085 | # We need two evals, because the panic used to happen on scope exit. | |
1086 | eval { eval { use warnings FATAL => 'all'; () = sort { undef } 1,2 } }; | |
1087 | is $@, "", | |
1088 | 'no panic/crash with fatal warnings when sort sub returns undef'; | |
1089 | eval { eval { use warnings FATAL => 'all'; () = sort { "no thin" } 1,2 } }; | |
1090 | is $@, "", | |
1091 | 'no panic/crash with fatal warnings when sort sub returns string'; | |
1092 | sub notdef($$) { undef } | |
1093 | eval { eval { use warnings FATAL => 'all'; () = sort notdef 1,2 } }; | |
1094 | is $@, "", | |
1095 | 'no panic/crash with fatal warnings when sort sub($$) returns undef'; | |
1096 | sub yarn($$) { "no thinking aloud" } | |
1097 | eval { eval { use warnings FATAL => 'all'; () = sort yarn 1,2 } }; | |
1098 | is $@, "", | |
1099 | 'no panic/crash with fatal warnings when sort sub($$) returns string'; | |
f65493df FC |
1100 | |
1101 | $#a = -1; | |
1102 | () = [sort { $a = 10; $b = 10; 0 } $#a, $#a]; | |
1103 | is $#a, 10, 'sort block modifying $a and $b'; | |
2d885586 | 1104 | |
2d885586 FC |
1105 | () = sort { |
1106 | is \$a, \$a, '[perl #78194] op return values passed to sort'; 0 | |
1107 | } "${\''}", "${\''}"; | |
8465ba45 FC |
1108 | |
1109 | package deletions { | |
1110 | @_=sort { delete $deletions::{a}; delete $deletions::{b}; 3 } 1..3; | |
1111 | } | |
1112 | pass "no crash when sort block deletes *a and *b"; | |
33411212 DM |
1113 | |
1114 | # make sure return args are always evaluated in scalar context | |
1115 | ||
1116 | { | |
1117 | package Ret; | |
1118 | no warnings 'void'; | |
1119 | sub f0 { } | |
1120 | sub f1 { $b <=> $a, $a <=> $b } | |
1121 | sub f2 { return ($b <=> $a, $a <=> $b) } | |
1122 | sub f3 { for ($b <=> $a) { return ($b <=> $a, $a <=> $b) } } | |
1123 | ||
1124 | { | |
1125 | no warnings 'uninitialized'; | |
1126 | ::is (join('-', sort { () } 3,1,2,4), '3-1-2-4', "Ret: null blk"); | |
1127 | } | |
1128 | ::is (join('-', sort { $b <=> $a, $a <=> $b } 3,1,2,4), '1-2-3-4', "Ret: blk"); | |
1129 | ::is (join('-', sort { for($b <=> $a) { return ($b <=> $a, $a <=> $b) } } | |
1130 | 3,1,2,4), '1-2-3-4', "Ret: blk ret"); | |
1131 | { | |
1132 | no warnings 'uninitialized'; | |
1133 | ::is (join('-', sort f0 3,1,2,4), '3-1-2-4', "Ret: f0"); | |
1134 | } | |
1135 | ::is (join('-', sort f1 3,1,2,4), '1-2-3-4', "Ret: f1"); | |
1136 | ::is (join('-', sort f2 3,1,2,4), '1-2-3-4', "Ret: f2"); | |
1137 | ::is (join('-', sort f3 3,1,2,4), '1-2-3-4', "Ret: f3"); | |
1138 | } | |
dc9ef998 TC |
1139 | |
1140 | { | |
1141 | @a = sort{ *a=0; 1} 0..1; | |
1142 | pass "No crash when GP deleted out from under us [perl 124097]"; | |
1143 | ||
1144 | no warnings 'redefine'; | |
1145 | # some alternative non-solutions localized modifications to *a and *b | |
1146 | sub a { 0 }; | |
1147 | @a = sort { *a = sub { 1 }; $a <=> $b } 0 .. 1; | |
1148 | ok(a(), "*a wasn't localized inadvertantly"); | |
1149 | } |