Commit | Line | Data |
a687059c |
1 | #!./perl |
2 | |
9c007264 |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
93430cb4 |
5 | unshift @INC, '../lib'; |
9c007264 |
6 | } |
ee8c7f54 |
7 | use warnings; |
8 | print "1..57\n"; |
a687059c |
9 | |
f63ceb1c |
10 | # XXX known to leak scalars |
ee8c7f54 |
11 | { |
12 | no warnings 'uninitialized'; |
13 | $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3; |
14 | } |
15 | |
16 | # these shouldn't hang |
17 | { |
18 | no warnings; |
19 | sort { for ($_ = 0;; $_++) {} } @a; |
20 | sort { while(1) {} } @a; |
21 | sort { while(1) { last; } } @a; |
22 | sort { while(0) { last; } } @a; |
23 | } |
f63ceb1c |
24 | |
ee8c7f54 |
25 | sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 } |
26 | sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 } |
a687059c |
27 | |
9d116dd7 |
28 | my $upperfirst = 'A' lt 'a'; |
29 | |
30 | # Beware: in future this may become hairier because of possible |
31 | # collation complications: qw(A a B c) can be sorted at least as |
32 | # any of the following |
33 | # |
34 | # A a B b |
35 | # A B a b |
36 | # a b A B |
37 | # a A b B |
38 | # |
39 | # All the above orders make sense. |
40 | # |
41 | # That said, EBCDIC sorts all small letters first, as opposed |
42 | # to ASCII which sorts all big letters first. |
43 | |
a687059c |
44 | @harry = ('dog','cat','x','Cain','Abel'); |
2f52a358 |
45 | @george = ('gone','chased','yz','punished','Axed'); |
a687059c |
46 | |
47 | $x = join('', sort @harry); |
9d116dd7 |
48 | $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; |
49 | print "# 1: x = '$x', expected = '$expected'\n"; |
50 | print ($x eq $expected ? "ok 1\n" : "not ok 1\n"); |
a687059c |
51 | |
ee8c7f54 |
52 | $x = join('', sort( Backwards @harry)); |
9d116dd7 |
53 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
54 | print "# 2: x = '$x', expected = '$expected'\n"; |
55 | print ($x eq $expected ? "ok 2\n" : "not ok 2\n"); |
a687059c |
56 | |
ee8c7f54 |
57 | $x = join('', sort( Backwards_stacked @harry)); |
146174a9 |
58 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
59 | print "# 3: x = '$x', expected = '$expected'\n"; |
60 | print ($x eq $expected ? "ok 3\n" : "not ok 3\n"); |
61 | |
a687059c |
62 | $x = join('', sort @george, 'to', @harry); |
9d116dd7 |
63 | $expected = $upperfirst ? |
64 | 'AbelAxedCaincatchaseddoggonepunishedtoxyz' : |
65 | 'catchaseddoggonepunishedtoxyzAbelAxedCain' ; |
146174a9 |
66 | print "# 4: x = '$x', expected = '$expected'\n"; |
67 | print ($x eq $expected ?"ok 4\n":"not ok 4\n"); |
03a14243 |
68 | |
69 | @a = (); |
70 | @b = reverse @a; |
146174a9 |
71 | print ("@b" eq "" ? "ok 5\n" : "not ok 5 (@b)\n"); |
03a14243 |
72 | |
73 | @a = (1); |
74 | @b = reverse @a; |
146174a9 |
75 | print ("@b" eq "1" ? "ok 6\n" : "not ok 6 (@b)\n"); |
03a14243 |
76 | |
77 | @a = (1,2); |
78 | @b = reverse @a; |
146174a9 |
79 | print ("@b" eq "2 1" ? "ok 7\n" : "not ok 7 (@b)\n"); |
03a14243 |
80 | |
81 | @a = (1,2,3); |
82 | @b = reverse @a; |
146174a9 |
83 | print ("@b" eq "3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n"); |
03a14243 |
84 | |
85 | @a = (1,2,3,4); |
86 | @b = reverse @a; |
146174a9 |
87 | print ("@b" eq "4 3 2 1" ? "ok 9\n" : "not ok 9 (@b)\n"); |
55204971 |
88 | |
89 | @a = (10,2,3,4); |
90 | @b = sort {$a <=> $b;} @a; |
146174a9 |
91 | print ("@b" eq "2 3 4 10" ? "ok 10\n" : "not ok 10 (@b)\n"); |
988174c1 |
92 | |
ee8c7f54 |
93 | $sub = 'Backwards'; |
988174c1 |
94 | $x = join('', sort $sub @harry); |
9d116dd7 |
95 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
146174a9 |
96 | print "# 11: x = $x, expected = '$expected'\n"; |
97 | print ($x eq $expected ? "ok 11\n" : "not ok 11\n"); |
98 | |
ee8c7f54 |
99 | $sub = 'Backwards_stacked'; |
146174a9 |
100 | $x = join('', sort $sub @harry); |
101 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
102 | print "# 12: x = $x, expected = '$expected'\n"; |
103 | print ($x eq $expected ? "ok 12\n" : "not ok 12\n"); |
988174c1 |
104 | |
cd5de442 |
105 | # literals, combinations |
106 | |
107 | @b = sort (4,1,3,2); |
146174a9 |
108 | print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n"); |
cd5de442 |
109 | print "# x = '@b'\n"; |
110 | |
111 | @b = sort grep { $_ } (4,1,3,2); |
146174a9 |
112 | print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n"); |
cd5de442 |
113 | print "# x = '@b'\n"; |
114 | |
115 | @b = sort map { $_ } (4,1,3,2); |
146174a9 |
116 | print ("@b" eq '1 2 3 4' ? "ok 15\n" : "not ok 15\n"); |
cd5de442 |
117 | print "# x = '@b'\n"; |
118 | |
119 | @b = sort reverse (4,1,3,2); |
146174a9 |
120 | print ("@b" eq '1 2 3 4' ? "ok 16\n" : "not ok 16\n"); |
cd5de442 |
121 | print "# x = '@b'\n"; |
7bac28a0 |
122 | |
7bac28a0 |
123 | # redefining sort sub inside the sort sub should fail |
124 | sub twoface { *twoface = sub { $a <=> $b }; &twoface } |
125 | eval { @b = sort twoface 4,1,3,2 }; |
146174a9 |
126 | print ($@ =~ /redefine active sort/ ? "ok 17\n" : "not ok 17\n"); |
7bac28a0 |
127 | |
128 | # redefining sort subs outside the sort should not fail |
ee8c7f54 |
129 | eval { no warnings 'redefine'; *twoface = sub { &Backwards } }; |
146174a9 |
130 | print $@ ? "not ok 18\n" : "ok 18\n"; |
7bac28a0 |
131 | |
132 | eval { @b = sort twoface 4,1,3,2 }; |
146174a9 |
133 | print ("@b" eq '4 3 2 1' ? "ok 19\n" : "not ok 19 |@b|\n"); |
7bac28a0 |
134 | |
ee8c7f54 |
135 | { |
136 | no warnings 'redefine'; |
137 | *twoface = sub { *twoface = *Backwards; $a <=> $b }; |
138 | } |
7bac28a0 |
139 | eval { @b = sort twoface 4,1 }; |
146174a9 |
140 | print ($@ =~ /redefine active sort/ ? "ok 20\n" : "not ok 20\n"); |
7bac28a0 |
141 | |
ee8c7f54 |
142 | { |
143 | no warnings 'redefine'; |
144 | *twoface = sub { |
7bac28a0 |
145 | eval 'sub twoface { $a <=> $b }'; |
146174a9 |
146 | die($@ =~ /redefine active sort/ ? "ok 21\n" : "not ok 21\n"); |
7bac28a0 |
147 | $a <=> $b; |
148 | }; |
ee8c7f54 |
149 | } |
7bac28a0 |
150 | eval { @b = sort twoface 4,1 }; |
146174a9 |
151 | print $@ ? "$@" : "not ok 21\n"; |
15f0808c |
152 | |
153 | eval <<'CODE'; |
ee8c7f54 |
154 | my @result = sort main'Backwards 'one', 'two'; |
15f0808c |
155 | CODE |
146174a9 |
156 | print $@ ? "not ok 22\n# $@" : "ok 22\n"; |
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 |
146174a9 |
162 | print $@ ? "not ok 23\n# $@" : "ok 23\n"; |
c6e96bcb |
163 | |
164 | { |
ee8c7f54 |
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; |
146174a9 |
170 | print ("@b" eq '4 3 2 1' ? "ok 24\n" : "not ok 24 |@b|\n"); |
c6e96bcb |
171 | @b = sort $sortglob 4,1,3,2; |
146174a9 |
172 | print ("@b" eq '4 3 2 1' ? "ok 25\n" : "not ok 25 |@b|\n"); |
c6e96bcb |
173 | @b = sort $sortname 4,1,3,2; |
146174a9 |
174 | print ("@b" eq '4 3 2 1' ? "ok 26\n" : "not ok 26 |@b|\n"); |
62f274bf |
175 | @b = sort $sortglobr 4,1,3,2; |
146174a9 |
176 | print ("@b" eq '4 3 2 1' ? "ok 27\n" : "not ok 27 |@b|\n"); |
177 | } |
178 | |
179 | { |
ee8c7f54 |
180 | my $sortsub = \&Backwards_stacked; |
181 | my $sortglob = *Backwards_stacked; |
182 | my $sortglobr = \*Backwards_stacked; |
183 | my $sortname = 'Backwards_stacked'; |
146174a9 |
184 | @b = sort $sortsub 4,1,3,2; |
185 | print ("@b" eq '4 3 2 1' ? "ok 28\n" : "not ok 28 |@b|\n"); |
186 | @b = sort $sortglob 4,1,3,2; |
187 | print ("@b" eq '4 3 2 1' ? "ok 29\n" : "not ok 29 |@b|\n"); |
188 | @b = sort $sortname 4,1,3,2; |
189 | print ("@b" eq '4 3 2 1' ? "ok 30\n" : "not ok 30 |@b|\n"); |
190 | @b = sort $sortglobr 4,1,3,2; |
191 | print ("@b" eq '4 3 2 1' ? "ok 31\n" : "not ok 31 |@b|\n"); |
c6e96bcb |
192 | } |
193 | |
194 | { |
ee8c7f54 |
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; |
146174a9 |
200 | print ("@b" eq '4 3 2 1' ? "ok 32\n" : "not ok 32 |@b|\n"); |
c6e96bcb |
201 | @b = sort $sortglob 4,1,3,2; |
146174a9 |
202 | print ("@b" eq '4 3 2 1' ? "ok 33\n" : "not ok 33 |@b|\n"); |
c6e96bcb |
203 | @b = sort $sortname 4,1,3,2; |
146174a9 |
204 | print ("@b" eq '4 3 2 1' ? "ok 34\n" : "not ok 34 |@b|\n"); |
62f274bf |
205 | @b = sort $sortglobr 4,1,3,2; |
146174a9 |
206 | print ("@b" eq '4 3 2 1' ? "ok 35\n" : "not ok 35 |@b|\n"); |
207 | } |
208 | |
209 | { |
ee8c7f54 |
210 | local $sortsub = \&Backwards_stacked; |
211 | local $sortglob = *Backwards_stacked; |
212 | local $sortglobr = \*Backwards_stacked; |
213 | local $sortname = 'Backwards_stacked'; |
146174a9 |
214 | @b = sort $sortsub 4,1,3,2; |
215 | print ("@b" eq '4 3 2 1' ? "ok 36\n" : "not ok 36 |@b|\n"); |
216 | @b = sort $sortglob 4,1,3,2; |
217 | print ("@b" eq '4 3 2 1' ? "ok 37\n" : "not ok 37 |@b|\n"); |
218 | @b = sort $sortname 4,1,3,2; |
219 | print ("@b" eq '4 3 2 1' ? "ok 38\n" : "not ok 38 |@b|\n"); |
220 | @b = sort $sortglobr 4,1,3,2; |
221 | print ("@b" eq '4 3 2 1' ? "ok 39\n" : "not ok 39 |@b|\n"); |
c6e96bcb |
222 | } |
223 | |
9c007264 |
224 | ## exercise sort builtins... ($a <=> $b already tested) |
225 | @a = ( 5, 19, 1996, 255, 90 ); |
146174a9 |
226 | @b = sort { |
227 | my $dummy; # force blockness |
228 | return $b <=> $a |
229 | } @a; |
230 | print ("@b" eq '1996 255 90 19 5' ? "ok 40\n" : "not ok 40\n"); |
9c007264 |
231 | print "# x = '@b'\n"; |
232 | $x = join('', sort { $a cmp $b } @harry); |
233 | $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; |
146174a9 |
234 | print ($x eq $expected ? "ok 41\n" : "not ok 41\n"); |
9c007264 |
235 | print "# x = '$x'; expected = '$expected'\n"; |
236 | $x = join('', sort { $b cmp $a } @harry); |
237 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
146174a9 |
238 | print ($x eq $expected ? "ok 42\n" : "not ok 42\n"); |
9c007264 |
239 | print "# x = '$x'; expected = '$expected'\n"; |
240 | { |
241 | use integer; |
242 | @b = sort { $a <=> $b } @a; |
146174a9 |
243 | print ("@b" eq '5 19 90 255 1996' ? "ok 43\n" : "not ok 43\n"); |
9c007264 |
244 | print "# x = '@b'\n"; |
245 | @b = sort { $b <=> $a } @a; |
146174a9 |
246 | print ("@b" eq '1996 255 90 19 5' ? "ok 44\n" : "not ok 44\n"); |
9c007264 |
247 | print "# x = '@b'\n"; |
248 | $x = join('', sort { $a cmp $b } @harry); |
249 | $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; |
146174a9 |
250 | print ($x eq $expected ? "ok 45\n" : "not ok 45\n"); |
9c007264 |
251 | print "# x = '$x'; expected = '$expected'\n"; |
252 | $x = join('', sort { $b cmp $a } @harry); |
253 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
146174a9 |
254 | print ($x eq $expected ? "ok 46\n" : "not ok 46\n"); |
9c007264 |
255 | print "# x = '$x'; expected = '$expected'\n"; |
256 | } |
e507f050 |
257 | |
258 | # test that an optimized-away comparison block doesn't take any other |
259 | # arguments away with it |
260 | $x = join('', sort { $a <=> $b } 3, 1, 2); |
146174a9 |
261 | print $x eq "123" ? "ok 47\n" : "not ok 47\n"; |
e507f050 |
262 | |
9c007264 |
263 | # test sorting in non-main package |
264 | package Foo; |
265 | @a = ( 5, 19, 1996, 255, 90 ); |
266 | @b = sort { $b <=> $a } @a; |
146174a9 |
267 | print ("@b" eq '1996 255 90 19 5' ? "ok 48\n" : "not ok 48\n"); |
268 | print "# x = '@b'\n"; |
269 | |
ee8c7f54 |
270 | @b = sort main::Backwards_stacked @a; |
146174a9 |
271 | print ("@b" eq '90 5 255 1996 19' ? "ok 49\n" : "not ok 49\n"); |
9c007264 |
272 | print "# x = '@b'\n"; |
ee8c7f54 |
273 | |
274 | # check if context for sort arguments is handled right |
275 | |
276 | $test = 49; |
277 | sub test_if_list { |
278 | my $gimme = wantarray; |
279 | print "not " unless $gimme; |
280 | ++$test; |
281 | print "ok $test\n"; |
282 | } |
283 | my $m = sub { $a <=> $b }; |
284 | |
285 | sub cxt_one { sort $m test_if_list() } |
286 | cxt_one(); |
287 | sub cxt_two { sort { $a <=> $b } test_if_list() } |
288 | cxt_two(); |
289 | sub cxt_three { sort &test_if_list() } |
290 | cxt_three(); |
291 | |
292 | sub test_if_scalar { |
293 | my $gimme = wantarray; |
294 | print "not " if $gimme or !defined($gimme); |
295 | ++$test; |
296 | print "ok $test\n"; |
297 | } |
298 | |
299 | $m = \&test_if_scalar; |
300 | sub cxt_four { sort $m 1,2 } |
301 | @x = cxt_four(); |
302 | sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 } |
303 | @x = cxt_five(); |
304 | sub cxt_six { sort test_if_scalar 1,2 } |
305 | @x = cxt_six(); |
306 | |
307 | # test against a reentrancy bug |
308 | { |
309 | package Bar; |
310 | sub compare { $a cmp $b } |
311 | sub reenter { my @force = sort compare qw/a b/ } |
312 | } |
313 | { |
314 | my($def, $init) = (0, 0); |
315 | @b = sort { |
316 | $def = 1 if defined $Bar::a; |
317 | Bar::reenter() unless $init++; |
318 | $a <=> $b |
319 | } qw/4 3 1 2/; |
320 | print ("@b" eq '1 2 3 4' ? "ok 56\n" : "not ok 56\n"); |
321 | print "# x = '@b'\n"; |
322 | print !$def ? "ok 57\n" : "not ok 57\n"; |
323 | } |