1#!./perl 2 3BEGIN { 4 chdir 't' if -d 't'; 5 @INC = qw(. ../lib); 6 require 'test.pl'; 7} 8use warnings; 9plan( tests => 148 ); 10 11# these shouldn't hang 12{ 13 no warnings; 14 sort { for ($_ = 0;; $_++) {} } @a; 15 sort { while(1) {} } @a; 16 sort { while(1) { last; } } @a; 17 sort { while(0) { last; } } @a; 18 19 # Change 26011: Re: A surprising segfault 20 map scalar(sort(+())), ('')x68; 21} 22 23sub Backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 } 24sub Backwards_stacked($$) { my($a,$b) = @_; $a lt $b ? 1 : $a gt $b ? -1 : 0 } 25sub Backwards_other { $a lt $b ? 1 : $a gt $b ? -1 : 0 } 26 27my $upperfirst = 'A' lt 'a'; 28 29# Beware: in future this may become hairier because of possible 30# collation complications: qw(A a B b) can be sorted at least as 31# any of the following 32# 33# A a B b 34# A B a b 35# a b A B 36# a A b B 37# 38# All the above orders make sense. 39# 40# That said, EBCDIC sorts all small letters first, as opposed 41# to ASCII which sorts all big letters first. 42 43@harry = ('dog','cat','x','Cain','Abel'); 44@george = ('gone','chased','yz','punished','Axed'); 45 46$x = join('', sort @harry); 47$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; 48 49cmp_ok($x,'eq',$expected,'upper first 1'); 50 51$x = join('', sort( Backwards @harry)); 52$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; 53 54cmp_ok($x,'eq',$expected,'upper first 2'); 55 56$x = join('', sort( Backwards_stacked @harry)); 57$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; 58 59cmp_ok($x,'eq',$expected,'upper first 3'); 60 61$x = join('', sort @george, 'to', @harry); 62$expected = $upperfirst ? 63 'AbelAxedCaincatchaseddoggonepunishedtoxyz' : 64 'catchaseddoggonepunishedtoxyzAbelAxedCain' ; 65 66cmp_ok($x,'eq',$expected,'upper first 4'); 67$" = ' '; 68@a = (); 69@b = reverse @a; 70cmp_ok("@b",'eq',"",'reverse 1'); 71 72@a = (1); 73@b = reverse @a; 74cmp_ok("@b",'eq',"1",'reverse 2'); 75 76@a = (1,2); 77@b = reverse @a; 78cmp_ok("@b",'eq',"2 1",'reverse 3'); 79 80@a = (1,2,3); 81@b = reverse @a; 82cmp_ok("@b",'eq',"3 2 1",'reverse 4'); 83 84@a = (1,2,3,4); 85@b = reverse @a; 86cmp_ok("@b",'eq',"4 3 2 1",'reverse 5'); 87 88@a = (10,2,3,4); 89@b = sort {$a <=> $b;} @a; 90cmp_ok("@b",'eq',"2 3 4 10",'sort numeric'); 91 92$sub = 'Backwards'; 93$x = join('', sort $sub @harry); 94$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; 95 96cmp_ok($x,'eq',$expected,'sorter sub name in var 1'); 97 98$sub = 'Backwards_stacked'; 99$x = join('', sort $sub @harry); 100$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; 101 102cmp_ok($x,'eq',$expected,'sorter sub name in var 2'); 103 104# literals, combinations 105 106@b = sort (4,1,3,2); 107cmp_ok("@b",'eq','1 2 3 4','just sort'); 108 109 110@b = sort grep { $_ } (4,1,3,2); 111cmp_ok("@b",'eq','1 2 3 4','grep then sort'); 112 113 114@b = sort map { $_ } (4,1,3,2); 115cmp_ok("@b",'eq','1 2 3 4','map then sort'); 116 117 118@b = sort reverse (4,1,3,2); 119cmp_ok("@b",'eq','1 2 3 4','reverse then sort'); 120 121 122 123sub twoface { no warnings 'redefine'; *twoface = sub { $a <=> $b }; &twoface } 124eval { @b = sort twoface 4,1,3,2 }; 125cmp_ok("@b",'eq','1 2 3 4','redefine sort sub inside the sort sub'); 126 127 128eval { no warnings 'redefine'; *twoface = sub { &Backwards } }; 129ok(!$@,"redefining sort subs outside the sort \$@=[$@]"); 130 131eval { @b = sort twoface 4,1,3,2 }; 132cmp_ok("@b",'eq','4 3 2 1','twoface redefinition'); 133 134{ 135 no warnings 'redefine'; 136 *twoface = sub { *twoface = *Backwards_other; $a <=> $b }; 137} 138 139eval { @b = sort twoface 4,1,9,5 }; 140ok(($@ eq "" && "@b" eq "1 4 5 9"),'redefinition should not take effect during the sort'); 141 142{ 143 no warnings 'redefine'; 144 *twoface = sub { 145 eval 'sub twoface { $a <=> $b }'; 146 die($@ eq "" ? "good\n" : "bad\n"); 147 $a <=> $b; 148 }; 149} 150eval { @b = sort twoface 4,1 }; 151cmp_ok(substr($@,0,4), 'eq', 'good', 'twoface eval'); 152 153eval <<'CODE'; 154 my @result = sort main'Backwards 'one', 'two'; 155CODE 156cmp_ok($@,'eq','',q(old skool package)); 157 158eval <<'CODE'; 159 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub 160 my @result = sort 'one', 'two'; 161CODE 162cmp_ok($@,'eq','',q(one is not a sub)); 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 cmp_ok("@b",'eq','4 3 2 1','sortname 1'); 171 @b = sort $sortglob 4,1,3,2; 172 cmp_ok("@b",'eq','4 3 2 1','sortname 2'); 173 @b = sort $sortname 4,1,3,2; 174 cmp_ok("@b",'eq','4 3 2 1','sortname 3'); 175 @b = sort $sortglobr 4,1,3,2; 176 cmp_ok("@b",'eq','4 3 2 1','sortname 4'); 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 cmp_ok("@b",'eq','4 3 2 1','sortname 5'); 186 @b = sort $sortglob 4,1,3,2; 187 cmp_ok("@b",'eq','4 3 2 1','sortname 6'); 188 @b = sort $sortname 4,1,3,2; 189 cmp_ok("@b",'eq','4 3 2 1','sortname 7'); 190 @b = sort $sortglobr 4,1,3,2; 191 cmp_ok("@b",'eq','4 3 2 1','sortname 8'); 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 cmp_ok("@b",'eq','4 3 2 1','sortname local 1'); 201 @b = sort $sortglob 4,1,3,2; 202 cmp_ok("@b",'eq','4 3 2 1','sortname local 2'); 203 @b = sort $sortname 4,1,3,2; 204 cmp_ok("@b",'eq','4 3 2 1','sortname local 3'); 205 @b = sort $sortglobr 4,1,3,2; 206 cmp_ok("@b",'eq','4 3 2 1','sortname local 4'); 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 cmp_ok("@b",'eq','4 3 2 1','sortname local 5'); 216 @b = sort $sortglob 4,1,3,2; 217 cmp_ok("@b",'eq','4 3 2 1','sortname local 6'); 218 @b = sort $sortname 4,1,3,2; 219 cmp_ok("@b",'eq','4 3 2 1','sortname local 7'); 220 @b = sort $sortglobr 4,1,3,2; 221 cmp_ok("@b",'eq','4 3 2 1','sortname local 8'); 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; 230cmp_ok("@b",'eq','1996 255 90 19 5','force blockness'); 231 232$x = join('', sort { $a cmp $b } @harry); 233$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; 234cmp_ok($x,'eq',$expected,'a cmp b'); 235 236$x = join('', sort { $b cmp $a } @harry); 237$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; 238cmp_ok($x,'eq',$expected,'b cmp a'); 239 240{ 241 use integer; 242 @b = sort { $a <=> $b } @a; 243 cmp_ok("@b",'eq','5 19 90 255 1996','integer a <=> b'); 244 245 @b = sort { $b <=> $a } @a; 246 cmp_ok("@b",'eq','1996 255 90 19 5','integer b <=> a'); 247 248 $x = join('', sort { $a cmp $b } @harry); 249 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain'; 250 cmp_ok($x,'eq',$expected,'integer a cmp b'); 251 252 $x = join('', sort { $b cmp $a } @harry); 253 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat'; 254 cmp_ok($x,'eq',$expected,'integer b cmp a'); 255 256} 257 258 259 260$x = join('', sort { $a <=> $b } 3, 1, 2); 261cmp_ok($x,'eq','123',q(optimized-away comparison block doesn't take any other arguments away with it)); 262 263# test sorting in non-main package 264{ 265 package Foo; 266 @a = ( 5, 19, 1996, 255, 90 ); 267 @b = sort { $b <=> $a } @a; 268 ::cmp_ok("@b",'eq','1996 255 90 19 5','not in main:: 1'); 269 270 @b = sort ::Backwards_stacked @a; 271 ::cmp_ok("@b",'eq','90 5 255 1996 19','not in main:: 2'); 272 273 # check if context for sort arguments is handled right 274 sub test_if_list { 275 my $gimme = wantarray; 276 ::is($gimme,1,'wantarray 1'); 277 } 278 my $m = sub { $a <=> $b }; 279 280 sub cxt_one { sort $m test_if_list() } 281 cxt_one(); 282 sub cxt_two { sort { $a <=> $b } test_if_list() } 283 cxt_two(); 284 sub cxt_three { sort &test_if_list() } 285 cxt_three(); 286 287 sub test_if_scalar { 288 my $gimme = wantarray; 289 ::is(!($gimme or !defined($gimme)),1,'wantarray 2'); 290 } 291 292 $m = \&test_if_scalar; 293 sub cxt_four { sort $m 1,2 } 294 @x = cxt_four(); 295 sub cxt_five { sort { test_if_scalar($a,$b); } 1,2 } 296 @x = cxt_five(); 297 sub cxt_six { sort test_if_scalar 1,2 } 298 @x = cxt_six(); 299} 300 301 302# test against a reentrancy bug 303{ 304 package Bar; 305 sub compare { $a cmp $b } 306 sub reenter { my @force = sort compare qw/a b/ } 307} 308{ 309 my($def, $init) = (0, 0); 310 @b = sort { 311 $def = 1 if defined $Bar::a; 312 Bar::reenter() unless $init++; 313 $a <=> $b 314 } qw/4 3 1 2/; 315 cmp_ok("@b",'eq','1 2 3 4','reenter 1'); 316 317 ok(!$def,'reenter 2'); 318} 319 320 321{ 322 sub routine { "one", "two" }; 323 @a = sort(routine(1)); 324 cmp_ok("@a",'eq',"one two",'bug id 19991001.003'); 325} 326 327 328# check for in-place optimisation of @a = sort @a 329{ 330 my ($r1,$r2,@a); 331 our @g; 332 @g = (3,2,1); $r1 = \$g[2]; @g = sort @g; $r2 = \$g[0]; 333 is "$r1-@g", "$r2-1 2 3", "inplace sort of global"; 334 335 @a = qw(b a c); $r1 = \$a[1]; @a = sort @a; $r2 = \$a[0]; 336 is "$r1-@a", "$r2-a b c", "inplace sort of lexical"; 337 338 @g = (2,3,1); $r1 = \$g[1]; @g = sort { $b <=> $a } @g; $r2 = \$g[0]; 339 is "$r1-@g", "$r2-3 2 1", "inplace reversed sort of global"; 340 341 @g = (2,3,1); 342 $r1 = \$g[1]; @g = sort { $a<$b?1:$a>$b?-1:0 } @g; $r2 = \$g[0]; 343 is "$r1-@g", "$r2-3 2 1", "inplace custom sort of global"; 344 345 sub mysort { $b cmp $a }; 346 @a = qw(b c a); $r1 = \$a[1]; @a = sort mysort @a; $r2 = \$a[0]; 347 is "$r1-@a", "$r2-c b a", "inplace sort with function of lexical"; 348 349 use Tie::Array; 350 my @t; 351 tie @t, 'Tie::StdArray'; 352 353 @t = qw(b c a); @t = sort @t; 354 is "@t", "a b c", "inplace sort of tied array"; 355 356 @t = qw(b c a); @t = sort mysort @t; 357 is "@t", "c b a", "inplace sort of tied array with function"; 358 359 # [perl #29790] don't optimise @a = ('a', sort @a) ! 360 361 @g = (3,2,1); @g = ('0', sort @g); 362 is "@g", "0 1 2 3", "un-inplace sort of global"; 363 @g = (3,2,1); @g = (sort(@g),'4'); 364 is "@g", "1 2 3 4", "un-inplace sort of global 2"; 365 366 @a = qw(b a c); @a = ('x', sort @a); 367 is "@a", "x a b c", "un-inplace sort of lexical"; 368 @a = qw(b a c); @a = ((sort @a), 'x'); 369 is "@a", "a b c x", "un-inplace sort of lexical 2"; 370 371 @g = (2,3,1); @g = ('0', sort { $b <=> $a } @g); 372 is "@g", "0 3 2 1", "un-inplace reversed sort of global"; 373 @g = (2,3,1); @g = ((sort { $b <=> $a } @g),'4'); 374 is "@g", "3 2 1 4", "un-inplace reversed sort of global 2"; 375 376 @g = (2,3,1); @g = ('0', sort { $a<$b?1:$a>$b?-1:0 } @g); 377 is "@g", "0 3 2 1", "un-inplace custom sort of global"; 378 @g = (2,3,1); @g = ((sort { $a<$b?1:$a>$b?-1:0 } @g),'4'); 379 is "@g", "3 2 1 4", "un-inplace custom sort of global 2"; 380 381 @a = qw(b c a); @a = ('x', sort mysort @a); 382 is "@a", "x c b a", "un-inplace sort with function of lexical"; 383 @a = qw(b c a); @a = ((sort mysort @a),'x'); 384 is "@a", "c b a x", "un-inplace sort with function of lexical 2"; 385 386 # RT#54758. Git 62b40d2474e7487e6909e1872b6bccdf812c6818 387 no warnings 'void'; 388 my @m; push @m, 0 for 1 .. 1024; $#m; @m = sort @m; 389 ::pass("in-place sorting segfault"); 390} 391 392# Test optimisations of reversed sorts. As we now guarantee stability by 393# default, # optimisations which do not provide this are bogus. 394 395{ 396 package Oscalar; 397 use overload (qw("" stringify 0+ numify fallback 1)); 398 399 sub new { 400 bless [$_[1], $_[2]], $_[0]; 401 } 402 403 sub stringify { $_[0]->[0] } 404 405 sub numify { $_[0]->[1] } 406} 407 408sub generate { 409 my $count = 0; 410 map {new Oscalar $_, $count++} qw(A A A B B B C C C); 411} 412 413my @input = &generate; 414my @output = sort @input; 415is join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", "Simple stable sort"; 416 417@input = &generate; 418@input = sort @input; 419is join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8", 420 "Simple stable in place sort"; 421 422# This won't be very interesting 423@input = &generate; 424@output = sort {$a <=> $b} @input; 425is "@output", "A A A B B B C C C", 'stable $a <=> $b sort'; 426 427@input = &generate; 428@output = sort {$a cmp $b} @input; 429is join(" ", map {0+$_} @output), "0 1 2 3 4 5 6 7 8", 'stable $a cmp $b sort'; 430 431@input = &generate; 432@input = sort {$a cmp $b} @input; 433is join(" ", map {0+$_} @input), "0 1 2 3 4 5 6 7 8", 434 'stable $a cmp $b in place sort'; 435 436@input = &generate; 437@output = sort {$b cmp $a} @input; 438is join(" ", map {0+$_} @output), "6 7 8 3 4 5 0 1 2", 'stable $b cmp $a sort'; 439 440@input = &generate; 441@input = sort {$b cmp $a} @input; 442is join(" ", map {0+$_} @input), "6 7 8 3 4 5 0 1 2", 443 'stable $b cmp $a in place sort'; 444 445@input = &generate; 446@output = reverse sort @input; 447is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", "Reversed stable sort"; 448 449@input = &generate; 450@input = reverse sort @input; 451is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0", 452 "Reversed stable in place sort"; 453 454@input = &generate; 455my $output = reverse sort @input; 456is $output, "CCCBBBAAA", "Reversed stable sort in scalar context"; 457 458 459@input = &generate; 460@output = reverse sort {$a cmp $b} @input; 461is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", 462 'reversed stable $a cmp $b sort'; 463 464@input = &generate; 465@input = reverse sort {$a cmp $b} @input; 466is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0", 467 'revesed stable $a cmp $b in place sort'; 468 469@input = &generate; 470$output = reverse sort {$a cmp $b} @input; 471is $output, "CCCBBBAAA", 'Reversed stable $a cmp $b sort in scalar context'; 472 473@input = &generate; 474@output = reverse sort {$b cmp $a} @input; 475is join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6", 476 'reversed stable $b cmp $a sort'; 477 478@input = &generate; 479@input = reverse sort {$b cmp $a} @input; 480is join(" ", map {0+$_} @input), "2 1 0 5 4 3 8 7 6", 481 'revesed stable $b cmp $a in place sort'; 482 483@input = &generate; 484$output = reverse sort {$b cmp $a} @input; 485is $output, "AAABBBCCC", 'Reversed stable $b cmp $a sort in scalar context'; 486 487sub stuff { 488 # Something complex enough to defeat any constant folding optimiser 489 $$ - $$; 490} 491 492@input = &generate; 493@output = reverse sort {stuff || $a cmp $b} @input; 494is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", 495 'reversed stable complex sort'; 496 497@input = &generate; 498@input = reverse sort {stuff || $a cmp $b} @input; 499is join(" ", map {0+$_} @input), "8 7 6 5 4 3 2 1 0", 500 'revesed stable complex in place sort'; 501 502@input = &generate; 503$output = reverse sort {stuff || $a cmp $b } @input; 504is $output, "CCCBBBAAA", 'Reversed stable complex sort in scalar context'; 505 506sub sortr { 507 reverse sort @_; 508} 509 510@output = sortr &generate; 511is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", 512 'reversed stable sort return list context'; 513$output = sortr &generate; 514is $output, "CCCBBBAAA", 515 'reversed stable sort return scalar context'; 516 517sub sortcmpr { 518 reverse sort {$a cmp $b} @_; 519} 520 521@output = sortcmpr &generate; 522is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", 523 'reversed stable $a cmp $b sort return list context'; 524$output = sortcmpr &generate; 525is $output, "CCCBBBAAA", 526 'reversed stable $a cmp $b sort return scalar context'; 527 528sub sortcmprba { 529 reverse sort {$b cmp $a} @_; 530} 531 532@output = sortcmprba &generate; 533is join(" ", map {0+$_} @output), "2 1 0 5 4 3 8 7 6", 534 'reversed stable $b cmp $a sort return list context'; 535$output = sortcmprba &generate; 536is $output, "AAABBBCCC", 537'reversed stable $b cmp $a sort return scalar context'; 538 539sub sortcmprq { 540 reverse sort {stuff || $a cmp $b} @_; 541} 542 543@output = sortcmpr &generate; 544is join(" ", map {0+$_} @output), "8 7 6 5 4 3 2 1 0", 545 'reversed stable complex sort return list context'; 546$output = sortcmpr &generate; 547is $output, "CCCBBBAAA", 548 'reversed stable complex sort return scalar context'; 549 550# And now with numbers 551 552sub generate1 { 553 my $count = 'A'; 554 map {new Oscalar $count++, $_} 0, 0, 0, 1, 1, 1, 2, 2, 2; 555} 556 557# This won't be very interesting 558@input = &generate1; 559@output = sort {$a cmp $b} @input; 560is "@output", "A B C D E F G H I", 'stable $a cmp $b sort'; 561 562@input = &generate1; 563@output = sort {$a <=> $b} @input; 564is "@output", "A B C D E F G H I", 'stable $a <=> $b sort'; 565 566@input = &generate1; 567@input = sort {$a <=> $b} @input; 568is "@input", "A B C D E F G H I", 'stable $a <=> $b in place sort'; 569 570@input = &generate1; 571@output = sort {$b <=> $a} @input; 572is "@output", "G H I D E F A B C", 'stable $b <=> $a sort'; 573 574@input = &generate1; 575@input = sort {$b <=> $a} @input; 576is "@input", "G H I D E F A B C", 'stable $b <=> $a in place sort'; 577 578# test that optimized {$b cmp $a} and {$b <=> $a} remain stable 579# (new in 5.9) without overloading 580{ no warnings; 581@b = sort { $b <=> $a } @input = qw/5first 6first 5second 6second/; 582is "@b" , "6first 6second 5first 5second", "optimized {$b <=> $a} without overloading" ; 583@input = sort {$b <=> $a} @input; 584is "@input" , "6first 6second 5first 5second","inline optimized {$b <=> $a} without overloading" ; 585}; 586 587# These two are actually doing string cmp on 0 1 and 2 588@input = &generate1; 589@output = reverse sort @input; 590is "@output", "I H G F E D C B A", "Reversed stable sort"; 591 592@input = &generate1; 593@input = reverse sort @input; 594is "@input", "I H G F E D C B A", "Reversed stable in place sort"; 595 596@input = &generate1; 597$output = reverse sort @input; 598is $output, "IHGFEDCBA", "Reversed stable sort in scalar context"; 599 600@input = &generate1; 601@output = reverse sort {$a <=> $b} @input; 602is "@output", "I H G F E D C B A", 'reversed stable $a <=> $b sort'; 603 604@input = &generate1; 605@input = reverse sort {$a <=> $b} @input; 606is "@input", "I H G F E D C B A", 'revesed stable $a <=> $b in place sort'; 607 608@input = &generate1; 609$output = reverse sort {$a <=> $b} @input; 610is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort in scalar context'; 611 612@input = &generate1; 613@output = reverse sort {$b <=> $a} @input; 614is "@output", "C B A F E D I H G", 'reversed stable $b <=> $a sort'; 615 616@input = &generate1; 617@input = reverse sort {$b <=> $a} @input; 618is "@input", "C B A F E D I H G", 'revesed stable $b <=> $a in place sort'; 619 620@input = &generate1; 621$output = reverse sort {$b <=> $a} @input; 622is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort in scalar context'; 623 624@input = &generate1; 625@output = reverse sort {stuff || $a <=> $b} @input; 626is "@output", "I H G F E D C B A", 'reversed stable complex sort'; 627 628@input = &generate1; 629@input = reverse sort {stuff || $a <=> $b} @input; 630is "@input", "I H G F E D C B A", 'revesed stable complex in place sort'; 631 632@input = &generate1; 633$output = reverse sort {stuff || $a <=> $b} @input; 634is $output, "IHGFEDCBA", 'reversed stable complex sort in scalar context'; 635 636sub sortnumr { 637 reverse sort {$a <=> $b} @_; 638} 639 640@output = sortnumr &generate1; 641is "@output", "I H G F E D C B A", 642 'reversed stable $a <=> $b sort return list context'; 643$output = sortnumr &generate1; 644is $output, "IHGFEDCBA", 'reversed stable $a <=> $b sort return scalar context'; 645 646sub sortnumrba { 647 reverse sort {$b <=> $a} @_; 648} 649 650@output = sortnumrba &generate1; 651is "@output", "C B A F E D I H G", 652 'reversed stable $b <=> $a sort return list context'; 653$output = sortnumrba &generate1; 654is $output, "CBAFEDIHG", 'reversed stable $b <=> $a sort return scalar context'; 655 656sub sortnumrq { 657 reverse sort {stuff || $a <=> $b} @_; 658} 659 660@output = sortnumrq &generate1; 661is "@output", "I H G F E D C B A", 662 'reversed stable complex sort return list context'; 663$output = sortnumrq &generate1; 664is $output, "IHGFEDCBA", 'reversed stable complex sort return scalar context'; 665 666@output = reverse (sort(qw(C A B)), 0); 667is "@output", "0 C B A", 'reversed sort with trailing argument'; 668 669@output = reverse (0, sort(qw(C A B))); 670is "@output", "C B A 0", 'reversed sort with leading argument'; 671 672eval { @output = sort {goto sub {}} 1,2; }; 673$fail_msg = q(Can't goto subroutine outside a subroutine); 674cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr outside subr'); 675 676 677 678sub goto_sub {goto sub{}} 679eval { @output = sort goto_sub 1,2; }; 680$fail_msg = q(Can't goto subroutine from a sort sub); 681cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto subr from a sort sub'); 682 683 684 685eval { @output = sort {goto label} 1,2; }; 686$fail_msg = q(Can't "goto" out of a pseudo block); 687cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 1'); 688 689 690 691sub goto_label {goto label} 692label: eval { @output = sort goto_label 1,2; }; 693$fail_msg = q(Can't "goto" out of a pseudo block); 694cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'goto out of a pseudo block 2'); 695 696 697 698sub self_immolate {undef &self_immolate; $a<=>$b} 699eval { @output = sort self_immolate 1,2,3 }; 700$fail_msg = q(Can't undef active subroutine); 701cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'undef active subr'); 702 703 704for(1,2) # We run this twice, to make sure sort does not lower the ref 705{ # count. See bug 71076. 706 my $failed = 0; 707 708 sub rec { 709 my $n = shift; 710 if (!defined($n)) { # No arg means we're being called by sort() 711 return 1; 712 } 713 if ($n<5) { rec($n+1); } 714 else { () = sort rec 1,2; } 715 716 $failed = 1 if !defined $n; 717 } 718 719 rec(1); 720 ok(!$failed, "sort from active sub"); 721} 722 723# $a and $b are set in the package the sort() is called from, 724# *not* the package the sort sub is in. This is longstanding 725# de facto behaviour that shouldn't be broken. 726my $answer = "good"; 727() = sort OtherPack::foo 1,2,3,4; 728 729{ 730 package OtherPack; 731 no warnings 'once'; 732 sub foo { 733 $answer = "something was unexpectedly defined or undefined" if 734 defined($a) || defined($b) || !defined($main::a) || !defined($main::b); 735 $main::a <=> $main::b; 736 } 737} 738 739cmp_ok($answer,'eq','good','sort subr called from other package'); 740 741 742# Bug 36430 - sort called in package2 while a 743# sort in package1 is active should set $package2::a/b. 744{ 745 my $answer = "good"; 746 my @list = sort { A::min(@$a) <=> A::min(@$b) } 747 [3, 1, 5], [2, 4], [0]; 748 749 cmp_ok($answer,'eq','good','bug 36430'); 750 751 package A; 752 sub min { 753 my @list = sort { 754 $answer = '$a and/or $b are not defined ' if !defined($a) || !defined($b); 755 $a <=> $b; 756 } @_; 757 $list[0]; 758 } 759} 760 761 762# Bug 7567 - an array shouldn't be modifiable while it's being 763# sorted in-place. 764{ 765 eval { @a=(1..8); @a = sort { @a = (0) } @a; }; 766 767 $fail_msg = q(Modification of a read-only value attempted); 768 cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'bug 7567'); 769} 770 771{ 772 local $TODO = "sort should make sure elements are not freed in the sort block"; 773 eval { @nomodify_x=(1..8); our @copy = sort { @nomodify_x = (0) } (@nomodify_x, 3); }; 774 is($@, ""); 775} 776 777 778# Sorting shouldn't increase the refcount of a sub 779{ 780 sub sportello {(1+$a) <=> (1+$b)} 781 my $refcnt = &Internals::SvREFCNT(\&sportello); 782 @output = sort sportello 3,7,9; 783 784 { 785 package Doc; 786 ::is($refcnt, &Internals::SvREFCNT(\&::sportello), "sort sub refcnt"); 787 $fail_msg = q(Modification of a read-only value attempted); 788 # Sorting a read-only array in-place shouldn't be allowed 789 my @readonly = (1..10); 790 Internals::SvREADONLY(@readonly, 1); 791 eval { @readonly = sort @readonly; }; 792 ::cmp_ok(substr($@,0,length($fail_msg)),'eq',$fail_msg,'in-place sort of read-only array'); 793 } 794} 795 796 797# Using return() should be okay even in a deeper context 798@b = sort {while (1) {return ($a <=> $b)} } 1..10; 799is("@b", "1 2 3 4 5 6 7 8 9 10", "return within loop"); 800 801# Using return() should be okay even if there are other items 802# on the stack at the time. 803@b = sort {$_ = ($a<=>$b) + do{return $b<=> $a}} 1..10; 804is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack"); 805 806# As above, but with a sort sub rather than a sort block. 807sub ret_with_stacked { $_ = ($a<=>$b) + do {return $b <=> $a} } 808@b = sort ret_with_stacked 1..10; 809is("@b", "10 9 8 7 6 5 4 3 2 1", "return with SVs on stack"); 810 811# Comparison code should be able to give result in non-integer representation. 812sub cmp_as_string($$) { $_[0] < $_[1] ? "-1" : $_[0] == $_[1] ? "0" : "+1" } 813@b = sort { cmp_as_string($a, $b) } (1,5,4,7,3,2,3); 814is("@b", "1 2 3 3 4 5 7", "comparison result as string"); 815@b = sort cmp_as_string (1,5,4,7,3,2,3); 816is("@b", "1 2 3 3 4 5 7", "comparison result as string"); 817