File: 050_noncharacters.t

package info (click to toggle)
libunicode-utf8-perl 0.62-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 492 kB
  • sloc: perl: 2,365; sh: 6; makefile: 3
file content (77 lines) | stat: -rwxr-xr-x 1,890 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!perl

use strict;
use warnings;
use lib 't';

use Test::More tests => 463;
use Util       qw[throws_ok warns_ok pack_utf8];

BEGIN {
    use_ok('Unicode::UTF8', qw[ decode_utf8
                                encode_utf8 
                                valid_utf8 ]);
}

my @NONCHARACTERS = (0xFDD0 .. 0xFDEF);
{
    for (my $i = 0; $i < 0x10FFFF; $i += 0x10000) {
        push @NONCHARACTERS, $i ^ 0xFFFE, $i ^ 0xFFFF;
    }
}

foreach my $cp (@NONCHARACTERS) {
    my $octets = pack_utf8($cp);

    my $name = sprintf 'decode_utf8(<%s>) noncharacter U+%.4X',
      join(' ', map { sprintf '%.2X', ord $_ } split //, $octets), $cp;

    my $match = qr/Can't interchange noncharacter code point/;

    throws_ok {
        use warnings FATAL => 'utf8';
        decode_utf8($octets);
    } $match, "$name throws an exception";

    my $got;

    warns_ok {
        use warnings 'utf8';
        $got = decode_utf8($octets);
    } $match, "$name issues a warning";

    is($got, "\x{FFFD}", "$name returned U+FFFD");
}

foreach my $cp (@NONCHARACTERS) {
    my $name = sprintf 'encode_utf8("\\x{%.4X}") noncharacter U+%.4X',
      $cp, $cp;

    my $string = do { no warnings 'utf8'; pack('U', $cp) };
    my $match  = qr/Can't interchange noncharacter code point/;
    my $exp    = do { utf8::encode(my $s = "\x{FFFD}"); $s };

    throws_ok {
        use warnings FATAL => 'utf8';
        encode_utf8($string)
    } $match, "$name throws an exception";

    my $got;

    warns_ok {
        use warnings 'utf8';
        $got = encode_utf8($string)
    } $match, "$name issues a warning";

    is($got, $exp, "$name returned encoded U+FFFD");
}

foreach my $cp (@NONCHARACTERS) {
    my $octets = pack_utf8($cp);

    my $name = sprintf 'valid_utf8(<%s>) noncharacter U+%.4X',
      join(' ', map { sprintf '%.2X', ord $_ } split //, $octets), $cp;

    ok(!valid_utf8($octets), $name);
}