This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re-write S_utf16_textfilter() to correctly handle partial reads of UTF-16.
[perl5.git] / t / comp / utf.t
index d2037a2..c1a3e82 100644 (file)
@@ -1,48 +1,48 @@
-#!./perl
-
-BEGIN {
-    chdir 't' if -d 't';
-    @INC = '../lib';
-    unless (find PerlIO::Layer 'perlio') {
-       print "1..0 # Skip: not perlio\n";
-       exit 0;
-    }
+#!./perl -w
+
+print "1..36\n";
+my $test = 0;
+
+my %templates = (
+                utf8 => 'C0U',
+                utf16be => 'n',
+                utf16le => 'v',
+               );
+
+sub bytes_to_utf {
+    my ($enc, $content, $do_bom) = @_;
+    my $template = $templates{$enc};
+    die "Unsupported encoding $enc" unless $template;
+    return pack "$template*", ($do_bom ? 0xFEFF : ()), unpack "C*", $content;
 }
 
-require "./test.pl";
-
-plan(tests => 15);
-
-my $BOM = chr(0xFEFF);
-
 sub test {
-    my ($enc, $tag, $bom) = @_;
-    open(UTF_PL, ">:raw:encoding($enc)", "utf.pl")
-       or die "utf.pl($enc,$tag,$bom): $!";
-    print UTF_PL $BOM if $bom;
-    print UTF_PL "$tag\n";
-    close(UTF_PL);
-    my $got = do "./utf.pl";
-    is($got, $tag);
+    my ($enc, $tag, $bom, $nl) = @_;
+    open my $fh, ">", "utf$$.pl" or die "utf.pl: $!";
+    binmode $fh;
+    print $fh bytes_to_utf($enc, $tag . ($nl ? "\n" : ''), $bom);
+    close $fh or die $!;
+    my $got = do "./utf$$.pl";
+    $test = $test + 1;
+    if (!defined $got) {
+       print "not ok $test # $enc $tag $bom $nl; got undef\n";
+    } elsif ($got ne $tag) {
+       print "not ok $test # $enc $tag $bom $nl; got '$got'\n";
+    } else {
+       print "ok $test # $enc $tag $bom $nl\n";
+    }
 }
 
-test("utf16le",    123,   1);
-test("utf16le",    1234,  1);
-test("utf16le",    12345, 1);
-test("utf16be",    123,   1);
-test("utf16be",    1234,  1);
-test("utf16be",    12345, 1);
-test("utf8",       123,   1);
-test("utf8",       1234,  1);
-test("utf8",       12345, 1);
-
-test("utf16le",    123,   0);
-test("utf16le",    1234,  0);
-test("utf16le",    12345, 0);
-test("utf16be",    123,   0);
-test("utf16be",    1234,  0);
-test("utf16be",    12345, 0);
+for my $bom (0, 1) {
+    for my $enc (qw(utf16le utf16be utf8)) {
+       for my $value (123, 1234, 12345) {
+           for my $nl (1, 0) {
+               test($enc, $value, $bom, $nl);
+           }
+       }
+    }
+}
 
 END {
-    1 while unlink "utf.pl";
+    1 while unlink "utf$$.pl";
 }