This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
SYN SYN
[perl5.git] / t / op / sort.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7 use warnings;
8 print "1..57\n";
9
10 # XXX known to leak scalars
11 {
12   no warnings 'uninitialized';
13   $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
14 }
15
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
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 }
27
28 my $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
44 @harry = ('dog','cat','x','Cain','Abel');
45 @george = ('gone','chased','yz','punished','Axed');
46
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");
51
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");
56
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");
61
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");
68
69 @a = ();
70 @b = reverse @a;
71 print ("@b" eq "" ? "ok 5\n" : "not ok 5 (@b)\n");
72
73 @a = (1);
74 @b = reverse @a;
75 print ("@b" eq "1" ? "ok 6\n" : "not ok 6 (@b)\n");
76
77 @a = (1,2);
78 @b = reverse @a;
79 print ("@b" eq "2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
80
81 @a = (1,2,3);
82 @b = reverse @a;
83 print ("@b" eq "3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
84
85 @a = (1,2,3,4);
86 @b = reverse @a;
87 print ("@b" eq "4 3 2 1" ? "ok 9\n" : "not ok 9 (@b)\n");
88
89 @a = (10,2,3,4);
90 @b = sort {$a <=> $b;} @a;
91 print ("@b" eq "2 3 4 10" ? "ok 10\n" : "not ok 10 (@b)\n");
92
93 $sub = 'Backwards';
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");
98
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");
104
105 # literals, combinations
106
107 @b = sort (4,1,3,2);
108 print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
109 print "# x = '@b'\n";
110
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";
114
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";
118
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";
122
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");
127
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";
131
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");
134
135 {
136   no warnings 'redefine';
137   *twoface = sub { *twoface = *Backwards; $a <=> $b };
138 }
139 eval { @b = sort twoface 4,1 };
140 print ($@ =~ /redefine active sort/ ? "ok 20\n" : "not ok 20\n");
141
142 {
143   no warnings 'redefine';
144   *twoface = sub {
145                  eval 'sub twoface { $a <=> $b }';
146                  die($@ =~ /redefine active sort/ ? "ok 21\n" : "not ok 21\n");
147                  $a <=> $b;
148                };
149 }
150 eval { @b = sort twoface 4,1 };
151 print $@ ? "$@" : "not ok 21\n";
152
153 eval <<'CODE';
154     my @result = sort main'Backwards 'one', 'two';
155 CODE
156 print $@ ? "not ok 22\n# $@" : "ok 22\n";
157
158 eval <<'CODE';
159     # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
160     my @result = sort 'one', 'two';
161 CODE
162 print $@ ? "not ok 23\n# $@" : "ok 23\n";
163
164 {
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");
177 }
178
179 {
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");
192 }
193
194 {
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");
207 }
208
209 {
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");
222 }
223
224 ## exercise sort builtins... ($a <=> $b already tested)
225 @a = ( 5, 19, 1996, 255, 90 );
226 @b = sort {
227     my $dummy;          # force blockness
228     return $b <=> $a
229 } @a;
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";
240 {
241     use integer;
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";
256 }
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);
261 print $x eq "123" ? "ok 47\n" : "not ok 47\n";
262
263 # test sorting in non-main package
264 package Foo;
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";
269
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";
273
274 # check if context for sort arguments is handled right
275
276 $test = 49;
277 sub test_if_list {
278     my $gimme = wantarray;
279     print "not " unless $gimme;
280     ++$test;
281     print "ok $test\n";
282 }
283 my $m = sub { $a <=> $b };
284
285 sub cxt_one { sort $m test_if_list() }
286 cxt_one();
287 sub cxt_two { sort { $a <=> $b } test_if_list() }
288 cxt_two();
289 sub cxt_three { sort &test_if_list() }
290 cxt_three();
291
292 sub 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;
300 sub cxt_four { sort $m 1,2 }
301 @x = cxt_four();
302 sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 }
303 @x = cxt_five();
304 sub cxt_six { sort test_if_scalar 1,2 }
305 @x = cxt_six();
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 }