fix C<if (...) { package Foo; ... }> misoptimization that fails
[p5sagit/p5-mst-13.2.git] / t / op / sort.t
CommitLineData
a687059c 1#!./perl
2
c6e96bcb 3print "1..27\n";
a687059c 4
2f52a358 5sub backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
a687059c 6
9d116dd7 7my $upperfirst = 'A' lt 'a';
8
9# Beware: in future this may become hairier because of possible
10# collation complications: qw(A a B c) can be sorted at least as
11# any of the following
12#
13# A a B b
14# A B a b
15# a b A B
16# a A b B
17#
18# All the above orders make sense.
19#
20# That said, EBCDIC sorts all small letters first, as opposed
21# to ASCII which sorts all big letters first.
22
a687059c 23@harry = ('dog','cat','x','Cain','Abel');
2f52a358 24@george = ('gone','chased','yz','punished','Axed');
a687059c 25
26$x = join('', sort @harry);
9d116dd7 27$expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
28print "# 1: x = '$x', expected = '$expected'\n";
29print ($x eq $expected ? "ok 1\n" : "not ok 1\n");
a687059c 30
a0d0e21e 31$x = join('', sort( backwards @harry));
9d116dd7 32$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
33print "# 2: x = '$x', expected = '$expected'\n";
34print ($x eq $expected ? "ok 2\n" : "not ok 2\n");
a687059c 35
36$x = join('', sort @george, 'to', @harry);
9d116dd7 37$expected = $upperfirst ?
38 'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
39 'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
40print "# 3: x = '$x', expected = '$expected'\n";
41print ($x eq $expected ?"ok 3\n":"not ok 3\n");
03a14243 42
43@a = ();
44@b = reverse @a;
45print ("@b" eq "" ? "ok 4\n" : "not ok 4 (@b)\n");
46
47@a = (1);
48@b = reverse @a;
49print ("@b" eq "1" ? "ok 5\n" : "not ok 5 (@b)\n");
50
51@a = (1,2);
52@b = reverse @a;
53print ("@b" eq "2 1" ? "ok 6\n" : "not ok 6 (@b)\n");
54
55@a = (1,2,3);
56@b = reverse @a;
57print ("@b" eq "3 2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
58
59@a = (1,2,3,4);
60@b = reverse @a;
61print ("@b" eq "4 3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
55204971 62
63@a = (10,2,3,4);
64@b = sort {$a <=> $b;} @a;
65print ("@b" eq "2 3 4 10" ? "ok 9\n" : "not ok 9 (@b)\n");
988174c1 66
463ee0b2 67$sub = 'backwards';
988174c1 68$x = join('', sort $sub @harry);
9d116dd7 69$expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
70print "# 10: x = $x, expected = '$expected'\n";
71print ($x eq $expected ? "ok 10\n" : "not ok 10\n");
988174c1 72
cd5de442 73# literals, combinations
74
75@b = sort (4,1,3,2);
76print ("@b" eq '1 2 3 4' ? "ok 11\n" : "not ok 11\n");
77print "# x = '@b'\n";
78
79@b = sort grep { $_ } (4,1,3,2);
80print ("@b" eq '1 2 3 4' ? "ok 12\n" : "not ok 12\n");
81print "# x = '@b'\n";
82
83@b = sort map { $_ } (4,1,3,2);
84print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
85print "# x = '@b'\n";
86
87@b = sort reverse (4,1,3,2);
88print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
89print "# x = '@b'\n";
7bac28a0 90
91$^W = 0;
92# redefining sort sub inside the sort sub should fail
93sub twoface { *twoface = sub { $a <=> $b }; &twoface }
94eval { @b = sort twoface 4,1,3,2 };
95print ($@ =~ /redefine active sort/ ? "ok 15\n" : "not ok 15\n");
96
97# redefining sort subs outside the sort should not fail
98eval { *twoface = sub { &backwards } };
99print $@ ? "not ok 16\n" : "ok 16\n";
100
101eval { @b = sort twoface 4,1,3,2 };
102print ("@b" eq '4 3 2 1' ? "ok 17\n" : "not ok 17 |@b|\n");
103
104*twoface = sub { *twoface = *backwards; $a <=> $b };
105eval { @b = sort twoface 4,1 };
106print ($@ =~ /redefine active sort/ ? "ok 18\n" : "not ok 18\n");
107
108*twoface = sub {
109 eval 'sub twoface { $a <=> $b }';
110 die($@ =~ /redefine active sort/ ? "ok 19\n" : "not ok 19\n");
111 $a <=> $b;
112 };
113eval { @b = sort twoface 4,1 };
114print $@ ? "$@" : "not ok 19\n";
15f0808c 115
116eval <<'CODE';
117 my @result = sort main'backwards 'one', 'two';
118CODE
119print $@ ? "not ok 20\n# $@" : "ok 20\n";
120
121eval <<'CODE';
122 # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
123 my @result = sort 'one', 'two';
124CODE
125print $@ ? "not ok 21\n# $@" : "ok 21\n";
c6e96bcb 126
127{
128 my $sortsub = \&backwards;
129 my $sortglob = *backwards;
130 my $sortname = 'backwards';
131 @b = sort $sortsub 4,1,3,2;
132 print ("@b" eq '4 3 2 1' ? "ok 22\n" : "not ok 22 |@b|\n");
133 @b = sort $sortglob 4,1,3,2;
134 print ("@b" eq '4 3 2 1' ? "ok 23\n" : "not ok 23 |@b|\n");
135 @b = sort $sortname 4,1,3,2;
136 print ("@b" eq '4 3 2 1' ? "ok 24\n" : "not ok 24 |@b|\n");
137}
138
139{
140 local $sortsub = \&backwards;
141 local $sortglob = *backwards;
142 local $sortname = 'backwards';
143 @b = sort $sortsub 4,1,3,2;
144 print ("@b" eq '4 3 2 1' ? "ok 25\n" : "not ok 22 |@b|\n");
145 @b = sort $sortglob 4,1,3,2;
146 print ("@b" eq '4 3 2 1' ? "ok 26\n" : "not ok 23 |@b|\n");
147 @b = sort $sortname 4,1,3,2;
148 print ("@b" eq '4 3 2 1' ? "ok 27\n" : "not ok 24 |@b|\n");
149}
150