| 1 | #!./perl -wT |
| 2 | |
| 3 | # This tests plain 'use locale' and adorned 'use locale ":not_characters"' |
| 4 | # Because these pragmas are compile time, and I (khw) am trying to test |
| 5 | # without using 'eval' as much as possible, which might cloud the issue, the |
| 6 | # crucial parts of the code are duplicated in a block for each pragma. |
| 7 | |
| 8 | # To make a TODO test, add the string 'TODO' to its %test_names value |
| 9 | |
| 10 | binmode STDOUT, ':utf8'; |
| 11 | binmode STDERR, ':utf8'; |
| 12 | |
| 13 | BEGIN { |
| 14 | chdir 't' if -d 't'; |
| 15 | @INC = '../lib'; |
| 16 | unshift @INC, '.'; |
| 17 | require Config; import Config; |
| 18 | if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) { |
| 19 | print "1..0\n"; |
| 20 | exit; |
| 21 | } |
| 22 | $| = 1; |
| 23 | } |
| 24 | |
| 25 | use strict; |
| 26 | use feature 'fc'; |
| 27 | |
| 28 | my $debug = $ENV{PERL_DEBUG_FULL_TEST} // 0; |
| 29 | |
| 30 | # Certain tests have been shown to be problematical for a few locales. Don't |
| 31 | # fail them unless at least this percentage of the tested locales fail. |
| 32 | my $acceptable_fold_failure_percentage = 5; |
| 33 | |
| 34 | use Dumpvalue; |
| 35 | |
| 36 | my $dumper = Dumpvalue->new( |
| 37 | tick => qq{"}, |
| 38 | quoteHighBit => 0, |
| 39 | unctrl => "quote" |
| 40 | ); |
| 41 | sub debug { |
| 42 | return unless $debug; |
| 43 | my($mess) = join "", @_; |
| 44 | chop $mess; |
| 45 | print $dumper->stringify($mess,1), "\n"; |
| 46 | } |
| 47 | |
| 48 | sub debugf { |
| 49 | printf @_ if $debug; |
| 50 | } |
| 51 | |
| 52 | my $have_setlocale = 0; |
| 53 | eval { |
| 54 | require POSIX; |
| 55 | import POSIX ':locale_h'; |
| 56 | $have_setlocale++; |
| 57 | }; |
| 58 | |
| 59 | # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1" |
| 60 | # and mingw32 uses said silly CRT |
| 61 | # This doesn't seem to be an issue any more, at least on Windows XP, |
| 62 | # so re-enable the tests for Windows XP onwards. |
| 63 | my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion && |
| 64 | join('.', (Win32::GetOSVersion())[1..2]) >= 5.1); |
| 65 | $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') && |
| 66 | $Config{cc} =~ /^(cl|gcc)/i); |
| 67 | |
| 68 | # UWIN seems to loop after taint tests, just skip for now |
| 69 | $have_setlocale = 0 if ($^O =~ /^uwin/); |
| 70 | |
| 71 | sub LC_ALL (); |
| 72 | |
| 73 | $a = 'abc %'; |
| 74 | |
| 75 | my $test_num = 0; |
| 76 | |
| 77 | sub ok { |
| 78 | my ($result, $message) = @_; |
| 79 | $message = "" unless defined $message; |
| 80 | |
| 81 | print 'not ' unless ($result); |
| 82 | print "ok " . ++$test_num; |
| 83 | print " $message"; |
| 84 | print "\n"; |
| 85 | } |
| 86 | |
| 87 | # First we'll do a lot of taint checking for locales. |
| 88 | # This is the easiest to test, actually, as any locale, |
| 89 | # even the default locale will taint under 'use locale'. |
| 90 | |
| 91 | sub is_tainted { # hello, camel two. |
| 92 | no warnings 'uninitialized' ; |
| 93 | my $dummy; |
| 94 | local $@; |
| 95 | not eval { $dummy = join("", @_), kill 0; 1 } |
| 96 | } |
| 97 | |
| 98 | sub check_taint ($;$) { |
| 99 | my $message_tail = $_[1] // ""; |
| 100 | $message_tail = ": $message_tail" if $message_tail; |
| 101 | ok is_tainted($_[0]), "verify that is tainted$message_tail"; |
| 102 | } |
| 103 | |
| 104 | sub check_taint_not ($;$) { |
| 105 | my $message_tail = $_[1] // ""; |
| 106 | $message_tail = ": $message_tail" if $message_tail; |
| 107 | ok((not is_tainted($_[0])), "verify that isn't tainted$message_tail"); |
| 108 | } |
| 109 | |
| 110 | "\tb\t" =~ /^m?(\s)(.*)\1$/; |
| 111 | check_taint_not $&, "not tainted outside 'use locale'"; |
| 112 | ; |
| 113 | |
| 114 | use locale; # engage locale and therefore locale taint. |
| 115 | |
| 116 | check_taint_not $a; |
| 117 | |
| 118 | check_taint uc($a); |
| 119 | check_taint "\U$a"; |
| 120 | check_taint ucfirst($a); |
| 121 | check_taint "\u$a"; |
| 122 | check_taint lc($a); |
| 123 | check_taint fc($a); |
| 124 | check_taint "\L$a"; |
| 125 | check_taint "\F$a"; |
| 126 | check_taint lcfirst($a); |
| 127 | check_taint "\l$a"; |
| 128 | |
| 129 | check_taint_not sprintf('%e', 123.456); |
| 130 | check_taint_not sprintf('%f', 123.456); |
| 131 | check_taint_not sprintf('%g', 123.456); |
| 132 | check_taint_not sprintf('%d', 123.456); |
| 133 | check_taint_not sprintf('%x', 123.456); |
| 134 | |
| 135 | $_ = $a; # untaint $_ |
| 136 | |
| 137 | $_ = uc($a); # taint $_ |
| 138 | |
| 139 | check_taint $_; |
| 140 | |
| 141 | /(\w)/; # taint $&, $`, $', $+, $1. |
| 142 | check_taint $&; |
| 143 | check_taint $`; |
| 144 | check_taint $'; |
| 145 | check_taint $+; |
| 146 | check_taint $1; |
| 147 | check_taint_not $2; |
| 148 | |
| 149 | /(.)/; # untaint $&, $`, $', $+, $1. |
| 150 | check_taint_not $&; |
| 151 | check_taint_not $`; |
| 152 | check_taint_not $'; |
| 153 | check_taint_not $+; |
| 154 | check_taint_not $1; |
| 155 | check_taint_not $2; |
| 156 | |
| 157 | /(\W)/; # taint $&, $`, $', $+, $1. |
| 158 | check_taint $&; |
| 159 | check_taint $`; |
| 160 | check_taint $'; |
| 161 | check_taint $+; |
| 162 | check_taint $1; |
| 163 | check_taint_not $2; |
| 164 | |
| 165 | /(\s)/; # taint $&, $`, $', $+, $1. |
| 166 | check_taint $&; |
| 167 | check_taint $`; |
| 168 | check_taint $'; |
| 169 | check_taint $+; |
| 170 | check_taint $1; |
| 171 | check_taint_not $2; |
| 172 | |
| 173 | /(\S)/; # taint $&, $`, $', $+, $1. |
| 174 | check_taint $&; |
| 175 | check_taint $`; |
| 176 | check_taint $'; |
| 177 | check_taint $+; |
| 178 | check_taint $1; |
| 179 | check_taint_not $2; |
| 180 | |
| 181 | $_ = $a; # untaint $_ |
| 182 | |
| 183 | check_taint_not $_; |
| 184 | |
| 185 | /(b)/; # this must not taint |
| 186 | check_taint_not $&; |
| 187 | check_taint_not $`; |
| 188 | check_taint_not $'; |
| 189 | check_taint_not $+; |
| 190 | check_taint_not $1; |
| 191 | check_taint_not $2; |
| 192 | |
| 193 | $_ = $a; # untaint $_ |
| 194 | |
| 195 | check_taint_not $_; |
| 196 | |
| 197 | $b = uc($a); # taint $b |
| 198 | s/(.+)/$b/; # this must taint only the $_ |
| 199 | |
| 200 | check_taint $_; |
| 201 | check_taint_not $&; |
| 202 | check_taint_not $`; |
| 203 | check_taint_not $'; |
| 204 | check_taint_not $+; |
| 205 | check_taint_not $1; |
| 206 | check_taint_not $2; |
| 207 | |
| 208 | $_ = $a; # untaint $_ |
| 209 | |
| 210 | s/(.+)/b/; # this must not taint |
| 211 | check_taint_not $_; |
| 212 | check_taint_not $&; |
| 213 | check_taint_not $`; |
| 214 | check_taint_not $'; |
| 215 | check_taint_not $+; |
| 216 | check_taint_not $1; |
| 217 | check_taint_not $2; |
| 218 | |
| 219 | $b = $a; # untaint $b |
| 220 | |
| 221 | ($b = $a) =~ s/\w/$&/; |
| 222 | check_taint $b; # $b should be tainted. |
| 223 | check_taint_not $a; # $a should be not. |
| 224 | |
| 225 | $_ = $a; # untaint $_ |
| 226 | |
| 227 | s/(\w)/\l$1/; # this must taint |
| 228 | check_taint $_; |
| 229 | check_taint $&; |
| 230 | check_taint $`; |
| 231 | check_taint $'; |
| 232 | check_taint $+; |
| 233 | check_taint $1; |
| 234 | check_taint_not $2; |
| 235 | |
| 236 | $_ = $a; # untaint $_ |
| 237 | |
| 238 | s/(\w)/\L$1/; # this must taint |
| 239 | check_taint $_; |
| 240 | check_taint $&; |
| 241 | check_taint $`; |
| 242 | check_taint $'; |
| 243 | check_taint $+; |
| 244 | check_taint $1; |
| 245 | check_taint_not $2; |
| 246 | |
| 247 | $_ = $a; # untaint $_ |
| 248 | |
| 249 | s/(\w)/\u$1/; # this must taint |
| 250 | check_taint $_; |
| 251 | check_taint $&; |
| 252 | check_taint $`; |
| 253 | check_taint $'; |
| 254 | check_taint $+; |
| 255 | check_taint $1; |
| 256 | check_taint_not $2; |
| 257 | |
| 258 | $_ = $a; # untaint $_ |
| 259 | |
| 260 | s/(\w)/\U$1/; # this must taint |
| 261 | check_taint $_; |
| 262 | check_taint $&; |
| 263 | check_taint $`; |
| 264 | check_taint $'; |
| 265 | check_taint $+; |
| 266 | check_taint $1; |
| 267 | check_taint_not $2; |
| 268 | |
| 269 | # After all this tainting $a should be cool. |
| 270 | |
| 271 | check_taint_not $a; |
| 272 | |
| 273 | { # This is just the previous tests copied here with a different |
| 274 | # compile-time pragma. |
| 275 | |
| 276 | use locale ':not_characters'; # engage restricted locale with different |
| 277 | # tainting rules |
| 278 | |
| 279 | check_taint_not $a; |
| 280 | |
| 281 | check_taint_not uc($a); |
| 282 | check_taint_not "\U$a"; |
| 283 | check_taint_not ucfirst($a); |
| 284 | check_taint_not "\u$a"; |
| 285 | check_taint_not lc($a); |
| 286 | check_taint_not fc($a); |
| 287 | check_taint_not "\L$a"; |
| 288 | check_taint_not "\F$a"; |
| 289 | check_taint_not lcfirst($a); |
| 290 | check_taint_not "\l$a"; |
| 291 | |
| 292 | check_taint_not sprintf('%e', 123.456); |
| 293 | check_taint_not sprintf('%f', 123.456); |
| 294 | check_taint_not sprintf('%g', 123.456); |
| 295 | check_taint_not sprintf('%d', 123.456); |
| 296 | check_taint_not sprintf('%x', 123.456); |
| 297 | |
| 298 | $_ = $a; # untaint $_ |
| 299 | |
| 300 | $_ = uc($a); # taint $_ |
| 301 | |
| 302 | check_taint_not $_; |
| 303 | |
| 304 | /(\w)/; # taint $&, $`, $', $+, $1. |
| 305 | check_taint_not $&; |
| 306 | check_taint_not $`; |
| 307 | check_taint_not $'; |
| 308 | check_taint_not $+; |
| 309 | check_taint_not $1; |
| 310 | check_taint_not $2; |
| 311 | |
| 312 | /(.)/; # untaint $&, $`, $', $+, $1. |
| 313 | check_taint_not $&; |
| 314 | check_taint_not $`; |
| 315 | check_taint_not $'; |
| 316 | check_taint_not $+; |
| 317 | check_taint_not $1; |
| 318 | check_taint_not $2; |
| 319 | |
| 320 | /(\W)/; # taint $&, $`, $', $+, $1. |
| 321 | check_taint_not $&; |
| 322 | check_taint_not $`; |
| 323 | check_taint_not $'; |
| 324 | check_taint_not $+; |
| 325 | check_taint_not $1; |
| 326 | check_taint_not $2; |
| 327 | |
| 328 | /(\s)/; # taint $&, $`, $', $+, $1. |
| 329 | check_taint_not $&; |
| 330 | check_taint_not $`; |
| 331 | check_taint_not $'; |
| 332 | check_taint_not $+; |
| 333 | check_taint_not $1; |
| 334 | check_taint_not $2; |
| 335 | |
| 336 | /(\S)/; # taint $&, $`, $', $+, $1. |
| 337 | check_taint_not $&; |
| 338 | check_taint_not $`; |
| 339 | check_taint_not $'; |
| 340 | check_taint_not $+; |
| 341 | check_taint_not $1; |
| 342 | check_taint_not $2; |
| 343 | |
| 344 | $_ = $a; # untaint $_ |
| 345 | |
| 346 | check_taint_not $_; |
| 347 | |
| 348 | /(b)/; # this must not taint |
| 349 | check_taint_not $&; |
| 350 | check_taint_not $`; |
| 351 | check_taint_not $'; |
| 352 | check_taint_not $+; |
| 353 | check_taint_not $1; |
| 354 | check_taint_not $2; |
| 355 | |
| 356 | $_ = $a; # untaint $_ |
| 357 | |
| 358 | check_taint_not $_; |
| 359 | |
| 360 | $b = uc($a); # taint $b |
| 361 | s/(.+)/$b/; # this must taint only the $_ |
| 362 | |
| 363 | check_taint_not $_; |
| 364 | check_taint_not $&; |
| 365 | check_taint_not $`; |
| 366 | check_taint_not $'; |
| 367 | check_taint_not $+; |
| 368 | check_taint_not $1; |
| 369 | check_taint_not $2; |
| 370 | |
| 371 | $_ = $a; # untaint $_ |
| 372 | |
| 373 | s/(.+)/b/; # this must not taint |
| 374 | check_taint_not $_; |
| 375 | check_taint_not $&; |
| 376 | check_taint_not $`; |
| 377 | check_taint_not $'; |
| 378 | check_taint_not $+; |
| 379 | check_taint_not $1; |
| 380 | check_taint_not $2; |
| 381 | |
| 382 | $b = $a; # untaint $b |
| 383 | |
| 384 | ($b = $a) =~ s/\w/$&/; |
| 385 | check_taint_not $b; # $b should be tainted. |
| 386 | check_taint_not $a; # $a should be not. |
| 387 | |
| 388 | $_ = $a; # untaint $_ |
| 389 | |
| 390 | s/(\w)/\l$1/; # this must taint |
| 391 | check_taint_not $_; |
| 392 | check_taint_not $&; |
| 393 | check_taint_not $`; |
| 394 | check_taint_not $'; |
| 395 | check_taint_not $+; |
| 396 | check_taint_not $1; |
| 397 | check_taint_not $2; |
| 398 | |
| 399 | $_ = $a; # untaint $_ |
| 400 | |
| 401 | s/(\w)/\L$1/; # this must taint |
| 402 | check_taint_not $_; |
| 403 | check_taint_not $&; |
| 404 | check_taint_not $`; |
| 405 | check_taint_not $'; |
| 406 | check_taint_not $+; |
| 407 | check_taint_not $1; |
| 408 | check_taint_not $2; |
| 409 | |
| 410 | $_ = $a; # untaint $_ |
| 411 | |
| 412 | s/(\w)/\u$1/; # this must taint |
| 413 | check_taint_not $_; |
| 414 | check_taint_not $&; |
| 415 | check_taint_not $`; |
| 416 | check_taint_not $'; |
| 417 | check_taint_not $+; |
| 418 | check_taint_not $1; |
| 419 | check_taint_not $2; |
| 420 | |
| 421 | $_ = $a; # untaint $_ |
| 422 | |
| 423 | s/(\w)/\U$1/; # this must taint |
| 424 | check_taint_not $_; |
| 425 | check_taint_not $&; |
| 426 | check_taint_not $`; |
| 427 | check_taint_not $'; |
| 428 | check_taint_not $+; |
| 429 | check_taint_not $1; |
| 430 | check_taint_not $2; |
| 431 | |
| 432 | # After all this tainting $a should be cool. |
| 433 | |
| 434 | check_taint_not $a; |
| 435 | } |
| 436 | |
| 437 | # Here are in scope of 'use locale' |
| 438 | |
| 439 | # I think we've seen quite enough of taint. |
| 440 | # Let us do some *real* locale work now, |
| 441 | # unless setlocale() is missing (i.e. minitest). |
| 442 | |
| 443 | unless ($have_setlocale) { |
| 444 | print "1..$test_num\n"; |
| 445 | exit; |
| 446 | } |
| 447 | |
| 448 | # The test number before our first setlocale() |
| 449 | my $final_without_setlocale = $test_num; |
| 450 | |
| 451 | # Find locales. |
| 452 | |
| 453 | debug "# Scanning for locales...\n"; |
| 454 | |
| 455 | # Note that it's okay that some languages have their native names |
| 456 | # capitalized here even though that's not "right". They are lowercased |
| 457 | # anyway later during the scanning process (and besides, some clueless |
| 458 | # vendor might have them capitalized erroneously anyway). |
| 459 | |
| 460 | my $locales = <<EOF; |
| 461 | Afrikaans:af:za:1 15 |
| 462 | Arabic:ar:dz eg sa:6 arabic8 |
| 463 | Brezhoneg Breton:br:fr:1 15 |
| 464 | Bulgarski Bulgarian:bg:bg:5 |
| 465 | Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC |
| 466 | Hrvatski Croatian:hr:hr:2 |
| 467 | Cymraeg Welsh:cy:cy:1 14 15 |
| 468 | Czech:cs:cz:2 |
| 469 | Dansk Danish:da:dk:1 15 |
| 470 | Nederlands Dutch:nl:be nl:1 15 |
| 471 | English American British:en:au ca gb ie nz us uk zw:1 15 cp850 |
| 472 | Esperanto:eo:eo:3 |
| 473 | Eesti Estonian:et:ee:4 6 13 |
| 474 | Suomi Finnish:fi:fi:1 15 |
| 475 | Flamish::fl:1 15 |
| 476 | Deutsch German:de:at be ch de lu:1 15 |
| 477 | Euskaraz Basque:eu:es fr:1 15 |
| 478 | Galego Galician:gl:es:1 15 |
| 479 | Ellada Greek:el:gr:7 g8 |
| 480 | Frysk:fy:nl:1 15 |
| 481 | Greenlandic:kl:gl:4 6 |
| 482 | Hebrew:iw:il:8 hebrew8 |
| 483 | Hungarian:hu:hu:2 |
| 484 | Indonesian:id:id:1 15 |
| 485 | Gaeilge Irish:ga:IE:1 14 15 |
| 486 | Italiano Italian:it:ch it:1 15 |
| 487 | Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis |
| 488 | Korean:ko:kr: |
| 489 | Latine Latin:la:va:1 15 |
| 490 | Latvian:lv:lv:4 6 13 |
| 491 | Lithuanian:lt:lt:4 6 13 |
| 492 | Macedonian:mk:mk:1 15 |
| 493 | Maltese:mt:mt:3 |
| 494 | Moldovan:mo:mo:2 |
| 495 | Norsk Norwegian:no no\@nynorsk nb nn:no:1 15 |
| 496 | Occitan:oc:es:1 15 |
| 497 | Polski Polish:pl:pl:2 |
| 498 | Rumanian:ro:ro:2 |
| 499 | Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866 |
| 500 | Serbski Serbian:sr:yu:5 |
| 501 | Slovak:sk:sk:2 |
| 502 | Slovene Slovenian:sl:si:2 |
| 503 | Sqhip Albanian:sq:sq:1 15 |
| 504 | Svenska Swedish:sv:fi se:1 15 |
| 505 | Thai:th:th:11 tis620 |
| 506 | Turkish:tr:tr:9 turkish8 |
| 507 | Yiddish:yi::1 15 |
| 508 | EOF |
| 509 | |
| 510 | if ($^O eq 'os390') { |
| 511 | # These cause heartburn. Broken locales? |
| 512 | $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//; |
| 513 | $locales =~ s/Thai:th:th:11 tis620\n//; |
| 514 | } |
| 515 | |
| 516 | sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ } |
| 517 | |
| 518 | if (in_utf8) { |
| 519 | require "lib/locale/utf8"; |
| 520 | } else { |
| 521 | require "lib/locale/latin1"; |
| 522 | } |
| 523 | |
| 524 | my @Locale; |
| 525 | my $Locale; |
| 526 | my @Alnum_; |
| 527 | |
| 528 | sub trylocale { |
| 529 | my $locale = shift; |
| 530 | return if grep { $locale eq $_ } @Locale; |
| 531 | return unless setlocale(LC_ALL, $locale); |
| 532 | my $badutf8; |
| 533 | { |
| 534 | local $SIG{__WARN__} = sub { |
| 535 | $badutf8 = $_[0] =~ /Malformed UTF-8/; |
| 536 | }; |
| 537 | $Locale =~ /UTF-?8/i; |
| 538 | } |
| 539 | |
| 540 | if ($badutf8) { |
| 541 | ok(0, "Locale name contains malformed utf8"); |
| 542 | return; |
| 543 | } |
| 544 | push @Locale, $locale; |
| 545 | } |
| 546 | |
| 547 | sub decode_encodings { |
| 548 | my @enc; |
| 549 | |
| 550 | foreach (split(/ /, shift)) { |
| 551 | if (/^(\d+)$/) { |
| 552 | push @enc, "ISO8859-$1"; |
| 553 | push @enc, "iso8859$1"; # HP |
| 554 | if ($1 eq '1') { |
| 555 | push @enc, "roman8"; # HP |
| 556 | } |
| 557 | } else { |
| 558 | push @enc, $_; |
| 559 | push @enc, "$_.UTF-8"; |
| 560 | } |
| 561 | } |
| 562 | if ($^O eq 'os390') { |
| 563 | push @enc, qw(IBM-037 IBM-819 IBM-1047); |
| 564 | } |
| 565 | |
| 566 | return @enc; |
| 567 | } |
| 568 | |
| 569 | trylocale("C"); |
| 570 | trylocale("POSIX"); |
| 571 | foreach (0..15) { |
| 572 | trylocale("ISO8859-$_"); |
| 573 | trylocale("iso8859$_"); |
| 574 | trylocale("iso8859-$_"); |
| 575 | trylocale("iso_8859_$_"); |
| 576 | trylocale("isolatin$_"); |
| 577 | trylocale("isolatin-$_"); |
| 578 | trylocale("iso_latin_$_"); |
| 579 | } |
| 580 | |
| 581 | # Sanitize the environment so that we can run the external 'locale' |
| 582 | # program without the taint mode getting grumpy. |
| 583 | |
| 584 | # $ENV{PATH} is special in VMS. |
| 585 | delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv}; |
| 586 | |
| 587 | # Other subversive stuff. |
| 588 | delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; |
| 589 | |
| 590 | if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) { |
| 591 | while (<LOCALES>) { |
| 592 | # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which |
| 593 | # ain't great when we're running this testPERL_UNICODE= so that utf8 |
| 594 | # locales will cause all IO hadles to default to (assume) utf8 |
| 595 | next unless utf8::valid($_); |
| 596 | chomp; |
| 597 | trylocale($_); |
| 598 | } |
| 599 | close(LOCALES); |
| 600 | } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') { |
| 601 | # The SYS$I18N_LOCALE logical name search list was not present on |
| 602 | # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions. |
| 603 | opendir(LOCALES, "SYS\$I18N_LOCALE:"); |
| 604 | while ($_ = readdir(LOCALES)) { |
| 605 | chomp; |
| 606 | trylocale($_); |
| 607 | } |
| 608 | close(LOCALES); |
| 609 | } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') { |
| 610 | |
| 611 | # OpenBSD doesn't have a locale executable, so reading /usr/share/locale |
| 612 | # is much easier and faster than the last resort method. |
| 613 | |
| 614 | opendir(LOCALES, '/usr/share/locale'); |
| 615 | while ($_ = readdir(LOCALES)) { |
| 616 | chomp; |
| 617 | trylocale($_); |
| 618 | } |
| 619 | close(LOCALES); |
| 620 | } else { |
| 621 | |
| 622 | # This is going to be slow. |
| 623 | |
| 624 | foreach my $locale (split(/\n/, $locales)) { |
| 625 | my ($locale_name, $language_codes, $country_codes, $encodings) = |
| 626 | split(/:/, $locale); |
| 627 | my @enc = decode_encodings($encodings); |
| 628 | foreach my $loc (split(/ /, $locale_name)) { |
| 629 | trylocale($loc); |
| 630 | foreach my $enc (@enc) { |
| 631 | trylocale("$loc.$enc"); |
| 632 | } |
| 633 | $loc = lc $loc; |
| 634 | foreach my $enc (@enc) { |
| 635 | trylocale("$loc.$enc"); |
| 636 | } |
| 637 | } |
| 638 | foreach my $lang (split(/ /, $language_codes)) { |
| 639 | trylocale($lang); |
| 640 | foreach my $country (split(/ /, $country_codes)) { |
| 641 | my $lc = "${lang}_${country}"; |
| 642 | trylocale($lc); |
| 643 | foreach my $enc (@enc) { |
| 644 | trylocale("$lc.$enc"); |
| 645 | } |
| 646 | my $lC = "${lang}_\U${country}"; |
| 647 | trylocale($lC); |
| 648 | foreach my $enc (@enc) { |
| 649 | trylocale("$lC.$enc"); |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | setlocale(LC_ALL, "C"); |
| 657 | |
| 658 | if ($^O eq 'darwin') { |
| 659 | # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895, |
| 660 | # Apple bug ID# 4139653. It also has a problem in Byelorussian. |
| 661 | (my $v) = $Config{osvers} =~ /^(\d+)/; |
| 662 | if ($v >= 8 and $v < 10) { |
| 663 | debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n"; |
| 664 | @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale; |
| 665 | } elsif ($v < 12) { |
| 666 | debug "# Skipping be_BY locales -- buggy in Darwin\n"; |
| 667 | @Locale = grep ! m/^be_BY\.CP1131$/, @Locale; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | @Locale = sort @Locale; |
| 672 | |
| 673 | debug "# Locales =\n"; |
| 674 | for ( @Locale ) { |
| 675 | debug "# $_\n"; |
| 676 | } |
| 677 | |
| 678 | my %Problem; |
| 679 | my %Okay; |
| 680 | my %Testing; |
| 681 | my @Added_alpha; # Alphas that aren't in the C locale. |
| 682 | my %test_names; |
| 683 | |
| 684 | sub report_result { |
| 685 | my ($Locale, $i, $pass_fail, $message) = @_; |
| 686 | $message //= ""; |
| 687 | $message = " ($message)" if $message; |
| 688 | unless ($pass_fail) { |
| 689 | $Problem{$i}{$Locale} = 1; |
| 690 | debug "# failed $i ($test_names{$i}) with locale '$Locale'$message\n"; |
| 691 | } else { |
| 692 | push @{$Okay{$i}}, $Locale; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | sub report_multi_result { |
| 697 | my ($Locale, $i, $results_ref) = @_; |
| 698 | |
| 699 | # $results_ref points to an array, each element of which is a character that was |
| 700 | # in error for this test numbered '$i'. If empty, the test passed |
| 701 | |
| 702 | my $message = ""; |
| 703 | if (@$results_ref) { |
| 704 | $message = join " ", "for", map { sprintf '\\x%02X', ord $_ } @$results_ref; |
| 705 | } |
| 706 | report_result($Locale, $i, @$results_ref == 0, $message); |
| 707 | } |
| 708 | |
| 709 | my $first_locales_test_number = $final_without_setlocale + 1; |
| 710 | my $locales_test_number; |
| 711 | my $not_necessarily_a_problem_test_number; |
| 712 | my $first_casing_test_number; |
| 713 | my $final_casing_test_number; |
| 714 | my %setlocale_failed; # List of locales that setlocale() didn't work on |
| 715 | |
| 716 | foreach $Locale (@Locale) { |
| 717 | $locales_test_number = $first_locales_test_number - 1; |
| 718 | debug "# Locale = $Locale\n"; |
| 719 | |
| 720 | unless (setlocale(LC_ALL, $Locale)) { |
| 721 | $setlocale_failed{$Locale} = $Locale; |
| 722 | next; |
| 723 | } |
| 724 | |
| 725 | # We test UTF-8 locales only under ':not_characters'; otherwise they have |
| 726 | # documented deficiencies. Non- UTF-8 locales are tested only under plain |
| 727 | # 'use locale', as otherwise we would have to convert everything in them |
| 728 | # to Unicode. |
| 729 | my $is_utf8_locale = $Locale =~ /UTF-?8/i; |
| 730 | |
| 731 | my %UPPER = (); # All alpha X for which uc(X) == X and lc(X) != X |
| 732 | my %lower = (); # All alpha X for which lc(X) == X and uc(X) != X |
| 733 | my %BoThCaSe = (); # All alpha X for which uc(X) == lc(X) == X |
| 734 | |
| 735 | if (! $is_utf8_locale) { |
| 736 | use locale; |
| 737 | @Alnum_ = sort grep /\w/, map { chr } 0..255; |
| 738 | |
| 739 | debug "# w = ", join("",@Alnum_), "\n"; |
| 740 | |
| 741 | # Sieve the uppercase and the lowercase. |
| 742 | |
| 743 | for (@Alnum_) { |
| 744 | if (/[^\d_]/) { # skip digits and the _ |
| 745 | if (uc($_) eq $_) { |
| 746 | $UPPER{$_} = $_; |
| 747 | } |
| 748 | if (lc($_) eq $_) { |
| 749 | $lower{$_} = $_; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | else { |
| 755 | use locale ':not_characters'; |
| 756 | @Alnum_ = sort grep /\w/, map { chr } 0..255; |
| 757 | debug "# w = ", join("",@Alnum_), "\n"; |
| 758 | for (@Alnum_) { |
| 759 | if (/[^\d_]/) { # skip digits and the _ |
| 760 | if (uc($_) eq $_) { |
| 761 | $UPPER{$_} = $_; |
| 762 | } |
| 763 | if (lc($_) eq $_) { |
| 764 | $lower{$_} = $_; |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | foreach (keys %UPPER) { |
| 770 | $BoThCaSe{$_}++ if exists $lower{$_}; |
| 771 | } |
| 772 | foreach (keys %lower) { |
| 773 | $BoThCaSe{$_}++ if exists $UPPER{$_}; |
| 774 | } |
| 775 | foreach (keys %BoThCaSe) { |
| 776 | delete $UPPER{$_}; |
| 777 | delete $lower{$_}; |
| 778 | } |
| 779 | |
| 780 | debug "# UPPER = ", join("", sort keys %UPPER ), "\n"; |
| 781 | debug "# lower = ", join("", sort keys %lower ), "\n"; |
| 782 | debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n"; |
| 783 | |
| 784 | my @failures; |
| 785 | my @fold_failures; |
| 786 | foreach my $x (sort keys %UPPER) { |
| 787 | my $ok; |
| 788 | my $fold_ok; |
| 789 | if ($is_utf8_locale) { |
| 790 | use locale ':not_characters'; |
| 791 | $ok = $x =~ /[[:upper:]]/; |
| 792 | $fold_ok = $x =~ /[[:lower:]]/i; |
| 793 | } |
| 794 | else { |
| 795 | use locale; |
| 796 | $ok = $x =~ /[[:upper:]]/; |
| 797 | $fold_ok = $x =~ /[[:lower:]]/i; |
| 798 | } |
| 799 | push @failures, $x unless $ok; |
| 800 | push @fold_failures, $x unless $fold_ok; |
| 801 | } |
| 802 | $locales_test_number++; |
| 803 | $first_casing_test_number = $locales_test_number; |
| 804 | $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/ matches all alpha X for which uc(X) == X and lc(X) != X'; |
| 805 | report_multi_result($Locale, $locales_test_number, \@failures); |
| 806 | |
| 807 | $locales_test_number++; |
| 808 | |
| 809 | $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/i matches all alpha X for which uc(X) == X and lc(X) != X'; |
| 810 | report_multi_result($Locale, $locales_test_number, \@fold_failures); |
| 811 | |
| 812 | undef @failures; |
| 813 | undef @fold_failures; |
| 814 | |
| 815 | foreach my $x (sort keys %lower) { |
| 816 | my $ok; |
| 817 | my $fold_ok; |
| 818 | if ($is_utf8_locale) { |
| 819 | use locale ':not_characters'; |
| 820 | $ok = $x =~ /[[:lower:]]/; |
| 821 | $fold_ok = $x =~ /[[:upper:]]/i; |
| 822 | } |
| 823 | else { |
| 824 | use locale; |
| 825 | $ok = $x =~ /[[:lower:]]/; |
| 826 | $fold_ok = $x =~ /[[:upper:]]/i; |
| 827 | } |
| 828 | push @failures, $x unless $ok; |
| 829 | push @fold_failures, $x unless $fold_ok; |
| 830 | } |
| 831 | |
| 832 | $locales_test_number++; |
| 833 | $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/ matches all alpha X for which lc(X) == X and uc(X) != X'; |
| 834 | report_multi_result($Locale, $locales_test_number, \@failures); |
| 835 | |
| 836 | $locales_test_number++; |
| 837 | $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/i matches all alpha X for which lc(X) == X and uc(X) != X'; |
| 838 | report_multi_result($Locale, $locales_test_number, \@fold_failures); |
| 839 | |
| 840 | { # Find the alphabetic characters that are not considered alphabetics |
| 841 | # in the default (C) locale. |
| 842 | |
| 843 | no locale; |
| 844 | |
| 845 | @Added_alpha = (); |
| 846 | for (keys %UPPER, keys %lower, keys %BoThCaSe) { |
| 847 | push(@Added_alpha, $_) if (/\W/); |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | @Added_alpha = sort @Added_alpha; |
| 852 | |
| 853 | debug "# Added_alpha = ", join("",@Added_alpha), "\n"; |
| 854 | |
| 855 | # Cross-check the whole 8-bit character set. |
| 856 | |
| 857 | ++$locales_test_number; |
| 858 | my @f; |
| 859 | $test_names{$locales_test_number} = 'Verify that \w and [:word:] are identical'; |
| 860 | for (map { chr } 0..255) { |
| 861 | if ($is_utf8_locale) { |
| 862 | use locale ':not_characters'; |
| 863 | push @f, $_ unless /[[:word:]]/ == /\w/; |
| 864 | } |
| 865 | else { |
| 866 | push @f, $_ unless /[[:word:]]/ == /\w/; |
| 867 | } |
| 868 | } |
| 869 | report_multi_result($Locale, $locales_test_number, \@f); |
| 870 | |
| 871 | ++$locales_test_number; |
| 872 | undef @f; |
| 873 | $test_names{$locales_test_number} = 'Verify that \d and [:digit:] are identical'; |
| 874 | for (map { chr } 0..255) { |
| 875 | if ($is_utf8_locale) { |
| 876 | use locale ':not_characters'; |
| 877 | push @f, $_ unless /[[:digit:]]/ == /\d/; |
| 878 | } |
| 879 | else { |
| 880 | push @f, $_ unless /[[:digit:]]/ == /\d/; |
| 881 | } |
| 882 | } |
| 883 | report_multi_result($Locale, $locales_test_number, \@f); |
| 884 | |
| 885 | ++$locales_test_number; |
| 886 | undef @f; |
| 887 | $test_names{$locales_test_number} = 'Verify that \s and [:space:] are identical'; |
| 888 | for (map { chr } 0..255) { |
| 889 | if ($is_utf8_locale) { |
| 890 | use locale ':not_characters'; |
| 891 | push @f, $_ unless /[[:space:]]/ == /\s/; |
| 892 | } |
| 893 | else { |
| 894 | push @f, $_ unless /[[:space:]]/ == /\s/; |
| 895 | } |
| 896 | } |
| 897 | report_multi_result($Locale, $locales_test_number, \@f); |
| 898 | |
| 899 | ++$locales_test_number; |
| 900 | undef @f; |
| 901 | $test_names{$locales_test_number} = 'Verify that [:posix:] and [:^posix:] are mutually exclusive'; |
| 902 | for (map { chr } 0..255) { |
| 903 | if ($is_utf8_locale) { |
| 904 | use locale ':not_characters'; |
| 905 | push @f, $_ unless (/[[:alpha:]]/ xor /[[:^alpha:]]/) || |
| 906 | (/[[:alnum:]]/ xor /[[:^alnum:]]/) || |
| 907 | (/[[:ascii:]]/ xor /[[:^ascii:]]/) || |
| 908 | (/[[:blank:]]/ xor /[[:^blank:]]/) || |
| 909 | (/[[:cntrl:]]/ xor /[[:^cntrl:]]/) || |
| 910 | (/[[:digit:]]/ xor /[[:^digit:]]/) || |
| 911 | (/[[:graph:]]/ xor /[[:^graph:]]/) || |
| 912 | (/[[:lower:]]/ xor /[[:^lower:]]/) || |
| 913 | (/[[:print:]]/ xor /[[:^print:]]/) || |
| 914 | (/[[:space:]]/ xor /[[:^space:]]/) || |
| 915 | (/[[:upper:]]/ xor /[[:^upper:]]/) || |
| 916 | (/[[:word:]]/ xor /[[:^word:]]/) || |
| 917 | (/[[:xdigit:]]/ xor /[[:^xdigit:]]/) || |
| 918 | |
| 919 | # effectively is what [:cased:] would be if it existed. |
| 920 | (/[[:upper:]]/i xor /[[:^upper:]]/i); |
| 921 | } |
| 922 | else { |
| 923 | push @f, $_ unless (/[[:alpha:]]/ xor /[[:^alpha:]]/) || |
| 924 | (/[[:alnum:]]/ xor /[[:^alnum:]]/) || |
| 925 | (/[[:ascii:]]/ xor /[[:^ascii:]]/) || |
| 926 | (/[[:blank:]]/ xor /[[:^blank:]]/) || |
| 927 | (/[[:cntrl:]]/ xor /[[:^cntrl:]]/) || |
| 928 | (/[[:digit:]]/ xor /[[:^digit:]]/) || |
| 929 | (/[[:graph:]]/ xor /[[:^graph:]]/) || |
| 930 | (/[[:lower:]]/ xor /[[:^lower:]]/) || |
| 931 | (/[[:print:]]/ xor /[[:^print:]]/) || |
| 932 | (/[[:space:]]/ xor /[[:^space:]]/) || |
| 933 | (/[[:upper:]]/ xor /[[:^upper:]]/) || |
| 934 | (/[[:word:]]/ xor /[[:^word:]]/) || |
| 935 | (/[[:xdigit:]]/ xor /[[:^xdigit:]]/) || |
| 936 | (/[[:upper:]]/i xor /[[:^upper:]]/i); |
| 937 | } |
| 938 | } |
| 939 | report_multi_result($Locale, $locales_test_number, \@f); |
| 940 | |
| 941 | # The rules for the relationships are given in: |
| 942 | # http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html |
| 943 | |
| 944 | ++$locales_test_number; |
| 945 | undef @f; |
| 946 | $test_names{$locales_test_number} = 'Verify that [:lower:] is a subset of [:alpha:]'; |
| 947 | for (map { chr } 0..255) { |
| 948 | if ($is_utf8_locale) { |
| 949 | use locale ':not_characters'; |
| 950 | push @f, $_ if /[[:lower:]]/ and ! /[[:alpha:]]/; |
| 951 | } |
| 952 | else { |
| 953 | push @f, $_ if /[[:lower:]]/ and ! /[[:alpha:]]/; |
| 954 | } |
| 955 | } |
| 956 | report_multi_result($Locale, $locales_test_number, \@f); |
| 957 | |
| 958 | ++$locales_test_number; |
| 959 | undef @f; |
| 960 | $test_names{$locales_test_number} = 'Verify that [:upper:] is a subset of [:alpha:]'; |
| 961 | for (map { chr } 0..255) { |
| 962 | if ($is_utf8_locale) { |
| 963 | use locale ':not_characters'; |
| 964 | push @f, $_ if /[[:upper:]]/ and ! /[[:alpha:]]/; |
| 965 | } |
| 966 | else { |
| 967 | push @f, $_ if /[[:upper:]]/ and ! /[[:alpha:]]/; |
| 968 | } |
| 969 | } |
| 970 | report_multi_result($Locale, $locales_test_number, \@f); |
| 971 | |
| 972 | ++$locales_test_number; |
| 973 | undef @f; |
| 974 | $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/i is a subset of [:alpha:]'; |
| 975 | for (map { chr } 0..255) { |
| 976 | if ($is_utf8_locale) { |
| 977 | use locale ':not_characters'; |
| 978 | push @f, $_ if /[[:lower:]]/i and ! /[[:alpha:]]/; |
| 979 | } |
| 980 | else { |
| 981 | push @f, $_ if /[[:lower:]]/i and ! /[[:alpha:]]/; |
| 982 | } |
| 983 | } |
| 984 | report_multi_result($Locale, $locales_test_number, \@f); |
| 985 | |
| 986 | ++$locales_test_number; |
| 987 | undef @f; |
| 988 | $test_names{$locales_test_number} = 'Verify that [:alpha:] is a subset of [:alnum:]'; |
| 989 | for (map { chr } 0..255) { |
| 990 | if ($is_utf8_locale) { |
| 991 | use locale ':not_characters'; |
| 992 | push @f, $_ if /[[:alpha:]]/ and ! /[[:alnum:]]/; |
| 993 | } |
| 994 | else { |
| 995 | push @f, $_ if /[[:alpha:]]/ and ! /[[:alnum:]]/; |
| 996 | } |
| 997 | } |
| 998 | report_multi_result($Locale, $locales_test_number, \@f); |
| 999 | |
| 1000 | ++$locales_test_number; |
| 1001 | undef @f; |
| 1002 | $test_names{$locales_test_number} = 'Verify that [:digit:] is a subset of [:alnum:]'; |
| 1003 | for (map { chr } 0..255) { |
| 1004 | if ($is_utf8_locale) { |
| 1005 | use locale ':not_characters'; |
| 1006 | push @f, $_ if /[[:digit:]]/ and ! /[[:alnum:]]/; |
| 1007 | } |
| 1008 | else { |
| 1009 | push @f, $_ if /[[:digit:]]/ and ! /[[:alnum:]]/; |
| 1010 | } |
| 1011 | } |
| 1012 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1013 | |
| 1014 | ++$locales_test_number; |
| 1015 | undef @f; |
| 1016 | $test_names{$locales_test_number} = 'Verify that [:digit:] is a subset of [:xdigit:]'; |
| 1017 | for (map { chr } 0..255) { |
| 1018 | if ($is_utf8_locale) { |
| 1019 | use locale ':not_characters'; |
| 1020 | push @f, $_ if /[[:digit:]]/ and ! /[[:xdigit:]]/; |
| 1021 | } |
| 1022 | else { |
| 1023 | push @f, $_ if /[[:digit:]]/ and ! /[[:xdigit:]]/; |
| 1024 | } |
| 1025 | } |
| 1026 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1027 | |
| 1028 | ++$locales_test_number; |
| 1029 | undef @f; |
| 1030 | $test_names{$locales_test_number} = 'Verify that [:alnum:] is a subset of [:graph:]'; |
| 1031 | for (map { chr } 0..255) { |
| 1032 | if ($is_utf8_locale) { |
| 1033 | use locale ':not_characters'; |
| 1034 | push @f, $_ if /[[:alnum:]]/ and ! /[[:graph:]]/; |
| 1035 | } |
| 1036 | else { |
| 1037 | push @f, $_ if /[[:alnum:]]/ and ! /[[:graph:]]/; |
| 1038 | } |
| 1039 | } |
| 1040 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1041 | |
| 1042 | # Note that xdigit doesn't have to be a subset of alnum |
| 1043 | |
| 1044 | ++$locales_test_number; |
| 1045 | undef @f; |
| 1046 | $test_names{$locales_test_number} = 'Verify that [:xdigit:] is a subset of [:graph:]'; |
| 1047 | for (map { chr } 0..255) { |
| 1048 | if ($is_utf8_locale) { |
| 1049 | use locale ':not_characters'; |
| 1050 | push @f, $_ if /[[:xdigit:]]/ and ! /[[:graph:]]/; |
| 1051 | } |
| 1052 | else { |
| 1053 | push @f, $_ if /[[:xdigit:]]/ and ! /[[:graph:]]/; |
| 1054 | } |
| 1055 | } |
| 1056 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1057 | |
| 1058 | ++$locales_test_number; |
| 1059 | undef @f; |
| 1060 | $test_names{$locales_test_number} = 'Verify that [:punct:] is a subset of [:graph:]'; |
| 1061 | for (map { chr } 0..255) { |
| 1062 | if ($is_utf8_locale) { |
| 1063 | use locale ':not_characters'; |
| 1064 | push @f, $_ if /[[:punct:]]/ and ! /[[:graph:]]/; |
| 1065 | } |
| 1066 | else { |
| 1067 | push @f, $_ if /[[:punct:]]/ and ! /[[:graph:]]/; |
| 1068 | } |
| 1069 | } |
| 1070 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1071 | |
| 1072 | ++$locales_test_number; |
| 1073 | undef @f; |
| 1074 | $test_names{$locales_test_number} = 'Verify that [:blank:] is a subset of [:space:]'; |
| 1075 | for (map { chr } 0..255) { |
| 1076 | if ($is_utf8_locale) { |
| 1077 | use locale ':not_characters'; |
| 1078 | push @f, $_ if /[[:blank:]]/ and ! /[[:space:]]/; |
| 1079 | } |
| 1080 | else { |
| 1081 | push @f, $_ if /[[:blank:]]/ and ! /[[:space:]]/; |
| 1082 | } |
| 1083 | } |
| 1084 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1085 | |
| 1086 | ++$locales_test_number; |
| 1087 | undef @f; |
| 1088 | $test_names{$locales_test_number} = 'Verify that [:graph:] is a subset of [:print:]'; |
| 1089 | for (map { chr } 0..255) { |
| 1090 | if ($is_utf8_locale) { |
| 1091 | use locale ':not_characters'; |
| 1092 | push @f, $_ if /[[:graph:]]/ and ! /[[:print:]]/; |
| 1093 | } |
| 1094 | else { |
| 1095 | push @f, $_ if /[[:graph:]]/ and ! /[[:print:]]/; |
| 1096 | } |
| 1097 | } |
| 1098 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1099 | |
| 1100 | ++$locales_test_number; |
| 1101 | undef @f; |
| 1102 | $test_names{$locales_test_number} = 'Verify that isn\'t both [:cntrl:] and [:print:]'; |
| 1103 | for (map { chr } 0..255) { |
| 1104 | if ($is_utf8_locale) { |
| 1105 | use locale ':not_characters'; |
| 1106 | push @f, $_ if (/[[:print:]]/ and /[[:cntrl:]]/); |
| 1107 | } |
| 1108 | else { |
| 1109 | push @f, $_ if (/[[:print:]]/ and /[[:cntrl:]]/); |
| 1110 | } |
| 1111 | } |
| 1112 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1113 | |
| 1114 | ++$locales_test_number; |
| 1115 | undef @f; |
| 1116 | $test_names{$locales_test_number} = 'Verify that isn\'t both [:alnum:] and [:punct:]'; |
| 1117 | for (map { chr } 0..255) { |
| 1118 | if ($is_utf8_locale) { |
| 1119 | use locale ':not_characters'; |
| 1120 | push @f, $_ if /[[:alnum:]]/ and /[[:punct:]]/; |
| 1121 | } |
| 1122 | else { |
| 1123 | push @f, $_ if /[[:alnum:]]/ and /[[:punct:]]/; |
| 1124 | } |
| 1125 | } |
| 1126 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1127 | |
| 1128 | ++$locales_test_number; |
| 1129 | undef @f; |
| 1130 | $test_names{$locales_test_number} = 'Verify that isn\'t both [:xdigit:] and [:punct:]'; |
| 1131 | for (map { chr } 0..255) { |
| 1132 | if ($is_utf8_locale) { |
| 1133 | use locale ':not_characters'; |
| 1134 | push @f, $_ if (/[[:punct:]]/ and /[[:xdigit:]]/); |
| 1135 | } |
| 1136 | else { |
| 1137 | push @f, $_ if (/[[:punct:]]/ and /[[:xdigit:]]/); |
| 1138 | } |
| 1139 | } |
| 1140 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1141 | |
| 1142 | ++$locales_test_number; |
| 1143 | undef @f; |
| 1144 | $test_names{$locales_test_number} = 'Verify that isn\'t both [:graph:] and [:space:]'; |
| 1145 | for (map { chr } 0..255) { |
| 1146 | if ($is_utf8_locale) { |
| 1147 | use locale ':not_characters'; |
| 1148 | push @f, $_ if (/[[:graph:]]/ and /[[:space:]]/); |
| 1149 | } |
| 1150 | else { |
| 1151 | push @f, $_ if (/[[:graph:]]/ and /[[:space:]]/); |
| 1152 | } |
| 1153 | } |
| 1154 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1155 | |
| 1156 | $final_casing_test_number = $locales_test_number; |
| 1157 | |
| 1158 | # Test for read-only scalars' locale vs non-locale comparisons. |
| 1159 | |
| 1160 | { |
| 1161 | no locale; |
| 1162 | my $ok; |
| 1163 | $a = "qwerty"; |
| 1164 | if ($is_utf8_locale) { |
| 1165 | use locale ':not_characters'; |
| 1166 | $ok = ($a cmp "qwerty") == 0; |
| 1167 | } |
| 1168 | else { |
| 1169 | use locale; |
| 1170 | $ok = ($a cmp "qwerty") == 0; |
| 1171 | } |
| 1172 | report_result($Locale, ++$locales_test_number, $ok); |
| 1173 | $test_names{$locales_test_number} = 'Verify that cmp works with a read-only scalar; no- vs locale'; |
| 1174 | } |
| 1175 | |
| 1176 | { |
| 1177 | my ($from, $to, $lesser, $greater, |
| 1178 | @test, %test, $test, $yes, $no, $sign); |
| 1179 | |
| 1180 | ++$locales_test_number; |
| 1181 | $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work'; |
| 1182 | $not_necessarily_a_problem_test_number = $locales_test_number; |
| 1183 | for (0..9) { |
| 1184 | # Select a slice. |
| 1185 | $from = int(($_*@Alnum_)/10); |
| 1186 | $to = $from + int(@Alnum_/10); |
| 1187 | $to = $#Alnum_ if ($to > $#Alnum_); |
| 1188 | $lesser = join('', @Alnum_[$from..$to]); |
| 1189 | # Select a slice one character on. |
| 1190 | $from++; $to++; |
| 1191 | $to = $#Alnum_ if ($to > $#Alnum_); |
| 1192 | $greater = join('', @Alnum_[$from..$to]); |
| 1193 | if ($is_utf8_locale) { |
| 1194 | use locale ':not_characters'; |
| 1195 | ($yes, $no, $sign) = ($lesser lt $greater |
| 1196 | ? (" ", "not ", 1) |
| 1197 | : ("not ", " ", -1)); |
| 1198 | } |
| 1199 | else { |
| 1200 | use locale; |
| 1201 | ($yes, $no, $sign) = ($lesser lt $greater |
| 1202 | ? (" ", "not ", 1) |
| 1203 | : ("not ", " ", -1)); |
| 1204 | } |
| 1205 | # all these tests should FAIL (return 0). Exact lt or gt cannot |
| 1206 | # be tested because in some locales, say, eacute and E may test |
| 1207 | # equal. |
| 1208 | @test = |
| 1209 | ( |
| 1210 | $no.' ($lesser le $greater)', # 1 |
| 1211 | 'not ($lesser ne $greater)', # 2 |
| 1212 | ' ($lesser eq $greater)', # 3 |
| 1213 | $yes.' ($lesser ge $greater)', # 4 |
| 1214 | $yes.' ($lesser ge $greater)', # 5 |
| 1215 | $yes.' ($greater le $lesser )', # 7 |
| 1216 | 'not ($greater ne $lesser )', # 8 |
| 1217 | ' ($greater eq $lesser )', # 9 |
| 1218 | $no.' ($greater ge $lesser )', # 10 |
| 1219 | 'not (($lesser cmp $greater) == -($sign))' # 11 |
| 1220 | ); |
| 1221 | @test{@test} = 0 x @test; |
| 1222 | $test = 0; |
| 1223 | for my $ti (@test) { |
| 1224 | if ($is_utf8_locale) { |
| 1225 | use locale ':not_characters'; |
| 1226 | $test{$ti} = eval $ti; |
| 1227 | } |
| 1228 | else { |
| 1229 | # Already in 'use locale'; |
| 1230 | $test{$ti} = eval $ti; |
| 1231 | } |
| 1232 | $test ||= $test{$ti} |
| 1233 | } |
| 1234 | report_result($Locale, $locales_test_number, $test == 0); |
| 1235 | if ($test) { |
| 1236 | debug "# lesser = '$lesser'\n"; |
| 1237 | debug "# greater = '$greater'\n"; |
| 1238 | debug "# lesser cmp greater = ", |
| 1239 | $lesser cmp $greater, "\n"; |
| 1240 | debug "# greater cmp lesser = ", |
| 1241 | $greater cmp $lesser, "\n"; |
| 1242 | debug "# (greater) from = $from, to = $to\n"; |
| 1243 | for my $ti (@test) { |
| 1244 | debugf("# %-40s %-4s", $ti, |
| 1245 | $test{$ti} ? 'FAIL' : 'ok'); |
| 1246 | if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) { |
| 1247 | debugf("(%s == %4d)", $1, eval $1); |
| 1248 | } |
| 1249 | debug "\n#"; |
| 1250 | } |
| 1251 | |
| 1252 | last; |
| 1253 | } |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | my $ok1; |
| 1258 | my $ok2; |
| 1259 | my $ok3; |
| 1260 | my $ok4; |
| 1261 | my $ok5; |
| 1262 | my $ok6; |
| 1263 | my $ok7; |
| 1264 | my $ok8; |
| 1265 | my $ok9; |
| 1266 | my $ok10; |
| 1267 | my $ok11; |
| 1268 | my $ok12; |
| 1269 | my $ok13; |
| 1270 | my $ok14; |
| 1271 | my $ok15; |
| 1272 | my $ok16; |
| 1273 | |
| 1274 | my $c; |
| 1275 | my $d; |
| 1276 | my $e; |
| 1277 | my $f; |
| 1278 | my $g; |
| 1279 | |
| 1280 | if (! $is_utf8_locale) { |
| 1281 | use locale; |
| 1282 | |
| 1283 | my ($x, $y) = (1.23, 1.23); |
| 1284 | |
| 1285 | $a = "$x"; |
| 1286 | printf ''; # printf used to reset locale to "C" |
| 1287 | $b = "$y"; |
| 1288 | $ok1 = $a eq $b; |
| 1289 | |
| 1290 | $c = "$x"; |
| 1291 | my $z = sprintf ''; # sprintf used to reset locale to "C" |
| 1292 | $d = "$y"; |
| 1293 | $ok2 = $c eq $d; |
| 1294 | { |
| 1295 | |
| 1296 | use warnings; |
| 1297 | my $w = 0; |
| 1298 | local $SIG{__WARN__} = |
| 1299 | sub { |
| 1300 | print "# @_\n"; |
| 1301 | $w++; |
| 1302 | }; |
| 1303 | |
| 1304 | # The == (among other ops) used to warn for locales |
| 1305 | # that had something else than "." as the radix character. |
| 1306 | |
| 1307 | $ok3 = $c == 1.23; |
| 1308 | $ok4 = $c == $x; |
| 1309 | $ok5 = $c == $d; |
| 1310 | { |
| 1311 | no locale; |
| 1312 | |
| 1313 | $e = "$x"; |
| 1314 | |
| 1315 | $ok6 = $e == 1.23; |
| 1316 | $ok7 = $e == $x; |
| 1317 | $ok8 = $e == $c; |
| 1318 | } |
| 1319 | |
| 1320 | $f = "1.23"; |
| 1321 | $g = 2.34; |
| 1322 | |
| 1323 | $ok9 = $f == 1.23; |
| 1324 | $ok10 = $f == $x; |
| 1325 | $ok11 = $f == $c; |
| 1326 | $ok12 = abs(($f + $g) - 3.57) < 0.01; |
| 1327 | $ok13 = $w == 0; |
| 1328 | $ok14 = $ok15 = $ok16 = 1; # Skip for non-utf8 locales |
| 1329 | } |
| 1330 | } |
| 1331 | else { |
| 1332 | use locale ':not_characters'; |
| 1333 | |
| 1334 | my ($x, $y) = (1.23, 1.23); |
| 1335 | $a = "$x"; |
| 1336 | printf ''; # printf used to reset locale to "C" |
| 1337 | $b = "$y"; |
| 1338 | $ok1 = $a eq $b; |
| 1339 | |
| 1340 | $c = "$x"; |
| 1341 | my $z = sprintf ''; # sprintf used to reset locale to "C" |
| 1342 | $d = "$y"; |
| 1343 | $ok2 = $c eq $d; |
| 1344 | { |
| 1345 | use warnings; |
| 1346 | my $w = 0; |
| 1347 | local $SIG{__WARN__} = |
| 1348 | sub { |
| 1349 | print "# @_\n"; |
| 1350 | $w++; |
| 1351 | }; |
| 1352 | $ok3 = $c == 1.23; |
| 1353 | $ok4 = $c == $x; |
| 1354 | $ok5 = $c == $d; |
| 1355 | { |
| 1356 | no locale; |
| 1357 | $e = "$x"; |
| 1358 | |
| 1359 | $ok6 = $e == 1.23; |
| 1360 | $ok7 = $e == $x; |
| 1361 | $ok8 = $e == $c; |
| 1362 | } |
| 1363 | |
| 1364 | $f = "1.23"; |
| 1365 | $g = 2.34; |
| 1366 | |
| 1367 | $ok9 = $f == 1.23; |
| 1368 | $ok10 = $f == $x; |
| 1369 | $ok11 = $f == $c; |
| 1370 | $ok12 = abs(($f + $g) - 3.57) < 0.01; |
| 1371 | $ok13 = $w == 0; |
| 1372 | |
| 1373 | # Look for non-ASCII error messages, and verify that the first |
| 1374 | # such is in UTF-8 (the others almost certainly will be like the |
| 1375 | # first). |
| 1376 | $ok14 = 1; |
| 1377 | foreach my $err (keys %!) { |
| 1378 | use Errno; |
| 1379 | $! = eval "&Errno::$err"; # Convert to strerror() output |
| 1380 | my $strerror = "$!"; |
| 1381 | if ("$strerror" =~ /\P{ASCII}/) { |
| 1382 | my $utf8_strerror = $strerror; |
| 1383 | utf8::upgrade($utf8_strerror); |
| 1384 | |
| 1385 | # If $! was already in UTF-8, the upgrade was a no-op; |
| 1386 | # otherwise they will be different byte strings. |
| 1387 | use bytes; |
| 1388 | $ok14 = $utf8_strerror eq $strerror; |
| 1389 | last; |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | # Similarly, we verify that a non-ASCII radix is in UTF-8. This |
| 1394 | # also catches if there is a disparity between sprintf and |
| 1395 | # stringification. |
| 1396 | |
| 1397 | my $string_g = "$g"; |
| 1398 | |
| 1399 | my $utf8_string_g = "$g"; |
| 1400 | utf8::upgrade($utf8_string_g); |
| 1401 | |
| 1402 | my $utf8_sprintf_g = sprintf("%g", $g); |
| 1403 | utf8::upgrade($utf8_sprintf_g); |
| 1404 | use bytes; |
| 1405 | $ok15 = $utf8_string_g eq $string_g; |
| 1406 | $ok16 = $utf8_sprintf_g eq $string_g; |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | report_result($Locale, ++$locales_test_number, $ok1); |
| 1411 | $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results'; |
| 1412 | my $first_a_test = $locales_test_number; |
| 1413 | |
| 1414 | debug "# $first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n"; |
| 1415 | |
| 1416 | report_result($Locale, ++$locales_test_number, $ok2); |
| 1417 | $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results'; |
| 1418 | |
| 1419 | my $first_c_test = $locales_test_number; |
| 1420 | |
| 1421 | report_result($Locale, ++$locales_test_number, $ok3); |
| 1422 | $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant'; |
| 1423 | |
| 1424 | report_result($Locale, ++$locales_test_number, $ok4); |
| 1425 | $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar'; |
| 1426 | |
| 1427 | report_result($Locale, ++$locales_test_number, $ok5); |
| 1428 | $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf'; |
| 1429 | |
| 1430 | debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n"; |
| 1431 | |
| 1432 | report_result($Locale, ++$locales_test_number, $ok6); |
| 1433 | $test_names{$locales_test_number} = 'Verify that can assign stringified under inner no-locale block'; |
| 1434 | my $first_e_test = $locales_test_number; |
| 1435 | |
| 1436 | report_result($Locale, ++$locales_test_number, $ok7); |
| 1437 | $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale'; |
| 1438 | |
| 1439 | report_result($Locale, ++$locales_test_number, $ok8); |
| 1440 | $test_names{$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale'; |
| 1441 | |
| 1442 | debug "# $first_e_test..$locales_test_number: \$e = $e, no locale\n"; |
| 1443 | |
| 1444 | report_result($Locale, ++$locales_test_number, $ok9); |
| 1445 | $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant'; |
| 1446 | my $first_f_test = $locales_test_number; |
| 1447 | |
| 1448 | report_result($Locale, ++$locales_test_number, $ok10); |
| 1449 | $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar'; |
| 1450 | |
| 1451 | report_result($Locale, ++$locales_test_number, $ok11); |
| 1452 | $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar and an intervening sprintf'; |
| 1453 | |
| 1454 | report_result($Locale, ++$locales_test_number, $ok12); |
| 1455 | $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix can participate in an addition and function call as numeric'; |
| 1456 | |
| 1457 | report_result($Locale, ++$locales_test_number, $ok13); |
| 1458 | $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot'; |
| 1459 | |
| 1460 | report_result($Locale, ++$locales_test_number, $ok14); |
| 1461 | $test_names{$locales_test_number} = 'Verify that non-ASCII UTF-8 error messages are in UTF-8'; |
| 1462 | |
| 1463 | report_result($Locale, ++$locales_test_number, $ok15); |
| 1464 | $test_names{$locales_test_number} = 'Verify that a number with a UTF-8 radix has a UTF-8 stringification'; |
| 1465 | |
| 1466 | report_result($Locale, ++$locales_test_number, $ok16); |
| 1467 | $test_names{$locales_test_number} = 'Verify that a sprintf of a number with a UTF-8 radix yields UTF-8'; |
| 1468 | |
| 1469 | debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n"; |
| 1470 | |
| 1471 | # Does taking lc separately differ from taking |
| 1472 | # the lc "in-line"? (This was the bug 19990704.002, change #3568.) |
| 1473 | # The bug was in the caching of the 'o'-magic. |
| 1474 | if (! $is_utf8_locale) { |
| 1475 | use locale; |
| 1476 | |
| 1477 | sub lcA { |
| 1478 | my $lc0 = lc $_[0]; |
| 1479 | my $lc1 = lc $_[1]; |
| 1480 | return $lc0 cmp $lc1; |
| 1481 | } |
| 1482 | |
| 1483 | sub lcB { |
| 1484 | return lc($_[0]) cmp lc($_[1]); |
| 1485 | } |
| 1486 | |
| 1487 | my $x = "ab"; |
| 1488 | my $y = "aa"; |
| 1489 | my $z = "AB"; |
| 1490 | |
| 1491 | report_result($Locale, ++$locales_test_number, |
| 1492 | lcA($x, $y) == 1 && lcB($x, $y) == 1 || |
| 1493 | lcA($x, $z) == 0 && lcB($x, $z) == 0); |
| 1494 | } |
| 1495 | else { |
| 1496 | use locale ':not_characters'; |
| 1497 | |
| 1498 | sub lcC { |
| 1499 | my $lc0 = lc $_[0]; |
| 1500 | my $lc1 = lc $_[1]; |
| 1501 | return $lc0 cmp $lc1; |
| 1502 | } |
| 1503 | |
| 1504 | sub lcD { |
| 1505 | return lc($_[0]) cmp lc($_[1]); |
| 1506 | } |
| 1507 | |
| 1508 | my $x = "ab"; |
| 1509 | my $y = "aa"; |
| 1510 | my $z = "AB"; |
| 1511 | |
| 1512 | report_result($Locale, ++$locales_test_number, |
| 1513 | lcC($x, $y) == 1 && lcD($x, $y) == 1 || |
| 1514 | lcC($x, $z) == 0 && lcD($x, $z) == 0); |
| 1515 | } |
| 1516 | $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp'; |
| 1517 | |
| 1518 | # Does lc of an UPPER (if different from the UPPER) match |
| 1519 | # case-insensitively the UPPER, and does the UPPER match |
| 1520 | # case-insensitively the lc of the UPPER. And vice versa. |
| 1521 | { |
| 1522 | use locale; |
| 1523 | no utf8; |
| 1524 | my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/; |
| 1525 | |
| 1526 | my @f = (); |
| 1527 | ++$locales_test_number; |
| 1528 | $test_names{$locales_test_number} = 'Verify case insensitive matching works'; |
| 1529 | foreach my $x (sort keys %UPPER) { |
| 1530 | if (! $is_utf8_locale) { |
| 1531 | my $y = lc $x; |
| 1532 | next unless uc $y eq $x; |
| 1533 | print "# UPPER $x lc $y ", |
| 1534 | $x =~ /$y/i ? 1 : 0, " ", |
| 1535 | $y =~ /$x/i ? 1 : 0, "\n" if 0; |
| 1536 | # |
| 1537 | # If $x and $y contain regular expression characters |
| 1538 | # AND THEY lowercase (/i) to regular expression characters, |
| 1539 | # regcomp() will be mightily confused. No, the \Q doesn't |
| 1540 | # help here (maybe regex engine internal lowercasing |
| 1541 | # is done after the \Q?) An example of this happening is |
| 1542 | # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS): |
| 1543 | # the chr(173) (the "[") is the lowercase of the chr(235). |
| 1544 | # |
| 1545 | # Similarly losing EBCDIC locales include cs_cz, cs_CZ, |
| 1546 | # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!), |
| 1547 | # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT, |
| 1548 | # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037, |
| 1549 | # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU, |
| 1550 | # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR. |
| 1551 | # |
| 1552 | # Similar things can happen even under (bastardised) |
| 1553 | # non-EBCDIC locales: in many European countries before the |
| 1554 | # advent of ISO 8859-x nationally customised versions of |
| 1555 | # ISO 646 were devised, reusing certain punctuation |
| 1556 | # characters for modified characters needed by the |
| 1557 | # country/language. For example, the "|" might have |
| 1558 | # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS. |
| 1559 | # |
| 1560 | if ($x =~ $re || $y =~ $re) { |
| 1561 | print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n"; |
| 1562 | next; |
| 1563 | } |
| 1564 | # With utf8 both will fail since the locale concept |
| 1565 | # of upper/lower does not work well in Unicode. |
| 1566 | push @f, $x unless $x =~ /$y/i == $y =~ /$x/i; |
| 1567 | |
| 1568 | # fc is not a locale concept, so Perl uses lc for it. |
| 1569 | push @f, $x unless lc $x eq fc $x; |
| 1570 | } |
| 1571 | else { |
| 1572 | use locale ':not_characters'; |
| 1573 | my $y = lc $x; |
| 1574 | next unless uc $y eq $x; |
| 1575 | print "# UPPER $x lc $y ", |
| 1576 | $x =~ /$y/i ? 1 : 0, " ", |
| 1577 | $y =~ /$x/i ? 1 : 0, "\n" if 0; |
| 1578 | |
| 1579 | # Here, we can fully test things, unlike plain 'use locale', |
| 1580 | # because this form does work well with Unicode |
| 1581 | push @f, $x unless $x =~ /$y/i && $y =~ /$x/i; |
| 1582 | |
| 1583 | # The places where Unicode's lc is different from fc are |
| 1584 | # skipped here by virtue of the 'next unless uc...' line above |
| 1585 | push @f, $x unless lc $x eq fc $x; |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | foreach my $x (sort keys %lower) { |
| 1590 | if (! $is_utf8_locale) { |
| 1591 | my $y = uc $x; |
| 1592 | next unless lc $y eq $x; |
| 1593 | print "# lower $x uc $y ", |
| 1594 | $x =~ /$y/i ? 1 : 0, " ", |
| 1595 | $y =~ /$x/i ? 1 : 0, "\n" if 0; |
| 1596 | if ($x =~ $re || $y =~ $re) { # See above. |
| 1597 | print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n"; |
| 1598 | next; |
| 1599 | } |
| 1600 | # With utf8 both will fail since the locale concept |
| 1601 | # of upper/lower does not work well in Unicode. |
| 1602 | push @f, $x unless $x =~ /$y/i == $y =~ /$x/i; |
| 1603 | |
| 1604 | push @f, $x unless lc $x eq fc $x; |
| 1605 | } |
| 1606 | else { |
| 1607 | use locale ':not_characters'; |
| 1608 | my $y = uc $x; |
| 1609 | next unless lc $y eq $x; |
| 1610 | print "# lower $x uc $y ", |
| 1611 | $x =~ /$y/i ? 1 : 0, " ", |
| 1612 | $y =~ /$x/i ? 1 : 0, "\n" if 0; |
| 1613 | push @f, $x unless $x =~ /$y/i && $y =~ /$x/i; |
| 1614 | |
| 1615 | push @f, $x unless lc $x eq fc $x; |
| 1616 | } |
| 1617 | } |
| 1618 | report_multi_result($Locale, $locales_test_number, \@f); |
| 1619 | } |
| 1620 | |
| 1621 | # [perl #109318] |
| 1622 | { |
| 1623 | my @f = (); |
| 1624 | ++$locales_test_number; |
| 1625 | $test_names{$locales_test_number} = 'Verify atof with locale radix and negative exponent'; |
| 1626 | |
| 1627 | my $radix = POSIX::localeconv()->{decimal_point}; |
| 1628 | my @nums = ( |
| 1629 | "3.14e+9", "3${radix}14e+9", "3.14e-9", "3${radix}14e-9", |
| 1630 | "-3.14e+9", "-3${radix}14e+9", "-3.14e-9", "-3${radix}14e-9", |
| 1631 | ); |
| 1632 | |
| 1633 | if (! $is_utf8_locale) { |
| 1634 | use locale; |
| 1635 | for my $num (@nums) { |
| 1636 | push @f, $num |
| 1637 | unless sprintf("%g", $num) =~ /3.+14/; |
| 1638 | } |
| 1639 | } |
| 1640 | else { |
| 1641 | use locale ':not_characters'; |
| 1642 | for my $num (@nums) { |
| 1643 | push @f, $num |
| 1644 | unless sprintf("%g", $num) =~ /3.+14/; |
| 1645 | } |
| 1646 | } |
| 1647 | |
| 1648 | report_result($Locale, $locales_test_number, @f == 0); |
| 1649 | if (@f) { |
| 1650 | print "# failed $locales_test_number locale '$Locale' numbers @f\n" |
| 1651 | } |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | my $final_locales_test_number = $locales_test_number; |
| 1656 | |
| 1657 | # Recount the errors. |
| 1658 | |
| 1659 | foreach ($first_locales_test_number..$final_locales_test_number) { |
| 1660 | if (%setlocale_failed) { |
| 1661 | print "not "; |
| 1662 | } |
| 1663 | elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) { |
| 1664 | if (defined $not_necessarily_a_problem_test_number |
| 1665 | && $_ == $not_necessarily_a_problem_test_number) |
| 1666 | { |
| 1667 | print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n"; |
| 1668 | print "# It usually indicates a problem in the environment,\n"; |
| 1669 | print "# not in Perl itself.\n"; |
| 1670 | } |
| 1671 | if ($Okay{$_} && ($_ >= $first_casing_test_number |
| 1672 | && $_ <= $final_casing_test_number)) |
| 1673 | { |
| 1674 | # Round to nearest .1% |
| 1675 | my $percent_fail = (int(.5 + (1000 * scalar(keys $Problem{$_}) |
| 1676 | / scalar(@Locale)))) |
| 1677 | / 10; |
| 1678 | if (! $debug && $percent_fail < $acceptable_fold_failure_percentage) |
| 1679 | { |
| 1680 | $test_names{$_} .= 'TODO'; |
| 1681 | print "# ", 100 - $percent_fail, "% of locales pass the following test, so it is likely that the failures\n"; |
| 1682 | print "# are errors in the locale definitions. The test is marked TODO, as the\n"; |
| 1683 | print "# problem is not likely to be Perl's\n"; |
| 1684 | } |
| 1685 | } |
| 1686 | print "#\n"; |
| 1687 | if ($debug) { |
| 1688 | print "# The code points that had this failure are given above. Look for lines\n"; |
| 1689 | print "# that match 'failed $_'\n"; |
| 1690 | } |
| 1691 | else { |
| 1692 | print "# For more details, rerun, with environment variable PERL_DEBUG_FULL_TEST=1.\n"; |
| 1693 | print "# Then look at that output for lines that match 'failed $_'\n"; |
| 1694 | } |
| 1695 | print "not "; |
| 1696 | } |
| 1697 | print "ok $_"; |
| 1698 | if (defined $test_names{$_}) { |
| 1699 | # If TODO is in the test name, make it thus |
| 1700 | my $todo = $test_names{$_} =~ s/TODO\s*//; |
| 1701 | print " $test_names{$_}"; |
| 1702 | print " # TODO" if $todo; |
| 1703 | } |
| 1704 | print "\n"; |
| 1705 | } |
| 1706 | |
| 1707 | $test_num = $final_locales_test_number; |
| 1708 | |
| 1709 | { # perl #115808 |
| 1710 | use warnings; |
| 1711 | my $warned = 0; |
| 1712 | local $SIG{__WARN__} = sub { |
| 1713 | $warned = $_[0] =~ /uninitialized/; |
| 1714 | }; |
| 1715 | my $z = "y" . setlocale(&POSIX::LC_ALL, "xyzzy"); |
| 1716 | ok($warned, "variable set to setlocale(BAD LOCALE) is considered uninitialized"); |
| 1717 | } |
| 1718 | |
| 1719 | # Test that tainting and case changing works on utf8 strings. These tests are |
| 1720 | # placed last to avoid disturbing the hard-coded test numbers that existed at |
| 1721 | # the time these were added above this in this file. |
| 1722 | # This also tests that locale overrides unicode_strings in the same scope for |
| 1723 | # non-utf8 strings. |
| 1724 | setlocale(LC_ALL, "C"); |
| 1725 | { |
| 1726 | use locale; |
| 1727 | use feature 'unicode_strings'; |
| 1728 | |
| 1729 | foreach my $function ("uc", "ucfirst", "lc", "lcfirst", "fc") { |
| 1730 | my @list; # List of code points to test for $function |
| 1731 | |
| 1732 | # Used to calculate the changed case for ASCII characters by using the |
| 1733 | # ord, instead of using one of the functions under test. |
| 1734 | my $ascii_case_change_delta; |
| 1735 | my $above_latin1_case_change_delta; # Same for the specific ords > 255 |
| 1736 | # that we use |
| 1737 | |
| 1738 | # We test an ASCII character, which should change case and be tainted; |
| 1739 | # a Latin1 character, which shouldn't change case under this C locale, |
| 1740 | # and is tainted. |
| 1741 | # an above-Latin1 character that when the case is changed would cross |
| 1742 | # the 255/256 boundary, so doesn't change case and isn't tainted |
| 1743 | # (the \x{149} is one of these, but changes into 2 characters, the |
| 1744 | # first one of which doesn't cross the boundary. |
| 1745 | # the final one in each list is an above-Latin1 character whose case |
| 1746 | # does change, and shouldn't be tainted. The code below uses its |
| 1747 | # position in its list as a marker to indicate that it, unlike the |
| 1748 | # other code points above ASCII, has a successful case change |
| 1749 | if ($function =~ /^u/) { |
| 1750 | @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}"); |
| 1751 | $ascii_case_change_delta = -32; |
| 1752 | $above_latin1_case_change_delta = -1; |
| 1753 | } |
| 1754 | else { |
| 1755 | @list = ("", "A", "\xC0", "\x{17F}", "\x{100}"); |
| 1756 | $ascii_case_change_delta = +32; |
| 1757 | $above_latin1_case_change_delta = +1; |
| 1758 | } |
| 1759 | foreach my $is_utf8_locale (0 .. 1) { |
| 1760 | foreach my $j (0 .. $#list) { |
| 1761 | my $char = $list[$j]; |
| 1762 | |
| 1763 | for my $encoded_in_utf8 (0 .. 1) { |
| 1764 | my $should_be; |
| 1765 | my $changed; |
| 1766 | if (! $is_utf8_locale) { |
| 1767 | $should_be = ($j == $#list) |
| 1768 | ? chr(ord($char) + $above_latin1_case_change_delta) |
| 1769 | : (length $char == 0 || ord($char) > 127) |
| 1770 | ? $char |
| 1771 | : chr(ord($char) + $ascii_case_change_delta); |
| 1772 | |
| 1773 | # This monstrosity is in order to avoid using an eval, |
| 1774 | # which might perturb the results |
| 1775 | $changed = ($function eq "uc") |
| 1776 | ? uc($char) |
| 1777 | : ($function eq "ucfirst") |
| 1778 | ? ucfirst($char) |
| 1779 | : ($function eq "lc") |
| 1780 | ? lc($char) |
| 1781 | : ($function eq "lcfirst") |
| 1782 | ? lcfirst($char) |
| 1783 | : ($function eq "fc") |
| 1784 | ? fc($char) |
| 1785 | : die("Unexpected function \"$function\""); |
| 1786 | } |
| 1787 | else { |
| 1788 | { |
| 1789 | no locale; |
| 1790 | |
| 1791 | # For utf8-locales the case changing functions |
| 1792 | # should work just like they do outside of locale. |
| 1793 | # Can use eval here because not testing it when |
| 1794 | # not in locale. |
| 1795 | $should_be = eval "$function('$char')"; |
| 1796 | die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if $@; |
| 1797 | |
| 1798 | } |
| 1799 | use locale ':not_characters'; |
| 1800 | $changed = ($function eq "uc") |
| 1801 | ? uc($char) |
| 1802 | : ($function eq "ucfirst") |
| 1803 | ? ucfirst($char) |
| 1804 | : ($function eq "lc") |
| 1805 | ? lc($char) |
| 1806 | : ($function eq "lcfirst") |
| 1807 | ? lcfirst($char) |
| 1808 | : ($function eq "fc") |
| 1809 | ? fc($char) |
| 1810 | : die("Unexpected function \"$function\""); |
| 1811 | } |
| 1812 | ok($changed eq $should_be, |
| 1813 | "$function(\"$char\") in C locale " |
| 1814 | . (($is_utf8_locale) |
| 1815 | ? "(use locale ':not_characters'" |
| 1816 | : "(use locale") |
| 1817 | . (($encoded_in_utf8) |
| 1818 | ? "; encoded in utf8)" |
| 1819 | : "; not encoded in utf8)") |
| 1820 | . " should be \"$should_be\", got \"$changed\""); |
| 1821 | |
| 1822 | # Tainting shouldn't happen for utf8 locales, empty |
| 1823 | # strings, or those characters above 255. |
| 1824 | (! $is_utf8_locale && length($char) > 0 && ord($char) < 256) |
| 1825 | ? check_taint($changed) |
| 1826 | : check_taint_not($changed); |
| 1827 | |
| 1828 | # Use UTF-8 next time through the loop |
| 1829 | utf8::upgrade($char); |
| 1830 | } |
| 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | # Give final advice. |
| 1837 | |
| 1838 | my $didwarn = 0; |
| 1839 | |
| 1840 | foreach ($first_locales_test_number..$final_locales_test_number) { |
| 1841 | if ($Problem{$_}) { |
| 1842 | my @f = sort keys %{ $Problem{$_} }; |
| 1843 | my $f = join(" ", @f); |
| 1844 | $f =~ s/(.{50,60}) /$1\n#\t/g; |
| 1845 | print |
| 1846 | "#\n", |
| 1847 | "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n", |
| 1848 | "#\t", $f, "\n#\n", |
| 1849 | "# on your system may have errors because the locale test $_\n", |
| 1850 | "# \"$test_names{$_}\"\n", |
| 1851 | "# failed in ", (@f == 1 ? "that locale" : "those locales"), |
| 1852 | ".\n"; |
| 1853 | print <<EOW; |
| 1854 | # |
| 1855 | # If your users are not using these locales you are safe for the moment, |
| 1856 | # but please report this failure first to perlbug\@perl.com using the |
| 1857 | # perlbug script (as described in the INSTALL file) so that the exact |
| 1858 | # details of the failures can be sorted out first and then your operating |
| 1859 | # system supplier can be alerted about these anomalies. |
| 1860 | # |
| 1861 | EOW |
| 1862 | $didwarn = 1; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | # Tell which locales were okay and which were not. |
| 1867 | |
| 1868 | if ($didwarn) { |
| 1869 | my (@s, @F); |
| 1870 | |
| 1871 | foreach my $l (@Locale) { |
| 1872 | my $p = 0; |
| 1873 | if ($setlocale_failed{$l}) { |
| 1874 | $p++; |
| 1875 | } |
| 1876 | else { |
| 1877 | foreach my $t |
| 1878 | ($first_locales_test_number..$final_locales_test_number) |
| 1879 | { |
| 1880 | $p++ if $Problem{$t}{$l}; |
| 1881 | } |
| 1882 | } |
| 1883 | push @s, $l if $p == 0; |
| 1884 | push @F, $l unless $p == 0; |
| 1885 | } |
| 1886 | |
| 1887 | if (@s) { |
| 1888 | my $s = join(" ", @s); |
| 1889 | $s =~ s/(.{50,60}) /$1\n#\t/g; |
| 1890 | |
| 1891 | warn |
| 1892 | "# The following locales\n#\n", |
| 1893 | "#\t", $s, "\n#\n", |
| 1894 | "# tested okay.\n#\n", |
| 1895 | } else { |
| 1896 | warn "# None of your locales were fully okay.\n"; |
| 1897 | } |
| 1898 | |
| 1899 | if (@F) { |
| 1900 | my $F = join(" ", @F); |
| 1901 | $F =~ s/(.{50,60}) /$1\n#\t/g; |
| 1902 | |
| 1903 | warn |
| 1904 | "# The following locales\n#\n", |
| 1905 | "#\t", $F, "\n#\n", |
| 1906 | "# had problems.\n#\n", |
| 1907 | "# For more details, rerun, with environment variable PERL_DEBUG_FULL_TEST=1.\n"; |
| 1908 | } else { |
| 1909 | warn "# None of your locales were broken.\n"; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | print "1..$test_num\n"; |
| 1914 | |
| 1915 | # eof |