Commit | Line | Data |
---|---|---|
a687059c LW |
1 | #!./perl |
2 | ||
9c007264 JH |
3 | BEGIN { |
4 | chdir 't' if -d 't'; | |
20822f61 | 5 | @INC = '../lib'; |
9c007264 | 6 | } |
9f1b1f2d | 7 | use warnings; |
d7507f74 | 8 | print "1..143\n"; |
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 JH |
46 | $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; |
47 | print "# 1: x = '$x', expected = '$expected'\n"; | |
48 | print ($x eq $expected ? "ok 1\n" : "not ok 1\n"); | |
a687059c | 49 | |
9f1b1f2d | 50 | $x = join('', sort( Backwards @harry)); |
9d116dd7 JH |
51 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
52 | print "# 2: x = '$x', expected = '$expected'\n"; | |
53 | print ($x eq $expected ? "ok 2\n" : "not ok 2\n"); | |
a687059c | 54 | |
9f1b1f2d | 55 | $x = join('', sort( Backwards_stacked @harry)); |
43481408 GS |
56 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
57 | print "# 3: x = '$x', expected = '$expected'\n"; | |
58 | print ($x eq $expected ? "ok 3\n" : "not ok 3\n"); | |
59 | ||
a687059c | 60 | $x = join('', sort @george, 'to', @harry); |
9d116dd7 JH |
61 | $expected = $upperfirst ? |
62 | 'AbelAxedCaincatchaseddoggonepunishedtoxyz' : | |
63 | 'catchaseddoggonepunishedtoxyzAbelAxedCain' ; | |
43481408 GS |
64 | print "# 4: x = '$x', expected = '$expected'\n"; |
65 | print ($x eq $expected ?"ok 4\n":"not ok 4\n"); | |
03a14243 LW |
66 | |
67 | @a = (); | |
68 | @b = reverse @a; | |
43481408 | 69 | print ("@b" eq "" ? "ok 5\n" : "not ok 5 (@b)\n"); |
03a14243 LW |
70 | |
71 | @a = (1); | |
72 | @b = reverse @a; | |
43481408 | 73 | print ("@b" eq "1" ? "ok 6\n" : "not ok 6 (@b)\n"); |
03a14243 LW |
74 | |
75 | @a = (1,2); | |
76 | @b = reverse @a; | |
43481408 | 77 | print ("@b" eq "2 1" ? "ok 7\n" : "not ok 7 (@b)\n"); |
03a14243 LW |
78 | |
79 | @a = (1,2,3); | |
80 | @b = reverse @a; | |
43481408 | 81 | print ("@b" eq "3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n"); |
03a14243 LW |
82 | |
83 | @a = (1,2,3,4); | |
84 | @b = reverse @a; | |
43481408 | 85 | print ("@b" eq "4 3 2 1" ? "ok 9\n" : "not ok 9 (@b)\n"); |
55204971 LW |
86 | |
87 | @a = (10,2,3,4); | |
88 | @b = sort {$a <=> $b;} @a; | |
43481408 | 89 | print ("@b" eq "2 3 4 10" ? "ok 10\n" : "not ok 10 (@b)\n"); |
988174c1 | 90 | |
9f1b1f2d | 91 | $sub = 'Backwards'; |
988174c1 | 92 | $x = join('', sort $sub @harry); |
9d116dd7 | 93 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
43481408 GS |
94 | print "# 11: x = $x, expected = '$expected'\n"; |
95 | print ($x eq $expected ? "ok 11\n" : "not ok 11\n"); | |
96 | ||
9f1b1f2d | 97 | $sub = 'Backwards_stacked'; |
43481408 GS |
98 | $x = join('', sort $sub @harry); |
99 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; | |
100 | print "# 12: x = $x, expected = '$expected'\n"; | |
101 | print ($x eq $expected ? "ok 12\n" : "not ok 12\n"); | |
988174c1 | 102 | |
cd5de442 GS |
103 | # literals, combinations |
104 | ||
105 | @b = sort (4,1,3,2); | |
43481408 | 106 | print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n"); |
cd5de442 GS |
107 | print "# x = '@b'\n"; |
108 | ||
109 | @b = sort grep { $_ } (4,1,3,2); | |
43481408 | 110 | print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n"); |
cd5de442 GS |
111 | print "# x = '@b'\n"; |
112 | ||
113 | @b = sort map { $_ } (4,1,3,2); | |
43481408 | 114 | print ("@b" eq '1 2 3 4' ? "ok 15\n" : "not ok 15\n"); |
cd5de442 GS |
115 | print "# x = '@b'\n"; |
116 | ||
117 | @b = sort reverse (4,1,3,2); | |
43481408 | 118 | print ("@b" eq '1 2 3 4' ? "ok 16\n" : "not ok 16\n"); |
cd5de442 | 119 | print "# x = '@b'\n"; |
7bac28a0 | 120 | |
9850bf21 RH |
121 | # redefining sort sub inside the sort sub should not fail |
122 | sub twoface { no warnings 'redefine'; *twoface = sub { $a <=> $b }; &twoface } | |
7bac28a0 | 123 | eval { @b = sort twoface 4,1,3,2 }; |
9850bf21 | 124 | print ($@ eq '' ? "ok 17\n" : "not ok 17\n"); |
7bac28a0 | 125 | |
9850bf21 | 126 | # redefining sort subs outside the sort should also not fail |
9f1b1f2d | 127 | eval { no warnings 'redefine'; *twoface = sub { &Backwards } }; |
43481408 | 128 | print $@ ? "not ok 18\n" : "ok 18\n"; |
7bac28a0 | 129 | |
130 | eval { @b = sort twoface 4,1,3,2 }; | |
43481408 | 131 | print ("@b" eq '4 3 2 1' ? "ok 19\n" : "not ok 19 |@b|\n"); |
7bac28a0 | 132 | |
9f1b1f2d GS |
133 | { |
134 | no warnings 'redefine'; | |
9850bf21 | 135 | *twoface = sub { *twoface = *Backwards_other; $a <=> $b }; |
9f1b1f2d | 136 | } |
9850bf21 RH |
137 | # The redefinition should not take effect during the sort |
138 | eval { @b = sort twoface 4,1,9,5 }; | |
139 | print (($@ eq "" && "@b" eq "1 4 5 9") ? "ok 20\n" : "not ok 20 # $@|@b\n"); | |
7bac28a0 | 140 | |
9f1b1f2d GS |
141 | { |
142 | no warnings 'redefine'; | |
143 | *twoface = sub { | |
7bac28a0 | 144 | eval 'sub twoface { $a <=> $b }'; |
9850bf21 | 145 | die($@ eq "" ? "ok 21\n" : "not ok 21\n"); |
7bac28a0 | 146 | $a <=> $b; |
147 | }; | |
9f1b1f2d | 148 | } |
7bac28a0 | 149 | eval { @b = sort twoface 4,1 }; |
9850bf21 | 150 | print($@ ? "$@" : "not ok 21 # $@\n"); |
15f0808c GS |
151 | |
152 | eval <<'CODE'; | |
9f1b1f2d | 153 | my @result = sort main'Backwards 'one', 'two'; |
15f0808c | 154 | CODE |
43481408 | 155 | print $@ ? "not ok 22\n# $@" : "ok 22\n"; |
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 | |
43481408 | 161 | print $@ ? "not ok 23\n# $@" : "ok 23\n"; |
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; |
43481408 | 169 | print ("@b" eq '4 3 2 1' ? "ok 24\n" : "not ok 24 |@b|\n"); |
c6e96bcb | 170 | @b = sort $sortglob 4,1,3,2; |
43481408 | 171 | print ("@b" eq '4 3 2 1' ? "ok 25\n" : "not ok 25 |@b|\n"); |
c6e96bcb | 172 | @b = sort $sortname 4,1,3,2; |
43481408 | 173 | print ("@b" eq '4 3 2 1' ? "ok 26\n" : "not ok 26 |@b|\n"); |
62f274bf | 174 | @b = sort $sortglobr 4,1,3,2; |
43481408 GS |
175 | print ("@b" eq '4 3 2 1' ? "ok 27\n" : "not ok 27 |@b|\n"); |
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 GS |
183 | @b = sort $sortsub 4,1,3,2; |
184 | print ("@b" eq '4 3 2 1' ? "ok 28\n" : "not ok 28 |@b|\n"); | |
185 | @b = sort $sortglob 4,1,3,2; | |
186 | print ("@b" eq '4 3 2 1' ? "ok 29\n" : "not ok 29 |@b|\n"); | |
187 | @b = sort $sortname 4,1,3,2; | |
188 | print ("@b" eq '4 3 2 1' ? "ok 30\n" : "not ok 30 |@b|\n"); | |
189 | @b = sort $sortglobr 4,1,3,2; | |
190 | print ("@b" eq '4 3 2 1' ? "ok 31\n" : "not ok 31 |@b|\n"); | |
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; |
43481408 | 199 | print ("@b" eq '4 3 2 1' ? "ok 32\n" : "not ok 32 |@b|\n"); |
c6e96bcb | 200 | @b = sort $sortglob 4,1,3,2; |
43481408 | 201 | print ("@b" eq '4 3 2 1' ? "ok 33\n" : "not ok 33 |@b|\n"); |
c6e96bcb | 202 | @b = sort $sortname 4,1,3,2; |
43481408 | 203 | print ("@b" eq '4 3 2 1' ? "ok 34\n" : "not ok 34 |@b|\n"); |
62f274bf | 204 | @b = sort $sortglobr 4,1,3,2; |
43481408 GS |
205 | print ("@b" eq '4 3 2 1' ? "ok 35\n" : "not ok 35 |@b|\n"); |
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 GS |
213 | @b = sort $sortsub 4,1,3,2; |
214 | print ("@b" eq '4 3 2 1' ? "ok 36\n" : "not ok 36 |@b|\n"); | |
215 | @b = sort $sortglob 4,1,3,2; | |
216 | print ("@b" eq '4 3 2 1' ? "ok 37\n" : "not ok 37 |@b|\n"); | |
217 | @b = sort $sortname 4,1,3,2; | |
218 | print ("@b" eq '4 3 2 1' ? "ok 38\n" : "not ok 38 |@b|\n"); | |
219 | @b = sort $sortglobr 4,1,3,2; | |
220 | print ("@b" eq '4 3 2 1' ? "ok 39\n" : "not ok 39 |@b|\n"); | |
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; | |
43481408 | 229 | print ("@b" eq '1996 255 90 19 5' ? "ok 40\n" : "not ok 40\n"); |
9c007264 JH |
230 | print "# x = '@b'\n"; |
231 | $x = join('', sort { $a cmp $b } @harry); | |
232 | $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; | |
43481408 | 233 | print ($x eq $expected ? "ok 41\n" : "not ok 41\n"); |
9c007264 JH |
234 | print "# x = '$x'; expected = '$expected'\n"; |
235 | $x = join('', sort { $b cmp $a } @harry); | |
236 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; | |
43481408 | 237 | print ($x eq $expected ? "ok 42\n" : "not ok 42\n"); |
9c007264 JH |
238 | print "# x = '$x'; expected = '$expected'\n"; |
239 | { | |
240 | use integer; | |
241 | @b = sort { $a <=> $b } @a; | |
43481408 | 242 | print ("@b" eq '5 19 90 255 1996' ? "ok 43\n" : "not ok 43\n"); |
9c007264 JH |
243 | print "# x = '@b'\n"; |
244 | @b = sort { $b <=> $a } @a; | |
43481408 | 245 | print ("@b" eq '1996 255 90 19 5' ? "ok 44\n" : "not ok 44\n"); |
9c007264 JH |
246 | print "# x = '@b'\n"; |
247 | $x = join('', sort { $a cmp $b } @harry); | |
248 | $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; | |
43481408 | 249 | print ($x eq $expected ? "ok 45\n" : "not ok 45\n"); |
9c007264 JH |
250 | print "# x = '$x'; expected = '$expected'\n"; |
251 | $x = join('', sort { $b cmp $a } @harry); | |
252 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; | |
43481408 | 253 | print ($x eq $expected ? "ok 46\n" : "not ok 46\n"); |
9c007264 JH |
254 | print "# x = '$x'; expected = '$expected'\n"; |
255 | } | |
e507f050 SM |
256 | |
257 | # test that an optimized-away comparison block doesn't take any other | |
258 | # arguments away with it | |
259 | $x = join('', sort { $a <=> $b } 3, 1, 2); | |
43481408 | 260 | print $x eq "123" ? "ok 47\n" : "not ok 47\n"; |
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; | |
43481408 GS |
266 | print ("@b" eq '1996 255 90 19 5' ? "ok 48\n" : "not ok 48\n"); |
267 | print "# x = '@b'\n"; | |
268 | ||
9f1b1f2d | 269 | @b = sort main::Backwards_stacked @a; |
43481408 | 270 | print ("@b" eq '90 5 255 1996 19' ? "ok 49\n" : "not ok 49\n"); |
9c007264 | 271 | print "# x = '@b'\n"; |
8e3f9bdf GS |
272 | |
273 | # check if context for sort arguments is handled right | |
274 | ||
275 | $test = 49; | |
276 | sub test_if_list { | |
277 | my $gimme = wantarray; | |
278 | print "not " unless $gimme; | |
279 | ++$test; | |
280 | print "ok $test\n"; | |
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; | |
293 | print "not " if $gimme or !defined($gimme); | |
294 | ++$test; | |
295 | print "ok $test\n"; | |
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/; | |
319 | print ("@b" eq '1 2 3 4' ? "ok 56\n" : "not ok 56\n"); | |
320 | print "# x = '@b'\n"; | |
321 | print !$def ? "ok 57\n" : "not ok 57\n"; | |
322 | } | |
f0670693 SC |
323 | |
324 | # Bug 19991001.003 | |
325 | { | |
326 | sub routine { "one", "two" }; | |
327 | @a = sort(routine(1)); | |
328 | print "@a" eq "one two" ? "ok 58\n" : "not ok 58\n"; | |
329 | } | |
fe1bc4cf DM |
330 | |
331 | ||
332 | my $test = 59; | |
333 | sub ok { | |
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++; | |
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"; | |
fe1bc4cf DM |
397 | } |
398 | ||
eb209983 NC |
399 | # Test optimisations of reversed sorts. As we now guarantee stability by |
400 | # default, # optimisations which do not provide this are bogus. | |
fe1bc4cf | 401 | |
eb209983 NC |
402 | { |
403 | package Oscalar; | |
404 | use overload (qw("" stringify 0+ numify fallback 1)); | |
405 | ||
406 | sub new { | |
407 | bless [$_[1], $_[2]], $_[0]; | |
408 | } | |
409 | ||
410 | sub stringify { $_[0]->[0] } | |
411 | ||
412 | sub numify { $_[0]->[1] } | |
413 | } | |
414 | ||
415 | sub generate { | |
416 | my $count = 0; | |
417 | map {new Oscalar $_, $count++} qw(A A A B B B C C C); | |
418 | } | |
419 | ||
420 | my @input = &generate; | |
421 | my @output = sort @input; | |
422 | ok join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", "Simple stable sort"; | |
423 | ||
424 | @input = &generate; | |
425 | @input = sort @input; | |
426 | ok join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8", | |
427 | "Simple stable in place sort"; | |
428 | ||
429 | # This won't be very interesting | |
430 | @input = &generate; | |
431 | @output = sort {$a <=> $b} @input; | |
432 | ok "@output", "A A A B B B C C C", 'stable $a <=> $b sort'; | |
433 | ||
434 | @input = &generate; | |
435 | @output = sort {$a cmp $b} @input; | |
436 | ok join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", 'stable $a cmp $b sort'; | |
437 | ||
438 | @input = &generate; | |
439 | @input = sort {$a cmp $b} @input; | |
440 | ok join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8", | |
441 | 'stable $a cmp $b in place sort'; | |
442 | ||
443 | @input = &generate; | |
444 | @output = sort {$b cmp $a} @input; | |
445 | ok join(" ", map {0+$_} @output), "6 7 8 3 4 5 0 1 2", 'stable $b cmp $a sort'; | |
446 | ||
447 | @input = &generate; | |
448 | @input = sort {$b cmp $a} @input; | |
449 | ok join(" ", map {0+$_} @input), "6 7 8 3 4 5 0 1 2", | |
450 | 'stable $b cmp $a in place sort'; | |
451 | ||
75dd5fa4 | 452 | @input = &generate; |
eb209983 NC |
453 | @output = reverse sort @input; |
454 | ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", "Reversed stable sort"; | |
455 | ||
456 | @input = &generate; | |
457 | @input = reverse sort @input; | |
458 | ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0", | |
459 | "Reversed stable in place sort"; | |
460 | ||
75dd5fa4 NC |
461 | @input = &generate; |
462 | my $output = reverse sort @input; | |
463 | ok $output, "CCCBBBAAA", "Reversed stable sort in scalar context"; | |
464 | ||
eb209983 NC |
465 | |
466 | @input = &generate; | |
467 | @output = reverse sort {$a cmp $b} @input; | |
468 | ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", | |
469 | 'reversed stable $a cmp $b sort'; | |
470 | ||
471 | @input = &generate; | |
472 | @input = reverse sort {$a cmp $b} @input; | |
473 | ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0", | |
474 | 'revesed stable $a cmp $b in place sort'; | |
475 | ||
476 | @input = &generate; | |
7e7a548e | 477 | $output = reverse sort {$a cmp $b} @input; |
75dd5fa4 NC |
478 | ok $output, "CCCBBBAAA", 'Reversed stable $a cmp $b sort in scalar context'; |
479 | ||
480 | @input = &generate; | |
eb209983 NC |
481 | @output = reverse sort {$b cmp $a} @input; |
482 | ok join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6", | |
483 | 'reversed stable $b cmp $a sort'; | |
484 | ||
485 | @input = &generate; | |
486 | @input = reverse sort {$b cmp $a} @input; | |
487 | ok join(" ", map {0+$_} @input), "2 1 0 5 4 3 8 7 6", | |
488 | 'revesed stable $b cmp $a in place sort'; | |
489 | ||
75dd5fa4 NC |
490 | @input = &generate; |
491 | $output = reverse sort {$b cmp $a} @input; | |
492 | ok $output, "AAABBBCCC", 'Reversed stable $b cmp $a sort in scalar context'; | |
493 | ||
7e7a548e NC |
494 | sub stuff { |
495 | # Something complex enough to defeat any constant folding optimiser | |
496 | $$ - $$; | |
497 | } | |
498 | ||
499 | @input = &generate; | |
500 | @output = reverse sort {stuff || $a cmp $b} @input; | |
501 | ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", | |
502 | 'reversed stable complex sort'; | |
503 | ||
504 | @input = &generate; | |
505 | @input = reverse sort {stuff || $a cmp $b} @input; | |
506 | ok join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0", | |
507 | 'revesed stable complex in place sort'; | |
508 | ||
509 | @input = &generate; | |
510 | $output = reverse sort {stuff || $a cmp $b } @input; | |
511 | ok $output, "CCCBBBAAA", 'Reversed stable complex sort in scalar context'; | |
512 | ||
a1824f2a NC |
513 | sub sortr { |
514 | reverse sort @_; | |
515 | } | |
516 | ||
517 | @output = sortr &generate; | |
518 | ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", | |
519 | 'reversed stable sort return list context'; | |
520 | $output = sortr &generate; | |
521 | ok $output, "CCCBBBAAA", | |
522 | 'reversed stable sort return scalar context'; | |
523 | ||
524 | sub sortcmpr { | |
525 | reverse sort {$a cmp $b} @_; | |
526 | } | |
527 | ||
528 | @output = sortcmpr &generate; | |
529 | ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", | |
530 | 'reversed stable $a cmp $b sort return list context'; | |
531 | $output = sortcmpr &generate; | |
532 | ok $output, "CCCBBBAAA", | |
533 | 'reversed stable $a cmp $b sort return scalar context'; | |
534 | ||
535 | sub sortcmprba { | |
536 | reverse sort {$b cmp $a} @_; | |
537 | } | |
538 | ||
539 | @output = sortcmprba &generate; | |
540 | ok join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6", | |
541 | 'reversed stable $b cmp $a sort return list context'; | |
542 | $output = sortcmprba &generate; | |
543 | ok $output, "AAABBBCCC", | |
544 | 'reversed stable $b cmp $a sort return scalar context'; | |
eb209983 | 545 | |
7e7a548e NC |
546 | sub sortcmprq { |
547 | reverse sort {stuff || $a cmp $b} @_; | |
548 | } | |
549 | ||
550 | @output = sortcmpr &generate; | |
551 | ok join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", | |
552 | 'reversed stable complex sort return list context'; | |
553 | $output = sortcmpr &generate; | |
554 | ok $output, "CCCBBBAAA", | |
555 | 'reversed stable complex sort return scalar context'; | |
556 | ||
eb209983 NC |
557 | # And now with numbers |
558 | ||
559 | sub generate1 { | |
560 | my $count = 'A'; | |
561 | map {new Oscalar $count++, $_} 0, 0, 0, 1, 1, 1, 2, 2, 2; | |
562 | } | |
563 | ||
564 | # This won't be very interesting | |
565 | @input = &generate1; | |
566 | @output = sort {$a cmp $b} @input; | |
567 | ok "@output", "A B C D E F G H I", 'stable $a cmp $b sort'; | |
568 | ||
569 | @input = &generate1; | |
570 | @output = sort {$a <=> $b} @input; | |
571 | ok "@output", "A B C D E F G H I", 'stable $a <=> $b sort'; | |
572 | ||
573 | @input = &generate1; | |
574 | @input = sort {$a <=> $b} @input; | |
575 | ok "@input", "A B C D E F G H I", 'stable $a <=> $b in place sort'; | |
576 | ||
577 | @input = &generate1; | |
578 | @output = sort {$b <=> $a} @input; | |
579 | ok "@output", "G H I D E F A B C", 'stable $b <=> $a sort'; | |
580 | ||
581 | @input = &generate1; | |
582 | @input = sort {$b <=> $a} @input; | |
583 | ok "@input", "G H I D E F A B C", 'stable $b <=> $a in place sort'; | |
584 | ||
59608b94 DN |
585 | # test that optimized {$b cmp $a} and {$b <=> $a} remain stable |
586 | # (new in 5.9) without overloading | |
587 | { no warnings; | |
588 | @b = sort { $b <=> $a } @input = qw/5first 6first 5second 6second/; | |
589 | ok "@b" , "6first 6second 5first 5second", "optimized {$b <=> $a} without overloading" ; | |
590 | @input = sort {$b <=> $a} @input; | |
591 | ok "@input" , "6first 6second 5first 5second","inline optimized {$b <=> $a} without overloading" ; | |
592 | }; | |
593 | ||
eb209983 | 594 | # These two are actually doing string cmp on 0 1 and 2 |
75dd5fa4 | 595 | @input = &generate1; |
eb209983 NC |
596 | @output = reverse sort @input; |
597 | ok "@output", "I H G F E D C B A", "Reversed stable sort"; | |
598 | ||
599 | @input = &generate1; | |
600 | @input = reverse sort @input; | |
601 | ok "@input", "I H G F E D C B A", "Reversed stable in place sort"; | |
602 | ||
603 | @input = &generate1; | |
75dd5fa4 NC |
604 | $output = reverse sort @input; |
605 | ok $output, "IHGFEDCBA", "Reversed stable sort in scalar context"; | |
606 | ||
607 | @input = &generate1; | |
eb209983 NC |
608 | @output = reverse sort {$a <=> $b} @input; |
609 | ok "@output", "I H G F E D C B A", 'reversed stable $a <=> $b sort'; | |
610 | ||
611 | @input = &generate1; | |
612 | @input = reverse sort {$a <=> $b} @input; | |
613 | ok "@input", "I H G F E D C B A", 'revesed stable $a <=> $b in place sort'; | |
fe1bc4cf | 614 | |
eb209983 | 615 | @input = &generate1; |
75dd5fa4 NC |
616 | $output = reverse sort {$a <=> $b} @input; |
617 | ok $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort in scalar context'; | |
618 | ||
619 | @input = &generate1; | |
eb209983 NC |
620 | @output = reverse sort {$b <=> $a} @input; |
621 | ok "@output", "C B A F E D I H G", 'reversed stable $b <=> $a sort'; | |
fe1bc4cf | 622 | |
eb209983 NC |
623 | @input = &generate1; |
624 | @input = reverse sort {$b <=> $a} @input; | |
625 | ok "@input", "C B A F E D I H G", 'revesed stable $b <=> $a in place sort'; | |
75dd5fa4 NC |
626 | |
627 | @input = &generate1; | |
628 | $output = reverse sort {$b <=> $a} @input; | |
629 | ok $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort in scalar context'; | |
a1824f2a | 630 | |
7e7a548e NC |
631 | @input = &generate1; |
632 | @output = reverse sort {stuff || $a <=> $b} @input; | |
633 | ok "@output", "I H G F E D C B A", 'reversed stable complex sort'; | |
634 | ||
635 | @input = &generate1; | |
636 | @input = reverse sort {stuff || $a <=> $b} @input; | |
637 | ok "@input", "I H G F E D C B A", 'revesed stable complex in place sort'; | |
638 | ||
639 | @input = &generate1; | |
640 | $output = reverse sort {stuff || $a <=> $b} @input; | |
641 | ok $output, "IHGFEDCBA", 'reversed stable complex sort in scalar context'; | |
a1824f2a NC |
642 | |
643 | sub sortnumr { | |
644 | reverse sort {$a <=> $b} @_; | |
645 | } | |
646 | ||
647 | @output = sortnumr &generate1; | |
648 | ok "@output", "I H G F E D C B A", | |
649 | 'reversed stable $a <=> $b sort return list context'; | |
650 | $output = sortnumr &generate1; | |
c093edd0 | 651 | ok $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort return scalar context'; |
a1824f2a NC |
652 | |
653 | sub sortnumrba { | |
654 | reverse sort {$b <=> $a} @_; | |
655 | } | |
656 | ||
657 | @output = sortnumrba &generate1; | |
658 | ok "@output", "C B A F E D I H G", | |
659 | 'reversed stable $b <=> $a sort return list context'; | |
660 | $output = sortnumrba &generate1; | |
c093edd0 | 661 | ok $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort return scalar context'; |
7e7a548e NC |
662 | |
663 | sub sortnumrq { | |
664 | reverse sort {stuff || $a <=> $b} @_; | |
665 | } | |
666 | ||
667 | @output = sortnumrq &generate1; | |
668 | ok "@output", "I H G F E D C B A", | |
669 | 'reversed stable complex sort return list context'; | |
670 | $output = sortnumrq &generate1; | |
c093edd0 NC |
671 | ok $output, "IHGFEDCBA", 'reversed stable complex sort return scalar context'; |
672 | ||
673 | @output = reverse (sort(qw(C A B)), 0); | |
674 | ok "@output", "0 C B A", 'reversed sort with trailing argument'; | |
675 | ||
676 | @output = reverse (0, sort(qw(C A B))); | |
677 | ok "@output", "C B A 0", 'reversed sort with leading argument'; | |
9850bf21 RH |
678 | |
679 | eval { @output = sort {goto sub {}} 1,2; }; | |
680 | print(($@ =~ /^Can't goto subroutine outside a subroutine/ ? | |
681 | "ok " : | |
682 | "not ok "), | |
683 | $test++, " # $@"); | |
684 | ||
685 | sub goto_sub {goto sub{}} | |
686 | eval { @output = sort goto_sub 1,2; }; | |
687 | print(($@ =~ /^Can't goto subroutine from a sort sub/ ? | |
688 | "ok " : | |
689 | "not ok "), | |
690 | $test++, " # $@"); | |
691 | ||
692 | eval { @output = sort {goto label} 1,2; }; | |
693 | print(($@ =~ /^Can't "goto" out of a pseudo block/ ? | |
694 | "ok " : | |
695 | "not ok "), | |
696 | $test++, " # $@"); | |
697 | ||
698 | sub goto_label {goto label} | |
699 | label: eval { @output = sort goto_label 1,2; }; | |
700 | print(($@ =~ /^Can't "goto" out of a pseudo block/ ? | |
701 | "ok " : | |
702 | "not ok "), | |
703 | $test++, " # $@"); | |
704 | ||
705 | sub self_immolate {undef &self_immolate; $a<=>$b} | |
706 | eval { @output = sort self_immolate 1,2,3 }; | |
707 | print(($@ =~ /^Can't undef active subroutine/ ? | |
708 | "ok " : | |
709 | "not ok "), | |
710 | $test++, " # $@"); | |
711 | ||
712 | { | |
713 | my $failed = 0; | |
714 | ||
715 | sub rec { | |
716 | my $n = shift; | |
717 | if (!defined($n)) { # No arg means we're being called by sort() | |
718 | return 1; | |
719 | } | |
720 | if ($n<5) { rec($n+1); } | |
721 | else { () = sort rec 1,2; } | |
722 | ||
723 | $failed = 1 if !defined $n; | |
724 | } | |
725 | ||
726 | rec(1); | |
727 | print((!$failed ? "ok " : "not ok "), $test++, " - sort from active sub\n"); | |
728 | } | |
729 | ||
730 | # $a and $b are set in the package the sort() is called from, | |
731 | # *not* the package the sort sub is in. This is longstanding | |
732 | # de facto behaviour that shouldn't be broken. | |
733 | package main; | |
734 | my $answer = "ok "; | |
735 | () = sort OtherPack::foo 1,2,3,4; | |
736 | ||
9da9462b RGS |
737 | { |
738 | package OtherPack; | |
739 | no warnings 'once'; | |
740 | sub foo { | |
741 | $answer = "not ok " if | |
742 | defined($a) || defined($b) || !defined($main::a) || !defined($main::b); | |
743 | $main::a <=> $main::b; | |
744 | } | |
745 | } | |
9850bf21 RH |
746 | |
747 | print $answer, $test++, "\n"; | |
748 | ||
749 | ||
750 | # Bug 36430 - sort called in package2 while a | |
751 | # sort in package1 is active should set $package2::a/b. | |
752 | ||
753 | $answer = "ok "; | |
754 | my @list = sort { A::min(@$a) <=> A::min(@$b) } | |
755 | [3, 1, 5], [2, 4], [0]; | |
756 | ||
757 | print $answer, $test++, "\n"; | |
758 | ||
759 | package A; | |
760 | sub min { | |
761 | my @list = sort { | |
762 | $answer = "not ok " if !defined($a) || !defined($b); | |
763 | $a <=> $b; | |
764 | } @_; | |
765 | $list[0]; | |
766 | } | |
767 | ||
768 | # Bug 7567 - an array shouldn't be modifiable while it's being | |
769 | # sorted in-place. | |
770 | eval { @a=(1..8); @a = sort { @a = (0) } @a; }; | |
771 | ||
772 | print(($@ =~ /^Modification of a read-only value attempted/ ? | |
773 | "ok " : | |
774 | "not ok "), | |
775 | $test++, " # $@"); | |
776 | ||
777 | # Sorting shouldn't increase the refcount of a sub | |
778 | sub foo {(1+$a) <=> (1+$b)} | |
779 | my $refcnt = &Internals::SvREFCNT(\&foo); | |
780 | @output = sort foo 3,7,9; | |
781 | package Foo; | |
782 | ok($refcnt, &Internals::SvREFCNT(\&foo), "sort sub refcnt"); | |
783 | ||
784 | # Sorting a read-only array in-place shouldn't be allowed | |
785 | my @readonly = (1..10); | |
786 | Internals::SvREADONLY(@readonly, 1); | |
787 | eval { @readonly = sort @readonly; }; | |
788 | print(($@ =~ /^Modification of a read-only value attempted/ ? | |
789 | "ok " : | |
790 | "not ok "), | |
791 | $test++, " # $@"); | |
792 | ||
793 | # Using return() should be okay even in a deeper context | |
794 | @b = sort {while (1) {return ($a <=> $b)} } 1..10; | |
795 | ok("@b", "1 2 3 4 5 6 7 8 9 10", "return within loop"); | |
d7507f74 RH |
796 | |
797 | # Using return() should be okay even if there are other items | |
798 | # on the stack at the time. | |
799 | @b = sort {$_ = ($a<=>$b) + do{return $b<=> $a}} 1..10; | |
800 | ok("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack"); | |
801 | ||
802 | # As above, but with a sort sub rather than a sort block. | |
803 | sub ret_with_stacked { $_ = ($a<=>$b) + do {return $b <=> $a} } | |
804 | @b = sort ret_with_stacked 1..10; | |
805 | ok("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack"); |