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