5 unshift @INC, '../lib';
9 # XXX known to leak scalars
10 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
12 sub backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
14 my $upperfirst = 'A' lt 'a';
16 # Beware: in future this may become hairier because of possible
17 # collation complications: qw(A a B c) can be sorted at least as
18 # any of the following
25 # All the above orders make sense.
27 # That said, EBCDIC sorts all small letters first, as opposed
28 # to ASCII which sorts all big letters first.
30 @harry = ('dog','cat','x','Cain','Abel');
31 @george = ('gone','chased','yz','punished','Axed');
33 $x = join('', sort @harry);
34 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
35 print "# 1: x = '$x', expected = '$expected'\n";
36 print ($x eq $expected ? "ok 1\n" : "not ok 1\n");
38 $x = join('', sort( backwards @harry));
39 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
40 print "# 2: x = '$x', expected = '$expected'\n";
41 print ($x eq $expected ? "ok 2\n" : "not ok 2\n");
43 $x = join('', sort @george, 'to', @harry);
44 $expected = $upperfirst ?
45 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
46 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
47 print "# 3: x = '$x', expected = '$expected'\n";
48 print ($x eq $expected ?"ok 3\n":"not ok 3\n");
52 print ("@b" eq "" ? "ok 4\n" : "not ok 4 (@b)\n");
56 print ("@b" eq "1" ? "ok 5\n" : "not ok 5 (@b)\n");
60 print ("@b" eq "2 1" ? "ok 6\n" : "not ok 6 (@b)\n");
64 print ("@b" eq "3 2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
68 print ("@b" eq "4 3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
71 @b = sort {$a <=> $b;} @a;
72 print ("@b" eq "2 3 4 10" ? "ok 9\n" : "not ok 9 (@b)\n");
75 $x = join('', sort $sub @harry);
76 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
77 print "# 10: x = $x, expected = '$expected'\n";
78 print ($x eq $expected ? "ok 10\n" : "not ok 10\n");
80 # literals, combinations
83 print ("@b" eq '1 2 3 4' ? "ok 11\n" : "not ok 11\n");
86 @b = sort grep { $_ } (4,1,3,2);
87 print ("@b" eq '1 2 3 4' ? "ok 12\n" : "not ok 12\n");
90 @b = sort map { $_ } (4,1,3,2);
91 print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
94 @b = sort reverse (4,1,3,2);
95 print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
99 # redefining sort sub inside the sort sub should fail
100 sub twoface { *twoface = sub { $a <=> $b }; &twoface }
101 eval { @b = sort twoface 4,1,3,2 };
102 print ($@ =~ /redefine active sort/ ? "ok 15\n" : "not ok 15\n");
104 # redefining sort subs outside the sort should not fail
105 eval { *twoface = sub { &backwards } };
106 print $@ ? "not ok 16\n" : "ok 16\n";
108 eval { @b = sort twoface 4,1,3,2 };
109 print ("@b" eq '4 3 2 1' ? "ok 17\n" : "not ok 17 |@b|\n");
111 *twoface = sub { *twoface = *backwards; $a <=> $b };
112 eval { @b = sort twoface 4,1 };
113 print ($@ =~ /redefine active sort/ ? "ok 18\n" : "not ok 18\n");
116 eval 'sub twoface { $a <=> $b }';
117 die($@ =~ /redefine active sort/ ? "ok 19\n" : "not ok 19\n");
120 eval { @b = sort twoface 4,1 };
121 print $@ ? "$@" : "not ok 19\n";
124 my @result = sort main'backwards 'one', 'two';
126 print $@ ? "not ok 20\n# $@" : "ok 20\n";
129 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
130 my @result = sort 'one', 'two';
132 print $@ ? "not ok 21\n# $@" : "ok 21\n";
135 my $sortsub = \&backwards;
136 my $sortglob = *backwards;
137 my $sortglobr = \*backwards;
138 my $sortname = 'backwards';
139 @b = sort $sortsub 4,1,3,2;
140 print ("@b" eq '4 3 2 1' ? "ok 22\n" : "not ok 22 |@b|\n");
141 @b = sort $sortglob 4,1,3,2;
142 print ("@b" eq '4 3 2 1' ? "ok 23\n" : "not ok 23 |@b|\n");
143 @b = sort $sortname 4,1,3,2;
144 print ("@b" eq '4 3 2 1' ? "ok 24\n" : "not ok 24 |@b|\n");
145 @b = sort $sortglobr 4,1,3,2;
146 print ("@b" eq '4 3 2 1' ? "ok 25\n" : "not ok 25 |@b|\n");
150 local $sortsub = \&backwards;
151 local $sortglob = *backwards;
152 local $sortglobr = \*backwards;
153 local $sortname = 'backwards';
154 @b = sort $sortsub 4,1,3,2;
155 print ("@b" eq '4 3 2 1' ? "ok 26\n" : "not ok 26 |@b|\n");
156 @b = sort $sortglob 4,1,3,2;
157 print ("@b" eq '4 3 2 1' ? "ok 27\n" : "not ok 27 |@b|\n");
158 @b = sort $sortname 4,1,3,2;
159 print ("@b" eq '4 3 2 1' ? "ok 28\n" : "not ok 28 |@b|\n");
160 @b = sort $sortglobr 4,1,3,2;
161 print ("@b" eq '4 3 2 1' ? "ok 29\n" : "not ok 29 |@b|\n");
164 ## exercise sort builtins... ($a <=> $b already tested)
165 @a = ( 5, 19, 1996, 255, 90 );
167 my $dummy; # force blockness
170 print ("@b" eq '1996 255 90 19 5' ? "ok 30\n" : "not ok 30\n");
171 print "# x = '@b'\n";
172 $x = join('', sort { $a cmp $b } @harry);
173 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
174 print ($x eq $expected ? "ok 31\n" : "not ok 31\n");
175 print "# x = '$x'; expected = '$expected'\n";
176 $x = join('', sort { $b cmp $a } @harry);
177 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
178 print ($x eq $expected ? "ok 32\n" : "not ok 32\n");
179 print "# x = '$x'; expected = '$expected'\n";
182 @b = sort { $a <=> $b } @a;
183 print ("@b" eq '5 19 90 255 1996' ? "ok 33\n" : "not ok 33\n");
184 print "# x = '@b'\n";
185 @b = sort { $b <=> $a } @a;
186 print ("@b" eq '1996 255 90 19 5' ? "ok 34\n" : "not ok 34\n");
187 print "# x = '@b'\n";
188 $x = join('', sort { $a cmp $b } @harry);
189 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
190 print ($x eq $expected ? "ok 35\n" : "not ok 35\n");
191 print "# x = '$x'; expected = '$expected'\n";
192 $x = join('', sort { $b cmp $a } @harry);
193 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
194 print ($x eq $expected ? "ok 36\n" : "not ok 36\n");
195 print "# x = '$x'; expected = '$expected'\n";
198 # test that an optimized-away comparison block doesn't take any other
199 # arguments away with it
200 $x = join('', sort { $a <=> $b } 3, 1, 2);
201 print $x eq "123" ? "ok 37\n" : "not ok 37\n";
203 # test sorting in non-main package
205 @a = ( 5, 19, 1996, 255, 90 );
206 @b = sort { $b <=> $a } @a;
207 print ("@b" eq '1996 255 90 19 5' ? "ok 38\n" : "not ok 38\n");
208 print "# x = '@b'\n";