Tests for UTF-16 characters > 256, including those containing the octet 10.
[p5sagit/p5-mst-13.2.git] / t / comp / utf.t
1 #!./perl -w
2
3 print "1..76\n";
4 my $test = 0;
5
6 my %templates = (
7                  utf8 => 'C0U',
8                  utf16be => 'n',
9                  utf16le => 'v',
10                 );
11
12 sub bytes_to_utf {
13     my ($enc, $content, $do_bom) = @_;
14     my $template = $templates{$enc};
15     die "Unsupported encoding $enc" unless $template;
16     return pack "$template*", ($do_bom ? 0xFEFF : ()), unpack "U*", $content;
17 }
18
19 sub test {
20     my ($enc, $write, $expect, $bom, $nl, $name) = @_;
21     open my $fh, ">", "utf$$.pl" or die "utf.pl: $!";
22     binmode $fh;
23     print $fh bytes_to_utf($enc, $write . ($nl ? "\n" : ''), $bom);
24     close $fh or die $!;
25     my $got = do "./utf$$.pl";
26     $test = $test + 1;
27     if (!defined $got) {
28         print "not ok $test # $enc $bom $nl $name; got undef\n";
29     } elsif ($got ne $expect) {
30         print "not ok $test # $enc $bom $nl $name; got '$got'\n";
31     } else {
32         print "ok $test # $enc $bom $nl $name\n";
33     }
34 }
35
36 for my $bom (0, 1) {
37     for my $enc (qw(utf16le utf16be utf8)) {
38         for my $nl (1, 0) {
39             for my $value (123, 1234, 12345) {
40                 test($enc, $value, $value, $bom, $nl, $value);
41             }
42             next if $enc eq 'utf8';
43             # Arguably a bug that currently string literals from UTF-8 file
44             # handles are not implicitly "use utf8", but don't FIXME that
45             # right now, as here we're testing the input filter itself.
46
47             for my $expect ("N", "\xFF", "\x{100}", "\x{010a}", "\x{0a23}",
48                            ) {
49                 # A space so that the UTF-16 heuristc triggers - " '" gives two
50                 # characters of ASCII.
51                 my $write = " '$expect'";
52                 my $name = 'chrs ' . join ', ', map {ord $_} split '', $expect;
53                 test($enc, $write, $expect, $bom, $nl, $name);
54             }
55         }
56     }
57 }
58
59 END {
60     1 while unlink "utf$$.pl";
61 }