Commit | Line | Data |
a687059c |
1 | #!./perl |
2 | |
9c007264 |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
93430cb4 |
5 | unshift @INC, '../lib'; |
9c007264 |
6 | } |
146174a9 |
7 | print "1..49\n"; |
a687059c |
8 | |
f63ceb1c |
9 | # XXX known to leak scalars |
10 | $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3; |
11 | |
2f52a358 |
12 | sub backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 } |
146174a9 |
13 | sub backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 } |
a687059c |
14 | |
9d116dd7 |
15 | my $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 |
33 | |
34 | $x = join('', sort @harry); |
9d116dd7 |
35 | $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; |
36 | print "# 1: x = '$x', expected = '$expected'\n"; |
37 | print ($x eq $expected ? "ok 1\n" : "not ok 1\n"); |
a687059c |
38 | |
a0d0e21e |
39 | $x = join('', sort( backwards @harry)); |
9d116dd7 |
40 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
41 | print "# 2: x = '$x', expected = '$expected'\n"; |
42 | print ($x eq $expected ? "ok 2\n" : "not ok 2\n"); |
a687059c |
43 | |
146174a9 |
44 | $x = join('', sort( backwards_stacked @harry)); |
45 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
46 | print "# 3: x = '$x', expected = '$expected'\n"; |
47 | print ($x eq $expected ? "ok 3\n" : "not ok 3\n"); |
48 | |
a687059c |
49 | $x = join('', sort @george, 'to', @harry); |
9d116dd7 |
50 | $expected = $upperfirst ? |
51 | 'AbelAxedCaincatchaseddoggonepunishedtoxyz' : |
52 | 'catchaseddoggonepunishedtoxyzAbelAxedCain' ; |
146174a9 |
53 | print "# 4: x = '$x', expected = '$expected'\n"; |
54 | print ($x eq $expected ?"ok 4\n":"not ok 4\n"); |
03a14243 |
55 | |
56 | @a = (); |
57 | @b = reverse @a; |
146174a9 |
58 | print ("@b" eq "" ? "ok 5\n" : "not ok 5 (@b)\n"); |
03a14243 |
59 | |
60 | @a = (1); |
61 | @b = reverse @a; |
146174a9 |
62 | print ("@b" eq "1" ? "ok 6\n" : "not ok 6 (@b)\n"); |
03a14243 |
63 | |
64 | @a = (1,2); |
65 | @b = reverse @a; |
146174a9 |
66 | print ("@b" eq "2 1" ? "ok 7\n" : "not ok 7 (@b)\n"); |
03a14243 |
67 | |
68 | @a = (1,2,3); |
69 | @b = reverse @a; |
146174a9 |
70 | print ("@b" eq "3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n"); |
03a14243 |
71 | |
72 | @a = (1,2,3,4); |
73 | @b = reverse @a; |
146174a9 |
74 | print ("@b" eq "4 3 2 1" ? "ok 9\n" : "not ok 9 (@b)\n"); |
55204971 |
75 | |
76 | @a = (10,2,3,4); |
77 | @b = sort {$a <=> $b;} @a; |
146174a9 |
78 | print ("@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'; |
146174a9 |
83 | print "# 11: x = $x, expected = '$expected'\n"; |
84 | print ($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'; |
89 | print "# 12: x = $x, expected = '$expected'\n"; |
90 | print ($x eq $expected ? "ok 12\n" : "not ok 12\n"); |
988174c1 |
91 | |
cd5de442 |
92 | # literals, combinations |
93 | |
94 | @b = sort (4,1,3,2); |
146174a9 |
95 | print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n"); |
cd5de442 |
96 | print "# x = '@b'\n"; |
97 | |
98 | @b = sort grep { $_ } (4,1,3,2); |
146174a9 |
99 | print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n"); |
cd5de442 |
100 | print "# x = '@b'\n"; |
101 | |
102 | @b = sort map { $_ } (4,1,3,2); |
146174a9 |
103 | print ("@b" eq '1 2 3 4' ? "ok 15\n" : "not ok 15\n"); |
cd5de442 |
104 | print "# x = '@b'\n"; |
105 | |
106 | @b = sort reverse (4,1,3,2); |
146174a9 |
107 | print ("@b" eq '1 2 3 4' ? "ok 16\n" : "not ok 16\n"); |
cd5de442 |
108 | print "# x = '@b'\n"; |
7bac28a0 |
109 | |
110 | $^W = 0; |
111 | # redefining sort sub inside the sort sub should fail |
112 | sub twoface { *twoface = sub { $a <=> $b }; &twoface } |
113 | eval { @b = sort twoface 4,1,3,2 }; |
146174a9 |
114 | print ($@ =~ /redefine active sort/ ? "ok 17\n" : "not ok 17\n"); |
7bac28a0 |
115 | |
116 | # redefining sort subs outside the sort should not fail |
117 | eval { *twoface = sub { &backwards } }; |
146174a9 |
118 | print $@ ? "not ok 18\n" : "ok 18\n"; |
7bac28a0 |
119 | |
120 | eval { @b = sort twoface 4,1,3,2 }; |
146174a9 |
121 | print ("@b" eq '4 3 2 1' ? "ok 19\n" : "not ok 19 |@b|\n"); |
7bac28a0 |
122 | |
123 | *twoface = sub { *twoface = *backwards; $a <=> $b }; |
124 | eval { @b = sort twoface 4,1 }; |
146174a9 |
125 | print ($@ =~ /redefine active sort/ ? "ok 20\n" : "not ok 20\n"); |
7bac28a0 |
126 | |
127 | *twoface = sub { |
128 | eval 'sub twoface { $a <=> $b }'; |
146174a9 |
129 | die($@ =~ /redefine active sort/ ? "ok 21\n" : "not ok 21\n"); |
7bac28a0 |
130 | $a <=> $b; |
131 | }; |
132 | eval { @b = sort twoface 4,1 }; |
146174a9 |
133 | print $@ ? "$@" : "not ok 21\n"; |
15f0808c |
134 | |
135 | eval <<'CODE'; |
136 | my @result = sort main'backwards 'one', 'two'; |
137 | CODE |
146174a9 |
138 | print $@ ? "not ok 22\n# $@" : "ok 22\n"; |
15f0808c |
139 | |
140 | eval <<'CODE'; |
141 | # "sort 'one', 'two'" should not try to parse "'one" as a sort sub |
142 | my @result = sort 'one', 'two'; |
143 | CODE |
146174a9 |
144 | print $@ ? "not ok 23\n# $@" : "ok 23\n"; |
c6e96bcb |
145 | |
146 | { |
147 | my $sortsub = \&backwards; |
148 | my $sortglob = *backwards; |
62f274bf |
149 | my $sortglobr = \*backwards; |
c6e96bcb |
150 | my $sortname = 'backwards'; |
151 | @b = sort $sortsub 4,1,3,2; |
146174a9 |
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; |
146174a9 |
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; |
146174a9 |
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; |
146174a9 |
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 |
174 | } |
175 | |
176 | { |
177 | local $sortsub = \&backwards; |
178 | local $sortglob = *backwards; |
62f274bf |
179 | local $sortglobr = \*backwards; |
c6e96bcb |
180 | local $sortname = 'backwards'; |
181 | @b = sort $sortsub 4,1,3,2; |
146174a9 |
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; |
146174a9 |
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; |
146174a9 |
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; |
146174a9 |
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 |
204 | } |
205 | |
9c007264 |
206 | ## exercise sort builtins... ($a <=> $b already tested) |
207 | @a = ( 5, 19, 1996, 255, 90 ); |
146174a9 |
208 | @b = sort { |
209 | my $dummy; # force blockness |
210 | return $b <=> $a |
211 | } @a; |
212 | print ("@b" eq '1996 255 90 19 5' ? "ok 40\n" : "not ok 40\n"); |
9c007264 |
213 | print "# x = '@b'\n"; |
214 | $x = join('', sort { $a cmp $b } @harry); |
215 | $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; |
146174a9 |
216 | print ($x eq $expected ? "ok 41\n" : "not ok 41\n"); |
9c007264 |
217 | print "# x = '$x'; expected = '$expected'\n"; |
218 | $x = join('', sort { $b cmp $a } @harry); |
219 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
146174a9 |
220 | print ($x eq $expected ? "ok 42\n" : "not ok 42\n"); |
9c007264 |
221 | print "# x = '$x'; expected = '$expected'\n"; |
222 | { |
223 | use integer; |
224 | @b = sort { $a <=> $b } @a; |
146174a9 |
225 | print ("@b" eq '5 19 90 255 1996' ? "ok 43\n" : "not ok 43\n"); |
9c007264 |
226 | print "# x = '@b'\n"; |
227 | @b = sort { $b <=> $a } @a; |
146174a9 |
228 | print ("@b" eq '1996 255 90 19 5' ? "ok 44\n" : "not ok 44\n"); |
9c007264 |
229 | print "# x = '@b'\n"; |
230 | $x = join('', sort { $a cmp $b } @harry); |
231 | $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; |
146174a9 |
232 | print ($x eq $expected ? "ok 45\n" : "not ok 45\n"); |
9c007264 |
233 | print "# x = '$x'; expected = '$expected'\n"; |
234 | $x = join('', sort { $b cmp $a } @harry); |
235 | $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; |
146174a9 |
236 | print ($x eq $expected ? "ok 46\n" : "not ok 46\n"); |
9c007264 |
237 | print "# x = '$x'; expected = '$expected'\n"; |
238 | } |
e507f050 |
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); |
146174a9 |
243 | print $x eq "123" ? "ok 47\n" : "not ok 47\n"; |
e507f050 |
244 | |
9c007264 |
245 | # test sorting in non-main package |
246 | package Foo; |
247 | @a = ( 5, 19, 1996, 255, 90 ); |
248 | @b = sort { $b <=> $a } @a; |
146174a9 |
249 | print ("@b" eq '1996 255 90 19 5' ? "ok 48\n" : "not ok 48\n"); |
250 | print "# x = '@b'\n"; |
251 | |
252 | @b = sort main::backwards_stacked @a; |
253 | print ("@b" eq '90 5 255 1996 19' ? "ok 49\n" : "not ok 49\n"); |
9c007264 |
254 | print "# x = '@b'\n"; |