Permission testing is tricky when we have too much power.
[p5sagit/p5-mst-13.2.git] / t / op / sort.t
1 #!./perl
2
3 print "1..27\n";
4
5 sub backwards { $a lt $b ? 1 : $a gt $b ? -1 : 0 }
6
7 my $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
23 @harry = ('dog','cat','x','Cain','Abel');
24 @george = ('gone','chased','yz','punished','Axed');
25
26 $x = join('', sort @harry);
27 $expected = $upperfirst ? 'AbelCaincatdogx' : 'catdogxAbelCain';
28 print "# 1: x = '$x', expected = '$expected'\n";
29 print ($x eq $expected ? "ok 1\n" : "not ok 1\n");
30
31 $x = join('', sort( backwards @harry));
32 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
33 print "# 2: x = '$x', expected = '$expected'\n";
34 print ($x eq $expected ? "ok 2\n" : "not ok 2\n");
35
36 $x = join('', sort @george, 'to', @harry);
37 $expected = $upperfirst ?
38     'AbelAxedCaincatchaseddoggonepunishedtoxyz' :
39     'catchaseddoggonepunishedtoxyzAbelAxedCain' ;
40 print "# 3: x = '$x', expected = '$expected'\n";
41 print ($x eq $expected ?"ok 3\n":"not ok 3\n");
42
43 @a = ();
44 @b = reverse @a;
45 print ("@b" eq "" ? "ok 4\n" : "not ok 4 (@b)\n");
46
47 @a = (1);
48 @b = reverse @a;
49 print ("@b" eq "1" ? "ok 5\n" : "not ok 5 (@b)\n");
50
51 @a = (1,2);
52 @b = reverse @a;
53 print ("@b" eq "2 1" ? "ok 6\n" : "not ok 6 (@b)\n");
54
55 @a = (1,2,3);
56 @b = reverse @a;
57 print ("@b" eq "3 2 1" ? "ok 7\n" : "not ok 7 (@b)\n");
58
59 @a = (1,2,3,4);
60 @b = reverse @a;
61 print ("@b" eq "4 3 2 1" ? "ok 8\n" : "not ok 8 (@b)\n");
62
63 @a = (10,2,3,4);
64 @b = sort {$a <=> $b;} @a;
65 print ("@b" eq "2 3 4 10" ? "ok 9\n" : "not ok 9 (@b)\n");
66
67 $sub = 'backwards';
68 $x = join('', sort $sub @harry);
69 $expected = $upperfirst ? 'xdogcatCainAbel' : 'CainAbelxdogcat';
70 print "# 10: x = $x, expected = '$expected'\n";
71 print ($x eq $expected ? "ok 10\n" : "not ok 10\n");
72
73 # literals, combinations
74
75 @b = sort (4,1,3,2);
76 print ("@b" eq '1 2 3 4' ? "ok 11\n" : "not ok 11\n");
77 print "# x = '@b'\n";
78
79 @b = sort grep { $_ } (4,1,3,2);
80 print ("@b" eq '1 2 3 4' ? "ok 12\n" : "not ok 12\n");
81 print "# x = '@b'\n";
82
83 @b = sort map { $_ } (4,1,3,2);
84 print ("@b" eq '1 2 3 4' ? "ok 13\n" : "not ok 13\n");
85 print "# x = '@b'\n";
86
87 @b = sort reverse (4,1,3,2);
88 print ("@b" eq '1 2 3 4' ? "ok 14\n" : "not ok 14\n");
89 print "# x = '@b'\n";
90
91 $^W = 0;
92 # redefining sort sub inside the sort sub should fail
93 sub twoface { *twoface = sub { $a <=> $b }; &twoface }
94 eval { @b = sort twoface 4,1,3,2 };
95 print ($@ =~ /redefine active sort/ ? "ok 15\n" : "not ok 15\n");
96
97 # redefining sort subs outside the sort should not fail
98 eval { *twoface = sub { &backwards } };
99 print $@ ? "not ok 16\n" : "ok 16\n";
100
101 eval { @b = sort twoface 4,1,3,2 };
102 print ("@b" eq '4 3 2 1' ? "ok 17\n" : "not ok 17 |@b|\n");
103
104 *twoface = sub { *twoface = *backwards; $a <=> $b };
105 eval { @b = sort twoface 4,1 };
106 print ($@ =~ /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                };
113 eval { @b = sort twoface 4,1 };
114 print $@ ? "$@" : "not ok 19\n";
115
116 eval <<'CODE';
117     my @result = sort main'backwards 'one', 'two';
118 CODE
119 print $@ ? "not ok 20\n# $@" : "ok 20\n";
120
121 eval <<'CODE';
122     # "sort 'one', 'two'" should not try to parse "'one" as a sort sub
123     my @result = sort 'one', 'two';
124 CODE
125 print $@ ? "not ok 21\n# $@" : "ok 21\n";
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