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