10 # XXX known to leak scalars
12 no warnings 'uninitialized';
13 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
16 # these shouldn't hang
19 sort { for ($_ = 0;; $_++) {} } @a;
20 sort { while(1) {} } @a;
21 sort { while(1) { last; } } @a;
22 sort { while(0) { last; } } @a;
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 }
28 my $upperfirst = 'A' lt 'a';
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
39 # All the above orders make sense.
41 # That said, EBCDIC sorts all small letters first, as opposed
42 # to ASCII which sorts all big letters first.
44 @harry = ('dog','cat','x','Cain','Abel');
45 @george = ('gone','chased','yz','punished','Axed');
47 $x = join('', sort @harry);
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");
52 $x = join('', sort( Backwards @harry));
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");
57 $x = join('', sort( Backwards_stacked @harry));
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");
62 $x = join('', sort @george, 'to', @harry);
63 $expected = $upperfirst ?
64 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
65 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
66 print "# 4: x = '$x', expected = '$expected'\n";
67 print ($x eq $expected ?"ok 4\n":"not ok 4\n");
71 print ("@b" eq "" ? "ok 5\n" : "not ok 5 (@b)\n");
75 print ("@b" eq "1" ? "ok 6\n" : "not ok 6 (@b)\n");
79 print ("@b" eq "2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
83 print ("@b" eq "3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
87 print ("@b" eq "4 3 2 1" ? "ok 9\n" : "not ok 9 (@b)\n");
90 @b = sort {$a <=> $b;} @a;
91 print ("@b" eq "2 3 4 10" ? "ok 10\n" : "not ok 10 (@b)\n");
94 $x = join('', sort $sub @harry);
95 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
96 print "# 11: x = $x, expected = '$expected'\n";
97 print ($x eq $expected ? "ok 11\n" : "not ok 11\n");
99 $sub = 'Backwards_stacked';
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");
105 # literals, combinations
108 print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
109 print "# x = '@b'\n";
111 @b = sort grep { $_ } (4,1,3,2);
112 print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
113 print "# x = '@b'\n";
115 @b = sort map { $_ } (4,1,3,2);
116 print ("@b" eq '1 2 3 4' ? "ok 15\n" : "not ok 15\n");
117 print "# x = '@b'\n";
119 @b = sort reverse (4,1,3,2);
120 print ("@b" eq '1 2 3 4' ? "ok 16\n" : "not ok 16\n");
121 print "# x = '@b'\n";
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 };
126 print ($@ =~ /redefine active sort/ ? "ok 17\n" : "not ok 17\n");
128 # redefining sort subs outside the sort should not fail
129 eval { no warnings 'redefine'; *twoface = sub { &Backwards } };
130 print $@ ? "not ok 18\n" : "ok 18\n";
132 eval { @b = sort twoface 4,1,3,2 };
133 print ("@b" eq '4 3 2 1' ? "ok 19\n" : "not ok 19 |@b|\n");
136 no warnings 'redefine';
137 *twoface = sub { *twoface = *Backwards; $a <=> $b };
139 eval { @b = sort twoface 4,1 };
140 print ($@ =~ /redefine active sort/ ? "ok 20\n" : "not ok 20\n");
143 no warnings 'redefine';
145 eval 'sub twoface { $a <=> $b }';
146 die($@ =~ /redefine active sort/ ? "ok 21\n" : "not ok 21\n");
150 eval { @b = sort twoface 4,1 };
151 print $@ ? "$@" : "not ok 21\n";
154 my @result = sort main'Backwards 'one', 'two';
156 print $@ ? "not ok 22\n# $@" : "ok 22\n";
159 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
160 my @result = sort 'one', 'two';
162 print $@ ? "not ok 23\n# $@" : "ok 23\n";
165 my $sortsub = \&Backwards;
166 my $sortglob = *Backwards;
167 my $sortglobr = \*Backwards;
168 my $sortname = 'Backwards';
169 @b = sort $sortsub 4,1,3,2;
170 print ("@b" eq '4 3 2 1' ? "ok 24\n" : "not ok 24 |@b|\n");
171 @b = sort $sortglob 4,1,3,2;
172 print ("@b" eq '4 3 2 1' ? "ok 25\n" : "not ok 25 |@b|\n");
173 @b = sort $sortname 4,1,3,2;
174 print ("@b" eq '4 3 2 1' ? "ok 26\n" : "not ok 26 |@b|\n");
175 @b = sort $sortglobr 4,1,3,2;
176 print ("@b" eq '4 3 2 1' ? "ok 27\n" : "not ok 27 |@b|\n");
180 my $sortsub = \&Backwards_stacked;
181 my $sortglob = *Backwards_stacked;
182 my $sortglobr = \*Backwards_stacked;
183 my $sortname = 'Backwards_stacked';
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");
195 local $sortsub = \&Backwards;
196 local $sortglob = *Backwards;
197 local $sortglobr = \*Backwards;
198 local $sortname = 'Backwards';
199 @b = sort $sortsub 4,1,3,2;
200 print ("@b" eq '4 3 2 1' ? "ok 32\n" : "not ok 32 |@b|\n");
201 @b = sort $sortglob 4,1,3,2;
202 print ("@b" eq '4 3 2 1' ? "ok 33\n" : "not ok 33 |@b|\n");
203 @b = sort $sortname 4,1,3,2;
204 print ("@b" eq '4 3 2 1' ? "ok 34\n" : "not ok 34 |@b|\n");
205 @b = sort $sortglobr 4,1,3,2;
206 print ("@b" eq '4 3 2 1' ? "ok 35\n" : "not ok 35 |@b|\n");
210 local $sortsub = \&Backwards_stacked;
211 local $sortglob = *Backwards_stacked;
212 local $sortglobr = \*Backwards_stacked;
213 local $sortname = 'Backwards_stacked';
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");
224 ## exercise sort builtins... ($a <=> $b already tested)
225 @a = ( 5, 19, 1996, 255, 90 );
227 my $dummy; # force blockness
230 print ("@b" eq '1996 255 90 19 5' ? "ok 40\n" : "not ok 40\n");
231 print "# x = '@b'\n";
232 $x = join('', sort { $a cmp $b } @harry);
233 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
234 print ($x eq $expected ? "ok 41\n" : "not ok 41\n");
235 print "# x = '$x'; expected = '$expected'\n";
236 $x = join('', sort { $b cmp $a } @harry);
237 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
238 print ($x eq $expected ? "ok 42\n" : "not ok 42\n");
239 print "# x = '$x'; expected = '$expected'\n";
242 @b = sort { $a <=> $b } @a;
243 print ("@b" eq '5 19 90 255 1996' ? "ok 43\n" : "not ok 43\n");
244 print "# x = '@b'\n";
245 @b = sort { $b <=> $a } @a;
246 print ("@b" eq '1996 255 90 19 5' ? "ok 44\n" : "not ok 44\n");
247 print "# x = '@b'\n";
248 $x = join('', sort { $a cmp $b } @harry);
249 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
250 print ($x eq $expected ? "ok 45\n" : "not ok 45\n");
251 print "# x = '$x'; expected = '$expected'\n";
252 $x = join('', sort { $b cmp $a } @harry);
253 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
254 print ($x eq $expected ? "ok 46\n" : "not ok 46\n");
255 print "# x = '$x'; expected = '$expected'\n";
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);
261 print $x eq "123" ? "ok 47\n" : "not ok 47\n";
263 # test sorting in non-main package
265 @a = ( 5, 19, 1996, 255, 90 );
266 @b = sort { $b <=> $a } @a;
267 print ("@b" eq '1996 255 90 19 5' ? "ok 48\n" : "not ok 48\n");
268 print "# x = '@b'\n";
270 @b = sort main::Backwards_stacked @a;
271 print ("@b" eq '90 5 255 1996 19' ? "ok 49\n" : "not ok 49\n");
272 print "# x = '@b'\n";
274 # check if context for sort arguments is handled right
278 my $gimme = wantarray;
279 print "not " unless $gimme;
283 my $m = sub { $a <=> $b };
285 sub cxt_one { sort $m test_if_list() }
287 sub cxt_two { sort { $a <=> $b } test_if_list() }
289 sub cxt_three { sort &test_if_list() }
293 my $gimme = wantarray;
294 print "not " if $gimme or !defined($gimme);
299 $m = \&test_if_scalar;
300 sub cxt_four { sort $m 1,2 }
302 sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 }
304 sub cxt_six { sort test_if_scalar 1,2 }
307 # test against a reentrancy bug
310 sub compare { $a cmp $b }
311 sub reenter { my @force = sort compare qw/a b/ }
314 my($def, $init) = (0, 0);
316 $def = 1 if defined $Bar::a;
317 Bar::reenter() unless $init++;
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";