5 # XXX known to leak scalars
6 $ENV{PERL_DESTRUCT_LEVEL} = 0 unless $ENV{PERL_DESTRUCT_LEVEL} > 3;
8 sub backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
10 my $upperfirst = 'A' lt 'a';
12 # Beware: in future this may become hairier because of possible
13 # collation complications: qw(A a B c) can be sorted at least as
14 # any of the following
21 # All the above orders make sense.
23 # That said, EBCDIC sorts all small letters first, as opposed
24 # to ASCII which sorts all big letters first.
26 @harry = ('dog','cat','x','Cain','Abel');
27 @george = ('gone','chased','yz','punished','Axed');
29 $x = join('', sort @harry);
30 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
31 print "# 1: x = '$x', expected = '$expected'\n";
32 print ($x eq $expected ? "ok 1\n" : "not ok 1\n");
34 $x = join('', sort( backwards @harry));
35 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
36 print "# 2: x = '$x', expected = '$expected'\n";
37 print ($x eq $expected ? "ok 2\n" : "not ok 2\n");
39 $x = join('', sort @george, 'to', @harry);
40 $expected = $upperfirst ?
41 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
42 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
43 print "# 3: x = '$x', expected = '$expected'\n";
44 print ($x eq $expected ?"ok 3\n":"not ok 3\n");
48 print ("@b" eq "" ? "ok 4\n" : "not ok 4 (@b)\n");
52 print ("@b" eq "1" ? "ok 5\n" : "not ok 5 (@b)\n");
56 print ("@b" eq "2 1" ? "ok 6\n" : "not ok 6 (@b)\n");
60 print ("@b" eq "3 2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
64 print ("@b" eq "4 3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
67 @b = sort {$a <=> $b;} @a;
68 print ("@b" eq "2 3 4 10" ? "ok 9\n" : "not ok 9 (@b)\n");
71 $x = join('', sort $sub @harry);
72 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
73 print "# 10: x = $x, expected = '$expected'\n";
74 print ($x eq $expected ? "ok 10\n" : "not ok 10\n");
76 # literals, combinations
79 print ("@b" eq '1 2 3 4' ? "ok 11\n" : "not ok 11\n");
82 @b = sort grep { $_ } (4,1,3,2);
83 print ("@b" eq '1 2 3 4' ? "ok 12\n" : "not ok 12\n");
86 @b = sort map { $_ } (4,1,3,2);
87 print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
90 @b = sort reverse (4,1,3,2);
91 print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
95 # redefining sort sub inside the sort sub should fail
96 sub twoface { *twoface = sub { $a <=> $b }; &twoface }
97 eval { @b = sort twoface 4,1,3,2 };
98 print ($@ =~ /redefine active sort/ ? "ok 15\n" : "not ok 15\n");
100 # redefining sort subs outside the sort should not fail
101 eval { *twoface = sub { &backwards } };
102 print $@ ? "not ok 16\n" : "ok 16\n";
104 eval { @b = sort twoface 4,1,3,2 };
105 print ("@b" eq '4 3 2 1' ? "ok 17\n" : "not ok 17 |@b|\n");
107 *twoface = sub { *twoface = *backwards; $a <=> $b };
108 eval { @b = sort twoface 4,1 };
109 print ($@ =~ /redefine active sort/ ? "ok 18\n" : "not ok 18\n");
112 eval 'sub twoface { $a <=> $b }';
113 die($@ =~ /redefine active sort/ ? "ok 19\n" : "not ok 19\n");
116 eval { @b = sort twoface 4,1 };
117 print $@ ? "$@" : "not ok 19\n";
120 my @result = sort main'backwards 'one', 'two';
122 print $@ ? "not ok 20\n# $@" : "ok 20\n";
125 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
126 my @result = sort 'one', 'two';
128 print $@ ? "not ok 21\n# $@" : "ok 21\n";
131 my $sortsub = \&backwards;
132 my $sortglob = *backwards;
133 my $sortglobr = \*backwards;
134 my $sortname = 'backwards';
135 @b = sort $sortsub 4,1,3,2;
136 print ("@b" eq '4 3 2 1' ? "ok 22\n" : "not ok 22 |@b|\n");
137 @b = sort $sortglob 4,1,3,2;
138 print ("@b" eq '4 3 2 1' ? "ok 23\n" : "not ok 23 |@b|\n");
139 @b = sort $sortname 4,1,3,2;
140 print ("@b" eq '4 3 2 1' ? "ok 24\n" : "not ok 24 |@b|\n");
141 @b = sort $sortglobr 4,1,3,2;
142 print ("@b" eq '4 3 2 1' ? "ok 25\n" : "not ok 25 |@b|\n");
146 local $sortsub = \&backwards;
147 local $sortglob = *backwards;
148 local $sortglobr = \*backwards;
149 local $sortname = 'backwards';
150 @b = sort $sortsub 4,1,3,2;
151 print ("@b" eq '4 3 2 1' ? "ok 26\n" : "not ok 26 |@b|\n");
152 @b = sort $sortglob 4,1,3,2;
153 print ("@b" eq '4 3 2 1' ? "ok 27\n" : "not ok 27 |@b|\n");
154 @b = sort $sortname 4,1,3,2;
155 print ("@b" eq '4 3 2 1' ? "ok 28\n" : "not ok 28 |@b|\n");
156 @b = sort $sortglobr 4,1,3,2;
157 print ("@b" eq '4 3 2 1' ? "ok 29\n" : "not ok 29 |@b|\n");