This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #61520] Segfault in debugger with tr// and UTF8
[perl5.git] / lib / charnames.t
1 #!./perl
2
3 my @WARN;
4
5 BEGIN {
6     unless(grep /blib/, @INC) {
7         chdir 't' if -d 't';
8         @INC = '../lib';
9         require './test.pl';
10     }
11     $SIG{__WARN__} = sub { push @WARN, @_ };
12 }
13
14 require File::Spec;
15
16 $| = 1;
17
18 print "1..79\n";
19
20 use charnames ':full';
21
22 print "not " unless "Here\N{EXCLAMATION MARK}?" eq "Here!?";
23 print "ok 1\n";
24
25 {
26   use bytes;                    # TEST -utf8 can switch utf8 on
27
28   print "# \$res=$res \$\@='$@'\nnot "
29     if $res = eval <<'EOE'
30 use charnames ":full";
31 "Here: \N{CYRILLIC SMALL LETTER BE}!";
32 1
33 EOE
34       or $@ !~ /above 0xFF/;
35   print "ok 2\n";
36   # print "# \$res=$res \$\@='$@'\n";
37
38   print "# \$res=$res \$\@='$@'\nnot "
39     if $res = eval <<'EOE'
40 use charnames 'cyrillic';
41 "Here: \N{Be}!";
42 1
43 EOE
44       or $@ !~ /CYRILLIC CAPITAL LETTER BE.*above 0xFF/;
45   print "ok 3\n";
46 }
47
48 # If octal representation of unicode char is \0xyzt, then the utf8 is \3xy\2zt
49 if (ord('A') == 65) { # as on ASCII or UTF-8 machines
50     $encoded_be = "\320\261";
51     $encoded_alpha = "\316\261";
52     $encoded_bet = "\327\221";
53     $encoded_deseng = "\360\220\221\215";
54 }
55 else { # EBCDIC where UTF-EBCDIC may be used (this may be 1047 specific since
56        # UTF-EBCDIC is codepage specific)
57     $encoded_be = "\270\102\130";
58     $encoded_alpha = "\264\130";
59     $encoded_bet = "\270\125\130";
60     $encoded_deseng = "\336\102\103\124";
61 }
62
63 sub to_bytes {
64     unpack"U0a*", shift;
65 }
66
67 {
68   use charnames ':full';
69
70   print "not " unless to_bytes("\N{CYRILLIC SMALL LETTER BE}") eq $encoded_be;
71   print "ok 4\n";
72
73   use charnames qw(cyrillic greek :short);
74
75   print "not " unless to_bytes("\N{be},\N{alpha},\N{hebrew:bet}")
76     eq "$encoded_be,$encoded_alpha,$encoded_bet";
77   print "ok 5\n";
78 }
79
80 {
81     use charnames ':full';
82     print "not " unless "\x{263a}" eq "\N{WHITE SMILING FACE}";
83     print "ok 6\n";
84     print "not " unless length("\x{263a}") == 1;
85     print "ok 7\n";
86     print "not " unless length("\N{WHITE SMILING FACE}") == 1;
87     print "ok 8\n";
88     print "not " unless sprintf("%vx", "\x{263a}") eq "263a";
89     print "ok 9\n";
90     print "not " unless sprintf("%vx", "\N{WHITE SMILING FACE}") eq "263a";
91     print "ok 10\n";
92     print "not " unless sprintf("%vx", "\xFF\N{WHITE SMILING FACE}") eq "ff.263a";
93     print "ok 11\n";
94     print "not " unless sprintf("%vx", "\x{ff}\N{WHITE SMILING FACE}") eq "ff.263a";
95     print "ok 12\n";
96 }
97
98 {
99    use charnames qw(:full);
100    use utf8;
101
102     my $x = "\x{221b}";
103     my $named = "\N{CUBE ROOT}";
104
105     print "not " unless ord($x) == ord($named);
106     print "ok 13\n";
107 }
108
109 {
110    use charnames qw(:full);
111    use utf8;
112    print "not " unless "\x{100}\N{CENT SIGN}" eq "\x{100}"."\N{CENT SIGN}";
113    print "ok 14\n";
114 }
115
116 {
117   use charnames ':full';
118
119   print "not "
120       unless to_bytes("\N{DESERET SMALL LETTER ENG}") eq $encoded_deseng;
121   print "ok 15\n";
122 }
123
124 {
125   # 20001114.001
126
127   no utf8; # naked Latin-1
128
129   if (ord("Ä") == 0xc4) { # Try to do this only on Latin-1.
130       use charnames ':full';
131       my $text = "\N{LATIN CAPITAL LETTER A WITH DIAERESIS}";
132       print "not " unless $text eq "\xc4" && ord($text) == 0xc4;
133       print "ok 16\n";
134   } else {
135       print "ok 16 # Skip: not Latin-1\n";
136   }
137 }
138
139 {
140     print "not " unless charnames::viacode(0x1234) eq "ETHIOPIC SYLLABLE SEE";
141     print "ok 17\n";
142
143     # Unused Hebrew.
144     print "not " if defined charnames::viacode(0x0590);
145     print "ok 18\n";
146 }
147
148 {
149     print "not " unless
150         sprintf("%04X", charnames::vianame("GOTHIC LETTER AHSA")) eq "10330";
151     print "ok 19\n";
152
153     print "not " if
154         defined charnames::vianame("NONE SUCH");
155     print "ok 20\n";
156 }
157
158 {
159     # check that caching at least hasn't broken anything
160
161     print "not " unless charnames::viacode(0x1234) eq "ETHIOPIC SYLLABLE SEE";
162     print "ok 21\n";
163
164     print "not " unless
165         sprintf("%04X", charnames::vianame("GOTHIC LETTER AHSA")) eq "10330";
166     print "ok 22\n";
167
168 }
169
170 print "not " unless "\N{CHARACTER TABULATION}" eq "\t";
171 print "ok 23\n";
172
173 print "not " unless "\N{ESCAPE}" eq "\e";
174 print "ok 24\n";
175
176 print "not " unless "\N{NULL}" eq "\c@";
177 print "ok 25\n";
178
179 if ($^O eq 'MacOS')
180 {
181         print "not " unless "\N{CARRIAGE RETURN (CR)}" eq "\n";
182         print "ok 26\n";
183
184         print "not " unless "\N{CARRIAGE RETURN}" eq "\n";
185         print "ok 27\n";
186
187         print "not " unless "\N{CR}" eq "\n";
188         print "ok 28\n";
189 }
190 else
191 {
192         print "not " unless "\N{LINE FEED (LF)}" eq "\n";
193         print "ok 26\n";
194
195         print "not " unless "\N{LINE FEED}" eq "\n";
196         print "ok 27\n";
197
198         print "not " unless "\N{LF}" eq "\n";
199         print "ok 28\n";
200 }
201
202 my $nel = ord("A") == 193 ? qr/^(?:\x15|\x25)$/ : qr/^\x85$/;
203
204 print "not " unless "\N{NEXT LINE (NEL)}" =~ $nel;
205 print "ok 29\n";
206
207 print "not " unless "\N{NEXT LINE}" =~ $nel;
208 print "ok 30\n";
209
210 print "not " unless "\N{NEL}" =~ $nel;
211 print "ok 31\n";
212
213 print "not " unless "\N{BYTE ORDER MARK}" eq chr(0xFEFF);
214 print "ok 32\n";
215
216 print "not " unless "\N{BOM}" eq chr(0xFEFF);
217 print "ok 33\n";
218
219 {
220     use warnings 'deprecated';
221
222     print "not " unless "\N{HORIZONTAL TABULATION}" eq "\t";
223     print "ok 34\n";
224
225     print "not " unless grep { /"HORIZONTAL TABULATION" is deprecated/ } @WARN;
226     print "ok 35\n";
227
228     no warnings 'deprecated';
229
230     print "not " unless "\N{VERTICAL TABULATION}" eq "\013";
231     print "ok 36\n";
232
233     print "not " if grep { /"VERTICAL TABULATION" is deprecated/ } @WARN;
234     print "ok 37\n";
235 }
236
237 print "not " unless charnames::viacode(0xFEFF) eq "ZERO WIDTH NO-BREAK SPACE";
238 print "ok 38\n";
239
240 {
241     use warnings;
242     print "not " unless ord("\N{BOM}") == 0xFEFF;
243     print "ok 39\n";
244 }
245
246 print "not " unless ord("\N{ZWNJ}") == 0x200C;
247 print "ok 40\n";
248
249 print "not " unless ord("\N{ZWJ}") == 0x200D;
250 print "ok 41\n";
251
252 print "not " unless "\N{U+263A}" eq "\N{WHITE SMILING FACE}";
253 print "ok 42\n";
254
255 {
256     print "not " unless
257         0x3093 == charnames::vianame("HIRAGANA LETTER N");
258     print "ok 43\n";
259
260     print "not " unless
261         0x0397 == charnames::vianame("GREEK CAPITAL LETTER ETA");
262     print "ok 44\n";
263 }
264
265 print "not " if defined charnames::viacode(0x110000);
266 print "ok 45\n";
267
268 print "not " if grep { /you asked for U+110000/ } @WARN;
269 print "ok 46\n";
270
271
272 # ---- Alias extensions
273
274 my $alifile = File::Spec->catfile(File::Spec->updir, qw(lib unicore xyzzy_alias.pl));
275 my $i = 0;
276
277 my @prgs;
278 {   local $/ = undef;
279     @prgs = split "\n########\n", <DATA>;
280     }
281
282 my $i = 46;
283 for (@prgs) {
284     my ($code, $exp) = ((split m/\nEXPECT\n/), '$');
285     my ($prog, $fil) = ((split m/\nFILE\n/, $code), "");
286     my $tmpfile = tempfile();
287     open my $tmp, "> $tmpfile" or die "Could not open $tmpfile: $!";
288     print $tmp $prog, "\n";
289     close $tmp or die "Could not close $tmpfile: $!";
290     if ($fil) {
291         $fil .= "\n";
292         open my $ali, "> $alifile" or die "Could not open $alifile: $!";
293         print $ali $fil;
294         close $ali or die "Could not close $alifile: $!";
295         }
296     my $res = runperl( switches => $switch, 
297                        progfile => $tmpfile,
298                        stderr => 1 );
299     my $status = $?;
300     $res =~ s/[\r\n]+$//;
301     $res =~ s/tmp\d+/-/g;                       # fake $prog from STDIN
302     $res =~ s/\n%[A-Z]+-[SIWEF]-.*$//           # clip off DCL status msg
303         if $^O eq "VMS";
304     $exp =~ s/[\r\n]+$//;
305     if ($^O eq "MacOS") {
306         $exp =~ s{(\./)?abc\.pm}{:abc.pm}g;
307         $exp =~ s{./abc}        {:abc}g;
308         }
309     my $pfx = ($res =~ s/^PREFIX\n//);
310     my $rexp = qr{^$exp};
311     if ($res =~ s/^SKIPPED\n//) {
312         print "$results\n";
313         }
314     elsif (($pfx and $res !~ /^\Q$expected/) or
315           (!$pfx and $res !~ $rexp)) {
316         print STDERR
317             "PROG:\n$prog\n",
318             "FILE:\n$fil",
319             "EXPECTED:\n$exp\n",
320             "GOT:\n$res\n";
321         print "not ";
322         }
323     print "ok ", ++$i, "\n";
324     $fil or next;
325     1 while unlink $alifile;
326     }
327
328 # [perl #30409] charnames.pm clobbers default variable
329 $_ = 'foobar';
330 eval "use charnames ':full';";
331 print "not " unless $_ eq 'foobar';
332 print "ok 74\n";
333
334 # Unicode slowdown noted by Phil Pennock, traced to a bug fix in index
335 # SADAHIRO Tomoyuki's suggestion is to ensure that the UTF-8ness of both
336 # arguments are indentical before calling index.
337 # To do this can take advantage of the fact that unicore/Name.pl is 7 bit
338 # (or at least should be). So assert that that it's true here.
339
340 my $names = do "unicore/Name.pl";
341 print defined $names ? "ok 75\n" : "not ok 75\n";
342 if (ord('A') == 65) { # as on ASCII or UTF-8 machines
343   my $non_ascii = $names =~ tr/\0-\177//c;
344   print $non_ascii ? "not ok 76 # $non_ascii\n" : "ok 76\n";
345 } else {
346   print "ok 76\n";
347 }
348
349 # Verify that charnames propagate to eval("")
350 my $evaltry = eval q[ "Eval: \N{LEFT-POINTING DOUBLE ANGLE QUOTATION MARK}" ];
351 if ($@) {
352     print "# $@not ok 77\nnot ok 78\n";
353 } else {
354     print "ok 77\n";
355     print "not " unless $evaltry eq "Eval: \N{LEFT-POINTING DOUBLE ANGLE QUOTATION MARK}";
356     print "ok 78\n";
357 }
358
359 # Verify that db includes the normative NameAliases.txt names
360 print "not " unless "\N{U+1D0C5}" eq "\N{BYZANTINE MUSICAL SYMBOL FTHORA SKLIRON CHROMA VASIS}";
361 print "ok 79\n";
362
363 __END__
364 # unsupported pragma
365 use charnames ":scoobydoo";
366 "Here: \N{e_ACUTE}!\n";
367 EXPECT
368 unsupported special ':scoobydoo' in charnames at
369 ########
370 # wrong type of alias (missing colon)
371 use charnames "alias";
372 "Here: \N{e_ACUTE}!\n";
373 EXPECT
374 Unknown charname 'e_ACUTE' at
375 ########
376 # alias without an argument
377 use charnames ":alias";
378 "Here: \N{e_ACUTE}!\n";
379 EXPECT
380 :alias needs an argument in charnames at
381 ########
382 # reversed sequence
383 use charnames ":alias" => ":full";
384 "Here: \N{e_ACUTE}!\n";
385 EXPECT
386 :alias cannot use existing pragma :full \(reversed order\?\) at
387 ########
388 # alias with hashref but no :full
389 use charnames ":alias" => { e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE" };
390 "Here: \N{e_ACUTE}!\n";
391 EXPECT
392 Unknown charname 'LATIN SMALL LETTER E WITH ACUTE' at
393 ########
394 # alias with hashref but with :short
395 use charnames ":short", ":alias" => { e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE" };
396 "Here: \N{e_ACUTE}!\n";
397 EXPECT
398 Unknown charname 'LATIN SMALL LETTER E WITH ACUTE' at
399 ########
400 # alias with hashref to :full OK
401 use charnames ":full", ":alias" => { e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE" };
402 "Here: \N{e_ACUTE}!\n";
403 EXPECT
404 $
405 ########
406 # alias with hashref to :short but using :full
407 use charnames ":full", ":alias" => { e_ACUTE => "LATIN:e WITH ACUTE" };
408 "Here: \N{e_ACUTE}!\n";
409 EXPECT
410 Unknown charname 'LATIN:e WITH ACUTE' at
411 ########
412 # alias with hashref to :short OK
413 use charnames ":short", ":alias" => { e_ACUTE => "LATIN:e WITH ACUTE" };
414 "Here: \N{e_ACUTE}!\n";
415 EXPECT
416 $
417 ########
418 # alias with bad hashref
419 use charnames ":short", ":alias" => "e_ACUTE";
420 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
421 EXPECT
422 unicore/e_ACUTE_alias.pl cannot be used as alias file for charnames at
423 ########
424 # alias with arrayref
425 use charnames ":short", ":alias" => [ e_ACUTE => "LATIN:e WITH ACUTE" ];
426 "Here: \N{e_ACUTE}!\n";
427 EXPECT
428 Only HASH reference supported as argument to :alias at
429 ########
430 # alias with bad hashref
431 use charnames ":short", ":alias" => { e_ACUTE => "LATIN:e WITH ACUTE", "a_ACUTE" };
432 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
433 EXPECT
434 Use of uninitialized value
435 ########
436 # alias with hashref two aliases
437 use charnames ":short", ":alias" => {
438     e_ACUTE => "LATIN:e WITH ACUTE",
439     a_ACUTE => "",
440     };
441 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
442 EXPECT
443 Unknown charname '' at
444 ########
445 # alias with hashref two aliases
446 use charnames ":short", ":alias" => {
447     e_ACUTE => "LATIN:e WITH ACUTE",
448     a_ACUTE => "LATIN:a WITH ACUTE",
449     };
450 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
451 EXPECT
452 $
453 ########
454 # alias with hashref using mixed aliasses
455 use charnames ":short", ":alias" => {
456     e_ACUTE => "LATIN:e WITH ACUTE",
457     a_ACUTE => "LATIN SMALL LETTER A WITH ACUT",
458     };
459 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
460 EXPECT
461 Unknown charname 'LATIN SMALL LETTER A WITH ACUT' at
462 ########
463 # alias with hashref using mixed aliasses
464 use charnames ":short", ":alias" => {
465     e_ACUTE => "LATIN:e WITH ACUTE",
466     a_ACUTE => "LATIN SMALL LETTER A WITH ACUTE",
467     };
468 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
469 EXPECT
470 Unknown charname 'LATIN SMALL LETTER A WITH ACUTE' at
471 ########
472 # alias with hashref using mixed aliasses
473 use charnames ":full", ":alias" => {
474     e_ACUTE => "LATIN:e WITH ACUTE",
475     a_ACUTE => "LATIN SMALL LETTER A WITH ACUTE",
476     };
477 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
478 EXPECT
479 Unknown charname 'LATIN:e WITH ACUTE' at
480 ########
481 # alias with nonexisting file
482 use charnames ":full", ":alias" => "xyzzy";
483 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
484 EXPECT
485 unicore/xyzzy_alias.pl cannot be used as alias file for charnames at
486 ########
487 # alias with bad file name
488 use charnames ":full", ":alias" => "xy 7-";
489 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
490 EXPECT
491 Charnames alias files can only have identifier characters at
492 ########
493 # alias with non_absolute (existing) file name (which it should /not/ use)
494 use charnames ":full", ":alias" => "perl";
495 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
496 EXPECT
497 unicore/perl_alias.pl cannot be used as alias file for charnames at
498 ########
499 # alias with bad file
500 use charnames ":full", ":alias" => "xyzzy";
501 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
502 FILE
503 #!perl
504 0;
505 EXPECT
506 unicore/xyzzy_alias.pl did not return a \(valid\) list of alias pairs at
507 ########
508 # alias with file with empty list
509 use charnames ":full", ":alias" => "xyzzy";
510 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
511 FILE
512 #!perl
513 ();
514 EXPECT
515 Unknown charname 'e_ACUTE' at
516 ########
517 # alias with file OK but file has :short aliasses
518 use charnames ":full", ":alias" => "xyzzy";
519 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
520 FILE
521 #!perl
522 (   e_ACUTE => "LATIN:e WITH ACUTE",
523     a_ACUTE => "LATIN:a WITH ACUTE",
524     );
525 EXPECT
526 Unknown charname 'LATIN:e WITH ACUTE' at
527 ########
528 # alias with :short and file OK
529 use charnames ":short", ":alias" => "xyzzy";
530 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
531 FILE
532 #!perl
533 (   e_ACUTE => "LATIN:e WITH ACUTE",
534     a_ACUTE => "LATIN:a WITH ACUTE",
535     );
536 EXPECT
537 $
538 ########
539 # alias with :short and file OK has :long aliasses
540 use charnames ":short", ":alias" => "xyzzy";
541 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
542 FILE
543 #!perl
544 (   e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE",
545     a_ACUTE => "LATIN SMALL LETTER A WITH ACUTE",
546     );
547 EXPECT
548 Unknown charname 'LATIN SMALL LETTER E WITH ACUTE' at
549 ########
550 # alias with file implicit :full but file has :short aliasses
551 use charnames ":alias" => ":xyzzy";
552 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
553 FILE
554 #!perl
555 (   e_ACUTE => "LATIN:e WITH ACUTE",
556     a_ACUTE => "LATIN:a WITH ACUTE",
557     );
558 EXPECT
559 Unknown charname 'LATIN:e WITH ACUTE' at
560 ########
561 # alias with file implicit :full and file has :long aliasses
562 use charnames ":alias" => ":xyzzy";
563 "Here: \N{e_ACUTE}\N{a_ACUTE}!\n";
564 FILE
565 #!perl
566 (   e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE",
567     a_ACUTE => "LATIN SMALL LETTER A WITH ACUTE",
568     );
569 EXPECT
570 $