This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlapi: Clarify isUTF8_CHAR()
[perl5.git] / ext / XS-APItest / t / utf8.t
CommitLineData
fed3ba5d
NC
1#!perl -w
2
3use strict;
4use Test::More;
7dfd8446 5$|=1;
fed3ba5d 6
760c7c2f
KW
7no warnings 'deprecated'; # Some of the below are above IV_MAX on 32 bit
8 # machines, and that is tested elsewhere
9
fed3ba5d
NC
10use XS::APItest;
11
4deba822
KW
12my $pound_sign = chr utf8::unicode_to_native(163);
13
7dfd8446
KW
14sub isASCII { ord "A" == 65 }
15
16sub display_bytes {
9d2d0ecd 17 use bytes;
7dfd8446
KW
18 my $string = shift;
19 return '"'
20 . join("", map { sprintf("\\x%02x", ord $_) } split "", $string)
21 . '"';
22}
23
24# This test file can't use byte_utf8a_to_utf8n() from t/charset_tools.pl
25# because that uses the same functions we are testing here. So UTF-EBCDIC
26# strings are hard-coded as I8 strings in this file instead, and we use array
27# lookup to translate into the appropriate code page.
28
29my @i8_to_native = ( # Only code page 1047 so far.
30# _0 _1 _2 _3 _4 _5 _6 _7 _8 _9 _A _B _C _D _E _F
310x00,0x01,0x02,0x03,0x37,0x2D,0x2E,0x2F,0x16,0x05,0x15,0x0B,0x0C,0x0D,0x0E,0x0F,
320x10,0x11,0x12,0x13,0x3C,0x3D,0x32,0x26,0x18,0x19,0x3F,0x27,0x1C,0x1D,0x1E,0x1F,
330x40,0x5A,0x7F,0x7B,0x5B,0x6C,0x50,0x7D,0x4D,0x5D,0x5C,0x4E,0x6B,0x60,0x4B,0x61,
340xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0x7A,0x5E,0x4C,0x7E,0x6E,0x6F,
350x7C,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,
360xD7,0xD8,0xD9,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xAD,0xE0,0xBD,0x5F,0x6D,
370x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,
380x97,0x98,0x99,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xC0,0x4F,0xD0,0xA1,0x07,
390x20,0x21,0x22,0x23,0x24,0x25,0x06,0x17,0x28,0x29,0x2A,0x2B,0x2C,0x09,0x0A,0x1B,
400x30,0x31,0x1A,0x33,0x34,0x35,0x36,0x08,0x38,0x39,0x3A,0x3B,0x04,0x14,0x3E,0xFF,
410x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x51,0x52,0x53,0x54,0x55,0x56,
420x57,0x58,0x59,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x70,0x71,0x72,0x73,
430x74,0x75,0x76,0x77,0x78,0x80,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x9A,0x9B,0x9C,
440x9D,0x9E,0x9F,0xA0,0xAA,0xAB,0xAC,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,
450xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBF,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xDA,0xDB,
460xDC,0xDD,0xDE,0xDF,0xE1,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xFA,0xFB,0xFC,0xFD,0xFE,
47);
48
49*I8_to_native = (isASCII)
50 ? sub { return shift }
51 : sub { return join "", map { chr $i8_to_native[ord $_] }
52 split "", shift };
53
54my $is64bit = length sprintf("%x", ~0) > 8;
55
56
57# Test utf8n_to_uvchr(). These provide essentially complete code coverage.
58# Copied from utf8.h
59my $UTF8_ALLOW_EMPTY = 0x0001;
60my $UTF8_ALLOW_CONTINUATION = 0x0002;
61my $UTF8_ALLOW_NON_CONTINUATION = 0x0004;
62my $UTF8_ALLOW_SHORT = 0x0008;
63my $UTF8_ALLOW_LONG = 0x0010;
64my $UTF8_DISALLOW_SURROGATE = 0x0020;
65my $UTF8_WARN_SURROGATE = 0x0040;
66my $UTF8_DISALLOW_NONCHAR = 0x0080;
67my $UTF8_WARN_NONCHAR = 0x0100;
68my $UTF8_DISALLOW_SUPER = 0x0200;
69my $UTF8_WARN_SUPER = 0x0400;
1d1c12d9
KW
70my $UTF8_DISALLOW_ABOVE_31_BIT = 0x0800;
71my $UTF8_WARN_ABOVE_31_BIT = 0x1000;
7dfd8446
KW
72my $UTF8_CHECK_ONLY = 0x2000;
73
046d01eb
KW
74# Test uvchr_to_utf8().
75my $UNICODE_WARN_SURROGATE = 0x0001;
76my $UNICODE_WARN_NONCHAR = 0x0002;
77my $UNICODE_WARN_SUPER = 0x0004;
78my $UNICODE_WARN_ABOVE_31_BIT = 0x0008;
79my $UNICODE_DISALLOW_SURROGATE = 0x0010;
80my $UNICODE_DISALLOW_NONCHAR = 0x0020;
81my $UNICODE_DISALLOW_SUPER = 0x0040;
82my $UNICODE_DISALLOW_ABOVE_31_BIT = 0x0080;
83
84my $look_for_everything_utf8n_to
85 = $UTF8_DISALLOW_SURROGATE
7dfd8446
KW
86 | $UTF8_WARN_SURROGATE
87 | $UTF8_DISALLOW_NONCHAR
88 | $UTF8_WARN_NONCHAR
89 | $UTF8_DISALLOW_SUPER
90 | $UTF8_WARN_SUPER
1d1c12d9
KW
91 | $UTF8_DISALLOW_ABOVE_31_BIT
92 | $UTF8_WARN_ABOVE_31_BIT;
046d01eb
KW
93my $look_for_everything_uvchr_to
94 = $UNICODE_DISALLOW_SURROGATE
95 | $UNICODE_WARN_SURROGATE
96 | $UNICODE_DISALLOW_NONCHAR
97 | $UNICODE_WARN_NONCHAR
98 | $UNICODE_DISALLOW_SUPER
99 | $UNICODE_WARN_SUPER
100 | $UNICODE_DISALLOW_ABOVE_31_BIT
101 | $UNICODE_WARN_ABOVE_31_BIT;
7dfd8446 102
fed3ba5d
NC
103foreach ([0, '', '', 'empty'],
104 [0, 'N', 'N', '1 char'],
105 [1, 'NN', 'N', '1 char substring'],
106 [-2, 'Perl', 'Rules', 'different'],
4deba822
KW
107 [0, $pound_sign, $pound_sign, 'pound sign'],
108 [1, $pound_sign . 10, $pound_sign . 1, '10 pounds is more than 1 pound'],
109 [1, $pound_sign . $pound_sign, $pound_sign, '2 pound signs are more than 1'],
fed3ba5d
NC
110 [-2, ' $!', " \x{1F42B}!", 'Camels are worth more than 1 dollar'],
111 [-1, '!', "!\x{1F42A}", 'Initial substrings match'],
112 ) {
113 my ($expect, $left, $right, $desc) = @$_;
114 my $copy = $right;
115 utf8::encode($copy);
116 is(bytes_cmp_utf8($left, $copy), $expect, $desc);
117 next if $right =~ tr/\0-\377//c;
118 utf8::encode($left);
119 is(bytes_cmp_utf8($right, $left), -$expect, "$desc reversed");
120}
121
7dfd8446
KW
122# The keys to this hash are Unicode code points, their values are the native
123# UTF-8 representations of them. The code points are chosen because they are
124# "interesting" on either or both ASCII and EBCDIC platforms. First we add
125# boundaries where the number of bytes required to represent them increase, or
126# are adjacent to problematic code points, so we want to make sure they aren't
127# considered problematic.
128my %code_points = (
129 0x0100 => (isASCII) ? "\xc4\x80" : I8_to_native("\xc8\xa0"),
130 0x0400 - 1 => (isASCII) ? "\xcf\xbf" : I8_to_native("\xdf\xbf"),
131 0x0400 => (isASCII) ? "\xd0\x80" : I8_to_native("\xe1\xa0\xa0"),
132 0x0800 - 1 => (isASCII) ? "\xdf\xbf" : I8_to_native("\xe1\xbf\xbf"),
133 0x0800 => (isASCII) ? "\xe0\xa0\x80" : I8_to_native("\xe2\xa0\xa0"),
134 0x4000 - 1 => (isASCII) ? "\xe3\xbf\xbf" : I8_to_native("\xef\xbf\xbf"),
135 0x4000 => (isASCII) ? "\xe4\x80\x80" : I8_to_native("\xf0\xb0\xa0\xa0"),
136 0x8000 - 1 => (isASCII) ? "\xe7\xbf\xbf" : I8_to_native("\xf0\xbf\xbf\xbf"),
137
138 # First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
139 # as of this writing, considers potentially problematic on EBCDIC
140 0x8000 => (isASCII) ? "\xe8\x80\x80" : I8_to_native("\xf1\xa0\xa0\xa0"),
141
142 0xD000 - 1 => (isASCII) ? "\xec\xbf\xbf" : I8_to_native("\xf1\xb3\xbf\xbf"),
143
144 # First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
145 # as of this writing, considers potentially problematic on ASCII
146 0xD000 => (isASCII) ? "\xed\x80\x80" : I8_to_native("\xf1\xb4\xa0\xa0"),
147
148 # Bracket the surrogates
149 0xD7FF => (isASCII) ? "\xed\x9f\xbf" : I8_to_native("\xf1\xb5\xbf\xbf"),
150 0xE000 => (isASCII) ? "\xee\x80\x80" : I8_to_native("\xf1\xb8\xa0\xa0"),
151
152 # Bracket the 32 contiguous non characters
153 0xFDCF => (isASCII) ? "\xef\xb7\x8f" : I8_to_native("\xf1\xbf\xae\xaf"),
154 0xFDF0 => (isASCII) ? "\xef\xb7\xb0" : I8_to_native("\xf1\xbf\xaf\xb0"),
155
156 # Mostly bracket non-characters, but some are transitions to longer
157 # strings
158 0xFFFD => (isASCII) ? "\xef\xbf\xbd" : I8_to_native("\xf1\xbf\xbf\xbd"),
159 0x10000 - 1 => (isASCII) ? "\xef\xbf\xbf" : I8_to_native("\xf1\xbf\xbf\xbf"),
160 0x10000 => (isASCII) ? "\xf0\x90\x80\x80" : I8_to_native("\xf2\xa0\xa0\xa0"),
161 0x1FFFD => (isASCII) ? "\xf0\x9f\xbf\xbd" : I8_to_native("\xf3\xbf\xbf\xbd"),
162 0x20000 => (isASCII) ? "\xf0\xa0\x80\x80" : I8_to_native("\xf4\xa0\xa0\xa0"),
163 0x2FFFD => (isASCII) ? "\xf0\xaf\xbf\xbd" : I8_to_native("\xf5\xbf\xbf\xbd"),
164 0x30000 => (isASCII) ? "\xf0\xb0\x80\x80" : I8_to_native("\xf6\xa0\xa0\xa0"),
165 0x3FFFD => (isASCII) ? "\xf0\xbf\xbf\xbd" : I8_to_native("\xf7\xbf\xbf\xbd"),
166 0x40000 - 1 => (isASCII) ? "\xf0\xbf\xbf\xbf" : I8_to_native("\xf7\xbf\xbf\xbf"),
167 0x40000 => (isASCII) ? "\xf1\x80\x80\x80" : I8_to_native("\xf8\xa8\xa0\xa0\xa0"),
168 0x4FFFD => (isASCII) ? "\xf1\x8f\xbf\xbd" : I8_to_native("\xf8\xa9\xbf\xbf\xbd"),
169 0x50000 => (isASCII) ? "\xf1\x90\x80\x80" : I8_to_native("\xf8\xaa\xa0\xa0\xa0"),
170 0x5FFFD => (isASCII) ? "\xf1\x9f\xbf\xbd" : I8_to_native("\xf8\xab\xbf\xbf\xbd"),
171 0x60000 => (isASCII) ? "\xf1\xa0\x80\x80" : I8_to_native("\xf8\xac\xa0\xa0\xa0"),
172 0x6FFFD => (isASCII) ? "\xf1\xaf\xbf\xbd" : I8_to_native("\xf8\xad\xbf\xbf\xbd"),
173 0x70000 => (isASCII) ? "\xf1\xb0\x80\x80" : I8_to_native("\xf8\xae\xa0\xa0\xa0"),
174 0x7FFFD => (isASCII) ? "\xf1\xbf\xbf\xbd" : I8_to_native("\xf8\xaf\xbf\xbf\xbd"),
175 0x80000 => (isASCII) ? "\xf2\x80\x80\x80" : I8_to_native("\xf8\xb0\xa0\xa0\xa0"),
176 0x8FFFD => (isASCII) ? "\xf2\x8f\xbf\xbd" : I8_to_native("\xf8\xb1\xbf\xbf\xbd"),
177 0x90000 => (isASCII) ? "\xf2\x90\x80\x80" : I8_to_native("\xf8\xb2\xa0\xa0\xa0"),
178 0x9FFFD => (isASCII) ? "\xf2\x9f\xbf\xbd" : I8_to_native("\xf8\xb3\xbf\xbf\xbd"),
179 0xA0000 => (isASCII) ? "\xf2\xa0\x80\x80" : I8_to_native("\xf8\xb4\xa0\xa0\xa0"),
180 0xAFFFD => (isASCII) ? "\xf2\xaf\xbf\xbd" : I8_to_native("\xf8\xb5\xbf\xbf\xbd"),
181 0xB0000 => (isASCII) ? "\xf2\xb0\x80\x80" : I8_to_native("\xf8\xb6\xa0\xa0\xa0"),
182 0xBFFFD => (isASCII) ? "\xf2\xbf\xbf\xbd" : I8_to_native("\xf8\xb7\xbf\xbf\xbd"),
183 0xC0000 => (isASCII) ? "\xf3\x80\x80\x80" : I8_to_native("\xf8\xb8\xa0\xa0\xa0"),
184 0xCFFFD => (isASCII) ? "\xf3\x8f\xbf\xbd" : I8_to_native("\xf8\xb9\xbf\xbf\xbd"),
185 0xD0000 => (isASCII) ? "\xf3\x90\x80\x80" : I8_to_native("\xf8\xba\xa0\xa0\xa0"),
186 0xDFFFD => (isASCII) ? "\xf3\x9f\xbf\xbd" : I8_to_native("\xf8\xbb\xbf\xbf\xbd"),
187 0xE0000 => (isASCII) ? "\xf3\xa0\x80\x80" : I8_to_native("\xf8\xbc\xa0\xa0\xa0"),
188 0xEFFFD => (isASCII) ? "\xf3\xaf\xbf\xbd" : I8_to_native("\xf8\xbd\xbf\xbf\xbd"),
189 0xF0000 => (isASCII) ? "\xf3\xb0\x80\x80" : I8_to_native("\xf8\xbe\xa0\xa0\xa0"),
190 0xFFFFD => (isASCII) ? "\xf3\xbf\xbf\xbd" : I8_to_native("\xf8\xbf\xbf\xbf\xbd"),
191 0x100000 => (isASCII) ? "\xf4\x80\x80\x80" : I8_to_native("\xf9\xa0\xa0\xa0\xa0"),
192 0x10FFFD => (isASCII) ? "\xf4\x8f\xbf\xbd" : I8_to_native("\xf9\xa1\xbf\xbf\xbd"),
193 0x110000 => (isASCII) ? "\xf4\x90\x80\x80" : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
194
195 # Things that would be noncharacters if they were in Unicode, and might be
196 # mistaken, if the C code is bad, to be nonchars
197 0x11FFFE => (isASCII) ? "\xf4\x9f\xbf\xbe" : I8_to_native("\xf9\xa3\xbf\xbf\xbe"),
198 0x11FFFF => (isASCII) ? "\xf4\x9f\xbf\xbf" : I8_to_native("\xf9\xa3\xbf\xbf\xbf"),
199 0x20FFFE => (isASCII) ? "\xf8\x88\x8f\xbf\xbe" : I8_to_native("\xfa\xa1\xbf\xbf\xbe"),
200 0x20FFFF => (isASCII) ? "\xf8\x88\x8f\xbf\xbf" : I8_to_native("\xfa\xa1\xbf\xbf\xbf"),
201
202 0x200000 - 1 => (isASCII) ? "\xf7\xbf\xbf\xbf" : I8_to_native("\xf9\xbf\xbf\xbf\xbf"),
203 0x200000 => (isASCII) ? "\xf8\x88\x80\x80\x80" : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
204 0x400000 - 1 => (isASCII) ? "\xf8\x8f\xbf\xbf\xbf" : I8_to_native("\xfb\xbf\xbf\xbf\xbf"),
205 0x400000 => (isASCII) ? "\xf8\x90\x80\x80\x80" : I8_to_native("\xfc\xa4\xa0\xa0\xa0\xa0"),
206 0x4000000 - 1 => (isASCII) ? "\xfb\xbf\xbf\xbf\xbf" : I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
207 0x4000000 => (isASCII) ? "\xfc\x84\x80\x80\x80\x80" : I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
208 0x4000000 - 1 => (isASCII) ? "\xfb\xbf\xbf\xbf\xbf" : I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
209 0x4000000 => (isASCII) ? "\xfc\x84\x80\x80\x80\x80" : I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
210 0x40000000 - 1 => (isASCII) ? "\xfc\xbf\xbf\xbf\xbf\xbf" : I8_to_native("\xfe\xbf\xbf\xbf\xbf\xbf\xbf"),
211 0x40000000 => (isASCII) ? "\xfd\x80\x80\x80\x80\x80" : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0"),
212 0x80000000 - 1 => (isASCII) ? "\xfd\xbf\xbf\xbf\xbf\xbf" : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf"),
c0236afe
KW
213 0x80000000 => (isASCII) ? "\xfe\x82\x80\x80\x80\x80\x80" : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
214 0xFFFFFFFF => (isASCII) ? "\xfe\x83\xbf\xbf\xbf\xbf\xbf" : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
7dfd8446 215);
4deba822 216
7dfd8446
KW
217if ($is64bit) {
218 no warnings qw(overflow portable);
9d2d0ecd
KW
219 $code_points{0x100000000} = (isASCII)
220 ? "\xfe\x84\x80\x80\x80\x80\x80"
221 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0");
222 $code_points{0x1000000000 - 1} = (isASCII)
223 ? "\xfe\xbf\xbf\xbf\xbf\xbf\xbf"
224 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
225 $code_points{0x1000000000} = (isASCII)
226 ? "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
227 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0");
228 $code_points{0xFFFFFFFFFFFFFFFF} = (isASCII)
229 ? "\xff\x80\x8f\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
230 : I8_to_native("\xff\xaf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
d566bd20
KW
231 if (isASCII) { # These could falsely show as overlongs in a naive implementation
232 $code_points{0x40000000000} = "\xff\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80";
233 $code_points{0x1000000000000} = "\xff\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80";
234 $code_points{0x40000000000000} = "\xff\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80";
235 $code_points{0x1000000000000000} = "\xff\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
236 # overflows
237 #$code_points{0xfoo} = "\xff\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
238 }
239}
240elsif (! isASCII) { # 32-bit EBCDIC. 64-bit is clearer to handle, so doesn't need this test case
241 no warnings qw(overflow portable);
242 $code_points{0x40000000} = I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0");
7dfd8446 243}
eb83ed87 244
7dfd8446
KW
245# Now add in entries for each of code points 0-255, which require special
246# handling on EBCDIC. Remember the keys are Unicode values, and the values
247# are the native UTF-8. For invariants, the bytes are just the native chr.
248
249my $cp = 0;
250while ($cp < ((isASCII) ? 128 : 160)) { # This is from the definition of
251 # invariant
252 $code_points{$cp} = chr utf8::unicode_to_native($cp);
253 $cp++;
254}
255
256# Done with the invariants. Now do the variants. All in this range are 2
257# byte. Again, we can't use the internal functions to generate UTF-8, as
258# those are what we are trying to test. In the loop, we know what range the
259# continuation bytes can be in, and what the lowest start byte can be. So we
260# cycle through them.
261
262my $first_continuation = (isASCII) ? 0x80 : 0xA0;
263my $final_continuation = 0xBF;
264my $start = (isASCII) ? 0xC2 : 0xC5;
265
266my $continuation = $first_continuation - 1;
267
268while ($cp < 255) {
269 if (++$continuation > $final_continuation) {
270
271 # Wrap to the next start byte when we reach the final continuation
272 # byte possible
273 $continuation = $first_continuation;
274 $start++;
275 }
276 $code_points{$cp} = I8_to_native(chr($start) . chr($continuation));
277
278 $cp++;
279}
eb83ed87
KW
280
281my @warnings;
282
283use warnings 'utf8';
284local $SIG{__WARN__} = sub { push @warnings, @_ };
285
7dfd8446
KW
286# This set of tests looks for basic sanity, and lastly tests the bottom level
287# decode routine for the given code point. If the earlier tests for that code
288# point fail, that one probably will too. Malformations are tested in later
289# segments of code.
290for my $u (sort { utf8::unicode_to_native($a) <=> utf8::unicode_to_native($b) }
291 keys %code_points)
292{
293 my $hex_u = sprintf("0x%02X", $u);
294 my $n = utf8::unicode_to_native($u);
295 my $hex_n = sprintf("0x%02X", $n);
296 my $bytes = $code_points{$u};
297
298 my $offskip_should_be;
299 {
300 no warnings qw(overflow portable);
301 $offskip_should_be = (isASCII)
302 ? ( $u < 0x80 ? 1 :
303 $u < 0x800 ? 2 :
304 $u < 0x10000 ? 3 :
305 $u < 0x200000 ? 4 :
306 $u < 0x4000000 ? 5 :
307 $u < 0x80000000 ? 6 : (($is64bit)
308 ? ($u < 0x1000000000 ? 7 : 13)
309 : 7)
310 )
311 : ($u < 0xA0 ? 1 :
312 $u < 0x400 ? 2 :
313 $u < 0x4000 ? 3 :
314 $u < 0x40000 ? 4 :
315 $u < 0x400000 ? 5 :
c0236afe
KW
316 $u < 0x4000000 ? 6 :
317 $u < 0x40000000 ? 7 : 14 );
7dfd8446
KW
318 }
319
320 # If this test fails, subsequent ones are meaningless.
321 next unless is(test_OFFUNISKIP($u), $offskip_should_be,
322 "Verify OFFUNISKIP($hex_u) is $offskip_should_be");
323 my $invariant = $offskip_should_be == 1;
324 my $display_invariant = $invariant || 0;
325 is(test_OFFUNI_IS_INVARIANT($u), $invariant,
326 "Verify OFFUNI_IS_INVARIANT($hex_u) is $display_invariant");
327
328 my $uvchr_skip_should_be = $offskip_should_be;
329 next unless is(test_UVCHR_SKIP($n), $uvchr_skip_should_be,
330 "Verify UVCHR_SKIP($hex_n) is $uvchr_skip_should_be");
331 is(test_UVCHR_IS_INVARIANT($n), $offskip_should_be == 1,
332 "Verify UVCHR_IS_INVARIANT($hex_n) is $display_invariant");
333
334 my $n_chr = chr $n;
335 utf8::upgrade $n_chr;
336
337 is(test_UTF8_SKIP($n_chr), $uvchr_skip_should_be,
338 "Verify UTF8_SKIP(chr $hex_n) is $uvchr_skip_should_be");
339
340 use bytes;
3d56ecbe
KW
341 my $byte_length = length $n_chr;
342 for (my $j = 0; $j < $byte_length; $j++) {
343 undef @warnings;
344
345 if ($j == $byte_length - 1) {
346 my $ret = test_is_utf8_valid_partial_char_flags($n_chr, $byte_length, 0);
347 is($ret, 0, " Verify is_utf8_valid_partial_char_flags(" . display_bytes($n_chr) . ") returns 0 for full character");
348 }
349 else {
350 my $bytes_so_far = substr($n_chr, 0, $j + 1);
351 my $ret = test_is_utf8_valid_partial_char_flags($bytes_so_far, $j + 1, 0);
352 is($ret, 1, " Verify is_utf8_valid_partial_char_flags(" . display_bytes($bytes_so_far) . ") returns 1");
353 }
354
355 unless (is(scalar @warnings, 0,
356 " Verify is_utf8_valid_partial_char_flags generated no warnings"))
357 {
358 diag "The warnings were: " . join(", ", @warnings);
359 }
360
7dfd8446
KW
361 my $b = substr($n_chr, $j, 1);
362 my $hex_b = sprintf("\"\\x%02x\"", ord $b);
363
364 my $byte_invariant = $j == 0 && $uvchr_skip_should_be == 1;
365 my $display_byte_invariant = $byte_invariant || 0;
366 next unless is(test_UTF8_IS_INVARIANT($b), $byte_invariant,
367 " Verify UTF8_IS_INVARIANT($hex_b) for byte $j "
368 . "is $display_byte_invariant");
369
370 my $is_start = $j == 0 && $uvchr_skip_should_be > 1;
371 my $display_is_start = $is_start || 0;
372 next unless is(test_UTF8_IS_START($b), $is_start,
373 " Verify UTF8_IS_START($hex_b) is $display_is_start");
374
375 my $is_continuation = $j != 0 && $uvchr_skip_should_be > 1;
376 my $display_is_continuation = $is_continuation || 0;
377 next unless is(test_UTF8_IS_CONTINUATION($b), $is_continuation,
378 " Verify UTF8_IS_CONTINUATION($hex_b) is "
379 . "$display_is_continuation");
380
381 my $is_continued = $uvchr_skip_should_be > 1;
382 my $display_is_continued = $is_continued || 0;
383 next unless is(test_UTF8_IS_CONTINUED($b), $is_continued,
384 " Verify UTF8_IS_CONTINUED($hex_b) is "
385 . "$display_is_continued");
386
387 my $is_downgradeable_start = $n < 256
388 && $uvchr_skip_should_be > 1
389 && $j == 0;
390 my $display_is_downgradeable_start = $is_downgradeable_start || 0;
391 next unless is(test_UTF8_IS_DOWNGRADEABLE_START($b),
392 $is_downgradeable_start,
393 " Verify UTF8_IS_DOWNGRADEABLE_START($hex_b) is "
394 . "$display_is_downgradeable_start");
395
396 my $is_above_latin1 = $n > 255 && $j == 0;
397 my $display_is_above_latin1 = $is_above_latin1 || 0;
398 next unless is(test_UTF8_IS_ABOVE_LATIN1($b),
399 $is_above_latin1,
400 " Verify UTF8_IS_ABOVE_LATIN1($hex_b) is "
401 . "$display_is_above_latin1");
402
403 my $is_possibly_problematic = $j == 0
404 && $n >= ((isASCII)
405 ? 0xD000
406 : 0x8000);
407 my $display_is_possibly_problematic = $is_possibly_problematic || 0;
408 next unless is(test_isUTF8_POSSIBLY_PROBLEMATIC($b),
409 $is_possibly_problematic,
410 " Verify isUTF8_POSSIBLY_PROBLEMATIC($hex_b) is "
411 . "$display_is_above_latin1");
412 }
413
414 # We are not trying to look for warnings, etc, so if they should occur, it
415 # is an error. But some of the code points here do cause warnings, so we
416 # check here and turn off the ones that apply to such code points. A
417 # later section of the code tests for these kinds of things.
046d01eb 418 my $this_utf8_flags = $look_for_everything_utf8n_to;
7dfd8446
KW
419 my $len = length $bytes;
420 if ($n > 2 ** 31 - 1) {
1d1c12d9
KW
421 $this_utf8_flags &=
422 ~($UTF8_DISALLOW_ABOVE_31_BIT|$UTF8_WARN_ABOVE_31_BIT);
7dfd8446
KW
423 }
424 if ($n > 0x10FFFF) {
425 $this_utf8_flags &= ~($UTF8_DISALLOW_SUPER|$UTF8_WARN_SUPER);
426 }
427 elsif (($n & 0xFFFE) == 0xFFFE) {
428 $this_utf8_flags &= ~($UTF8_DISALLOW_NONCHAR|$UTF8_WARN_NONCHAR);
429 }
430
431 undef @warnings;
432
433 my $display_flags = sprintf "0x%x", $this_utf8_flags;
7dfd8446 434 my $display_bytes = display_bytes($bytes);
d7874298 435 my $ret_ref = test_utf8n_to_uvchr($bytes, $len, $this_utf8_flags);
7dfd8446 436 is($ret_ref->[0], $n, "Verify utf8n_to_uvchr($display_bytes, $display_flags) returns $hex_n");
9d2d0ecd 437 is($ret_ref->[1], $len, "Verify utf8n_to_uvchr() for $hex_n returns expected length: $len");
7dfd8446
KW
438
439 unless (is(scalar @warnings, 0,
440 "Verify utf8n_to_uvchr() for $hex_n generated no warnings"))
441 {
442 diag "The warnings were: " . join(", ", @warnings);
443 }
046d01eb 444
75ffa578
KW
445 undef @warnings;
446
d7874298 447 my $ret = test_isUTF8_CHAR($bytes, $len);
9d2d0ecd 448 is($ret, $len, "Verify isUTF8_CHAR($display_bytes) returns expected length: $len");
d7874298
KW
449
450 unless (is(scalar @warnings, 0,
451 "Verify isUTF8_CHAR() for $hex_n generated no warnings"))
452 {
453 diag "The warnings were: " . join(", ", @warnings);
454 }
455
456 undef @warnings;
457
458 $ret = test_isUTF8_CHAR($bytes, $len - 1);
459 is($ret, 0, "Verify isUTF8_CHAR() with too short length parameter returns 0");
460
461 unless (is(scalar @warnings, 0,
462 "Verify isUTF8_CHAR() generated no warnings"))
463 {
464 diag "The warnings were: " . join(", ", @warnings);
465 }
466
467 undef @warnings;
468
75ffa578
KW
469 $ret_ref = test_valid_utf8_to_uvchr($bytes);
470 is($ret_ref->[0], $n, "Verify valid_utf8_to_uvchr($display_bytes) returns $hex_n");
9d2d0ecd 471 is($ret_ref->[1], $len, "Verify valid_utf8_to_uvchr() for $hex_n returns expected length: $len");
75ffa578
KW
472
473 unless (is(scalar @warnings, 0,
474 "Verify valid_utf8_to_uvchr() for $hex_n generated no warnings"))
475 {
476 diag "The warnings were: " . join(", ", @warnings);
477 }
478
046d01eb
KW
479 # Similarly for uvchr_to_utf8
480 my $this_uvchr_flags = $look_for_everything_uvchr_to;
481 if ($n > 2 ** 31 - 1) {
482 $this_uvchr_flags &=
483 ~($UNICODE_DISALLOW_ABOVE_31_BIT|$UNICODE_WARN_ABOVE_31_BIT);
484 }
485 if ($n > 0x10FFFF) {
486 $this_uvchr_flags &= ~($UNICODE_DISALLOW_SUPER|$UNICODE_WARN_SUPER);
487 }
488 elsif (($n & 0xFFFE) == 0xFFFE) {
489 $this_uvchr_flags &= ~($UNICODE_DISALLOW_NONCHAR|$UNICODE_WARN_NONCHAR);
490 }
491 $display_flags = sprintf "0x%x", $this_uvchr_flags;
492
493 undef @warnings;
494
d7874298 495 $ret = test_uvchr_to_utf8_flags($n, $this_uvchr_flags);
046d01eb
KW
496 ok(defined $ret, "Verify uvchr_to_utf8_flags($hex_n, $display_flags) returned success");
497 is($ret, $bytes, "Verify uvchr_to_utf8_flags($hex_n, $display_flags) returns correct bytes");
498
499 unless (is(scalar @warnings, 0,
500 "Verify uvchr_to_utf8_flags($hex_n, $display_flags) for $hex_n generated no warnings"))
501 {
502 diag "The warnings were: " . join(", ", @warnings);
503 }
7dfd8446
KW
504}
505
506my $REPLACEMENT = 0xFFFD;
507
508# Now test the malformations. All these raise category utf8 warnings.
509my $c = (isASCII) ? "\x80" : "\xa0"; # A continuation byte
510my @malformations = (
eb83ed87
KW
511 [ "zero length string malformation", "", 0,
512 $UTF8_ALLOW_EMPTY, 0, 0,
513 qr/empty string/
514 ],
7dfd8446
KW
515 [ "orphan continuation byte malformation", I8_to_native("${c}a"),
516 2,
eb83ed87
KW
517 $UTF8_ALLOW_CONTINUATION, $REPLACEMENT, 1,
518 qr/unexpected continuation byte/
519 ],
7dfd8446 520 [ "premature next character malformation (immediate)",
9d2d0ecd
KW
521 (isASCII) ? "\xc2\xc2\x80" : I8_to_native("\xc5\xc5\xa0"),
522 3,
eb83ed87
KW
523 $UTF8_ALLOW_NON_CONTINUATION, $REPLACEMENT, 1,
524 qr/unexpected non-continuation byte.*immediately after start byte/
525 ],
7dfd8446
KW
526 [ "premature next character malformation (non-immediate)",
527 I8_to_native("\xf0${c}a"),
528 3,
eb83ed87
KW
529 $UTF8_ALLOW_NON_CONTINUATION, $REPLACEMENT, 2,
530 qr/unexpected non-continuation byte .* 2 bytes after start byte/
531 ],
7dfd8446 532 [ "too short malformation", I8_to_native("\xf0${c}a"), 2,
eb83ed87
KW
533 # Having the 'a' after this, but saying there are only 2 bytes also
534 # tests that we pay attention to the passed in length
535 $UTF8_ALLOW_SHORT, $REPLACEMENT, 2,
536 qr/2 bytes, need 4/
537 ],
d566bd20
KW
538 [ "overlong malformation, lowest 2-byte",
539 (isASCII) ? "\xc0\x80" : I8_to_native("\xc0\xa0"),
540 2,
7dfd8446
KW
541 $UTF8_ALLOW_LONG,
542 0, # NUL
543 2,
eb83ed87 544 qr/2 bytes, need 1/
c0236afe 545 ],
d566bd20
KW
546 [ "overlong malformation, highest 2-byte",
547 (isASCII) ? "\xc1\xbf" : I8_to_native("\xc4\xbf"),
548 2,
549 $UTF8_ALLOW_LONG,
550 (isASCII) ? 0x7F : utf8::unicode_to_native(0xBF),
551 2,
552 qr/2 bytes, need 1/
553 ],
554 [ "overlong malformation, lowest 3-byte",
555 (isASCII) ? "\xe0\x80\x80" : I8_to_native("\xe0\xa0\xa0"),
556 3,
557 $UTF8_ALLOW_LONG,
558 0, # NUL
559 3,
560 qr/3 bytes, need 1/
561 ],
562 [ "overlong malformation, highest 3-byte",
563 (isASCII) ? "\xe0\x9f\xbf" : I8_to_native("\xe0\xbf\xbf"),
564 3,
565 $UTF8_ALLOW_LONG,
566 (isASCII) ? 0x7FF : 0x3FF,
567 3,
568 qr/3 bytes, need 2/
569 ],
570 [ "overlong malformation, lowest 4-byte",
571 (isASCII) ? "\xf0\x80\x80\x80" : I8_to_native("\xf0\xa0\xa0\xa0"),
572 4,
573 $UTF8_ALLOW_LONG,
574 0, # NUL
575 4,
576 qr/4 bytes, need 1/
577 ],
578 [ "overlong malformation, highest 4-byte",
579 (isASCII) ? "\xf0\x8F\xbf\xbf" : I8_to_native("\xf0\xaf\xbf\xbf"),
580 4,
581 $UTF8_ALLOW_LONG,
582 (isASCII) ? 0xFFFF : 0x3FFF,
583 4,
584 qr/4 bytes, need 3/
585 ],
586 [ "overlong malformation, lowest 5-byte",
587 (isASCII)
588 ? "\xf8\x80\x80\x80\x80"
589 : I8_to_native("\xf8\xa0\xa0\xa0\xa0"),
590 5,
591 $UTF8_ALLOW_LONG,
592 0, # NUL
593 5,
594 qr/5 bytes, need 1/
595 ],
596 [ "overlong malformation, highest 5-byte",
597 (isASCII)
598 ? "\xf8\x87\xbf\xbf\xbf"
599 : I8_to_native("\xf8\xa7\xbf\xbf\xbf"),
600 5,
601 $UTF8_ALLOW_LONG,
602 (isASCII) ? 0x1FFFFF : 0x3FFFF,
603 5,
604 qr/5 bytes, need 4/
605 ],
606 [ "overlong malformation, lowest 6-byte",
607 (isASCII)
608 ? "\xfc\x80\x80\x80\x80\x80"
609 : I8_to_native("\xfc\xa0\xa0\xa0\xa0\xa0"),
610 6,
611 $UTF8_ALLOW_LONG,
612 0, # NUL
613 6,
614 qr/6 bytes, need 1/
615 ],
616 [ "overlong malformation, highest 6-byte",
617 (isASCII)
618 ? "\xfc\x83\xbf\xbf\xbf\xbf"
619 : I8_to_native("\xfc\xa3\xbf\xbf\xbf\xbf"),
620 6,
621 $UTF8_ALLOW_LONG,
622 (isASCII) ? 0x3FFFFFF : 0x3FFFFF,
623 6,
624 qr/6 bytes, need 5/
625 ],
626 [ "overlong malformation, lowest 7-byte",
627 (isASCII)
628 ? "\xfe\x80\x80\x80\x80\x80\x80"
629 : I8_to_native("\xfe\xa0\xa0\xa0\xa0\xa0\xa0"),
630 7,
631 $UTF8_ALLOW_LONG,
632 0, # NUL
633 7,
634 qr/7 bytes, need 1/
635 ],
636 [ "overlong malformation, highest 7-byte",
637 (isASCII)
638 ? "\xfe\x81\xbf\xbf\xbf\xbf\xbf"
639 : I8_to_native("\xfe\xa1\xbf\xbf\xbf\xbf\xbf"),
640 7,
641 $UTF8_ALLOW_LONG,
642 (isASCII) ? 0x7FFFFFFF : 0x3FFFFFF,
643 7,
644 qr/7 bytes, need 6/
c0236afe
KW
645 ],
646);
7dfd8446 647
d566bd20
KW
648if (isASCII && ! $is64bit) { # 32-bit ASCII platform
649 no warnings 'portable';
650 push @malformations,
651 [ "overflow malformation",
652 "\xfe\x84\x80\x80\x80\x80\x80", # Represents 2**32
653 7,
654 0, # There is no way to allow this malformation
655 $REPLACEMENT,
656 7,
657 qr/overflow/
658 ],
659 [ "overflow malformation, can tell on first byte",
660 "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80",
661 13,
662 0, # There is no way to allow this malformation
663 $REPLACEMENT,
664 13,
665 qr/overflow/
666 ];
667}
668else {
669 # On EBCDIC platforms, another overlong test is needed even on 32-bit
670 # systems, whereas it doesn't happen on ASCII except on 64-bit ones.
671
672 no warnings 'portable';
673 no warnings 'overflow'; # Doesn't run on 32-bit systems, but compiles
674 push @malformations,
675 [ "overlong malformation, lowest max-byte",
676 (isASCII)
677 ? "\xff\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80"
678 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
679 (isASCII) ? 13 : 14,
680 $UTF8_ALLOW_LONG,
681 0, # NUL
682 (isASCII) ? 13 : 14,
683 qr/1[34] bytes, need 1/, # 1[34] to work on either ASCII or EBCDIC
684 ],
685 [ "overlong malformation, highest max-byte",
686 (isASCII) # 2**36-1 on ASCII; 2**30-1 on EBCDIC
687 ? "\xff\x80\x80\x80\x80\x80\x80\xbf\xbf\xbf\xbf\xbf\xbf"
688 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xbf\xbf\xbf\xbf\xbf\xbf"),
689 (isASCII) ? 13 : 14,
690 $UTF8_ALLOW_LONG,
691 (isASCII) ? 0xFFFFFFFFF : 0x3FFFFFFF,
692 (isASCII) ? 13 : 14,
693 qr/1[34] bytes, need 7/,
694 ];
695
696 if (! $is64bit) { # 32-bit EBCDIC
697 push @malformations,
698 [ "overflow malformation",
699 I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"),
700 14,
701 0, # There is no way to allow this malformation
702 $REPLACEMENT,
703 14,
704 qr/overflow/
705 ];
706 }
707 else { # 64-bit
708 push @malformations,
709 [ "overflow malformation",
710 (isASCII)
711 ? "\xff\x80\x90\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"
712 : I8_to_native("\xff\xb0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
713 (isASCII) ? 13 : 14,
714 0, # There is no way to allow this malformation
715 $REPLACEMENT,
716 (isASCII) ? 13 : 14,
717 qr/overflow/
718 ];
719 }
720}
721
7dfd8446 722foreach my $test (@malformations) {
eb83ed87
KW
723 my ($testname, $bytes, $length, $allow_flags, $allowed_uv, $expected_len, $message ) = @$test;
724
725 next if ! ok(length($bytes) >= $length, "$testname: Make sure won't read beyond buffer: " . length($bytes) . " >= $length");
726
d7874298
KW
727 undef @warnings;
728
729 my $ret = test_isUTF8_CHAR($bytes, $length);
730 is($ret, 0, "$testname: isUTF8_CHAR returns 0");
731 unless (is(scalar @warnings, 0,
732 "$testname: isUTF8_CHAR() generated no warnings"))
733 {
734 diag "The warnings were: " . join(", ", @warnings);
735 }
736
3d56ecbe
KW
737 for my $j (1 .. $length - 1) {
738 my $partial = substr($bytes, 0, $j);
739
740 undef @warnings;
741
742 $ret = test_is_utf8_valid_partial_char_flags($bytes, $j, 0);
743 my $ret_should_be = 0;
744 my $comment = "";
745 if ($testname =~ /premature|short/ && $j < 2) {
746 $ret_should_be = 1;
747 $comment = ", but need 2 bytes to discern:";
748 }
749 elsif ($testname =~ /overlong/ && $length > 2) {
750 if ($length <= 7 && $j < 2) {
751 $ret_should_be = 1;
752 $comment = ", but need 2 bytes to discern:";
753 }
754 elsif ($length > 7 && $j < 7) {
755 $ret_should_be = 1;
756 $comment = ", but need 7 bytes to discern:";
757 }
758 }
759 elsif ($testname =~ /overflow/ && $testname !~ /first byte/) {
760 if (isASCII) {
761 if ($j < (($is64bit) ? 3 : 2)) {
762 $comment = ", but need $j bytes to discern:";
763 $ret_should_be = 1;
764 }
765 }
766 else {
767 if ($j < (($is64bit) ? 2 : 8)) {
768 $comment = ", but need $j bytes to discern:";
769 $ret_should_be = 1;
770 }
771 }
772 }
773 is($ret, $ret_should_be, "$testname: is_utf8_valid_partial_char_flags("
774 . display_bytes($partial)
775 . ")$comment returns $ret_should_be");
776 unless (is(scalar @warnings, 0,
777 "$testname: is_utf8_valid_partial_char_flags() generated no warnings"))
778 {
779 diag "The warnings were: " . join(", ", @warnings);
780 }
781 }
782
d7874298 783
eb83ed87
KW
784 # Test what happens when this malformation is not allowed
785 undef @warnings;
bd70aaaf 786 my $ret_ref = test_utf8n_to_uvchr($bytes, $length, 0);
eb83ed87 787 is($ret_ref->[0], 0, "$testname: disallowed: Returns 0");
9d2d0ecd 788 is($ret_ref->[1], $expected_len, "$testname: utf8n_to_uvchr(), disallowed: Returns expected length: $expected_len");
eb83ed87
KW
789 if (is(scalar @warnings, 1, "$testname: disallowed: Got a single warning ")) {
790 like($warnings[0], $message, "$testname: disallowed: Got expected warning");
791 }
792 else {
793 if (scalar @warnings) {
7dfd8446 794 diag "The warnings were: " . join(", ", @warnings);
eb83ed87
KW
795 }
796 }
797
798 { # Next test when disallowed, and warnings are off.
799 undef @warnings;
800 no warnings 'utf8';
bd70aaaf 801 my $ret_ref = test_utf8n_to_uvchr($bytes, $length, 0);
9d2d0ecd
KW
802 is($ret_ref->[0], 0, "$testname: utf8n_to_uvchr(), disallowed: no warnings 'utf8': Returns 0");
803 is($ret_ref->[1], $expected_len, "$testname: utf8n_to_uvchr(), disallowed: no warnings 'utf8': Returns expected length: $expected_len");
804 if (!is(scalar @warnings, 0, "$testname: utf8n_to_uvchr(), disallowed: no warnings 'utf8': no warnings generated")) {
7dfd8446 805 diag "The warnings were: " . join(", ", @warnings);
eb83ed87
KW
806 }
807 }
808
809 # Test with CHECK_ONLY
810 undef @warnings;
bd70aaaf 811 $ret_ref = test_utf8n_to_uvchr($bytes, $length, $UTF8_CHECK_ONLY);
eb83ed87 812 is($ret_ref->[0], 0, "$testname: CHECK_ONLY: Returns 0");
9d2d0ecd 813 is($ret_ref->[1], -1, "$testname: CHECK_ONLY: returns -1 for length");
eb83ed87 814 if (! is(scalar @warnings, 0, "$testname: CHECK_ONLY: no warnings generated")) {
7dfd8446 815 diag "The warnings were: " . join(", ", @warnings);
eb83ed87
KW
816 }
817
818 next if $allow_flags == 0; # Skip if can't allow this malformation
819
820 # Test when the malformation is allowed
821 undef @warnings;
bd70aaaf 822 $ret_ref = test_utf8n_to_uvchr($bytes, $length, $allow_flags);
9d2d0ecd
KW
823 is($ret_ref->[0], $allowed_uv, "$testname: utf8n_to_uvchr(), allowed: Returns expected uv: " . sprintf("0x%04X", $allowed_uv));
824 is($ret_ref->[1], $expected_len, "$testname: utf8n_to_uvchr(), allowed: Returns expected length: $expected_len");
825 if (!is(scalar @warnings, 0, "$testname: utf8n_to_uvchr(), allowed: no warnings generated"))
eb83ed87 826 {
7dfd8446 827 diag "The warnings were: " . join(", ", @warnings);
eb83ed87
KW
828 }
829}
830
eb83ed87
KW
831# Now test the cases where a legal code point is generated, but may or may not
832# be allowed/warned on.
2f8f112e 833my @tests = (
7dfd8446
KW
834 [ "lowest surrogate",
835 (isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
836 $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE,
837 'surrogate', 0xD800,
838 (isASCII) ? 3 : 4,
839 qr/surrogate/
840 ],
841 [ "a middle surrogate",
842 (isASCII) ? "\xed\xa4\x8d" : I8_to_native("\xf1\xb6\xa8\xad"),
843 $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE,
844 'surrogate', 0xD90D,
845 (isASCII) ? 3 : 4,
eb83ed87
KW
846 qr/surrogate/
847 ],
7dfd8446
KW
848 [ "highest surrogate",
849 (isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
850 $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE,
851 'surrogate', 0xDFFF,
852 (isASCII) ? 3 : 4,
853 qr/surrogate/
854 ],
855 [ "first non_unicode",
856 (isASCII) ? "\xf4\x90\x80\x80" : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
857 $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER,
858 'non_unicode', 0x110000,
859 (isASCII) ? 4 : 5,
860 qr/not Unicode.* may not be portable/
861 ],
d566bd20
KW
862 [ "non_unicode whose first byte tells that",
863 (isASCII) ? "\xf5\x80\x80\x80" : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
864 $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER,
865 'non_unicode',
866 (isASCII) ? 0x140000 : 0x200000,
867 (isASCII) ? 4 : 5,
868 qr/not Unicode.* may not be portable/
869 ],
7dfd8446
KW
870 [ "first of 32 consecutive non-character code points",
871 (isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
872 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
873 'nonchar', 0xFDD0,
874 (isASCII) ? 3 : 4,
875 qr/Unicode non-character.*is not recommended for open interchange/
876 ],
877 [ "a mid non-character code point of the 32 consecutive ones",
878 (isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
879 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
880 'nonchar', 0xFDE0,
881 (isASCII) ? 3 : 4,
882 qr/Unicode non-character.*is not recommended for open interchange/
883 ],
884 [ "final of 32 consecutive non-character code points",
885 (isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
886 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
887 'nonchar', 0xFDEF,
888 (isASCII) ? 3 : 4,
889 qr/Unicode non-character.*is not recommended for open interchange/
890 ],
891 [ "non-character code point U+FFFE",
892 (isASCII) ? "\xef\xbf\xbe" : I8_to_native("\xf1\xbf\xbf\xbe"),
893 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
894 'nonchar', 0xFFFE,
895 (isASCII) ? 3 : 4,
896 qr/Unicode non-character.*is not recommended for open interchange/
897 ],
898 [ "non-character code point U+FFFF",
899 (isASCII) ? "\xef\xbf\xbf" : I8_to_native("\xf1\xbf\xbf\xbf"),
900 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
901 'nonchar', 0xFFFF,
902 (isASCII) ? 3 : 4,
903 qr/Unicode non-character.*is not recommended for open interchange/
904 ],
905 [ "non-character code point U+1FFFE",
906 (isASCII) ? "\xf0\x9f\xbf\xbe" : I8_to_native("\xf3\xbf\xbf\xbe"),
907 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
908 'nonchar', 0x1FFFE, 4,
909 qr/Unicode non-character.*is not recommended for open interchange/
910 ],
911 [ "non-character code point U+1FFFF",
912 (isASCII) ? "\xf0\x9f\xbf\xbf" : I8_to_native("\xf3\xbf\xbf\xbf"),
913 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
914 'nonchar', 0x1FFFF, 4,
915 qr/Unicode non-character.*is not recommended for open interchange/
916 ],
917 [ "non-character code point U+2FFFE",
918 (isASCII) ? "\xf0\xaf\xbf\xbe" : I8_to_native("\xf5\xbf\xbf\xbe"),
919 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
920 'nonchar', 0x2FFFE, 4,
921 qr/Unicode non-character.*is not recommended for open interchange/
922 ],
923 [ "non-character code point U+2FFFF",
924 (isASCII) ? "\xf0\xaf\xbf\xbf" : I8_to_native("\xf5\xbf\xbf\xbf"),
925 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
926 'nonchar', 0x2FFFF, 4,
927 qr/Unicode non-character.*is not recommended for open interchange/
928 ],
929 [ "non-character code point U+3FFFE",
930 (isASCII) ? "\xf0\xbf\xbf\xbe" : I8_to_native("\xf7\xbf\xbf\xbe"),
931 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
932 'nonchar', 0x3FFFE, 4,
933 qr/Unicode non-character.*is not recommended for open interchange/
934 ],
935 [ "non-character code point U+3FFFF",
936 (isASCII) ? "\xf0\xbf\xbf\xbf" : I8_to_native("\xf7\xbf\xbf\xbf"),
937 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
938 'nonchar', 0x3FFFF, 4,
939 qr/Unicode non-character.*is not recommended for open interchange/
940 ],
941 [ "non-character code point U+4FFFE",
942 (isASCII) ? "\xf1\x8f\xbf\xbe" : I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
943 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
944 'nonchar', 0x4FFFE,
945 (isASCII) ? 4 : 5,
946 qr/Unicode non-character.*is not recommended for open interchange/
947 ],
948 [ "non-character code point U+4FFFF",
949 (isASCII) ? "\xf1\x8f\xbf\xbf" : I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
950 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
951 'nonchar', 0x4FFFF,
952 (isASCII) ? 4 : 5,
953 qr/Unicode non-character.*is not recommended for open interchange/
954 ],
955 [ "non-character code point U+5FFFE",
956 (isASCII) ? "\xf1\x9f\xbf\xbe" : I8_to_native("\xf8\xab\xbf\xbf\xbe"),
957 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
958 'nonchar', 0x5FFFE,
959 (isASCII) ? 4 : 5,
960 qr/Unicode non-character.*is not recommended for open interchange/
961 ],
962 [ "non-character code point U+5FFFF",
963 (isASCII) ? "\xf1\x9f\xbf\xbf" : I8_to_native("\xf8\xab\xbf\xbf\xbf"),
964 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
965 'nonchar', 0x5FFFF,
966 (isASCII) ? 4 : 5,
967 qr/Unicode non-character.*is not recommended for open interchange/
968 ],
969 [ "non-character code point U+6FFFE",
970 (isASCII) ? "\xf1\xaf\xbf\xbe" : I8_to_native("\xf8\xad\xbf\xbf\xbe"),
971 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
972 'nonchar', 0x6FFFE,
973 (isASCII) ? 4 : 5,
974 qr/Unicode non-character.*is not recommended for open interchange/
975 ],
976 [ "non-character code point U+6FFFF",
977 (isASCII) ? "\xf1\xaf\xbf\xbf" : I8_to_native("\xf8\xad\xbf\xbf\xbf"),
978 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
979 'nonchar', 0x6FFFF,
980 (isASCII) ? 4 : 5,
981 qr/Unicode non-character.*is not recommended for open interchange/
982 ],
983 [ "non-character code point U+7FFFE",
984 (isASCII) ? "\xf1\xbf\xbf\xbe" : I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
985 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
986 'nonchar', 0x7FFFE,
987 (isASCII) ? 4 : 5,
988 qr/Unicode non-character.*is not recommended for open interchange/
989 ],
990 [ "non-character code point U+7FFFF",
991 (isASCII) ? "\xf1\xbf\xbf\xbf" : I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
992 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
993 'nonchar', 0x7FFFF,
994 (isASCII) ? 4 : 5,
995 qr/Unicode non-character.*is not recommended for open interchange/
996 ],
997 [ "non-character code point U+8FFFE",
998 (isASCII) ? "\xf2\x8f\xbf\xbe" : I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
999 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1000 'nonchar', 0x8FFFE,
1001 (isASCII) ? 4 : 5,
1002 qr/Unicode non-character.*is not recommended for open interchange/
1003 ],
1004 [ "non-character code point U+8FFFF",
1005 (isASCII) ? "\xf2\x8f\xbf\xbf" : I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
1006 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1007 'nonchar', 0x8FFFF,
1008 (isASCII) ? 4 : 5,
1009 qr/Unicode non-character.*is not recommended for open interchange/
1010 ],
1011 [ "non-character code point U+9FFFE",
1012 (isASCII) ? "\xf2\x9f\xbf\xbe" : I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
1013 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1014 'nonchar', 0x9FFFE,
1015 (isASCII) ? 4 : 5,
1016 qr/Unicode non-character.*is not recommended for open interchange/
1017 ],
1018 [ "non-character code point U+9FFFF",
1019 (isASCII) ? "\xf2\x9f\xbf\xbf" : I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
1020 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1021 'nonchar', 0x9FFFF,
1022 (isASCII) ? 4 : 5,
1023 qr/Unicode non-character.*is not recommended for open interchange/
1024 ],
1025 [ "non-character code point U+AFFFE",
1026 (isASCII) ? "\xf2\xaf\xbf\xbe" : I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
1027 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1028 'nonchar', 0xAFFFE,
1029 (isASCII) ? 4 : 5,
1030 qr/Unicode non-character.*is not recommended for open interchange/
1031 ],
1032 [ "non-character code point U+AFFFF",
1033 (isASCII) ? "\xf2\xaf\xbf\xbf" : I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
1034 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1035 'nonchar', 0xAFFFF,
1036 (isASCII) ? 4 : 5,
1037 qr/Unicode non-character.*is not recommended for open interchange/
1038 ],
1039 [ "non-character code point U+BFFFE",
1040 (isASCII) ? "\xf2\xbf\xbf\xbe" : I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
1041 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1042 'nonchar', 0xBFFFE,
1043 (isASCII) ? 4 : 5,
1044 qr/Unicode non-character.*is not recommended for open interchange/
1045 ],
1046 [ "non-character code point U+BFFFF",
1047 (isASCII) ? "\xf2\xbf\xbf\xbf" : I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
1048 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1049 'nonchar', 0xBFFFF,
1050 (isASCII) ? 4 : 5,
1051 qr/Unicode non-character.*is not recommended for open interchange/
1052 ],
1053 [ "non-character code point U+CFFFE",
1054 (isASCII) ? "\xf3\x8f\xbf\xbe" : I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
1055 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1056 'nonchar', 0xCFFFE,
1057 (isASCII) ? 4 : 5,
1058 qr/Unicode non-character.*is not recommended for open interchange/
1059 ],
1060 [ "non-character code point U+CFFFF",
1061 (isASCII) ? "\xf3\x8f\xbf\xbf" : I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
1062 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1063 'nonchar', 0xCFFFF,
1064 (isASCII) ? 4 : 5,
1065 qr/Unicode non-character.*is not recommended for open interchange/
1066 ],
1067 [ "non-character code point U+DFFFE",
1068 (isASCII) ? "\xf3\x9f\xbf\xbe" : I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
1069 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1070 'nonchar', 0xDFFFE,
1071 (isASCII) ? 4 : 5,
1072 qr/Unicode non-character.*is not recommended for open interchange/
1073 ],
1074 [ "non-character code point U+DFFFF",
1075 (isASCII) ? "\xf3\x9f\xbf\xbf" : I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
1076 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1077 'nonchar', 0xDFFFF,
1078 (isASCII) ? 4 : 5,
1079 qr/Unicode non-character.*is not recommended for open interchange/
1080 ],
1081 [ "non-character code point U+EFFFE",
1082 (isASCII) ? "\xf3\xaf\xbf\xbe" : I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
1083 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1084 'nonchar', 0xEFFFE,
1085 (isASCII) ? 4 : 5,
1086 qr/Unicode non-character.*is not recommended for open interchange/
1087 ],
1088 [ "non-character code point U+EFFFF",
1089 (isASCII) ? "\xf3\xaf\xbf\xbf" : I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
1090 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1091 'nonchar', 0xEFFFF,
1092 (isASCII) ? 4 : 5,
1093 qr/Unicode non-character.*is not recommended for open interchange/
1094 ],
1095 [ "non-character code point U+FFFFE",
1096 (isASCII) ? "\xf3\xbf\xbf\xbe" : I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
1097 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1098 'nonchar', 0xFFFFE,
1099 (isASCII) ? 4 : 5,
1100 qr/Unicode non-character.*is not recommended for open interchange/
1101 ],
1102 [ "non-character code point U+FFFFF",
1103 (isASCII) ? "\xf3\xbf\xbf\xbf" : I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
1104 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1105 'nonchar', 0xFFFFF,
1106 (isASCII) ? 4 : 5,
1107 qr/Unicode non-character.*is not recommended for open interchange/
1108 ],
1109 [ "non-character code point U+10FFFE",
1110 (isASCII) ? "\xf4\x8f\xbf\xbe" : I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
1111 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1112 'nonchar', 0x10FFFE,
1113 (isASCII) ? 4 : 5,
1114 qr/Unicode non-character.*is not recommended for open interchange/
eb83ed87 1115 ],
7dfd8446
KW
1116 [ "non-character code point U+10FFFF",
1117 (isASCII) ? "\xf4\x8f\xbf\xbf" : I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
1118 $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR,
1119 'nonchar', 0x10FFFF,
1120 (isASCII) ? 4 : 5,
ba707cdc 1121 qr/Unicode non-character.*is not recommended for open interchange/
eb83ed87 1122 ],
1d1c12d9 1123 [ "requires at least 32 bits",
c0236afe
KW
1124 (isASCII)
1125 ? "\xfe\x82\x80\x80\x80\x80\x80"
1126 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
eb83ed87 1127 # This code point is chosen so that it is representable in a UV on
2f8f112e 1128 # 32-bit machines
1d1c12d9 1129 $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
c0236afe 1130 'utf8', 0x80000000, (isASCII) ? 7 :14,
7dfd8446
KW
1131 qr/Code point 0x80000000 is not Unicode, and not portable/
1132 ],
1d1c12d9 1133 [ "requires at least 32 bits, and use SUPER-type flags, instead of ABOVE_31_BIT",
c0236afe
KW
1134 (isASCII)
1135 ? "\xfe\x82\x80\x80\x80\x80\x80"
1136 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
7dfd8446 1137 $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER,
c0236afe 1138 'utf8', 0x80000000, (isASCII) ? 7 :14,
ea5ced44 1139 qr/Code point 0x80000000 is not Unicode, and not portable/
eb83ed87 1140 ],
1d1c12d9
KW
1141 [ "overflow with warnings/disallow for more than 31 bits",
1142 # This tests the interaction of WARN_ABOVE_31_BIT/DISALLOW_ABOVE_31_BIT
1143 # with overflow. The overflow malformation is never allowed, so
1144 # preventing it takes precedence if the ABOVE_31_BIT options would
c0236afe 1145 # otherwise allow in an overflowing value. The ASCII code points (1
1d1c12d9
KW
1146 # for 32-bits; 1 for 64) were chosen because the old overflow
1147 # detection algorithm did not catch them; this means this test also
c0236afe
KW
1148 # checks for that fix. The EBCDIC are arbitrary overflowing ones
1149 # since we have no reports of failures with it.
1150 (($is64bit)
1151 ? ((isASCII)
9d2d0ecd 1152 ? "\xff\x80\x90\x90\x90\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
c0236afe
KW
1153 : I8_to_native("\xff\xB0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"))
1154 : ((isASCII)
9d2d0ecd 1155 ? "\xfe\x86\x80\x80\x80\x80\x80"
c0236afe 1156 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"))),
ea5ced44 1157
1d1c12d9
KW
1158 # We include both warning categories to make sure the ABOVE_31_BIT one
1159 # has precedence
1160 "$UTF8_WARN_ABOVE_31_BIT|$UTF8_WARN_SUPER",
1161 "$UTF8_DISALLOW_ABOVE_31_BIT",
7dfd8446 1162 'utf8', 0,
c0236afe 1163 (! isASCII) ? 14 : ($is64bit) ? 13 : 7,
ea5ced44 1164 qr/overflow at byte .*, after start byte 0xf/
eb83ed87 1165 ],
c0236afe 1166);
2f8f112e 1167
c0236afe
KW
1168if ($is64bit) {
1169 no warnings qw{portable overflow};
2f8f112e 1170 push @tests,
c0236afe
KW
1171 [ "More than 32 bits",
1172 (isASCII)
9d2d0ecd 1173 ? "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
c0236afe 1174 : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1d1c12d9 1175 $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
c0236afe 1176 'utf8', 0x1000000000, (isASCII) ? 13 : 14,
ea5ced44 1177 qr/Code point 0x.* is not Unicode, and not portable/
2f8f112e 1178 ];
83dc0f42
KW
1179 if (! isASCII) {
1180 push @tests, # These could falsely show wrongly in a naive implementation
1181 [ "requires at least 32 bits",
1182 I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1183 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1184 'utf8', 0x800000000, 14,
1185 qr/Code point 0x800000000 is not Unicode, and not portable/
1186 ],
1187 [ "requires at least 32 bits",
1188 I8_to_native("\xff\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1189 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1190 'utf8', 0x10000000000, 14,
1191 qr/Code point 0x10000000000 is not Unicode, and not portable/
1192 ],
1193 [ "requires at least 32 bits",
1194 I8_to_native("\xff\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1195 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1196 'utf8', 0x200000000000, 14,
1197 qr/Code point 0x200000000000 is not Unicode, and not portable/
1198 ],
1199 [ "requires at least 32 bits",
1200 I8_to_native("\xff\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1201 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1202 'utf8', 0x4000000000000, 14,
1203 qr/Code point 0x4000000000000 is not Unicode, and not portable/
1204 ],
1205 [ "requires at least 32 bits",
1206 I8_to_native("\xff\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1207 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1208 'utf8', 0x80000000000000, 14,
1209 qr/Code point 0x80000000000000 is not Unicode, and not portable/
1210 ],
1211 [ "requires at least 32 bits",
1212 I8_to_native("\xff\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1213 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
1214 'utf8', 0x1000000000000000, 14,
1215 qr/Code point 0x1000000000000000 is not Unicode, and not portable/
1216 ];
1217 }
2f8f112e
KW
1218}
1219
1220foreach my $test (@tests) {
eb83ed87
KW
1221 my ($testname, $bytes, $warn_flags, $disallow_flags, $category, $allowed_uv, $expected_len, $message ) = @$test;
1222
1223 my $length = length $bytes;
2f8f112e 1224 my $will_overflow = $testname =~ /overflow/;
eb83ed87 1225
d7874298
KW
1226 {
1227 use warnings;
1228 undef @warnings;
1229 my $ret = test_isUTF8_CHAR($bytes, $length);
1230 if ($will_overflow) {
1231 is($ret, 0, "isUTF8_CHAR() $testname: returns 0");
1232 }
1233 else {
1234 is($ret, $length,
1235 "isUTF8_CHAR() $testname: returns expected length: $length");
1236 }
1237 unless (is(scalar @warnings, 0,
1238 "isUTF8_CHAR() $testname: generated no warnings"))
1239 {
1240 diag "The warnings were: " . join(", ", @warnings);
1241 }
3d56ecbe
KW
1242
1243 # Test partial character handling, for each byte not a full character
1244 for my $j (1.. $length - 1) {
1245
1246 # Skip the test for the interaction between overflow and above-31
1247 # bit. It is really testing other things than the partial
1248 # character tests, for which other tests in this file are
1249 # sufficient
1250 last if $testname =~ /overflow/;
1251
1252 foreach my $disallow_flag (0, $disallow_flags) {
1253 my $partial = substr($bytes, 0, $j);
1254 my $ret_should_be;
1255 my $comment;
1256 if ($disallow_flag) {
1257 $ret_should_be = 0;
1258 $comment = "disallowed";
1259 }
1260 else {
1261 $ret_should_be = 1;
1262 $comment = "allowed";
1263 }
1264
1265 if ($disallow_flag) {
1266 if ($testname =~ /non-character/) {
1267 $ret_should_be = 1;
1268 $comment .= ", but but need full char to discern";
1269 }
1270 elsif ($testname =~ /surrogate/) {
1271 if ($j < 2) {
1272 $ret_should_be = 1;
1273 $comment .= ", but need 2 bytes to discern";
1274 }
1275 }
1276 elsif ($testname =~ /first non_unicode/ && $j < 2) {
1277 $ret_should_be = 1;
1278 $comment .= ", but need 2 bytes to discern";
1279 }
1280 }
1281
1282 undef @warnings;
1283
1284 $ret = test_is_utf8_valid_partial_char_flags($partial, $j, $disallow_flag);
1285 is($ret, $ret_should_be, "$testname: is_utf8_valid_partial_char_flags("
1286 . display_bytes($partial)
1287 . "), $comment: returns $ret_should_be");
1288 unless (is(scalar @warnings, 0,
1289 "$testname: is_utf8_valid_partial_char_flags() generated no warnings"))
1290 {
1291 diag "The warnings were: " . join(", ", @warnings);
1292 }
1293 }
1294 }
d7874298
KW
1295 }
1296
eb83ed87
KW
1297 # This is more complicated than the malformations tested earlier, as there
1298 # are several orthogonal variables involved. We test all the subclasses
1299 # of utf8 warnings to verify they work with and without the utf8 class,
1300 # and don't have effects on other sublass warnings
54f4afef 1301 foreach my $warning ('utf8', 'surrogate', 'nonchar', 'non_unicode') {
eb83ed87
KW
1302 foreach my $warn_flag (0, $warn_flags) {
1303 foreach my $disallow_flag (0, $disallow_flags) {
54f4afef 1304 foreach my $do_warning (0, 1) {
eb83ed87 1305
13d7a909
KW
1306 my $eval_warn = $do_warning
1307 ? "use warnings '$warning'"
1308 : $warning eq "utf8"
7dfd8446
KW
1309 ? "no warnings 'utf8'"
1310 : "use warnings 'utf8'; no warnings '$warning'";
2f8f112e 1311
13d7a909
KW
1312 # is effectively disallowed if will overflow, even if the
1313 # flag indicates it is allowed, fix up test name to
1314 # indicate this as well
1315 my $disallowed = $disallow_flag || $will_overflow;
2f8f112e 1316
046d01eb 1317 my $this_name = "utf8n_to_uvchr() $testname: " . (($disallow_flag)
13d7a909
KW
1318 ? 'disallowed'
1319 : ($disallowed)
1d1c12d9 1320 ? 'ABOVE_31_BIT allowed'
13d7a909
KW
1321 : 'allowed');
1322 $this_name .= ", $eval_warn";
1323 $this_name .= ", " . (($warn_flag)
1324 ? 'with warning flag'
1325 : 'no warning flag');
eb83ed87 1326
13d7a909
KW
1327 undef @warnings;
1328 my $ret_ref;
7dfd8446
KW
1329 my $display_bytes = display_bytes($bytes);
1330 my $call = "Call was: $eval_warn; \$ret_ref = test_utf8n_to_uvchr('$display_bytes', $length, $warn_flag|$disallow_flag)";
1331 my $eval_text = "$eval_warn; \$ret_ref = test_utf8n_to_uvchr('$bytes', $length, $warn_flag|$disallow_flag)";
13d7a909
KW
1332 eval "$eval_text";
1333 if (! ok ("$@ eq ''", "$this_name: eval succeeded")) {
7dfd8446 1334 diag "\$!='$!'; eval'd=\"$call\"";
13d7a909 1335 next;
54f4afef 1336 }
13d7a909 1337 if ($disallowed) {
7dfd8446
KW
1338 unless (is($ret_ref->[0], 0, "$this_name: Returns 0"))
1339 {
1340 diag $call;
1341 }
2f8f112e
KW
1342 }
1343 else {
7dfd8446 1344 unless (is($ret_ref->[0], $allowed_uv,
9d2d0ecd
KW
1345 "$this_name: Returns expected uv: "
1346 . sprintf("0x%04X", $allowed_uv)))
7dfd8446
KW
1347 {
1348 diag $call;
1349 }
1350 }
1351 unless (is($ret_ref->[1], $expected_len,
9d2d0ecd 1352 "$this_name: Returns expected length: $expected_len"))
7dfd8446
KW
1353 {
1354 diag $call;
13d7a909 1355 }
13d7a909
KW
1356
1357 if (! $do_warning
1358 && ($warning eq 'utf8' || $warning eq $category))
1359 {
1360 if (!is(scalar @warnings, 0,
1361 "$this_name: No warnings generated"))
1362 {
7dfd8446
KW
1363 diag $call;
1364 diag "The warnings were: " . join(", ", @warnings);
2f8f112e
KW
1365 }
1366 }
13d7a909
KW
1367 elsif ($will_overflow
1368 && ! $disallow_flag
1369 && $warning eq 'utf8')
1370 {
1371
1372 # Will get the overflow message instead of the expected
1373 # message under these circumstances, as they would
1374 # otherwise accept an overflowed value, which the code
1375 # should not allow, so falls back to overflow.
1376 if (is(scalar @warnings, 1,
1377 "$this_name: Got a single warning "))
1378 {
7dfd8446
KW
1379 unless (like($warnings[0], qr/overflow/,
1380 "$this_name: Got overflow warning"))
1381 {
1382 diag $call;
1383 }
13d7a909
KW
1384 }
1385 else {
7dfd8446 1386 diag $call;
13d7a909 1387 if (scalar @warnings) {
7dfd8446 1388 diag "The warnings were: "
13d7a909
KW
1389 . join(", ", @warnings);
1390 }
1391 }
eb83ed87 1392 }
13d7a909
KW
1393 elsif ($warn_flag
1394 && ($warning eq 'utf8' || $warning eq $category))
1395 {
1396 if (is(scalar @warnings, 1,
1397 "$this_name: Got a single warning "))
1398 {
7dfd8446
KW
1399 unless (like($warnings[0], $message,
1400 "$this_name: Got expected warning"))
1401 {
1402 diag $call;
1403 }
13d7a909
KW
1404 }
1405 else {
7dfd8446 1406 diag $call;
13d7a909 1407 if (scalar @warnings) {
7dfd8446 1408 diag "The warnings were: "
13d7a909
KW
1409 . join(", ", @warnings);
1410 }
eb83ed87
KW
1411 }
1412 }
eb83ed87 1413
13d7a909
KW
1414 # Check CHECK_ONLY results when the input is disallowed. Do
1415 # this when actually disallowed, not just when the
1416 # $disallow_flag is set
1417 if ($disallowed) {
1418 undef @warnings;
1419 $ret_ref = test_utf8n_to_uvchr($bytes, $length,
1420 $disallow_flag|$UTF8_CHECK_ONLY);
7dfd8446
KW
1421 unless (is($ret_ref->[0], 0, "$this_name, CHECK_ONLY: Returns 0")) {
1422 diag $call;
1423 }
1424 unless (is($ret_ref->[1], -1,
9d2d0ecd 1425 "$this_name: CHECK_ONLY: returns -1 for length"))
7dfd8446
KW
1426 {
1427 diag $call;
1428 }
13d7a909
KW
1429 if (! is(scalar @warnings, 0,
1430 "$this_name, CHECK_ONLY: no warnings generated"))
1431 {
7dfd8446
KW
1432 diag $call;
1433 diag "The warnings were: " . join(", ", @warnings);
13d7a909 1434 }
eb83ed87 1435 }
046d01eb
KW
1436
1437 # Now repeat some of the above, but for
1438 # uvchr_to_utf8_flags(). Since this comes from an
1439 # existing code point, it hasn't overflowed.
1440 next if $will_overflow;
1441
1442 # The warning and disallow flags passed in are for
1443 # utf8n_to_uvchr(). Convert them for
1444 # uvchr_to_utf8_flags().
1445 my $uvchr_warn_flag = 0;
1446 my $uvchr_disallow_flag = 0;
1447 if ($warn_flag) {
1448 if ($warn_flag == $UTF8_WARN_SURROGATE) {
1449 $uvchr_warn_flag = $UNICODE_WARN_SURROGATE
1450 }
1451 elsif ($warn_flag == $UTF8_WARN_NONCHAR) {
1452 $uvchr_warn_flag = $UNICODE_WARN_NONCHAR
1453 }
1454 elsif ($warn_flag == $UTF8_WARN_SUPER) {
1455 $uvchr_warn_flag = $UNICODE_WARN_SUPER
1456 }
1d1c12d9 1457 elsif ($warn_flag == $UTF8_WARN_ABOVE_31_BIT) {
046d01eb
KW
1458 $uvchr_warn_flag = $UNICODE_WARN_ABOVE_31_BIT;
1459 }
1460 else {
1461 fail(sprintf "Unexpected warn flag: %x",
1462 $warn_flag);
1463 next;
1464 }
1465 }
1466 if ($disallow_flag) {
1467 if ($disallow_flag == $UTF8_DISALLOW_SURROGATE) {
1468 $uvchr_disallow_flag = $UNICODE_DISALLOW_SURROGATE
1469 }
1470 elsif ($disallow_flag == $UTF8_DISALLOW_NONCHAR) {
1471 $uvchr_disallow_flag = $UNICODE_DISALLOW_NONCHAR
1472 }
1473 elsif ($disallow_flag == $UTF8_DISALLOW_SUPER) {
1474 $uvchr_disallow_flag = $UNICODE_DISALLOW_SUPER
1475 }
1d1c12d9 1476 elsif ($disallow_flag == $UTF8_DISALLOW_ABOVE_31_BIT) {
046d01eb
KW
1477 $uvchr_disallow_flag =
1478 $UNICODE_DISALLOW_ABOVE_31_BIT;
1479 }
1480 else {
1481 fail(sprintf "Unexpected disallow flag: %x",
1482 $disallow_flag);
1483 next;
1484 }
1485 }
1486
1487 $disallowed = $uvchr_disallow_flag;
1488
1489 $this_name = "uvchr_to_utf8_flags() $testname: "
1490 . (($uvchr_disallow_flag)
1491 ? 'disallowed'
1492 : ($disallowed)
1493 ? 'ABOVE_31_BIT allowed'
1494 : 'allowed');
1495 $this_name .= ", $eval_warn";
1496 $this_name .= ", " . (($uvchr_warn_flag)
1497 ? 'with warning flag'
1498 : 'no warning flag');
1499
1500 undef @warnings;
1501 my $ret;
1502 my $warn_flag = sprintf "0x%x", $uvchr_warn_flag;
1503 my $disallow_flag = sprintf "0x%x", $uvchr_disallow_flag;
1504 $call = sprintf "call was: $eval_warn; \$ret = test_uvchr_to_utf8_flags(0x%x, $warn_flag|$disallow_flag)", $allowed_uv;
1505 $eval_text = "$eval_warn; \$ret = test_uvchr_to_utf8_flags($allowed_uv, $warn_flag|$disallow_flag)";
1506 eval "$eval_text";
1507 if (! ok ("$@ eq ''", "$this_name: eval succeeded")) {
1508 diag "\$!='$!'; eval'd=\"$eval_text\"";
1509 next;
1510 }
1511 if ($disallowed) {
1512 unless (is($ret, undef, "$this_name: Returns undef")) {
1513 diag $call;
1514 }
1515 }
1516 else {
1517 unless (is($ret, $bytes, "$this_name: Returns expected string")) {
1518 diag $call;
1519 }
1520 }
1521 if (! $do_warning
1522 && ($warning eq 'utf8' || $warning eq $category))
1523 {
1524 if (!is(scalar @warnings, 0,
1525 "$this_name: No warnings generated"))
1526 {
1527 diag $call;
1528 diag "The warnings were: " . join(", ", @warnings);
1529 }
1530 }
1531 elsif ($uvchr_warn_flag
1532 && ($warning eq 'utf8' || $warning eq $category))
1533 {
1534 if (is(scalar @warnings, 1,
1535 "$this_name: Got a single warning "))
1536 {
046d01eb
KW
1537 unless (like($warnings[0], $message,
1538 "$this_name: Got expected warning"))
1539 {
1540 diag $call;
1541 }
046d01eb
KW
1542 }
1543 else {
1544 diag $call;
1545 if (scalar @warnings) {
1546 diag "The warnings were: "
1547 . join(", ", @warnings);
1548 }
1549 }
1550 }
eb83ed87
KW
1551 }
1552 }
1553 }
1554 }
1555}
6e3d6c02 1556
fed3ba5d 1557done_testing;