Commit | Line | Data |
7aa207d6 |
1 | #!./perl |
2 | |
768fd157 |
3 | BEGIN { require "./test.pl"; } |
7aa207d6 |
4 | |
9ef34c7a |
5 | plan(tests => 18); |
7aa207d6 |
6 | |
5c7da53c |
7 | my %templates = ( |
8 | utf8 => 'C0U', |
9 | utf16be => 'n', |
10 | utf16le => 'v', |
11 | ); |
12 | |
13 | sub bytes_to_utf { |
14 | my ($enc, $content, $do_bom) = @_; |
15 | my $template = $templates{$enc}; |
16 | die "Unsupported encoding $enc" unless $template; |
17 | return pack "$template*", ($do_bom ? 0xFEFF : ()), unpack "C*", $content; |
18 | } |
7aa207d6 |
19 | |
20 | sub test { |
21 | my ($enc, $tag, $bom) = @_; |
5c7da53c |
22 | open my $fh, ">", "utf$$.pl" or die "utf.pl: $!"; |
23 | binmode $fh; |
24 | print $fh bytes_to_utf($enc, "$tag\n", $bom); |
25 | close $fh or die $!; |
2d90ac95 |
26 | my $got = do "./utf$$.pl"; |
7aa207d6 |
27 | is($got, $tag); |
28 | } |
29 | |
386ac4df |
30 | for my $bom (0, 1) { |
31 | for my $enc (qw(utf16le utf16be utf8)) { |
32 | for my $value (123, 1234, 12345) { |
33 | test($enc, $value, $bom); |
34 | } |
35 | } |
36 | } |
7aa207d6 |
37 | |
38 | END { |
2d90ac95 |
39 | 1 while unlink "utf$$.pl"; |
7aa207d6 |
40 | } |