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