1#!./perl 2 3BEGIN { 4 chdir 't' if -d 't'; 5 unshift @INC, '../lib'; 6} 7use warnings; 8print "1..49\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 25sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 } 26sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 } 27 28my $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'; 49print "# 1: x = '$x', expected = '$expected'\n"; 50print ($x eq $expected ? "ok 1\n" : "not ok 1\n"); 51 52$x = join('', sort( Backwards @harry)); 53$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; 54print "# 2: x = '$x', expected = '$expected'\n"; 55print ($x eq $expected ? "ok 2\n" : "not ok 2\n"); 56 57$x = join('', sort( Backwards_stacked @harry)); 58$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; 59print "# 3: x = '$x', expected = '$expected'\n"; 60print ($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' ; 66print "# 4: x = '$x', expected = '$expected'\n"; 67print ($x eq $expected ?"ok 4\n":"not ok 4\n"); 68 69@a = (); 70@b = reverse @a; 71print ("@b" eq "" ? "ok 5\n" : "not ok 5 (@b)\n"); 72 73@a = (1); 74@b = reverse @a; 75print ("@b" eq "1" ? "ok 6\n" : "not ok 6 (@b)\n"); 76 77@a = (1,2); 78@b = reverse @a; 79print ("@b" eq "2 1" ? "ok 7\n" : "not ok 7 (@b)\n"); 80 81@a = (1,2,3); 82@b = reverse @a; 83print ("@b" eq "3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n"); 84 85@a = (1,2,3,4); 86@b = reverse @a; 87print ("@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; 91print ("@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'; 96print "# 11: x = $x, expected = '$expected'\n"; 97print ($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'; 102print "# 12: x = $x, expected = '$expected'\n"; 103print ($x eq $expected ? "ok 12\n" : "not ok 12\n"); 104 105# literals, combinations 106 107@b = sort (4,1,3,2); 108print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n"); 109print "# x = '@b'\n"; 110 111@b = sort grep { $_ } (4,1,3,2); 112print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n"); 113print "# x = '@b'\n"; 114 115@b = sort map { $_ } (4,1,3,2); 116print ("@b" eq '1 2 3 4' ? "ok 15\n" : "not ok 15\n"); 117print "# x = '@b'\n"; 118 119@b = sort reverse (4,1,3,2); 120print ("@b" eq '1 2 3 4' ? "ok 16\n" : "not ok 16\n"); 121print "# x = '@b'\n"; 122 123# redefining sort sub inside the sort sub should fail 124sub twoface { *twoface = sub { $a <=> $b }; &twoface } 125eval { @b = sort twoface 4,1,3,2 }; 126print ($@ =~ /redefine active sort/ ? "ok 17\n" : "not ok 17\n"); 127 128# redefining sort subs outside the sort should not fail 129eval { no warnings 'redefine'; *twoface = sub { &Backwards } }; 130print $@ ? "not ok 18\n" : "ok 18\n"; 131 132eval { @b = sort twoface 4,1,3,2 }; 133print ("@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} 139eval { @b = sort twoface 4,1 }; 140print ($@ =~ /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} 150eval { @b = sort twoface 4,1 }; 151print $@ ? "$@" : "not ok 21\n"; 152 153eval <<'CODE'; 154 my @result = sort main'Backwards 'one', 'two'; 155CODE 156print $@ ? "not ok 22\n# $@" : "ok 22\n"; 157 158eval <<'CODE'; 159 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub 160 my @result = sort 'one', 'two'; 161CODE 162print $@ ? "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; 230print ("@b" eq '1996 255 90 19 5' ? "ok 40\n" : "not ok 40\n"); 231print "# x = '@b'\n"; 232$x = join('', sort { $a cmp $b } @harry); 233$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; 234print ($x eq $expected ? "ok 41\n" : "not ok 41\n"); 235print "# x = '$x'; expected = '$expected'\n"; 236$x = join('', sort { $b cmp $a } @harry); 237$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; 238print ($x eq $expected ? "ok 42\n" : "not ok 42\n"); 239print "# 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); 261print $x eq "123" ? "ok 47\n" : "not ok 47\n"; 262 263# test sorting in non-main package 264package Foo; 265@a = ( 5, 19, 1996, 255, 90 ); 266@b = sort { $b <=> $a } @a; 267print ("@b" eq '1996 255 90 19 5' ? "ok 48\n" : "not ok 48\n"); 268print "# x = '@b'\n"; 269 270@b = sort main::Backwards_stacked @a; 271print ("@b" eq '90 5 255 1996 19' ? "ok 49\n" : "not ok 49\n"); 272print "# x = '@b'\n"; 273