File: 060_surrogates.t

package info (click to toggle)
libunicode-utf8-perl 0.60-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 464 kB
  • ctags: 868
  • sloc: perl: 2,337; makefile: 10
file content (49 lines) | stat: -rw-r--r-- 1,199 bytes parent folder | download
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
#!perl

use strict;
use warnings;

use Test::More tests => 6145;
use t::Util    qw[throws_ok pack_utf8];

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

my @SURROGATES = (0xD800 .. 0xDFFF);

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

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

    throws_ok {
        use warnings FATAL => 'utf8';
        decode_utf8($octets);
    } qr/Can't decode ill-formed UTF-8 octet sequence/, $name;
}

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

    my $string = do { no warnings 'utf8'; pack('U', $cp) };

    throws_ok { 
        use warnings FATAL => 'utf8';
        encode_utf8($string); 
    } qr/Can't represent surrogate code point/, $name;
}

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

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

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