This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
c221b89c5bdba28cd0549fa688af0234e111e3a1
[perl5.git] / ext / XS-APItest / t / utf8.t
1 #!perl -w
2
3 use strict;
4 use Test::More;
5 $|=1;
6
7 no warnings 'deprecated'; # Some of the below are above IV_MAX on 32 bit
8                           # machines, and that is tested elsewhere
9
10 use XS::APItest;
11 use Data::Dumper;
12 my $pound_sign = chr utf8::unicode_to_native(163);
13
14 sub isASCII { ord "A" == 65 }
15
16 sub display_bytes {
17     use bytes;
18     my $string = shift;
19     return   '"'
20            . join("", map { sprintf("\\x%02x", ord $_) } split "", $string)
21            . '"';
22 }
23
24 sub output_warnings(@) {
25     diag "The warnings were:\n" . join("", @_);
26 }
27
28 # This  test file can't use byte_utf8a_to_utf8n() from t/charset_tools.pl
29 # because that uses the same functions we are testing here.  So UTF-EBCDIC
30 # strings are hard-coded as I8 strings in this file instead, and we use array
31 # lookup to translate into the appropriate code page.
32
33 my @i8_to_native = (    # Only code page 1047 so far.
34 # _0   _1   _2   _3   _4   _5   _6   _7   _8   _9   _A   _B   _C   _D   _E   _F
35 0x00,0x01,0x02,0x03,0x37,0x2D,0x2E,0x2F,0x16,0x05,0x15,0x0B,0x0C,0x0D,0x0E,0x0F,
36 0x10,0x11,0x12,0x13,0x3C,0x3D,0x32,0x26,0x18,0x19,0x3F,0x27,0x1C,0x1D,0x1E,0x1F,
37 0x40,0x5A,0x7F,0x7B,0x5B,0x6C,0x50,0x7D,0x4D,0x5D,0x5C,0x4E,0x6B,0x60,0x4B,0x61,
38 0xF0,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,0xF9,0x7A,0x5E,0x4C,0x7E,0x6E,0x6F,
39 0x7C,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,
40 0xD7,0xD8,0xD9,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xAD,0xE0,0xBD,0x5F,0x6D,
41 0x79,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x91,0x92,0x93,0x94,0x95,0x96,
42 0x97,0x98,0x99,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xC0,0x4F,0xD0,0xA1,0x07,
43 0x20,0x21,0x22,0x23,0x24,0x25,0x06,0x17,0x28,0x29,0x2A,0x2B,0x2C,0x09,0x0A,0x1B,
44 0x30,0x31,0x1A,0x33,0x34,0x35,0x36,0x08,0x38,0x39,0x3A,0x3B,0x04,0x14,0x3E,0xFF,
45 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x51,0x52,0x53,0x54,0x55,0x56,
46 0x57,0x58,0x59,0x62,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x70,0x71,0x72,0x73,
47 0x74,0x75,0x76,0x77,0x78,0x80,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x9A,0x9B,0x9C,
48 0x9D,0x9E,0x9F,0xA0,0xAA,0xAB,0xAC,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,
49 0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBE,0xBF,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xDA,0xDB,
50 0xDC,0xDD,0xDE,0xDF,0xE1,0xEA,0xEB,0xEC,0xED,0xEE,0xEF,0xFA,0xFB,0xFC,0xFD,0xFE,
51 );
52
53 my @native_to_i8;
54 for (my $i = 0; $i < 256; $i++) {
55     $native_to_i8[$i8_to_native[$i]] = $i;
56 }
57
58 *I8_to_native = (isASCII)
59                 ? sub { return shift }
60                 : sub { return join "", map { chr $i8_to_native[ord $_] }
61                                             split "", shift };
62 *native_to_I8 = (isASCII)
63                 ? sub { return shift }
64                 : sub { return join "", map { chr $native_to_i8[ord $_] }
65                                             split "", shift };
66 sub start_byte_to_cont($) {
67
68     # Extract the code point information from the input UTF-8 start byte, and
69     # return a continuation byte containing the same information.  This is
70     # used in constructing an overlong malformation from valid input.
71
72     my $byte = shift;
73     my $len = test_UTF8_SKIP($byte);
74     if ($len < 2) {
75         die "start_byte_to_cont() is expecting a UTF-8 variant";
76     }
77
78     $byte = ord native_to_I8($byte);
79
80     # Copied from utf8.h.  This gets rid of the leading 1 bits.
81     $byte &= ((($len) >= 7) ? 0x00 : (0x1F >> (($len)-2)));
82
83     $byte |= (isASCII) ? 0x80 : 0xA0;
84     return I8_to_native(chr $byte);
85 }
86
87 my $is64bit = length sprintf("%x", ~0) > 8;
88
89
90 # Test utf8n_to_uvchr_error().  These provide essentially complete code
91 # coverage.  Copied from utf8.h
92 my $UTF8_ALLOW_EMPTY            = 0x0001;
93 my $UTF8_GOT_EMPTY              = $UTF8_ALLOW_EMPTY;
94 my $UTF8_ALLOW_CONTINUATION     = 0x0002;
95 my $UTF8_GOT_CONTINUATION       = $UTF8_ALLOW_CONTINUATION;
96 my $UTF8_ALLOW_NON_CONTINUATION = 0x0004;
97 my $UTF8_GOT_NON_CONTINUATION   = $UTF8_ALLOW_NON_CONTINUATION;
98 my $UTF8_ALLOW_SHORT            = 0x0008;
99 my $UTF8_GOT_SHORT              = $UTF8_ALLOW_SHORT;
100 my $UTF8_ALLOW_LONG             = 0x0010;
101 my $UTF8_GOT_LONG               = $UTF8_ALLOW_LONG;
102 my $UTF8_GOT_OVERFLOW           = 0x0020;
103 my $UTF8_DISALLOW_SURROGATE     = 0x0040;
104 my $UTF8_GOT_SURROGATE          = $UTF8_DISALLOW_SURROGATE;
105 my $UTF8_WARN_SURROGATE         = 0x0080;
106 my $UTF8_DISALLOW_NONCHAR       = 0x0100;
107 my $UTF8_GOT_NONCHAR            = $UTF8_DISALLOW_NONCHAR;
108 my $UTF8_WARN_NONCHAR           = 0x0200;
109 my $UTF8_DISALLOW_SUPER         = 0x0400;
110 my $UTF8_GOT_SUPER              = $UTF8_DISALLOW_SUPER;
111 my $UTF8_WARN_SUPER             = 0x0800;
112 my $UTF8_DISALLOW_ABOVE_31_BIT  = 0x1000;
113 my $UTF8_GOT_ABOVE_31_BIT       = $UTF8_DISALLOW_ABOVE_31_BIT;
114 my $UTF8_WARN_ABOVE_31_BIT      = 0x2000;
115 my $UTF8_CHECK_ONLY             = 0x4000;
116 my $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE
117                              = $UTF8_DISALLOW_SUPER|$UTF8_DISALLOW_SURROGATE;
118 my $UTF8_DISALLOW_ILLEGAL_INTERCHANGE
119               = $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE|$UTF8_DISALLOW_NONCHAR;
120 my $UTF8_WARN_ILLEGAL_C9_INTERCHANGE
121                              = $UTF8_WARN_SUPER|$UTF8_WARN_SURROGATE;
122 my $UTF8_WARN_ILLEGAL_INTERCHANGE
123               = $UTF8_WARN_ILLEGAL_C9_INTERCHANGE|$UTF8_WARN_NONCHAR;
124
125 # Test uvchr_to_utf8().
126 my $UNICODE_WARN_SURROGATE        = 0x0001;
127 my $UNICODE_WARN_NONCHAR          = 0x0002;
128 my $UNICODE_WARN_SUPER            = 0x0004;
129 my $UNICODE_WARN_ABOVE_31_BIT     = 0x0008;
130 my $UNICODE_DISALLOW_SURROGATE    = 0x0010;
131 my $UNICODE_DISALLOW_NONCHAR      = 0x0020;
132 my $UNICODE_DISALLOW_SUPER        = 0x0040;
133 my $UNICODE_DISALLOW_ABOVE_31_BIT = 0x0080;
134
135 my $look_for_everything_utf8n_to
136                         = $UTF8_DISALLOW_SURROGATE
137                         | $UTF8_WARN_SURROGATE
138                         | $UTF8_DISALLOW_NONCHAR
139                         | $UTF8_WARN_NONCHAR
140                         | $UTF8_DISALLOW_SUPER
141                         | $UTF8_WARN_SUPER
142                         | $UTF8_DISALLOW_ABOVE_31_BIT
143                         | $UTF8_WARN_ABOVE_31_BIT;
144 my $look_for_everything_uvchr_to
145                         = $UNICODE_DISALLOW_SURROGATE
146                         | $UNICODE_WARN_SURROGATE
147                         | $UNICODE_DISALLOW_NONCHAR
148                         | $UNICODE_WARN_NONCHAR
149                         | $UNICODE_DISALLOW_SUPER
150                         | $UNICODE_WARN_SUPER
151                         | $UNICODE_DISALLOW_ABOVE_31_BIT
152                         | $UNICODE_WARN_ABOVE_31_BIT;
153
154 foreach ([0, '', '', 'empty'],
155          [0, 'N', 'N', '1 char'],
156          [1, 'NN', 'N', '1 char substring'],
157          [-2, 'Perl', 'Rules', 'different'],
158          [0, $pound_sign, $pound_sign, 'pound sign'],
159          [1, $pound_sign . 10, $pound_sign . 1,
160                                             '10 pounds is more than 1 pound'],
161          [1, $pound_sign . $pound_sign, $pound_sign,
162                                             '2 pound signs are more than 1'],
163          [-2, ' $!', " \x{1F42B}!", 'Camels are worth more than 1 dollar'],
164          [-1, '!', "!\x{1F42A}", 'Initial substrings match'],
165         ) {
166     my ($expect, $left, $right, $desc) = @$_;
167     my $copy = $right;
168     utf8::encode($copy);
169     is(bytes_cmp_utf8($left, $copy), $expect, $desc);
170     next if $right =~ tr/\0-\377//c;
171     utf8::encode($left);
172     is(bytes_cmp_utf8($right, $left), -$expect, "$desc reversed");
173 }
174
175 # The keys to this hash are Unicode code points, their values are the native
176 # UTF-8 representations of them.  The code points are chosen because they are
177 # "interesting" on either or both ASCII and EBCDIC platforms.  First we add
178 # boundaries where the number of bytes required to represent them increase, or
179 # are adjacent to problematic code points, so we want to make sure they aren't
180 # considered problematic.
181 my %code_points = (
182     0x0100     => (isASCII) ? "\xc4\x80" : I8_to_native("\xc8\xa0"),
183     0x0400 - 1 => (isASCII) ? "\xcf\xbf" : I8_to_native("\xdf\xbf"),
184     0x0400     => (isASCII) ? "\xd0\x80" : I8_to_native("\xe1\xa0\xa0"),
185     0x0800 - 1 => (isASCII) ? "\xdf\xbf"     : I8_to_native("\xe1\xbf\xbf"),
186     0x0800     => (isASCII) ? "\xe0\xa0\x80" : I8_to_native("\xe2\xa0\xa0"),
187     0x4000 - 1 => (isASCII) ? "\xe3\xbf\xbf" : I8_to_native("\xef\xbf\xbf"),
188     0x4000     => (isASCII) ? "\xe4\x80\x80" : I8_to_native("\xf0\xb0\xa0\xa0"),
189     0x8000 - 1 => (isASCII) ? "\xe7\xbf\xbf" : I8_to_native("\xf0\xbf\xbf\xbf"),
190
191     # First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
192     # as of this writing, considers potentially problematic on EBCDIC
193     0x8000     => (isASCII) ? "\xe8\x80\x80" : I8_to_native("\xf1\xa0\xa0\xa0"),
194
195     0xD000 - 1 => (isASCII) ? "\xec\xbf\xbf" : I8_to_native("\xf1\xb3\xbf\xbf"),
196
197     # First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
198     # as of this writing, considers potentially problematic on ASCII
199     0xD000     => (isASCII) ? "\xed\x80\x80" : I8_to_native("\xf1\xb4\xa0\xa0"),
200
201     # Bracket the surrogates, and include several surrogates
202     0xD7FF     => (isASCII) ? "\xed\x9f\xbf" : I8_to_native("\xf1\xb5\xbf\xbf"),
203     0xD800     => (isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
204     0xDC00     => (isASCII) ? "\xed\xb0\x80" : I8_to_native("\xf1\xb7\xa0\xa0"),
205     0xDFFF     => (isASCII) ? "\xee\x80\x80" : I8_to_native("\xf1\xb8\xa0\xa0"),
206     0xDFFF     => (isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
207     0xE000     => (isASCII) ? "\xee\x80\x80" : I8_to_native("\xf1\xb8\xa0\xa0"),
208
209     # Include the 32 contiguous non characters, and surrounding code points
210     0xFDCF     => (isASCII) ? "\xef\xb7\x8f" : I8_to_native("\xf1\xbf\xae\xaf"),
211     0xFDD0     => (isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
212     0xFDD1     => (isASCII) ? "\xef\xb7\x91" : I8_to_native("\xf1\xbf\xae\xb1"),
213     0xFDD2     => (isASCII) ? "\xef\xb7\x92" : I8_to_native("\xf1\xbf\xae\xb2"),
214     0xFDD3     => (isASCII) ? "\xef\xb7\x93" : I8_to_native("\xf1\xbf\xae\xb3"),
215     0xFDD4     => (isASCII) ? "\xef\xb7\x94" : I8_to_native("\xf1\xbf\xae\xb4"),
216     0xFDD5     => (isASCII) ? "\xef\xb7\x95" : I8_to_native("\xf1\xbf\xae\xb5"),
217     0xFDD6     => (isASCII) ? "\xef\xb7\x96" : I8_to_native("\xf1\xbf\xae\xb6"),
218     0xFDD7     => (isASCII) ? "\xef\xb7\x97" : I8_to_native("\xf1\xbf\xae\xb7"),
219     0xFDD8     => (isASCII) ? "\xef\xb7\x98" : I8_to_native("\xf1\xbf\xae\xb8"),
220     0xFDD9     => (isASCII) ? "\xef\xb7\x99" : I8_to_native("\xf1\xbf\xae\xb9"),
221     0xFDDA     => (isASCII) ? "\xef\xb7\x9a" : I8_to_native("\xf1\xbf\xae\xba"),
222     0xFDDB     => (isASCII) ? "\xef\xb7\x9b" : I8_to_native("\xf1\xbf\xae\xbb"),
223     0xFDDC     => (isASCII) ? "\xef\xb7\x9c" : I8_to_native("\xf1\xbf\xae\xbc"),
224     0xFDDD     => (isASCII) ? "\xef\xb7\x9d" : I8_to_native("\xf1\xbf\xae\xbd"),
225     0xFDDE     => (isASCII) ? "\xef\xb7\x9e" : I8_to_native("\xf1\xbf\xae\xbe"),
226     0xFDDF     => (isASCII) ? "\xef\xb7\x9f" : I8_to_native("\xf1\xbf\xae\xbf"),
227     0xFDE0     => (isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
228     0xFDE1     => (isASCII) ? "\xef\xb7\xa1" : I8_to_native("\xf1\xbf\xaf\xa1"),
229     0xFDE2     => (isASCII) ? "\xef\xb7\xa2" : I8_to_native("\xf1\xbf\xaf\xa2"),
230     0xFDE3     => (isASCII) ? "\xef\xb7\xa3" : I8_to_native("\xf1\xbf\xaf\xa3"),
231     0xFDE4     => (isASCII) ? "\xef\xb7\xa4" : I8_to_native("\xf1\xbf\xaf\xa4"),
232     0xFDE5     => (isASCII) ? "\xef\xb7\xa5" : I8_to_native("\xf1\xbf\xaf\xa5"),
233     0xFDE6     => (isASCII) ? "\xef\xb7\xa6" : I8_to_native("\xf1\xbf\xaf\xa6"),
234     0xFDE7     => (isASCII) ? "\xef\xb7\xa7" : I8_to_native("\xf1\xbf\xaf\xa7"),
235     0xFDE8     => (isASCII) ? "\xef\xb7\xa8" : I8_to_native("\xf1\xbf\xaf\xa8"),
236     0xFDEa     => (isASCII) ? "\xef\xb7\x99" : I8_to_native("\xf1\xbf\xaf\xa9"),
237     0xFDEA     => (isASCII) ? "\xef\xb7\xaa" : I8_to_native("\xf1\xbf\xaf\xaa"),
238     0xFDEB     => (isASCII) ? "\xef\xb7\xab" : I8_to_native("\xf1\xbf\xaf\xab"),
239     0xFDEC     => (isASCII) ? "\xef\xb7\xac" : I8_to_native("\xf1\xbf\xaf\xac"),
240     0xFDED     => (isASCII) ? "\xef\xb7\xad" : I8_to_native("\xf1\xbf\xaf\xad"),
241     0xFDEE     => (isASCII) ? "\xef\xb7\xae" : I8_to_native("\xf1\xbf\xaf\xae"),
242     0xFDEF     => (isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
243     0xFDF0     => (isASCII) ? "\xef\xb7\xb0" : I8_to_native("\xf1\xbf\xaf\xb0"),
244
245     # Mostly around non-characters, but some are transitions to longer strings
246     0xFFFD     => (isASCII) ? "\xef\xbf\xbd" : I8_to_native("\xf1\xbf\xbf\xbd"),
247     0x10000 - 1 => (isASCII)
248                    ?              "\xef\xbf\xbf"
249                    : I8_to_native("\xf1\xbf\xbf\xbf"),
250     0x10000     => (isASCII)
251                    ?              "\xf0\x90\x80\x80"
252                    : I8_to_native("\xf2\xa0\xa0\xa0"),
253     0x1FFFD     => (isASCII)
254                    ?              "\xf0\x9f\xbf\xbd"
255                    : I8_to_native("\xf3\xbf\xbf\xbd"),
256     0x1FFFE     => (isASCII)
257                    ?              "\xf0\x9f\xbf\xbe"
258                    : I8_to_native("\xf3\xbf\xbf\xbe"),
259     0x1FFFF     => (isASCII)
260                    ?              "\xf0\x9f\xbf\xbf"
261                    : I8_to_native("\xf3\xbf\xbf\xbf"),
262     0x20000     => (isASCII)
263                    ?              "\xf0\xa0\x80\x80"
264                    : I8_to_native("\xf4\xa0\xa0\xa0"),
265     0x2FFFD     => (isASCII)
266                    ?              "\xf0\xaf\xbf\xbd"
267                    : I8_to_native("\xf5\xbf\xbf\xbd"),
268     0x2FFFE     => (isASCII)
269                    ?              "\xf0\xaf\xbf\xbe"
270                    : I8_to_native("\xf5\xbf\xbf\xbe"),
271     0x2FFFF     => (isASCII)
272                    ?              "\xf0\xaf\xbf\xbf"
273                    : I8_to_native("\xf5\xbf\xbf\xbf"),
274     0x30000     => (isASCII)
275                    ?              "\xf0\xb0\x80\x80"
276                    : I8_to_native("\xf6\xa0\xa0\xa0"),
277     0x3FFFD     => (isASCII)
278                    ?              "\xf0\xbf\xbf\xbd"
279                    : I8_to_native("\xf7\xbf\xbf\xbd"),
280     0x3FFFE     => (isASCII)
281                    ?              "\xf0\xbf\xbf\xbe"
282                    : I8_to_native("\xf7\xbf\xbf\xbe"),
283     0x40000 - 1 => (isASCII)
284                    ?              "\xf0\xbf\xbf\xbf"
285                    : I8_to_native("\xf7\xbf\xbf\xbf"),
286     0x40000     => (isASCII)
287                    ?              "\xf1\x80\x80\x80"
288                    : I8_to_native("\xf8\xa8\xa0\xa0\xa0"),
289     0x4FFFD     => (isASCII)
290                    ?              "\xf1\x8f\xbf\xbd"
291                    : I8_to_native("\xf8\xa9\xbf\xbf\xbd"),
292     0x4FFFE     => (isASCII)
293                    ?              "\xf1\x8f\xbf\xbe"
294                    : I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
295     0x4FFFF     => (isASCII)
296                    ?              "\xf1\x8f\xbf\xbf"
297                    : I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
298     0x50000     => (isASCII)
299                    ?              "\xf1\x90\x80\x80"
300                    : I8_to_native("\xf8\xaa\xa0\xa0\xa0"),
301     0x5FFFD     => (isASCII)
302                    ?              "\xf1\x9f\xbf\xbd"
303                    : I8_to_native("\xf8\xab\xbf\xbf\xbd"),
304     0x5FFFE     => (isASCII)
305                    ?              "\xf1\x9f\xbf\xbe"
306                    : I8_to_native("\xf8\xab\xbf\xbf\xbe"),
307     0x5FFFF     => (isASCII)
308                    ?              "\xf1\x9f\xbf\xbf"
309                    : I8_to_native("\xf8\xab\xbf\xbf\xbf"),
310     0x60000     => (isASCII)
311                    ?              "\xf1\xa0\x80\x80"
312                    : I8_to_native("\xf8\xac\xa0\xa0\xa0"),
313     0x6FFFD     => (isASCII)
314                    ?              "\xf1\xaf\xbf\xbd"
315                    : I8_to_native("\xf8\xad\xbf\xbf\xbd"),
316     0x6FFFE     => (isASCII)
317                    ?              "\xf1\xaf\xbf\xbe"
318                    : I8_to_native("\xf8\xad\xbf\xbf\xbe"),
319     0x6FFFF     => (isASCII)
320                    ?              "\xf1\xaf\xbf\xbf"
321                    : I8_to_native("\xf8\xad\xbf\xbf\xbf"),
322     0x70000     => (isASCII)
323                    ?              "\xf1\xb0\x80\x80"
324                    : I8_to_native("\xf8\xae\xa0\xa0\xa0"),
325     0x7FFFD     => (isASCII)
326                    ?              "\xf1\xbf\xbf\xbd"
327                    : I8_to_native("\xf8\xaf\xbf\xbf\xbd"),
328     0x7FFFE     => (isASCII)
329                    ?              "\xf1\xbf\xbf\xbe"
330                    : I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
331     0x7FFFF     => (isASCII)
332                    ?              "\xf1\xbf\xbf\xbf"
333                    : I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
334     0x80000     => (isASCII)
335                    ?              "\xf2\x80\x80\x80"
336                    : I8_to_native("\xf8\xb0\xa0\xa0\xa0"),
337     0x8FFFD     => (isASCII)
338                    ?              "\xf2\x8f\xbf\xbd"
339                    : I8_to_native("\xf8\xb1\xbf\xbf\xbd"),
340     0x8FFFE     => (isASCII)
341                    ?              "\xf2\x8f\xbf\xbe"
342                    : I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
343     0x8FFFF     => (isASCII)
344                    ?              "\xf2\x8f\xbf\xbf"
345                    : I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
346     0x90000     => (isASCII)
347                    ?              "\xf2\x90\x80\x80"
348                    : I8_to_native("\xf8\xb2\xa0\xa0\xa0"),
349     0x9FFFD     => (isASCII)
350                    ?              "\xf2\x9f\xbf\xbd"
351                    : I8_to_native("\xf8\xb3\xbf\xbf\xbd"),
352     0x9FFFE     => (isASCII)
353                    ?              "\xf2\x9f\xbf\xbe"
354                    : I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
355     0x9FFFF     => (isASCII)
356                    ?              "\xf2\x9f\xbf\xbf"
357                    : I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
358     0xA0000     => (isASCII)
359                    ?              "\xf2\xa0\x80\x80"
360                    : I8_to_native("\xf8\xb4\xa0\xa0\xa0"),
361     0xAFFFD     => (isASCII)
362                    ?              "\xf2\xaf\xbf\xbd"
363                    : I8_to_native("\xf8\xb5\xbf\xbf\xbd"),
364     0xAFFFE     => (isASCII)
365                    ?              "\xf2\xaf\xbf\xbe"
366                    : I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
367     0xAFFFF     => (isASCII)
368                    ?              "\xf2\xaf\xbf\xbf"
369                    : I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
370     0xB0000     => (isASCII)
371                    ?              "\xf2\xb0\x80\x80"
372                    : I8_to_native("\xf8\xb6\xa0\xa0\xa0"),
373     0xBFFFD     => (isASCII)
374                    ?              "\xf2\xbf\xbf\xbd"
375                    : I8_to_native("\xf8\xb7\xbf\xbf\xbd"),
376     0xBFFFE     => (isASCII)
377                    ?              "\xf2\xbf\xbf\xbe"
378                    : I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
379     0xBFFFF     => (isASCII)
380                    ?               "\xf2\xbf\xbf\xbf"
381                    : I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
382     0xC0000     => (isASCII)
383                    ?               "\xf3\x80\x80\x80"
384                    : I8_to_native("\xf8\xb8\xa0\xa0\xa0"),
385     0xCFFFD     => (isASCII)
386                    ?               "\xf3\x8f\xbf\xbd"
387                    : I8_to_native("\xf8\xb9\xbf\xbf\xbd"),
388     0xCFFFE     => (isASCII)
389                    ?               "\xf3\x8f\xbf\xbe"
390                    : I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
391     0xCFFFF     => (isASCII)
392                    ?               "\xf3\x8f\xbf\xbf"
393                    : I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
394     0xD0000     => (isASCII)
395                    ?               "\xf3\x90\x80\x80"
396                    : I8_to_native("\xf8\xba\xa0\xa0\xa0"),
397     0xDFFFD     => (isASCII)
398                    ?               "\xf3\x9f\xbf\xbd"
399                    : I8_to_native("\xf8\xbb\xbf\xbf\xbd"),
400     0xDFFFE     => (isASCII)
401                    ?               "\xf3\x9f\xbf\xbe"
402                    : I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
403     0xDFFFF     => (isASCII)
404                    ?               "\xf3\x9f\xbf\xbf"
405                    : I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
406     0xE0000     => (isASCII)
407                    ?               "\xf3\xa0\x80\x80"
408                    : I8_to_native("\xf8\xbc\xa0\xa0\xa0"),
409     0xEFFFD     => (isASCII)
410                    ?               "\xf3\xaf\xbf\xbd"
411                    : I8_to_native("\xf8\xbd\xbf\xbf\xbd"),
412     0xEFFFE     => (isASCII)
413                    ?               "\xf3\xaf\xbf\xbe"
414                    : I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
415     0xEFFFF     => (isASCII)
416                    ?               "\xf3\xaf\xbf\xbf"
417                    : I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
418     0xF0000     => (isASCII)
419                    ?               "\xf3\xb0\x80\x80"
420                    : I8_to_native("\xf8\xbe\xa0\xa0\xa0"),
421     0xFFFFD     => (isASCII)
422                    ?               "\xf3\xbf\xbf\xbd"
423                    : I8_to_native("\xf8\xbf\xbf\xbf\xbd"),
424     0xFFFFE     => (isASCII)
425                    ?               "\xf3\xbf\xbf\xbe"
426                    : I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
427     0xFFFFF     => (isASCII)
428                    ?               "\xf3\xbf\xbf\xbf"
429                    : I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
430     0x100000    => (isASCII)
431                    ?               "\xf4\x80\x80\x80"
432                    : I8_to_native("\xf9\xa0\xa0\xa0\xa0"),
433     0x10FFFD    => (isASCII)
434                    ?               "\xf4\x8f\xbf\xbd"
435                    : I8_to_native("\xf9\xa1\xbf\xbf\xbd"),
436     0x10FFFE    => (isASCII)
437                    ?               "\xf4\x8f\xbf\xbe"
438                    : I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
439     0x10FFFF    => (isASCII)
440                    ?               "\xf4\x8f\xbf\xbf"
441                    : I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
442     0x110000    => (isASCII)
443                    ?               "\xf4\x90\x80\x80"
444                    : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
445
446     # Things that would be noncharacters if they were in Unicode, and might be
447     # mistaken, if the C code is bad, to be nonchars
448     0x11FFFE    => (isASCII)
449                    ?               "\xf4\x9f\xbf\xbe"
450                     : I8_to_native("\xf9\xa3\xbf\xbf\xbe"),
451     0x11FFFF    => (isASCII)
452                    ?               "\xf4\x9f\xbf\xbf"
453                     : I8_to_native("\xf9\xa3\xbf\xbf\xbf"),
454     0x20FFFE    => (isASCII)
455                    ?               "\xf8\x88\x8f\xbf\xbe"
456                     : I8_to_native("\xfa\xa1\xbf\xbf\xbe"),
457     0x20FFFF    => (isASCII)
458                    ?               "\xf8\x88\x8f\xbf\xbf"
459                     : I8_to_native("\xfa\xa1\xbf\xbf\xbf"),
460
461     0x200000 - 1 => (isASCII)
462                     ?              "\xf7\xbf\xbf\xbf"
463                     : I8_to_native("\xf9\xbf\xbf\xbf\xbf"),
464     0x200000     => (isASCII)
465                     ?              "\xf8\x88\x80\x80\x80"
466                     : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
467     0x400000 - 1 => (isASCII)
468                     ?              "\xf8\x8f\xbf\xbf\xbf"
469                     : I8_to_native("\xfb\xbf\xbf\xbf\xbf"),
470     0x400000     => (isASCII)
471                     ?              "\xf8\x90\x80\x80\x80"
472                     : I8_to_native("\xfc\xa4\xa0\xa0\xa0\xa0"),
473     0x4000000 - 1 => (isASCII)
474                      ?              "\xfb\xbf\xbf\xbf\xbf"
475                      : I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
476     0x4000000     => (isASCII)
477                      ?              "\xfc\x84\x80\x80\x80\x80"
478                      : I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
479     0x4000000 - 1 => (isASCII)
480                      ?              "\xfb\xbf\xbf\xbf\xbf"
481                      : I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
482     0x4000000     => (isASCII)
483                      ?              "\xfc\x84\x80\x80\x80\x80"
484                      : I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
485     0x40000000 - 1 => (isASCII)
486                       ?              "\xfc\xbf\xbf\xbf\xbf\xbf"
487                       : I8_to_native("\xfe\xbf\xbf\xbf\xbf\xbf\xbf"),
488     0x40000000     =>
489     (isASCII) ?    "\xfd\x80\x80\x80\x80\x80"
490     : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0"),
491     0x80000000 - 1 =>
492     (isASCII) ?    "\xfd\xbf\xbf\xbf\xbf\xbf"
493     : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf"),
494     0x80000000     =>
495     (isASCII) ?    "\xfe\x82\x80\x80\x80\x80\x80"
496     : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
497     0xFFFFFFFF     =>
498     (isASCII) ?    "\xfe\x83\xbf\xbf\xbf\xbf\xbf"
499     : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
500 );
501
502 if ($is64bit) {
503     no warnings qw(overflow portable);
504     $code_points{0x100000000}
505      = (isASCII)
506      ?              "\xfe\x84\x80\x80\x80\x80\x80"
507      : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0");
508     $code_points{0x1000000000 - 1}
509      = (isASCII)
510      ?              "\xfe\xbf\xbf\xbf\xbf\xbf\xbf"
511      : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
512     $code_points{0x1000000000}
513      = (isASCII)
514      ?              "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
515      : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0");
516     $code_points{0xFFFFFFFFFFFFFFFF}
517      = (isASCII)
518      ?              "\xff\x80\x8f\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
519      : I8_to_native("\xff\xaf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
520     if (isASCII) {  # These could falsely show as overlongs in a naive
521                     # implementation
522         $code_points{0x40000000000}
523                       = "\xff\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80";
524         $code_points{0x1000000000000}
525                       = "\xff\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80";
526         $code_points{0x40000000000000}
527                       = "\xff\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80";
528         $code_points{0x1000000000000000}
529                       = "\xff\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
530         # overflows
531         #$code_points{0xfoo}
532         #           = "\xff\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
533     }
534 }
535 elsif (! isASCII) { # 32-bit EBCDIC.  64-bit is clearer to handle, so doesn't
536                     # need this test case
537     no warnings qw(overflow portable);
538     $code_points{0x40000000} = I8_to_native(
539                     "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0");
540 }
541
542 # Now add in entries for each of code points 0-255, which require special
543 # handling on EBCDIC.  Remember the keys are Unicode values, and the values
544 # are the native UTF-8.  For invariants, the bytes are just the native chr.
545
546 my $cp = 0;
547 while ($cp < ((isASCII) ? 128 : 160)) {   # This is from the definition of
548                                         # invariant
549     $code_points{$cp} = chr utf8::unicode_to_native($cp);
550     $cp++;
551 }
552
553 # Done with the invariants.  Now do the variants.  All in this range are 2
554 # byte.  Again, we can't use the internal functions to generate UTF-8, as
555 # those are what we are trying to test.  In the loop, we know what range the
556 # continuation bytes can be in, and what the lowest start byte can be.  So we
557 # cycle through them.
558
559 my $first_continuation = (isASCII) ? 0x80 : 0xA0;
560 my $final_continuation = 0xBF;
561 my $start = (isASCII) ? 0xC2 : 0xC5;
562
563 my $max_bytes = (isASCII) ? 13 : 14; # Max number of bytes in a UTF-8 sequence
564                                      # representing a single code point
565
566 my $continuation = $first_continuation - 1;
567
568 while ($cp < 255) {
569     if (++$continuation > $final_continuation) {
570
571         # Wrap to the next start byte when we reach the final continuation
572         # byte possible
573         $continuation = $first_continuation;
574         $start++;
575     }
576     $code_points{$cp} = I8_to_native(chr($start) . chr($continuation));
577
578     $cp++;
579 }
580
581 my @warnings;
582
583 use warnings 'utf8';
584 local $SIG{__WARN__} = sub { push @warnings, @_ };
585
586 my %restriction_types;
587
588 $restriction_types{""}{'valid_strings'} = "";
589 $restriction_types{"c9strict"}{'valid_strings'} = "";
590 $restriction_types{"strict"}{'valid_strings'} = "";
591 $restriction_types{"fits_in_31_bits"}{'valid_strings'} = "";
592
593 # This set of tests looks for basic sanity, and lastly tests various routines
594 # for the given code point.  If the earlier tests for that code point fail,
595 # the later ones probably will too.  Malformations are tested in later
596 # segments of code.
597 for my $u (sort { utf8::unicode_to_native($a) <=> utf8::unicode_to_native($b) }
598           keys %code_points)
599 {
600     my $hex_u = sprintf("0x%02X", $u);
601     my $n = utf8::unicode_to_native($u);
602     my $hex_n = sprintf("0x%02X", $n);
603     my $bytes = $code_points{$u};
604
605     my $offskip_should_be;
606     {
607         no warnings qw(overflow portable);
608         $offskip_should_be = (isASCII)
609             ? ( $u < 0x80           ? 1 :
610                 $u < 0x800          ? 2 :
611                 $u < 0x10000        ? 3 :
612                 $u < 0x200000       ? 4 :
613                 $u < 0x4000000      ? 5 :
614                 $u < 0x80000000     ? 6 : (($is64bit)
615                                         ? ($u < 0x1000000000 ? 7 : $max_bytes)
616                                         : 7)
617               )
618             : ($u < 0xA0        ? 1 :
619                $u < 0x400       ? 2 :
620                $u < 0x4000      ? 3 :
621                $u < 0x40000     ? 4 :
622                $u < 0x400000    ? 5 :
623                $u < 0x4000000   ? 6 :
624                $u < 0x40000000  ? 7 : $max_bytes );
625     }
626
627     # If this test fails, subsequent ones are meaningless.
628     next unless is(test_OFFUNISKIP($u), $offskip_should_be,
629                    "Verify OFFUNISKIP($hex_u) is $offskip_should_be");
630     my $invariant = $offskip_should_be == 1;
631     my $display_invariant = $invariant || 0;
632     is(test_OFFUNI_IS_INVARIANT($u), $invariant,
633        "Verify OFFUNI_IS_INVARIANT($hex_u) is $display_invariant");
634
635     my $uvchr_skip_should_be = $offskip_should_be;
636     next unless is(test_UVCHR_SKIP($n), $uvchr_skip_should_be,
637                    "Verify UVCHR_SKIP($hex_n) is $uvchr_skip_should_be");
638     is(test_UVCHR_IS_INVARIANT($n), $offskip_should_be == 1,
639        "Verify UVCHR_IS_INVARIANT($hex_n) is $display_invariant");
640
641     my $n_chr = chr $n;
642     utf8::upgrade $n_chr;
643
644     is(test_UTF8_SKIP($n_chr), $uvchr_skip_should_be,
645         "Verify UTF8_SKIP(chr $hex_n) is $uvchr_skip_should_be");
646
647     use bytes;
648     my $byte_length = length $n_chr;
649     for (my $j = 0; $j < $byte_length; $j++) {
650         undef @warnings;
651
652         if ($j == $byte_length - 1) {
653             my $ret
654               = test_is_utf8_valid_partial_char_flags($n_chr, $byte_length, 0);
655             is($ret, 0, "   Verify is_utf8_valid_partial_char_flags("
656                       . display_bytes($n_chr)
657                       . ") returns 0 for full character");
658         }
659         else {
660             my $bytes_so_far = substr($n_chr, 0, $j + 1);
661             my $ret
662              = test_is_utf8_valid_partial_char_flags($bytes_so_far, $j + 1, 0);
663             is($ret, 1, "   Verify is_utf8_valid_partial_char_flags("
664                       . display_bytes($bytes_so_far)
665                       . ") returns 1");
666         }
667
668         is(scalar @warnings, 0, "   Verify is_utf8_valid_partial_char_flags"
669                               . " generated no warnings")
670           or output_warnings(@warnings);
671
672         my $b = substr($n_chr, $j, 1);
673         my $hex_b = sprintf("\"\\x%02x\"", ord $b);
674
675         my $byte_invariant = $j == 0 && $uvchr_skip_should_be == 1;
676         my $display_byte_invariant = $byte_invariant || 0;
677         next unless is(test_UTF8_IS_INVARIANT($b), $byte_invariant,
678                        "   Verify UTF8_IS_INVARIANT($hex_b) for byte $j "
679                      . "is $display_byte_invariant");
680
681         my $is_start = $j == 0 && $uvchr_skip_should_be > 1;
682         my $display_is_start = $is_start || 0;
683         next unless is(test_UTF8_IS_START($b), $is_start,
684                     "      Verify UTF8_IS_START($hex_b) is $display_is_start");
685
686         my $is_continuation = $j != 0 && $uvchr_skip_should_be > 1;
687         my $display_is_continuation = $is_continuation || 0;
688         next unless is(test_UTF8_IS_CONTINUATION($b), $is_continuation,
689                        "      Verify UTF8_IS_CONTINUATION($hex_b) is "
690                      . "$display_is_continuation");
691
692         my $is_continued = $uvchr_skip_should_be > 1;
693         my $display_is_continued = $is_continued || 0;
694         next unless is(test_UTF8_IS_CONTINUED($b), $is_continued,
695                        "      Verify UTF8_IS_CONTINUED($hex_b) is "
696                      . "$display_is_continued");
697
698         my $is_downgradeable_start =    $n < 256
699                                      && $uvchr_skip_should_be > 1
700                                      && $j == 0;
701         my $display_is_downgradeable_start = $is_downgradeable_start || 0;
702         next unless is(test_UTF8_IS_DOWNGRADEABLE_START($b),
703                        $is_downgradeable_start,
704                        "      Verify UTF8_IS_DOWNGRADEABLE_START($hex_b) is "
705                      . "$display_is_downgradeable_start");
706
707         my $is_above_latin1 =  $n > 255 && $j == 0;
708         my $display_is_above_latin1 = $is_above_latin1 || 0;
709         next unless is(test_UTF8_IS_ABOVE_LATIN1($b),
710                        $is_above_latin1,
711                        "      Verify UTF8_IS_ABOVE_LATIN1($hex_b) is "
712                      . "$display_is_above_latin1");
713
714         my $is_possibly_problematic =  $j == 0
715                                     && $n >= ((isASCII)
716                                               ? 0xD000
717                                               : 0x8000);
718         my $display_is_possibly_problematic = $is_possibly_problematic || 0;
719         next unless is(test_isUTF8_POSSIBLY_PROBLEMATIC($b),
720                        $is_possibly_problematic,
721                        "      Verify isUTF8_POSSIBLY_PROBLEMATIC($hex_b) is "
722                      . "$display_is_above_latin1");
723     }
724
725     # We are not trying to look for warnings, etc, so if they should occur, it
726     # is an error.  But some of the code points here do cause warnings, so we
727     # check here and turn off the ones that apply to such code points.  A
728     # later section of the code tests for these kinds of things.
729     my $this_utf8_flags = $look_for_everything_utf8n_to;
730     my $len = length $bytes;
731
732     my $valid_under_strict = 1;
733     my $valid_under_c9strict = 1;
734     my $valid_for_fits_in_31_bits = 1;
735     if ($n > 0x10FFFF) {
736         $this_utf8_flags &= ~($UTF8_DISALLOW_SUPER|$UTF8_WARN_SUPER);
737         $valid_under_strict = 0;
738         $valid_under_c9strict = 0;
739         if ($n > 2 ** 31 - 1) {
740             $this_utf8_flags &=
741                         ~($UTF8_DISALLOW_ABOVE_31_BIT|$UTF8_WARN_ABOVE_31_BIT);
742             $valid_for_fits_in_31_bits = 0;
743         }
744     }
745     elsif (($n >= 0xFDD0 && $n <= 0xFDEF) || ($n & 0xFFFE) == 0xFFFE) {
746         $this_utf8_flags &= ~($UTF8_DISALLOW_NONCHAR|$UTF8_WARN_NONCHAR);
747         $valid_under_strict = 0;
748     }
749     elsif ($n >= 0xD800 && $n <= 0xDFFF) {
750         $this_utf8_flags &= ~($UTF8_DISALLOW_SURROGATE|$UTF8_WARN_SURROGATE);
751         $valid_under_c9strict = 0;
752         $valid_under_strict = 0;
753     }
754
755     undef @warnings;
756
757     my $display_flags = sprintf "0x%x", $this_utf8_flags;
758     my $display_bytes = display_bytes($bytes);
759     my $ret_ref = test_utf8n_to_uvchr_error($bytes, $len, $this_utf8_flags);
760
761     # Rest of tests likely meaningless if it gets the wrong code point.
762     next unless is($ret_ref->[0], $n,
763                    "Verify utf8n_to_uvchr_error($display_bytes, $display_flags)"
764                  . "returns $hex_n");
765     is($ret_ref->[1], $len,
766        "Verify utf8n_to_uvchr_error() for $hex_n returns expected length:"
767      . " $len");
768
769     unless (is(scalar @warnings, 0,
770              "Verify utf8n_to_uvchr_error() for $hex_n generated no warnings"))
771     {
772         output_warnings(@warnings);
773     }
774     is($ret_ref->[2], 0,
775        "Verify utf8n_to_uvchr_error() returned no error bits");
776
777     undef @warnings;
778
779     my $ret = test_isUTF8_CHAR($bytes, $len);
780     is($ret, $len,
781             "Verify isUTF8_CHAR($display_bytes) returns expected length: $len");
782
783     unless (is(scalar @warnings, 0,
784                "Verify isUTF8_CHAR() for $hex_n generated no warnings"))
785     {
786         output_warnings(@warnings);
787     }
788
789     undef @warnings;
790
791     $ret = test_isUTF8_CHAR($bytes, $len - 1);
792     is($ret, 0,
793             "Verify isUTF8_CHAR() with too short length parameter returns 0");
794
795     is(scalar @warnings, 0, "Verify isUTF8_CHAR() generated no warnings")
796       or output_warnings(@warnings);
797
798     undef @warnings;
799
800     $ret = test_isUTF8_CHAR_flags($bytes, $len, 0);
801     is($ret, $len, "Verify isUTF8_CHAR_flags($display_bytes, 0)"
802                  . " returns expected length: $len");
803
804     is(scalar @warnings, 0,
805                "Verify isUTF8_CHAR_flags() for $hex_n generated no warnings")
806       or output_warnings(@warnings);
807
808     undef @warnings;
809
810     $ret = test_isUTF8_CHAR_flags($bytes, $len - 1, 0);
811     is($ret, 0,
812         "Verify isUTF8_CHAR_flags() with too short length parameter returns 0");
813
814     is(scalar @warnings, 0, "Verify isUTF8_CHAR_flags() generated no warnings")
815       or output_warnings(@warnings);
816
817     undef @warnings;
818
819     $ret = test_isSTRICT_UTF8_CHAR($bytes, $len);
820     my $expected_len = ($valid_under_strict) ? $len : 0;
821     is($ret, $expected_len, "Verify isSTRICT_UTF8_CHAR($display_bytes)"
822                           . " returns expected length: $expected_len");
823
824     is(scalar @warnings, 0,
825                "Verify isSTRICT_UTF8_CHAR() for $hex_n generated no warnings")
826       or output_warnings(@warnings);
827
828     undef @warnings;
829
830     $ret = test_isSTRICT_UTF8_CHAR($bytes, $len - 1);
831     is($ret, 0,
832        "Verify isSTRICT_UTF8_CHAR() with too short length parameter returns 0");
833
834     is(scalar @warnings, 0, "Verify isSTRICT_UTF8_CHAR() generated no warnings")
835       or output_warnings(@warnings);
836
837     undef @warnings;
838
839     $ret = test_isUTF8_CHAR_flags($bytes, $len,
840                                             $UTF8_DISALLOW_ILLEGAL_INTERCHANGE);
841     is($ret, $expected_len,
842                     "Verify isUTF8_CHAR_flags('DISALLOW_ILLEGAL_INTERCHANGE')"
843                   . " acts like isSTRICT_UTF8_CHAR");
844
845     is(scalar @warnings, 0,
846                "Verify isUTF8_CHAR() for $hex_n generated no warnings")
847       or output_warnings(@warnings);
848
849     undef @warnings;
850
851     $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len);
852     $expected_len = ($valid_under_c9strict) ? $len : 0;
853     is($ret, $expected_len, "Verify isC9_STRICT_UTF8_CHAR($display_bytes)"
854                           . " returns expected length: $len");
855
856     is(scalar @warnings, 0,
857             "Verify isC9_STRICT_UTF8_CHAR() for $hex_n generated no warnings")
858       or output_warnings(@warnings);
859
860     undef @warnings;
861
862     $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len - 1);
863     is($ret, 0,
864     "Verify isC9_STRICT_UTF8_CHAR() with too short length parameter returns 0");
865
866     is(scalar @warnings, 0,
867                "Verify isC9_STRICT_UTF8_CHAR() generated no warnings")
868       or output_warnings(@warnings);
869
870     undef @warnings;
871
872     $ret = test_isUTF8_CHAR_flags($bytes, $len,
873                                         $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE);
874     is($ret, $expected_len,
875                    "Verify isUTF8_CHAR_flags('DISALLOW_ILLEGAL_C9_INTERCHANGE')"
876                   ." acts like isC9_STRICT_UTF8_CHAR");
877
878     is(scalar @warnings, 0,
879                "Verify isUTF8_CHAR() for $hex_n generated no warnings")
880       or output_warnings(@warnings);
881
882     undef @warnings;
883
884     $ret_ref = test_valid_utf8_to_uvchr($bytes);
885     is($ret_ref->[0], $n,
886                    "Verify valid_utf8_to_uvchr($display_bytes) returns $hex_n");
887     is($ret_ref->[1], $len,
888        "Verify valid_utf8_to_uvchr() for $hex_n returns expected length: $len");
889
890     is(scalar @warnings, 0,
891                "Verify valid_utf8_to_uvchr() for $hex_n generated no warnings")
892       or output_warnings(@warnings);
893
894     # Similarly for uvchr_to_utf8
895     my $this_uvchr_flags = $look_for_everything_uvchr_to;
896     if ($n > 2 ** 31 - 1) {
897         $this_uvchr_flags &=
898                 ~($UNICODE_DISALLOW_ABOVE_31_BIT|$UNICODE_WARN_ABOVE_31_BIT);
899     }
900     if ($n > 0x10FFFF) {
901         $this_uvchr_flags &= ~($UNICODE_DISALLOW_SUPER|$UNICODE_WARN_SUPER);
902     }
903     elsif (($n >= 0xFDD0 && $n <= 0xFDEF) || ($n & 0xFFFE) == 0xFFFE) {
904         $this_uvchr_flags &= ~($UNICODE_DISALLOW_NONCHAR|$UNICODE_WARN_NONCHAR);
905     }
906     elsif ($n >= 0xD800 && $n <= 0xDFFF) {
907         $this_uvchr_flags
908                      &= ~($UNICODE_DISALLOW_SURROGATE|$UNICODE_WARN_SURROGATE);
909     }
910     $display_flags = sprintf "0x%x", $this_uvchr_flags;
911
912     undef @warnings;
913
914     $ret = test_uvchr_to_utf8_flags($n, $this_uvchr_flags);
915     ok(defined $ret,
916         "Verify uvchr_to_utf8_flags($hex_n, $display_flags) returned success");
917     is($ret, $bytes,
918     "Verify uvchr_to_utf8_flags($hex_n, $display_flags) returns correct bytes");
919
920     is(scalar @warnings, 0,
921                 "Verify uvchr_to_utf8_flags($hex_n, $display_flags) for $hex_n"
922               . " generated no warnings")
923       or output_warnings(@warnings);
924
925     # Now append this code point to a string that we will test various
926     # versions of is_foo_utf8_string_bar on, and keep a count of how many code
927     # points are in it.  All the code points in this loop are valid in Perl's
928     # extended UTF-8, but some are not valid under various restrictions.  A
929     # string and count is kept separately that is entirely valid for each
930     # restriction.  And, for each restriction, we note the first occurrence in
931     # the unrestricted string where we find something not in the restricted
932     # string.
933     $restriction_types{""}{'valid_strings'} .= $bytes;
934     $restriction_types{""}{'valid_counts'}++;
935
936     if ($valid_under_c9strict) {
937         $restriction_types{"c9strict"}{'valid_strings'} .= $bytes;
938         $restriction_types{"c9strict"}{'valid_counts'}++;
939     }
940     elsif (! exists $restriction_types{"c9strict"}{'first_invalid_offset'}) {
941         $restriction_types{"c9strict"}{'first_invalid_offset'}
942                     = length $restriction_types{"c9strict"}{'valid_strings'};
943         $restriction_types{"c9strict"}{'first_invalid_count'}
944                             = $restriction_types{"c9strict"}{'valid_counts'};
945     }
946
947     if ($valid_under_strict) {
948         $restriction_types{"strict"}{'valid_strings'} .= $bytes;
949         $restriction_types{"strict"}{'valid_counts'}++;
950     }
951     elsif (! exists $restriction_types{"strict"}{'first_invalid_offset'}) {
952         $restriction_types{"strict"}{'first_invalid_offset'}
953                         = length $restriction_types{"strict"}{'valid_strings'};
954         $restriction_types{"strict"}{'first_invalid_count'}
955                                 = $restriction_types{"strict"}{'valid_counts'};
956     }
957
958     if ($valid_for_fits_in_31_bits) {
959         $restriction_types{"fits_in_31_bits"}{'valid_strings'} .= $bytes;
960         $restriction_types{"fits_in_31_bits"}{'valid_counts'}++;
961     }
962     elsif (! exists
963                 $restriction_types{"fits_in_31_bits"}{'first_invalid_offset'})
964     {
965         $restriction_types{"fits_in_31_bits"}{'first_invalid_offset'}
966                 = length $restriction_types{"fits_in_31_bits"}{'valid_strings'};
967         $restriction_types{"fits_in_31_bits"}{'first_invalid_count'}
968                         = $restriction_types{"fits_in_31_bits"}{'valid_counts'};
969     }
970 }
971
972 my $I8c = (isASCII) ? "\x80" : "\xa0";    # A continuation byte
973 my $cont_byte = I8_to_native($I8c);
974 my $p = (isASCII) ? "\xe1\x80" : I8_to_native("\xE4\xA0");  # partial
975
976 # The loop above tested the single or partial character functions/macros,
977 # while building up strings to test the string functions, which we do now.
978
979 for my $restriction (sort keys %restriction_types) {
980     use bytes;
981
982     for my $use_flags ("", "_flags") {
983
984         # For each restriction, we test it in both the is_foo_flags functions
985         # and the specially named foo function.  But not if there isn't such a
986         # specially named function.  Currently, this is the only tested
987         # restriction that doesn't have a specially named function
988         next if $use_flags eq "" && $restriction eq "fits_in_31_bits";
989
990         # Start building up the name of the function we will test.
991         my $base_name = "is_";
992
993         if (! $use_flags  && $restriction ne "") {
994             $base_name .= $restriction . "_";
995         }
996
997         # We test both "is_utf8_string_foo" and "is_fixed_width_buf" functions
998         foreach my $operand ('string', 'fixed_width_buf') {
999
1000             # Currently, the only fixed_width_buf functions have the '_flags'
1001             # suffix.
1002             next if $operand eq 'fixed_width_buf' && $use_flags eq "";
1003
1004             my $name = "${base_name}utf8_$operand";
1005
1006             # We test each version of the function
1007             for my $function ("_loclen", "_loc", "") {
1008
1009                 # We test each function against
1010                 #   a) valid input
1011                 #   b) invalid input created by appending an out-of-place
1012                 #      continuation character to the valid string
1013                 #   c) input created by appending a partial character.  This
1014                 #      is valid in the 'fixed_width' functions, but invalid in
1015                 #   the 'string' ones
1016                 #   d) invalid input created by calling a function that is
1017                 #      expecting a restricted form of the input using the string
1018                 #      that's valid when unrestricted
1019                 for my $error_type (0, $cont_byte, $p, $restriction) {
1020                     #diag "restriction=$restriction, use_flags=$use_flags, function=$function, error_type=" . display_bytes($error_type);
1021
1022                     # If there is no restriction, the error type will be "",
1023                     # which is redundant with 0.
1024                     next if $error_type eq "";
1025
1026                     my $this_name = "$name$function$use_flags";
1027                     my $bytes
1028                             = $restriction_types{$restriction}{'valid_strings'};
1029                     my $expected_offset = length $bytes;
1030                     my $expected_count
1031                             = $restriction_types{$restriction}{'valid_counts'};
1032                     my $test_name_suffix = "";
1033
1034                     my $this_error_type = $error_type;
1035                     if ($this_error_type) {
1036
1037                         # Appending a bare continuation byte or a partial
1038                         # character doesn't change the character count or
1039                         # offset.  But in the other cases, we have saved where
1040                         # the failures should occur, so use those.  Appending
1041                         # a continuation byte makes it invalid; appending a
1042                         # partial character makes the 'string' form invalid,
1043                         # but not the 'fixed_width_buf' form.
1044                         if (   $this_error_type eq $cont_byte
1045                             || $this_error_type eq $p)
1046                         {
1047                             $bytes .= $this_error_type;
1048                             if ($this_error_type eq $cont_byte) {
1049                                 $test_name_suffix
1050                                             = " for an unexpected continuation";
1051                             }
1052                             else {
1053                                 $test_name_suffix
1054                                         = " if ends with a partial character";
1055                                 $this_error_type
1056                                         = 0 if $operand eq "fixed_width_buf";
1057                             }
1058                         }
1059                         else {
1060                             $test_name_suffix
1061                                         = " if contains forbidden code points";
1062                             if ($this_error_type eq "c9strict") {
1063                                 $bytes = $restriction_types{""}{'valid_strings'};
1064                                 $expected_offset
1065                                  = $restriction_types{"c9strict"}
1066                                                      {'first_invalid_offset'};
1067                                 $expected_count
1068                                   = $restriction_types{"c9strict"}
1069                                                       {'first_invalid_count'};
1070                             }
1071                             elsif ($this_error_type eq "strict") {
1072                                 $bytes = $restriction_types{""}{'valid_strings'};
1073                                 $expected_offset
1074                                   = $restriction_types{"strict"}
1075                                                       {'first_invalid_offset'};
1076                                 $expected_count
1077                                   = $restriction_types{"strict"}
1078                                                       {'first_invalid_count'};
1079
1080                             }
1081                             elsif ($this_error_type eq "fits_in_31_bits") {
1082                                 $bytes = $restriction_types{""}{'valid_strings'};
1083                                 $expected_offset
1084                                   = $restriction_types{"fits_in_31_bits"}
1085                                                       {'first_invalid_offset'};
1086                                 $expected_count
1087                                     = $restriction_types{"fits_in_31_bits"}
1088                                                         {'first_invalid_count'};
1089                             }
1090                             else {
1091                                 fail("Internal test error: Unknown error type "
1092                                 . "'$this_error_type'");
1093                                 next;
1094                             }
1095                         }
1096                     }
1097
1098                     my $length = length $bytes;
1099                     my $ret_ref;
1100
1101                     my $test = "\$ret_ref = test_$this_name(\$bytes, $length";
1102
1103                     # If using the _flags functions, we have to figure out what
1104                     # flags to pass.  This is done to match the restriction.
1105                     if ($use_flags eq "_flags") {
1106                         if (! $restriction) {
1107                             $test .= ", 0";     # The flag
1108
1109                             # Indicate the kind of flag in the test name.
1110                             $this_name .= "(0)";
1111                         }
1112                         else {
1113                             $this_name .= "($restriction)";
1114                             if ($restriction eq "c9strict") {
1115                                 $test
1116                                   .= ", $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE";
1117                             }
1118                             elsif ($restriction eq "strict") {
1119                                 $test .= ", $UTF8_DISALLOW_ILLEGAL_INTERCHANGE";
1120                             }
1121                             elsif ($restriction eq "fits_in_31_bits") {
1122                                 $test .= ", $UTF8_DISALLOW_ABOVE_31_BIT";
1123                             }
1124                             else {
1125                                 fail("Internal test error: Unknown restriction "
1126                                 . "'$restriction'");
1127                                 next;
1128                             }
1129                         }
1130                     }
1131                     $test .= ")";
1132
1133                     # Actually run the test
1134                     eval $test;
1135                     if ($@) {
1136                         fail($test);
1137                         diag $@;
1138                         next;
1139                     }
1140
1141                     my $ret;
1142                     my $error_offset;
1143                     my $cp_count;
1144
1145                     if ($function eq "") {
1146                         $ret = $ret_ref;    # For plain function, there's only a
1147                                             # single return value
1148                     }
1149                     else {  # Otherwise, the multiple values come in an array.
1150                         $ret = shift @$ret_ref ;
1151                         $error_offset = shift @$ret_ref;
1152                         $cp_count = shift@$ret_ref if $function eq "_loclen";
1153                     }
1154
1155                     if ($this_error_type) {
1156                         is($ret, 0,
1157                            "Verify $this_name is FALSE$test_name_suffix");
1158                     }
1159                     else {
1160                         unless(is($ret, 1,
1161                                   "Verify $this_name is TRUE for valid input"
1162                                 . "$test_name_suffix"))
1163                         {
1164                             diag("    The bytes starting at offset"
1165                                . " $error_offset are"
1166                                . display_bytes(substr(
1167                                           $restriction_types{$restriction}
1168                                                             {'valid_strings'},
1169                                           $error_offset)));
1170                             next;
1171                         }
1172                     }
1173
1174                     if ($function ne "") {
1175                         unless (is($error_offset, $expected_offset,
1176                                    "\tAnd returns the correct offset"))
1177                         {
1178                             my $min = ($error_offset < $expected_offset)
1179                                     ? $error_offset
1180                                     : $expected_offset;
1181                             diag("    The bytes starting at offset" . $min
1182                               . " are " . display_bytes(substr($bytes, $min)));
1183                         }
1184
1185                         if ($function eq '_loclen') {
1186                             is($cp_count, $expected_count,
1187                                "\tAnd returns the correct character count");
1188                         }
1189                     }
1190                 }
1191             }
1192         }
1193     }
1194 }
1195
1196 my $REPLACEMENT = 0xFFFD;
1197
1198 # Now test the malformations.  All these raise category utf8 warnings.
1199 my @malformations = (
1200     # ($testname, $bytes, $length, $allow_flags, $expected_error_flags,
1201     #  $allowed_uv, $expected_len, $needed_to_discern_len, $message )
1202     [ "zero length string malformation", "", 0,
1203         $UTF8_ALLOW_EMPTY, $UTF8_GOT_EMPTY, 0, 0, 0,
1204         qr/empty string/
1205     ],
1206     [ "orphan continuation byte malformation", I8_to_native("${I8c}a"), 2,
1207         $UTF8_ALLOW_CONTINUATION, $UTF8_GOT_CONTINUATION, $REPLACEMENT,
1208         1, 1,
1209         qr/unexpected continuation byte/
1210     ],
1211     [ "premature next character malformation (immediate)",
1212         (isASCII) ? "\xc2\xc2\x80" : I8_to_native("\xc5\xc5\xa0"),
1213         3,
1214         $UTF8_ALLOW_NON_CONTINUATION, $UTF8_GOT_NON_CONTINUATION, $REPLACEMENT,
1215         1, 2,
1216         qr/unexpected non-continuation byte.*immediately after start byte/
1217     ],
1218     [ "premature next character malformation (non-immediate)",
1219         I8_to_native("\xef${I8c}a"), 3,
1220         $UTF8_ALLOW_NON_CONTINUATION, $UTF8_GOT_NON_CONTINUATION, $REPLACEMENT,
1221         2, 3,
1222         qr/unexpected non-continuation byte .* 2 bytes after start byte/
1223     ],
1224     [ "too short malformation", I8_to_native("\xf1${I8c}a"), 2,
1225         # Having the 'a' after this, but saying there are only 2 bytes also
1226         # tests that we pay attention to the passed in length
1227         $UTF8_ALLOW_SHORT, $UTF8_GOT_SHORT, $REPLACEMENT,
1228         2, 2,
1229         qr/2 bytes available, need 4/
1230     ],
1231     [ "overlong malformation, lowest 2-byte",
1232         (isASCII) ? "\xc0\x80" : I8_to_native("\xc0\xa0"),
1233         2,
1234         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1235         0,   # NUL
1236         2, 1,
1237         qr/overlong/
1238     ],
1239     [ "overlong malformation, highest 2-byte",
1240         (isASCII) ? "\xc1\xbf" : I8_to_native("\xc4\xbf"),
1241         2,
1242         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1243         (isASCII) ? 0x7F : utf8::unicode_to_native(0x9F),
1244         2, 1,
1245         qr/overlong/
1246     ],
1247     [ "overlong malformation, lowest 3-byte",
1248         (isASCII) ? "\xe0\x80\x80" : I8_to_native("\xe0\xa0\xa0"),
1249         3,
1250         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1251         0,   # NUL
1252         3, (isASCII) ? 2 : 1,
1253         qr/overlong/
1254     ],
1255     [ "overlong malformation, highest 3-byte",
1256         (isASCII) ? "\xe0\x9f\xbf" : I8_to_native("\xe0\xbf\xbf"),
1257         3,
1258         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1259         (isASCII) ? 0x7FF : 0x3FF,
1260         3, (isASCII) ? 2 : 1,
1261         qr/overlong/
1262     ],
1263     [ "overlong malformation, lowest 4-byte",
1264         (isASCII) ? "\xf0\x80\x80\x80" : I8_to_native("\xf0\xa0\xa0\xa0"),
1265         4,
1266         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1267         0,   # NUL
1268         4, 2,
1269         qr/overlong/
1270     ],
1271     [ "overlong malformation, highest 4-byte",
1272         (isASCII) ? "\xf0\x8F\xbf\xbf" : I8_to_native("\xf0\xaf\xbf\xbf"),
1273         4,
1274         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1275         (isASCII) ? 0xFFFF : 0x3FFF,
1276         4, 2,
1277         qr/overlong/
1278     ],
1279     [ "overlong malformation, lowest 5-byte",
1280         (isASCII)
1281          ?              "\xf8\x80\x80\x80\x80"
1282          : I8_to_native("\xf8\xa0\xa0\xa0\xa0"),
1283         5,
1284         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1285         0,   # NUL
1286         5, 2,
1287         qr/overlong/
1288     ],
1289     [ "overlong malformation, highest 5-byte",
1290         (isASCII)
1291          ?              "\xf8\x87\xbf\xbf\xbf"
1292          : I8_to_native("\xf8\xa7\xbf\xbf\xbf"),
1293         5,
1294         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1295         (isASCII) ? 0x1FFFFF : 0x3FFFF,
1296         5, 2,
1297         qr/overlong/
1298     ],
1299     [ "overlong malformation, lowest 6-byte",
1300         (isASCII)
1301          ?              "\xfc\x80\x80\x80\x80\x80"
1302          : I8_to_native("\xfc\xa0\xa0\xa0\xa0\xa0"),
1303         6,
1304         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1305         0,   # NUL
1306         6, 2,
1307         qr/overlong/
1308     ],
1309     [ "overlong malformation, highest 6-byte",
1310         (isASCII)
1311          ?              "\xfc\x83\xbf\xbf\xbf\xbf"
1312          : I8_to_native("\xfc\xa3\xbf\xbf\xbf\xbf"),
1313         6,
1314         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1315         (isASCII) ? 0x3FFFFFF : 0x3FFFFF,
1316         6, 2,
1317         qr/overlong/
1318     ],
1319     [ "overlong malformation, lowest 7-byte",
1320         (isASCII)
1321          ?              "\xfe\x80\x80\x80\x80\x80\x80"
1322          : I8_to_native("\xfe\xa0\xa0\xa0\xa0\xa0\xa0"),
1323         7,
1324         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1325         0,   # NUL
1326         7, 2,
1327         qr/overlong/
1328     ],
1329     [ "overlong malformation, highest 7-byte",
1330         (isASCII)
1331          ?              "\xfe\x81\xbf\xbf\xbf\xbf\xbf"
1332          : I8_to_native("\xfe\xa1\xbf\xbf\xbf\xbf\xbf"),
1333         7,
1334         $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1335         (isASCII) ? 0x7FFFFFFF : 0x3FFFFFF,
1336         7, 2,
1337         qr/overlong/
1338     ],
1339 );
1340
1341 if (isASCII && ! $is64bit) {    # 32-bit ASCII platform
1342     no warnings 'portable';
1343     push @malformations,
1344         [ "overflow malformation",
1345             "\xfe\x84\x80\x80\x80\x80\x80",  # Represents 2**32
1346             7,
1347             0,  # There is no way to allow this malformation
1348             $UTF8_GOT_OVERFLOW,
1349             $REPLACEMENT,
1350             7, 2,
1351             qr/overflows/
1352         ],
1353         [ "overflow malformation",
1354             "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80",
1355             $max_bytes,
1356             0,  # There is no way to allow this malformation
1357             $UTF8_GOT_OVERFLOW,
1358             $REPLACEMENT,
1359             $max_bytes, 1,
1360             qr/overflows/
1361         ];
1362 }
1363 else { # 64-bit ASCII, or EBCDIC of any size.
1364     # On EBCDIC platforms, another overlong test is needed even on 32-bit
1365     # systems, whereas it doesn't happen on ASCII except on 64-bit ones.
1366
1367     no warnings 'portable';
1368     no warnings 'overflow'; # Doesn't run on 32-bit systems, but compiles
1369     push @malformations,
1370         [ "overlong malformation, lowest max-byte",
1371             (isASCII)
1372              ?      "\xff\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80"
1373              : I8_to_native(
1374                     "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1375             $max_bytes,
1376             $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1377             0,   # NUL
1378             $max_bytes, (isASCII) ? 7 : 8,
1379             qr/overlong/,
1380         ],
1381         [ "overlong malformation, highest max-byte",
1382             (isASCII)    # 2**36-1 on ASCII; 2**30-1 on EBCDIC
1383              ?      "\xff\x80\x80\x80\x80\x80\x80\xbf\xbf\xbf\xbf\xbf\xbf"
1384              : I8_to_native(
1385                     "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xbf\xbf\xbf\xbf\xbf\xbf"),
1386             $max_bytes,
1387             $UTF8_ALLOW_LONG, $UTF8_GOT_LONG,
1388             (isASCII) ? 0xFFFFFFFFF : 0x3FFFFFFF,
1389             $max_bytes, (isASCII) ? 7 : 8,
1390             qr/overlong/,
1391         ];
1392
1393     if (! $is64bit) {   # 32-bit EBCDIC
1394         push @malformations,
1395         [ "overflow malformation",
1396             I8_to_native(
1397                     "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"),
1398             $max_bytes,
1399             0,  # There is no way to allow this malformation
1400             $UTF8_GOT_OVERFLOW,
1401             $REPLACEMENT,
1402             $max_bytes, 8,
1403             qr/overflows/
1404         ];
1405     }
1406     else {  # 64-bit, either ASCII or EBCDIC
1407         push @malformations,
1408             [ "overflow malformation",
1409                (isASCII)
1410                 ?   "\xff\x80\x90\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"
1411                 : I8_to_native(
1412                     "\xff\xb0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
1413                 $max_bytes,
1414                 0,  # There is no way to allow this malformation
1415                 $UTF8_GOT_OVERFLOW,
1416                 $REPLACEMENT,
1417                 $max_bytes, (isASCII) ? 3 : 2,
1418                 qr/overflows/
1419             ];
1420     }
1421 }
1422
1423 foreach my $test (@malformations) {
1424     my ($testname, $bytes, $length, $allow_flags, $expected_error_flags,
1425         $allowed_uv, $expected_len, $needed_to_discern_len, $message ) = @$test;
1426
1427     if (length($bytes) < $length) {
1428         fail("Internal test error: actual buffer length (" . length($bytes)
1429            . ") must be at least as high as how far we are allowed to read"
1430            . " into it ($length)");
1431         diag($testname);
1432         next;
1433     }
1434
1435     undef @warnings;
1436
1437     my $ret = test_isUTF8_CHAR($bytes, $length);
1438     is($ret, 0, "$testname: isUTF8_CHAR returns 0");
1439     is(scalar @warnings, 0, "$testname: isUTF8_CHAR() generated no warnings")
1440       or output_warnings(@warnings);
1441
1442     undef @warnings;
1443
1444     $ret = test_isUTF8_CHAR_flags($bytes, $length, 0);
1445     is($ret, 0, "$testname: isUTF8_CHAR_flags returns 0");
1446     is(scalar @warnings, 0, "$testname: isUTF8_CHAR() generated no warnings")
1447       or output_warnings(@warnings);
1448
1449     $ret = test_isSTRICT_UTF8_CHAR($bytes, $length);
1450     is($ret, 0, "$testname: isSTRICT_UTF8_CHAR returns 0");
1451     is(scalar @warnings, 0,
1452                     "$testname: isSTRICT_UTF8_CHAR() generated no warnings")
1453       or output_warnings(@warnings);
1454
1455     $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $length);
1456     is($ret, 0, "$testname: isC9_STRICT_UTF8_CHAR returns 0");
1457     is(scalar @warnings, 0,
1458                "$testname: isC9_STRICT_UTF8_CHAR() generated no warnings")
1459       or output_warnings(@warnings);
1460
1461     for my $j (1 .. $length - 1) {
1462         my $partial = substr($bytes, 0, $j);
1463
1464         undef @warnings;
1465
1466         $ret = test_is_utf8_valid_partial_char_flags($bytes, $j, 0);
1467
1468         my $ret_should_be = 0;
1469         my $comment = "";
1470         if ($j < $needed_to_discern_len) {
1471             $ret_should_be = 1;
1472             $comment = ", but need $needed_to_discern_len bytes to discern:";
1473         }
1474
1475         is($ret, $ret_should_be, "$testname: is_utf8_valid_partial_char_flags("
1476                                 . display_bytes($partial)
1477                                 . ")$comment returns $ret_should_be");
1478         is(scalar @warnings, 0,
1479                 "$testname: is_utf8_valid_partial_char_flags() generated"
1480               . " no warnings")
1481           or output_warnings(@warnings);
1482     }
1483
1484
1485     # Test what happens when this malformation is not allowed
1486     undef @warnings;
1487     my $ret_ref = test_utf8n_to_uvchr_error($bytes, $length, 0);
1488     is($ret_ref->[0], 0, "$testname: disallowed: Returns 0");
1489     is($ret_ref->[1], $expected_len,
1490        "$testname: utf8n_to_uvchr_error(), disallowed: Returns expected"
1491      . " length: $expected_len");
1492     if (is(scalar @warnings, 1,
1493            "$testname: disallowed: Got a single warning "))
1494     {
1495         like($warnings[0], $message,
1496              "$testname: disallowed: Got expected warning");
1497     }
1498     else {
1499         if (scalar @warnings) {
1500             output_warnings(@warnings);
1501         }
1502     }
1503     is($ret_ref->[2], $expected_error_flags,
1504        "$testname: utf8n_to_uvchr_error(), disallowed:"
1505      . " Returns expected error");
1506
1507     {   # Next test when disallowed, and warnings are off.
1508         undef @warnings;
1509         no warnings 'utf8';
1510         my $ret_ref = test_utf8n_to_uvchr_error($bytes, $length, 0);
1511         is($ret_ref->[0], 0,
1512            "$testname: utf8n_to_uvchr_error(), disallowed: no warnings 'utf8':"
1513          . " Returns 0");
1514         is($ret_ref->[1], $expected_len,
1515            "$testname: utf8n_to_uvchr_error(), disallowed: no warnings 'utf8':"
1516          . " Returns expected length: $expected_len");
1517         if (!is(scalar @warnings, 0,
1518             "$testname: utf8n_to_uvchr_error(), disallowed: no warnings 'utf8':"
1519           . " no warnings generated"))
1520         {
1521             output_warnings(@warnings);
1522         }
1523         is($ret_ref->[2], $expected_error_flags,
1524            "$testname: utf8n_to_uvchr_error(), disallowed: Returns"
1525          . " expected error");
1526     }
1527
1528     # Test with CHECK_ONLY
1529     undef @warnings;
1530     $ret_ref = test_utf8n_to_uvchr_error($bytes, $length, $UTF8_CHECK_ONLY);
1531     is($ret_ref->[0], 0, "$testname: CHECK_ONLY: Returns 0");
1532     is($ret_ref->[1], -1, "$testname: CHECK_ONLY: returns -1 for length");
1533     if (! is(scalar @warnings, 0,
1534                                "$testname: CHECK_ONLY: no warnings generated"))
1535     {
1536         output_warnings(@warnings);
1537     }
1538     is($ret_ref->[2], $expected_error_flags,
1539        "$testname: utf8n_to_uvchr_error(), disallowed: Returns expected"
1540      . " error");
1541
1542     next if $allow_flags == 0;    # Skip if can't allow this malformation
1543
1544     # Test when the malformation is allowed
1545     undef @warnings;
1546     $ret_ref = test_utf8n_to_uvchr_error($bytes, $length, $allow_flags);
1547     is($ret_ref->[0], $allowed_uv,
1548        "$testname: utf8n_to_uvchr_error(), allowed: Returns expected uv: "
1549      . sprintf("0x%04X", $allowed_uv));
1550     is($ret_ref->[1], $expected_len,
1551        "$testname: utf8n_to_uvchr_error(), allowed: Returns expected length:"
1552      . " $expected_len");
1553     if (!is(scalar @warnings, 0,
1554             "$testname: utf8n_to_uvchr_error(), allowed: no warnings"
1555           . " generated"))
1556     {
1557         output_warnings(@warnings);
1558     }
1559     is($ret_ref->[2], $expected_error_flags,
1560        "$testname: utf8n_to_uvchr_error(), disallowed: Returns"
1561      . " expected error");
1562 }
1563
1564 sub nonportable_regex ($) {
1565
1566     # Returns a pattern that matches the non-portable message raised either
1567     # for the specific input code point, or the one generated when there
1568     # is some malformation that precludes the message containing the specific
1569     # code point
1570
1571     my $code_point = shift;
1572
1573     my $string = sprintf '(Code point 0x%X is not Unicode, and'
1574                        . '|Any UTF-8 sequence that starts with'
1575                        . ' "(\\\x[[:xdigit:]]{2})+" is for a'
1576                        . ' non-Unicode code point, and is) not portable',
1577                     $code_point;
1578     return qr/$string/;
1579 }
1580
1581 # Now test the cases where a legal code point is generated, but may or may not
1582 # be allowed/warned on.
1583 my @tests = (
1584      # ($testname, $bytes, $warn_flags, $disallow_flags, $expected_error_flags,
1585      #  $category, $allowed_uv, $expected_len, $needed_to_discern_len, $message )
1586     [ "lowest surrogate",
1587         (isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
1588         $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE, $UTF8_GOT_SURROGATE,
1589         'surrogate', 0xD800,
1590         (isASCII) ? 3 : 4,
1591         2,
1592         qr/surrogate/
1593     ],
1594     [ "a middle surrogate",
1595         (isASCII) ? "\xed\xa4\x8d" : I8_to_native("\xf1\xb6\xa8\xad"),
1596         $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE, $UTF8_GOT_SURROGATE,
1597         'surrogate', 0xD90D,
1598         (isASCII) ? 3 : 4,
1599         2,
1600         qr/surrogate/
1601     ],
1602     [ "highest surrogate",
1603         (isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
1604         $UTF8_WARN_SURROGATE, $UTF8_DISALLOW_SURROGATE, $UTF8_GOT_SURROGATE,
1605         'surrogate', 0xDFFF,
1606         (isASCII) ? 3 : 4,
1607         2,
1608         qr/surrogate/
1609     ],
1610     [ "first non_unicode",
1611         (isASCII) ? "\xf4\x90\x80\x80" : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
1612         $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER, $UTF8_GOT_SUPER,
1613         'non_unicode', 0x110000,
1614         (isASCII) ? 4 : 5,
1615         2,
1616         qr/(not Unicode|for a non-Unicode code point).* may not be portable/
1617     ],
1618     [ "non_unicode whose first byte tells that",
1619         (isASCII) ? "\xf5\x80\x80\x80" : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
1620         $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER, $UTF8_GOT_SUPER,
1621         'non_unicode',
1622         (isASCII) ? 0x140000 : 0x200000,
1623         (isASCII) ? 4 : 5,
1624         1,
1625         qr/(not Unicode|for a non-Unicode code point).* may not be portable/
1626     ],
1627     [ "first of 32 consecutive non-character code points",
1628         (isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
1629         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1630         'nonchar', 0xFDD0,
1631         (isASCII) ? 3 : 4,
1632         (isASCII) ? 3 : 4,
1633         qr/Unicode non-character.*is not recommended for open interchange/
1634     ],
1635     [ "a mid non-character code point of the 32 consecutive ones",
1636         (isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
1637         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1638         'nonchar', 0xFDE0,
1639         (isASCII) ? 3 : 4,
1640         (isASCII) ? 3 : 4,
1641         qr/Unicode non-character.*is not recommended for open interchange/
1642     ],
1643     [ "final of 32 consecutive non-character code points",
1644         (isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
1645         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1646         'nonchar', 0xFDEF,
1647         (isASCII) ? 3 : 4,
1648         (isASCII) ? 3 : 4,
1649         qr/Unicode non-character.*is not recommended for open interchange/
1650     ],
1651     [ "non-character code point U+FFFE",
1652         (isASCII) ? "\xef\xbf\xbe" : I8_to_native("\xf1\xbf\xbf\xbe"),
1653         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1654         'nonchar', 0xFFFE,
1655         (isASCII) ? 3 : 4,
1656         (isASCII) ? 3 : 4,
1657         qr/Unicode non-character.*is not recommended for open interchange/
1658     ],
1659     [ "non-character code point U+FFFF",
1660         (isASCII) ? "\xef\xbf\xbf" : I8_to_native("\xf1\xbf\xbf\xbf"),
1661         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1662         'nonchar', 0xFFFF,
1663         (isASCII) ? 3 : 4,
1664         (isASCII) ? 3 : 4,
1665         qr/Unicode non-character.*is not recommended for open interchange/
1666     ],
1667     [ "non-character code point U+1FFFE",
1668         (isASCII) ? "\xf0\x9f\xbf\xbe" : I8_to_native("\xf3\xbf\xbf\xbe"),
1669         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1670         'nonchar', 0x1FFFE,
1671         4, 4,
1672         qr/Unicode non-character.*is not recommended for open interchange/
1673     ],
1674     [ "non-character code point U+1FFFF",
1675         (isASCII) ? "\xf0\x9f\xbf\xbf" : I8_to_native("\xf3\xbf\xbf\xbf"),
1676         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1677         'nonchar', 0x1FFFF,
1678         4, 4,
1679         qr/Unicode non-character.*is not recommended for open interchange/
1680     ],
1681     [ "non-character code point U+2FFFE",
1682         (isASCII) ? "\xf0\xaf\xbf\xbe" : I8_to_native("\xf5\xbf\xbf\xbe"),
1683         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1684         'nonchar', 0x2FFFE,
1685         4, 4,
1686         qr/Unicode non-character.*is not recommended for open interchange/
1687     ],
1688     [ "non-character code point U+2FFFF",
1689         (isASCII) ? "\xf0\xaf\xbf\xbf" : I8_to_native("\xf5\xbf\xbf\xbf"),
1690         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1691         'nonchar', 0x2FFFF,
1692         4, 4,
1693         qr/Unicode non-character.*is not recommended for open interchange/
1694     ],
1695     [ "non-character code point U+3FFFE",
1696         (isASCII) ? "\xf0\xbf\xbf\xbe" : I8_to_native("\xf7\xbf\xbf\xbe"),
1697         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1698         'nonchar', 0x3FFFE,
1699         4, 4,
1700         qr/Unicode non-character.*is not recommended for open interchange/
1701     ],
1702     [ "non-character code point U+3FFFF",
1703         (isASCII) ? "\xf0\xbf\xbf\xbf" : I8_to_native("\xf7\xbf\xbf\xbf"),
1704         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1705         'nonchar', 0x3FFFF,
1706         4, 4,
1707         qr/Unicode non-character.*is not recommended for open interchange/
1708     ],
1709     [ "non-character code point U+4FFFE",
1710         (isASCII) ? "\xf1\x8f\xbf\xbe" : I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
1711         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1712         'nonchar', 0x4FFFE,
1713         (isASCII) ? 4 : 5,
1714         (isASCII) ? 4 : 5,
1715         qr/Unicode non-character.*is not recommended for open interchange/
1716     ],
1717     [ "non-character code point U+4FFFF",
1718         (isASCII) ? "\xf1\x8f\xbf\xbf" : I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
1719         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1720         'nonchar', 0x4FFFF,
1721         (isASCII) ? 4 : 5,
1722         (isASCII) ? 4 : 5,
1723         qr/Unicode non-character.*is not recommended for open interchange/
1724     ],
1725     [ "non-character code point U+5FFFE",
1726         (isASCII) ? "\xf1\x9f\xbf\xbe" : I8_to_native("\xf8\xab\xbf\xbf\xbe"),
1727         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1728         'nonchar', 0x5FFFE,
1729         (isASCII) ? 4 : 5,
1730         (isASCII) ? 4 : 5,
1731         qr/Unicode non-character.*is not recommended for open interchange/
1732     ],
1733     [ "non-character code point U+5FFFF",
1734         (isASCII) ? "\xf1\x9f\xbf\xbf" : I8_to_native("\xf8\xab\xbf\xbf\xbf"),
1735         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1736         'nonchar', 0x5FFFF,
1737         (isASCII) ? 4 : 5,
1738         (isASCII) ? 4 : 5,
1739         qr/Unicode non-character.*is not recommended for open interchange/
1740     ],
1741     [ "non-character code point U+6FFFE",
1742         (isASCII) ? "\xf1\xaf\xbf\xbe" : I8_to_native("\xf8\xad\xbf\xbf\xbe"),
1743         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1744         'nonchar', 0x6FFFE,
1745         (isASCII) ? 4 : 5,
1746         (isASCII) ? 4 : 5,
1747         qr/Unicode non-character.*is not recommended for open interchange/
1748     ],
1749     [ "non-character code point U+6FFFF",
1750         (isASCII) ? "\xf1\xaf\xbf\xbf" : I8_to_native("\xf8\xad\xbf\xbf\xbf"),
1751         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1752         'nonchar', 0x6FFFF,
1753         (isASCII) ? 4 : 5,
1754         (isASCII) ? 4 : 5,
1755         qr/Unicode non-character.*is not recommended for open interchange/
1756     ],
1757     [ "non-character code point U+7FFFE",
1758         (isASCII) ? "\xf1\xbf\xbf\xbe" : I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
1759         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1760         'nonchar', 0x7FFFE,
1761         (isASCII) ? 4 : 5,
1762         (isASCII) ? 4 : 5,
1763         qr/Unicode non-character.*is not recommended for open interchange/
1764     ],
1765     [ "non-character code point U+7FFFF",
1766         (isASCII) ? "\xf1\xbf\xbf\xbf" : I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
1767         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1768         'nonchar', 0x7FFFF,
1769         (isASCII) ? 4 : 5,
1770         (isASCII) ? 4 : 5,
1771         qr/Unicode non-character.*is not recommended for open interchange/
1772     ],
1773     [ "non-character code point U+8FFFE",
1774         (isASCII) ? "\xf2\x8f\xbf\xbe" : I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
1775         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1776         'nonchar', 0x8FFFE,
1777         (isASCII) ? 4 : 5,
1778         (isASCII) ? 4 : 5,
1779         qr/Unicode non-character.*is not recommended for open interchange/
1780     ],
1781     [ "non-character code point U+8FFFF",
1782         (isASCII) ? "\xf2\x8f\xbf\xbf" : I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
1783         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1784         'nonchar', 0x8FFFF,
1785         (isASCII) ? 4 : 5,
1786         (isASCII) ? 4 : 5,
1787         qr/Unicode non-character.*is not recommended for open interchange/
1788     ],
1789     [ "non-character code point U+9FFFE",
1790         (isASCII) ? "\xf2\x9f\xbf\xbe" : I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
1791         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1792         'nonchar', 0x9FFFE,
1793         (isASCII) ? 4 : 5,
1794         (isASCII) ? 4 : 5,
1795         qr/Unicode non-character.*is not recommended for open interchange/
1796     ],
1797     [ "non-character code point U+9FFFF",
1798         (isASCII) ? "\xf2\x9f\xbf\xbf" : I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
1799         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1800         'nonchar', 0x9FFFF,
1801         (isASCII) ? 4 : 5,
1802         (isASCII) ? 4 : 5,
1803         qr/Unicode non-character.*is not recommended for open interchange/
1804     ],
1805     [ "non-character code point U+AFFFE",
1806         (isASCII) ? "\xf2\xaf\xbf\xbe" : I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
1807         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1808         'nonchar', 0xAFFFE,
1809         (isASCII) ? 4 : 5,
1810         (isASCII) ? 4 : 5,
1811         qr/Unicode non-character.*is not recommended for open interchange/
1812     ],
1813     [ "non-character code point U+AFFFF",
1814         (isASCII) ? "\xf2\xaf\xbf\xbf" : I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
1815         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1816         'nonchar', 0xAFFFF,
1817         (isASCII) ? 4 : 5,
1818         (isASCII) ? 4 : 5,
1819         qr/Unicode non-character.*is not recommended for open interchange/
1820     ],
1821     [ "non-character code point U+BFFFE",
1822         (isASCII) ? "\xf2\xbf\xbf\xbe" : I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
1823         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1824         'nonchar', 0xBFFFE,
1825         (isASCII) ? 4 : 5,
1826         (isASCII) ? 4 : 5,
1827         qr/Unicode non-character.*is not recommended for open interchange/
1828     ],
1829     [ "non-character code point U+BFFFF",
1830         (isASCII) ? "\xf2\xbf\xbf\xbf" : I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
1831         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1832         'nonchar', 0xBFFFF,
1833         (isASCII) ? 4 : 5,
1834         (isASCII) ? 4 : 5,
1835         qr/Unicode non-character.*is not recommended for open interchange/
1836     ],
1837     [ "non-character code point U+CFFFE",
1838         (isASCII) ? "\xf3\x8f\xbf\xbe" : I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
1839         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1840         'nonchar', 0xCFFFE,
1841         (isASCII) ? 4 : 5,
1842         (isASCII) ? 4 : 5,
1843         qr/Unicode non-character.*is not recommended for open interchange/
1844     ],
1845     [ "non-character code point U+CFFFF",
1846         (isASCII) ? "\xf3\x8f\xbf\xbf" : I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
1847         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1848         'nonchar', 0xCFFFF,
1849         (isASCII) ? 4 : 5,
1850         (isASCII) ? 4 : 5,
1851         qr/Unicode non-character.*is not recommended for open interchange/
1852     ],
1853     [ "non-character code point U+DFFFE",
1854         (isASCII) ? "\xf3\x9f\xbf\xbe" : I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
1855         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1856         'nonchar', 0xDFFFE,
1857         (isASCII) ? 4 : 5,
1858         (isASCII) ? 4 : 5,
1859         qr/Unicode non-character.*is not recommended for open interchange/
1860     ],
1861     [ "non-character code point U+DFFFF",
1862         (isASCII) ? "\xf3\x9f\xbf\xbf" : I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
1863         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1864         'nonchar', 0xDFFFF,
1865         (isASCII) ? 4 : 5,
1866         (isASCII) ? 4 : 5,
1867         qr/Unicode non-character.*is not recommended for open interchange/
1868     ],
1869     [ "non-character code point U+EFFFE",
1870         (isASCII) ? "\xf3\xaf\xbf\xbe" : I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
1871         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1872         'nonchar', 0xEFFFE,
1873         (isASCII) ? 4 : 5,
1874         (isASCII) ? 4 : 5,
1875         qr/Unicode non-character.*is not recommended for open interchange/
1876     ],
1877     [ "non-character code point U+EFFFF",
1878         (isASCII) ? "\xf3\xaf\xbf\xbf" : I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
1879         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1880         'nonchar', 0xEFFFF,
1881         (isASCII) ? 4 : 5,
1882         (isASCII) ? 4 : 5,
1883         qr/Unicode non-character.*is not recommended for open interchange/
1884     ],
1885     [ "non-character code point U+FFFFE",
1886         (isASCII) ? "\xf3\xbf\xbf\xbe" : I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
1887         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1888         'nonchar', 0xFFFFE,
1889         (isASCII) ? 4 : 5,
1890         (isASCII) ? 4 : 5,
1891         qr/Unicode non-character.*is not recommended for open interchange/
1892     ],
1893     [ "non-character code point U+FFFFF",
1894         (isASCII) ? "\xf3\xbf\xbf\xbf" : I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
1895         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1896         'nonchar', 0xFFFFF,
1897         (isASCII) ? 4 : 5,
1898         (isASCII) ? 4 : 5,
1899         qr/Unicode non-character.*is not recommended for open interchange/
1900     ],
1901     [ "non-character code point U+10FFFE",
1902         (isASCII) ? "\xf4\x8f\xbf\xbe" : I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
1903         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1904         'nonchar', 0x10FFFE,
1905         (isASCII) ? 4 : 5,
1906         (isASCII) ? 4 : 5,
1907         qr/Unicode non-character.*is not recommended for open interchange/
1908     ],
1909     [ "non-character code point U+10FFFF",
1910         (isASCII) ? "\xf4\x8f\xbf\xbf" : I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
1911         $UTF8_WARN_NONCHAR, $UTF8_DISALLOW_NONCHAR, $UTF8_GOT_NONCHAR,
1912         'nonchar', 0x10FFFF,
1913         (isASCII) ? 4 : 5,
1914         (isASCII) ? 4 : 5,
1915         qr/Unicode non-character.*is not recommended for open interchange/
1916     ],
1917     [ "requires at least 32 bits",
1918         (isASCII)
1919          ?  "\xfe\x82\x80\x80\x80\x80\x80"
1920          : I8_to_native(
1921             "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
1922         # This code point is chosen so that it is representable in a UV on
1923         # 32-bit machines
1924         $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
1925         $UTF8_GOT_ABOVE_31_BIT,
1926         'utf8', 0x80000000,
1927         (isASCII) ? 7 : $max_bytes,
1928         (isASCII) ? 1 : 8,
1929         nonportable_regex(0x80000000)
1930     ],
1931     [ "highest 32 bit code point",
1932         (isASCII)
1933          ?  "\xfe\x83\xbf\xbf\xbf\xbf\xbf"
1934          : I8_to_native(
1935             "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
1936         $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
1937         $UTF8_GOT_ABOVE_31_BIT,
1938         'utf8', 0xFFFFFFFF,
1939         (isASCII) ? 7 : $max_bytes,
1940         (isASCII) ? 1 : 8,
1941         nonportable_regex(0xffffffff)
1942     ],
1943     [ "requires at least 32 bits, and use SUPER-type flags, instead of"
1944     . " ABOVE_31_BIT",
1945         (isASCII)
1946          ? "\xfe\x82\x80\x80\x80\x80\x80"
1947          : I8_to_native(
1948            "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
1949         $UTF8_WARN_SUPER, $UTF8_DISALLOW_SUPER, $UTF8_GOT_SUPER,
1950         'utf8', 0x80000000,
1951         (isASCII) ? 7 : $max_bytes,
1952         1,
1953         nonportable_regex(0x80000000)
1954     ],
1955     [ "overflow with warnings/disallow for more than 31 bits",
1956         # This tests the interaction of WARN_ABOVE_31_BIT/DISALLOW_ABOVE_31_BIT
1957         # with overflow.  The overflow malformation is never allowed, so
1958         # preventing it takes precedence if the ABOVE_31_BIT options would
1959         # otherwise allow in an overflowing value.  The ASCII code points (1
1960         # for 32-bits; 1 for 64) were chosen because the old overflow
1961         # detection algorithm did not catch them; this means this test also
1962         # checks for that fix.  The EBCDIC are arbitrary overflowing ones
1963         # since we have no reports of failures with it.
1964        (($is64bit)
1965         ? ((isASCII)
1966            ?    "\xff\x80\x90\x90\x90\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
1967            : I8_to_native(
1968                 "\xff\xB0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"))
1969         : ((isASCII)
1970            ?    "\xfe\x86\x80\x80\x80\x80\x80"
1971            : I8_to_native(
1972                 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"))),
1973         $UTF8_WARN_ABOVE_31_BIT,
1974         $UTF8_DISALLOW_ABOVE_31_BIT,
1975         $UTF8_GOT_ABOVE_31_BIT,
1976         'utf8', 0,
1977         (! isASCII || $is64bit) ? $max_bytes : 7,
1978         (isASCII || $is64bit) ? 2 : 8,
1979         qr/overflows/
1980     ],
1981 );
1982
1983 if (! $is64bit) {
1984     if (isASCII) {
1985         no warnings qw{portable overflow};
1986         push @tests,
1987             [ "Lowest 33 bit code point: overflow",
1988                 "\xFE\x84\x80\x80\x80\x80\x80",
1989                 $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
1990                 $UTF8_GOT_ABOVE_31_BIT,
1991                 'utf8', 0x100000000,
1992                 7, 1,
1993                 qr/and( is)? not portable/
1994             ];
1995     }
1996 }
1997 else {
1998     no warnings qw{portable overflow};
1999     push @tests,
2000         [ "More than 32 bits",
2001             (isASCII)
2002             ?       "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
2003             : I8_to_native(
2004                     "\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2005             $UTF8_WARN_ABOVE_31_BIT, $UTF8_DISALLOW_ABOVE_31_BIT,
2006             $UTF8_GOT_ABOVE_31_BIT,
2007             'utf8', 0x1000000000,
2008             $max_bytes, (isASCII) ? 1 : 7,
2009             qr/and( is)? not portable/
2010         ];
2011     if (! isASCII) {
2012         push @tests,   # These could falsely show wrongly in a naive
2013                        # implementation
2014             [ "requires at least 32 bits",
2015                 I8_to_native(
2016                     "\xff\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2017                 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2018                 $UTF8_GOT_ABOVE_31_BIT,
2019                 'utf8', 0x800000000,
2020                 $max_bytes, 7,
2021                 nonportable_regex(0x80000000)
2022             ],
2023             [ "requires at least 32 bits",
2024                 I8_to_native(
2025                     "\xff\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2026                 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2027                 $UTF8_GOT_ABOVE_31_BIT,
2028                 'utf8', 0x10000000000,
2029                 $max_bytes, 6,
2030                 nonportable_regex(0x10000000000)
2031             ],
2032             [ "requires at least 32 bits",
2033                 I8_to_native(
2034                     "\xff\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2035                 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2036                 $UTF8_GOT_ABOVE_31_BIT,
2037                 'utf8', 0x200000000000,
2038                 $max_bytes, 5,
2039                 nonportable_regex(0x20000000000)
2040             ],
2041             [ "requires at least 32 bits",
2042                 I8_to_native(
2043                     "\xff\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2044                 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2045                 $UTF8_GOT_ABOVE_31_BIT,
2046                 'utf8', 0x4000000000000,
2047                 $max_bytes, 4,
2048                 nonportable_regex(0x4000000000000)
2049             ],
2050             [ "requires at least 32 bits",
2051                 I8_to_native(
2052                     "\xff\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2053                 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2054                 $UTF8_GOT_ABOVE_31_BIT,
2055                 'utf8', 0x80000000000000,
2056                 $max_bytes, 3,
2057                 nonportable_regex(0x80000000000000)
2058             ],
2059             [ "requires at least 32 bits",
2060                 I8_to_native(
2061                     "\xff\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
2062                 $UTF8_WARN_ABOVE_31_BIT,$UTF8_DISALLOW_ABOVE_31_BIT,
2063                 $UTF8_GOT_ABOVE_31_BIT,
2064                 'utf8', 0x1000000000000000,
2065                 $max_bytes, 2,
2066                 nonportable_regex(0x1000000000000000)
2067             ];
2068     }
2069 }
2070
2071 foreach my $test (@tests) {
2072     my ($testname, $bytes, $warn_flags, $disallow_flags, $expected_error_flags,
2073         $category, $allowed_uv, $expected_len, $needed_to_discern_len, $message
2074        ) = @$test;
2075
2076     my $length = length $bytes;
2077     my $will_overflow = $testname =~ /overflow/ ? 'overflow' : "";
2078
2079     {
2080         use warnings;
2081         undef @warnings;
2082         my $ret = test_isUTF8_CHAR($bytes, $length);
2083         my $ret_flags = test_isUTF8_CHAR_flags($bytes, $length, 0);
2084         if ($will_overflow) {
2085             is($ret, 0, "isUTF8_CHAR() $testname: returns 0");
2086             is($ret_flags, 0, "isUTF8_CHAR_flags() $testname: returns 0");
2087         }
2088         else {
2089             is($ret, $length,
2090                "isUTF8_CHAR() $testname: returns expected length: $length");
2091             is($ret_flags, $length, "isUTF8_CHAR_flags(...,0) $testname:"
2092                                   . " returns expected length: $length");
2093         }
2094         is(scalar @warnings, 0,
2095                 "isUTF8_CHAR() and isUTF8_CHAR()_flags $testname: generated"
2096               . " no warnings")
2097           or output_warnings(@warnings);
2098
2099         undef @warnings;
2100         $ret = test_isSTRICT_UTF8_CHAR($bytes, $length);
2101         if ($will_overflow) {
2102             is($ret, 0, "isSTRICT_UTF8_CHAR() $testname: returns 0");
2103         }
2104         else {
2105             my $expected_ret = (   $testname =~ /surrogate|non-character/
2106                                 || $allowed_uv > 0x10FFFF)
2107                                ? 0
2108                                : $length;
2109             is($ret, $expected_ret, "isSTRICT_UTF8_CHAR() $testname: returns"
2110                                   . " expected length: $expected_ret");
2111             $ret = test_isUTF8_CHAR_flags($bytes, $length,
2112                                           $UTF8_DISALLOW_ILLEGAL_INTERCHANGE);
2113             is($ret, $expected_ret,
2114                             "isUTF8_CHAR_flags('DISALLOW_ILLEGAL_INTERCHANGE')"
2115                           . " acts like isSTRICT_UTF8_CHAR");
2116         }
2117         is(scalar @warnings, 0,
2118                 "isSTRICT_UTF8_CHAR() and isUTF8_CHAR_flags $testname:"
2119               . " generated no warnings")
2120           or output_warnings(@warnings);
2121
2122         undef @warnings;
2123         $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $length);
2124         if ($will_overflow) {
2125             is($ret, 0, "isC9_STRICT_UTF8_CHAR() $testname: returns 0");
2126         }
2127         else {
2128             my $expected_ret = (   $testname =~ /surrogate/
2129                                 || $allowed_uv > 0x10FFFF)
2130                                ? 0
2131                                : $length;
2132             is($ret, $expected_ret, "isC9_STRICT_UTF8_CHAR() $testname:"
2133                                    ." returns expected length: $expected_ret");
2134             $ret = test_isUTF8_CHAR_flags($bytes, $length,
2135                                           $UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE);
2136             is($ret, $expected_ret,
2137                           "isUTF8_CHAR_flags('DISALLOW_ILLEGAL_C9_INTERCHANGE')"
2138                         . " acts like isC9_STRICT_UTF8_CHAR");
2139         }
2140         is(scalar @warnings, 0,
2141                 "isC9_STRICT_UTF8_CHAR() and isUTF8_CHAR_flags $testname:"
2142               . " generated no warnings")
2143           or output_warnings(@warnings);
2144
2145         # Test partial character handling, for each byte not a full character
2146         for my $j (1.. $length - 1) {
2147
2148             # Skip the test for the interaction between overflow and above-31
2149             # bit.  It is really testing other things than the partial
2150             # character tests, for which other tests in this file are
2151             # sufficient
2152             last if $testname =~ /overflow/;
2153
2154             foreach my $disallow_flag (0, $disallow_flags) {
2155                 my $partial = substr($bytes, 0, $j);
2156                 my $ret_should_be;
2157                 my $comment;
2158                 if ($disallow_flag) {
2159                     $ret_should_be = 0;
2160                     $comment = "disallowed";
2161                     if ($j < $needed_to_discern_len) {
2162                         $ret_should_be = 1;
2163                         $comment .= ", but need $needed_to_discern_len bytes"
2164                                  .  " to discern:";
2165                     }
2166                 }
2167                 else {
2168                     $ret_should_be = 1;
2169                     $comment = "allowed";
2170                 }
2171
2172                 undef @warnings;
2173
2174                 $ret = test_is_utf8_valid_partial_char_flags($partial, $j,
2175                                                              $disallow_flag);
2176                 is($ret, $ret_should_be,
2177                                 "$testname: is_utf8_valid_partial_char_flags("
2178                                         . display_bytes($partial)
2179                                         . "), $comment: returns $ret_should_be");
2180                 is(scalar @warnings, 0,
2181                         "$testname: is_utf8_valid_partial_char_flags()"
2182                       . " generated no warnings")
2183                   or output_warnings(@warnings);
2184             }
2185         }
2186     }
2187
2188     # This is more complicated than the malformations tested earlier, as there
2189     # are several orthogonal variables involved.  We test all the subclasses
2190     # of utf8 warnings to verify they work with and without the utf8 class,
2191     # and don't have effects on other sublass warnings
2192     foreach my $warning ('utf8', 'surrogate', 'nonchar', 'non_unicode') {
2193       foreach my $warn_flag (0, $warn_flags) {
2194         foreach my $disallow_flag (0, $disallow_flags) {
2195           foreach my $do_warning (0, 1) {
2196
2197             # We try each of the above with various combinations of
2198             # malformations that can occur on the same input sequence.
2199             foreach my $short ("", "short") {
2200               foreach my $unexpected_noncont ("",
2201                                               "unexpected non-continuation")
2202               {
2203                 foreach my $overlong ("", "overlong") {
2204
2205                     # If we're already at the longest possible, we
2206                     # can't create an overlong (which would be longer)
2207                     # can't handle anything larger.
2208                     next if $overlong && $expected_len >= $max_bytes;
2209
2210                     my @malformations;
2211                     my @expected_errors;
2212                     push @malformations, $short if $short;
2213                     push @malformations, $unexpected_noncont
2214                                                       if $unexpected_noncont;
2215                     push @malformations, $overlong if $overlong;
2216
2217                     # The overflow malformation test in the input
2218                     # array is coerced into being treated like one of
2219                     # the others.
2220                     if ($will_overflow) {
2221                         push @malformations, 'overflow';
2222                         push @expected_errors, $UTF8_GOT_OVERFLOW;
2223                     }
2224
2225                     my $malformations_name = join "/", @malformations;
2226                     $malformations_name .= " malformation"
2227                                                 if $malformations_name;
2228                     $malformations_name .= "s" if @malformations > 1;
2229                     my $this_bytes = $bytes;
2230                     my $this_length = $length;
2231                     my $expected_uv = $allowed_uv;
2232                     my $this_expected_len = $expected_len;
2233                     my $this_needed_to_discern_len = $needed_to_discern_len;
2234                     if ($malformations_name) {
2235                         $expected_uv = 0;
2236
2237                         # Coerce the input into the desired
2238                         # malformation
2239                         if ($malformations_name =~ /overlong/) {
2240
2241                             # For an overlong, we convert the original
2242                             # start byte into a continuation byte with
2243                             # the same data bits as originally. ...
2244                             substr($this_bytes, 0, 1)
2245                                 = start_byte_to_cont(substr($this_bytes,
2246                                                             0, 1));
2247
2248                             # ... Then we prepend it with a known
2249                             # overlong sequence.  This should evaluate
2250                             # to the exact same code point as the
2251                             # original.
2252                             $this_bytes
2253                             = I8_to_native("\xff")
2254                             . (I8_to_native(chr $first_continuation)
2255                             x ( $max_bytes - 1 - length($this_bytes)))
2256                             . $this_bytes;
2257                             $this_length = length($this_bytes);
2258                             $this_needed_to_discern_len
2259                                  = $max_bytes - ($this_expected_len
2260                                                - $this_needed_to_discern_len);
2261                             $this_expected_len = $max_bytes;
2262                             push @expected_errors, $UTF8_GOT_LONG;
2263                         }
2264                         if ($malformations_name =~ /short/) {
2265
2266                             # Just tell the test to not look far
2267                             # enough into the input.
2268                             $this_length--;
2269                             $this_expected_len--;
2270                             push @expected_errors, $UTF8_GOT_SHORT;
2271                         }
2272                         if ($malformations_name
2273                                                 =~ /non-continuation/)
2274                         {
2275                             # Change the final continuation byte into
2276                             # a non one.
2277                             my $pos = ($short) ? -2 : -1;
2278                             substr($this_bytes, $pos, 1) = '?';
2279                             $this_expected_len--;
2280                             push @expected_errors,
2281                                             $UTF8_GOT_NON_CONTINUATION;
2282                         }
2283                     }
2284
2285                     my $eval_warn = $do_warning
2286                                 ? "use warnings '$warning'"
2287                                 : $warning eq "utf8"
2288                                     ? "no warnings 'utf8'"
2289                                     : ( "use warnings 'utf8';"
2290                                     . " no warnings '$warning'");
2291
2292                     # Is effectively disallowed if we've set up a
2293                     # malformation, even if the flag indicates it is
2294                     # allowed.  Fix up test name to indicate this as
2295                     # well
2296                     my $disallowed = $disallow_flag
2297                                 || $malformations_name;
2298                     my $this_name = "utf8n_to_uvchr_error() $testname: "
2299                                                 . (($disallow_flag)
2300                                                 ? 'disallowed'
2301                                                 : $disallowed
2302                                                     ? $disallowed
2303                                                     : 'allowed');
2304                     $this_name .= ", $eval_warn";
2305                     $this_name .= ", " . (($warn_flag)
2306                                         ? 'with warning flag'
2307                                         : 'no warning flag');
2308
2309                     undef @warnings;
2310                     my $ret_ref;
2311                     my $display_bytes = display_bytes($this_bytes);
2312                     my $call = "    Call was: $eval_warn; \$ret_ref"
2313                             . " = test_utf8n_to_uvchr_error("
2314                             . "'$display_bytes', $this_length,"
2315                             . "$warn_flag"
2316                             . "|$disallow_flag)";
2317                     my $eval_text =      "$eval_warn; \$ret_ref"
2318                             . " = test_utf8n_to_uvchr_error("
2319                             . "'$this_bytes',"
2320                             . " $this_length, $warn_flag"
2321                             . "|$disallow_flag)";
2322                     eval "$eval_text";
2323                     if (! ok ("$@ eq ''",
2324                         "$this_name: eval succeeded"))
2325                     {
2326                         diag "\$!='$!'; eval'd=\"$call\"";
2327                         next;
2328                     }
2329                     if ($disallowed) {
2330                         is($ret_ref->[0], 0, "$this_name: Returns 0")
2331                           or diag $call;
2332                     }
2333                     else {
2334                         is($ret_ref->[0], $expected_uv,
2335                                 "$this_name: Returns expected uv: "
2336                                 . sprintf("0x%04X", $expected_uv))
2337                           or diag $call;
2338                     }
2339                     is($ret_ref->[1], $this_expected_len,
2340                                         "$this_name: Returns expected length:"
2341                                       . " $this_expected_len")
2342                       or diag $call;
2343
2344                     my $errors = $ret_ref->[2];
2345
2346                     for (my $i = @expected_errors - 1; $i >= 0; $i--) {
2347                         if (ok($expected_errors[$i] & $errors,
2348                             "Expected and got error bit return"
2349                             . " for $malformations[$i] malformation"))
2350                         {
2351                             $errors &= ~$expected_errors[$i];
2352                         }
2353                         splice @expected_errors, $i, 1;
2354                     }
2355                     is(scalar @expected_errors, 0,
2356                             "Got all the expected malformation errors")
2357                       or diag Dumper \@expected_errors;
2358
2359                     if (   $this_expected_len >= $this_needed_to_discern_len
2360                         && ($warn_flag || $disallow_flag))
2361                     {
2362                         is($errors, $expected_error_flags,
2363                                 "Got the correct error flag")
2364                           or diag $call;
2365                     }
2366                     else {
2367                         is($errors, 0, "Got no other error flag");
2368                     }
2369
2370                     if (@malformations) {
2371                         if (! $do_warning && $warning eq 'utf8') {
2372                             goto no_warnings_expected;
2373                         }
2374
2375                         # Check that each malformation generates a
2376                         # warning, removing that warning if found
2377                     MALFORMATION:
2378                         foreach my $malformation (@malformations) {
2379                             foreach (my $i = 0; $i < @warnings; $i++) {
2380                                 if ($warnings[$i] =~ /$malformation/) {
2381                                     pass("Expected and got"
2382                                     . "'$malformation' warning");
2383                                     splice @warnings, $i, 1;
2384                                     next MALFORMATION;
2385                                 }
2386                             }
2387                             fail("Expected '$malformation' warning"
2388                             . " but didn't get it");
2389
2390                         }
2391                     }
2392
2393                     # Any overflow will override any super or above-31
2394                     # warnings.
2395                     goto no_warnings_expected
2396                                 if $will_overflow || $this_expected_len
2397                                         < $this_needed_to_discern_len;
2398
2399                     if (    ! $do_warning
2400                         && (   $warning eq 'utf8'
2401                             || $warning eq $category))
2402                     {
2403                         goto no_warnings_expected;
2404                     }
2405                     elsif ($warn_flag) {
2406                         if (is(scalar @warnings, 1,
2407                             "$this_name: Got a single warning "))
2408                         {
2409                             like($warnings[0], $message,
2410                                     "$this_name: Got expected warning")
2411                                 or diag $call;
2412                         }
2413                         else {
2414                             diag $call;
2415                             if (scalar @warnings) {
2416                                 output_warnings(@warnings);
2417                             }
2418                         }
2419                     }
2420                     else {
2421                     no_warnings_expected:
2422                         unless (is(scalar @warnings, 0,
2423                                 "$this_name: Got no warnings"))
2424                         {
2425                             diag $call;
2426                             output_warnings(@warnings);
2427                         }
2428                     }
2429
2430                     # Check CHECK_ONLY results when the input is
2431                     # disallowed.  Do this when actually disallowed,
2432                     # not just when the $disallow_flag is set
2433                     if ($disallowed) {
2434                         undef @warnings;
2435                         $ret_ref = test_utf8n_to_uvchr_error(
2436                                     $this_bytes, $this_length,
2437                                     $disallow_flag|$UTF8_CHECK_ONLY);
2438                         is($ret_ref->[0], 0,
2439                                         "$this_name, CHECK_ONLY: Returns 0")
2440                           or diag $call;
2441                         is($ret_ref->[1], -1,
2442                             "$this_name: CHECK_ONLY: returns -1 for length")
2443                           or diag $call;
2444                         if (! is(scalar @warnings, 0,
2445                             "$this_name, CHECK_ONLY: no warnings"
2446                         . " generated"))
2447                         {
2448                             diag $call;
2449                             output_warnings(@warnings);
2450                         }
2451                     }
2452
2453                     # Now repeat some of the above, but for
2454                     # uvchr_to_utf8_flags().  Since this comes from an
2455                     # existing code point, it hasn't overflowed, and
2456                     # isn't malformed.
2457                     next if @malformations;
2458
2459                     # The warning and disallow flags passed in are for
2460                     # utf8n_to_uvchr_error().  Convert them for
2461                     # uvchr_to_utf8_flags().
2462                     my $uvchr_warn_flag = 0;
2463                     my $uvchr_disallow_flag = 0;
2464                     if ($warn_flag) {
2465                         if ($warn_flag == $UTF8_WARN_SURROGATE) {
2466                             $uvchr_warn_flag = $UNICODE_WARN_SURROGATE
2467                         }
2468                         elsif ($warn_flag == $UTF8_WARN_NONCHAR) {
2469                             $uvchr_warn_flag = $UNICODE_WARN_NONCHAR
2470                         }
2471                         elsif ($warn_flag == $UTF8_WARN_SUPER) {
2472                             $uvchr_warn_flag = $UNICODE_WARN_SUPER
2473                         }
2474                         elsif ($warn_flag == $UTF8_WARN_ABOVE_31_BIT) {
2475                             $uvchr_warn_flag
2476                                         = $UNICODE_WARN_ABOVE_31_BIT;
2477                         }
2478                         else {
2479                             fail(sprintf "Unexpected warn flag: %x",
2480                                 $warn_flag);
2481                             next;
2482                         }
2483                     }
2484                     if ($disallow_flag) {
2485                         if ($disallow_flag == $UTF8_DISALLOW_SURROGATE)
2486                         {
2487                             $uvchr_disallow_flag
2488                                         = $UNICODE_DISALLOW_SURROGATE;
2489                         }
2490                         elsif ($disallow_flag == $UTF8_DISALLOW_NONCHAR)
2491                         {
2492                             $uvchr_disallow_flag
2493                                         = $UNICODE_DISALLOW_NONCHAR;
2494                         }
2495                         elsif ($disallow_flag == $UTF8_DISALLOW_SUPER) {
2496                             $uvchr_disallow_flag
2497                                         = $UNICODE_DISALLOW_SUPER;
2498                         }
2499                         elsif ($disallow_flag
2500                                         == $UTF8_DISALLOW_ABOVE_31_BIT)
2501                         {
2502                             $uvchr_disallow_flag =
2503                                         $UNICODE_DISALLOW_ABOVE_31_BIT;
2504                         }
2505                         else {
2506                             fail(sprintf "Unexpected disallow flag: %x",
2507                                 $disallow_flag);
2508                             next;
2509                         }
2510                     }
2511
2512                     $disallowed = $uvchr_disallow_flag;
2513
2514                     $this_name = "uvchr_to_utf8_flags() $testname: "
2515                                             . (($uvchr_disallow_flag)
2516                                                 ? 'disallowed'
2517                                                 : ($disallowed)
2518                                                 ? 'ABOVE_31_BIT allowed'
2519                                                 : 'allowed');
2520                     $this_name .= ", $eval_warn";
2521                     $this_name .= ", " . (($uvchr_warn_flag)
2522                                         ? 'with warning flag'
2523                                         : 'no warning flag');
2524
2525                     undef @warnings;
2526                     my $ret;
2527                     my $warn_flag = sprintf "0x%x", $uvchr_warn_flag;
2528                     my $disallow_flag = sprintf "0x%x",
2529                                                 $uvchr_disallow_flag;
2530                     $call = sprintf("    Call was: $eval_warn; \$ret"
2531                                 . " = test_uvchr_to_utf8_flags("
2532                                 . " 0x%x, $warn_flag|$disallow_flag)",
2533                                 $allowed_uv);
2534                     $eval_text = "$eval_warn; \$ret ="
2535                             . " test_uvchr_to_utf8_flags("
2536                             . "$allowed_uv, $warn_flag|"
2537                             . "$disallow_flag)";
2538                     eval "$eval_text";
2539                     if (! ok ("$@ eq ''", "$this_name: eval succeeded"))
2540                     {
2541                         diag "\$!='$!'; eval'd=\"$eval_text\"";
2542                         next;
2543                     }
2544                     if ($disallowed) {
2545                         is($ret, undef, "$this_name: Returns undef")
2546                           or diag $call;
2547                     }
2548                     else {
2549                         is($ret, $bytes, "$this_name: Returns expected string")
2550                           or diag $call;
2551                     }
2552                     if (! $do_warning
2553                         && ($warning eq 'utf8' || $warning eq $category))
2554                     {
2555                         if (!is(scalar @warnings, 0,
2556                                 "$this_name: No warnings generated"))
2557                         {
2558                             diag $call;
2559                             output_warnings(@warnings);
2560                         }
2561                     }
2562                     elsif (       $uvchr_warn_flag
2563                         && (   $warning eq 'utf8'
2564                             || $warning eq $category))
2565                     {
2566                         if (is(scalar @warnings, 1,
2567                             "$this_name: Got a single warning "))
2568                         {
2569                             like($warnings[0], $message,
2570                                     "$this_name: Got expected warning")
2571                                 or diag $call;
2572                         }
2573                         else {
2574                             diag $call;
2575                             output_warnings(@warnings)
2576                                                 if scalar @warnings;
2577                         }
2578                     }
2579                 }
2580               }
2581             }
2582           }
2583         }
2584       }
2585     }
2586 }
2587
2588 SKIP:
2589 {
2590     isASCII
2591       or skip "These tests probably break on non-ASCII", 1;
2592     my $simple = join "", "A" .. "J";
2593     my $utf_ch = "\x{7fffffff}";
2594     utf8::encode($utf_ch);
2595     my $utf_ch_len = length $utf_ch;
2596     note "utf_ch_len $utf_ch_len";
2597     my $utf = $utf_ch x 10;
2598     my $bad_start = substr($utf, 1);
2599     # $bad_end ends with a start byte and a single continuation
2600     my $bad_end = substr($utf, 0, length($utf)-$utf_ch_len+2);
2601
2602     # WARNING: all offsets are *byte* offsets
2603     my @hop_tests =
2604       (
2605        # string      s                off        expected         name
2606        [ $simple,    0,               5,         5,               "simple in range, forward" ],
2607        [ $simple,    10,              -5,        5,               "simple in range, backward" ],
2608        [ $simple,    5,               10,        10,              "simple out of range, forward" ],
2609        [ $simple,    5,               -10,       0,               "simple out of range, backward" ],
2610        [ $utf,       $utf_ch_len * 5, 5,         length($utf),    "utf in range, forward" ],
2611        [ $utf,       $utf_ch_len * 5, -5,        0,               "utf in range, backward" ],
2612        [ $utf,       $utf_ch_len * 5, 4,         $utf_ch_len * 9, "utf in range b, forward" ],
2613        [ $utf,       $utf_ch_len * 5, -4,        $utf_ch_len,     "utf in range b, backward" ],
2614        [ $utf,       $utf_ch_len * 5, 6,         length($utf),    "utf out of range, forward" ],
2615        [ $utf,       $utf_ch_len * 5, -6,        0,               "utf out of range, backward"  ],
2616        [ $bad_start, 0,               1,         1,               "bad start, forward 1 from 0" ],
2617        [ $bad_start, 0,               $utf_ch_len-1, $utf_ch_len-1, "bad start, forward ch_len-1 from 0" ],
2618        [ $bad_start, 0,               $utf_ch_len, $utf_ch_len*2-1, "bad start, forward ch_len from 0" ],
2619        [ $bad_start, $utf_ch_len-1,   -1,        0,                "bad start, back 1 from first start byte" ],
2620        [ $bad_start, $utf_ch_len-2,   -1,        0,                "bad start, back 1 from before first start byte" ],
2621        [ $bad_start, 0,               -1,        0,                "bad start, back 1 from 0" ],
2622        [ $bad_start, length $bad_start, -10,     0,                "bad start, back 10 from end" ],
2623        [ $bad_end,   0,               10,        length $bad_end, "bad end, forward 10 from 0" ],
2624        [ $bad_end,   length($bad_end)-1, 10,     length $bad_end, "bad end, forward 1 from end-1" ],
2625        );
2626
2627     for my $test (@hop_tests) {
2628         my ($str, $s_off, $off, $want, $name) = @$test;
2629         my $result = test_utf8_hop_safe($str, $s_off, $off);
2630         is($result, $want, "utf8_hop_safe: $name");
2631     }
2632 }
2633
2634 done_testing;