This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix test that fails with new warny File::Path
[perl5.git] / t / uni / overload.t
CommitLineData
92331800
NC
1#!perl -w
2
3BEGIN {
a817e89d 4 chdir 't' if -d 't';
e4206093 5 @INC = '../lib';
569f7fc5 6 require Config; import Config;
f6fca319 7 require './test.pl'; require './charset_tools.pl';
92331800
NC
8}
9
e4206093 10plan(tests => 215);
92331800 11
ec9af7d4 12package UTF8Toggle;
92331800
NC
13use strict;
14
12abf4f0 15use overload '""' => 'stringify', fallback => 1;
92331800
NC
16
17sub new {
18 my $class = shift;
676f44e7
NC
19 my $value = shift;
20 my $state = shift||0;
21 return bless [$value, $state], $class;
92331800
NC
22}
23
24sub 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
35package main;
36
61fc5122
KW
37# These tests are based on characters 128-255 not having latin1, and hence
38# Unicode, semantics
1863b879 39# no feature "unicode_strings";
61fc5122 40
92331800 41# Bug 34297
f6fca319 42foreach my $t ("ASCII", "B" . uni_to_native("\366") . "se") {
92331800
NC
43 my $length = length $t;
44
ec9af7d4 45 my $u = UTF8Toggle->new($t);
92331800
NC
46 is (length $u, $length, "length of '$t'");
47 is (length $u, $length, "length of '$t'");
48 is (length $u, $length, "length of '$t'");
49 is (length $u, $length, "length of '$t'");
50}
ec9af7d4 51
f6fca319
KW
52my $E_acute = uni_to_native("\311");
53my $e_acute = uni_to_native("\351");
54my $u = UTF8Toggle->new($E_acute);
6e08b83d
NC
55my $lc = lc $u;
56is (length $lc, 1);
f6fca319 57is ($lc, $E_acute, "E acute -> e acute");
6e08b83d
NC
58$lc = lc $u;
59is (length $lc, 1);
f6fca319 60is ($lc, $e_acute, "E acute -> e acute");
6e08b83d
NC
61$lc = lc $u;
62is (length $lc, 1);
f6fca319 63is ($lc, $E_acute, "E acute -> e acute");
6e08b83d 64
f6fca319 65$u = UTF8Toggle->new($e_acute);
6e08b83d
NC
66my $uc = uc $u;
67is (length $uc, 1);
f6fca319 68is ($uc, $e_acute, "e acute -> E acute");
6e08b83d
NC
69$uc = uc $u;
70is (length $uc, 1);
f6fca319 71is ($uc, $E_acute, "e acute -> E acute");
6e08b83d
NC
72$uc = uc $u;
73is (length $uc, 1);
f6fca319 74is ($uc, $e_acute, "e acute -> E acute");
6e08b83d 75
f6fca319 76$u = UTF8Toggle->new($E_acute);
6e08b83d
NC
77$lc = lcfirst $u;
78is (length $lc, 1);
f6fca319 79is ($lc, $E_acute, "E acute -> e acute");
6e08b83d
NC
80$lc = lcfirst $u;
81is (length $lc, 1);
f6fca319 82is ($lc, $e_acute, "E acute -> e acute");
6e08b83d
NC
83$lc = lcfirst $u;
84is (length $lc, 1);
f6fca319 85is ($lc, $E_acute, "E acute -> e acute");
6e08b83d 86
f6fca319 87$u = UTF8Toggle->new($e_acute);
6e08b83d
NC
88$uc = ucfirst $u;
89is (length $uc, 1);
f6fca319 90is ($uc, $e_acute, "e acute -> E acute");
6e08b83d
NC
91$uc = ucfirst $u;
92is (length $uc, 1);
f6fca319 93is ($uc, $E_acute, "e acute -> E acute");
6e08b83d
NC
94$uc = ucfirst $u;
95is (length $uc, 1);
f6fca319 96is ($uc, $e_acute, "e acute -> E acute");
6e08b83d 97
ec9af7d4
NC
98my $have_setlocale = 0;
99eval {
100 require POSIX;
569f7fc5
JR
101 if($Config{d_setlocale}) {
102 import POSIX ':locale_h';
103 $have_setlocale++;
104 }
ec9af7d4
NC
105};
106
107SKIP: {
108 if (!$have_setlocale) {
6e08b83d 109 skip "No setlocale", 24;
ec9af7d4 110 } elsif (!setlocale(&POSIX::LC_ALL, "en_GB.ISO8859-1")) {
6e08b83d 111 skip "Could not setlocale to en_GB.ISO8859-1", 24;
23ae3dfb 112 } elsif ($^O eq 'dec_osf' || $^O eq 'VMS') {
bce8aa37 113 skip "$^O has broken en_GB.ISO8859-1 locale", 24;
ec9af7d4 114 } else {
5f1269ab 115 use locale;
f6fca319 116 my $u = UTF8Toggle->new($E_acute);
ec9af7d4
NC
117 my $lc = lc $u;
118 is (length $lc, 1);
f6fca319 119 is ($lc, $e_acute, "E acute -> e acute");
ec9af7d4
NC
120 $lc = lc $u;
121 is (length $lc, 1);
f6fca319 122 is ($lc, $e_acute, "E acute -> e acute");
6e08b83d
NC
123 $lc = lc $u;
124 is (length $lc, 1);
f6fca319 125 is ($lc, $e_acute, "E acute -> e acute");
67306194 126
f6fca319 127 $u = UTF8Toggle->new($e_acute);
67306194
NC
128 my $uc = uc $u;
129 is (length $uc, 1);
f6fca319 130 is ($uc, $E_acute, "e acute -> E acute");
67306194
NC
131 $uc = uc $u;
132 is (length $uc, 1);
f6fca319 133 is ($uc, $E_acute, "e acute -> E acute");
6e08b83d
NC
134 $uc = uc $u;
135 is (length $uc, 1);
f6fca319 136 is ($uc, $E_acute, "e acute -> E acute");
d54190f6 137
f6fca319 138 $u = UTF8Toggle->new($E_acute);
d54190f6
NC
139 $lc = lcfirst $u;
140 is (length $lc, 1);
f6fca319 141 is ($lc, $e_acute, "E acute -> e acute");
d54190f6
NC
142 $lc = lcfirst $u;
143 is (length $lc, 1);
f6fca319 144 is ($lc, $e_acute, "E acute -> e acute");
6e08b83d
NC
145 $lc = lcfirst $u;
146 is (length $lc, 1);
f6fca319 147 is ($lc, $e_acute, "E acute -> e acute");
d54190f6 148
f6fca319 149 $u = UTF8Toggle->new($e_acute);
d54190f6
NC
150 $uc = ucfirst $u;
151 is (length $uc, 1);
f6fca319 152 is ($uc, $E_acute, "e acute -> E acute");
d54190f6
NC
153 $uc = ucfirst $u;
154 is (length $uc, 1);
f6fca319 155 is ($uc, $E_acute, "e acute -> E acute");
6e08b83d
NC
156 $uc = ucfirst $u;
157 is (length $uc, 1);
f6fca319 158 is ($uc, $E_acute, "e acute -> E acute");
ec9af7d4
NC
159 }
160}
676f44e7 161
6ddfe9e8 162my $tmpfile = tempfile();
676f44e7 163
c9cb0f41
NC
164foreach my $operator ('print', 'syswrite', 'syswrite len', 'syswrite off',
165 'syswrite len off') {
676f44e7
NC
166 foreach my $layer ('', ':utf8') {
167 open my $fh, "+>$layer", $tmpfile or die $!;
c9cb0f41
NC
168 my $pad = $operator =~ /\boff\b/ ? "\243" : "";
169 my $trail = $operator =~ /\blen\b/ ? "!" : "";
f6fca319
KW
170 my $u = UTF8Toggle->new("$pad$E_acute\n$trail");
171 my $l = UTF8Toggle->new("$pad$e_acute\n$trail", 1);
c9cb0f41 172 if ($operator eq 'print') {
b3c6e229 173 no warnings 'utf8';
c9cb0f41
NC
174 print $fh $u;
175 print $fh $u;
176 print $fh $u;
177 print $fh $l;
178 print $fh $l;
179 print $fh $l;
180 } elsif ($operator eq 'syswrite') {
181 syswrite $fh, $u;
182 syswrite $fh, $u;
183 syswrite $fh, $u;
184 syswrite $fh, $l;
185 syswrite $fh, $l;
186 syswrite $fh, $l;
187 } elsif ($operator eq 'syswrite len') {
188 syswrite $fh, $u, 2;
189 syswrite $fh, $u, 2;
190 syswrite $fh, $u, 2;
191 syswrite $fh, $l, 2;
192 syswrite $fh, $l, 2;
193 syswrite $fh, $l, 2;
194 } elsif ($operator eq 'syswrite off'
195 || $operator eq 'syswrite len off') {
196 syswrite $fh, $u, 2, 1;
197 syswrite $fh, $u, 2, 1;
198 syswrite $fh, $u, 2, 1;
199 syswrite $fh, $l, 2, 1;
200 syswrite $fh, $l, 2, 1;
201 syswrite $fh, $l, 2, 1;
202 } else {
203 die $operator;
204 }
676f44e7
NC
205
206 seek $fh, 0, 0 or die $!;
207 my $line;
208 chomp ($line = <$fh>);
f6fca319 209 is ($line, $E_acute, "$operator $layer");
676f44e7 210 chomp ($line = <$fh>);
f6fca319 211 is ($line, $E_acute, "$operator $layer");
676f44e7 212 chomp ($line = <$fh>);
f6fca319 213 is ($line, $E_acute, "$operator $layer");
676f44e7 214 chomp ($line = <$fh>);
f6fca319 215 is ($line, $e_acute, "$operator $layer");
676f44e7 216 chomp ($line = <$fh>);
f6fca319 217 is ($line, $e_acute, "$operator $layer");
676f44e7 218 chomp ($line = <$fh>);
f6fca319 219 is ($line, $e_acute, "$operator $layer");
676f44e7
NC
220
221 close $fh or die $!;
676f44e7
NC
222 }
223}
224
73ee8be2
NC
225my $little = "\243\243";
226my $big = " \243 $little ! $little ! $little \243 ";
227my $right = rindex $big, $little;
228my $right1 = rindex $big, $little, 11;
229my $left = index $big, $little;
230my $left1 = index $big, $little, 4;
231
232cmp_ok ($right, ">", $right1, "Sanity check our rindex tests");
233cmp_ok ($left, "<", $left1, "Sanity check our index tests");
234
235foreach my $b ($big, UTF8Toggle->new($big)) {
236 foreach my $l ($little, UTF8Toggle->new($little),
237 UTF8Toggle->new($little, 1)) {
238 is (rindex ($b, $l), $right, "rindex");
239 is (rindex ($b, $l), $right, "rindex");
240 is (rindex ($b, $l), $right, "rindex");
241
242 is (rindex ($b, $l, 11), $right1, "rindex 11");
243 is (rindex ($b, $l, 11), $right1, "rindex 11");
244 is (rindex ($b, $l, 11), $right1, "rindex 11");
245
246 is (index ($b, $l), $left, "index");
247 is (index ($b, $l), $left, "index");
248 is (index ($b, $l), $left, "index");
249
250 is (index ($b, $l, 4), $left1, "index 4");
251 is (index ($b, $l, 4), $left1, "index 4");
252 is (index ($b, $l, 4), $left1, "index 4");
253 }
254}
676f44e7 255
f6fca319 256my $bits = $E_acute;
12abf4f0
NC
257foreach my $pieces ($bits, UTF8Toggle->new($bits)) {
258 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
259 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
260 like ($bits ^ $pieces, qr/\A\0+\z/, "something xor itself is zeros");
261
262 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
263 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
264 like ($pieces ^ $bits, qr/\A\0+\z/, "something xor itself is zeros");
265}
266
ce399ba6
NC
267foreach my $value ("\243", UTF8Toggle->new("\243")) {
268 is (pack ("A/A", $value), pack ("A/A", "\243"),
269 "pack copes with overloading");
270 is (pack ("A/A", $value), pack ("A/A", "\243"));
271 is (pack ("A/A", $value), pack ("A/A", "\243"));
272}
273
ab8be49d
NC
274foreach my $value ("\243", UTF8Toggle->new("\243")) {
275 my $v;
276 $v = substr $value, 0, 1;
277 is ($v, "\243");
278 $v = substr $value, 0, 1;
279 is ($v, "\243");
280 $v = substr $value, 0, 1;
281 is ($v, "\243");
282}
283
284{
285 package RT69422;
286 use overload '""' => sub { $_[0]->{data} }
287}
288
289{
290 my $text = bless { data => "\x{3075}" }, 'RT69422';
291 my $p = substr $text, 0, 1;
292 is ($p, "\x{3075}");
293}