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