This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
utf8n_to_uvchr: U+ should be for only Unicode code points
[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
8f79178b
KW
4# It tests various code points that are "problematic", and verifies that the
5# correct warnings/flags etc are generated when using them. It also takes the
6# UTF-8 for some of them and perturbs it to be malformed in various ways, and
7# tests that this gets appropriately detected.
6aa905cf
KW
8
9use strict;
10use Test::More;
11
12BEGIN {
13 use_ok('XS::APItest');
14 require 'charset_tools.pl';
15 require './t/utf8_setup.pl';
16};
17
18$|=1;
19
20no warnings 'deprecated'; # Some of the below are above IV_MAX on 32 bit
21 # machines, and that is tested elsewhere
22
23use XS::APItest;
24use Data::Dumper;
25
9cdc3054 26my @warnings_gotten;
6aa905cf
KW
27
28use warnings 'utf8';
8d6f1506
KW
29local $SIG{__WARN__} = sub { my @copy = @_;
30 push @warnings_gotten, map { chomp; $_ } @copy;
31 };
c7f9e4bb
KW
32my @tests;
33{
34 no warnings qw(portable overflow);
35 @tests = (
8f79178b
KW
36 # $testname,
37 # $bytes, UTF-8 string
38 # $allowed_uv, code point $bytes evaluates to; -1 if
39 # overflows
40 # $needed_to_discern_len optional, how long an initial substring do
41 # we need to tell that the string must be for
42 # a code point in the category it falls in,
43 # like being a surrogate; 0 indicates we need
44 # the whole string. Some categories have a
45 # default that is used if this is omitted.
46 [ "lowest surrogate",
47 (isASCII) ? "\xed\xa0\x80" : I8_to_native("\xf1\xb6\xa0\xa0"),
48 0xD800,
49 ],
50 [ "a middle surrogate",
51 (isASCII) ? "\xed\xa4\x8d" : I8_to_native("\xf1\xb6\xa8\xad"),
52 0xD90D,
53 ],
54 [ "highest surrogate",
55 (isASCII) ? "\xed\xbf\xbf" : I8_to_native("\xf1\xb7\xbf\xbf"),
56 0xDFFF,
57 ],
8f79178b
KW
58 [ "first of 32 consecutive non-character code points",
59 (isASCII) ? "\xef\xb7\x90" : I8_to_native("\xf1\xbf\xae\xb0"),
60 0xFDD0,
61 ],
62 [ "a mid non-character code point of the 32 consecutive ones",
63 (isASCII) ? "\xef\xb7\xa0" : I8_to_native("\xf1\xbf\xaf\xa0"),
64 0xFDE0,
65 ],
66 [ "final of 32 consecutive non-character code points",
67 (isASCII) ? "\xef\xb7\xaf" : I8_to_native("\xf1\xbf\xaf\xaf"),
68 0xFDEF,
69 ],
70 [ "non-character code point U+FFFE",
71 (isASCII) ? "\xef\xbf\xbe" : I8_to_native("\xf1\xbf\xbf\xbe"),
72 0xFFFE,
73 ],
74 [ "non-character code point U+FFFF",
75 (isASCII) ? "\xef\xbf\xbf" : I8_to_native("\xf1\xbf\xbf\xbf"),
76 0xFFFF,
77 ],
78 [ "non-character code point U+1FFFE",
79 (isASCII) ? "\xf0\x9f\xbf\xbe" : I8_to_native("\xf3\xbf\xbf\xbe"),
80 0x1FFFE,
81 ],
82 [ "non-character code point U+1FFFF",
83 (isASCII) ? "\xf0\x9f\xbf\xbf" : I8_to_native("\xf3\xbf\xbf\xbf"),
84 0x1FFFF,
85 ],
86 [ "non-character code point U+2FFFE",
87 (isASCII) ? "\xf0\xaf\xbf\xbe" : I8_to_native("\xf5\xbf\xbf\xbe"),
88 0x2FFFE,
89 ],
90 [ "non-character code point U+2FFFF",
91 (isASCII) ? "\xf0\xaf\xbf\xbf" : I8_to_native("\xf5\xbf\xbf\xbf"),
92 0x2FFFF,
93 ],
94 [ "non-character code point U+3FFFE",
95 (isASCII) ? "\xf0\xbf\xbf\xbe" : I8_to_native("\xf7\xbf\xbf\xbe"),
96 0x3FFFE,
97 ],
98 [ "non-character code point U+3FFFF",
99 (isASCII) ? "\xf0\xbf\xbf\xbf" : I8_to_native("\xf7\xbf\xbf\xbf"),
100 0x3FFFF,
101 ],
102 [ "non-character code point U+4FFFE",
103 (isASCII)
104 ? "\xf1\x8f\xbf\xbe"
105 : I8_to_native("\xf8\xa9\xbf\xbf\xbe"),
106 0x4FFFE,
107 ],
108 [ "non-character code point U+4FFFF",
109 (isASCII)
110 ? "\xf1\x8f\xbf\xbf"
111 : I8_to_native("\xf8\xa9\xbf\xbf\xbf"),
112 0x4FFFF,
113 ],
114 [ "non-character code point U+5FFFE",
115 (isASCII)
116 ? "\xf1\x9f\xbf\xbe"
117 : I8_to_native("\xf8\xab\xbf\xbf\xbe"),
118 0x5FFFE,
119 ],
120 [ "non-character code point U+5FFFF",
121 (isASCII)
122 ? "\xf1\x9f\xbf\xbf"
123 : I8_to_native("\xf8\xab\xbf\xbf\xbf"),
124 0x5FFFF,
125 ],
126 [ "non-character code point U+6FFFE",
127 (isASCII)
128 ? "\xf1\xaf\xbf\xbe"
129 : I8_to_native("\xf8\xad\xbf\xbf\xbe"),
130 0x6FFFE,
131 ],
132 [ "non-character code point U+6FFFF",
133 (isASCII)
134 ? "\xf1\xaf\xbf\xbf"
135 : I8_to_native("\xf8\xad\xbf\xbf\xbf"),
136 0x6FFFF,
137 ],
138 [ "non-character code point U+7FFFE",
139 (isASCII)
140 ? "\xf1\xbf\xbf\xbe"
141 : I8_to_native("\xf8\xaf\xbf\xbf\xbe"),
142 0x7FFFE,
143 ],
144 [ "non-character code point U+7FFFF",
145 (isASCII)
146 ? "\xf1\xbf\xbf\xbf"
147 : I8_to_native("\xf8\xaf\xbf\xbf\xbf"),
148 0x7FFFF,
149 ],
150 [ "non-character code point U+8FFFE",
151 (isASCII)
152 ? "\xf2\x8f\xbf\xbe"
153 : I8_to_native("\xf8\xb1\xbf\xbf\xbe"),
154 0x8FFFE,
155 ],
156 [ "non-character code point U+8FFFF",
157 (isASCII)
158 ? "\xf2\x8f\xbf\xbf"
159 : I8_to_native("\xf8\xb1\xbf\xbf\xbf"),
160 0x8FFFF,
161 ],
162 [ "non-character code point U+9FFFE",
163 (isASCII)
164 ? "\xf2\x9f\xbf\xbe"
165 : I8_to_native("\xf8\xb3\xbf\xbf\xbe"),
166 0x9FFFE,
167 ],
168 [ "non-character code point U+9FFFF",
169 (isASCII)
170 ? "\xf2\x9f\xbf\xbf"
171 : I8_to_native("\xf8\xb3\xbf\xbf\xbf"),
172 0x9FFFF,
173 ],
174 [ "non-character code point U+AFFFE",
175 (isASCII)
176 ? "\xf2\xaf\xbf\xbe"
177 : I8_to_native("\xf8\xb5\xbf\xbf\xbe"),
178 0xAFFFE,
179 ],
180 [ "non-character code point U+AFFFF",
181 (isASCII)
182 ? "\xf2\xaf\xbf\xbf"
183 : I8_to_native("\xf8\xb5\xbf\xbf\xbf"),
184 0xAFFFF,
185 ],
186 [ "non-character code point U+BFFFE",
187 (isASCII)
188 ? "\xf2\xbf\xbf\xbe"
189 : I8_to_native("\xf8\xb7\xbf\xbf\xbe"),
190 0xBFFFE,
191 ],
192 [ "non-character code point U+BFFFF",
193 (isASCII)
194 ? "\xf2\xbf\xbf\xbf"
195 : I8_to_native("\xf8\xb7\xbf\xbf\xbf"),
196 0xBFFFF,
197 ],
198 [ "non-character code point U+CFFFE",
199 (isASCII)
200 ? "\xf3\x8f\xbf\xbe"
201 : I8_to_native("\xf8\xb9\xbf\xbf\xbe"),
202 0xCFFFE,
203 ],
204 [ "non-character code point U+CFFFF",
205 (isASCII)
206 ? "\xf3\x8f\xbf\xbf"
207 : I8_to_native("\xf8\xb9\xbf\xbf\xbf"),
208 0xCFFFF,
209 ],
210 [ "non-character code point U+DFFFE",
211 (isASCII)
212 ? "\xf3\x9f\xbf\xbe"
213 : I8_to_native("\xf8\xbb\xbf\xbf\xbe"),
214 0xDFFFE,
215 ],
216 [ "non-character code point U+DFFFF",
217 (isASCII)
218 ? "\xf3\x9f\xbf\xbf"
219 : I8_to_native("\xf8\xbb\xbf\xbf\xbf"),
220 0xDFFFF,
221 ],
222 [ "non-character code point U+EFFFE",
223 (isASCII)
224 ? "\xf3\xaf\xbf\xbe"
225 : I8_to_native("\xf8\xbd\xbf\xbf\xbe"),
226 0xEFFFE,
227 ],
228 [ "non-character code point U+EFFFF",
229 (isASCII)
230 ? "\xf3\xaf\xbf\xbf"
231 : I8_to_native("\xf8\xbd\xbf\xbf\xbf"),
232 0xEFFFF,
233 ],
234 [ "non-character code point U+FFFFE",
235 (isASCII)
236 ? "\xf3\xbf\xbf\xbe"
237 : I8_to_native("\xf8\xbf\xbf\xbf\xbe"),
238 0xFFFFE,
239 ],
240 [ "non-character code point U+FFFFF",
241 (isASCII)
242 ? "\xf3\xbf\xbf\xbf"
243 : I8_to_native("\xf8\xbf\xbf\xbf\xbf"),
244 0xFFFFF,
245 ],
246 [ "non-character code point U+10FFFE",
247 (isASCII)
248 ? "\xf4\x8f\xbf\xbe"
249 : I8_to_native("\xf9\xa1\xbf\xbf\xbe"),
250 0x10FFFE,
251 ],
252 [ "non-character code point U+10FFFF",
253 (isASCII)
254 ? "\xf4\x8f\xbf\xbf"
255 : I8_to_native("\xf9\xa1\xbf\xbf\xbf"),
256 0x10FFFF,
257 ],
7c9f4ec6
KW
258 [ "first non_unicode",
259 (isASCII)
260 ? "\xf4\x90\x80\x80"
261 : I8_to_native("\xf9\xa2\xa0\xa0\xa0"),
262 0x110000,
263 2,
264 ],
265 [ "non_unicode whose first byte tells that",
266 (isASCII)
267 ? "\xf5\x80\x80\x80"
268 : I8_to_native("\xfa\xa0\xa0\xa0\xa0"),
269 (isASCII) ? 0x140000 : 0x200000,
270 1,
271 ],
48ceb60a 272 [ "lowest 32 bit code point",
8f79178b
KW
273 (isASCII)
274 ? "\xfe\x82\x80\x80\x80\x80\x80"
275 : I8_to_native(
276 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa2\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b
KW
277 0x80000000,
278 (isASCII) ? 1 : 8,
279 ],
280 [ "highest 32 bit code point",
281 (isASCII)
282 ? "\xfe\x83\xbf\xbf\xbf\xbf\xbf"
283 : I8_to_native(
284 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa3\xbf\xbf\xbf\xbf\xbf\xbf"),
285 0xFFFFFFFF,
286 (isASCII) ? 1 : 8,
287 ],
f4da64d4
KW
288 [ "Lowest 33 bit code point",
289 (isASCII)
290 ? "\xfe\x84\x80\x80\x80\x80\x80"
291 : I8_to_native(
292 "\xff\xa0\xa0\xa0\xa0\xa0\xa0\xa4\xa0\xa0\xa0\xa0\xa0\xa0"),
293 ($::is64bit) ? 0x100000000 : -1, # Overflows on 32-bit systems
294 (isASCII && ! $::is64bit) ? 2 : 1,
295 ],
8f79178b
KW
296 );
297
298 if (! $::is64bit) {
299 if (isASCII) {
8f79178b 300 push @tests,
c81d4d83
KW
301 [ "overflow that old algorithm failed to detect",
302 "\xfe\x86\x80\x80\x80\x80\x80",
303 -1,
304 2,
8f79178b
KW
305 ];
306 }
307 }
c81d4d83
KW
308
309 if ($::is64bit) {
ba627a0b
KW
310 push @tests,
311 [ "highest 64 bit code point",
312 (isASCII)
313 ? "\xff\x80\x8f\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"
314 : I8_to_native(
315 "\xff\xaf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf"),
316 0xFFFFFFFFFFFFFFFF,
317 (isASCII) ? 1 : 2,
318 ],
319 [ "first 65 bit code point",
320 (isASCII)
321 ? "\xff\x80\x9f\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80"
322 : I8_to_native(
323 "\xff\xb0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
324 -1,
325 (isASCII) ? 3 : 2,
326 ];
c81d4d83
KW
327 if (isASCII) {
328 push @tests,
f6104ed7
KW
329 [ "Lowest code point requiring 13 bytes to represent",
330 "\xff\x80\x80\x80\x80\x80\x81\x80\x80\x80\x80\x80\x80",
331 0x1000000000,
332 1,
333 ],
c81d4d83
KW
334 [ "overflow that old algorithm failed to detect",
335 "\xff\x80\x90\x90\x90\xbf\xbf\xbf\xbf\xbf\xbf\xbf\xbf",
336 -1,
337 3,
338 ];
339 }
340 else {
8f79178b
KW
341 push @tests, # These could falsely show wrongly in a naive
342 # implementation
343 [ "requires at least 32 bits",
344 I8_to_native(
6aa905cf 345 "\xff\xa0\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b
KW
346 0x800000000,
347 7,
348 ],
349 [ "requires at least 32 bits",
350 I8_to_native(
6aa905cf 351 "\xff\xa0\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b
KW
352 0x10000000000,
353 6,
354 ],
355 [ "requires at least 32 bits",
356 I8_to_native(
6aa905cf 357 "\xff\xa0\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b
KW
358 0x200000000000,
359 5,
360 ],
361 [ "requires at least 32 bits",
362 I8_to_native(
6aa905cf 363 "\xff\xa0\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b
KW
364 0x4000000000000,
365 4,
366 ],
367 [ "requires at least 32 bits",
368 I8_to_native(
6aa905cf 369 "\xff\xa0\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b
KW
370 0x80000000000000,
371 3,
372 ],
373 [ "requires at least 32 bits",
374 I8_to_native(
6aa905cf 375 "\xff\xa1\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0"),
8f79178b
KW
376 0x1000000000000000,
377 2,
378 ];
379 }
6aa905cf 380 }
c7f9e4bb 381}
6aa905cf 382
5722c46d
KW
383sub flags_to_text($$)
384{
385 my ($flags, $flags_to_text_ref) = @_;
386
387 # Returns a string containing a mnemonic representation of the bits that
388 # are set in the $flags. These are assumed to be flag bits. The return
389 # looks like "FOO|BAR|BAZ". The second parameter is a reference to an
390 # array that gives the textual representation of all the possible flags.
391 # Element 0 is the text for the bit 0 flag; element 1 for bit 1; .... If
392 # no bits at all are set the string "0" is returned;
393
394 my @flag_text;
395 my $shift = 0;
396
397 return "0" if $flags == 0;
398
399 while ($flags) {
400 #diag sprintf "%x", $flags;
401 if ($flags & 1) {
402 push @flag_text, $flags_to_text_ref->[$shift];
403 }
404 $shift++;
405 $flags >>= 1;
406 }
407
408 return join "|", @flag_text;
409}
410
411# Possible flag returns from utf8n_to_uvchr_error(). These should have G_,
412# instead of A_, D_, but the prefixes will be used in a a later commit, so
413# minimize churn by having them here.
414my @utf8n_flags_to_text = ( qw(
415 A_EMPTY
416 A_CONTINUATION
417 A_NON_CONTINUATION
418 A_SHORT
419 A_LONG
420 A_LONG_AND_ITS_VALUE
421 PLACEHOLDER
422 A_OVERFLOW
423 D_SURROGATE
424 W_SURROGATE
425 D_NONCHAR
426 W_NONCHAR
427 D_SUPER
428 W_SUPER
429 D_ABOVE_31_BIT
430 W_ABOVE_31_BIT
431 CHECK_ONLY
432 NO_CONFIDENCE_IN_CURLEN_
433 ) );
434
a8ee5133
KW
435sub utf8n_display_call($)
436{
437 # Converts an eval string that calls test_utf8n_to_uvchr into a more human
438 # readable form, and returns it. Doesn't work if the byte string contains
439 # an apostrophe. The return will look something like:
440 # test_utf8n_to_uvchr_error('$bytes', $length, $flags)
441 #diag $_[0];
442
443 $_[0] =~ / ^ ( [^(]* \( ) ' ( [^']*? ) ' ( .+ , \D* ) ( \d+ ) \) $ /x;
444 my $text1 = $1; # Everything before the byte string
445 my $bytes = $2;
446 my $text2 = $3; # Includes the length
447 my $flags = $4;
448
449 return $text1
450 . display_bytes($bytes)
451 . $text2
452 . flags_to_text($flags, \@utf8n_flags_to_text)
453 . ')';
454}
455
d884ea32
KW
456sub uvchr_display_call($)
457{
458 # Converts an eval string that calls test_uvchr_to_utf8 into a more human
459 # readable form, and returns it. The return will look something like:
460 # test_uvchr_to_utf8n_flags($uv, $flags)
461 #diag $_[0];
462
463 my @flags_to_text = ( qw(
464 W_SURROGATE
465 W_NONCHAR
466 W_SUPER
467 W_ABOVE_31_BIT
468 D_SURROGATE
469 D_NONCHAR
470 D_SUPER
471 D_ABOVE_31_BIT
472 ) );
473
474 $_[0] =~ / ^ ( [^(]* \( ) ( \d+ ) , \s* ( \d+ ) \) $ /x;
475 my $text = $1;
476 my $cp = sprintf "%X", $2;
477 my $flags = $3;
478
479 return "${text}0x$cp, " . flags_to_text($flags, \@flags_to_text) . ')';
480}
481
69485e19
KW
482sub do_warnings_test(@)
483{
484 my @expected_warnings = @_;
485
486 # Compares the input expected warnings array with @warnings_gotten,
487 # generating a pass for each found, removing it from @warnings_gotten.
488 # Any discrepancies generate test failures. Returns TRUE if no
489 # discrepcancies; otherwise FALSE.
490
491 my $succeeded = 1;
492
493 if (@expected_warnings == 0) {
494 if (! is(@warnings_gotten, 0, " Expected and got no warnings")) {
495 output_warnings(@warnings_gotten);
496 $succeeded = 0;
497 }
498 return $succeeded;
499 }
500
501 # Check that we got all the expected warnings,
502 # removing each one found
503 WARNING:
504 foreach my $expected (@expected_warnings) {
505 foreach (my $i = 0; $i < @warnings_gotten; $i++) {
506 if ($warnings_gotten[$i] =~ $expected) {
507 pass(" Expected and got warning: "
508 . " $warnings_gotten[$i]");
509 splice @warnings_gotten, $i, 1;
510 next WARNING;
511 }
512 }
513 fail(" Expected a warning that matches "
514 . $expected . " but didn't get it");
515 $succeeded = 0;
516 }
517
518 if (! is(@warnings_gotten, 0, " Got no unexpected warnings")) {
519 output_warnings(@warnings_gotten);
520 $succeeded = 0;
521 }
522
523 return $succeeded;
524}
525
6aa905cf
KW
526# This test is split into this number of files.
527my $num_test_files = $ENV{TEST_JOBS} || 1;
528$num_test_files = 10 if $num_test_files > 10;
529
530my $test_count = -1;
531foreach my $test (@tests) {
532 $test_count++;
533 next if $test_count % $num_test_files != $::TEST_CHUNK;
534
d42d7565 535 my ($testname, $bytes, $allowed_uv, $needed_to_discern_len) = @$test;
6aa905cf
KW
536
537 my $length = length $bytes;
04f42bf6 538 my $will_overflow = $allowed_uv < 0;
6aa905cf 539
2c511c58
KW
540 my $uv_string = sprintf(($allowed_uv < 0x100) ? "%02X" : "%04X", $allowed_uv);
541
d42d7565 542 my $controlling_warning_category;
af816908
KW
543 my $utf8n_flag_to_warn;
544 my $utf8n_flag_to_disallow;
545 my $uvchr_flag_to_warn;
546 my $uvchr_flag_to_disallow;
b7e1f4b2 547
e4e140b4
KW
548 # We want to test that the independent flags are actually independent.
549 # For example, that a surrogate doesn't trigger a non-character warning,
550 # and conversely, turning off an above-Unicode flag doesn't suppress a
551 # surrogate warning. Earlier versions of this file used nested loops to
552 # test all possible combinations. But that creates lots of tests, making
553 # this run too long. What is now done instead is to use the complement of
554 # the category we are testing to greatly reduce the combinatorial
555 # explosion. For example, if we have a surrogate and we aren't expecting
556 # a warning about it, we set all the flags for non-surrogates to raise
557 # warnings. If one shows up, it indicates the flags aren't independent.
558 my $utf8n_flag_to_warn_complement;
559 my $utf8n_flag_to_disallow_complement;
560 my $uvchr_flag_to_warn_complement;
561 my $uvchr_flag_to_disallow_complement;
562
bf422d6a
KW
563 # Many of the code points being tested are middling in that if code point
564 # edge cases work, these are very likely to as well. Because this test
565 # file takes a while to execute, we skip testing the edge effects of code
566 # points deemed middling, while testing their basics and continuing to
567 # fully test the non-middling code points.
568 my $skip_most_tests = 0;
569
601e92f1
KW
570 my $cp_message_qr; # Pattern that matches the message raised when
571 # that message contains the problematic code
572 # point. The message is the same (currently) both
573 # when going from/to utf8.
574 my $non_cp_trailing_text; # The suffix text when the message doesn't
575 # contain a code point. (This is a result of
576 # some sort of malformation that means we
577 # can't get an exact code poin
578
af816908
KW
579 if ($will_overflow || $allowed_uv > 0x10FFFF) {
580
e4e140b4 581 # Set the SUPER flags; later, we test for ABOVE_31_BIT as well.
af816908
KW
582 $utf8n_flag_to_warn = $::UTF8_WARN_SUPER;
583 $utf8n_flag_to_disallow = $::UTF8_DISALLOW_SUPER;
584 $uvchr_flag_to_warn = $::UNICODE_WARN_SUPER;
585 $uvchr_flag_to_disallow = $::UNICODE_DISALLOW_SUPER;;
586
e4e140b4
KW
587 # Below, we add the flags for non-above-31 bit to the code points that
588 # don't fit that category. Special tests are done for this category
589 # in the inner loop.
590 $utf8n_flag_to_warn_complement = $::UTF8_WARN_NONCHAR
591 |$::UTF8_WARN_SURROGATE;
592 $utf8n_flag_to_disallow_complement = $::UTF8_DISALLOW_NONCHAR
593 |$::UTF8_DISALLOW_SURROGATE;
594 $uvchr_flag_to_warn_complement = $::UNICODE_WARN_NONCHAR
595 |$::UNICODE_WARN_SURROGATE;
596 $uvchr_flag_to_disallow_complement = $::UNICODE_DISALLOW_NONCHAR
597 |$::UNICODE_DISALLOW_SURROGATE;
598 $controlling_warning_category = 'non_unicode';
599
8f79178b 600 if ($will_overflow) { # This is realy a malformation
69485e19 601 $non_cp_trailing_text = "if you see this, there is an error";
601e92f1 602 $cp_message_qr = qr/\Q$non_cp_trailing_text\E/;
af816908
KW
603 }
604 elsif ($allowed_uv > 0x7FFFFFFF) {
601e92f1
KW
605 $cp_message_qr = qr/\QCode point 0x$uv_string is not Unicode,\E
606 \Q and not portable\E/x;
607 $non_cp_trailing_text = "is for a non-Unicode code point, and is not portable";
af816908 608 }
601e92f1
KW
609 else {
610 $cp_message_qr = qr/\QCode point 0x$uv_string is not Unicode,\E
611 \Q may not be portable\E/x;
612 $non_cp_trailing_text = "is for a non-Unicode code point, may not"
613 . " be portable";
e4e140b4
KW
614 $utf8n_flag_to_warn_complement |= $::UTF8_WARN_ABOVE_31_BIT;
615 $utf8n_flag_to_disallow_complement |= $::UTF8_DISALLOW_ABOVE_31_BIT;
616 $uvchr_flag_to_warn_complement |= $::UNICODE_WARN_ABOVE_31_BIT;
617 $uvchr_flag_to_disallow_complement
618 |= $::UNICODE_DISALLOW_ABOVE_31_BIT;
af816908 619 }
3022ad00
KW
620 }
621 elsif ($allowed_uv >= 0xD800 && $allowed_uv <= 0xDFFF) {
601e92f1
KW
622 $cp_message_qr = qr/UTF-16 surrogate U\+$uv_string/;
623 $non_cp_trailing_text = "is for a surrogate";
67e45424 624 $needed_to_discern_len = 2 unless defined $needed_to_discern_len;
bf422d6a 625 $skip_most_tests = 1 if $allowed_uv > 0xD800 && $allowed_uv < 0xDFFF;
af816908
KW
626
627 $utf8n_flag_to_warn = $::UTF8_WARN_SURROGATE;
628 $utf8n_flag_to_disallow = $::UTF8_DISALLOW_SURROGATE;
629 $uvchr_flag_to_warn = $::UNICODE_WARN_SURROGATE;
630 $uvchr_flag_to_disallow = $::UNICODE_DISALLOW_SURROGATE;;
e4e140b4
KW
631
632 $utf8n_flag_to_warn_complement = $::UTF8_WARN_NONCHAR
633 |$::UTF8_WARN_SUPER
634 |$::UTF8_WARN_ABOVE_31_BIT;
635 $utf8n_flag_to_disallow_complement = $::UTF8_DISALLOW_NONCHAR
636 |$::UTF8_DISALLOW_SUPER
637 |$::UTF8_DISALLOW_ABOVE_31_BIT;
638 $uvchr_flag_to_warn_complement = $::UNICODE_WARN_NONCHAR
639 |$::UNICODE_WARN_SUPER
640 |$::UNICODE_WARN_ABOVE_31_BIT;
641 $uvchr_flag_to_disallow_complement = $::UNICODE_DISALLOW_NONCHAR
642 |$::UNICODE_DISALLOW_SUPER
643 |$::UNICODE_DISALLOW_ABOVE_31_BIT;
d42d7565 644 $controlling_warning_category = 'surrogate';
3022ad00
KW
645 }
646 elsif ( ($allowed_uv >= 0xFDD0 && $allowed_uv <= 0xFDEF)
647 || ($allowed_uv & 0xFFFE) == 0xFFFE)
648 {
601e92f1
KW
649 $cp_message_qr = qr/\QUnicode non-character U+$uv_string\E
650 \Q is not recommended for open interchange\E/x;
651 $non_cp_trailing_text = "if you see this, there is an error";
67e45424 652 $needed_to_discern_len = $length unless defined $needed_to_discern_len;
bf422d6a
KW
653 if ( ($allowed_uv > 0xFDD0 && $allowed_uv < 0xFDEF)
654 || ($allowed_uv > 0xFFFF && $allowed_uv < 0x10FFFE))
655 {
656 $skip_most_tests = 1;
657 }
af816908
KW
658
659 $utf8n_flag_to_warn = $::UTF8_WARN_NONCHAR;
660 $utf8n_flag_to_disallow = $::UTF8_DISALLOW_NONCHAR;
661 $uvchr_flag_to_warn = $::UNICODE_WARN_NONCHAR;
662 $uvchr_flag_to_disallow = $::UNICODE_DISALLOW_NONCHAR;;
e4e140b4
KW
663
664 $utf8n_flag_to_warn_complement = $::UTF8_WARN_SURROGATE
665 |$::UTF8_WARN_SUPER
666 |$::UTF8_WARN_ABOVE_31_BIT;
667 $utf8n_flag_to_disallow_complement = $::UTF8_DISALLOW_SURROGATE
668 |$::UTF8_DISALLOW_SUPER
669 |$::UTF8_DISALLOW_ABOVE_31_BIT;
670 $uvchr_flag_to_warn_complement = $::UNICODE_WARN_SURROGATE
671 |$::UNICODE_WARN_SUPER
672 |$::UNICODE_WARN_ABOVE_31_BIT;
673 $uvchr_flag_to_disallow_complement = $::UNICODE_DISALLOW_SURROGATE
674 |$::UNICODE_DISALLOW_SUPER
675 |$::UNICODE_DISALLOW_ABOVE_31_BIT;
676
d42d7565 677 $controlling_warning_category = 'nonchar';
3022ad00
KW
678 }
679 else {
680 die "Can't figure out what type of warning to test for $testname"
681 }
682
67e45424
KW
683 die 'Didn\'t set $needed_to_discern_len for ' . $testname
684 unless defined $needed_to_discern_len;
8f79178b
KW
685 { # First test the isFOO calls
686 use warnings; # Make sure these don't raise warnings
9cdc3054 687 undef @warnings_gotten;
8f79178b 688
6aa905cf
KW
689 my $ret = test_isUTF8_CHAR($bytes, $length);
690 my $ret_flags = test_isUTF8_CHAR_flags($bytes, $length, 0);
691 if ($will_overflow) {
d402d77f
KW
692 is($ret, 0, "For $testname: isUTF8_CHAR() returns 0");
693 is($ret_flags, 0, " And isUTF8_CHAR_flags() returns 0");
6aa905cf
KW
694 }
695 else {
696 is($ret, $length,
d402d77f
KW
697 "For $testname: isUTF8_CHAR() returns expected length: $length");
698 is($ret_flags, $length, " And isUTF8_CHAR_flags(...,0)"
6aa905cf
KW
699 . " returns expected length: $length");
700 }
9cdc3054 701 is(scalar @warnings_gotten, 0,
d402d77f
KW
702 " And neither isUTF8_CHAR() nor isUTF8_CHAR()_flags generated"
703 . " any warnings")
9cdc3054 704 or output_warnings(@warnings_gotten);
6aa905cf 705
9cdc3054 706 undef @warnings_gotten;
6aa905cf
KW
707 $ret = test_isSTRICT_UTF8_CHAR($bytes, $length);
708 if ($will_overflow) {
d402d77f 709 is($ret, 0, " And isSTRICT_UTF8_CHAR() returns 0");
6aa905cf
KW
710 }
711 else {
712 my $expected_ret = ( $testname =~ /surrogate|non-character/
713 || $allowed_uv > 0x10FFFF)
714 ? 0
715 : $length;
d402d77f 716 is($ret, $expected_ret, " And isSTRICT_UTF8_CHAR() returns"
6aa905cf
KW
717 . " expected length: $expected_ret");
718 $ret = test_isUTF8_CHAR_flags($bytes, $length,
719 $::UTF8_DISALLOW_ILLEGAL_INTERCHANGE);
720 is($ret, $expected_ret,
d402d77f
KW
721 " And isUTF8_CHAR_flags('DISALLOW_ILLEGAL_INTERCHANGE')"
722 . " acts like isSTRICT_UTF8_CHAR");
6aa905cf 723 }
9cdc3054 724 is(scalar @warnings_gotten, 0,
d402d77f
KW
725 " And neither isSTRICT_UTF8_CHAR() nor isUTF8_CHAR_flags"
726 . " generated any warnings")
9cdc3054 727 or output_warnings(@warnings_gotten);
6aa905cf 728
9cdc3054 729 undef @warnings_gotten;
6aa905cf
KW
730 $ret = test_isC9_STRICT_UTF8_CHAR($bytes, $length);
731 if ($will_overflow) {
d402d77f 732 is($ret, 0, " And isC9_STRICT_UTF8_CHAR() returns 0");
6aa905cf
KW
733 }
734 else {
735 my $expected_ret = ( $testname =~ /surrogate/
736 || $allowed_uv > 0x10FFFF)
737 ? 0
738 : $length;
d402d77f 739 is($ret, $expected_ret, " And isC9_STRICT_UTF8_CHAR()"
6aa905cf
KW
740 ." returns expected length: $expected_ret");
741 $ret = test_isUTF8_CHAR_flags($bytes, $length,
8f79178b 742 $::UTF8_DISALLOW_ILLEGAL_C9_INTERCHANGE);
6aa905cf 743 is($ret, $expected_ret,
d402d77f
KW
744 " And isUTF8_CHAR_flags('DISALLOW_ILLEGAL_C9_INTERCHANGE')"
745 . " acts like isC9_STRICT_UTF8_CHAR");
6aa905cf 746 }
9cdc3054 747 is(scalar @warnings_gotten, 0,
d402d77f
KW
748 " And neither isC9_STRICT_UTF8_CHAR() nor isUTF8_CHAR_flags"
749 . " generated any warnings")
9cdc3054 750 or output_warnings(@warnings_gotten);
6aa905cf
KW
751
752 # Test partial character handling, for each byte not a full character
753 for my $j (1.. $length - 1) {
754
755 # Skip the test for the interaction between overflow and above-31
756 # bit. It is really testing other things than the partial
757 # character tests, for which other tests in this file are
758 # sufficient
04f42bf6 759 last if $will_overflow;
6aa905cf 760
69485e19 761 foreach my $disallow_flag (0, $utf8n_flag_to_disallow) {
6aa905cf
KW
762 my $partial = substr($bytes, 0, $j);
763 my $ret_should_be;
764 my $comment;
765 if ($disallow_flag) {
766 $ret_should_be = 0;
767 $comment = "disallowed";
768 if ($j < $needed_to_discern_len) {
769 $ret_should_be = 1;
770 $comment .= ", but need $needed_to_discern_len bytes"
771 . " to discern:";
772 }
773 }
774 else {
775 $ret_should_be = 1;
776 $comment = "allowed";
777 }
778
9cdc3054 779 undef @warnings_gotten;
6aa905cf
KW
780
781 $ret = test_is_utf8_valid_partial_char_flags($partial, $j,
782 $disallow_flag);
783 is($ret, $ret_should_be,
d402d77f
KW
784 " And is_utf8_valid_partial_char_flags("
785 . display_bytes($partial)
786 . "), $comment: returns $ret_should_be");
9cdc3054 787 is(scalar @warnings_gotten, 0,
d402d77f 788 " And is_utf8_valid_partial_char_flags()"
6aa905cf 789 . " generated no warnings")
9cdc3054 790 or output_warnings(@warnings_gotten);
6aa905cf
KW
791 }
792 }
793 }
794
8f79178b 795 # Now test the to/from UTF-8 calls
69485e19
KW
796 # This is more complicated than the malformations tested in other files in
797 # this directory, as there are several orthogonal variables involved. We
798 # test most possible combinations
8f79178b
KW
799 foreach my $do_disallow (0, 1) {
800 next if $skip_most_tests && ! $do_disallow;
801
802 # We try various combinations of malformations that can occur
803 foreach my $short ("", "short") {
804 next if $skip_most_tests && $short;
805 foreach my $unexpected_noncont ("", "unexpected non-continuation") {
806 next if $skip_most_tests && $unexpected_noncont;
807 foreach my $overlong ("", "overlong") {
808 next if $overlong && $skip_most_tests;
809
810 # If we're creating an overlong, it can't be longer than the
811 # maximum length, so skip if we're already at that length.
812 next if $overlong && $length >= $::max_bytes;
6aa905cf 813
69485e19
KW
814 # We classify the warnings into certain "interesting" types,
815 # described later
816 foreach my $warning_type (0..4) {
817 next if $skip_most_tests && $warning_type != 1;
818 foreach my $use_warn_flag (0, 1) {
819 next if $skip_most_tests && ! $use_warn_flag;
820
8f79178b
KW
821 # Finally, here is the inner loop
822
69485e19
KW
823 my $this_utf8n_flag_to_warn = $utf8n_flag_to_warn;
824 my $this_utf8n_flag_to_disallow = $utf8n_flag_to_disallow;
825 my $this_uvchr_flag_to_warn = $uvchr_flag_to_warn;
826 my $this_uvchr_flag_to_disallow = $uvchr_flag_to_disallow;
827
828 my $eval_warn;
829 my $expect_regular_warnings;
830 my $expect_warnings_for_malformed;
831 my $expect_warnings_for_overflow;
832
833 if ($warning_type == 0) {
834 $eval_warn = "use warnings; no warnings 'deprecated'";
835 $expect_regular_warnings = $use_warn_flag;
836 $expect_warnings_for_overflow = 1;
837 $expect_warnings_for_malformed = 1;
6aa905cf 838 }
69485e19
KW
839 elsif ($warning_type == 1) {
840 $eval_warn = "no warnings";
841 $expect_regular_warnings = 0;
842 $expect_warnings_for_overflow = 0;
843 $expect_warnings_for_malformed = 0;
844 }
845 elsif ($warning_type == 2) {
846 $eval_warn = "no warnings; use warnings 'utf8'";
847 $expect_regular_warnings = $use_warn_flag;
848 $expect_warnings_for_overflow = 1;
849 $expect_warnings_for_malformed = 1;
850 }
851 elsif ($warning_type == 3) {
852 $eval_warn = "no warnings; use warnings"
e4e140b4
KW
853 . " '$controlling_warning_category'";
854 $expect_regular_warnings = $use_warn_flag;
69485e19 855 $expect_warnings_for_overflow
e4e140b4 856 = $controlling_warning_category eq 'non_unicode';
69485e19
KW
857 $expect_warnings_for_malformed = 0;
858 }
859 elsif ($warning_type == 4) { # Like type 3, but uses the
860 # above-31-bit flags
861 # The complement flags were set up so that the
862 # above-31-bit flags have been tested that they don't
863 # trigger wrongly for too small code points. And the
864 # flags have been set up so that those small code
865 # points are tested for being above Unicode. What's
866 # left to test is that the large code points do
867 # trigger the above-31-bit flags.
868 next if ! $will_overflow && $allowed_uv < 0x80000000;
e4e140b4 869 next if $controlling_warning_category ne 'non_unicode';
69485e19
KW
870 $eval_warn = "no warnings; use warnings 'non_unicode'";
871 $expect_regular_warnings = 1;
872 $expect_warnings_for_overflow = 1;
873 $expect_warnings_for_malformed = 0;
874 $this_utf8n_flag_to_warn = $::UTF8_WARN_ABOVE_31_BIT;
875 $this_utf8n_flag_to_disallow
876 = $::UTF8_DISALLOW_ABOVE_31_BIT;
877 $this_uvchr_flag_to_warn = $::UNICODE_WARN_ABOVE_31_BIT;
878 $this_uvchr_flag_to_disallow
879 = $::UNICODE_DISALLOW_ABOVE_31_BIT;
601e92f1
KW
880 }
881 else {
69485e19 882 die "Unexpected warning type '$warning_type'";
601e92f1
KW
883 }
884
69485e19
KW
885 # We only need to test the case where all warnings are
886 # enabled (type 0) to see if turning off the warning flag
887 # causes things to not be output. If those pass, then
888 # turning on some sub-category of warnings, or turning off
889 # warnings altogether are extremely likely to not output
890 # warnings either, given how the warnings subsystem is
891 # supposed to work, and this file assumes it does work.
892 next if $warning_type != 0 && ! $use_warn_flag;
893
894 # The convention is that the 'got' flag is the same value
895 # as the disallow one. If this were violated, the tests
896 # here should start failing.
897 my $return_flag = $this_utf8n_flag_to_disallow;
898
e4e140b4
KW
899 # If we aren't expecting warnings/disallow for this, turn
900 # on all the other flags. That makes sure that they all
901 # are independent of this flag, and so we don't need to
902 # test them individually.
903 my $this_warning_flags = ($use_warn_flag)
904 ? $this_utf8n_flag_to_warn
905 : $utf8n_flag_to_warn_complement;
69485e19 906 my $this_disallow_flags = ($do_disallow)
e4e140b4
KW
907 ? $this_utf8n_flag_to_disallow
908 : $utf8n_flag_to_disallow_complement;
6aa905cf
KW
909 my $this_bytes = $bytes;
910 my $this_length = $length;
911 my $expected_uv = $allowed_uv;
b3169593 912 my $this_expected_len = $length;
6aa905cf 913 my $this_needed_to_discern_len = $needed_to_discern_len;
6aa905cf 914
69485e19
KW
915 my @malformation_names;
916 my @expected_warnings;
917 my @expected_return_flags;
6aa905cf 918
69485e19
KW
919 # Now go through the possible malformations wanted, and
920 # change the input accordingly. We also can set up
921 # certain other things now, like whether we expect a
922 # return flag from this malformation and which flag.
923 if ($overlong) {
8f79178b
KW
924
925 # To force this malformation, we convert the original
926 # start byte into a continuation byte with the same
927 # data bits as originally. ...
928 substr($this_bytes, 0, 1)
929 = start_byte_to_cont(substr($this_bytes,
930 0, 1));
931
932 # ... Then we prepend it with a known overlong
933 # sequence. This should evaluate to the exact same
934 # code point as the original.
935 $this_bytes
936 = I8_to_native("\xff")
937 . (I8_to_native(chr $::lowest_continuation)
938 x ( $::max_bytes - 1 - length($this_bytes)))
939 . $this_bytes;
940 $this_length = length($this_bytes);
941 $this_needed_to_discern_len
942 = $::max_bytes - ($this_expected_len
6aa905cf 943 - $this_needed_to_discern_len);
8f79178b
KW
944 $this_expected_len = $::max_bytes;
945 push @expected_return_flags, $::UTF8_GOT_LONG;
69485e19
KW
946 push @malformation_names, $overlong;
947 if ($expect_warnings_for_malformed) {
948 if ( ! $short
949 && ! $unexpected_noncont
950 && ! $will_overflow)
951 {
952 my $overlong_bytes
953 = display_bytes_no_quotes($this_bytes);
954 my $correct_bytes
955 = display_bytes_no_quotes($bytes);
56d0c377
KW
956 my $prefix = ($allowed_uv > 0x10FFFF)
957 ? "0x"
958 : "U+";
69485e19
KW
959 push @expected_warnings,
960 qr/\QMalformed UTF-8 character:\E
961 \Q $overlong_bytes (overlong;\E
962 \Q instead use $correct_bytes to\E
56d0c377 963 \Q represent $prefix$uv_string)/x;
69485e19
KW
964 }
965 else {
966 push @expected_warnings, qr/overlong/;
967 }
6aa905cf 968 }
69485e19 969 }
6aa905cf 970
69485e19
KW
971 if ($short) {
972 push @malformation_names, $short;
973 push @expected_warnings, qr/short/
974 if $expect_warnings_for_malformed;
975
976 # To force this malformation, just tell the test to
977 # not look as far as it should into the input.
978 $this_length--;
979 $this_expected_len--;
980 push @expected_return_flags, $::UTF8_GOT_SHORT;
981 }
982
983 if ($unexpected_noncont) {
984 push @malformation_names, $unexpected_noncont;
985 push @expected_warnings, qr/$unexpected_noncont/
986 if $expect_warnings_for_malformed;
987
988 # To force this malformation, change the final
989 # continuation byte into a non continuation.
990 my $pos = ($short) ? -2 : -1;
991 substr($this_bytes, $pos, 1) = '?';
992 $this_expected_len--;
993 push @expected_return_flags,
994 $::UTF8_GOT_NON_CONTINUATION;
995 }
996
997 # The overflow malformation is done differently than other
998 # malformations. It comes from manually typed tests in
999 # the test array, but it also is above Unicode and uses
1000 # Perl extended UTF-8, so affects some of the flags being
1001 # tested. We now make it be treated like one of the other
1002 # generated malformations.
1003 if ($will_overflow) {
1004
1005 # An overflow is (way) above Unicode, and overrides
1006 # everything else.
1007 $expect_regular_warnings = 0;
1008
1009 push @malformation_names, 'overflow';
1010 if ($expect_warnings_for_overflow) {
1011 my $qr = display_bytes_no_quotes(
1012 substr($this_bytes, 0, $this_expected_len));
1013 $qr = qr/\QMalformed UTF-8 character: \E
1014 \Q$qr (overflows)\E/x;
1015 push @expected_warnings, $qr;
6aa905cf 1016 }
69485e19 1017 push @expected_return_flags, $::UTF8_GOT_OVERFLOW;
6aa905cf
KW
1018 }
1019
69485e19
KW
1020 # Here, we've set things up based on the malformations.
1021 # Now generate the text for them for the test name.
1022 my $malformations_name = "";
1023 if (@malformation_names) {
1024 $malformations_name .= "malformation";
1025 $malformations_name .= "s" if @malformation_names > 1;
1026 $malformations_name .= ": ";
1027 $malformations_name .= join "/", @malformation_names;
1028 $malformations_name = " ($malformations_name)";
1029 }
1030
1031 # It may be that the malformations have shortened the
1032 # amount of input we look at so much that we can't tell
1033 # what the category the code point was in. Otherwise, set
1034 # up the expected return flags based on the warnings and
1035 # disallowments.
1036 if ($this_expected_len < $this_needed_to_discern_len) {
1037 $expect_regular_warnings = 0;
1038 }
1039 elsif ( ($this_warning_flags & $this_utf8n_flag_to_warn)
1040 || ( $this_disallow_flags
1041 & $this_utf8n_flag_to_disallow))
1042 {
1043 push @expected_return_flags, $return_flag;
1044 }
1045
1046 # Finish setting up the expected warning.
1047 if ($expect_regular_warnings) {
1048
1049 # So far the array contains warnings generated by
1050 # malformations. Add the expected regular one.
1051 unshift @expected_warnings, $cp_message_qr;
1052
1053 # But it may need to be modified, because either of
1054 # these malformations means we can't determine the
1055 # expected code point.
1056 if ($short || $unexpected_noncont) {
1057 my $first_byte = substr($this_bytes, 0, 1);
1058 $expected_warnings[0] = display_bytes(
1059 substr($this_bytes, 0, $this_expected_len));
1060 $expected_warnings[0]
1061 = qr/[Aa]\Qny UTF-8 sequence that starts with\E
1062 \Q $expected_warnings[0]\E
1063 \Q $non_cp_trailing_text\E/x;
1064 }
1065 }
6aa905cf
KW
1066
1067 # Is effectively disallowed if we've set up a
1068 # malformation, even if the flag indicates it is
1069 # allowed. Fix up test name to indicate this as
1070 # well
69485e19
KW
1071 my $disallowed = ( $this_disallow_flags
1072 & $this_utf8n_flag_to_disallow)
1073 || $malformations_name;
6aa905cf 1074 my $this_name = "utf8n_to_uvchr_error() $testname: "
69485e19
KW
1075 . (($disallowed)
1076 ? 'disallowed'
1077 : 'allowed');
6aa905cf 1078 $this_name .= ", $eval_warn";
69485e19
KW
1079 $this_name .= ", " . (( $this_warning_flags
1080 & $this_utf8n_flag_to_warn)
1081 ? 'with flag for raising warnings'
1082 : 'no flag for raising warnings');
1083 $this_name .= $malformations_name;
db0f09e6 1084
8f79178b 1085 # Do the actual test using an eval
9cdc3054 1086 undef @warnings_gotten;
6aa905cf 1087 my $ret_ref;
69485e19 1088 my $this_flags = $this_warning_flags|$this_disallow_flags;
6aa905cf
KW
1089 my $eval_text = "$eval_warn; \$ret_ref"
1090 . " = test_utf8n_to_uvchr_error("
69485e19 1091 . "'$this_bytes', $this_length, $this_flags)";
6aa905cf 1092 eval "$eval_text";
69485e19 1093 if (! ok ("$@ eq ''", "$this_name: eval succeeded"))
6aa905cf 1094 {
a8ee5133
KW
1095 diag "\$@='$@'; call was: "
1096 . utf8n_display_call($eval_text);
6aa905cf
KW
1097 next;
1098 }
1099 if ($disallowed) {
d402d77f 1100 is($ret_ref->[0], 0, " And returns 0")
a8ee5133 1101 or diag "Call was: " . utf8n_display_call($eval_text);
6aa905cf
KW
1102 }
1103 else {
1104 is($ret_ref->[0], $expected_uv,
d402d77f
KW
1105 " And returns expected uv: "
1106 . $uv_string)
a8ee5133 1107 or diag "Call was: " . utf8n_display_call($eval_text);
6aa905cf
KW
1108 }
1109 is($ret_ref->[1], $this_expected_len,
d402d77f 1110 " And returns expected length:"
6aa905cf 1111 . " $this_expected_len")
a8ee5133 1112 or diag "Call was: " . utf8n_display_call($eval_text);
6aa905cf 1113
9cdc3054 1114 my $returned_flags = $ret_ref->[2];
6aa905cf 1115
9cdc3054 1116 for (my $i = @expected_return_flags - 1; $i >= 0; $i--) {
69485e19
KW
1117 if ($expected_return_flags[$i] & $returned_flags) {
1118 if ($expected_return_flags[$i]
1119 == $::UTF8_DISALLOW_ABOVE_31_BIT)
1120 {
1121 pass(" Expected and got return flag for"
1122 . " above_31_bit");
1123 }
1124 # The first entries in this are
1125 # malformations
1126 elsif ($i > @malformation_names - 1) {
1127 pass(" Expected and got return flag"
e4e140b4 1128 . " for " . $controlling_warning_category);
69485e19
KW
1129 }
1130 else {
1131 pass(" Expected and got return flag for "
1132 . $malformation_names[$i]
1133 . " malformation");
1134 }
9cdc3054 1135 $returned_flags &= ~$expected_return_flags[$i];
69485e19 1136 splice @expected_return_flags, $i, 1;
6aa905cf 1137 }
6aa905cf 1138 }
6aa905cf 1139
69485e19
KW
1140 is($returned_flags, 0,
1141 " Got no unexpected return flags")
1142 or diag "The unexpected flags gotten were: "
5722c46d
KW
1143 . (flags_to_text($returned_flags,
1144 \@utf8n_flags_to_text)
69485e19
KW
1145 # We strip off any prefixes from the flag
1146 # names
1147 =~ s/ \b [A-Z] _ //xgr);
1148 is (scalar @expected_return_flags, 0,
1149 " Got all expected return flags")
1150 or diag "The expected flags not gotten were: "
1151 . (flags_to_text(eval join("|",
1152 @expected_return_flags),
1153 \@utf8n_flags_to_text)
1154 # We strip off any prefixes from the flag
1155 # names
5722c46d 1156 =~ s/ \b [A-Z] _ //xgr);
9cdc3054 1157
69485e19
KW
1158 do_warnings_test(@expected_warnings)
1159 or diag "Call was: " . utf8n_display_call($eval_text);
1160 undef @warnings_gotten;
6aa905cf
KW
1161
1162 # Check CHECK_ONLY results when the input is
1163 # disallowed. Do this when actually disallowed,
69485e19 1164 # not just when the $this_disallow_flags is set
6aa905cf 1165 if ($disallowed) {
69485e19
KW
1166 my $this_flags = $this_disallow_flags|$::UTF8_CHECK_ONLY;
1167 my $eval_text = "use warnings; no warnings 'deprecated'; \$ret_ref ="
1168 . " test_utf8n_to_uvchr_error('"
1169 . "$this_bytes', $this_length,"
1170 . " $this_flags)";
1171 eval $eval_text;
a8ee5133
KW
1172 if (! ok ("$@ eq ''",
1173 " And eval succeeded with CHECK_ONLY"))
1174 {
1175 diag "\$@='$@'; Call was: "
1176 . utf8n_display_call($eval_text);
1177 next;
1178 }
d402d77f 1179 is($ret_ref->[0], 0, " CHECK_ONLY: Returns 0")
a8ee5133 1180 or diag "Call was: " . utf8n_display_call($eval_text);
6aa905cf 1181 is($ret_ref->[1], -1,
d402d77f 1182 " CHECK_ONLY: returns -1 for length")
a8ee5133 1183 or diag "Call was: " . utf8n_display_call($eval_text);
9cdc3054 1184 if (! is(scalar @warnings_gotten, 0,
d402d77f 1185 " CHECK_ONLY: no warnings generated"))
6aa905cf 1186 {
a8ee5133 1187 diag "Call was: " . utf8n_display_call($eval_text);
9cdc3054 1188 output_warnings(@warnings_gotten);
6aa905cf
KW
1189 }
1190 }
1191
1192 # Now repeat some of the above, but for
1193 # uvchr_to_utf8_flags(). Since this comes from an
8f79178b
KW
1194 # existing code point, it hasn't overflowed, and isn't
1195 # malformed.
69485e19
KW
1196 next if @malformation_names;
1197
1198 $this_warning_flags = ($use_warn_flag)
1199 ? $this_uvchr_flag_to_warn
1200 : 0;
1201 $this_disallow_flags = ($do_disallow)
1202 ? $this_uvchr_flag_to_disallow
1203 : 0;
1204
1205 $disallowed = $this_disallow_flags
1206 & $this_uvchr_flag_to_disallow;
1207 $this_name .= ", " . (( $this_warning_flags
1208 & $this_utf8n_flag_to_warn)
1209 ? 'with flag for raising warnings'
1210 : 'no flag for raising warnings');
6aa905cf
KW
1211
1212 $this_name = "uvchr_to_utf8_flags() $testname: "
69485e19 1213 . (($disallowed)
6aa905cf 1214 ? 'disallowed'
6aa905cf
KW
1215 : 'allowed');
1216 $this_name .= ", $eval_warn";
69485e19
KW
1217 $this_name .= ", " . (( $this_warning_flags
1218 & $this_uvchr_flag_to_warn)
6aa905cf
KW
1219 ? 'with warning flag'
1220 : 'no warning flag');
1221
9cdc3054 1222 undef @warnings_gotten;
6aa905cf 1223 my $ret;
69485e19 1224 $this_flags = $this_warning_flags|$this_disallow_flags;
6aa905cf
KW
1225 $eval_text = "$eval_warn; \$ret ="
1226 . " test_uvchr_to_utf8_flags("
d884ea32 1227 . "$allowed_uv, $this_flags)";
6aa905cf
KW
1228 eval "$eval_text";
1229 if (! ok ("$@ eq ''", "$this_name: eval succeeded"))
1230 {
d884ea32
KW
1231 diag "\$@='$@'; call was: "
1232 . uvchr_display_call($eval_text);
6aa905cf
KW
1233 next;
1234 }
1235 if ($disallowed) {
d402d77f 1236 is($ret, undef, " And returns undef")
d884ea32 1237 or diag "Call was: " . uvchr_display_call($eval_text);
6aa905cf
KW
1238 }
1239 else {
d402d77f 1240 is($ret, $this_bytes, " And returns expected string")
d884ea32 1241 or diag "Call was: " . uvchr_display_call($eval_text);
6aa905cf 1242 }
69485e19
KW
1243
1244 do_warnings_test(@expected_warnings)
1245 or diag "Call was: " . uvchr_display_call($eval_text);
6aa905cf
KW
1246 }
1247 }
6aa905cf
KW
1248 }
1249 }
8f79178b 1250 }
6aa905cf
KW
1251 }
1252}
1253
1254done_testing;