Forbid out of range Unicode code points.
[perl.git] / ext / XS-APItest / t / utf8.t
1 #!perl -w
2
3 use strict;
4 use Test::More;
5
6 # This file tests various functions and macros in the API related to UTF-8.
7
8 BEGIN {
9     use_ok('XS::APItest');
10     require 'charset_tools.pl';
11     require './t/utf8_setup.pl';
12 };
13
14 $|=1;
15
16 no warnings 'deprecated'; # Some of the below are above IV_MAX on 32 bit
17                           # machines, and that is tested elsewhere
18
19 use XS::APItest;
20
21 my $pound_sign = chr utf8::unicode_to_native(163);
22
23 # This  test file can't use byte_utf8a_to_utf8n() from t/charset_tools.pl
24 # because that uses the same functions we are testing here.  So UTF-EBCDIC
25 # strings are hard-coded as I8 strings in this file instead, and we use the
26 # translation functions to/from I8 from that file instead.
27
28 my $look_for_everything_utf8n_to
29                         = $::UTF8_DISALLOW_SURROGATE
30                         | $::UTF8_WARN_SURROGATE
31                         | $::UTF8_DISALLOW_NONCHAR
32                         | $::UTF8_WARN_NONCHAR
33                         | $::UTF8_DISALLOW_SUPER
34                         | $::UTF8_WARN_SUPER
35                         | $::UTF8_DISALLOW_ABOVE_31_BIT
36                         | $::UTF8_WARN_ABOVE_31_BIT;
37 my $look_for_everything_uvchr_to
38                         = $::UNICODE_DISALLOW_SURROGATE
39                         | $::UNICODE_WARN_SURROGATE
40                         | $::UNICODE_DISALLOW_NONCHAR
41                         | $::UNICODE_WARN_NONCHAR
42                         | $::UNICODE_DISALLOW_SUPER
43                         | $::UNICODE_WARN_SUPER
44                         | $::UNICODE_DISALLOW_ABOVE_31_BIT
45                         | $::UNICODE_WARN_ABOVE_31_BIT;
46
47 foreach ([0, '', '', 'empty'],
48          [0, 'N', 'N', '1 char'],
49          [1, 'NN', 'N', '1 char substring'],
50          [-2, 'Perl', 'Rules', 'different'],
51          [0, $pound_sign, $pound_sign, 'pound sign'],
52          [1, $pound_sign . 10, $pound_sign . 1,
53                                             '10 pounds is more than 1 pound'],
54          [1, $pound_sign . $pound_sign, $pound_sign,
55                                             '2 pound signs are more than 1'],
56          [-2, ' $!', " \x{1F42B}!", 'Camels are worth more than 1 dollar'],
57          [-1, '!', "!\x{1F42A}", 'Initial substrings match'],
58         ) {
59     my ($expect, $left, $right, $desc) = @$_;
60     my $copy = $right;
61     utf8::encode($copy);
62     is(bytes_cmp_utf8($left, $copy), $expect, $desc);
63     next if $right =~ tr/\0-\377//c;
64     utf8::encode($left);
65     is(bytes_cmp_utf8($right, $left), -$expect, "$desc reversed");
66 }
67
68 # The keys to this hash are Unicode code points, their values are the native
69 # UTF-8 representations of them.  The code points are chosen because they are
70 # "interesting" on either or both ASCII and EBCDIC platforms.  First we add
71 # boundaries where the number of bytes required to represent them increase, or
72 # are adjacent to problematic code points, so we want to make sure they aren't
73 # considered problematic.
74 my %code_points = (
75     0x0100     => (isASCII) ? "\xc4\x80" : I8_to_native("\xc8\xa0"),
76     0x0400 - 1 => (isASCII) ? "\xcf\xbf" : I8_to_native("\xdf\xbf"),
77     0x0400     => (isASCII) ? "\xd0\x80" : I8_to_native("\xe1\xa0\xa0"),
78     0x0800 - 1 => (isASCII) ? "\xdf\xbf"     : I8_to_native("\xe1\xbf\xbf"),
79     0x0800     => (isASCII) ? "\xe0\xa0\x80" : I8_to_native("\xe2\xa0\xa0"),
80     0x4000 - 1 => (isASCII) ? "\xe3\xbf\xbf" : I8_to_native("\xef\xbf\xbf"),
81     0x4000     => (isASCII) ? "\xe4\x80\x80" : I8_to_native("\xf0\xb0\xa0\xa0"),
82     0x8000 - 1 => (isASCII) ? "\xe7\xbf\xbf" : I8_to_native("\xf0\xbf\xbf\xbf"),
83
84     # First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
85     # as of this writing, considers potentially problematic on EBCDIC
86     0x8000     => (isASCII) ? "\xe8\x80\x80" : I8_to_native("\xf1\xa0\xa0\xa0"),
87
88     0xD000 - 1 => (isASCII) ? "\xec\xbf\xbf" : I8_to_native("\xf1\xb3\xbf\xbf"),
89
90     # First code point that the implementation of isUTF8_POSSIBLY_PROBLEMATIC,
91     # as of this writing, considers potentially problematic on ASCII
92     0xD000     => (isASCII) ? "\xed\x80\x80" : I8_to_native("\xf1\xb4\xa0\xa0"),
93
94     # Bracket the surrogates, and include several surrogates
95     0xD7FF     => (isASCII) ? "\xed\x9f\xbf" : I8_to_native("\xf1\xb5\xbf\xbf"),
96     0xD800     => (isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
97     0xDC00     => (isASCII) ? "\xed\xb0\x80" : I8_to_native("\xf1\xb7\xa0\xa0"),
98     0xDFFF     => (isASCII) ? "\xee\x80\x80" : I8_to_native("\xf1\xb8\xa0\xa0"),
99     0xDFFF     => (isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
100     0xE000     => (isASCII) ? "\xee\x80\x80" : I8_to_native("\xf1\xb8\xa0\xa0"),
101
102     # Include the 32 contiguous non characters, and surrounding code points
103     0xFDCF     => (isASCII) ? "\xef\xb7\x8f" : I8_to_native("\xf1\xbf\xae\xaf"),
104     0xFDD0     => (isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
105     0xFDD1     => (isASCII) ? "\xef\xb7\x91" : I8_to_native("\xf1\xbf\xae\xb1"),
106     0xFDD2     => (isASCII) ? "\xef\xb7\x92" : I8_to_native("\xf1\xbf\xae\xb2"),
107     0xFDD3     => (isASCII) ? "\xef\xb7\x93" : I8_to_native("\xf1\xbf\xae\xb3"),
108     0xFDD4     => (isASCII) ? "\xef\xb7\x94" : I8_to_native("\xf1\xbf\xae\xb4"),
109     0xFDD5     => (isASCII) ? "\xef\xb7\x95" : I8_to_native("\xf1\xbf\xae\xb5"),
110     0xFDD6     => (isASCII) ? "\xef\xb7\x96" : I8_to_native("\xf1\xbf\xae\xb6"),
111     0xFDD7     => (isASCII) ? "\xef\xb7\x97" : I8_to_native("\xf1\xbf\xae\xb7"),
112     0xFDD8     => (isASCII) ? "\xef\xb7\x98" : I8_to_native("\xf1\xbf\xae\xb8"),
113     0xFDD9     => (isASCII) ? "\xef\xb7\x99" : I8_to_native("\xf1\xbf\xae\xb9"),
114     0xFDDA     => (isASCII) ? "\xef\xb7\x9a" : I8_to_native("\xf1\xbf\xae\xba"),
115     0xFDDB     => (isASCII) ? "\xef\xb7\x9b" : I8_to_native("\xf1\xbf\xae\xbb"),
116     0xFDDC     => (isASCII) ? "\xef\xb7\x9c" : I8_to_native("\xf1\xbf\xae\xbc"),
117     0xFDDD     => (isASCII) ? "\xef\xb7\x9d" : I8_to_native("\xf1\xbf\xae\xbd"),
118     0xFDDE     => (isASCII) ? "\xef\xb7\x9e" : I8_to_native("\xf1\xbf\xae\xbe"),
119     0xFDDF     => (isASCII) ? "\xef\xb7\x9f" : I8_to_native("\xf1\xbf\xae\xbf"),
120     0xFDE0     => (isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
121     0xFDE1     => (isASCII) ? "\xef\xb7\xa1" : I8_to_native("\xf1\xbf\xaf\xa1"),
122     0xFDE2     => (isASCII) ? "\xef\xb7\xa2" : I8_to_native("\xf1\xbf\xaf\xa2"),
123     0xFDE3     => (isASCII) ? "\xef\xb7\xa3" : I8_to_native("\xf1\xbf\xaf\xa3"),
124     0xFDE4     => (isASCII) ? "\xef\xb7\xa4" : I8_to_native("\xf1\xbf\xaf\xa4"),
125     0xFDE5     => (isASCII) ? "\xef\xb7\xa5" : I8_to_native("\xf1\xbf\xaf\xa5"),
126     0xFDE6     => (isASCII) ? "\xef\xb7\xa6" : I8_to_native("\xf1\xbf\xaf\xa6"),
127     0xFDE7     => (isASCII) ? "\xef\xb7\xa7" : I8_to_native("\xf1\xbf\xaf\xa7"),
128     0xFDE8     => (isASCII) ? "\xef\xb7\xa8" : I8_to_native("\xf1\xbf\xaf\xa8"),
129     0xFDEa     => (isASCII) ? "\xef\xb7\x99" : I8_to_native("\xf1\xbf\xaf\xa9"),
130     0xFDEA     => (isASCII) ? "\xef\xb7\xaa" : I8_to_native("\xf1\xbf\xaf\xaa"),
131     0xFDEB     => (isASCII) ? "\xef\xb7\xab" : I8_to_native("\xf1\xbf\xaf\xab"),
132     0xFDEC     => (isASCII) ? "\xef\xb7\xac" : I8_to_native("\xf1\xbf\xaf\xac"),
133     0xFDED     => (isASCII) ? "\xef\xb7\xad" : I8_to_native("\xf1\xbf\xaf\xad"),
134     0xFDEE     => (isASCII) ? "\xef\xb7\xae" : I8_to_native("\xf1\xbf\xaf\xae"),
135     0xFDEF     => (isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
136     0xFDF0     => (isASCII) ? "\xef\xb7\xb0" : I8_to_native("\xf1\xbf\xaf\xb0"),
137
138     # Mostly around non-characters, but some are transitions to longer strings
139     0xFFFD     => (isASCII) ? "\xef\xbf\xbd" : I8_to_native("\xf1\xbf\xbf\xbd"),
140     0x10000 - 1 => (isASCII)
141                    ?              "\xef\xbf\xbf"
142                    : I8_to_native("\xf1\xbf\xbf\xbf"),
143     0x10000     => (isASCII)
144                    ?              "\xf0\x90\x80\x80"
145                    : I8_to_native("\xf2\xa0\xa0\xa0"),
146     0x1FFFD     => (isASCII)
147                    ?              "\xf0\x9f\xbf\xbd"
148                    : I8_to_native("\xf3\xbf\xbf\xbd"),
149     0x1FFFE     => (isASCII)
150                    ?              "\xf0\x9f\xbf\xbe"
151                    : I8_to_native("\xf3\xbf\xbf\xbe"),
152     0x1FFFF     => (isASCII)
153                    ?              "\xf0\x9f\xbf\xbf"
154                    : I8_to_native("\xf3\xbf\xbf\xbf"),
155     0x20000     => (isASCII)
156                    ?              "\xf0\xa0\x80\x80"
157                    : I8_to_native("\xf4\xa0\xa0\xa0"),
158     0x2FFFD     => (isASCII)
159                    ?              "\xf0\xaf\xbf\xbd"
160                    : I8_to_native("\xf5\xbf\xbf\xbd"),
161     0x2FFFE     => (isASCII)
162                    ?              "\xf0\xaf\xbf\xbe"
163                    : I8_to_native("\xf5\xbf\xbf\xbe"),
164     0x2FFFF     => (isASCII)
165                    ?              "\xf0\xaf\xbf\xbf"
166                    : I8_to_native("\xf5\xbf\xbf\xbf"),
167     0x30000     => (isASCII)
168                    ?              "\xf0\xb0\x80\x80"
169                    : I8_to_native("\xf6\xa0\xa0\xa0"),
170     0x3FFFD     => (isASCII)
171                    ?              "\xf0\xbf\xbf\xbd"
172                    : I8_to_native("\xf7\xbf\xbf\xbd"),
173     0x3FFFE     => (isASCII)
174                    ?              "\xf0\xbf\xbf\xbe"
175                    : I8_to_native("\xf7\xbf\xbf\xbe"),
176     0x40000 - 1 => (isASCII)
177                    ?              "\xf0\xbf\xbf\xbf"
178                    : I8_to_native("\xf7\xbf\xbf\xbf"),
179     0x40000     => (isASCII)
180                    ?              "\xf1\x80\x80\x80"
181                    : I8_to_native("\xf8\xa8\xa0\xa0\xa0"),
182     0x4FFFD     => (isASCII)
183                    ?              "\xf1\x8f\xbf\xbd"
184                    : I8_to_native("\xf8\xa9\xbf\xbf\xbd"),
185     0x4FFFE     => (isASCII)
186                    ?              "\xf1\x8f\xbf\xbe"
187                    : I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
188     0x4FFFF     => (isASCII)
189                    ?              "\xf1\x8f\xbf\xbf"
190                    : I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
191     0x50000     => (isASCII)
192                    ?              "\xf1\x90\x80\x80"
193                    : I8_to_native("\xf8\xaa\xa0\xa0\xa0"),
194     0x5FFFD     => (isASCII)
195                    ?              "\xf1\x9f\xbf\xbd"
196                    : I8_to_native("\xf8\xab\xbf\xbf\xbd"),
197     0x5FFFE     => (isASCII)
198                    ?              "\xf1\x9f\xbf\xbe"
199                    : I8_to_native("\xf8\xab\xbf\xbf\xbe"),
200     0x5FFFF     => (isASCII)
201                    ?              "\xf1\x9f\xbf\xbf"
202                    : I8_to_native("\xf8\xab\xbf\xbf\xbf"),
203     0x60000     => (isASCII)
204                    ?              "\xf1\xa0\x80\x80"
205                    : I8_to_native("\xf8\xac\xa0\xa0\xa0"),
206     0x6FFFD     => (isASCII)
207                    ?              "\xf1\xaf\xbf\xbd"
208                    : I8_to_native("\xf8\xad\xbf\xbf\xbd"),
209     0x6FFFE     => (isASCII)
210                    ?              "\xf1\xaf\xbf\xbe"
211                    : I8_to_native("\xf8\xad\xbf\xbf\xbe"),
212     0x6FFFF     => (isASCII)
213                    ?              "\xf1\xaf\xbf\xbf"
214                    : I8_to_native("\xf8\xad\xbf\xbf\xbf"),
215     0x70000     => (isASCII)
216                    ?              "\xf1\xb0\x80\x80"
217                    : I8_to_native("\xf8\xae\xa0\xa0\xa0"),
218     0x7FFFD     => (isASCII)
219                    ?              "\xf1\xbf\xbf\xbd"
220                    : I8_to_native("\xf8\xaf\xbf\xbf\xbd"),
221     0x7FFFE     => (isASCII)
222                    ?              "\xf1\xbf\xbf\xbe"
223                    : I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
224     0x7FFFF     => (isASCII)
225                    ?              "\xf1\xbf\xbf\xbf"
226                    : I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
227     0x80000     => (isASCII)
228                    ?              "\xf2\x80\x80\x80"
229                    : I8_to_native("\xf8\xb0\xa0\xa0\xa0"),
230     0x8FFFD     => (isASCII)
231                    ?              "\xf2\x8f\xbf\xbd"
232                    : I8_to_native("\xf8\xb1\xbf\xbf\xbd"),
233     0x8FFFE     => (isASCII)
234                    ?              "\xf2\x8f\xbf\xbe"
235                    : I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
236     0x8FFFF     => (isASCII)
237                    ?              "\xf2\x8f\xbf\xbf"
238                    : I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
239     0x90000     => (isASCII)
240                    ?              "\xf2\x90\x80\x80"
241                    : I8_to_native("\xf8\xb2\xa0\xa0\xa0"),
242     0x9FFFD     => (isASCII)
243                    ?              "\xf2\x9f\xbf\xbd"
244                    : I8_to_native("\xf8\xb3\xbf\xbf\xbd"),
245     0x9FFFE     => (isASCII)
246                    ?              "\xf2\x9f\xbf\xbe"
247                    : I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
248     0x9FFFF     => (isASCII)
249                    ?              "\xf2\x9f\xbf\xbf"
250                    : I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
251     0xA0000     => (isASCII)
252                    ?              "\xf2\xa0\x80\x80"
253                    : I8_to_native("\xf8\xb4\xa0\xa0\xa0"),
254     0xAFFFD     => (isASCII)
255                    ?              "\xf2\xaf\xbf\xbd"
256                    : I8_to_native("\xf8\xb5\xbf\xbf\xbd"),
257     0xAFFFE     => (isASCII)
258                    ?              "\xf2\xaf\xbf\xbe"
259                    : I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
260     0xAFFFF     => (isASCII)
261                    ?              "\xf2\xaf\xbf\xbf"
262                    : I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
263     0xB0000     => (isASCII)
264                    ?              "\xf2\xb0\x80\x80"
265                    : I8_to_native("\xf8\xb6\xa0\xa0\xa0"),
266     0xBFFFD     => (isASCII)
267                    ?              "\xf2\xbf\xbf\xbd"
268                    : I8_to_native("\xf8\xb7\xbf\xbf\xbd"),
269     0xBFFFE     => (isASCII)
270                    ?              "\xf2\xbf\xbf\xbe"
271                    : I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
272     0xBFFFF     => (isASCII)
273                    ?               "\xf2\xbf\xbf\xbf"
274                    : I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
275     0xC0000     => (isASCII)
276                    ?               "\xf3\x80\x80\x80"
277                    : I8_to_native("\xf8\xb8\xa0\xa0\xa0"),
278     0xCFFFD     => (isASCII)
279                    ?               "\xf3\x8f\xbf\xbd"
280                    : I8_to_native("\xf8\xb9\xbf\xbf\xbd"),
281     0xCFFFE     => (isASCII)
282                    ?               "\xf3\x8f\xbf\xbe"
283                    : I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
284     0xCFFFF     => (isASCII)
285                    ?               "\xf3\x8f\xbf\xbf"
286                    : I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
287     0xD0000     => (isASCII)
288                    ?               "\xf3\x90\x80\x80"
289                    : I8_to_native("\xf8\xba\xa0\xa0\xa0"),
290     0xDFFFD     => (isASCII)
291                    ?               "\xf3\x9f\xbf\xbd"
292                    : I8_to_native("\xf8\xbb\xbf\xbf\xbd"),
293     0xDFFFE     => (isASCII)
294                    ?               "\xf3\x9f\xbf\xbe"
295                    : I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
296     0xDFFFF     => (isASCII)
297                    ?               "\xf3\x9f\xbf\xbf"
298                    : I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
299     0xE0000     => (isASCII)
300                    ?               "\xf3\xa0\x80\x80"
301                    : I8_to_native("\xf8\xbc\xa0\xa0\xa0"),
302     0xEFFFD     => (isASCII)
303                    ?               "\xf3\xaf\xbf\xbd"
304                    : I8_to_native("\xf8\xbd\xbf\xbf\xbd"),
305     0xEFFFE     => (isASCII)
306                    ?               "\xf3\xaf\xbf\xbe"
307                    : I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
308     0xEFFFF     => (isASCII)
309                    ?               "\xf3\xaf\xbf\xbf"
310                    : I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
311     0xF0000     => (isASCII)
312                    ?               "\xf3\xb0\x80\x80"
313                    : I8_to_native("\xf8\xbe\xa0\xa0\xa0"),
314     0xFFFFD     => (isASCII)
315                    ?               "\xf3\xbf\xbf\xbd"
316                    : I8_to_native("\xf8\xbf\xbf\xbf\xbd"),
317     0xFFFFE     => (isASCII)
318                    ?               "\xf3\xbf\xbf\xbe"
319                    : I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
320     0xFFFFF     => (isASCII)
321                    ?               "\xf3\xbf\xbf\xbf"
322                    : I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
323     0x100000    => (isASCII)
324                    ?               "\xf4\x80\x80\x80"
325                    : I8_to_native("\xf9\xa0\xa0\xa0\xa0"),
326     0x10FFFD    => (isASCII)
327                    ?               "\xf4\x8f\xbf\xbd"
328                    : I8_to_native("\xf9\xa1\xbf\xbf\xbd"),
329     0x10FFFE    => (isASCII)
330                    ?               "\xf4\x8f\xbf\xbe"
331                    : I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
332     0x10FFFF    => (isASCII)
333                    ?               "\xf4\x8f\xbf\xbf"
334                    : I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
335     0x110000    => (isASCII)
336                    ?               "\xf4\x90\x80\x80"
337                    : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
338
339     # Things that would be noncharacters if they were in Unicode, and might be
340     # mistaken, if the C code is bad, to be nonchars
341     0x11FFFE    => (isASCII)
342                    ?               "\xf4\x9f\xbf\xbe"
343                     : I8_to_native("\xf9\xa3\xbf\xbf\xbe"),
344     0x11FFFF    => (isASCII)
345                    ?               "\xf4\x9f\xbf\xbf"
346                     : I8_to_native("\xf9\xa3\xbf\xbf\xbf"),
347     0x20FFFE    => (isASCII)
348                    ?               "\xf8\x88\x8f\xbf\xbe"
349                     : I8_to_native("\xfa\xa1\xbf\xbf\xbe"),
350     0x20FFFF    => (isASCII)
351                    ?               "\xf8\x88\x8f\xbf\xbf"
352                     : I8_to_native("\xfa\xa1\xbf\xbf\xbf"),
353
354     0x200000 - 1 => (isASCII)
355                     ?              "\xf7\xbf\xbf\xbf"
356                     : I8_to_native("\xf9\xbf\xbf\xbf\xbf"),
357     0x200000     => (isASCII)
358                     ?              "\xf8\x88\x80\x80\x80"
359                     : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
360     0x400000 - 1 => (isASCII)
361                     ?              "\xf8\x8f\xbf\xbf\xbf"
362                     : I8_to_native("\xfb\xbf\xbf\xbf\xbf"),
363     0x400000     => (isASCII)
364                     ?              "\xf8\x90\x80\x80\x80"
365                     : I8_to_native("\xfc\xa4\xa0\xa0\xa0\xa0"),
366     0x4000000 - 1 => (isASCII)
367                      ?              "\xfb\xbf\xbf\xbf\xbf"
368                      : I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
369     0x4000000     => (isASCII)
370                      ?              "\xfc\x84\x80\x80\x80\x80"
371                      : I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
372     0x4000000 - 1 => (isASCII)
373                      ?              "\xfb\xbf\xbf\xbf\xbf"
374                      : I8_to_native("\xfd\xbf\xbf\xbf\xbf\xbf"),
375     0x4000000     => (isASCII)
376                      ?              "\xfc\x84\x80\x80\x80\x80"
377                      : I8_to_native("\xfe\xa2\xa0\xa0\xa0\xa0\xa0"),
378     0x40000000 - 1 => (isASCII)
379                       ?              "\xfc\xbf\xbf\xbf\xbf\xbf"
380                       : I8_to_native("\xfe\xbf\xbf\xbf\xbf\xbf\xbf"),
381     0x40000000     =>
382     (isASCII) ?    "\xfd\x80\x80\x80\x80\x80"
383     : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0"),
384 );
385
386 if ($::is64bit) {
387     no warnings qw(overflow portable);
388
389     $code_points{0x80000000 - 1}
390      = (isASCII)
391      ?    "\xfd\xbf\xbf\xbf\xbf\xbf"
392      : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf"),
393     $code_points{0x80000000}
394      = (isASCII)
395      ?    "\xfe\x82\x80\x80\x80\x80\x80"
396      : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
397     $code_points{0xFFFFFFFF}
398      = (isASCII)
399      ?    "\xfe\x83\xbf\xbf\xbf\xbf\xbf"
400      : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
401     $code_points{0x100000000}
402      = (isASCII)
403      ?              "\xfe\x84\x80\x80\x80\x80\x80"
404      : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0");
405     $code_points{0x1000000000 - 1}
406      = (isASCII)
407      ?              "\xfe\xbf\xbf\xbf\xbf\xbf\xbf"
408      : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf\xbf");
409     $code_points{0x1000000000}
410      = (isASCII)
411      ?              "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80"
412      : I8_to_native("\xff\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0\xa0");
413
414     if (isASCII) {  # These could falsely show as overlongs in a naive
415                     # implementation
416         $code_points{0x40000000000}
417                       = "\xff\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80";
418         $code_points{0x1000000000000}
419                       = "\xff\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80";
420         $code_points{0x40000000000000}
421                       = "\xff\x80\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80";
422         $code_points{0x1000000000000000}
423                       = "\xff\x80\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
424         # overflows
425         #$code_points{0xfoo}
426         #           = "\xff\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
427     }
428 }
429 elsif (! isASCII) { # 32-bit EBCDIC.  64-bit is clearer to handle, so doesn't
430                     # need this test case
431     no warnings qw(overflow portable);
432     $code_points{0x40000000} = I8_to_native(
433                     "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0");
434 }
435
436 # Now add in entries for each of code points 0-255, which require special
437 # handling on EBCDIC.  Remember the keys are Unicode values, and the values
438 # are the native UTF-8.  For invariants, the bytes are just the native chr.
439
440 my $cp = 0;
441 while ($cp < ((isASCII) ? 128 : 160)) {   # This is from the definition of
442                                         # invariant
443     $code_points{$cp} = chr utf8::unicode_to_native($cp);
444     $cp++;
445 }
446
447 # Done with the invariants.  Now do the variants.  All in this range are 2
448 # byte.  Again, we can't use the internal functions to generate UTF-8, as
449 # those are what we are trying to test.  In the loop, we know what range the
450 # continuation bytes can be in, and what the lowest start byte can be.  So we
451 # cycle through them.
452
453 my $final_continuation = 0xBF;
454 my $start = (isASCII) ? 0xC2 : 0xC5;
455
456 my $continuation = $::first_continuation - 1;
457
458 while ($cp < 255) {
459     if (++$continuation > $final_continuation) {
460
461         # Wrap to the next start byte when we reach the final continuation
462         # byte possible
463         $continuation = $::first_continuation;
464         $start++;
465     }
466     $code_points{$cp} = I8_to_native(chr($start) . chr($continuation));
467
468     $cp++;
469 }
470
471 my @warnings;
472
473 use warnings 'utf8';
474 local $SIG{__WARN__} = sub { push @warnings, @_ };
475
476 my %restriction_types;
477
478 $restriction_types{""}{'valid_strings'} = "";
479 $restriction_types{"c9strict"}{'valid_strings'} = "";
480 $restriction_types{"strict"}{'valid_strings'} = "";
481 $restriction_types{"fits_in_31_bits"}{'valid_strings'} = "";
482
483 # This set of tests looks for basic sanity, and lastly tests various routines
484 # for the given code point.  If the earlier tests for that code point fail,
485 # the later ones probably will too.  Malformations are tested in later
486 # segments of code.
487 for my $u (sort { utf8::unicode_to_native($a) <=> utf8::unicode_to_native($b) }
488           keys %code_points)
489 {
490     my $hex_u = sprintf("0x%02X", $u);
491     my $n = utf8::unicode_to_native($u);
492     my $hex_n = sprintf("0x%02X", $n);
493     my $bytes = $code_points{$u};
494
495     my $offskip_should_be;
496     {
497         no warnings qw(overflow portable);
498         $offskip_should_be = (isASCII)
499             ? ( $u < 0x80           ? 1 :
500                 $u < 0x800          ? 2 :
501                 $u < 0x10000        ? 3 :
502                 $u < 0x200000       ? 4 :
503                 $u < 0x4000000      ? 5 :
504                 $u < 0x80000000     ? 6 : (($::is64bit)
505                                         ? ($u < 0x1000000000 ? 7 : $::max_bytes)
506                                         : 7)
507               )
508             : ($u < 0xA0        ? 1 :
509                $u < 0x400       ? 2 :
510                $u < 0x4000      ? 3 :
511                $u < 0x40000     ? 4 :
512                $u < 0x400000    ? 5 :
513                $u < 0x4000000   ? 6 :
514                $u < 0x40000000  ? 7 : $::max_bytes );
515     }
516
517     # If this test fails, subsequent ones are meaningless.
518     next unless is(test_OFFUNISKIP($u), $offskip_should_be,
519                    "Verify OFFUNISKIP($hex_u) is $offskip_should_be");
520     my $invariant = $offskip_should_be == 1;
521     my $display_invariant = $invariant || 0;
522     is(test_OFFUNI_IS_INVARIANT($u), $invariant,
523        "Verify OFFUNI_IS_INVARIANT($hex_u) is $display_invariant");
524
525     my $uvchr_skip_should_be = $offskip_should_be;
526     next unless is(test_UVCHR_SKIP($n), $uvchr_skip_should_be,
527                    "Verify UVCHR_SKIP($hex_n) is $uvchr_skip_should_be");
528     is(test_UVCHR_IS_INVARIANT($n), $offskip_should_be == 1,
529        "Verify UVCHR_IS_INVARIANT($hex_n) is $display_invariant");
530
531     my $n_chr = chr $n;
532     utf8::upgrade $n_chr;
533
534     is(test_UTF8_SKIP($n_chr), $uvchr_skip_should_be,
535         "Verify UTF8_SKIP(chr $hex_n) is $uvchr_skip_should_be");
536
537     use bytes;
538     my $byte_length = length $n_chr;
539     for (my $j = 0; $j < $byte_length; $j++) {
540         undef @warnings;
541
542         if ($j == $byte_length - 1) {
543             my $ret
544               = test_is_utf8_valid_partial_char_flags($n_chr, $byte_length, 0);
545             is($ret, 0, "   Verify is_utf8_valid_partial_char_flags("
546                       . display_bytes($n_chr)
547                       . ") returns 0 for full character");
548         }
549         else {
550             my $bytes_so_far = substr($n_chr, 0, $j + 1);
551             my $ret
552              = test_is_utf8_valid_partial_char_flags($bytes_so_far, $j + 1, 0);
553             is($ret, 1, "   Verify is_utf8_valid_partial_char_flags("
554                       . display_bytes($bytes_so_far)
555                       . ") returns 1");
556         }
557
558         is(scalar @warnings, 0, "   Verify is_utf8_valid_partial_char_flags"
559                               . " generated no warnings")
560           or output_warnings(@warnings);
561
562         my $b = substr($n_chr, $j, 1);
563         my $hex_b = sprintf("\"\\x%02x\"", ord $b);
564
565         my $byte_invariant = $j == 0 && $uvchr_skip_should_be == 1;
566         my $display_byte_invariant = $byte_invariant || 0;
567         next unless is(test_UTF8_IS_INVARIANT($b), $byte_invariant,
568                        "   Verify UTF8_IS_INVARIANT($hex_b) for byte $j "
569                      . "is $display_byte_invariant");
570
571         my $is_start = $j == 0 && $uvchr_skip_should_be > 1;
572         my $display_is_start = $is_start || 0;
573         next unless is(test_UTF8_IS_START($b), $is_start,
574                     "      Verify UTF8_IS_START($hex_b) is $display_is_start");
575
576         my $is_continuation = $j != 0 && $uvchr_skip_should_be > 1;
577         my $display_is_continuation = $is_continuation || 0;
578         next unless is(test_UTF8_IS_CONTINUATION($b), $is_continuation,
579                        "      Verify UTF8_IS_CONTINUATION($hex_b) is "
580                      . "$display_is_continuation");
581
582         my $is_continued = $uvchr_skip_should_be > 1;
583         my $display_is_continued = $is_continued || 0;
584         next unless is(test_UTF8_IS_CONTINUED($b), $is_continued,
585                        "      Verify UTF8_IS_CONTINUED($hex_b) is "
586                      . "$display_is_continued");
587
588         my $is_downgradeable_start =    $n < 256
589                                      && $uvchr_skip_should_be > 1
590                                      && $j == 0;
591         my $display_is_downgradeable_start = $is_downgradeable_start || 0;
592         next unless is(test_UTF8_IS_DOWNGRADEABLE_START($b),
593                        $is_downgradeable_start,
594                        "      Verify UTF8_IS_DOWNGRADEABLE_START($hex_b) is "
595                      . "$display_is_downgradeable_start");
596
597         my $is_above_latin1 =  $n > 255 && $j == 0;
598         my $display_is_above_latin1 = $is_above_latin1 || 0;
599         next unless is(test_UTF8_IS_ABOVE_LATIN1($b),
600                        $is_above_latin1,
601                        "      Verify UTF8_IS_ABOVE_LATIN1($hex_b) is "
602                      . "$display_is_above_latin1");
603
604         my $is_possibly_problematic =  $j == 0
605                                     && $n >= ((isASCII)
606                                               ? 0xD000
607                                               : 0x8000);
608         my $display_is_possibly_problematic = $is_possibly_problematic || 0;
609         next unless is(test_isUTF8_POSSIBLY_PROBLEMATIC($b),
610                        $is_possibly_problematic,
611                        "      Verify isUTF8_POSSIBLY_PROBLEMATIC($hex_b) is "
612                      . "$display_is_above_latin1");
613     }
614
615     # We are not trying to look for warnings, etc, so if they should occur, it
616     # is an error.  But some of the code points here do cause warnings, so we
617     # check here and turn off the ones that apply to such code points.  A
618     # later section of the code tests for these kinds of things.
619     my $this_utf8_flags = $look_for_everything_utf8n_to;
620     my $len = length $bytes;
621
622     my $valid_under_strict = 1;
623     my $valid_under_c9strict = 1;
624     my $valid_for_fits_in_31_bits = 1;
625     if ($n > 0x10FFFF) {
626         $this_utf8_flags &= ~($::UTF8_DISALLOW_SUPER|$::UTF8_WARN_SUPER);
627         $valid_under_strict = 0;
628         $valid_under_c9strict = 0;
629         if ($n > 2 ** 31 - 1) {
630             $this_utf8_flags &=
631                         ~($::UTF8_DISALLOW_ABOVE_31_BIT|$::UTF8_WARN_ABOVE_31_BIT);
632             $valid_for_fits_in_31_bits = 0;
633         }
634     }
635     elsif (($n >= 0xFDD0 && $n <= 0xFDEF) || ($n & 0xFFFE) == 0xFFFE) {
636         $this_utf8_flags &= ~($::UTF8_DISALLOW_NONCHAR|$::UTF8_WARN_NONCHAR);
637         $valid_under_strict = 0;
638     }
639     elsif ($n >= 0xD800 && $n <= 0xDFFF) {
640         $this_utf8_flags &= ~($::UTF8_DISALLOW_SURROGATE|$::UTF8_WARN_SURROGATE);
641         $valid_under_c9strict = 0;
642         $valid_under_strict = 0;
643     }
644
645     undef @warnings;
646
647     my $display_flags = sprintf "0x%x", $this_utf8_flags;
648     my $display_bytes = display_bytes($bytes);
649     my $ret_ref = test_utf8n_to_uvchr_error($bytes, $len, $this_utf8_flags);
650
651     # Rest of tests likely meaningless if it gets the wrong code point.
652     next unless is($ret_ref->[0], $n,
653                    "Verify utf8n_to_uvchr_error($display_bytes, $display_flags)"
654                  . "returns $hex_n");
655     is($ret_ref->[1], $len,
656        "Verify utf8n_to_uvchr_error() for $hex_n returns expected length:"
657      . " $len");
658
659     unless (is(scalar @warnings, 0,
660              "Verify utf8n_to_uvchr_error() for $hex_n generated no warnings"))
661     {
662         output_warnings(@warnings);
663     }
664     is($ret_ref->[2], 0,
665        "Verify utf8n_to_uvchr_error() returned no error bits");
666
667     undef @warnings;
668
669     my $ret = test_isUTF8_CHAR($bytes, $len);
670     is($ret, $len,
671             "Verify isUTF8_CHAR($display_bytes) returns expected length: $len");
672
673     unless (is(scalar @warnings, 0,
674                "Verify isUTF8_CHAR() for $hex_n generated no warnings"))
675     {
676         output_warnings(@warnings);
677     }
678
679     undef @warnings;
680
681     $ret = test_isUTF8_CHAR($bytes, $len - 1);
682     is($ret, 0,
683             "Verify isUTF8_CHAR() with too short length parameter returns 0");
684
685     is(scalar @warnings, 0, "Verify isUTF8_CHAR() generated no warnings")
686       or output_warnings(@warnings);
687
688     undef @warnings;
689
690     $ret = test_isUTF8_CHAR_flags($bytes, $len, 0);
691     is($ret, $len, "Verify isUTF8_CHAR_flags($display_bytes, 0)"
692                  . " returns expected length: $len");
693
694     is(scalar @warnings, 0,
695                "Verify isUTF8_CHAR_flags() for $hex_n generated no warnings")
696       or output_warnings(@warnings);
697
698     undef @warnings;
699
700     $ret = test_isUTF8_CHAR_flags($bytes, $len - 1, 0);
701     is($ret, 0,
702         "Verify isUTF8_CHAR_flags() with too short length parameter returns 0");
703
704     is(scalar @warnings, 0, "Verify isUTF8_CHAR_flags() generated no warnings")
705       or output_warnings(@warnings);
706
707     undef @warnings;
708
709     $ret = test_isSTRICT_UTF8_CHAR($bytes, $len);
710     my $expected_len = ($valid_under_strict) ? $len : 0;
711     is($ret, $expected_len, "Verify isSTRICT_UTF8_CHAR($display_bytes)"
712                           . " returns expected length: $expected_len");
713
714     is(scalar @warnings, 0,
715                "Verify isSTRICT_UTF8_CHAR() for $hex_n generated no warnings")
716       or output_warnings(@warnings);
717
718     undef @warnings;
719
720     $ret = test_isSTRICT_UTF8_CHAR($bytes, $len - 1);
721     is($ret, 0,
722        "Verify isSTRICT_UTF8_CHAR() with too short length parameter returns 0");
723
724     is(scalar @warnings, 0, "Verify isSTRICT_UTF8_CHAR() generated no warnings")
725       or output_warnings(@warnings);
726
727     undef @warnings;
728
729     $ret = test_isUTF8_CHAR_flags($bytes, $len,
730                                             $::UTF8_DISALLOW_ILLEGAL_INTERCHANGE);
731     is($ret, $expected_len,
732                     "Verify isUTF8_CHAR_flags('DISALLOW_ILLEGAL_INTERCHANGE')"
733                   . " acts like isSTRICT_UTF8_CHAR");
734
735     is(scalar @warnings, 0,
736                "Verify isUTF8_CHAR() for $hex_n generated no warnings")
737       or output_warnings(@warnings);
738
739     undef @warnings;
740
741     $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len);
742     $expected_len = ($valid_under_c9strict) ? $len : 0;
743     is($ret, $expected_len, "Verify isC9_STRICT_UTF8_CHAR($display_bytes)"
744                           . " returns expected length: $len");
745
746     is(scalar @warnings, 0,
747             "Verify isC9_STRICT_UTF8_CHAR() for $hex_n generated no warnings")
748       or output_warnings(@warnings);
749
750     undef @warnings;
751
752     $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len - 1);
753     is($ret, 0,
754     "Verify isC9_STRICT_UTF8_CHAR() with too short length parameter returns 0");
755
756     is(scalar @warnings, 0,
757                "Verify isC9_STRICT_UTF8_CHAR() generated no warnings")
758       or output_warnings(@warnings);
759
760     undef @warnings;
761
762     $ret = test_isUTF8_CHAR_flags($bytes, $len,
763                                         $::UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE);
764     is($ret, $expected_len,
765                    "Verify isUTF8_CHAR_flags('DISALLOW_ILLEGAL_C9_INTERCHANGE')"
766                   ." acts like isC9_STRICT_UTF8_CHAR");
767
768     is(scalar @warnings, 0,
769                "Verify isUTF8_CHAR() for $hex_n generated no warnings")
770       or output_warnings(@warnings);
771
772     undef @warnings;
773
774     $ret_ref = test_valid_utf8_to_uvchr($bytes);
775     is($ret_ref->[0], $n,
776                    "Verify valid_utf8_to_uvchr($display_bytes) returns $hex_n");
777     is($ret_ref->[1], $len,
778        "Verify valid_utf8_to_uvchr() for $hex_n returns expected length: $len");
779
780     is(scalar @warnings, 0,
781                "Verify valid_utf8_to_uvchr() for $hex_n generated no warnings")
782       or output_warnings(@warnings);
783
784     # Similarly for uvchr_to_utf8
785     my $this_uvchr_flags = $look_for_everything_uvchr_to;
786     if ($n > 2 ** 31 - 1) {
787         $this_uvchr_flags &=
788                 ~($::UNICODE_DISALLOW_ABOVE_31_BIT|$::UNICODE_WARN_ABOVE_31_BIT);
789     }
790     if ($n > 0x10FFFF) {
791         $this_uvchr_flags &= ~($::UNICODE_DISALLOW_SUPER|$::UNICODE_WARN_SUPER);
792     }
793     elsif (($n >= 0xFDD0 && $n <= 0xFDEF) || ($n & 0xFFFE) == 0xFFFE) {
794         $this_uvchr_flags &= ~($::UNICODE_DISALLOW_NONCHAR|$::UNICODE_WARN_NONCHAR);
795     }
796     elsif ($n >= 0xD800 && $n <= 0xDFFF) {
797         $this_uvchr_flags
798                      &= ~($::UNICODE_DISALLOW_SURROGATE|$::UNICODE_WARN_SURROGATE);
799     }
800     $display_flags = sprintf "0x%x", $this_uvchr_flags;
801
802     undef @warnings;
803
804     $ret = test_uvchr_to_utf8_flags($n, $this_uvchr_flags);
805     ok(defined $ret,
806         "Verify uvchr_to_utf8_flags($hex_n, $display_flags) returned success");
807     is($ret, $bytes,
808     "Verify uvchr_to_utf8_flags($hex_n, $display_flags) returns correct bytes");
809
810     is(scalar @warnings, 0,
811                 "Verify uvchr_to_utf8_flags($hex_n, $display_flags) for $hex_n"
812               . " generated no warnings")
813       or output_warnings(@warnings);
814
815     # Now append this code point to a string that we will test various
816     # versions of is_foo_utf8_string_bar on, and keep a count of how many code
817     # points are in it.  All the code points in this loop are valid in Perl's
818     # extended UTF-8, but some are not valid under various restrictions.  A
819     # string and count is kept separately that is entirely valid for each
820     # restriction.  And, for each restriction, we note the first occurrence in
821     # the unrestricted string where we find something not in the restricted
822     # string.
823     $restriction_types{""}{'valid_strings'} .= $bytes;
824     $restriction_types{""}{'valid_counts'}++;
825
826     if ($valid_under_c9strict) {
827         $restriction_types{"c9strict"}{'valid_strings'} .= $bytes;
828         $restriction_types{"c9strict"}{'valid_counts'}++;
829     }
830     elsif (! exists $restriction_types{"c9strict"}{'first_invalid_offset'}) {
831         $restriction_types{"c9strict"}{'first_invalid_offset'}
832                     = length $restriction_types{"c9strict"}{'valid_strings'};
833         $restriction_types{"c9strict"}{'first_invalid_count'}
834                             = $restriction_types{"c9strict"}{'valid_counts'};
835     }
836
837     if ($valid_under_strict) {
838         $restriction_types{"strict"}{'valid_strings'} .= $bytes;
839         $restriction_types{"strict"}{'valid_counts'}++;
840     }
841     elsif (! exists $restriction_types{"strict"}{'first_invalid_offset'}) {
842         $restriction_types{"strict"}{'first_invalid_offset'}
843                         = length $restriction_types{"strict"}{'valid_strings'};
844         $restriction_types{"strict"}{'first_invalid_count'}
845                                 = $restriction_types{"strict"}{'valid_counts'};
846     }
847
848     if ($valid_for_fits_in_31_bits) {
849         $restriction_types{"fits_in_31_bits"}{'valid_strings'} .= $bytes;
850         $restriction_types{"fits_in_31_bits"}{'valid_counts'}++;
851     }
852     elsif (! exists
853                 $restriction_types{"fits_in_31_bits"}{'first_invalid_offset'})
854     {
855         $restriction_types{"fits_in_31_bits"}{'first_invalid_offset'}
856                 = length $restriction_types{"fits_in_31_bits"}{'valid_strings'};
857         $restriction_types{"fits_in_31_bits"}{'first_invalid_count'}
858                         = $restriction_types{"fits_in_31_bits"}{'valid_counts'};
859     }
860 }
861
862 my $I8c = (isASCII) ? "\x80" : "\xa0";    # A continuation byte
863 my $cont_byte = I8_to_native($I8c);
864 my $p = (isASCII) ? "\xe1\x80" : I8_to_native("\xE4\xA0");  # partial
865
866 # The loop above tested the single or partial character functions/macros,
867 # while building up strings to test the string functions, which we do now.
868
869 for my $restriction (sort keys %restriction_types) {
870     use bytes;
871
872     for my $use_flags ("", "_flags") {
873
874         # For each restriction, we test it in both the is_foo_flags functions
875         # and the specially named foo function.  But not if there isn't such a
876         # specially named function.  Currently, this is the only tested
877         # restriction that doesn't have a specially named function
878         next if $use_flags eq "" && $restriction eq "fits_in_31_bits";
879
880         # Start building up the name of the function we will test.
881         my $base_name = "is_";
882
883         if (! $use_flags  && $restriction ne "") {
884             $base_name .= $restriction . "_";
885         }
886
887         # We test both "is_utf8_string_foo" and "is_fixed_width_buf" functions
888         foreach my $operand ('string', 'fixed_width_buf') {
889
890             # Currently, the only fixed_width_buf functions have the '_flags'
891             # suffix.
892             next if $operand eq 'fixed_width_buf' && $use_flags eq "";
893
894             my $name = "${base_name}utf8_$operand";
895
896             # We test each version of the function
897             for my $function ("_loclen", "_loc", "") {
898
899                 # We test each function against
900                 #   a) valid input
901                 #   b) invalid input created by appending an out-of-place
902                 #      continuation character to the valid string
903                 #   c) input created by appending a partial character.  This
904                 #      is valid in the 'fixed_width' functions, but invalid in
905                 #   the 'string' ones
906                 #   d) invalid input created by calling a function that is
907                 #      expecting a restricted form of the input using the string
908                 #      that's valid when unrestricted
909                 for my $error_type (0, $cont_byte, $p, $restriction) {
910                     #diag "restriction=$restriction, use_flags=$use_flags, function=$function, error_type=" . display_bytes($error_type);
911
912                     # If there is no restriction, the error type will be "",
913                     # which is redundant with 0.
914                     next if $error_type eq "";
915
916                     my $this_name = "$name$function$use_flags";
917                     my $bytes
918                             = $restriction_types{$restriction}{'valid_strings'};
919                     my $expected_offset = length $bytes;
920                     my $expected_count
921                             = $restriction_types{$restriction}{'valid_counts'};
922                     my $test_name_suffix = "";
923
924                     my $this_error_type = $error_type;
925                     if ($this_error_type) {
926
927                         # Appending a bare continuation byte or a partial
928                         # character doesn't change the character count or
929                         # offset.  But in the other cases, we have saved where
930                         # the failures should occur, so use those.  Appending
931                         # a continuation byte makes it invalid; appending a
932                         # partial character makes the 'string' form invalid,
933                         # but not the 'fixed_width_buf' form.
934                         if (   $this_error_type eq $cont_byte
935                             || $this_error_type eq $p)
936                         {
937                             $bytes .= $this_error_type;
938                             if ($this_error_type eq $cont_byte) {
939                                 $test_name_suffix
940                                             = " for an unexpected continuation";
941                             }
942                             else {
943                                 $test_name_suffix
944                                         = " if ends with a partial character";
945                                 $this_error_type
946                                         = 0 if $operand eq "fixed_width_buf";
947                             }
948                         }
949                         else {
950                             $test_name_suffix
951                                         = " if contains forbidden code points";
952                             if ($this_error_type eq "c9strict") {
953                                 $bytes = $restriction_types{""}{'valid_strings'};
954                                 $expected_offset
955                                  = $restriction_types{"c9strict"}
956                                                      {'first_invalid_offset'};
957                                 $expected_count
958                                   = $restriction_types{"c9strict"}
959                                                       {'first_invalid_count'};
960                             }
961                             elsif ($this_error_type eq "strict") {
962                                 $bytes = $restriction_types{""}{'valid_strings'};
963                                 $expected_offset
964                                   = $restriction_types{"strict"}
965                                                       {'first_invalid_offset'};
966                                 $expected_count
967                                   = $restriction_types{"strict"}
968                                                       {'first_invalid_count'};
969
970                             }
971                             elsif ($this_error_type eq "fits_in_31_bits") {
972                                 $bytes = $restriction_types{""}{'valid_strings'};
973                                 $expected_offset
974                                   = $restriction_types{"fits_in_31_bits"}
975                                                       {'first_invalid_offset'};
976                                 $expected_count
977                                     = $restriction_types{"fits_in_31_bits"}
978                                                         {'first_invalid_count'};
979                             }
980                             else {
981                                 fail("Internal test error: Unknown error type "
982                                 . "'$this_error_type'");
983                                 next;
984                             }
985                         }
986                     }
987
988                     my $length = length $bytes;
989                     my $ret_ref;
990
991                     my $test = "\$ret_ref = test_$this_name(\$bytes, $length";
992
993                     # If using the _flags functions, we have to figure out what
994                     # flags to pass.  This is done to match the restriction.
995                     if ($use_flags eq "_flags") {
996                         if (! $restriction) {
997                             $test .= ", 0";     # The flag
998
999                             # Indicate the kind of flag in the test name.
1000                             $this_name .= "(0)";
1001                         }
1002                         else {
1003                             $this_name .= "($restriction)";
1004                             if ($restriction eq "c9strict") {
1005                                 $test
1006                                   .= ", $::UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE";
1007                             }
1008                             elsif ($restriction eq "strict") {
1009                                 $test .= ", $::UTF8_DISALLOW_ILLEGAL_INTERCHANGE";
1010                             }
1011                             elsif ($restriction eq "fits_in_31_bits") {
1012                                 $test .= ", $::UTF8_DISALLOW_ABOVE_31_BIT";
1013                             }
1014                             else {
1015                                 fail("Internal test error: Unknown restriction "
1016                                 . "'$restriction'");
1017                                 next;
1018                             }
1019                         }
1020                     }
1021                     $test .= ")";
1022
1023                     # Actually run the test
1024                     eval $test;
1025                     if ($@) {
1026                         fail($test);
1027                         diag $@;
1028                         next;
1029                     }
1030
1031                     my $ret;
1032                     my $error_offset;
1033                     my $cp_count;
1034
1035                     if ($function eq "") {
1036                         $ret = $ret_ref;    # For plain function, there's only a
1037                                             # single return value
1038                     }
1039                     else {  # Otherwise, the multiple values come in an array.
1040                         $ret = shift @$ret_ref ;
1041                         $error_offset = shift @$ret_ref;
1042                         $cp_count = shift@$ret_ref if $function eq "_loclen";
1043                     }
1044
1045                     if ($this_error_type) {
1046                         is($ret, 0,
1047                            "Verify $this_name is FALSE$test_name_suffix");
1048                     }
1049                     else {
1050                         unless(is($ret, 1,
1051                                   "Verify $this_name is TRUE for valid input"
1052                                 . "$test_name_suffix"))
1053                         {
1054                             diag("    The bytes starting at offset"
1055                                . " $error_offset are"
1056                                . display_bytes(substr(
1057                                           $restriction_types{$restriction}
1058                                                             {'valid_strings'},
1059                                           $error_offset)));
1060                             next;
1061                         }
1062                     }
1063
1064                     if ($function ne "") {
1065                         unless (is($error_offset, $expected_offset,
1066                                    "\tAnd returns the correct offset"))
1067                         {
1068                             my $min = ($error_offset < $expected_offset)
1069                                     ? $error_offset
1070                                     : $expected_offset;
1071                             diag("    The bytes starting at offset" . $min
1072                               . " are " . display_bytes(substr($bytes, $min)));
1073                         }
1074
1075                         if ($function eq '_loclen') {
1076                             is($cp_count, $expected_count,
1077                                "\tAnd returns the correct character count");
1078                         }
1079                     }
1080                 }
1081             }
1082         }
1083     }
1084 }
1085
1086 SKIP:
1087 {
1088     isASCII
1089       or skip "These tests probably break on non-ASCII", 1;
1090     my $simple = join "", "A" .. "J";
1091     my $utf_ch = "\x{7fffffff}";
1092     utf8::encode($utf_ch);
1093     my $utf_ch_len = length $utf_ch;
1094     note "utf_ch_len $utf_ch_len";
1095     my $utf = $utf_ch x 10;
1096     my $bad_start = substr($utf, 1);
1097     # $bad_end ends with a start byte and a single continuation
1098     my $bad_end = substr($utf, 0, length($utf)-$utf_ch_len+2);
1099
1100     # WARNING: all offsets are *byte* offsets
1101     my @hop_tests =
1102       (
1103        # string      s                off        expected         name
1104        [ $simple,    0,               5,         5,               "simple in range, forward" ],
1105        [ $simple,    10,              -5,        5,               "simple in range, backward" ],
1106        [ $simple,    5,               10,        10,              "simple out of range, forward" ],
1107        [ $simple,    5,               -10,       0,               "simple out of range, backward" ],
1108        [ $utf,       $utf_ch_len * 5, 5,         length($utf),    "utf in range, forward" ],
1109        [ $utf,       $utf_ch_len * 5, -5,        0,               "utf in range, backward" ],
1110        [ $utf,       $utf_ch_len * 5, 4,         $utf_ch_len * 9, "utf in range b, forward" ],
1111        [ $utf,       $utf_ch_len * 5, -4,        $utf_ch_len,     "utf in range b, backward" ],
1112        [ $utf,       $utf_ch_len * 5, 6,         length($utf),    "utf out of range, forward" ],
1113        [ $utf,       $utf_ch_len * 5, -6,        0,               "utf out of range, backward"  ],
1114        [ $bad_start, 0,               1,         1,               "bad start, forward 1 from 0" ],
1115        [ $bad_start, 0,               $utf_ch_len-1, $utf_ch_len-1, "bad start, forward ch_len-1 from 0" ],
1116        [ $bad_start, 0,               $utf_ch_len, $utf_ch_len*2-1, "bad start, forward ch_len from 0" ],
1117        [ $bad_start, $utf_ch_len-1,   -1,        0,                "bad start, back 1 from first start byte" ],
1118        [ $bad_start, $utf_ch_len-2,   -1,        0,                "bad start, back 1 from before first start byte" ],
1119        [ $bad_start, 0,               -1,        0,                "bad start, back 1 from 0" ],
1120        [ $bad_start, length $bad_start, -10,     0,                "bad start, back 10 from end" ],
1121        [ $bad_end,   0,               10,        length $bad_end, "bad end, forward 10 from 0" ],
1122        [ $bad_end,   length($bad_end)-1, 10,     length $bad_end, "bad end, forward 1 from end-1" ],
1123        );
1124
1125     for my $test (@hop_tests) {
1126         my ($str, $s_off, $off, $want, $name) = @$test;
1127         my $result = test_utf8_hop_safe($str, $s_off, $off);
1128         is($result, $want, "utf8_hop_safe: $name");
1129     }
1130 }
1131
1132 done_testing;