This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
APItest:t/utf8_warn_base.pl; Fix 'ok' tests
[perl5.git] / ext / XS-APItest / t / utf8_warn_base.pl
CommitLineData
6aa905cf
KW
1#!perl -w
2
3# This is a base file to be used by various .t's in its directory
3f055917
KW
4# It tests various malformed UTF-8 sequences and some code points that are
5# "problematic", and verifies that the correct warnings/flags etc are
6# generated when using them. For the code points, it also takes the UTF-8 and
7# perturbs it to be malformed in various ways, and tests that this gets
8# appropriately detected.
6aa905cf
KW
9
10use strict;
11use Test::More;
12
13BEGIN {
14 use_ok('XS::APItest');
15 require 'charset_tools.pl';
16 require './t/utf8_setup.pl';
17};
18
19$|=1;
20
6aa905cf 21use XS::APItest;
6aa905cf 22
9cdc3054 23my @warnings_gotten;
6aa905cf
KW
24
25use warnings 'utf8';
8d6f1506
KW
26local $SIG{__WARN__} = sub { my @copy = @_;
27 push @warnings_gotten, map { chomp; $_ } @copy;
28 };
6d736463 29
57ff5f59 30my $highest_non_extended_utf8_cp = (isASCII) ? 0x7FFFFFFF : 0x3FFFFFFF;
9e35eec9
KW
31my $native_lowest_continuation_chr = I8_to_native(chr $::lowest_continuation);
32
08e73697
KW
33# C5 is chosen as it is valid for both ASCII and EBCDIC platforms
34my $known_start_byte = I8_to_native("\xC5");
35
6d736463
KW
36sub requires_extended_utf8($) {
37
38 # Returns a boolean as to whether or not the code point parameter fits
39 # into 31 bits, subject to the convention that a negative code point
40 # stands for one that overflows the word size, so won't fit in 31 bits.
41
57ff5f59 42 return shift > $highest_non_extended_utf8_cp;
6d736463
KW
43}
44
3f055917
KW
45sub is_extended_utf8($) {
46
47 # Returns a boolean as to whether or not the input UTF-8 sequence uses
48 # Perl extended UTF-8.
49
50 my $byte = substr(shift, 0, 1);
51 return ord $byte >= 0xFE if isASCII;
52 return $byte == I8_to_native("\xFF");
53}
54
404a1403
KW
55sub overflow_discern_len($) {
56
8e0327af
KW
57 # Returns how many bytes are needed to tell if a non-overlong UTF-8
58 # sequence is for a code point that won't fit in the platform's word size.
59 # Only the length of the sequence representing a single code point is
60 # needed.
404a1403
KW
61
62 if (isASCII) {
d22ec717
KW
63 return ($::is64bit) ? 3 : 1;
64
65 # Below is needed for code points above IV_MAX
66 #return ($::is64bit) ? 3 : ((shift == $::max_bytes)
67 # ? 1
68 # : 2);
404a1403
KW
69 }
70
71 return ($::is64bit) ? 2 : 8;
72}
73
8e0327af
KW
74sub overlong_discern_len($) {
75
76 # Returns how many bytes are needed to tell if the input UTF-8 sequence
77 # for a code point is overlong
78
79 my $string = shift;
80 my $length = length $string;
81 my $byte = ord native_to_I8(substr($string, 0, 1));
82 if (isASCII) {
d22ec717
KW
83 return ($byte >= 0xFE)
84 ? ((! $::is64bit)
85 ? 1
86 : ($byte == 0xFF) ? 7 : 2)
8e0327af 87 : (($length == 2) ? 1 : 2);
d22ec717
KW
88 # Below is needed for code points above IV_MAX
89 #return ($length == $::max_bytes)
90 # # This is constrained to 1 on 32-bit machines, as it
91 # # overflows there
92 # ? (($::is64bit) ? 7 : 1)
93 # : (($length == 2) ? 1 : 2);
8e0327af
KW
94 }
95
96 return ($length == $::max_bytes) ? 8 : (($length <= 3) ? 1 : 2);
97}
98
c7f9e4bb
KW
99my @tests;
100{
101 no warnings qw(portable overflow);
102 @tests = (
8f79178b
KW
103 # $testname,
104 # $bytes, UTF-8 string
105 # $allowed_uv, code point $bytes evaluates to; -1 if
106 # overflows
107 # $needed_to_discern_len optional, how long an initial substring do
108 # we need to tell that the string must be for
109 # a code point in the category it falls in,
110 # like being a surrogate; 0 indicates we need
111 # the whole string. Some categories have a
112 # default that is used if this is omitted.
1d21b5e7
KW
113 [ "orphan continuation byte malformation",
114 I8_to_native("$::I8c"),
115 0xFFFD,
116 1,
117 ],
3f055917
KW
118 [ "overlong malformation, lowest 2-byte",
119 (isASCII) ? "\xc0\x80" : I8_to_native("\xc0\xa0"),
120 0, # NUL
121 ],
122 [ "overlong malformation, highest 2-byte",
123 (isASCII) ? "\xc1\xbf" : I8_to_native("\xc4\xbf"),
d819dc50 124 (isASCII) ? 0x7F : 0xFF,
3f055917
KW
125 ],
126 [ "overlong malformation, lowest 3-byte",
127 (isASCII) ? "\xe0\x80\x80" : I8_to_native("\xe0\xa0\xa0"),
128 0, # NUL
129 ],
130 [ "overlong malformation, highest 3-byte",
131 (isASCII) ? "\xe0\x9f\xbf" : I8_to_native("\xe0\xbf\xbf"),
132 (isASCII) ? 0x7FF : 0x3FF,
133 ],
8f79178b
KW
134 [ "lowest surrogate",
135 (isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
136 0xD800,
137 ],
138 [ "a middle surrogate",
139 (isASCII) ? "\xed\xa4\x8d" : I8_to_native("\xf1\xb6\xa8\xad"),
140 0xD90D,
141 ],
142 [ "highest surrogate",
143 (isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
144 0xDFFF,
145 ],
8f79178b
KW
146 [ "first of 32 consecutive non-character code points",
147 (isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
148 0xFDD0,
149 ],
150 [ "a mid non-character code point of the 32 consecutive ones",
151 (isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
152 0xFDE0,
153 ],
154 [ "final of 32 consecutive non-character code points",
155 (isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
156 0xFDEF,
157 ],
158 [ "non-character code point U+FFFE",
159 (isASCII) ? "\xef\xbf\xbe" : I8_to_native("\xf1\xbf\xbf\xbe"),
160 0xFFFE,
161 ],
162 [ "non-character code point U+FFFF",
163 (isASCII) ? "\xef\xbf\xbf" : I8_to_native("\xf1\xbf\xbf\xbf"),
164 0xFFFF,
165 ],
3f055917
KW
166 [ "overlong malformation, lowest 4-byte",
167 (isASCII) ? "\xf0\x80\x80\x80" : I8_to_native("\xf0\xa0\xa0\xa0"),
168 0, # NUL
169 ],
170 [ "overlong malformation, highest 4-byte",
171 (isASCII) ? "\xf0\x8F\xbf\xbf" : I8_to_native("\xf0\xaf\xbf\xbf"),
172 (isASCII) ? 0xFFFF : 0x3FFF,
173 ],
8f79178b
KW
174 [ "non-character code point U+1FFFE",
175 (isASCII) ? "\xf0\x9f\xbf\xbe" : I8_to_native("\xf3\xbf\xbf\xbe"),
176 0x1FFFE,
177 ],
178 [ "non-character code point U+1FFFF",
179 (isASCII) ? "\xf0\x9f\xbf\xbf" : I8_to_native("\xf3\xbf\xbf\xbf"),
180 0x1FFFF,
181 ],
182 [ "non-character code point U+2FFFE",
183 (isASCII) ? "\xf0\xaf\xbf\xbe" : I8_to_native("\xf5\xbf\xbf\xbe"),
184 0x2FFFE,
185 ],
186 [ "non-character code point U+2FFFF",
187 (isASCII) ? "\xf0\xaf\xbf\xbf" : I8_to_native("\xf5\xbf\xbf\xbf"),
188 0x2FFFF,
189 ],
190 [ "non-character code point U+3FFFE",
191 (isASCII) ? "\xf0\xbf\xbf\xbe" : I8_to_native("\xf7\xbf\xbf\xbe"),
192 0x3FFFE,
193 ],
194 [ "non-character code point U+3FFFF",
195 (isASCII) ? "\xf0\xbf\xbf\xbf" : I8_to_native("\xf7\xbf\xbf\xbf"),
196 0x3FFFF,
197 ],
198 [ "non-character code point U+4FFFE",
199 (isASCII)
200 ? "\xf1\x8f\xbf\xbe"
201 : I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
202 0x4FFFE,
203 ],
204 [ "non-character code point U+4FFFF",
205 (isASCII)
206 ? "\xf1\x8f\xbf\xbf"
207 : I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
208 0x4FFFF,
209 ],
210 [ "non-character code point U+5FFFE",
211 (isASCII)
212 ? "\xf1\x9f\xbf\xbe"
213 : I8_to_native("\xf8\xab\xbf\xbf\xbe"),
214 0x5FFFE,
215 ],
216 [ "non-character code point U+5FFFF",
217 (isASCII)
218 ? "\xf1\x9f\xbf\xbf"
219 : I8_to_native("\xf8\xab\xbf\xbf\xbf"),
220 0x5FFFF,
221 ],
222 [ "non-character code point U+6FFFE",
223 (isASCII)
224 ? "\xf1\xaf\xbf\xbe"
225 : I8_to_native("\xf8\xad\xbf\xbf\xbe"),
226 0x6FFFE,
227 ],
228 [ "non-character code point U+6FFFF",
229 (isASCII)
230 ? "\xf1\xaf\xbf\xbf"
231 : I8_to_native("\xf8\xad\xbf\xbf\xbf"),
232 0x6FFFF,
233 ],
234 [ "non-character code point U+7FFFE",
235 (isASCII)
236 ? "\xf1\xbf\xbf\xbe"
237 : I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
238 0x7FFFE,
239 ],
240 [ "non-character code point U+7FFFF",
241 (isASCII)
242 ? "\xf1\xbf\xbf\xbf"
243 : I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
244 0x7FFFF,
245 ],
246 [ "non-character code point U+8FFFE",
247 (isASCII)
248 ? "\xf2\x8f\xbf\xbe"
249 : I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
250 0x8FFFE,
251 ],
252 [ "non-character code point U+8FFFF",
253 (isASCII)
254 ? "\xf2\x8f\xbf\xbf"
255 : I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
256 0x8FFFF,
257 ],
258 [ "non-character code point U+9FFFE",
259 (isASCII)
260 ? "\xf2\x9f\xbf\xbe"
261 : I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
262 0x9FFFE,
263 ],
264 [ "non-character code point U+9FFFF",
265 (isASCII)
266 ? "\xf2\x9f\xbf\xbf"
267 : I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
268 0x9FFFF,
269 ],
270 [ "non-character code point U+AFFFE",
271 (isASCII)
272 ? "\xf2\xaf\xbf\xbe"
273 : I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
274 0xAFFFE,
275 ],
276 [ "non-character code point U+AFFFF",
277 (isASCII)
278 ? "\xf2\xaf\xbf\xbf"
279 : I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
280 0xAFFFF,
281 ],
282 [ "non-character code point U+BFFFE",
283 (isASCII)
284 ? "\xf2\xbf\xbf\xbe"
285 : I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
286 0xBFFFE,
287 ],
288 [ "non-character code point U+BFFFF",
289 (isASCII)
290 ? "\xf2\xbf\xbf\xbf"
291 : I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
292 0xBFFFF,
293 ],
294 [ "non-character code point U+CFFFE",
295 (isASCII)
296 ? "\xf3\x8f\xbf\xbe"
297 : I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
298 0xCFFFE,
299 ],
300 [ "non-character code point U+CFFFF",
301 (isASCII)
302 ? "\xf3\x8f\xbf\xbf"
303 : I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
304 0xCFFFF,
305 ],
306 [ "non-character code point U+DFFFE",
307 (isASCII)
308 ? "\xf3\x9f\xbf\xbe"
309 : I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
310 0xDFFFE,
311 ],
312 [ "non-character code point U+DFFFF",
313 (isASCII)
314 ? "\xf3\x9f\xbf\xbf"
315 : I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
316 0xDFFFF,
317 ],
318 [ "non-character code point U+EFFFE",
319 (isASCII)
320 ? "\xf3\xaf\xbf\xbe"
321 : I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
322 0xEFFFE,
323 ],
324 [ "non-character code point U+EFFFF",
325 (isASCII)
326 ? "\xf3\xaf\xbf\xbf"
327 : I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
328 0xEFFFF,
329 ],
330 [ "non-character code point U+FFFFE",
331 (isASCII)
332 ? "\xf3\xbf\xbf\xbe"
333 : I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
334 0xFFFFE,
335 ],
336 [ "non-character code point U+FFFFF",
337 (isASCII)
338 ? "\xf3\xbf\xbf\xbf"
339 : I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
340 0xFFFFF,
341 ],
342 [ "non-character code point U+10FFFE",
343 (isASCII)
344 ? "\xf4\x8f\xbf\xbe"
345 : I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
346 0x10FFFE,
347 ],
348 [ "non-character code point U+10FFFF",
349 (isASCII)
350 ? "\xf4\x8f\xbf\xbf"
351 : I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
352 0x10FFFF,
353 ],
7c9f4ec6
KW
354 [ "first non_unicode",
355 (isASCII)
356 ? "\xf4\x90\x80\x80"
357 : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
358 0x110000,
359 2,
360 ],
361 [ "non_unicode whose first byte tells that",
362 (isASCII)
363 ? "\xf5\x80\x80\x80"
364 : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
365 (isASCII) ? 0x140000 : 0x200000,
366 1,
367 ],
3f055917
KW
368 [ "overlong malformation, lowest 5-byte",
369 (isASCII)
370 ? "\xf8\x80\x80\x80\x80"
371 : I8_to_native("\xf8\xa0\xa0\xa0\xa0"),
372 0, # NUL
373 ],
374 [ "overlong malformation, highest 5-byte",
375 (isASCII)
376 ? "\xf8\x87\xbf\xbf\xbf"
377 : I8_to_native("\xf8\xa7\xbf\xbf\xbf"),
378 (isASCII) ? 0x1FFFFF : 0x3FFFF,
379 ],
380 [ "overlong malformation, lowest 6-byte",
381 (isASCII)
382 ? "\xfc\x80\x80\x80\x80\x80"
383 : I8_to_native("\xfc\xa0\xa0\xa0\xa0\xa0"),
384 0, # NUL
385 ],
386 [ "overlong malformation, highest 6-byte",
387 (isASCII)
388 ? "\xfc\x83\xbf\xbf\xbf\xbf"
389 : I8_to_native("\xfc\xa3\xbf\xbf\xbf\xbf"),
390 (isASCII) ? 0x3FFFFFF : 0x3FFFFF,
391 ],
392 [ "overlong malformation, lowest 7-byte",
393 (isASCII)
394 ? "\xfe\x80\x80\x80\x80\x80\x80"
395 : I8_to_native("\xfe\xa0\xa0\xa0\xa0\xa0\xa0"),
396 0, # NUL
397 ],
398 [ "overlong malformation, highest 7-byte",
399 (isASCII)
400 ? "\xfe\x81\xbf\xbf\xbf\xbf\xbf"
401 : I8_to_native("\xfe\xa1\xbf\xbf\xbf\xbf\xbf"),
402 (isASCII) ? 0x7FFFFFFF : 0x3FFFFFF,
403 ],
d22ec717 404 [ "highest 31 bit code point",
8f79178b 405 (isASCII)
d22ec717 406 ? "\xfd\xbf\xbf\xbf\xbf\xbf"
8f79178b 407 : I8_to_native(
d22ec717
KW
408 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa1\xbf\xbf\xbf\xbf\xbf\xbf"),
409 0x7FFFFFFF,
410 1,
8f79178b 411 ],
d22ec717 412 [ "lowest 32 bit code point",
f4da64d4 413 (isASCII)
d22ec717 414 ? "\xfe\x82\x80\x80\x80\x80\x80"
f4da64d4 415 : I8_to_native(
d22ec717
KW
416 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
417 ($::is64bit) ? 0x80000000 : -1, # Overflows on 32-bit systems
418 1,
f4da64d4 419 ],
d22ec717
KW
420 # Used when UV_MAX is allowed as a code point
421 #[ "highest 32 bit code point",
422 # (isASCII)
423 # ? "\xfe\x83\xbf\xbf\xbf\xbf\xbf"
424 # : I8_to_native(
425 # "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
426 # 0xFFFFFFFF,
427 #],
428 #[ "Lowest 33 bit code point",
429 # (isASCII)
430 # ? "\xfe\x84\x80\x80\x80\x80\x80"
431 # : I8_to_native(
432 # "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"),
433 # ($::is64bit) ? 0x100000000 : 0x0, # Overflows on 32-bit systems
434 #],
8f79178b
KW
435 );
436
437 if (! $::is64bit) {
438 if (isASCII) {
8f79178b 439 push @tests,
cf8a8202 440 [ "overlong malformation, but naively looks like overflow",
d22ec717
KW
441 "\xff\x80\x80\x80\x80\x80\x80\x81\xbf\xbf\xbf\xbf\xbf",
442 0x7FFFFFFF,
cf8a8202 443 ],
d22ec717
KW
444 # Used when above IV_MAX are allowed.
445 #[ "overlong malformation, but naively looks like overflow",
446 # "\xff\x80\x80\x80\x80\x80\x80\x83\xbf\xbf\xbf\xbf\xbf",
447 # 0xFFFFFFFF,
448 #],
c81d4d83
KW
449 [ "overflow that old algorithm failed to detect",
450 "\xfe\x86\x80\x80\x80\x80\x80",
451 -1,
8f79178b
KW
452 ];
453 }
454 }
c81d4d83 455
3f055917
KW
456 push @tests,
457 [ "overlong malformation, lowest max-byte",
458 (isASCII)
459 ? "\xff\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80"
460 : I8_to_native(
461 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
462 0, # NUL
463 ],
464 [ "overlong malformation, highest max-byte",
465 (isASCII) # 2**36-1 on ASCII; 2**30-1 on EBCDIC
466 ? "\xff\x80\x80\x80\x80\x80\x80\xbf\xbf\xbf\xbf\xbf\xbf"
467 : I8_to_native(
468 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xbf\xbf\xbf\xbf\xbf\xbf"),
469 (isASCII) ? (($::is64bit) ? 0xFFFFFFFFF : -1) : 0x3FFFFFFF,
470 ];
471
45d8ef8c
KW
472 if (isASCII) {
473 push @tests,
474 [ "Lowest code point requiring 13 bytes to represent", # 2**36
475 "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80",
476 ($::is64bit) ? 0x1000000000 : -1, # overflows on 32bit
477 ],
478 };
479
c81d4d83 480 if ($::is64bit) {
ba627a0b 481 push @tests,
d22ec717 482 [ "highest 63 bit code point",
ba627a0b 483 (isASCII)
d22ec717 484 ? "\xff\x80\x87\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
ba627a0b 485 : I8_to_native(
d22ec717
KW
486 "\xff\xa7\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"),
487 0x7FFFFFFFFFFFFFFF,
ba627a0b
KW
488 (isASCII) ? 1 : 2,
489 ],
d22ec717 490 [ "first 64 bit code point",
ba627a0b 491 (isASCII)
d22ec717 492 ? "\xff\x80\x88\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80"
ba627a0b 493 : I8_to_native(
d22ec717 494 "\xff\xa8\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
ba627a0b 495 -1,
ba627a0b 496 ];
d22ec717
KW
497 # Used when UV_MAX is allowed as a code point
498 #[ "highest 64 bit code point",
499 # (isASCII)
500 # ? "\xff\x80\x8f\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
501 # : I8_to_native(
502 # "\xff\xaf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"),
503 # 0xFFFFFFFFFFFFFFFF,
504 # (isASCII) ? 1 : 2,
505 #],
506 #[ "first 65 bit code point",
507 # (isASCII)
508 # ? "\xff\x80\x9f\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80"
509 # : I8_to_native(
510 # "\xff\xb0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
511 # 0,
512 #];
c81d4d83
KW
513 if (isASCII) {
514 push @tests,
515 [ "overflow that old algorithm failed to detect",
516 "\xff\x80\x90\x90\x90\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf",
517 -1,
c81d4d83
KW
518 ];
519 }
520 else {
8f79178b
KW
521 push @tests, # These could falsely show wrongly in a naive
522 # implementation
523 [ "requires at least 32 bits",
524 I8_to_native(
6aa905cf 525 "\xff\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b 526 0x800000000,
5064495b 527 40000000
8f79178b
KW
528 ],
529 [ "requires at least 32 bits",
530 I8_to_native(
6aa905cf 531 "\xff\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b 532 0x10000000000,
8f79178b
KW
533 ],
534 [ "requires at least 32 bits",
535 I8_to_native(
6aa905cf 536 "\xff\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b 537 0x200000000000,
8f79178b
KW
538 ],
539 [ "requires at least 32 bits",
540 I8_to_native(
6aa905cf 541 "\xff\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b 542 0x4000000000000,
8f79178b
KW
543 ],
544 [ "requires at least 32 bits",
545 I8_to_native(
6aa905cf 546 "\xff\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b 547 0x80000000000000,
8f79178b
KW
548 ],
549 [ "requires at least 32 bits",
550 I8_to_native(
6aa905cf 551 "\xff\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b 552 0x1000000000000000,
8f79178b
KW
553 ];
554 }
6aa905cf 555 }
c7f9e4bb 556}
6aa905cf 557
5722c46d
KW
558sub flags_to_text($$)
559{
560 my ($flags, $flags_to_text_ref) = @_;
561
562 # Returns a string containing a mnemonic representation of the bits that
563 # are set in the $flags. These are assumed to be flag bits. The return
564 # looks like "FOO|BAR|BAZ". The second parameter is a reference to an
565 # array that gives the textual representation of all the possible flags.
566 # Element 0 is the text for the bit 0 flag; element 1 for bit 1; .... If
567 # no bits at all are set the string "0" is returned;
568
569 my @flag_text;
570 my $shift = 0;
571
572 return "0" if $flags == 0;
573
574 while ($flags) {
575 #diag sprintf "%x", $flags;
576 if ($flags & 1) {
577 push @flag_text, $flags_to_text_ref->[$shift];
578 }
579 $shift++;
580 $flags >>= 1;
581 }
582
583 return join "|", @flag_text;
584}
585
586# Possible flag returns from utf8n_to_uvchr_error(). These should have G_,
587# instead of A_, D_, but the prefixes will be used in a a later commit, so
588# minimize churn by having them here.
589my @utf8n_flags_to_text = ( qw(
590 A_EMPTY
591 A_CONTINUATION
592 A_NON_CONTINUATION
593 A_SHORT
594 A_LONG
595 A_LONG_AND_ITS_VALUE
596 PLACEHOLDER
597 A_OVERFLOW
598 D_SURROGATE
599 W_SURROGATE
600 D_NONCHAR
601 W_NONCHAR
602 D_SUPER
603 W_SUPER
d044b7a7
KW
604 D_PERL_EXTENDED
605 W_PERL_EXTENDED
5722c46d
KW
606 CHECK_ONLY
607 NO_CONFIDENCE_IN_CURLEN_
608 ) );
609
a8ee5133
KW
610sub utf8n_display_call($)
611{
612 # Converts an eval string that calls test_utf8n_to_uvchr into a more human
613 # readable form, and returns it. Doesn't work if the byte string contains
614 # an apostrophe. The return will look something like:
615 # test_utf8n_to_uvchr_error('$bytes', $length, $flags)
616 #diag $_[0];
617
618 $_[0] =~ / ^ ( [^(]* \( ) ' ( [^']*? ) ' ( .+ , \D* ) ( \d+ ) \) $ /x;
619 my $text1 = $1; # Everything before the byte string
620 my $bytes = $2;
621 my $text2 = $3; # Includes the length
622 my $flags = $4;
623
624 return $text1
625 . display_bytes($bytes)
626 . $text2
627 . flags_to_text($flags, \@utf8n_flags_to_text)
628 . ')';
629}
630
d884ea32
KW
631sub uvchr_display_call($)
632{
633 # Converts an eval string that calls test_uvchr_to_utf8 into a more human
634 # readable form, and returns it. The return will look something like:
635 # test_uvchr_to_utf8n_flags($uv, $flags)
636 #diag $_[0];
637
638 my @flags_to_text = ( qw(
639 W_SURROGATE
640 W_NONCHAR
641 W_SUPER
d044b7a7 642 W_PERL_EXTENDED
d884ea32
KW
643 D_SURROGATE
644 D_NONCHAR
645 D_SUPER
d044b7a7 646 D_PERL_EXTENDED
d884ea32
KW
647 ) );
648
649 $_[0] =~ / ^ ( [^(]* \( ) ( \d+ ) , \s* ( \d+ ) \) $ /x;
650 my $text = $1;
651 my $cp = sprintf "%X", $2;
652 my $flags = $3;
653
654 return "${text}0x$cp, " . flags_to_text($flags, \@flags_to_text) . ')';
655}
656
69485e19
KW
657sub do_warnings_test(@)
658{
659 my @expected_warnings = @_;
660
661 # Compares the input expected warnings array with @warnings_gotten,
662 # generating a pass for each found, removing it from @warnings_gotten.
663 # Any discrepancies generate test failures. Returns TRUE if no
664 # discrepcancies; otherwise FALSE.
665
666 my $succeeded = 1;
667
668 if (@expected_warnings == 0) {
669 if (! is(@warnings_gotten, 0, " Expected and got no warnings")) {
670 output_warnings(@warnings_gotten);
671 $succeeded = 0;
672 }
673 return $succeeded;
674 }
675
676 # Check that we got all the expected warnings,
677 # removing each one found
678 WARNING:
679 foreach my $expected (@expected_warnings) {
680 foreach (my $i = 0; $i < @warnings_gotten; $i++) {
681 if ($warnings_gotten[$i] =~ $expected) {
682 pass(" Expected and got warning: "
683 . " $warnings_gotten[$i]");
684 splice @warnings_gotten, $i, 1;
685 next WARNING;
686 }
687 }
688 fail(" Expected a warning that matches "
689 . $expected . " but didn't get it");
690 $succeeded = 0;
691 }
692
693 if (! is(@warnings_gotten, 0, " Got no unexpected warnings")) {
694 output_warnings(@warnings_gotten);
695 $succeeded = 0;
696 }
697
698 return $succeeded;
699}
700
6aa905cf
KW
701# This test is split into this number of files.
702my $num_test_files = $ENV{TEST_JOBS} || 1;
703$num_test_files = 10 if $num_test_files > 10;
704
37657a5b
KW
705# We only really need to test utf8n_to_uvchr_msgs() once with this flag.
706my $tested_CHECK_ONLY = 0;
707
6aa905cf
KW
708my $test_count = -1;
709foreach my $test (@tests) {
e0803729
KW
710 $test_count++;
711 next if $test_count % $num_test_files != $::TEST_CHUNK;
712
713 my ($testname, $bytes, $allowed_uv, $needed_to_discern_len) = @$test;
714
715 my $length = length $bytes;
716 my $initially_overlong = $testname =~ /overlong/;
717 my $initially_orphan = $testname =~ /orphan/;
718 my $will_overflow = $allowed_uv < 0;
719
720 my $uv_string = sprintf(($allowed_uv < 0x100) ? "%02X" : "%04X", $allowed_uv);
721 my $display_bytes = display_bytes($bytes);
722
723 my $controlling_warning_category;
724 my $utf8n_flag_to_warn;
725 my $utf8n_flag_to_disallow;
726 my $uvchr_flag_to_warn;
727 my $uvchr_flag_to_disallow;
728
729 # We want to test that the independent flags are actually independent.
730 # For example, that a surrogate doesn't trigger a non-character warning,
731 # and conversely, turning off an above-Unicode flag doesn't suppress a
732 # surrogate warning. Earlier versions of this file used nested loops to
733 # test all possible combinations. But that creates lots of tests, making
734 # this run too long. What is now done instead is to use the complement of
735 # the category we are testing to greatly reduce the combinatorial
736 # explosion. For example, if we have a surrogate and we aren't expecting
737 # a warning about it, we set all the flags for non-surrogates to raise
738 # warnings. If one shows up, it indicates the flags aren't independent.
739 my $utf8n_flag_to_warn_complement;
740 my $utf8n_flag_to_disallow_complement;
741 my $uvchr_flag_to_warn_complement;
742 my $uvchr_flag_to_disallow_complement;
743
744 # Many of the code points being tested are middling in that if code point
745 # edge cases work, these are very likely to as well. Because this test
746 # file takes a while to execute, we skip testing the edge effects of code
747 # points deemed middling, while testing their basics and continuing to
748 # fully test the non-middling code points.
749 my $skip_most_tests = 0;
750
751 my $cp_message_qr; # Pattern that matches the message raised when
752 # that message contains the problematic code
753 # point. The message is the same (currently) both
754 # when going from/to utf8.
755 my $non_cp_trailing_text; # The suffix text when the message doesn't
756 # contain a code point. (This is a result of
757 # some sort of malformation that means we
758 # can't get an exact code poin
759 my $extended_cp_message_qr = qr/\QCode point 0x$uv_string is not Unicode,\E
760 \Q requires a Perl extension, and so is not\E
761 \Q portable\E/x;
762 my $extended_non_cp_trailing_text
763 = "is a Perl extension, and so is not portable";
764
765 # What bytes should have been used to specify a code point that has been
766 # specified as an overlong.
767 my $correct_bytes_for_overlong;
768
769 # Is this test malformed from the beginning? If so, we know to generally
770 # expect that the tests will show it isn't valid.
771 my $initially_malformed = 0;
772
773 if ($initially_overlong || $initially_orphan) {
774 $non_cp_trailing_text = "if you see this, there is an error";
775 $cp_message_qr = qr/\Q$non_cp_trailing_text\E/;
776 $initially_malformed = 1;
777 $utf8n_flag_to_warn = 0;
778 $utf8n_flag_to_disallow = 0;
779
780 $utf8n_flag_to_warn_complement = $::UTF8_WARN_SURROGATE;
781 $utf8n_flag_to_disallow_complement = $::UTF8_DISALLOW_SURROGATE;
782 if (! $will_overflow && $allowed_uv <= 0x10FFFF) {
783 $utf8n_flag_to_warn_complement |= $::UTF8_WARN_SUPER;
784 $utf8n_flag_to_disallow_complement |= $::UTF8_DISALLOW_SUPER;
785 if (($allowed_uv & 0xFFFF) != 0xFFFF) {
786 $utf8n_flag_to_warn_complement |= $::UTF8_WARN_NONCHAR;
787 $utf8n_flag_to_disallow_complement |= $::UTF8_DISALLOW_NONCHAR;
788 }
789 }
790 if (! is_extended_utf8($bytes)) {
791 $utf8n_flag_to_warn_complement |= $::UTF8_WARN_PERL_EXTENDED;
792 $utf8n_flag_to_disallow_complement |= $::UTF8_DISALLOW_PERL_EXTENDED;
793 }
2282dfc4 794
e0803729 795 $controlling_warning_category = 'utf8';
2282dfc4 796
e0803729
KW
797 if ($initially_overlong) {
798 if (! defined $needed_to_discern_len) {
799 $needed_to_discern_len = overlong_discern_len($bytes);
800 }
801 $correct_bytes_for_overlong = display_bytes_no_quotes(chr $allowed_uv);
802 }
803 }
804 elsif($will_overflow || $allowed_uv > 0x10FFFF) {
805
806 # Set the SUPER flags; later, we test for PERL_EXTENDED as well.
807 $utf8n_flag_to_warn = $::UTF8_WARN_SUPER;
808 $utf8n_flag_to_disallow = $::UTF8_DISALLOW_SUPER;
809 $uvchr_flag_to_warn = $::UNICODE_WARN_SUPER;
810 $uvchr_flag_to_disallow = $::UNICODE_DISALLOW_SUPER;;
811
812 # Below, we add the flags for non-perl_extended to the code points
813 # that don't fit that category. Special tests are done for this
814 # category in the inner loop.
815 $utf8n_flag_to_warn_complement = $::UTF8_WARN_NONCHAR
816 |$::UTF8_WARN_SURROGATE;
817 $utf8n_flag_to_disallow_complement = $::UTF8_DISALLOW_NONCHAR
818 |$::UTF8_DISALLOW_SURROGATE;
819 $uvchr_flag_to_warn_complement = $::UNICODE_WARN_NONCHAR
820 |$::UNICODE_WARN_SURROGATE;
821 $uvchr_flag_to_disallow_complement = $::UNICODE_DISALLOW_NONCHAR
822 |$::UNICODE_DISALLOW_SURROGATE;
823 $controlling_warning_category = 'non_unicode';
824
825 if ($will_overflow) { # This is realy a malformation
826 $non_cp_trailing_text = "if you see this, there is an error";
827 $cp_message_qr = qr/\Q$non_cp_trailing_text\E/;
828 $initially_malformed = 1;
829 if (! defined $needed_to_discern_len) {
830 $needed_to_discern_len = overflow_discern_len($length);
831 }
832 }
833 elsif (requires_extended_utf8($allowed_uv)) {
834 $cp_message_qr = $extended_cp_message_qr;
835 $non_cp_trailing_text = $extended_non_cp_trailing_text;
836 $needed_to_discern_len = 1 unless defined $needed_to_discern_len;
837 }
838 else {
839 $cp_message_qr = qr/\QCode point 0x$uv_string is not Unicode,\E
840 \Q may not be portable\E/x;
841 $non_cp_trailing_text = "is for a non-Unicode code point, may not"
842 . " be portable";
843 $utf8n_flag_to_warn_complement |= $::UTF8_WARN_PERL_EXTENDED;
844 $utf8n_flag_to_disallow_complement
845 |= $::UTF8_DISALLOW_PERL_EXTENDED;
846 $uvchr_flag_to_warn_complement |= $::UNICODE_WARN_PERL_EXTENDED;
847 $uvchr_flag_to_disallow_complement
848 |= $::UNICODE_DISALLOW_PERL_EXTENDED;
849 }
850 }
851 elsif ($allowed_uv >= 0xD800 && $allowed_uv <= 0xDFFF) {
852 $cp_message_qr = qr/UTF-16 surrogate U\+$uv_string/;
853 $non_cp_trailing_text = "is for a surrogate";
854 $needed_to_discern_len = 2 unless defined $needed_to_discern_len;
855 $skip_most_tests = 1 if $allowed_uv > 0xD800 && $allowed_uv < 0xDFFF;
856
857 $utf8n_flag_to_warn = $::UTF8_WARN_SURROGATE;
858 $utf8n_flag_to_disallow = $::UTF8_DISALLOW_SURROGATE;
859 $uvchr_flag_to_warn = $::UNICODE_WARN_SURROGATE;
860 $uvchr_flag_to_disallow = $::UNICODE_DISALLOW_SURROGATE;;
861
862 $utf8n_flag_to_warn_complement = $::UTF8_WARN_NONCHAR
863 |$::UTF8_WARN_SUPER
864 |$::UTF8_WARN_PERL_EXTENDED;
865 $utf8n_flag_to_disallow_complement = $::UTF8_DISALLOW_NONCHAR
866 |$::UTF8_DISALLOW_SUPER
867 |$::UTF8_DISALLOW_PERL_EXTENDED;
868 $uvchr_flag_to_warn_complement = $::UNICODE_WARN_NONCHAR
869 |$::UNICODE_WARN_SUPER
870 |$::UNICODE_WARN_PERL_EXTENDED;
871 $uvchr_flag_to_disallow_complement = $::UNICODE_DISALLOW_NONCHAR
872 |$::UNICODE_DISALLOW_SUPER
873 |$::UNICODE_DISALLOW_PERL_EXTENDED;
874 $controlling_warning_category = 'surrogate';
875 }
876 elsif ( ($allowed_uv >= 0xFDD0 && $allowed_uv <= 0xFDEF)
877 || ($allowed_uv & 0xFFFE) == 0xFFFE)
878 {
879 $cp_message_qr = qr/\QUnicode non-character U+$uv_string\E
880 \Q is not recommended for open interchange\E/x;
881 $non_cp_trailing_text = "if you see this, there is an error";
882 $needed_to_discern_len = $length unless defined $needed_to_discern_len;
883 if ( ($allowed_uv > 0xFDD0 && $allowed_uv < 0xFDEF)
884 || ($allowed_uv > 0xFFFF && $allowed_uv < 0x10FFFE))
885 {
886 $skip_most_tests = 1;
887 }
4816e15f 888
e0803729
KW
889 $utf8n_flag_to_warn = $::UTF8_WARN_NONCHAR;
890 $utf8n_flag_to_disallow = $::UTF8_DISALLOW_NONCHAR;
891 $uvchr_flag_to_warn = $::UNICODE_WARN_NONCHAR;
892 $uvchr_flag_to_disallow = $::UNICODE_DISALLOW_NONCHAR;;
893
894 $utf8n_flag_to_warn_complement = $::UTF8_WARN_SURROGATE
895 |$::UTF8_WARN_SUPER
896 |$::UTF8_WARN_PERL_EXTENDED;
897 $utf8n_flag_to_disallow_complement = $::UTF8_DISALLOW_SURROGATE
898 |$::UTF8_DISALLOW_SUPER
899 |$::UTF8_DISALLOW_PERL_EXTENDED;
900 $uvchr_flag_to_warn_complement = $::UNICODE_WARN_SURROGATE
901 |$::UNICODE_WARN_SUPER
902 |$::UNICODE_WARN_PERL_EXTENDED;
903 $uvchr_flag_to_disallow_complement = $::UNICODE_DISALLOW_SURROGATE
904 |$::UNICODE_DISALLOW_SUPER
905 |$::UNICODE_DISALLOW_PERL_EXTENDED;
906
907 $controlling_warning_category = 'nonchar';
908 }
909 else {
910 die "Can't figure out what type of warning to test for $testname"
911 }
912
913 die 'Didn\'t set $needed_to_discern_len for ' . $testname
914 unless defined $needed_to_discern_len;
915
916 # We try various combinations of malformations that can occur
917 foreach my $short (0, 1) {
918 next if $skip_most_tests && $short;
919 foreach my $unexpected_noncont (0, 1) {
920 next if $skip_most_tests && $unexpected_noncont;
921 foreach my $overlong (0, 1) {
922 next if $overlong && $skip_most_tests;
923 next if $initially_overlong && ! $overlong;
924
925 # If we're creating an overlong, it can't be longer than the
926 # maximum length, so skip if we're already at that length.
927 next if (! $initially_overlong && $overlong)
928 && $length >= $::max_bytes;
929
930 my $this_cp_message_qr = $cp_message_qr;
931 my $this_non_cp_trailing_text = $non_cp_trailing_text;
932
933 foreach my $malformed_allow_type (0..2) {
934 # 0 don't allow this malformation; ignored if no malformation
935 # 1 allow, with REPLACEMENT CHARACTER returned
936 # 2 allow, with intended code point returned. All malformations
937 # other than overlong can't determine the intended code point,
938 # so this isn't valid for them.
939 next if $malformed_allow_type == 2
940 && ($will_overflow || $short || $unexpected_noncont);
941 next if $skip_most_tests && $malformed_allow_type;
942
943 # Here we are in the innermost loop for malformations. So we
944 # know which ones are in effect. Can now change the input to be
945 # appropriately malformed. We also can set up certain other
946 # things now, like whether we expect a return flag from this
947 # malformation, and which flag.
948
949 my $this_bytes = $bytes;
950 my $this_length = $length;
951 my $this_expected_len = $length;
952 my $this_needed_to_discern_len = $needed_to_discern_len;
953
954 my @malformation_names;
955 my @expected_malformation_warnings;
956 my @expected_malformation_return_flags;
957
958 # Contains the flags for any allowed malformations. Currently no
959 # combinations of on/off are tested for. It's either all are
960 # allowed, or none are.
961 my $allow_flags = 0;
962 my $overlong_is_in_perl_extended_utf8 = 0;
963 my $dont_use_overlong_cp = 0;
964
965 if ($initially_orphan) {
966 next if $overlong || $short || $unexpected_noncont;
967 }
717dd9f9 968
e0803729
KW
969 if ($overlong) {
970 if (! $initially_overlong) {
971 my $new_expected_len;
972
973 # To force this malformation, we convert the original start
974 # byte into a continuation byte with the same data bits as
975 # originally. ...
976 my $start_byte = substr($this_bytes, 0, 1);
977 my $converted_to_continuation_byte
978 = start_byte_to_cont($start_byte);
979
980 # ... Then we prepend it with a known overlong sequence.
981 # This should evaluate to the exact same code point as the
982 # original. We try to avoid an overlong using Perl
983 # extended UTF-8. The code points are the highest
984 # representable as overlongs on the respective platform
985 # without using extended UTF-8.
986 if (native_to_I8($start_byte) lt "\xFC") {
987 $start_byte = I8_to_native("\xFC");
988 $new_expected_len = 6;
989 }
990 elsif (! isASCII && native_to_I8($start_byte) lt "\xFE") {
991
992 # FE is not extended UTF-8 on EBCDIC
993 $start_byte = I8_to_native("\xFE");
994 $new_expected_len = 7;
995 }
996 else { # Must use extended UTF-8. On ASCII platforms, we
997 # could express some overlongs here starting with
998 # \xFE, but there's no real reason to do so.
999 $overlong_is_in_perl_extended_utf8 = 1;
1000 $start_byte = I8_to_native("\xFF");
1001 $new_expected_len = $::max_bytes;
1002 $this_cp_message_qr = $extended_cp_message_qr;
1003
1004 # The warning that gets raised doesn't include the
1005 # code point in the message if the code point can be
1006 # expressed without using extended UTF-8, but the
1007 # particular overlong sequence used is in extended
1008 # UTF-8. To do otherwise would be confusing to the
1009 # user, as it would claim the code point requires
1010 # extended, when it doesn't.
1011 $dont_use_overlong_cp = 1
1012 unless requires_extended_utf8($allowed_uv);
1013 $this_non_cp_trailing_text
1014 = $extended_non_cp_trailing_text;
1015 }
1016
1017 # Splice in the revise continuation byte, preceded by the
1018 # start byte and the proper number of the lowest
1019 # continuation bytes.
1020 $this_bytes = $start_byte
1021 . ($native_lowest_continuation_chr
1022 x ( $new_expected_len
1023 - 1
1024 - length($this_bytes)))
1025 . $converted_to_continuation_byte
1026 . substr($this_bytes, 1);
1027 $this_length = length($this_bytes);
1028 $this_needed_to_discern_len = $new_expected_len
1029 - ( $this_expected_len
1030 - $this_needed_to_discern_len);
1031 $this_expected_len = $new_expected_len;
1032 }
1033 }
4816e15f 1034
e0803729 1035 if ($short) {
4816e15f 1036
e0803729
KW
1037 # To force this malformation, just tell the test to not look
1038 # as far as it should into the input.
1039 $this_length--;
1040 $this_expected_len--;
4816e15f 1041
e0803729
KW
1042 $allow_flags |= $::UTF8_ALLOW_SHORT if $malformed_allow_type;
1043 }
1d21b5e7 1044
e0803729 1045 if ($unexpected_noncont) {
4816e15f 1046
e0803729
KW
1047 # To force this malformation, change the final continuation
1048 # byte into a start byte.
1049 my $pos = ($short) ? -2 : -1;
1050 substr($this_bytes, $pos, 1) = $known_start_byte;
1051 $this_expected_len--;
1052 }
153bcbd6 1053
e0803729
KW
1054 # The whole point of a test that is malformed from the beginning
1055 # is to test for that malformation. If we've modified things so
1056 # much that we don't have enough information to detect that
1057 # malformation, there's no point in testing.
1058 next if $initially_malformed
1059 && $this_expected_len < $this_needed_to_discern_len;
1060
1061 # Here, we've transformed the input with all of the desired
1062 # non-overflow malformations. We are now in a position to
1063 # construct any potential warnings for those malformations. But
1064 # it's a pain to get the detailed messages exactly right, so for
1065 # now XXX, only do so for those that return an explicit code
1066 # point.
1067
1068 if ($initially_orphan) {
1069 push @malformation_names, "orphan continuation";
1070 push @expected_malformation_return_flags,
1071 $::UTF8_GOT_CONTINUATION;
1072 $allow_flags |= $::UTF8_ALLOW_CONTINUATION
1073 if $malformed_allow_type;
1074 push @expected_malformation_warnings, qr/unexpected continuation/;
1075 }
4816e15f 1076
e0803729
KW
1077 if ($overlong) {
1078 push @malformation_names, 'overlong';
1079 push @expected_malformation_return_flags, $::UTF8_GOT_LONG;
4816e15f 1080
e0803729
KW
1081 # If one of the other malformation types is also in effect, we
1082 # don't know what the intended code point was.
1083 if ($short || $unexpected_noncont || $will_overflow) {
1084 push @expected_malformation_warnings, qr/overlong/;
1085 }
1086 else {
1087 my $wrong_bytes = display_bytes_no_quotes(
1088 substr($this_bytes, 0, $this_length));
1089 if (! defined $correct_bytes_for_overlong) {
1090 $correct_bytes_for_overlong
1091 = display_bytes_no_quotes($bytes);
1092 }
1093 my $prefix = ( $allowed_uv > 0x10FFFF
1094 || ! isASCII && $allowed_uv < 256)
1095 ? "0x"
1096 : "U+";
1097 push @expected_malformation_warnings,
1098 qr/\QMalformed UTF-8 character: $wrong_bytes\E
1099 \Q (overlong; instead use\E
1100 \Q $correct_bytes_for_overlong to\E
1101 \Q represent $prefix$uv_string)/x;
1102 }
4816e15f 1103
e0803729
KW
1104 if ($malformed_allow_type == 2) {
1105 $allow_flags |= $::UTF8_ALLOW_LONG_AND_ITS_VALUE;
1106 }
1107 elsif ($malformed_allow_type) {
1108 $allow_flags |= $::UTF8_ALLOW_LONG;
1109 }
1110 }
1111 if ($short) {
1112 push @malformation_names, 'short';
1113 push @expected_malformation_return_flags, $::UTF8_GOT_SHORT;
1114 push @expected_malformation_warnings, qr/too short/;
1115 }
1116 if ($unexpected_noncont) {
1117 push @malformation_names, 'unexpected non-continuation';
1118 push @expected_malformation_return_flags,
1119 $::UTF8_GOT_NON_CONTINUATION;
1120 $allow_flags |= $::UTF8_ALLOW_NON_CONTINUATION
1121 if $malformed_allow_type;
1122 push @expected_malformation_warnings,
1123 qr/unexpected non-continuation byte/;
1124 }
4816e15f 1125
e0803729
KW
1126 # The overflow malformation is done differently than other
1127 # malformations. It comes from manually typed tests in the test
1128 # array. We now make it be treated like one of the other
1129 # malformations. But some has to be deferred until the inner loop
1130 my $overflow_msg_pattern;
1131 if ($will_overflow) {
1132 push @malformation_names, 'overflow';
1133
1134 $overflow_msg_pattern = display_bytes_no_quotes(
1135 substr($this_bytes, 0, $this_expected_len));
1136 $overflow_msg_pattern = qr/\QMalformed UTF-8 character:\E
1137 \Q $overflow_msg_pattern\E
1138 \Q (overflows)\E/x;
1139 push @expected_malformation_return_flags, $::UTF8_GOT_OVERFLOW;
1140 $allow_flags |= $::UTF8_ALLOW_OVERFLOW if $malformed_allow_type;
1141 }
8e0e76af 1142
e0803729
KW
1143 # And we can create the malformation-related text for the the test
1144 # names we eventually will generate.
1145 my $malformations_name = "";
1146 if (@malformation_names) {
1147 $malformations_name .= "dis" unless $malformed_allow_type;
1148 $malformations_name .= "allowed ";
1149 $malformations_name .= "malformation";
1150 $malformations_name .= "s" if @malformation_names > 1;
1151 $malformations_name .= ": ";
1152 $malformations_name .= join "/", @malformation_names;
1153 $malformations_name = " ($malformations_name)";
1154 }
8e0e76af 1155
e0803729 1156 # Done setting up the malformation related stuff
8e0e76af 1157
e0803729
KW
1158 { # First test the isFOO calls
1159 use warnings; # XXX no warnings 'deprecated'; # Make sure these don't raise warnings
1160 undef @warnings_gotten;
8e0e76af 1161
e0803729
KW
1162 my $ret = test_isUTF8_CHAR($this_bytes, $this_length);
1163 my $ret_flags
1164 = test_isUTF8_CHAR_flags($this_bytes, $this_length, 0);
1165 if ($malformations_name) {
1166 is($ret, 0, "For $testname$malformations_name: isUTF8_CHAR() returns 0");
1167 is($ret_flags, 0, " And isUTF8_CHAR_flags() returns 0");
1168 }
1169 else {
1170 is($ret, $this_length, "For $testname: isUTF8_CHAR() returns"
1171 . " expected length: $this_length");
1172 is($ret_flags, $this_length,
1173 " And isUTF8_CHAR_flags(...,0) returns expected"
1174 . " length: $this_length");
1175 }
1176 is(scalar @warnings_gotten, 0,
1177 " And neither isUTF8_CHAR() nor isUTF8_CHAR()_flags"
1178 . " generated any warnings")
1179 or output_warnings(@warnings_gotten);
1180
1181 undef @warnings_gotten;
1182 $ret = test_isSTRICT_UTF8_CHAR($this_bytes, $this_length);
1183 if ($malformations_name) {
1184 is($ret, 0, " And isSTRICT_UTF8_CHAR() returns 0");
1185 }
1186 else {
1187 my $expected_ret
1188 = ( $testname =~ /surrogate|non-character/
1189 || $allowed_uv > 0x10FFFF)
1190 ? 0
1191 : $this_length;
1192 is($ret, $expected_ret,
1193 " And isSTRICT_UTF8_CHAR() returns expected"
1194 . " length: $expected_ret");
1195 $ret = test_isUTF8_CHAR_flags($this_bytes, $this_length,
1196 $::UTF8_DISALLOW_ILLEGAL_INTERCHANGE);
1197 is($ret, $expected_ret,
1198 " And isUTF8_CHAR_flags('"
1199 . "DISALLOW_ILLEGAL_INTERCHANGE') acts like"
1200 . " isSTRICT_UTF8_CHAR");
1201 }
1202 is(scalar @warnings_gotten, 0,
1203 " And neither isSTRICT_UTF8_CHAR() nor"
1204 . " isUTF8_CHAR_flags generated any warnings")
1205 or output_warnings(@warnings_gotten);
1206
1207 undef @warnings_gotten;
1208 $ret = test_isC9_STRICT_UTF8_CHAR($this_bytes, $this_length);
1209 if ($malformations_name) {
1210 is($ret, 0, " And isC9_STRICT_UTF8_CHAR() returns 0");
1211 }
1212 else {
1213 my $expected_ret = ( $testname =~ /surrogate/
1214 || $allowed_uv > 0x10FFFF)
1215 ? 0
1216 : $this_expected_len;
1217 is($ret, $expected_ret, " And isC9_STRICT_UTF8_CHAR()"
1218 . " returns expected length:"
1219 . " $expected_ret");
1220 $ret = test_isUTF8_CHAR_flags($this_bytes, $this_length,
1221 $::UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE);
1222 is($ret, $expected_ret,
1223 " And isUTF8_CHAR_flags('"
1224 . "DISALLOW_ILLEGAL_C9_INTERCHANGE') acts like"
1225 . " isC9_STRICT_UTF8_CHAR");
1226 }
1227 is(scalar @warnings_gotten, 0,
1228 " And neither isC9_STRICT_UTF8_CHAR() nor"
1229 . " isUTF8_CHAR_flags generated any warnings")
1230 or output_warnings(@warnings_gotten);
1231
1232 foreach my $disallow_type (0..2) {
1233 # 0 is don't disallow this type of code point
1234 # 1 is do disallow
1235 # 2 is do disallow, but only code points requiring
1236 # perl-extended-UTF8
1237
1238 my $disallow_flags;
1239 my $expected_ret;
1240
1241 if ($malformations_name) {
1242
1243 # Malformations are by default disallowed, so testing
1244 # with $disallow_type equal to 0 is sufficicient.
1245 next if $disallow_type;
1246
1247 $disallow_flags = 0;
1248 $expected_ret = 0;
1249 }
1250 elsif ($disallow_type == 1) {
1251 $disallow_flags = $utf8n_flag_to_disallow;
1252 $expected_ret = 0;
1253 }
1254 elsif ($disallow_type == 2) {
1255 next if ! requires_extended_utf8($allowed_uv);
1256 $disallow_flags = $::UTF8_DISALLOW_PERL_EXTENDED;
1257 $expected_ret = 0;
1258 }
1259 else { # type is 0
1260 $disallow_flags = $utf8n_flag_to_disallow_complement;
1261 $expected_ret = $this_length;
1262 }
1263
1264 $ret = test_isUTF8_CHAR_flags($this_bytes, $this_length,
1265 $disallow_flags);
1266 is($ret, $expected_ret,
1267 " And isUTF8_CHAR_flags($display_bytes,"
1268 . " $disallow_flags) returns $expected_ret")
1269 or diag "The flags mean "
1270 . flags_to_text($disallow_flags,
1271 \@utf8n_flags_to_text);
1272 is(scalar @warnings_gotten, 0,
1273 " And isUTF8_CHAR_flags(...) generated"
1274 . " no warnings")
1275 or output_warnings(@warnings_gotten);
1276
1277 # Test partial character handling, for each byte not a
1278 # full character
1279 my $did_test_partial = 0;
1280 for (my $j = 1; $j < $this_length - 1; $j++) {
1281 $did_test_partial = 1;
1282 my $partial = substr($this_bytes, 0, $j);
1283 my $ret_should_be;
1284 my $comment;
1285 if ($disallow_type || $malformations_name) {
1286 $ret_should_be = 0;
1287 $comment = "disallowed";
1288
1289 # The number of bytes required to tell if a
1290 # sequence has something wrong is the smallest of
1291 # all the things wrong with it. We start with the
1292 # number for this type of code point, if that is
1293 # disallowed; or the whole length if not. The
1294 # latter is what a couple of the malformations
1295 # require.
1296 my $needed_to_tell = ($disallow_type)
1297 ? $this_needed_to_discern_len
1298 : $this_expected_len;
1299
1300 # Then we see if the malformations that are
1301 # detectable early in the string are present.
1302 if ($overlong) {
1303 my $dl = overlong_discern_len($this_bytes);
1304 $needed_to_tell = $dl if $dl < $needed_to_tell;
1305 }
1306 if ($will_overflow) {
1307 my $dl = overflow_discern_len($length);
1308 $needed_to_tell = $dl if $dl < $needed_to_tell;
1309 }
8e0e76af 1310
e0803729
KW
1311 if ($j < $needed_to_tell) {
1312 $ret_should_be = 1;
1313 $comment .= ", but need $needed_to_tell"
1314 . " bytes to discern:";
1315 }
1316 }
1317 else {
1318 $ret_should_be = 1;
1319 $comment = "allowed";
1320 }
8e0e76af 1321
e0803729 1322 undef @warnings_gotten;
8e0e76af 1323
e0803729
KW
1324 $ret = test_is_utf8_valid_partial_char_flags($partial,
1325 $j, $disallow_flags);
1326 is($ret, $ret_should_be,
1327 " And is_utf8_valid_partial_char_flags("
1328 . display_bytes($partial)
1329 . ", $disallow_flags), $comment: returns"
1330 . " $ret_should_be")
8e0e76af 1331 or diag "The flags mean "
e0803729
KW
1332 . flags_to_text($disallow_flags, \@utf8n_flags_to_text);
1333 }
1334
1335 if ($did_test_partial) {
1336 is(scalar @warnings_gotten, 0,
1337 " And is_utf8_valid_partial_char_flags()"
1338 . " generated no warnings for any of the lengths")
1339 or output_warnings(@warnings_gotten);
1340 }
1341 }
1342 }
8e0e76af 1343
e0803729
KW
1344 # Now test the to/from UTF-8 calls. There are several orthogonal
1345 # variables involved. We test most possible combinations
8e0e76af 1346
e0803729
KW
1347 foreach my $do_disallow (0, 1) {
1348 if ($do_disallow) {
1349 next if $initially_overlong || $initially_orphan;
1350 }
1351 else {
1352 next if $skip_most_tests;
8e0e76af
KW
1353 }
1354
e0803729
KW
1355 # This tests three functions. utf8n_to_uvchr_error,
1356 # utf8n_to_uvchr_msgs, and uvchr_to_utf8_flags. But only the
1357 # first two are variants of each other. We use a loop
1358 # 'which_func' to determine which of these. uvchr_to_utf8_flags
1359 # is done separately at the end of each iteration, only when
1360 # which_func is 0. which_func is numeric in part so we don't
1361 # have to type in the function name and risk misspelling it
1362 # somewhere, and also it sets whether we are expecting warnings
1363 # or not in certain places. The _msgs() version of the function
1364 # expects warnings even if lexical ones are turned off, so by
1365 # making its which_func == 1, we can say we want warnings;
1366 # whereas the other one with the value 0, doesn't get them.
1367 for my $which_func (0, 1) {
1368 my $func = ($which_func)
1369 ? 'utf8n_to_uvchr_msgs'
1370 : 'utf8n_to_uvchr_error';
6aa905cf 1371
69485e19
KW
1372 # We classify the warnings into certain "interesting" types,
1373 # described later
1374 foreach my $warning_type (0..4) {
1375 next if $skip_most_tests && $warning_type != 1;
1376 foreach my $use_warn_flag (0, 1) {
3f055917 1377 if ($use_warn_flag) {
1d21b5e7 1378 next if $initially_overlong || $initially_orphan;
37657a5b
KW
1379
1380 # Since utf8n_to_uvchr_msgs() expects warnings even
1381 # when lexical ones are turned off, we can skip
1382 # testing it when they are turned on, with little
1383 # likelihood of missing an error case.
1384 next if $which_func;
3f055917
KW
1385 }
1386 else {
1387 next if $skip_most_tests;
1388 }
69485e19 1389
8f79178b
KW
1390 # Finally, here is the inner loop
1391
69485e19
KW
1392 my $this_utf8n_flag_to_warn = $utf8n_flag_to_warn;
1393 my $this_utf8n_flag_to_disallow = $utf8n_flag_to_disallow;
1394 my $this_uvchr_flag_to_warn = $uvchr_flag_to_warn;
1395 my $this_uvchr_flag_to_disallow = $uvchr_flag_to_disallow;
1396
1397 my $eval_warn;
1398 my $expect_regular_warnings;
1399 my $expect_warnings_for_malformed;
1400 my $expect_warnings_for_overflow;
1401
1402 if ($warning_type == 0) {
d22ec717 1403 $eval_warn = "use warnings";
69485e19 1404 $expect_regular_warnings = $use_warn_flag;
717dd9f9
KW
1405
1406 # We ordinarily expect overflow warnings here. But it
1407 # is somewhat more complicated, and the final
1408 # determination is deferred to one place in the filw
1409 # where we handle overflow.
69485e19 1410 $expect_warnings_for_overflow = 1;
717dd9f9
KW
1411
1412 # We would ordinarily expect malformed warnings in
1413 # this case, but not if malformations are allowed.
1414 $expect_warnings_for_malformed
1415 = $malformed_allow_type == 0;
6aa905cf 1416 }
69485e19
KW
1417 elsif ($warning_type == 1) {
1418 $eval_warn = "no warnings";
37657a5b
KW
1419 $expect_regular_warnings = $which_func;
1420 $expect_warnings_for_overflow = $which_func;
1421 $expect_warnings_for_malformed = $which_func;
69485e19
KW
1422 }
1423 elsif ($warning_type == 2) {
1424 $eval_warn = "no warnings; use warnings 'utf8'";
1425 $expect_regular_warnings = $use_warn_flag;
1426 $expect_warnings_for_overflow = 1;
717dd9f9
KW
1427 $expect_warnings_for_malformed
1428 = $malformed_allow_type == 0;
69485e19
KW
1429 }
1430 elsif ($warning_type == 3) {
1431 $eval_warn = "no warnings; use warnings"
e4e140b4
KW
1432 . " '$controlling_warning_category'";
1433 $expect_regular_warnings = $use_warn_flag;
69485e19 1434 $expect_warnings_for_overflow
e4e140b4 1435 = $controlling_warning_category eq 'non_unicode';
37657a5b 1436 $expect_warnings_for_malformed = $which_func;
69485e19
KW
1437 }
1438 elsif ($warning_type == 4) { # Like type 3, but uses the
d044b7a7 1439 # PERL_EXTENDED flags
69485e19 1440 # The complement flags were set up so that the
d044b7a7 1441 # PERL_EXTENDED flags have been tested that they don't
69485e19
KW
1442 # trigger wrongly for too small code points. And the
1443 # flags have been set up so that those small code
1444 # points are tested for being above Unicode. What's
1445 # left to test is that the large code points do
d044b7a7 1446 # trigger the PERL_EXTENDED flags.
6d736463 1447 next if ! requires_extended_utf8($allowed_uv);
e4e140b4 1448 next if $controlling_warning_category ne 'non_unicode';
69485e19
KW
1449 $eval_warn = "no warnings; use warnings 'non_unicode'";
1450 $expect_regular_warnings = 1;
1451 $expect_warnings_for_overflow = 1;
1452 $expect_warnings_for_malformed = 0;
d044b7a7 1453 $this_utf8n_flag_to_warn = $::UTF8_WARN_PERL_EXTENDED;
69485e19 1454 $this_utf8n_flag_to_disallow
d044b7a7
KW
1455 = $::UTF8_DISALLOW_PERL_EXTENDED;
1456 $this_uvchr_flag_to_warn
1457 = $::UNICODE_WARN_PERL_EXTENDED;
69485e19 1458 $this_uvchr_flag_to_disallow
d044b7a7 1459 = $::UNICODE_DISALLOW_PERL_EXTENDED;
601e92f1
KW
1460 }
1461 else {
69485e19 1462 die "Unexpected warning type '$warning_type'";
601e92f1
KW
1463 }
1464
69485e19
KW
1465 # We only need to test the case where all warnings are
1466 # enabled (type 0) to see if turning off the warning flag
1467 # causes things to not be output. If those pass, then
1468 # turning on some sub-category of warnings, or turning off
1469 # warnings altogether are extremely likely to not output
1470 # warnings either, given how the warnings subsystem is
1471 # supposed to work, and this file assumes it does work.
1472 next if $warning_type != 0 && ! $use_warn_flag;
1473
1474 # The convention is that the 'got' flag is the same value
1475 # as the disallow one. If this were violated, the tests
1476 # here should start failing.
1477 my $return_flag = $this_utf8n_flag_to_disallow;
1478
e4e140b4
KW
1479 # If we aren't expecting warnings/disallow for this, turn
1480 # on all the other flags. That makes sure that they all
1481 # are independent of this flag, and so we don't need to
1482 # test them individually.
57ff5f59
KW
1483 my $this_warning_flags
1484 = ($use_warn_flag)
1485 ? $this_utf8n_flag_to_warn
1486 : ($overlong_is_in_perl_extended_utf8
1487 ? ($utf8n_flag_to_warn_complement
1488 & ~$::UTF8_WARN_PERL_EXTENDED)
1489 : $utf8n_flag_to_warn_complement);
1490 my $this_disallow_flags
1491 = ($do_disallow)
1492 ? $this_utf8n_flag_to_disallow
1493 : ($overlong_is_in_perl_extended_utf8
1494 ? ($utf8n_flag_to_disallow_complement
1495 & ~$::UTF8_DISALLOW_PERL_EXTENDED)
1496 : $utf8n_flag_to_disallow_complement);
6aa905cf 1497 my $expected_uv = $allowed_uv;
717dd9f9 1498 my $this_uv_string = $uv_string;
6aa905cf 1499
4816e15f
KW
1500 my @expected_return_flags
1501 = @expected_malformation_return_flags;
69485e19 1502 my @expected_warnings;
4816e15f 1503 push @expected_warnings, @expected_malformation_warnings
69485e19
KW
1504 if $expect_warnings_for_malformed;
1505
69485e19
KW
1506 # The overflow malformation is done differently than other
1507 # malformations. It comes from manually typed tests in
1508 # the test array, but it also is above Unicode and uses
1509 # Perl extended UTF-8, so affects some of the flags being
1510 # tested. We now make it be treated like one of the other
1511 # generated malformations.
1512 if ($will_overflow) {
1513
1514 # An overflow is (way) above Unicode, and overrides
1515 # everything else.
1516 $expect_regular_warnings = 0;
1517
717dd9f9
KW
1518 # Earlier, we tentatively calculated whether this
1519 # should emit a message or not. It's tentative
1520 # because, even if we ordinarily would output it, we
1521 # don't if malformations are allowed -- except an
d044b7a7 1522 # overflow is also a SUPER and PERL_EXTENDED, and if
717dd9f9
KW
1523 # warnings for those are enabled, the overflow
1524 # warning does get raised.
1525 if ( $expect_warnings_for_overflow
1526 && ( $malformed_allow_type == 0
1527 || ( $this_warning_flags
1528 & ($::UTF8_WARN_SUPER
d044b7a7 1529 |$::UTF8_WARN_PERL_EXTENDED))))
717dd9f9 1530 {
4816e15f 1531 push @expected_warnings, $overflow_msg_pattern;
6aa905cf 1532 }
69485e19
KW
1533 }
1534
1535 # It may be that the malformations have shortened the
1536 # amount of input we look at so much that we can't tell
1537 # what the category the code point was in. Otherwise, set
1538 # up the expected return flags based on the warnings and
1539 # disallowments.
1540 if ($this_expected_len < $this_needed_to_discern_len) {
1541 $expect_regular_warnings = 0;
1542 }
1543 elsif ( ($this_warning_flags & $this_utf8n_flag_to_warn)
1544 || ( $this_disallow_flags
1545 & $this_utf8n_flag_to_disallow))
1546 {
1547 push @expected_return_flags, $return_flag;
1548 }
1549
1550 # Finish setting up the expected warning.
1551 if ($expect_regular_warnings) {
1552
1553 # So far the array contains warnings generated by
1554 # malformations. Add the expected regular one.
57ff5f59 1555 unshift @expected_warnings, $this_cp_message_qr;
69485e19
KW
1556
1557 # But it may need to be modified, because either of
1558 # these malformations means we can't determine the
1559 # expected code point.
57ff5f59
KW
1560 if ( $short || $unexpected_noncont
1561 || $dont_use_overlong_cp)
1562 {
69485e19
KW
1563 my $first_byte = substr($this_bytes, 0, 1);
1564 $expected_warnings[0] = display_bytes(
1565 substr($this_bytes, 0, $this_expected_len));
1566 $expected_warnings[0]
1567 = qr/[Aa]\Qny UTF-8 sequence that starts with\E
1568 \Q $expected_warnings[0]\E
57ff5f59 1569 \Q $this_non_cp_trailing_text\E/x;
69485e19
KW
1570 }
1571 }
6aa905cf 1572
717dd9f9
KW
1573 # Is effectively disallowed if we've set up a malformation
1574 # (unless malformations are allowed), even if the flag
1575 # indicates it is allowed. Fix up test name to indicate
1576 # this as well
1577 my $disallowed = 0;
1578 if ( $this_disallow_flags & $this_utf8n_flag_to_disallow
1579 && $this_expected_len >= $this_needed_to_discern_len)
1580 {
1581 $disallowed = 1;
1582 }
1583 if ($malformations_name) {
1584 if ($malformed_allow_type == 0) {
1585 $disallowed = 1;
1586 }
1587 elsif ($malformed_allow_type == 1) {
1588
1589 # Even if allowed, the malformation returns the
1590 # REPLACEMENT CHARACTER.
1591 $expected_uv = 0xFFFD;
1592 $this_uv_string = "0xFFFD"
1593 }
1594 }
1595
37657a5b
KW
1596 my $this_name = "$func() $testname: ";
1597 my @scratch_expected_return_flags = @expected_return_flags;
a93dd121
KW
1598 if (! $initially_malformed) {
1599 $this_name .= ($disallowed)
1600 ? 'disallowed, '
1601 : 'allowed, ';
1602 }
1603 $this_name .= "$eval_warn";
69485e19
KW
1604 $this_name .= ", " . (( $this_warning_flags
1605 & $this_utf8n_flag_to_warn)
1606 ? 'with flag for raising warnings'
1607 : 'no flag for raising warnings');
1608 $this_name .= $malformations_name;
db0f09e6 1609
8f79178b 1610 # Do the actual test using an eval
9cdc3054 1611 undef @warnings_gotten;
6aa905cf 1612 my $ret_ref;
717dd9f9
KW
1613 my $this_flags
1614 = $allow_flags|$this_warning_flags|$this_disallow_flags;
6aa905cf 1615 my $eval_text = "$eval_warn; \$ret_ref"
37657a5b 1616 . " = test_$func("
69485e19 1617 . "'$this_bytes', $this_length, $this_flags)";
6aa905cf 1618 eval "$eval_text";
1a35ea23 1619 if (! ok ($@ eq "", "$this_name: eval succeeded"))
6aa905cf 1620 {
a8ee5133
KW
1621 diag "\$@='$@'; call was: "
1622 . utf8n_display_call($eval_text);
6aa905cf
KW
1623 next;
1624 }
37657a5b 1625
6aa905cf 1626 if ($disallowed) {
d402d77f 1627 is($ret_ref->[0], 0, " And returns 0")
a8ee5133 1628 or diag "Call was: " . utf8n_display_call($eval_text);
6aa905cf
KW
1629 }
1630 else {
1631 is($ret_ref->[0], $expected_uv,
d402d77f 1632 " And returns expected uv: "
717dd9f9 1633 . $this_uv_string)
a8ee5133 1634 or diag "Call was: " . utf8n_display_call($eval_text);
6aa905cf
KW
1635 }
1636 is($ret_ref->[1], $this_expected_len,
d402d77f 1637 " And returns expected length:"
6aa905cf 1638 . " $this_expected_len")
a8ee5133 1639 or diag "Call was: " . utf8n_display_call($eval_text);
6aa905cf 1640
9cdc3054 1641 my $returned_flags = $ret_ref->[2];
6aa905cf 1642
37657a5b
KW
1643 for (my $i = @scratch_expected_return_flags - 1;
1644 $i >= 0;
1645 $i--)
1646 {
1647 if ($scratch_expected_return_flags[$i] & $returned_flags)
1648 {
1649 if ($scratch_expected_return_flags[$i]
1650 == $::UTF8_GOT_PERL_EXTENDED)
1651 {
1652 pass(" Expected and got return flag for"
1653 . " PERL_EXTENDED");
1654 }
1655 # The first entries in this are
1656 # malformations
1657 elsif ($i > @malformation_names - 1) {
1658 pass(" Expected and got return flag"
1659 . " for " . $controlling_warning_category);
1660 }
1661 else {
1662 pass(" Expected and got return flag for "
1663 . $malformation_names[$i]
1664 . " malformation");
1665 }
1666 $returned_flags
1667 &= ~$scratch_expected_return_flags[$i];
1668 splice @scratch_expected_return_flags, $i, 1;
1669 }
6aa905cf 1670 }
6aa905cf 1671
23038144
KW
1672 if (! is($returned_flags, 0,
1673 " Got no unexpected return flags"))
1674 {
1675 diag "The unexpected flags gotten were: "
5722c46d
KW
1676 . (flags_to_text($returned_flags,
1677 \@utf8n_flags_to_text)
69485e19
KW
1678 # We strip off any prefixes from the flag
1679 # names
1680 =~ s/ \b [A-Z] _ //xgr);
23038144
KW
1681 diag "Call was: " . utf8n_display_call($eval_text);
1682 }
1683
1684 if (! is (scalar @scratch_expected_return_flags, 0,
1685 " Got all expected return flags"))
1686 {
1687 diag "The expected flags not gotten were: "
69485e19 1688 . (flags_to_text(eval join("|",
37657a5b 1689 @scratch_expected_return_flags),
69485e19
KW
1690 \@utf8n_flags_to_text)
1691 # We strip off any prefixes from the flag
1692 # names
5722c46d 1693 =~ s/ \b [A-Z] _ //xgr);
23038144
KW
1694 diag "Call was: " . utf8n_display_call($eval_text);
1695 }
9cdc3054 1696
37657a5b
KW
1697 if ($which_func) {
1698 my @returned_warnings;
1699 for my $element_ref (@{$ret_ref->[3]}) {
1700 push @returned_warnings, $element_ref->{'text'};
1701 my $text = $element_ref->{'text'};
1702 my $flag = $element_ref->{'flag_bit'};
1703 my $category = $element_ref->{'warning_category'};
1704
1705 if (! ok(($flag & ($flag-1)) == 0,
1706 "flag for returned msg is a single bit"))
1707 {
1708 diag sprintf("flags are %x; msg=%s", $flag, $text);
1709 }
1710 else {
1711 if (grep { $_ == $flag } @expected_return_flags) {
1712 pass("flag for returned msg is expected");
1713 }
1714 else {
1715 fail("flag for returned msg is expected: "
1716 . flags_to_text($flag, \@utf8n_flags_to_text));
1717 }
1718 }
1719
1720 # In perl space, don't know the category numbers
1721 isnt($category, 0,
1722 "returned category for msg isn't 0");
1723 }
1724
1725 ok(@warnings_gotten == 0, "$func raised no warnings;"
1726 . " the next tests are for ones in the returned"
1727 . " variable")
1728 or diag join "\n", "The unexpected warnings were:",
1729 @warnings_gotten;
1730 @warnings_gotten = @returned_warnings;
1731 }
1732
69485e19
KW
1733 do_warnings_test(@expected_warnings)
1734 or diag "Call was: " . utf8n_display_call($eval_text);
1735 undef @warnings_gotten;
6aa905cf
KW
1736
1737 # Check CHECK_ONLY results when the input is
1738 # disallowed. Do this when actually disallowed,
37657a5b
KW
1739 # not just when the $this_disallow_flags is set. We only
1740 # test once utf8n_to_uvchr_msgs() with this.
1741 if ( $disallowed
1742 && ($which_func == 0 || ! $tested_CHECK_ONLY))
1743 {
1744 $tested_CHECK_ONLY = 1;
69485e19 1745 my $this_flags = $this_disallow_flags|$::UTF8_CHECK_ONLY;
d22ec717 1746 my $eval_text = "use warnings; \$ret_ref ="
37657a5b 1747 . " test_$func('"
69485e19
KW
1748 . "$this_bytes', $this_length,"
1749 . " $this_flags)";
1750 eval $eval_text;
1a35ea23 1751 if (! ok ($@ eq "",
a8ee5133
KW
1752 " And eval succeeded with CHECK_ONLY"))
1753 {
1754 diag "\$@='$@'; Call was: "
1755 . utf8n_display_call($eval_text);
1756 next;
1757 }
d402d77f 1758 is($ret_ref->[0], 0, " CHECK_ONLY: Returns 0")
a8ee5133 1759 or diag "Call was: " . utf8n_display_call($eval_text);
6aa905cf 1760 is($ret_ref->[1], -1,
d402d77f 1761 " CHECK_ONLY: returns -1 for length")
a8ee5133 1762 or diag "Call was: " . utf8n_display_call($eval_text);
9cdc3054 1763 if (! is(scalar @warnings_gotten, 0,
d402d77f 1764 " CHECK_ONLY: no warnings generated"))
6aa905cf 1765 {
a8ee5133 1766 diag "Call was: " . utf8n_display_call($eval_text);
9cdc3054 1767 output_warnings(@warnings_gotten);
6aa905cf
KW
1768 }
1769 }
1770
1771 # Now repeat some of the above, but for
1772 # uvchr_to_utf8_flags(). Since this comes from an
8f79178b
KW
1773 # existing code point, it hasn't overflowed, and isn't
1774 # malformed.
69485e19 1775 next if @malformation_names;
37657a5b 1776 next if $which_func;
69485e19
KW
1777
1778 $this_warning_flags = ($use_warn_flag)
1779 ? $this_uvchr_flag_to_warn
1780 : 0;
1781 $this_disallow_flags = ($do_disallow)
1782 ? $this_uvchr_flag_to_disallow
1783 : 0;
1784
1785 $disallowed = $this_disallow_flags
1786 & $this_uvchr_flag_to_disallow;
1787 $this_name .= ", " . (( $this_warning_flags
1788 & $this_utf8n_flag_to_warn)
1789 ? 'with flag for raising warnings'
1790 : 'no flag for raising warnings');
6aa905cf
KW
1791
1792 $this_name = "uvchr_to_utf8_flags() $testname: "
69485e19 1793 . (($disallowed)
6aa905cf 1794 ? 'disallowed'
6aa905cf
KW
1795 : 'allowed');
1796 $this_name .= ", $eval_warn";
69485e19
KW
1797 $this_name .= ", " . (( $this_warning_flags
1798 & $this_uvchr_flag_to_warn)
6aa905cf
KW
1799 ? 'with warning flag'
1800 : 'no warning flag');
1801
9cdc3054 1802 undef @warnings_gotten;
6aa905cf 1803 my $ret;
69485e19 1804 $this_flags = $this_warning_flags|$this_disallow_flags;
6aa905cf
KW
1805 $eval_text = "$eval_warn; \$ret ="
1806 . " test_uvchr_to_utf8_flags("
d884ea32 1807 . "$allowed_uv, $this_flags)";
6aa905cf 1808 eval "$eval_text";
1a35ea23 1809 if (! ok ($@ eq "", "$this_name: eval succeeded"))
6aa905cf 1810 {
d884ea32
KW
1811 diag "\$@='$@'; call was: "
1812 . uvchr_display_call($eval_text);
6aa905cf
KW
1813 next;
1814 }
1815 if ($disallowed) {
d402d77f 1816 is($ret, undef, " And returns undef")
d884ea32 1817 or diag "Call was: " . uvchr_display_call($eval_text);
6aa905cf
KW
1818 }
1819 else {
d402d77f 1820 is($ret, $this_bytes, " And returns expected string")
d884ea32 1821 or diag "Call was: " . uvchr_display_call($eval_text);
6aa905cf 1822 }
69485e19
KW
1823
1824 do_warnings_test(@expected_warnings)
1825 or diag "Call was: " . uvchr_display_call($eval_text);
6aa905cf
KW
1826 }
1827 }
717dd9f9 1828 }
6aa905cf
KW
1829 }
1830 }
8f79178b 1831 }
6aa905cf 1832 }
e0803729 1833 }
6aa905cf
KW
1834}
1835
1836done_testing;