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