This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlfunc: re-document old split() @_ side effect
[perl5.git] / lib / utf8.t
CommitLineData
f96ec2a2
GS
1#!./perl
2
47520729
JH
3my $has_perlio;
4
f96ec2a2
GS
5BEGIN {
6 chdir 't' if -d 't';
20822f61 7 @INC = '../lib';
d591e493 8 require './test.pl'; require './charset_tools.pl';
47520729
JH
9 unless ($has_perlio = find PerlIO::Layer 'perlio') {
10 print <<EOF;
11# Since you don't have perlio you might get failures with UTF-8 locales.
12EOF
13 }
f96ec2a2
GS
14}
15
e4dc48dc
NC
16use strict;
17use warnings;
e9f01ab9
JH
18no utf8; # Ironic, no?
19
4765795a
JH
20# NOTE!
21#
22# Think carefully before adding tests here. In general this should be
23# used only for about three categories of tests:
24#
25# (1) tests that absolutely require 'use utf8', and since that in general
26# shouldn't be needed as the utf8 is being obsoleted, this should
27# have rather few tests. If you want to test Unicode and regexes,
28# you probably want to go to op/regexp or op/pat; if you want to test
29# split, go to op/split; pack, op/pack; appending or joining,
30# op/append or op/join, and so forth
31#
32# (2) tests that have to do with Unicode tokenizing (though it's likely
33# that all the other Unicode tests sprinkled around the t/**/*.t are
34# going to catch that)
35#
36# (3) complicated tests that simultaneously stress so many Unicode features
37# that deciding into which other test script the tests should go to
38# is hard -- maybe consider breaking up the complicated test
39#
40#
41
7bbb0251 42{
126fe723 43 # bug id 20001009.001 (#4409)
da450f52 44
89491803
SC
45 my ($a, $b);
46
d591e493
KW
47 { use bytes; $a = byte_utf8a_to_utf8n("\xc3\xa4") }
48 { use utf8; $b = uni_to_native("\xe4") }
89491803 49
4765795a 50 my $test = 68;
31067593 51
4765795a 52 ok($a ne $b);
da450f52 53
4765795a 54 { use utf8; ok($a ne $b) }
da450f52
JH
55}
56
60ff4832
JH
57
58{
126fe723 59 # bug id 20000730.004 (#3599)
60ff4832 60
60ff4832
JH
61 my $smiley = "\x{263a}";
62
4765795a
JH
63 for my $s ("\x{263a}",
64 $smiley,
60ff4832 65
4765795a
JH
66 "" . $smiley,
67 "" . "\x{263a}",
60ff4832 68
4765795a
JH
69 $smiley . "",
70 "\x{263a}" . "",
60ff4832
JH
71 ) {
72 my $length_chars = length($s);
73 my $length_bytes;
74 { use bytes; $length_bytes = length($s) }
75 my @regex_chars = $s =~ m/(.)/g;
76 my $regex_chars = @regex_chars;
77 my @split_chars = split //, $s;
78 my $split_chars = @split_chars;
4765795a
JH
79 ok("$length_chars/$regex_chars/$split_chars/$length_bytes" eq
80 "1/1/1/3");
60ff4832
JH
81 }
82
4765795a
JH
83 for my $s ("\x{263a}" . "\x{263a}",
84 $smiley . $smiley,
60ff4832 85
4765795a
JH
86 "\x{263a}\x{263a}",
87 "$smiley$smiley",
60ff4832 88
4765795a
JH
89 "\x{263a}" x 2,
90 $smiley x 2,
60ff4832
JH
91 ) {
92 my $length_chars = length($s);
93 my $length_bytes;
94 { use bytes; $length_bytes = length($s) }
95 my @regex_chars = $s =~ m/(.)/g;
96 my $regex_chars = @regex_chars;
97 my @split_chars = split //, $s;
98 my $split_chars = @split_chars;
4765795a
JH
99 ok("$length_chars/$regex_chars/$split_chars/$length_bytes" eq
100 "2/2/2/6");
60ff4832
JH
101 }
102}
ffc61ed2 103
ffc61ed2
JH
104
105{
f9a63242
JH
106 my $w = 0;
107 local $SIG{__WARN__} = sub { print "#($_[0])\n"; $w++ };
108 my $x = eval q/"\\/ . "\x{100}" . q/"/;;
109
4765795a 110 ok($w == 0 && $x eq "\x{100}");
f9a63242
JH
111}
112
8ae6c9f9 113{
4c26891c
NC
114 my $show = q(
115 sub show {
116 my $result;
117 $result .= '>' . join (',', map {ord} split //, $_) . '<'
118 foreach @_;
119 $result;
120 }
121 1;
122 );
123 eval $show or die $@; # We don't expect this sub definition to fail.
8ae6c9f9 124 my $progfile = 'utf' . $$;
435e7af6
NC
125 END {unlink_all $progfile}
126
679f623f
KW
127 # 64 is '@' in ASCII, ' ' in EBCDIC
128 # 193 is not punctuation in either ASCII nor EBCDIC
435e7af6 129 my (@char);
679f623f 130 foreach (64, 193, 257, 65532) {
435e7af6
NC
131 my $char = chr $_;
132 utf8::encode($char);
133 # I don't want to use map {ord} and I've no need to hardcode the UTF
134 # version
135 my $charsubst = $char;
136 $charsubst =~ s/(.)/ord ($1) . ','/ge;
137 chop $charsubst;
4c26891c
NC
138 # Not testing this one against map {ord}
139 my $char_as_ord
140 = join " . ", map {sprintf 'chr (%d)', ord $_} split //, $char;
141 push @char, [$_, $char, $charsubst, $char_as_ord];
435e7af6 142 }
4c26891c
NC
143 # Now we've done all the UTF8 munching hopefully we're safe
144 my @tests = (
435e7af6 145 ['check our detection program works',
679f623f 146 'my @a = ("'.chr(64).'\x2A", ""); $b = show @a', qr/^>64,42<><$/],
435e7af6 147 ['check literal 8 bit input',
679f623f 148 '$a = "' . chr (193) . '"; $b = show $a', qr/^>193<$/],
435e7af6 149 ['check no utf8; makes no change',
679f623f 150 'no utf8; $a = "' . chr (193) . '"; $b = show $a', qr/^>193<$/],
435e7af6
NC
151 # Now we do the real byte sequences that are valid UTF8
152 (map {
153 ["the utf8 sequence for chr $_->[0]",
347a2837 154 qq{\$a = "$_->[1]"; \$b = show \$a}, qr/^>$_->[2]<$/],
435e7af6 155 ["no utf8; for the utf8 sequence for chr $_->[0]",
4c26891c 156 qq(no utf8; \$a = "$_->[1]"; \$b = show \$a), qr/^>$_->[2]<$/],
435e7af6 157 ["use utf8; for the utf8 sequence for chr $_->[0]",
4c26891c 158 qq(use utf8; \$a = "$_->[1]"; \$b = show \$a), qr/^>$_->[0]<$/],
435e7af6
NC
159 } @char),
160 # Interpolation of hex characters needs to take place now, as we're
161 # testing feeding malformed utf8 into perl. Bug now fixed was an
162 # "out of memory" error. We really need the "" [rather than qq()
163 # or q()] to get the best explosion.
164 ["!Feed malformed utf8 into perl.", <<"BANG",
8ae6c9f9 165 use utf8; %a = ("\xE1\xA0"=>"sterling");
435e7af6 166 print 'start'; printf '%x,', ord \$_ foreach keys %a; print "end\n";
8ae6c9f9 167BANG
746b446a 168 qr/^Malformed UTF-8 character \(\d bytes?, need \d, .+\).*start\d+,end$/sm
435e7af6 169 ],
4c26891c
NC
170 );
171 foreach (@tests) {
435e7af6
NC
172 my ($why, $prog, $expect) = @$_;
173 open P, ">$progfile" or die "Can't open '$progfile': $!";
47520729 174 binmode(P, ":bytes") if $has_perlio;
4c26891c
NC
175 print P $show, $prog, '; print $b'
176 or die "Print to 'progfile' failed: $!";
435e7af6
NC
177 close P or die "Can't close '$progfile': $!";
178 if ($why =~ s/^!//) {
179 print "# Possible delay...\n";
180 } else {
181 print "# $prog\n";
182 }
183 my $result = runperl ( stderr => 1, progfile => $progfile );
184 like ($result, $expect, $why);
185 }
4c26891c
NC
186 print
187 "# Again! Again! [but this time as eval, and not the explosive one]\n";
188 # and now we've safely done them all as separate files, check that the
a6d05634 189 # evals do the same thing. Hopefully doing it later successfully decouples
4c26891c
NC
190 # the previous tests from anything messy that may go wrong with the evals.
191 foreach (@tests) {
192 my ($why, $prog, $expect) = @$_;
193 next if $why =~ m/^!/; # Goes bang.
194 my $result = eval $prog;
195 if ($@) {
196 print "# prog is $prog\n";
197 print "# \$\@=", _qq($@), "\n";
198 }
199 like ($result, $expect, $why);
200 }
201
202 # See what the tokeniser does with hash keys.
203 print "# What does the tokeniser do with utf8 hash keys?\n";
204 @tests = (map {
205 # This is the control - I don't expect it to fail
206 ["assign utf8 for chr $_->[0] to a hash",
207 qq(my \$a = "$_->[1]"; my %h; \$h{\$a} = 1;
208 my \$b = show keys %h; \$b .= 'F' unless \$h{$_->[3]}; \$b),
209 qr/^>$_->[2]<$/],
210 ["no utf8; assign utf8 for chr $_->[0] to a hash",
211 qq(no utf8; my \$a = "$_->[1]"; my %h; \$h{\$a} = 1;
212 my \$b = show keys %h; \$b .= 'F' unless \$h{$_->[3]}; \$b),
213 qr/^>$_->[2]<$/],
214 ["use utf8; assign utf8 for chr $_->[0] to a hash",
215 qq(use utf8; my \$a = "$_->[1]"; my %h; \$h{\$a} = 1;
216 my \$b = show keys %h; \$b .= 'F' unless \$h{chr $_->[0]}; \$b),
217 qr/^>$_->[0]<$/],
218 # Now check literal $h{"x"} constructions.
219 ["\$h{\"x\"} construction, where x is utf8 for chr $_->[0]",
220 qq(my \$a = "$_->[1]"; my %h; \$h{"$_->[1]"} = 1;
221 my \$b = show keys %h; \$b .= 'F' unless \$h{$_->[3]}; \$b),
222 qr/^>$_->[2]<$/],
223 ["no utf8; \$h{\"x\"} construction, where x is utf8 for chr $_->[0]",
224 qq(no utf8; my \$a = "$_->[1]"; my %h; \$h{"$_->[1]"} = 1;
225 my \$b = show keys %h; \$b .= 'F' unless \$h{$_->[3]}; \$b),
226 qr/^>$_->[2]<$/],
227 ["use utf8; \$h{\"x\"} construction, where x is utf8 for chr $_->[0]",
228 qq(use utf8; my \$a = "$_->[1]"; my %h; \$h{"$_->[1]"} = 1;
229 my \$b = show keys %h; \$b .= 'F' unless \$h{chr $_->[0]}; \$b),
230 qr/^>$_->[0]<$/],
231 # Now check "x" => constructions.
232 ["assign \"x\"=>1 to a hash, where x is utf8 for chr $_->[0]",
233 qq(my \$a = "$_->[1]"; my %h; %h = ("$_->[1]" => 1);
234 my \$b = show keys %h; \$b .= 'F' unless \$h{$_->[3]}; \$b),
235 qr/^>$_->[2]<$/],
236 ["no utf8; assign \"x\"=>1 to a hash, where x is utf8 for chr $_->[0]",
237 qq(no utf8; my \$a = "$_->[1]"; my %h; %h = ("$_->[1]" => 1);
238 my \$b = show keys %h; \$b .= 'F' unless \$h{$_->[3]}; \$b),
239 qr/^>$_->[2]<$/],
240 ["use utf8; assign \"x\"=>1 to a hash, where x is utf8 for chr $_->[0]",
241 qq(use utf8; my \$a = "$_->[1]"; my %h; %h = ("$_->[1]" => 1);
242 my \$b = show keys %h; \$b .= 'F' unless \$h{chr $_->[0]}; \$b),
243 qr/^>$_->[0]<$/],
244 # Check copies of hashes made from literal utf8 keys
245 ["assign utf8 for chr $_->[0] to a hash, then copy it",
246 qq(my \$a = "$_->[1]"; my %i; \$i{\$a} = 1; my %h = %i;
247 my \$b = show keys %h; \$b .= 'F' unless \$h{$_->[3]}; \$b),
248 qr/^>$_->[2]<$/],
249 ["no utf8; assign utf8 for chr $_->[0] to a hash, then copy it",
250 qq(no utf8; my \$a = "$_->[1]"; my %i; \$i{\$a} = 1;; my %h = %i;
251 my \$b = show keys %h; \$b .= 'F' unless \$h{$_->[3]}; \$b),
252 qr/^>$_->[2]<$/],
253 ["use utf8; assign utf8 for chr $_->[0] to a hash, then copy it",
254 qq(use utf8; my \$a = "$_->[1]"; my %i; \$i{\$a} = 1; my %h = %i;
255 my \$b = show keys %h; \$b .= 'F' unless \$h{chr $_->[0]}; \$b),
256 qr/^>$_->[0]<$/],
257 } @char);
258 foreach (@tests) {
259 my ($why, $prog, $expect) = @$_;
260 # print "# $prog\n";
261 my $result = eval $prog;
262 like ($result, $expect, $why);
263 }
8ae6c9f9 264}
107c2363
MP
265
266#
267# bug fixed by change #17928
268# separate perl used because we rely on 'strict' not yet loaded;
269# before the patch, the eval died with an error like:
270# "my" variable $strict::VERSION can't be in a package
271#
a901eef8 272SKIP: {
679f623f
KW
273 skip("Haven't bothered to port this to EBCDIC non-1047", 1) if $::IS_EBCDIC
274 && ord '^' != 95;
275 if ($::IS_ASCII) {
276 ok('' eq runperl(prog => <<'CODE'), "change #17928");
277 my $code = qq{ my \$\xe3\x83\x95\xe3\x83\xbc = 5; };
278 {
279 use utf8;
280 eval $code;
281 print $@ if $@;
282 }
283CODE
107c2363 284 }
679f623f
KW
285 else {
286 ok('' eq runperl(prog => <<'CODE'), "change #17928");
287 my $code = qq{ my \$\xCE\x47\x64\xCE\x48\x70 = 5; };
288 {
289 use utf8;
290 eval $code;
291 print $@ if $@;
292 }
107c2363 293CODE
679f623f 294 }
a901eef8 295}
a5b510f2
AE
296
297{
298 use utf8;
299 $a = <<'END';
3000 ....... 1 ....... 2 ....... 3 ....... 4 ....... 5 ....... 6 ....... 7 .......
301END
302 my (@i, $s);
303
304 @i = ();
305 push @i, $s = index($a, '6'); # 60
306 push @i, $s = index($a, '.', $s); # next . after 60 is 62
307 push @i, $s = index($a, '5'); # 50
308 push @i, $s = index($a, '.', $s); # next . after 52 is 52
309 push @i, $s = index($a, '7'); # 70
310 push @i, $s = index($a, '.', $s); # next . after 70 is 72
311 push @i, $s = index($a, '4'); # 40
312 push @i, $s = index($a, '.', $s); # next . after 40 is 42
313 is("@i", "60 62 50 52 70 72 40 42", "utf8 heredoc index");
314
315 @i = ();
316 push @i, $s = rindex($a, '6'); # 60
317 push @i, $s = rindex($a, '.', $s); # previous . before 60 is 58
318 push @i, $s = rindex($a, '5'); # 50
319 push @i, $s = rindex($a, '.', $s); # previous . before 52 is 48
320 push @i, $s = rindex($a, '7'); # 70
321 push @i, $s = rindex($a, '.', $s); # previous . before 70 is 68
322 push @i, $s = rindex($a, '4'); # 40
323 push @i, $s = rindex($a, '.', $s); # previous . before 40 is 38
324 is("@i", "60 58 50 48 70 68 40 38", "utf8 heredoc rindex");
325
326 @i = ();
327 push @i, $s = index($a, '6'); # 60
328 push @i, index($a, '.', $s); # next . after 60 is 62
329 push @i, rindex($a, '.', $s); # previous . before 60 is 58
330 push @i, $s = rindex($a, '5'); # 60
331 push @i, index($a, '.', $s); # next . after 50 is 52
332 push @i, rindex($a, '.', $s); # previous . before 50 is 48
333 push @i, $s = index($a, '7', $s); # 70
334 push @i, index($a, '.', $s); # next . after 70 is 72
335 push @i, rindex($a, '.', $s); # previous . before 70 is 68
336 is("@i", "60 62 58 50 52 48 70 72 68", "utf8 heredoc index and rindex");
337}
338
a901eef8 339SKIP: {
679f623f
KW
340 skip("Haven't bothered to port this to EBCDIC non-1047", 1) if $::IS_EBCDIC
341 && ord '^' != 95;
220e2d4e 342 use utf8;
679f623f
KW
343 if ($::IS_ASCII) {
344 is eval qq{q \xc3\xbc test \xc3\xbc . qq\xc2\xb7 test \xc2\xb7},
345 ' test test ',
346 "utf8 quote delimiters [perl #16823]";
347 }
348 else {
349 is eval qq{q \x8B\x70 test \x8B\x70 . qq\x80\x66 test \x80\x66},
350 ' test test ',
351 "utf8 quote delimiters [perl #16823]";
352 }
220e2d4e 353}
6e37fd2a
JH
354
355# Test the "internals".
356
357{
358 my $a = "A";
359 my $b = chr(0x0FF);
7adaf5b2
KW
360 my $c = chr(0x0DF); # FF is invariant in many EBCDIC pages, so is not a
361 # fair test of 'beyond'; but DF is variant (in all
362 # supported EBCDIC pages so far), so make 2 'beyond'
363 # tests
364 my $d = chr(0x100);
6e37fd2a
JH
365
366 ok( utf8::valid($a), "utf8::valid basic");
367 ok( utf8::valid($b), "utf8::valid beyond");
7adaf5b2
KW
368 ok( utf8::valid($c), "utf8::valid beyond");
369 ok( utf8::valid($d), "utf8::valid unicode");
6e37fd2a
JH
370
371 ok(!utf8::is_utf8($a), "!utf8::is_utf8 basic");
372 ok(!utf8::is_utf8($b), "!utf8::is_utf8 beyond");
7adaf5b2
KW
373 ok(!utf8::is_utf8($c), "!utf8::is_utf8 beyond");
374 ok( utf8::is_utf8($d), "utf8::is_utf8 unicode");
6e37fd2a
JH
375
376 is(utf8::upgrade($a), 1, "utf8::upgrade basic");
83bcbc61 377 if ($::IS_EBCDIC) { # EBCDIC.
2f3efc97
JH
378 is(utf8::upgrade($b), 1, "utf8::upgrade beyond");
379 } else {
380 is(utf8::upgrade($b), 2, "utf8::upgrade beyond");
381 }
7adaf5b2
KW
382 is(utf8::upgrade($c), 2, "utf8::upgrade beyond");
383 is(utf8::upgrade($d), 2, "utf8::upgrade unicode");
6e37fd2a
JH
384
385 is($a, "A", "basic");
386 is($b, "\xFF", "beyond");
7adaf5b2
KW
387 is($c, "\xDF", "beyond");
388 is($d, "\x{100}", "unicode");
6e37fd2a
JH
389
390 ok( utf8::valid($a), "utf8::valid basic");
391 ok( utf8::valid($b), "utf8::valid beyond");
7adaf5b2
KW
392 ok( utf8::valid($c), "utf8::valid beyond");
393 ok( utf8::valid($d), "utf8::valid unicode");
6e37fd2a
JH
394
395 ok( utf8::is_utf8($a), "utf8::is_utf8 basic");
396 ok( utf8::is_utf8($b), "utf8::is_utf8 beyond");
7adaf5b2
KW
397 ok( utf8::is_utf8($c), "utf8::is_utf8 beyond");
398 ok( utf8::is_utf8($d), "utf8::is_utf8 unicode");
6e37fd2a
JH
399
400 is(utf8::downgrade($a), 1, "utf8::downgrade basic");
401 is(utf8::downgrade($b), 1, "utf8::downgrade beyond");
7adaf5b2 402 is(utf8::downgrade($c), 1, "utf8::downgrade beyond");
6e37fd2a
JH
403
404 is($a, "A", "basic");
405 is($b, "\xFF", "beyond");
7adaf5b2 406 is($c, "\xDF", "beyond");
6e37fd2a
JH
407
408 ok( utf8::valid($a), "utf8::valid basic");
409 ok( utf8::valid($b), "utf8::valid beyond");
7adaf5b2 410 ok( utf8::valid($c), "utf8::valid beyond");
6e37fd2a
JH
411
412 ok(!utf8::is_utf8($a), "!utf8::is_utf8 basic");
413 ok(!utf8::is_utf8($b), "!utf8::is_utf8 beyond");
7adaf5b2 414 ok(!utf8::is_utf8($c), "!utf8::is_utf8 beyond");
6e37fd2a
JH
415
416 utf8::encode($a);
417 utf8::encode($b);
418 utf8::encode($c);
7adaf5b2 419 utf8::encode($d);
6e37fd2a
JH
420
421 is($a, "A", "basic");
83bcbc61 422 if ($::IS_EBCDIC) { # EBCDIC.
2f3efc97
JH
423 is(length($b), 1, "beyond length");
424 } else {
425 is(length($b), 2, "beyond length");
426 }
7adaf5b2
KW
427 is(length($c), 2, "beyond length");
428 is(length($d), 2, "unicode length");
6e37fd2a
JH
429
430 ok(utf8::valid($a), "utf8::valid basic");
431 ok(utf8::valid($b), "utf8::valid beyond");
7adaf5b2
KW
432 ok(utf8::valid($c), "utf8::valid beyond");
433 ok(utf8::valid($d), "utf8::valid unicode");
6e37fd2a
JH
434
435 # encode() clears the UTF-8 flag (unlike upgrade()).
436 ok(!utf8::is_utf8($a), "!utf8::is_utf8 basic");
437 ok(!utf8::is_utf8($b), "!utf8::is_utf8 beyond");
7adaf5b2
KW
438 ok(!utf8::is_utf8($c), "!utf8::is_utf8 beyond");
439 ok(!utf8::is_utf8($d), "!utf8::is_utf8 unicode");
6e37fd2a
JH
440
441 utf8::decode($a);
442 utf8::decode($b);
443 utf8::decode($c);
7adaf5b2 444 utf8::decode($d);
6e37fd2a
JH
445
446 is($a, "A", "basic");
447 is($b, "\xFF", "beyond");
7adaf5b2
KW
448 is($c, "\xDF", "beyond");
449 is($d, "\x{100}", "unicode");
6e37fd2a
JH
450
451 ok(utf8::valid($a), "!utf8::valid basic");
452 ok(utf8::valid($b), "!utf8::valid beyond");
7adaf5b2
KW
453 ok(utf8::valid($c), "!utf8::valid beyond");
454 ok(utf8::valid($d), " utf8::valid unicode");
6e37fd2a
JH
455
456 ok(!utf8::is_utf8($a), "!utf8::is_utf8 basic");
83bcbc61 457 if ($::IS_EBCDIC) { # EBCDIC.
2f3efc97
JH
458 ok( utf8::is_utf8(pack('U',0x0ff)), " utf8::is_utf8 beyond");
459 } else {
460 ok( utf8::is_utf8($b), " utf8::is_utf8 beyond"); # $b stays in UTF-8.
461 }
7adaf5b2
KW
462 ok( utf8::is_utf8($c), " utf8::is_utf8 beyond"); # $c stays in UTF-8.
463 ok( utf8::is_utf8($d), " utf8::is_utf8 unicode");
6e37fd2a 464}
5fec3b1d
NC
465
466{
467 eval {utf8::encode("£")};
468 like($@, qr/^Modification of a read-only value attempted/,
469 "utf8::encode should refuse to touch read-only values");
470}
06a45632
TS
471
472{
b2b7346b
FC
473 # Make sure utf8::decode respects copy-on-write [perl #91834].
474 # Hash keys are the easiest way to test this.
d591e493 475 my $name = byte_utf8a_to_utf8n("\x{c3}\x{b3}");
b2b7346b
FC
476 my ($k1) = keys %{ { $name=>undef } };
477 my $k2 = $name;
478 utf8::decode($k1);
479 utf8::decode($k2);
480 my $h = { $k1 => 1, $k2 => 2 };
1df6869d 481 is join('', keys %$h), $k2, 'utf8::decode respects copy-on-write';
b2b7346b
FC
482}
483
484{
492ebaba
FC
485 # Make sure utf8::decode does not modify read-only scalars
486 # [perl #91850].
487
d591e493 488 my $name = byte_utf8a_to_utf8n("\x{c3}\x{b3}");
492ebaba
FC
489 Internals::SvREADONLY($name, 1);
490 eval { utf8::decode($name) };
491 like $@, qr/^Modification of a read-only/,
492 'utf8::decode respects readonliness';
493}
494
495{
892522a6 496 # utf8::decode should stringify refs [perl #91852].
c7102404 497
d591e493 498 package eieifg { use overload '""' => sub { main::byte_utf8a_to_utf8n("\x{c3}\x{b3}") },
c7102404
FC
499 fallback => 1 }
500
501 my $name = bless[], eieifg::;
502 utf8::decode($name);
d591e493 503 is $name, uni_to_native("\xf3"), 'utf8::decode flattens references';
c7102404
FC
504}
505
506{
64470437
AB
507 # What do the utf8::* functions do when given a reference? A test
508 # for a behavior change that made this start dying as of
509 # v5.15.6-407-gc710240 due to a fix for [perl #91852]:
510 #
511 # ./miniperl -Ilib -wle 'use strict; print $]; my $s = shift; my $s_ref = \$s; utf8::decode($s_ref); print $$s_ref' hlagh
512 my %expected = (
513 'utf8::is_utf8' => { returns => "hlagh" },
514 'utf8::valid' => { returns => "hlagh" },
515 'utf8::encode' => { error => qr/Can't use string .*? as a SCALAR ref/},
516 'utf8::decode' => { error => qr/Can't use string .*? as a SCALAR ref/},
517 'utf8::upgrade' => { error => qr/Can't use string .*? as a SCALAR ref/ },
518 'utf8::downgrade' => { returns => "hlagh" },
519 'utf8::native_to_unicode' => { returns => "hlagh" },
520 'utf8::unicode_to_native' => { returns => "hlagh" },
521 );
522 for my $func (sort keys %expected) { # sort just so it's deterministic wrt diffing *.t output
523 my $code = sprintf q[
524 use strict;
525 my $s = "hlagh";
526 my $r = \$s;
273e254d 527 my $dummy = %s($r);
64470437
AB
528 $$r;
529 ], $func;
530 my $ret = eval $code or my $error = $@;
531 if (my $error_rx = $expected{$func}->{error}) {
532 if (defined $error) {
533 like $error, $error_rx, "The $func function should die with an error matching $error_rx";
534 } else {
535 fail("We were expecting an error when calling the $func function but got a value of '$ret' instead");
536 }
537 } elsif (my $returns = $expected{$func}->{returns}) {
538 is($ret, $returns, "The $func function lives and returns '$returns' as expected");
539 } else {
540 die "PANIC: Internal Error"
541 }
542 }
543}
544
545{
d591e493 546 my $a = "456" . uni_to_native("\xb6");
06a45632
TS
547 utf8::upgrade($a);
548
d591e493 549 my $b = "123456" . uni_to_native("\xb6");
ccc73c2d 550 $b =~ s/^...//;
06a45632
TS
551 utf8::upgrade($b);
552 is($b, $a, "utf8::upgrade OffsetOK");
553}
bd7017d3
NC
554
555{
556 fresh_perl_like ('use utf8; utf8::moo()',
557 qr/Undefined subroutine utf8::moo/, {stderr=>1},
558 "Check Carp is loaded for AUTOLOADing errors")
559}
77263263
TS
560
561{
562 # failure of is_utf8_char() without NATIVE_TO_UTF on EBCDIC (0260..027F)
563 ok(utf8::valid(chr(0x250)), "0x250");
564 ok(utf8::valid(chr(0x260)), "0x260");
565 ok(utf8::valid(chr(0x270)), "0x270");
566 ok(utf8::valid(chr(0x280)), "0x280");
567}
96d9949f
EB
568
569{
570 use utf8;
571 ok( !utf8::is_utf8( "asd" ), "Wasteful format - qq{}" );
572 ok( !utf8::is_utf8( 'asd' ), "Wasteful format - q{}" );
573 ok( !utf8::is_utf8( qw(asd) ), "Wasteful format - qw{}" );
574 ok( !utf8::is_utf8( (asd => 1)[0] ), "Wasteful format - =>" );
96d9949f 575 ok( !utf8::is_utf8( -asd ), "Wasteful format - -word" );
e4dc48dc 576 no warnings 'bareword';
96d9949f 577 ok( !utf8::is_utf8( asd:: ), "Wasteful format - word::" );
e4dc48dc
NC
578 no warnings 'reserved';
579 no strict 'subs';
580 ok( !utf8::is_utf8( asd ), "Wasteful format - bareword" );
96d9949f 581}
a18d6e6e
NC
582
583{
584 my @highest =
585 (undef, 0x7F, 0x7FF, 0xFFFF, 0x1FFFFF, 0x3FFFFFF, 0x7FFFFFFF);
586 my @step =
587 (undef, undef, 0x40, 0x1000, 0x40000, 0x1000000, 0x40000000);
588
589 foreach my $length (6, 5, 4, 3, 2) {
590 my $high = $highest[$length];
591 while ($high > $highest[$length - 1]) {
592 my $low = $high - $step[$length] + 1;
593 $low = $highest[$length - 1] + 1 if $low <= $highest[$length - 1];
594 ok(utf8::valid(do {no warnings 'utf8'; chr $low}),
595 sprintf "chr %x, length $length is valid", $low);
596 ok(utf8::valid(do {no warnings 'utf8'; chr $high}),
597 sprintf "chr %x, length $length is valid", $high);
598 $high -= $step[$length];
599 }
600 }
601}
602
75da9d4c
DM
603# #80190 update pos, and cached length/position-mapping after
604# utf8 upgrade/downgrade, encode/decode
605
606for my $pos (0..5) {
607
75da9d4c 608 my $p;
d591e493
KW
609 my $utf8_bytes = byte_utf8a_to_utf8n("\xc8\x81\xe3\xbf\xbf");
610 my $s = "A$utf8_bytes\x{100}";
75da9d4c
DM
611 chop($s);
612
613 pos($s) = $pos;
614 # also sets cache
615 is(length($s), 6, "(pos $pos) len before utf8::downgrade");
616 is(pos($s), $pos, "(pos $pos) pos before utf8::downgrade");
617 utf8::downgrade($s);
618 is(length($s), 6, "(pos $pos) len after utf8::downgrade");
619 is(pos($s), $pos, "(pos $pos) pos after utf8::downgrade");
d591e493 620 is($s, "A$utf8_bytes","(pos $pos) str after utf8::downgrade");
75da9d4c
DM
621 utf8::decode($s);
622 is(length($s), 3, "(pos $pos) len after D; utf8::decode");
158f7f72 623 is(pos($s), undef, "(pos $pos) pos after D; utf8::decode");
d591e493 624 is($s, "A\x{201}\x{3fff}", "(pos $pos) str after D; utf8::decode");
75da9d4c
DM
625 utf8::encode($s);
626 is(length($s), 6, "(pos $pos) len after D; utf8::encode");
158f7f72 627 is(pos($s), undef, "(pos $pos) pos after D; utf8::encode");
d591e493 628 is($s, "A$utf8_bytes","(pos $pos) str after D; utf8::encode");
75da9d4c 629
d591e493 630 $s = "A$utf8_bytes";
75da9d4c
DM
631
632 pos($s) = $pos;
633 is(length($s), 6, "(pos $pos) len before utf8::upgrade");
634 is(pos($s), $pos, "(pos $pos) pos before utf8::upgrade");
635 utf8::upgrade($s);
636 is(length($s), 6, "(pos $pos) len after utf8::upgrade");
637 is(pos($s), $pos, "(pos $pos) pos after utf8::upgrade");
d591e493 638 is($s, "A$utf8_bytes","(pos $pos) str after utf8::upgrade");
75da9d4c
DM
639 utf8::decode($s);
640 is(length($s), 3, "(pos $pos) len after U; utf8::decode");
158f7f72 641 is(pos($s), undef, "(pos $pos) pos after U; utf8::decode");
d591e493 642 is($s, "A\x{201}\x{3fff}", "(pos $pos) str after U; utf8::decode");
75da9d4c
DM
643 utf8::encode($s);
644 is(length($s), 6, "(pos $pos) len after U; utf8::encode");
158f7f72 645 is(pos($s), undef, "(pos $pos) pos after U; utf8::encode");
d591e493 646 is($s, "A$utf8_bytes","(pos $pos) str after U; utf8::encode");
75da9d4c
DM
647}
648
273e254d
KW
649SKIP: {
650 skip("Test only valid on ASCII platform", 1) unless $::IS_ASCII;
651 require Config;
652 skip("Test needs a B module, which is lacking in this Perl", 1)
653 if $Config::Config{'extensions'} !~ /\bB\b/;
654
655 my $out = runperl ( switches => ["-XMO=Concise"],
656 prog => 'utf8::unicode_to_native(0x41);
657 utf8::native_to_unicode(0x42)',
658 stderr => 1 );
659 unlike($out, qr/entersub/,
660 "utf8::unicode_to_native() and native_to_unicode() optimized out");
661}
662
663
c56ed9f6
FC
664# [perl #119043] utf8::upgrade should not croak on read-only COWs
665for(__PACKAGE__) {
c56ed9f6
FC
666 eval { utf8::upgrade($_) };
667 is $@, "", 'no error with utf8::upgrade on read-only COW';
c56ed9f6
FC
668}
669# This one croaks, but not because the scalar is read-only
670eval "package \x{100};\n" . <<'END'
671 for(__PACKAGE__) {
672 eval { utf8::downgrade($_) };
673 ::like $@, qr/^Wide character/,
674 'right error with utf8::downgrade on read-only COW';
675 }
676 1
677END
678or die $@;
679
a18d6e6e 680done_testing();