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