Use minimal @INC in tests, most of the time just '../lib',
[p5sagit/p5-mst-13.2.git] / t / op / sort.t
CommitLineData
a687059c 1#!./perl
2
9c007264 3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
9c007264 6}
9f1b1f2d 7use warnings;
8e664e10 8print "1..57\n";
a687059c 9
f63ceb1c 10# XXX known to leak scalars
9f1b1f2d 11{
12 no warnings 'uninitialized';
13 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
14}
f63ceb1c 15
71a29c3c 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}
24
9f1b1f2d 25sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
26sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 }
a687059c 27
9d116dd7 28my $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';
49print "# 1: x = '$x', expected = '$expected'\n";
50print ($x eq $expected ? "ok 1\n" : "not ok 1\n");
a687059c 51
9f1b1f2d 52$x = join('', sort( Backwards @harry));
9d116dd7 53$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
54print "# 2: x = '$x', expected = '$expected'\n";
55print ($x eq $expected ? "ok 2\n" : "not ok 2\n");
a687059c 56
9f1b1f2d 57$x = join('', sort( Backwards_stacked @harry));
43481408 58$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
59print "# 3: x = '$x', expected = '$expected'\n";
60print ($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' ;
43481408 66print "# 4: x = '$x', expected = '$expected'\n";
67print ($x eq $expected ?"ok 4\n":"not ok 4\n");
03a14243 68
69@a = ();
70@b = reverse @a;
43481408 71print ("@b" eq "" ? "ok 5\n" : "not ok 5 (@b)\n");
03a14243 72
73@a = (1);
74@b = reverse @a;
43481408 75print ("@b" eq "1" ? "ok 6\n" : "not ok 6 (@b)\n");
03a14243 76
77@a = (1,2);
78@b = reverse @a;
43481408 79print ("@b" eq "2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
03a14243 80
81@a = (1,2,3);
82@b = reverse @a;
43481408 83print ("@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;
43481408 87print ("@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;
43481408 91print ("@b" eq "2 3 4 10" ? "ok 10\n" : "not ok 10 (@b)\n");
988174c1 92
9f1b1f2d 93$sub = 'Backwards';
988174c1 94$x = join('', sort $sub @harry);
9d116dd7 95$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
43481408 96print "# 11: x = $x, expected = '$expected'\n";
97print ($x eq $expected ? "ok 11\n" : "not ok 11\n");
98
9f1b1f2d 99$sub = 'Backwards_stacked';
43481408 100$x = join('', sort $sub @harry);
101$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
102print "# 12: x = $x, expected = '$expected'\n";
103print ($x eq $expected ? "ok 12\n" : "not ok 12\n");
988174c1 104
cd5de442 105# literals, combinations
106
107@b = sort (4,1,3,2);
43481408 108print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
cd5de442 109print "# x = '@b'\n";
110
111@b = sort grep { $_ } (4,1,3,2);
43481408 112print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
cd5de442 113print "# x = '@b'\n";
114
115@b = sort map { $_ } (4,1,3,2);
43481408 116print ("@b" eq '1 2 3 4' ? "ok 15\n" : "not ok 15\n");
cd5de442 117print "# x = '@b'\n";
118
119@b = sort reverse (4,1,3,2);
43481408 120print ("@b" eq '1 2 3 4' ? "ok 16\n" : "not ok 16\n");
cd5de442 121print "# x = '@b'\n";
7bac28a0 122
7bac28a0 123# redefining sort sub inside the sort sub should fail
124sub twoface { *twoface = sub { $a <=> $b }; &twoface }
125eval { @b = sort twoface 4,1,3,2 };
43481408 126print ($@ =~ /redefine active sort/ ? "ok 17\n" : "not ok 17\n");
7bac28a0 127
128# redefining sort subs outside the sort should not fail
9f1b1f2d 129eval { no warnings 'redefine'; *twoface = sub { &Backwards } };
43481408 130print $@ ? "not ok 18\n" : "ok 18\n";
7bac28a0 131
132eval { @b = sort twoface 4,1,3,2 };
43481408 133print ("@b" eq '4 3 2 1' ? "ok 19\n" : "not ok 19 |@b|\n");
7bac28a0 134
9f1b1f2d 135{
136 no warnings 'redefine';
137 *twoface = sub { *twoface = *Backwards; $a <=> $b };
138}
7bac28a0 139eval { @b = sort twoface 4,1 };
43481408 140print ($@ =~ /redefine active sort/ ? "ok 20\n" : "not ok 20\n");
7bac28a0 141
9f1b1f2d 142{
143 no warnings 'redefine';
144 *twoface = sub {
7bac28a0 145 eval 'sub twoface { $a <=> $b }';
43481408 146 die($@ =~ /redefine active sort/ ? "ok 21\n" : "not ok 21\n");
7bac28a0 147 $a <=> $b;
148 };
9f1b1f2d 149}
7bac28a0 150eval { @b = sort twoface 4,1 };
43481408 151print $@ ? "$@" : "not ok 21\n";
15f0808c 152
153eval <<'CODE';
9f1b1f2d 154 my @result = sort main'Backwards 'one', 'two';
15f0808c 155CODE
43481408 156print $@ ? "not ok 22\n# $@" : "ok 22\n";
15f0808c 157
158eval <<'CODE';
159 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
160 my @result = sort 'one', 'two';
161CODE
43481408 162print $@ ? "not ok 23\n# $@" : "ok 23\n";
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;
43481408 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;
43481408 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;
43481408 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;
43481408 176 print ("@b" eq '4 3 2 1' ? "ok 27\n" : "not ok 27 |@b|\n");
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;
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{
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;
43481408 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;
43481408 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;
43481408 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;
43481408 206 print ("@b" eq '4 3 2 1' ? "ok 35\n" : "not ok 35 |@b|\n");
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;
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 );
5d4fa709 226@b = sort {
227 my $dummy; # force blockness
228 return $b <=> $a
229} @a;
43481408 230print ("@b" eq '1996 255 90 19 5' ? "ok 40\n" : "not ok 40\n");
9c007264 231print "# x = '@b'\n";
232$x = join('', sort { $a cmp $b } @harry);
233$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
43481408 234print ($x eq $expected ? "ok 41\n" : "not ok 41\n");
9c007264 235print "# x = '$x'; expected = '$expected'\n";
236$x = join('', sort { $b cmp $a } @harry);
237$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
43481408 238print ($x eq $expected ? "ok 42\n" : "not ok 42\n");
9c007264 239print "# x = '$x'; expected = '$expected'\n";
240{
241 use integer;
242 @b = sort { $a <=> $b } @a;
43481408 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;
43481408 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';
43481408 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';
43481408 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);
43481408 261print $x eq "123" ? "ok 47\n" : "not ok 47\n";
e507f050 262
9c007264 263# test sorting in non-main package
264package Foo;
265@a = ( 5, 19, 1996, 255, 90 );
266@b = sort { $b <=> $a } @a;
43481408 267print ("@b" eq '1996 255 90 19 5' ? "ok 48\n" : "not ok 48\n");
268print "# x = '@b'\n";
269
9f1b1f2d 270@b = sort main::Backwards_stacked @a;
43481408 271print ("@b" eq '90 5 255 1996 19' ? "ok 49\n" : "not ok 49\n");
9c007264 272print "# x = '@b'\n";
8e3f9bdf 273
274# check if context for sort arguments is handled right
275
276$test = 49;
277sub test_if_list {
278 my $gimme = wantarray;
279 print "not " unless $gimme;
280 ++$test;
281 print "ok $test\n";
282}
283my $m = sub { $a <=> $b };
284
285sub cxt_one { sort $m test_if_list() }
286cxt_one();
287sub cxt_two { sort { $a <=> $b } test_if_list() }
288cxt_two();
289sub cxt_three { sort &test_if_list() }
290cxt_three();
291
292sub 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;
300sub cxt_four { sort $m 1,2 }
301@x = cxt_four();
302sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 }
303@x = cxt_five();
304sub cxt_six { sort test_if_scalar 1,2 }
305@x = cxt_six();
8e664e10 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}