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
CommitLineData
fed3ba5d
NC
1#!perl -w
2
3use strict;
4use Test::More;
5d4414ef 5
6aa905cf
KW
6# This file tests various functions and macros in the API related to UTF-8.
7
5d4414ef
KW
8BEGIN {
9 use_ok('XS::APItest');
10 require 'charset_tools.pl';
6aa905cf 11 require './t/utf8_setup.pl';
5d4414ef
KW
12};
13
7dfd8446 14$|=1;
fed3ba5d 15
760c7c2f
KW
16no warnings 'deprecated'; # Some of the below are above IV_MAX on 32 bit
17 # machines, and that is tested elsewhere
18
fed3ba5d 19use XS::APItest;
4deba822 20
6aa905cf 21my $pound_sign = chr utf8::unicode_to_native(163);
d84e92aa 22
7dfd8446
KW
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
5d4414ef
KW
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.
418080dc 27
046d01eb 28my $look_for_everything_utf8n_to
6aa905cf
KW
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;
046d01eb 37my $look_for_everything_uvchr_to
6aa905cf
KW
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;
7dfd8446 46
fed3ba5d
NC
47foreach ([0, '', '', 'empty'],
48 [0, 'N', 'N', '1 char'],
49 [1, 'NN', 'N', '1 char substring'],
50 [-2, 'Perl', 'Rules', 'different'],
4deba822 51 [0, $pound_sign, $pound_sign, 'pound sign'],
2956ebbf
KW
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'],
fed3ba5d
NC
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
7dfd8446
KW
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.
74my %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
5f8a3d1d 94 # Bracket the surrogates, and include several surrogates
2956ebbf
KW
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"),
2956ebbf
KW
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"),
7dfd8446 100
5f8a3d1d 101 # Include the 32 contiguous non characters, and surrounding code points
2956ebbf
KW
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"),
7dfd8446 136
5f8a3d1d 137 # Mostly around non-characters, but some are transitions to longer strings
2956ebbf
KW
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"),
7dfd8446
KW
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
2956ebbf
KW
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"),
76513bdc
KW
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"),
7dfd8446 392);
4deba822 393
6aa905cf 394if ($::is64bit) {
7dfd8446 395 no warnings qw(overflow portable);
2956ebbf
KW
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");
76513bdc
KW
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");
2956ebbf
KW
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";
d566bd20 422 # overflows
2956ebbf
KW
423 #$code_points{0xfoo}
424 # = "\xff\x81\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80";
d566bd20
KW
425 }
426}
2956ebbf
KW
427elsif (! isASCII) { # 32-bit EBCDIC. 64-bit is clearer to handle, so doesn't
428 # need this test case
d566bd20 429 no warnings qw(overflow portable);
2956ebbf
KW
430 $code_points{0x40000000} = I8_to_native(
431 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0");
7dfd8446 432}
eb83ed87 433
7dfd8446
KW
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
438my $cp = 0;
439while ($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
dbb8d798 451my $highest_continuation = 0xBF;
7dfd8446
KW
452my $start = (isASCII) ? 0xC2 : 0xC5;
453
dbb8d798 454my $continuation = $::lowest_continuation - 1;
7dfd8446
KW
455
456while ($cp < 255) {
dbb8d798 457 if (++$continuation > $highest_continuation) {
7dfd8446
KW
458
459 # Wrap to the next start byte when we reach the final continuation
460 # byte possible
dbb8d798 461 $continuation = $::lowest_continuation;
7dfd8446
KW
462 $start++;
463 }
464 $code_points{$cp} = I8_to_native(chr($start) . chr($continuation));
465
466 $cp++;
467}
eb83ed87
KW
468
469my @warnings;
470
471use warnings 'utf8';
472local $SIG{__WARN__} = sub { push @warnings, @_ };
473
9f2abfde
KW
474my %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
7dfd8446
KW
484# segments of code.
485for 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 :
6aa905cf
KW
502 $u < 0x80000000 ? 6 : (($::is64bit)
503 ? ($u < 0x1000000000 ? 7 : $::max_bytes)
7dfd8446
KW
504 : 7)
505 )
506 : ($u < 0xA0 ? 1 :
507 $u < 0x400 ? 2 :
508 $u < 0x4000 ? 3 :
509 $u < 0x40000 ? 4 :
510 $u < 0x400000 ? 5 :
c0236afe 511 $u < 0x4000000 ? 6 :
6aa905cf 512 $u < 0x40000000 ? 7 : $::max_bytes );
7dfd8446
KW
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;
3d56ecbe
KW
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) {
2956ebbf
KW
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");
3d56ecbe
KW
546 }
547 else {
548 my $bytes_so_far = substr($n_chr, 0, $j + 1);
2956ebbf
KW
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");
3d56ecbe
KW
554 }
555
6a0eaf3d
KW
556 is(scalar @warnings, 0, " Verify is_utf8_valid_partial_char_flags"
557 . " generated no warnings")
558 or output_warnings(@warnings);
3d56ecbe 559
7dfd8446
KW
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.
046d01eb 617 my $this_utf8_flags = $look_for_everything_utf8n_to;
7dfd8446 618 my $len = length $bytes;
e23e8bc1
KW
619
620 my $valid_under_strict = 1;
a82be82b 621 my $valid_under_c9strict = 1;
9f2abfde 622 my $valid_for_fits_in_31_bits = 1;
7dfd8446 623 if ($n > 0x10FFFF) {
6aa905cf 624 $this_utf8_flags &= ~($::UTF8_DISALLOW_SUPER|$::UTF8_WARN_SUPER);
e23e8bc1 625 $valid_under_strict = 0;
a82be82b 626 $valid_under_c9strict = 0;
5f8a3d1d
KW
627 if ($n > 2 ** 31 - 1) {
628 $this_utf8_flags &=
6aa905cf 629 ~($::UTF8_DISALLOW_ABOVE_31_BIT|$::UTF8_WARN_ABOVE_31_BIT);
9f2abfde 630 $valid_for_fits_in_31_bits = 0;
5f8a3d1d 631 }
7dfd8446 632 }
5f8a3d1d 633 elsif (($n >= 0xFDD0 && $n <= 0xFDEF) || ($n & 0xFFFE) == 0xFFFE) {
6aa905cf 634 $this_utf8_flags &= ~($::UTF8_DISALLOW_NONCHAR|$::UTF8_WARN_NONCHAR);
e23e8bc1 635 $valid_under_strict = 0;
7dfd8446 636 }
5f8a3d1d 637 elsif ($n >= 0xD800 && $n <= 0xDFFF) {
6aa905cf 638 $this_utf8_flags &= ~($::UTF8_DISALLOW_SURROGATE|$::UTF8_WARN_SURROGATE);
5f8a3d1d
KW
639 $valid_under_c9strict = 0;
640 $valid_under_strict = 0;
641 }
7dfd8446
KW
642
643 undef @warnings;
644
645 my $display_flags = sprintf "0x%x", $this_utf8_flags;
7dfd8446 646 my $display_bytes = display_bytes($bytes);
f9380377 647 my $ret_ref = test_utf8n_to_uvchr_error($bytes, $len, $this_utf8_flags);
c0d8738e
KW
648
649 # Rest of tests likely meaningless if it gets the wrong code point.
650 next unless is($ret_ref->[0], $n,
f9380377
KW
651 "Verify utf8n_to_uvchr_error($display_bytes, $display_flags)"
652 . "returns $hex_n");
c0d8738e 653 is($ret_ref->[1], $len,
f9380377
KW
654 "Verify utf8n_to_uvchr_error() for $hex_n returns expected length:"
655 . " $len");
7dfd8446
KW
656
657 unless (is(scalar @warnings, 0,
f9380377 658 "Verify utf8n_to_uvchr_error() for $hex_n generated no warnings"))
7dfd8446 659 {
d84e92aa 660 output_warnings(@warnings);
7dfd8446 661 }
f9380377
KW
662 is($ret_ref->[2], 0,
663 "Verify utf8n_to_uvchr_error() returned no error bits");
046d01eb 664
75ffa578
KW
665 undef @warnings;
666
d7874298 667 my $ret = test_isUTF8_CHAR($bytes, $len);
2956ebbf
KW
668 is($ret, $len,
669 "Verify isUTF8_CHAR($display_bytes) returns expected length: $len");
d7874298
KW
670
671 unless (is(scalar @warnings, 0,
672 "Verify isUTF8_CHAR() for $hex_n generated no warnings"))
673 {
d84e92aa 674 output_warnings(@warnings);
d7874298
KW
675 }
676
677 undef @warnings;
678
679 $ret = test_isUTF8_CHAR($bytes, $len - 1);
2956ebbf
KW
680 is($ret, 0,
681 "Verify isUTF8_CHAR() with too short length parameter returns 0");
d7874298 682
6a0eaf3d
KW
683 is(scalar @warnings, 0, "Verify isUTF8_CHAR() generated no warnings")
684 or output_warnings(@warnings);
d7874298
KW
685
686 undef @warnings;
687
25e3a4e0 688 $ret = test_isUTF8_CHAR_flags($bytes, $len, 0);
2956ebbf
KW
689 is($ret, $len, "Verify isUTF8_CHAR_flags($display_bytes, 0)"
690 . " returns expected length: $len");
25e3a4e0 691
6a0eaf3d
KW
692 is(scalar @warnings, 0,
693 "Verify isUTF8_CHAR_flags() for $hex_n generated no warnings")
694 or output_warnings(@warnings);
25e3a4e0
KW
695
696 undef @warnings;
697
698 $ret = test_isUTF8_CHAR_flags($bytes, $len - 1, 0);
2956ebbf
KW
699 is($ret, 0,
700 "Verify isUTF8_CHAR_flags() with too short length parameter returns 0");
25e3a4e0 701
6a0eaf3d
KW
702 is(scalar @warnings, 0, "Verify isUTF8_CHAR_flags() generated no warnings")
703 or output_warnings(@warnings);
25e3a4e0
KW
704
705 undef @warnings;
706
e23e8bc1
KW
707 $ret = test_isSTRICT_UTF8_CHAR($bytes, $len);
708 my $expected_len = ($valid_under_strict) ? $len : 0;
2956ebbf
KW
709 is($ret, $expected_len, "Verify isSTRICT_UTF8_CHAR($display_bytes)"
710 . " returns expected length: $expected_len");
e23e8bc1 711
6a0eaf3d
KW
712 is(scalar @warnings, 0,
713 "Verify isSTRICT_UTF8_CHAR() for $hex_n generated no warnings")
714 or output_warnings(@warnings);
e23e8bc1
KW
715
716 undef @warnings;
717
718 $ret = test_isSTRICT_UTF8_CHAR($bytes, $len - 1);
2956ebbf
KW
719 is($ret, 0,
720 "Verify isSTRICT_UTF8_CHAR() with too short length parameter returns 0");
e23e8bc1 721
6a0eaf3d
KW
722 is(scalar @warnings, 0, "Verify isSTRICT_UTF8_CHAR() generated no warnings")
723 or output_warnings(@warnings);
e23e8bc1 724
25e3a4e0
KW
725 undef @warnings;
726
2956ebbf 727 $ret = test_isUTF8_CHAR_flags($bytes, $len,
6aa905cf 728 $::UTF8_DISALLOW_ILLEGAL_INTERCHANGE);
2956ebbf
KW
729 is($ret, $expected_len,
730 "Verify isUTF8_CHAR_flags('DISALLOW_ILLEGAL_INTERCHANGE')"
731 . " acts like isSTRICT_UTF8_CHAR");
25e3a4e0 732
6a0eaf3d
KW
733 is(scalar @warnings, 0,
734 "Verify isUTF8_CHAR() for $hex_n generated no warnings")
735 or output_warnings(@warnings);
25e3a4e0
KW
736
737 undef @warnings;
738
a82be82b
KW
739 $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len);
740 $expected_len = ($valid_under_c9strict) ? $len : 0;
2956ebbf
KW
741 is($ret, $expected_len, "Verify isC9_STRICT_UTF8_CHAR($display_bytes)"
742 . " returns expected length: $len");
a82be82b 743
6a0eaf3d
KW
744 is(scalar @warnings, 0,
745 "Verify isC9_STRICT_UTF8_CHAR() for $hex_n generated no warnings")
746 or output_warnings(@warnings);
a82be82b
KW
747
748 undef @warnings;
749
750 $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $len - 1);
2956ebbf
KW
751 is($ret, 0,
752 "Verify isC9_STRICT_UTF8_CHAR() with too short length parameter returns 0");
a82be82b 753
6a0eaf3d
KW
754 is(scalar @warnings, 0,
755 "Verify isC9_STRICT_UTF8_CHAR() generated no warnings")
756 or output_warnings(@warnings);
a82be82b 757
e23e8bc1
KW
758 undef @warnings;
759
2956ebbf 760 $ret = test_isUTF8_CHAR_flags($bytes, $len,
6aa905cf 761 $::UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE);
2956ebbf
KW
762 is($ret, $expected_len,
763 "Verify isUTF8_CHAR_flags('DISALLOW_ILLEGAL_C9_INTERCHANGE')"
764 ." acts like isC9_STRICT_UTF8_CHAR");
25e3a4e0 765
6a0eaf3d
KW
766 is(scalar @warnings, 0,
767 "Verify isUTF8_CHAR() for $hex_n generated no warnings")
768 or output_warnings(@warnings);
25e3a4e0
KW
769
770 undef @warnings;
771
75ffa578 772 $ret_ref = test_valid_utf8_to_uvchr($bytes);
2956ebbf
KW
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");
75ffa578 777
6a0eaf3d
KW
778 is(scalar @warnings, 0,
779 "Verify valid_utf8_to_uvchr() for $hex_n generated no warnings")
780 or output_warnings(@warnings);
75ffa578 781
046d01eb
KW
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 &=
6aa905cf 786 ~($::UNICODE_DISALLOW_ABOVE_31_BIT|$::UNICODE_WARN_ABOVE_31_BIT);
046d01eb
KW
787 }
788 if ($n > 0x10FFFF) {
6aa905cf 789 $this_uvchr_flags &= ~($::UNICODE_DISALLOW_SUPER|$::UNICODE_WARN_SUPER);
046d01eb 790 }
5f8a3d1d 791 elsif (($n >= 0xFDD0 && $n <= 0xFDEF) || ($n & 0xFFFE) == 0xFFFE) {
6aa905cf 792 $this_uvchr_flags &= ~($::UNICODE_DISALLOW_NONCHAR|$::UNICODE_WARN_NONCHAR);
046d01eb 793 }
5f8a3d1d 794 elsif ($n >= 0xD800 && $n <= 0xDFFF) {
2956ebbf 795 $this_uvchr_flags
6aa905cf 796 &= ~($::UNICODE_DISALLOW_SURROGATE|$::UNICODE_WARN_SURROGATE);
5f8a3d1d 797 }
046d01eb
KW
798 $display_flags = sprintf "0x%x", $this_uvchr_flags;
799
800 undef @warnings;
801
d7874298 802 $ret = test_uvchr_to_utf8_flags($n, $this_uvchr_flags);
2956ebbf
KW
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");
046d01eb 807
6a0eaf3d
KW
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);
9f2abfde
KW
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
860my $I8c = (isASCII) ? "\x80" : "\xa0"; # A continuation byte
861my $cont_byte = I8_to_native($I8c);
862my $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
867for 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.
8bc127bf 879 my $base_name = "is_";
9f2abfde
KW
880
881 if (! $use_flags && $restriction ne "") {
8bc127bf 882 $base_name .= $restriction . "_";
9f2abfde 883 }
8bc127bf
KW
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";
9f2abfde
KW
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
8bc127bf
KW
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
9f2abfde
KW
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
8bc127bf
KW
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.
2956ebbf
KW
932 if ( $this_error_type eq $cont_byte
933 || $this_error_type eq $p)
934 {
9f2abfde
KW
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";
8bc127bf
KW
943 $this_error_type
944 = 0 if $operand eq "fixed_width_buf";
9f2abfde
KW
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
6aa905cf 1004 .= ", $::UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE";
9f2abfde
KW
1005 }
1006 elsif ($restriction eq "strict") {
6aa905cf 1007 $test .= ", $::UTF8_DISALLOW_ILLEGAL_INTERCHANGE";
9f2abfde
KW
1008 }
1009 elsif ($restriction eq "fits_in_31_bits") {
6aa905cf 1010 $test .= ", $::UTF8_DISALLOW_ABOVE_31_BIT";
9f2abfde
KW
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 {
5d03eac4 1052 diag(" The bytes starting at offset"
9f2abfde
KW
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;
5d03eac4
KW
1069 diag(" The bytes starting at offset" . $min
1070 . " are " . display_bytes(substr($bytes, $min)));
9f2abfde
KW
1071 }
1072
1073 if ($function eq '_loclen') {
1074 is($cp_count, $expected_count,
1075 "\tAnd returns the correct character count");
1076 }
1077 }
1078 }
1079 }
8bc127bf 1080 }
9f2abfde 1081 }
7dfd8446
KW
1082}
1083
65df57a8
TC
1084SKIP:
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
fed3ba5d 1130done_testing;