Tests for UTF-16 characters > 256, including those containing the octet 10.
[p5sagit/p5-mst-13.2.git] / t / comp / utf.t
CommitLineData
61ad1ccd 1#!./perl -w
7aa207d6 2
02512a66 3print "1..76\n";
61ad1ccd 4my $test = 0;
7aa207d6 5
5c7da53c 6my %templates = (
7 utf8 => 'C0U',
8 utf16be => 'n',
9 utf16le => 'v',
10 );
11
12sub bytes_to_utf {
13 my ($enc, $content, $do_bom) = @_;
14 my $template = $templates{$enc};
15 die "Unsupported encoding $enc" unless $template;
02512a66 16 return pack "$template*", ($do_bom ? 0xFEFF : ()), unpack "U*", $content;
5c7da53c 17}
7aa207d6 18
19sub test {
02512a66 20 my ($enc, $write, $expect, $bom, $nl, $name) = @_;
5c7da53c 21 open my $fh, ">", "utf$$.pl" or die "utf.pl: $!";
22 binmode $fh;
02512a66 23 print $fh bytes_to_utf($enc, $write . ($nl ? "\n" : ''), $bom);
5c7da53c 24 close $fh or die $!;
2d90ac95 25 my $got = do "./utf$$.pl";
61ad1ccd 26 $test = $test + 1;
27 if (!defined $got) {
02512a66 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";
61ad1ccd 31 } else {
02512a66 32 print "ok $test # $enc $bom $nl $name\n";
61ad1ccd 33 }
7aa207d6 34}
35
386ac4df 36for my $bom (0, 1) {
37 for my $enc (qw(utf16le utf16be utf8)) {
02512a66 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);
c28d6105 54 }
386ac4df 55 }
56 }
57}
7aa207d6 58
59END {
2d90ac95 60 1 while unlink "utf$$.pl";
7aa207d6 61}