This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
overload.t has same locale problem with VMS that it has with dec_osf
[perl5.git] / t / uni / overload.t
1 #!perl -w
2
3 BEGIN {
4     if ($ENV{'PERL_CORE'}){
5         chdir 't';
6         @INC = '../lib';
7     }
8 }
9
10 use Test::More tests => 208;
11
12 package UTF8Toggle;
13 use strict;
14
15 use overload '""' => 'stringify', fallback => 1;
16
17 sub new {
18     my $class = shift;
19     my $value = shift;
20     my $state = shift||0;
21     return bless [$value, $state], $class;
22 }
23
24 sub stringify {
25     my $self = shift;
26     $self->[1] = ! $self->[1];
27     if ($self->[1]) {
28         utf8::downgrade($self->[0]);
29     } else {
30         utf8::upgrade($self->[0]);
31     }
32     $self->[0];
33 }
34
35 package main;
36
37 # Bug 34297
38 foreach my $t ("ASCII", "B\366se") {
39     my $length = length $t;
40
41     my $u = UTF8Toggle->new($t);
42     is (length $u, $length, "length of '$t'");
43     is (length $u, $length, "length of '$t'");
44     is (length $u, $length, "length of '$t'");
45     is (length $u, $length, "length of '$t'");
46 }
47
48 my $u = UTF8Toggle->new("\311");
49 my $lc = lc $u;
50 is (length $lc, 1);
51 is ($lc, "\311", "E acute -> e acute");
52 $lc = lc $u;
53 is (length $lc, 1);
54 is ($lc, "\351", "E acute -> e acute");
55 $lc = lc $u;
56 is (length $lc, 1);
57 is ($lc, "\311", "E acute -> e acute");
58
59 $u = UTF8Toggle->new("\351");
60 my $uc = uc $u;
61 is (length $uc, 1);
62 is ($uc, "\351", "e acute -> E acute");
63 $uc = uc $u;
64 is (length $uc, 1);
65 is ($uc, "\311", "e acute -> E acute");
66 $uc = uc $u;
67 is (length $uc, 1);
68 is ($uc, "\351", "e acute -> E acute");
69
70 $u = UTF8Toggle->new("\311");
71 $lc = lcfirst $u;
72 is (length $lc, 1);
73 is ($lc, "\311", "E acute -> e acute");
74 $lc = lcfirst $u;
75 is (length $lc, 1);
76 is ($lc, "\351", "E acute -> e acute");
77 $lc = lcfirst $u;
78 is (length $lc, 1);
79 is ($lc, "\311", "E acute -> e acute");
80
81 $u = UTF8Toggle->new("\351");
82 $uc = ucfirst $u;
83 is (length $uc, 1);
84 is ($uc, "\351", "e acute -> E acute");
85 $uc = ucfirst $u;
86 is (length $uc, 1);
87 is ($uc, "\311", "e acute -> E acute");
88 $uc = ucfirst $u;
89 is (length $uc, 1);
90 is ($uc, "\351", "e acute -> E acute");
91
92 my $have_setlocale = 0;
93 eval {
94     require POSIX;
95     import POSIX ':locale_h';
96     $have_setlocale++;
97 };
98
99 SKIP: {
100     if (!$have_setlocale) {
101         skip "No setlocale", 24;
102     } elsif (!setlocale(&POSIX::LC_ALL, "en_GB.ISO8859-1")) {
103         skip "Could not setlocale to en_GB.ISO8859-1", 24;
104     } elsif ($^O eq 'dec_osf' || $^O eq 'VMS') {
105         skip "$^O has broken en_GB.ISO8859-1 locale", 24;
106     } else {
107         use locale;
108         my $u = UTF8Toggle->new("\311");
109         my $lc = lc $u;
110         is (length $lc, 1);
111         is ($lc, "\351", "E acute -> e acute");
112         $lc = lc $u;
113         is (length $lc, 1);
114         is ($lc, "\351", "E acute -> e acute");
115         $lc = lc $u;
116         is (length $lc, 1);
117         is ($lc, "\351", "E acute -> e acute");
118
119         $u = UTF8Toggle->new("\351");
120         my $uc = uc $u;
121         is (length $uc, 1);
122         is ($uc, "\311", "e acute -> E acute");
123         $uc = uc $u;
124         is (length $uc, 1);
125         is ($uc, "\311", "e acute -> E acute");
126         $uc = uc $u;
127         is (length $uc, 1);
128         is ($uc, "\311", "e acute -> E acute");
129
130         $u = UTF8Toggle->new("\311");
131         $lc = lcfirst $u;
132         is (length $lc, 1);
133         is ($lc, "\351", "E acute -> e acute");
134         $lc = lcfirst $u;
135         is (length $lc, 1);
136         is ($lc, "\351", "E acute -> e acute");
137         $lc = lcfirst $u;
138         is (length $lc, 1);
139         is ($lc, "\351", "E acute -> e acute");
140
141         $u = UTF8Toggle->new("\351");
142         $uc = ucfirst $u;
143         is (length $uc, 1);
144         is ($uc, "\311", "e acute -> E acute");
145         $uc = ucfirst $u;
146         is (length $uc, 1);
147         is ($uc, "\311", "e acute -> E acute");
148         $uc = ucfirst $u;
149         is (length $uc, 1);
150         is ($uc, "\311", "e acute -> E acute");
151     }
152 }
153
154 my $tmpfile = 'overload.tmp';
155
156 foreach my $operator ('print', 'syswrite', 'syswrite len', 'syswrite off',
157                       'syswrite len off') {
158     foreach my $layer ('', ':utf8') {
159         open my $fh, "+>$layer", $tmpfile or die $!;
160         my $pad = $operator =~ /\boff\b/ ? "\243" : "";
161         my $trail = $operator =~ /\blen\b/ ? "!" : "";
162         my $u = UTF8Toggle->new("$pad\311\n$trail");
163         my $l = UTF8Toggle->new("$pad\351\n$trail", 1);
164         if ($operator eq 'print') {
165             print $fh $u;
166             print $fh $u;
167             print $fh $u;
168             print $fh $l;
169             print $fh $l;
170             print $fh $l;
171         } elsif ($operator eq 'syswrite') {
172             syswrite $fh, $u;
173             syswrite $fh, $u;
174             syswrite $fh, $u;
175             syswrite $fh, $l;
176             syswrite $fh, $l;
177             syswrite $fh, $l;
178         } elsif ($operator eq 'syswrite len') {
179             syswrite $fh, $u, 2;
180             syswrite $fh, $u, 2;
181             syswrite $fh, $u, 2;
182             syswrite $fh, $l, 2;
183             syswrite $fh, $l, 2;
184             syswrite $fh, $l, 2;
185         } elsif ($operator eq 'syswrite off'
186                  || $operator eq 'syswrite len off') {
187             syswrite $fh, $u, 2, 1;
188             syswrite $fh, $u, 2, 1;
189             syswrite $fh, $u, 2, 1;
190             syswrite $fh, $l, 2, 1;
191             syswrite $fh, $l, 2, 1;
192             syswrite $fh, $l, 2, 1;
193         } else {
194             die $operator;
195         }
196
197         seek $fh, 0, 0 or die $!;
198         my $line;
199         chomp ($line = <$fh>);
200         is ($line, "\311", "$operator $layer");
201         chomp ($line = <$fh>);
202         is ($line, "\311", "$operator $layer");
203         chomp ($line = <$fh>);
204         is ($line, "\311", "$operator $layer");
205         chomp ($line = <$fh>);
206         is ($line, "\351", "$operator $layer");
207         chomp ($line = <$fh>);
208         is ($line, "\351", "$operator $layer");
209         chomp ($line = <$fh>);
210         is ($line, "\351", "$operator $layer");
211
212         close $fh or die $!;
213         unlink $tmpfile or die $!;
214     }
215 }
216
217 my $little = "\243\243";
218 my $big = " \243 $little ! $little ! $little \243 ";
219 my $right = rindex $big, $little;
220 my $right1 = rindex $big, $little, 11;
221 my $left = index $big, $little;
222 my $left1 = index $big, $little, 4;
223
224 cmp_ok ($right, ">", $right1, "Sanity check our rindex tests");
225 cmp_ok ($left, "<", $left1, "Sanity check our index tests");
226
227 foreach my $b ($big, UTF8Toggle->new($big)) {
228     foreach my $l ($little, UTF8Toggle->new($little),
229                    UTF8Toggle->new($little, 1)) {
230         is (rindex ($b, $l), $right, "rindex");
231         is (rindex ($b, $l), $right, "rindex");
232         is (rindex ($b, $l), $right, "rindex");
233
234         is (rindex ($b, $l, 11), $right1, "rindex 11");
235         is (rindex ($b, $l, 11), $right1, "rindex 11");
236         is (rindex ($b, $l, 11), $right1, "rindex 11");
237
238         is (index ($b, $l), $left, "index");
239         is (index ($b, $l), $left, "index");
240         is (index ($b, $l), $left, "index");
241
242         is (index ($b, $l, 4), $left1, "index 4");
243         is (index ($b, $l, 4), $left1, "index 4");
244         is (index ($b, $l, 4), $left1, "index 4");
245     }
246 }
247
248 my $bits = "\311";
249 foreach my $pieces ($bits, UTF8Toggle->new($bits)) {
250     like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
251     like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
252     like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
253
254     like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
255     like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
256     like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
257 }
258
259 foreach my $value ("\243", UTF8Toggle->new("\243")) {
260     is (pack ("A/A", $value), pack ("A/A", "\243"),
261         "pack copes with overloading");
262     is (pack ("A/A", $value), pack ("A/A", "\243"));
263     is (pack ("A/A", $value), pack ("A/A", "\243"));
264 }
265
266 END {
267     1 while -f $tmpfile and unlink $tmpfile || die "unlink '$tmpfile': $!";
268 }