This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
updates from PodParser v1.12 on CPAN (from Brad Appleton)
[perl5.git] / t / op / sort.t
CommitLineData
a687059c
LW
1#!./perl
2
9c007264
JH
3BEGIN {
4 chdir 't' if -d 't';
93430cb4 5 unshift @INC, '../lib';
9c007264 6}
43481408 7print "1..49\n";
a687059c 8
f63ceb1c
GS
9# XXX known to leak scalars
10$ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
11
2f52a358 12sub backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
43481408 13sub backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 }
a687059c 14
9d116dd7
JH
15my $upperfirst = 'A' lt 'a';
16
17# Beware: in future this may become hairier because of possible
18# collation complications: qw(A a B c) can be sorted at least as
19# any of the following
20#
21# A a B b
22# A B a b
23# a b A B
24# a A b B
25#
26# All the above orders make sense.
27#
28# That said, EBCDIC sorts all small letters first, as opposed
29# to ASCII which sorts all big letters first.
30
a687059c 31@harry = ('dog','cat','x','Cain','Abel');
2f52a358 32@george = ('gone','chased','yz','punished','Axed');
a687059c
LW
33
34$x = join('', sort @harry);
9d116dd7
JH
35$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
36print "# 1: x = '$x', expected = '$expected'\n";
37print ($x eq $expected ? "ok 1\n" : "not ok 1\n");
a687059c 38
a0d0e21e 39$x = join('', sort( backwards @harry));
9d116dd7
JH
40$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
41print "# 2: x = '$x', expected = '$expected'\n";
42print ($x eq $expected ? "ok 2\n" : "not ok 2\n");
a687059c 43
43481408
GS
44$x = join('', sort( backwards_stacked @harry));
45$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
46print "# 3: x = '$x', expected = '$expected'\n";
47print ($x eq $expected ? "ok 3\n" : "not ok 3\n");
48
a687059c 49$x = join('', sort @george, 'to', @harry);
9d116dd7
JH
50$expected = $upperfirst ?
51 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
52 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
43481408
GS
53print "# 4: x = '$x', expected = '$expected'\n";
54print ($x eq $expected ?"ok 4\n":"not ok 4\n");
03a14243
LW
55
56@a = ();
57@b = reverse @a;
43481408 58print ("@b" eq "" ? "ok 5\n" : "not ok 5 (@b)\n");
03a14243
LW
59
60@a = (1);
61@b = reverse @a;
43481408 62print ("@b" eq "1" ? "ok 6\n" : "not ok 6 (@b)\n");
03a14243
LW
63
64@a = (1,2);
65@b = reverse @a;
43481408 66print ("@b" eq "2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
03a14243
LW
67
68@a = (1,2,3);
69@b = reverse @a;
43481408 70print ("@b" eq "3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
03a14243
LW
71
72@a = (1,2,3,4);
73@b = reverse @a;
43481408 74print ("@b" eq "4 3 2 1" ? "ok 9\n" : "not ok 9 (@b)\n");
55204971
LW
75
76@a = (10,2,3,4);
77@b = sort {$a <=> $b;} @a;
43481408 78print ("@b" eq "2 3 4 10" ? "ok 10\n" : "not ok 10 (@b)\n");
988174c1 79
463ee0b2 80$sub = 'backwards';
988174c1 81$x = join('', sort $sub @harry);
9d116dd7 82$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
43481408
GS
83print "# 11: x = $x, expected = '$expected'\n";
84print ($x eq $expected ? "ok 11\n" : "not ok 11\n");
85
86$sub = 'backwards_stacked';
87$x = join('', sort $sub @harry);
88$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
89print "# 12: x = $x, expected = '$expected'\n";
90print ($x eq $expected ? "ok 12\n" : "not ok 12\n");
988174c1 91
cd5de442
GS
92# literals, combinations
93
94@b = sort (4,1,3,2);
43481408 95print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
cd5de442
GS
96print "# x = '@b'\n";
97
98@b = sort grep { $_ } (4,1,3,2);
43481408 99print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
cd5de442
GS
100print "# x = '@b'\n";
101
102@b = sort map { $_ } (4,1,3,2);
43481408 103print ("@b" eq '1 2 3 4' ? "ok 15\n" : "not ok 15\n");
cd5de442
GS
104print "# x = '@b'\n";
105
106@b = sort reverse (4,1,3,2);
43481408 107print ("@b" eq '1 2 3 4' ? "ok 16\n" : "not ok 16\n");
cd5de442 108print "# x = '@b'\n";
7bac28a0
PP
109
110$^W = 0;
111# redefining sort sub inside the sort sub should fail
112sub twoface { *twoface = sub { $a <=> $b }; &twoface }
113eval { @b = sort twoface 4,1,3,2 };
43481408 114print ($@ =~ /redefine active sort/ ? "ok 17\n" : "not ok 17\n");
7bac28a0
PP
115
116# redefining sort subs outside the sort should not fail
117eval { *twoface = sub { &backwards } };
43481408 118print $@ ? "not ok 18\n" : "ok 18\n";
7bac28a0
PP
119
120eval { @b = sort twoface 4,1,3,2 };
43481408 121print ("@b" eq '4 3 2 1' ? "ok 19\n" : "not ok 19 |@b|\n");
7bac28a0
PP
122
123*twoface = sub { *twoface = *backwards; $a <=> $b };
124eval { @b = sort twoface 4,1 };
43481408 125print ($@ =~ /redefine active sort/ ? "ok 20\n" : "not ok 20\n");
7bac28a0
PP
126
127*twoface = sub {
128 eval 'sub twoface { $a <=> $b }';
43481408 129 die($@ =~ /redefine active sort/ ? "ok 21\n" : "not ok 21\n");
7bac28a0
PP
130 $a <=> $b;
131 };
132eval { @b = sort twoface 4,1 };
43481408 133print $@ ? "$@" : "not ok 21\n";
15f0808c
GS
134
135eval <<'CODE';
136 my @result = sort main'backwards 'one', 'two';
137CODE
43481408 138print $@ ? "not ok 22\n# $@" : "ok 22\n";
15f0808c
GS
139
140eval <<'CODE';
141 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
142 my @result = sort 'one', 'two';
143CODE
43481408 144print $@ ? "not ok 23\n# $@" : "ok 23\n";
c6e96bcb
GS
145
146{
147 my $sortsub = \&backwards;
148 my $sortglob = *backwards;
62f274bf 149 my $sortglobr = \*backwards;
c6e96bcb
GS
150 my $sortname = 'backwards';
151 @b = sort $sortsub 4,1,3,2;
43481408 152 print ("@b" eq '4 3 2 1' ? "ok 24\n" : "not ok 24 |@b|\n");
c6e96bcb 153 @b = sort $sortglob 4,1,3,2;
43481408 154 print ("@b" eq '4 3 2 1' ? "ok 25\n" : "not ok 25 |@b|\n");
c6e96bcb 155 @b = sort $sortname 4,1,3,2;
43481408 156 print ("@b" eq '4 3 2 1' ? "ok 26\n" : "not ok 26 |@b|\n");
62f274bf 157 @b = sort $sortglobr 4,1,3,2;
43481408
GS
158 print ("@b" eq '4 3 2 1' ? "ok 27\n" : "not ok 27 |@b|\n");
159}
160
161{
162 my $sortsub = \&backwards_stacked;
163 my $sortglob = *backwards_stacked;
164 my $sortglobr = \*backwards_stacked;
165 my $sortname = 'backwards_stacked';
166 @b = sort $sortsub 4,1,3,2;
167 print ("@b" eq '4 3 2 1' ? "ok 28\n" : "not ok 28 |@b|\n");
168 @b = sort $sortglob 4,1,3,2;
169 print ("@b" eq '4 3 2 1' ? "ok 29\n" : "not ok 29 |@b|\n");
170 @b = sort $sortname 4,1,3,2;
171 print ("@b" eq '4 3 2 1' ? "ok 30\n" : "not ok 30 |@b|\n");
172 @b = sort $sortglobr 4,1,3,2;
173 print ("@b" eq '4 3 2 1' ? "ok 31\n" : "not ok 31 |@b|\n");
c6e96bcb
GS
174}
175
176{
177 local $sortsub = \&backwards;
178 local $sortglob = *backwards;
62f274bf 179 local $sortglobr = \*backwards;
c6e96bcb
GS
180 local $sortname = 'backwards';
181 @b = sort $sortsub 4,1,3,2;
43481408 182 print ("@b" eq '4 3 2 1' ? "ok 32\n" : "not ok 32 |@b|\n");
c6e96bcb 183 @b = sort $sortglob 4,1,3,2;
43481408 184 print ("@b" eq '4 3 2 1' ? "ok 33\n" : "not ok 33 |@b|\n");
c6e96bcb 185 @b = sort $sortname 4,1,3,2;
43481408 186 print ("@b" eq '4 3 2 1' ? "ok 34\n" : "not ok 34 |@b|\n");
62f274bf 187 @b = sort $sortglobr 4,1,3,2;
43481408
GS
188 print ("@b" eq '4 3 2 1' ? "ok 35\n" : "not ok 35 |@b|\n");
189}
190
191{
192 local $sortsub = \&backwards_stacked;
193 local $sortglob = *backwards_stacked;
194 local $sortglobr = \*backwards_stacked;
195 local $sortname = 'backwards_stacked';
196 @b = sort $sortsub 4,1,3,2;
197 print ("@b" eq '4 3 2 1' ? "ok 36\n" : "not ok 36 |@b|\n");
198 @b = sort $sortglob 4,1,3,2;
199 print ("@b" eq '4 3 2 1' ? "ok 37\n" : "not ok 37 |@b|\n");
200 @b = sort $sortname 4,1,3,2;
201 print ("@b" eq '4 3 2 1' ? "ok 38\n" : "not ok 38 |@b|\n");
202 @b = sort $sortglobr 4,1,3,2;
203 print ("@b" eq '4 3 2 1' ? "ok 39\n" : "not ok 39 |@b|\n");
c6e96bcb
GS
204}
205
9c007264
JH
206## exercise sort builtins... ($a <=> $b already tested)
207@a = ( 5, 19, 1996, 255, 90 );
5d4fa709
GS
208@b = sort {
209 my $dummy; # force blockness
210 return $b <=> $a
211} @a;
43481408 212print ("@b" eq '1996 255 90 19 5' ? "ok 40\n" : "not ok 40\n");
9c007264
JH
213print "# x = '@b'\n";
214$x = join('', sort { $a cmp $b } @harry);
215$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
43481408 216print ($x eq $expected ? "ok 41\n" : "not ok 41\n");
9c007264
JH
217print "# x = '$x'; expected = '$expected'\n";
218$x = join('', sort { $b cmp $a } @harry);
219$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
43481408 220print ($x eq $expected ? "ok 42\n" : "not ok 42\n");
9c007264
JH
221print "# x = '$x'; expected = '$expected'\n";
222{
223 use integer;
224 @b = sort { $a <=> $b } @a;
43481408 225 print ("@b" eq '5 19 90 255 1996' ? "ok 43\n" : "not ok 43\n");
9c007264
JH
226 print "# x = '@b'\n";
227 @b = sort { $b <=> $a } @a;
43481408 228 print ("@b" eq '1996 255 90 19 5' ? "ok 44\n" : "not ok 44\n");
9c007264
JH
229 print "# x = '@b'\n";
230 $x = join('', sort { $a cmp $b } @harry);
231 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
43481408 232 print ($x eq $expected ? "ok 45\n" : "not ok 45\n");
9c007264
JH
233 print "# x = '$x'; expected = '$expected'\n";
234 $x = join('', sort { $b cmp $a } @harry);
235 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
43481408 236 print ($x eq $expected ? "ok 46\n" : "not ok 46\n");
9c007264
JH
237 print "# x = '$x'; expected = '$expected'\n";
238}
e507f050
SM
239
240# test that an optimized-away comparison block doesn't take any other
241# arguments away with it
242$x = join('', sort { $a <=> $b } 3, 1, 2);
43481408 243print $x eq "123" ? "ok 47\n" : "not ok 47\n";
e507f050 244
9c007264
JH
245# test sorting in non-main package
246package Foo;
247@a = ( 5, 19, 1996, 255, 90 );
248@b = sort { $b <=> $a } @a;
43481408
GS
249print ("@b" eq '1996 255 90 19 5' ? "ok 48\n" : "not ok 48\n");
250print "# x = '@b'\n";
251
252@b = sort main::backwards_stacked @a;
253print ("@b" eq '90 5 255 1996 19' ? "ok 49\n" : "not ok 49\n");
9c007264 254print "# x = '@b'\n";