1#!perl -w 2 3BEGIN { 4 require 'loc_tools.pl'; # Contains locales_enabled() and 5 # find_utf8_ctype_locale() 6} 7 8use strict; 9use Test::More; 10use Config; 11 12use XS::APItest; 13 14my $tab = " " x 4; # Indent subsidiary tests this much 15 16use Unicode::UCD qw(search_invlist prop_invmap prop_invlist); 17my ($charname_list, $charname_map, $format, $default) = prop_invmap("Name Alias"); 18 19sub get_charname($) { 20 my $cp = shift; 21 22 # If there is a an abbreviation for the code point name, use it 23 my $name_index = search_invlist(\@{$charname_list}, $cp); 24 if (defined $name_index) { 25 my $synonyms = $charname_map->[$name_index]; 26 if (ref $synonyms) { 27 my $pat = qr/: abbreviation/; 28 my @abbreviations = grep { $_ =~ $pat } @$synonyms; 29 if (@abbreviations) { 30 return $abbreviations[0] =~ s/$pat//r; 31 } 32 } 33 } 34 35 # Otherwise, use the full name 36 use charnames (); 37 return charnames::viacode($cp) // "No name"; 38} 39 40sub truth($) { # Converts values so is() works 41 return (shift) ? 1 : 0; 42} 43 44my $base_locale; 45my $utf8_locale; 46if(locales_enabled('LC_ALL')) { 47 require POSIX; 48 $base_locale = POSIX::setlocale( &POSIX::LC_ALL, "C"); 49 if (defined $base_locale && $base_locale eq 'C') { 50 use locale; # make \w work right in non-ASCII lands 51 52 # Some locale implementations don't have the 128-255 characters all 53 # mean nothing. Skip the locale tests in that situation 54 for my $u (128 .. 255) { 55 if (chr(utf8::unicode_to_native($u)) =~ /[[:print:]]/) { 56 undef $base_locale; 57 last; 58 } 59 } 60 61 $utf8_locale = find_utf8_ctype_locale() if $base_locale; 62 } 63} 64 65sub get_display_locale_or_skip($$) { 66 67 # Helper function intimately tied to its callers. It knows the loop 68 # iterates with a locale of "", meaning don't use locale; $base_locale 69 # meaning to use a non-UTF-8 locale; and $utf8_locale. 70 # 71 # It checks to see if the current test should be skipped or executed, 72 # returning an empty list for the former, and for the latter: 73 # ( 'locale display name', 74 # bool of is this a UTF-8 locale ) 75 # 76 # The display name is the empty string if not using locale. Functions 77 # with _LC in their name are skipped unless in locale, and functions 78 # without _LC are executed only outside locale. 79 80 my ($locale, $suffix) = @_; 81 82 # The test should be skipped if the input is for a non-existent locale 83 return unless defined $locale; 84 85 # Here the input is defined, either a locale name or "". If the test is 86 # for not using locales, we want to do the test for non-LC functions, 87 # and skip it for LC ones. 88 if ($locale eq "") { 89 return ("", 0) if $suffix !~ /LC/; 90 return; 91 } 92 93 # Here the input is for a real locale. We don't test the non-LC functions 94 # for locales. 95 return if $suffix !~ /LC/; 96 97 # Here is for a LC function and a real locale. The base locale is not 98 # UTF-8. 99 return (" ($locale locale)", 0) if $locale eq $base_locale; 100 101 # The only other possibility is that we have a UTF-8 locale 102 return (" ($locale)", 1); 103} 104 105sub try_malforming($$$) 106{ 107 # Determines if the tests for malformed UTF-8 should be done. When done, 108 # the .xs code creates malformations by pretending the length is shorter 109 # than it actually is. Some things can't be malformed, and sometimes this 110 # test knows that the current code doesn't look for a malformation under 111 # various circumstances. 112 113 my ($u, $function, $using_locale) = @_; 114 # $u is unicode code point; 115 116 # Single bytes can't be malformed 117 return 0 if $u < ((ord "A" == 65) ? 128 : 160); 118 119 # ASCII doesn't need to ever look beyond the first byte. 120 return 0 if $function eq "ASCII"; 121 122 # Nor, on EBCDIC systems, does CNTRL 123 return 0 if ord "A" != 65 && $function eq "CNTRL"; 124 125 # No controls above 255, so the code doesn't look at those 126 return 0 if $u > 255 && $function eq "CNTRL"; 127 128 # No non-ASCII digits below 256, except if using locales. 129 return 0 if $u < 256 && ! $using_locale && $function =~ /X?DIGIT/; 130 131 return 1; 132} 133 134my %properties = ( 135 # name => Lookup-property name 136 alnum => 'Word', 137 wordchar => 'Word', 138 alphanumeric => 'Alnum', 139 alpha => 'XPosixAlpha', 140 ascii => 'ASCII', 141 blank => 'Blank', 142 cntrl => 'Control', 143 digit => 'Digit', 144 graph => 'Graph', 145 idfirst => '_Perl_IDStart', 146 idcont => '_Perl_IDCont', 147 lower => 'XPosixLower', 148 print => 'Print', 149 psxspc => 'XPosixSpace', 150 punct => 'XPosixPunct', 151 quotemeta => '_Perl_Quotemeta', 152 space => 'XPerlSpace', 153 vertws => 'VertSpace', 154 upper => 'XPosixUpper', 155 xdigit => 'XDigit', 156 ); 157 158my %seen; 159my @warnings; 160local $SIG{__WARN__} = sub { push @warnings, @_ }; 161 162my %utf8_param_code = ( 163 "_safe" => 0, 164 "_safe, malformed" => 1, 165 ); 166 167# This test is split into this number of files. 168my $num_test_files = $ENV{TEST_JOBS} || 1; 169$::TEST_CHUNK = 0 if $num_test_files == 1 && ! defined $::TEST_CHUNK; 170$num_test_files = 10 if $num_test_files > 10; 171 172my $property_count = -1; 173foreach my $name (sort keys %properties, 'octal') { 174 175 # We test every nth property in this run so that this test is split into 176 # smaller chunks to minimize test suite elapsed time when run in parallel. 177 $property_count++; 178 next if $property_count % $num_test_files != $::TEST_CHUNK; 179 180 my @invlist; 181 if ($name eq 'octal') { 182 # Hand-roll an inversion list with 0-7 in it and nothing else. 183 push @invlist, ord "0", ord "8"; 184 } 185 else { 186 my $property = $properties{$name}; 187 @invlist = prop_invlist($property, '_perl_core_internal_ok'); 188 if (! @invlist) { 189 190 # An empty return could mean an unknown property, or merely that 191 # it is empty. Call in scalar context to differentiate 192 if (! prop_invlist($property, '_perl_core_internal_ok')) { 193 fail("No inversion list found for $property"); 194 next; 195 } 196 } 197 } 198 199 # Include all the Latin1 code points, plus 0x100. 200 my @code_points = (0 .. 256); 201 202 # Then include the next few boundaries above those from this property 203 my $above_latins = 0; 204 foreach my $range_start (@invlist) { 205 next if $range_start < 257; 206 push @code_points, $range_start - 1, $range_start; 207 $above_latins++; 208 last if $above_latins > 5; 209 } 210 211 # This makes sure we are using the Perl definition of idfirst and idcont, 212 # and not the Unicode. There are a few differences. 213 push @code_points, ord "\N{ESTIMATED SYMBOL}" if $name =~ /^id(first|cont)/; 214 if ($name eq "idcont") { # And some that are continuation but not start 215 push @code_points, ord("\N{GREEK ANO TELEIA}"), 216 ord("\N{COMBINING GRAVE ACCENT}"); 217 } 218 219 # And finally one non-Unicode code point. 220 push @code_points, 0x110000; # Above Unicode, no prop should match 221 no warnings 'non_unicode'; 222 223 for my $n (@code_points) { 224 my $u = utf8::native_to_unicode($n); 225 my $function = uc($name); 226 227 is (@warnings, 0, "Got no unexpected warnings in previous iteration") 228 or diag("@warnings"); 229 undef @warnings; 230 231 my $matches = search_invlist(\@invlist, $n); 232 if (! defined $matches) { 233 $matches = 0; 234 } 235 else { 236 $matches = truth(! ($matches % 2)); 237 } 238 239 my $ret; 240 my $char_name = get_charname($n); 241 my $display_name = sprintf "\\x{%02X, %s}", $n, $char_name; 242 my $display_call = "is${function}( $display_name )"; 243 244 foreach my $suffix ("", "_A", "_L1", "_LC", "_uni", "_uvchr", 245 "_LC_uvchr", "_utf8", "_LC_utf8") 246 { 247 248 # Not all possible macros have been defined 249 if ($name eq 'vertws') { 250 251 # vertws is always all of Unicode 252 next if $suffix !~ / ^ _ ( uni | uvchr | utf8 ) $ /x; 253 } 254 elsif ($name eq 'alnum') { 255 256 # ALNUM_A, ALNUM_L1, and ALNUM_uvchr are not defined as these 257 # suffixes were added later, after WORDCHAR was created to be 258 # a clearer synonym for ALNUM 259 next if $suffix eq '_A' 260 || $suffix eq '_L1' 261 || $suffix eq '_uvchr'; 262 } 263 elsif ($name eq 'octal') { 264 next if $suffix ne "" && $suffix ne '_A' && $suffix ne '_L1'; 265 } 266 elsif ($name eq 'quotemeta') { 267 # There is only one macro for this, and is defined only for 268 # Latin1 range 269 next if $suffix ne "" 270 } 271 272 foreach my $locale ("", $base_locale, $utf8_locale) { 273 274 my ($display_locale, $locale_is_utf8) 275 = get_display_locale_or_skip($locale, $suffix); 276 next unless defined $display_locale; 277 278 use if $locale, "locale"; 279 POSIX::setlocale( &POSIX::LC_ALL, $locale) if $locale; 280 281 if ($suffix !~ /utf8/) { # _utf8 has to handled specially 282 my $display_call 283 = "is${function}$suffix( $display_name )$display_locale"; 284 $ret = truth eval "test_is${function}$suffix($n)"; 285 if (is ($@, "", "$display_call didn't give error")) { 286 my $truth = $matches; 287 if ($truth) { 288 289 # The single byte functions are false for 290 # above-Latin1 291 if ($n >= 256) { 292 $truth = 0 293 if $suffix=~ / ^ ( _A | _L [1C] )? $ /x; 294 } 295 elsif ( $u >= 128 296 && $name ne 'quotemeta') 297 { 298 299 # The no-suffix and _A functions are false 300 # for non-ASCII. So are _LC functions on a 301 # non-UTF-8 locale 302 $truth = 0 if $suffix eq "_A" 303 || $suffix eq "" 304 || ( $suffix =~ /LC/ 305 && ! $locale_is_utf8); 306 } 307 } 308 309 is ($ret, $truth, "${tab}And correctly returns $truth"); 310 } 311 } 312 else { # _utf8 suffix 313 my $char = chr($n); 314 utf8::upgrade($char); 315 $char = quotemeta $char if $char eq '\\' || $char eq "'"; 316 my $truth; 317 if ( $suffix =~ /LC/ 318 && ! $locale_is_utf8 319 && $n < 256 320 && $u >= 128) 321 { # The C-locale _LC function returns FALSE for Latin1 322 # above ASCII 323 $truth = 0; 324 } 325 else { 326 $truth = $matches; 327 } 328 329 foreach my $utf8_param("_safe", 330 "_safe, malformed", 331 ) 332 { 333 my $utf8_param_code = $utf8_param_code{$utf8_param}; 334 my $expect_error = $utf8_param_code > 0; 335 next if $expect_error 336 && ! try_malforming($u, $function, 337 $suffix =~ /LC/); 338 339 my $display_call = "is${function}$suffix( $display_name" 340 . ", $utf8_param )$display_locale"; 341 $ret = truth eval "test_is${function}$suffix('$char'," 342 . " $utf8_param_code)"; 343 if ($expect_error) { 344 isnt ($@, "", 345 "expected and got error in $display_call"); 346 like($@, qr/Malformed UTF-8 character/, 347 "${tab}And got expected message"); 348 if (is (@warnings, 1, 349 "${tab}Got a single warning besides")) 350 { 351 like($warnings[0], 352 qr/Malformed UTF-8 character.*short/, 353 "${tab}Got expected warning"); 354 } 355 else { 356 diag("@warnings"); 357 } 358 undef @warnings; 359 } 360 elsif (is ($@, "", "$display_call didn't give error")) { 361 is ($ret, $truth, 362 "${tab}And correctly returned $truth"); 363 if ($utf8_param_code < 0) { 364 my $warnings_ok; 365 my $unique_function = "is" . $function . $suffix; 366 if (! $seen{$unique_function}++) { 367 $warnings_ok = is(@warnings, 1, 368 "${tab}This is first call to" 369 . " $unique_function; Got a single" 370 . " warning"); 371 if ($warnings_ok) { 372 $warnings_ok = like($warnings[0], 373 qr/starting in Perl .* will require an additional parameter/, 374 "${tab}The warning was the expected" 375 . " deprecation one"); 376 } 377 } 378 else { 379 $warnings_ok = is(@warnings, 0, 380 "${tab}This subsequent call to" 381 . " $unique_function did not warn"); 382 } 383 $warnings_ok or diag("@warnings"); 384 undef @warnings; 385 } 386 } 387 } 388 } 389 } 390 } 391 } 392} 393 394my %to_properties = ( 395 FOLD => 'Case_Folding', 396 LOWER => 'Lowercase_Mapping', 397 TITLE => 'Titlecase_Mapping', 398 UPPER => 'Uppercase_Mapping', 399 ); 400 401$property_count = -1; 402foreach my $name (sort keys %to_properties) { 403 404 $property_count++; 405 next if $property_count % $num_test_files != $::TEST_CHUNK; 406 407 my $property = $to_properties{$name}; 408 my ($list_ref, $map_ref, $format, $missing) 409 = prop_invmap($property, ); 410 if (! $list_ref || ! $map_ref) { 411 fail("No inversion map found for $property"); 412 next; 413 } 414 if ($format !~ / ^ a l? $ /x) { 415 fail("Unexpected inversion map format ('$format') found for $property"); 416 next; 417 } 418 419 # Include all the Latin1 code points, plus 0x100. 420 my @code_points = (0 .. 256); 421 422 # Then include the next few multi-char folds above those from this 423 # property, and include the next few single folds as well 424 my $above_latins = 0; 425 my $multi_char = 0; 426 for my $i (0 .. @{$list_ref} - 1) { 427 my $range_start = $list_ref->[$i]; 428 next if $range_start < 257; 429 if (ref $map_ref->[$i] && $multi_char < 5) { 430 push @code_points, $range_start - 1 431 if $code_points[-1] != $range_start - 1; 432 push @code_points, $range_start; 433 $multi_char++; 434 } 435 elsif ($above_latins < 5) { 436 push @code_points, $range_start - 1 437 if $code_points[-1] != $range_start - 1; 438 push @code_points, $range_start; 439 $above_latins++; 440 } 441 last if $above_latins >= 5 && $multi_char >= 5; 442 } 443 444 # And finally one non-Unicode code point. 445 push @code_points, 0x110000; # Above Unicode, no prop should match 446 no warnings 'non_unicode'; 447 448 # $n is native; $u unicode. 449 for my $n (@code_points) { 450 my $u = utf8::native_to_unicode($n); 451 my $function = $name; 452 453 my $index = search_invlist(\@{$list_ref}, $n); 454 455 my $ret; 456 my $char_name = get_charname($n); 457 my $display_name = sprintf "\\N{U+%02X, %s}", $n, $char_name; 458 459 foreach my $suffix ("", "_L1", "_LC") { 460 461 # This is the only macro defined for L1 462 next if $suffix eq "_L1" && $function ne "LOWER"; 463 464 SKIP: 465 foreach my $locale ("", $base_locale, $utf8_locale) { 466 467 # titlecase is not defined in locales. 468 next if $name eq 'TITLE' && $suffix eq "_LC"; 469 470 my ($display_locale, $locale_is_utf8) 471 = get_display_locale_or_skip($locale, $suffix); 472 next unless defined $display_locale; 473 474 skip("to${name}_LC does not work for LATIN SMALL LETTER SHARP S" 475 . "$display_locale", 1) 476 if $u == 0xDF && $name =~ / FOLD | UPPER /x 477 && $suffix eq "_LC" && $locale_is_utf8; 478 479 use if $locale, "locale"; 480 POSIX::setlocale( &POSIX::LC_ALL, $locale) if $locale; 481 482 my $display_call = "to${function}$suffix(" 483 . " $display_name )$display_locale"; 484 $ret = eval "test_to${function}$suffix($n)"; 485 if (is ($@, "", "$display_call didn't give error")) { 486 my $should_be; 487 if ($n > 255) { 488 $should_be = $n; 489 } 490 elsif ( $u > 127 491 && ( $suffix eq "" 492 || ($suffix eq "_LC" && ! $locale_is_utf8))) 493 { 494 $should_be = $n; 495 } 496 elsif ($map_ref->[$index] != $missing) { 497 $should_be = $map_ref->[$index] + $n - $list_ref->[$index] 498 } 499 else { 500 $should_be = $n; 501 } 502 503 is ($ret, $should_be, 504 sprintf("${tab}And correctly returned 0x%02X", 505 $should_be)); 506 } 507 } 508 } 509 510 # The _uni, uvchr, and _utf8 functions return both the ordinal of the 511 # first code point of the result, and the result in utf8. The .xs 512 # tests return these in an array, in [0] and [1] respectively, with 513 # [2] the length of the utf8 in bytes. 514 my $utf8_should_be = ""; 515 my $first_ord_should_be; 516 if (ref $map_ref->[$index]) { # A multi-char result 517 for my $n (0 .. @{$map_ref->[$index]} - 1) { 518 $utf8_should_be .= chr $map_ref->[$index][$n]; 519 } 520 521 $first_ord_should_be = $map_ref->[$index][0]; 522 } 523 else { # A single-char result 524 $first_ord_should_be = ($map_ref->[$index] != $missing) 525 ? $map_ref->[$index] + $n 526 - $list_ref->[$index] 527 : $n; 528 $utf8_should_be = chr $first_ord_should_be; 529 } 530 utf8::upgrade($utf8_should_be); 531 532 # Test _uni, uvchr 533 foreach my $suffix ('_uni', '_uvchr') { 534 my $s; 535 my $len; 536 my $display_call = "to${function}$suffix( $display_name )"; 537 $ret = eval "test_to${function}$suffix($n)"; 538 if (is ($@, "", "$display_call didn't give error")) { 539 is ($ret->[0], $first_ord_should_be, 540 sprintf("${tab}And correctly returned 0x%02X", 541 $first_ord_should_be)); 542 is ($ret->[1], $utf8_should_be, "${tab}Got correct utf8"); 543 use bytes; 544 is ($ret->[2], length $utf8_should_be, 545 "${tab}Got correct number of bytes for utf8 length"); 546 } 547 } 548 549 # Test _utf8 550 my $char = chr($n); 551 utf8::upgrade($char); 552 $char = quotemeta $char if $char eq '\\' || $char eq "'"; 553 foreach my $utf8_param("_safe", 554 "_safe, malformed", 555 ) 556 { 557 use Config; 558 next if $utf8_param eq 'deprecated mathoms' 559 && $Config{'ccflags'} =~ /-DNO_MATHOMS/; 560 561 my $utf8_param_code = $utf8_param_code{$utf8_param}; 562 my $expect_error = $utf8_param_code > 0; 563 564 # Skip if can't malform (because is a UTF-8 invariant) 565 next if $expect_error && $u < ((ord "A" == 65) ? 128 : 160); 566 567 my $display_call = "to${function}_utf8($display_name, $utf8_param )"; 568 $ret = eval "test_to${function}_utf8('$char', $utf8_param_code)"; 569 if ($expect_error) { 570 isnt ($@, "", "expected and got error in $display_call"); 571 like($@, qr/Malformed UTF-8 character/, 572 "${tab}And got expected message"); 573 undef @warnings; 574 } 575 elsif (is ($@, "", "$display_call didn't give error")) { 576 is ($ret->[0], $first_ord_should_be, 577 sprintf("${tab}And correctly returned 0x%02X", 578 $first_ord_should_be)); 579 is ($ret->[1], $utf8_should_be, "${tab}Got correct utf8"); 580 use bytes; 581 is ($ret->[2], length $utf8_should_be, 582 "${tab}Got correct number of bytes for utf8 length"); 583 if ($utf8_param_code < 0) { 584 my $warnings_ok; 585 if (! $seen{"${function}_utf8$utf8_param"}++) { 586 $warnings_ok = is(@warnings, 1, 587 "${tab}Got a single warning"); 588 if ($warnings_ok) { 589 my $expected; 590 if ($utf8_param_code == -2) { 591 my $lc_func = lc $function; 592 $expected 593 = qr/starting in Perl .* to_utf8_$lc_func\(\) will be removed/; 594 } 595 else { 596 $expected 597 = qr/starting in Perl .* will require an additional parameter/; 598 } 599 $warnings_ok = like($warnings[0], $expected, 600 "${tab}Got expected deprecation warning"); 601 } 602 } 603 else { 604 $warnings_ok = is(@warnings, 0, 605 "${tab}Deprecation warned only the one time"); 606 } 607 $warnings_ok or diag("@warnings"); 608 undef @warnings; 609 } 610 } 611 } 612 } 613} 614 615# This is primarily to make sure that no non-Unicode warnings get generated 616is(scalar @warnings, 0, "No unexpected warnings were generated in the tests") 617 or diag @warnings; 618 619done_testing; 620