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.
8 # To make a TODO test, add the string 'TODO' to its %test_names value
10 binmode STDOUT, ':utf8';
11 binmode STDERR, ':utf8';
17 require Config; import Config;
18 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
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;
36 my $dumper = Dumpvalue->new(
43 my($mess) = join "", @_;
45 print $dumper->stringify($mess,1), "\n";
52 my $have_setlocale = 0;
55 import POSIX ':locale_h';
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);
68 # UWIN seems to loop after taint tests, just skip for now
69 $have_setlocale = 0 if ($^O =~ /^uwin/);
78 my ($result, $message) = @_;
79 $message = "" unless defined $message;
81 print 'not ' unless ($result);
82 print "ok " . ++$test_num;
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'.
91 sub is_tainted { # hello, camel two.
92 no warnings 'uninitialized' ;
95 not eval { $dummy = join("", @_), kill 0; 1 }
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";
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");
110 "\tb\t" =~ /^m?(\s)(.*)\1$/;
111 check_taint_not $&, "not tainted outside 'use locale'";
114 use locale; # engage locale and therefore locale taint.
120 check_taint ucfirst($a);
126 check_taint lcfirst($a);
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);
135 $_ = $a; # untaint $_
137 $_ = uc($a); # taint $_
141 /(\w)/; # taint $&, $`, $', $+, $1.
149 /(.)/; # untaint $&, $`, $', $+, $1.
157 /(\W)/; # taint $&, $`, $', $+, $1.
165 /(\s)/; # taint $&, $`, $', $+, $1.
173 /(\S)/; # taint $&, $`, $', $+, $1.
181 $_ = $a; # untaint $_
185 /(b)/; # this must not taint
193 $_ = $a; # untaint $_
197 $b = uc($a); # taint $b
198 s/(.+)/$b/; # this must taint only the $_
208 $_ = $a; # untaint $_
210 s/(.+)/b/; # this must not taint
219 $b = $a; # untaint $b
221 ($b = $a) =~ s/\w/$&/;
222 check_taint $b; # $b should be tainted.
223 check_taint_not $a; # $a should be not.
225 $_ = $a; # untaint $_
227 s/(\w)/\l$1/; # this must taint
236 $_ = $a; # untaint $_
238 s/(\w)/\L$1/; # this must taint
247 $_ = $a; # untaint $_
249 s/(\w)/\u$1/; # this must taint
258 $_ = $a; # untaint $_
260 s/(\w)/\U$1/; # this must taint
269 # After all this tainting $a should be cool.
273 { # This is just the previous tests copied here with a different
274 # compile-time pragma.
276 use locale ':not_characters'; # engage restricted locale with different
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";
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);
298 $_ = $a; # untaint $_
300 $_ = uc($a); # taint $_
304 /(\w)/; # taint $&, $`, $', $+, $1.
312 /(.)/; # untaint $&, $`, $', $+, $1.
320 /(\W)/; # taint $&, $`, $', $+, $1.
328 /(\s)/; # taint $&, $`, $', $+, $1.
336 /(\S)/; # taint $&, $`, $', $+, $1.
344 $_ = $a; # untaint $_
348 /(b)/; # this must not taint
356 $_ = $a; # untaint $_
360 $b = uc($a); # taint $b
361 s/(.+)/$b/; # this must taint only the $_
371 $_ = $a; # untaint $_
373 s/(.+)/b/; # this must not taint
382 $b = $a; # untaint $b
384 ($b = $a) =~ s/\w/$&/;
385 check_taint_not $b; # $b should be tainted.
386 check_taint_not $a; # $a should be not.
388 $_ = $a; # untaint $_
390 s/(\w)/\l$1/; # this must taint
399 $_ = $a; # untaint $_
401 s/(\w)/\L$1/; # this must taint
410 $_ = $a; # untaint $_
412 s/(\w)/\u$1/; # this must taint
421 $_ = $a; # untaint $_
423 s/(\w)/\U$1/; # this must taint
432 # After all this tainting $a should be cool.
437 # Here are in scope of 'use locale'
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).
443 unless ($have_setlocale) {
444 print "1..$test_num\n";
448 # The test number before our first setlocale()
449 my $final_without_setlocale = $test_num;
453 debug "# Scanning for locales...\n";
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).
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
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
473 Eesti Estonian:et:ee:4 6 13
474 Suomi Finnish:fi:fi: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
481 Greenlandic:kl:gl:4 6
482 Hebrew:iw:il:8 hebrew8
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
489 Latine Latin:la:va:1 15
491 Lithuanian:lt:lt:4 6 13
492 Macedonian:mk:mk:1 15
495 Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
497 Polski Polish:pl:pl:2
499 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
500 Serbski Serbian:sr:yu:5
502 Slovene Slovenian:sl:si:2
503 Sqhip Albanian:sq:sq:1 15
504 Svenska Swedish:sv:fi se:1 15
506 Turkish:tr:tr:9 turkish8
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//;
516 sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
519 require "lib/locale/utf8";
521 require "lib/locale/latin1";
530 return if grep { $locale eq $_ } @Locale;
531 return unless setlocale(LC_ALL, $locale);
534 local $SIG{__WARN__} = sub {
535 $badutf8 = $_[0] =~ /Malformed UTF-8/;
537 $Locale =~ /UTF-?8/i;
541 ok(0, "Locale name contains malformed utf8");
544 push @Locale, $locale;
547 sub decode_encodings {
550 foreach (split(/ /, shift)) {
552 push @enc, "ISO8859-$1";
553 push @enc, "iso8859$1"; # HP
555 push @enc, "roman8"; # HP
559 push @enc, "$_.UTF-8";
562 if ($^O eq 'os390') {
563 push @enc, qw(IBM-037 IBM-819 IBM-1047);
572 trylocale("ISO8859-$_");
573 trylocale("iso8859$_");
574 trylocale("iso8859-$_");
575 trylocale("iso_8859_$_");
576 trylocale("isolatin$_");
577 trylocale("isolatin-$_");
578 trylocale("iso_latin_$_");
581 # Sanitize the environment so that we can run the external 'locale'
582 # program without the taint mode getting grumpy.
584 # $ENV{PATH} is special in VMS.
585 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
587 # Other subversive stuff.
588 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
590 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
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($_);
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)) {
609 } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
611 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
612 # is much easier and faster than the last resort method.
614 opendir(LOCALES, '/usr/share/locale');
615 while ($_ = readdir(LOCALES)) {
622 # This is going to be slow.
624 foreach my $locale (split(/\n/, $locales)) {
625 my ($locale_name, $language_codes, $country_codes, $encodings) =
627 my @enc = decode_encodings($encodings);
628 foreach my $loc (split(/ /, $locale_name)) {
630 foreach my $enc (@enc) {
631 trylocale("$loc.$enc");
634 foreach my $enc (@enc) {
635 trylocale("$loc.$enc");
638 foreach my $lang (split(/ /, $language_codes)) {
640 foreach my $country (split(/ /, $country_codes)) {
641 my $lc = "${lang}_${country}";
643 foreach my $enc (@enc) {
644 trylocale("$lc.$enc");
646 my $lC = "${lang}_\U${country}";
648 foreach my $enc (@enc) {
649 trylocale("$lC.$enc");
656 setlocale(LC_ALL, "C");
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;
666 debug "# Skipping be_BY locales -- buggy in Darwin\n";
667 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
671 @Locale = sort @Locale;
673 debug "# Locales =\n";
681 my @Neoalpha; # Alnums that aren't in the C locale.
685 my ($Locale, $i, $test, $message) = @_;
687 $message = " ($message)" if $message;
689 $Problem{$i}{$Locale} = 1;
690 debug "# failed $i with locale '$Locale'$message\n";
692 push @{$Okay{$i}}, $Locale;
696 my $first_locales_test_number = $final_without_setlocale + 1;
697 my $locales_test_number;
698 my $not_necessarily_a_problem_test_number;
699 my $first_casing_test_number;
700 my $final_casing_test_number;
701 my %setlocale_failed; # List of locales that setlocale() didn't work on
703 foreach $Locale (@Locale) {
704 $locales_test_number = $first_locales_test_number - 1;
705 debug "# Locale = $Locale\n";
707 unless (setlocale(LC_ALL, $Locale)) {
708 $setlocale_failed{$Locale} = $Locale;
712 # We test UTF-8 locales only under ':not_characters'; otherwise they have
713 # documented deficiencies. Non- UTF-8 locales are tested only under plain
714 # 'use locale', as otherwise we would have to convert everything in them
716 my $is_utf8_locale = $Locale =~ /UTF-?8/i;
722 if (! $is_utf8_locale) {
724 @Alnum_ = sort grep /\w/, map { chr } 0..255;
726 debug "# w = ", join("",@Alnum_), "\n";
728 # Sieve the uppercase and the lowercase.
731 if (/[^\d_]/) { # skip digits and the _
742 use locale ':not_characters';
743 @Alnum_ = sort grep /\w/, map { chr } 0..255;
744 debug "# w = ", join("",@Alnum_), "\n";
746 if (/[^\d_]/) { # skip digits and the _
756 foreach (keys %UPPER) {
757 $BoThCaSe{$_}++ if exists $lower{$_};
759 foreach (keys %lower) {
760 $BoThCaSe{$_}++ if exists $UPPER{$_};
762 foreach (keys %BoThCaSe) {
767 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
768 debug "# lower = ", join("", sort keys %lower ), "\n";
769 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
773 foreach my $x (sort keys %UPPER) {
776 if ($is_utf8_locale) {
777 use locale ':not_characters';
778 $ok = $x =~ /[[:upper:]]/;
779 $fold_ok = $x =~ /[[:lower:]]/i;
783 $ok = $x =~ /[[:upper:]]/;
784 $fold_ok = $x =~ /[[:lower:]]/i;
786 push @failures, $x unless $ok;
787 push @fold_failures, $x unless $fold_ok;
790 $locales_test_number++;
791 $first_casing_test_number = $locales_test_number;
792 $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/ matches sieved uppercase characters.';
793 $message = 'Failed for ' . join ", ", @failures if @failures;
794 tryneoalpha($Locale, $locales_test_number, scalar @failures == 0, $message);
797 $locales_test_number++;
799 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/i matches sieved uppercase characters.';
800 $message = 'Failed for ' . join ", ", @fold_failures if @fold_failures;
801 tryneoalpha($Locale, $locales_test_number, scalar @fold_failures == 0, $message);
805 undef @fold_failures;
807 foreach my $x (sort keys %lower) {
810 if ($is_utf8_locale) {
811 use locale ':not_characters';
812 $ok = $x =~ /[[:lower:]]/;
813 $fold_ok = $x =~ /[[:upper:]]/i;
817 $ok = $x =~ /[[:lower:]]/;
818 $fold_ok = $x =~ /[[:upper:]]/i;
820 push @failures, $x unless $ok;
821 push @fold_failures, $x unless $fold_ok;
824 $locales_test_number++;
825 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/ matches sieved lowercase characters.';
826 $message = 'Failed for ' . join ", ", @failures if @failures;
827 tryneoalpha($Locale, $locales_test_number, scalar @failures == 0, $message);
829 $locales_test_number++;
830 $final_casing_test_number = $locales_test_number;
831 $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/i matches sieved lowercase characters.';
832 $message = 'Failed for ' . join ", ", @fold_failures if @fold_failures;
833 tryneoalpha($Locale, $locales_test_number, scalar @fold_failures == 0, $message);
835 { # Find the alphabetic characters that are not considered alphabetics
836 # in the default (C) locale.
841 for (keys %UPPER, keys %lower) {
842 push(@Neoalpha, $_) if (/\W/);
846 @Neoalpha = sort @Neoalpha;
848 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
850 my $first_Neoalpha_test_number = $locales_test_number + 1;
851 my $final_Neoalpha_test_number = $first_Neoalpha_test_number + 3;
852 if (@Neoalpha == 0) {
853 # If we have no Neoalphas the remaining tests are no-ops.
854 debug "# no Neoalpha, skipping tests $first_Neoalpha_test_number..$final_Neoalpha_test_number for locale '$Locale'\n";
855 foreach ($locales_test_number+1..$final_Neoalpha_test_number) {
856 push @{$Okay{$_}}, $Locale;
857 $locales_test_number++;
863 my $word = join('', @Neoalpha);
865 ++$locales_test_number;
866 $test_names{$locales_test_number} = 'Verify that alnums outside the C locale match \w';
868 if ($is_utf8_locale) {
869 use locale ':not_characters';
870 $ok = $word =~ /^(\w+)$/;
873 # Already in 'use locale'; this tests that exiting scopes works
874 $ok = $word =~ /^(\w+)$/;
876 tryneoalpha($Locale, $locales_test_number, $ok);
878 # Cross-check the whole 8-bit character set.
880 ++$locales_test_number;
881 $test_names{$locales_test_number} = 'Verify that \w and \W are mutually exclusive, as are \d, \D; \s, \S';
882 for (map { chr } 0..255) {
883 if ($is_utf8_locale) {
884 use locale ':not_characters';
885 $ok = (/\w/ xor /\W/) ||
890 $ok = (/\w/ xor /\W/) ||
894 tryneoalpha($Locale, $locales_test_number, $ok);
897 # Test for read-only scalars' locale vs non-locale comparisons.
902 if ($is_utf8_locale) {
903 use locale ':not_characters';
904 $ok = ($a cmp "qwerty") == 0;
908 $ok = ($a cmp "qwerty") == 0;
910 tryneoalpha($Locale, ++$locales_test_number, $ok);
911 $test_names{$locales_test_number} = 'Verify that cmp works with a read-only scalar; no- vs locale';
915 my ($from, $to, $lesser, $greater,
916 @test, %test, $test, $yes, $no, $sign);
918 ++$locales_test_number;
919 $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work';
920 $not_necessarily_a_problem_test_number = $locales_test_number;
923 $from = int(($_*@Alnum_)/10);
924 $to = $from + int(@Alnum_/10);
925 $to = $#Alnum_ if ($to > $#Alnum_);
926 $lesser = join('', @Alnum_[$from..$to]);
927 # Select a slice one character on.
929 $to = $#Alnum_ if ($to > $#Alnum_);
930 $greater = join('', @Alnum_[$from..$to]);
931 if ($is_utf8_locale) {
932 use locale ':not_characters';
933 ($yes, $no, $sign) = ($lesser lt $greater
935 : ("not ", " ", -1));
939 ($yes, $no, $sign) = ($lesser lt $greater
941 : ("not ", " ", -1));
943 # all these tests should FAIL (return 0).
944 # Exact lt or gt cannot be tested because
945 # in some locales, say, eacute and E may test equal.
948 $no.' ($lesser le $greater)', # 1
949 'not ($lesser ne $greater)', # 2
950 ' ($lesser eq $greater)', # 3
951 $yes.' ($lesser ge $greater)', # 4
952 $yes.' ($lesser ge $greater)', # 5
953 $yes.' ($greater le $lesser )', # 7
954 'not ($greater ne $lesser )', # 8
955 ' ($greater eq $lesser )', # 9
956 $no.' ($greater ge $lesser )', # 10
957 'not (($lesser cmp $greater) == -($sign))' # 11
959 @test{@test} = 0 x @test;
962 if ($is_utf8_locale) {
963 use locale ':not_characters';
964 $test{$ti} = eval $ti;
967 # Already in 'use locale';
968 $test{$ti} = eval $ti;
972 tryneoalpha($Locale, $locales_test_number, $test == 0);
974 debug "# lesser = '$lesser'\n";
975 debug "# greater = '$greater'\n";
976 debug "# lesser cmp greater = ",
977 $lesser cmp $greater, "\n";
978 debug "# greater cmp lesser = ",
979 $greater cmp $lesser, "\n";
980 debug "# (greater) from = $from, to = $to\n";
982 debugf("# %-40s %-4s", $ti,
983 $test{$ti} ? 'FAIL' : 'ok');
984 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
985 debugf("(%s == %4d)", $1, eval $1);
996 if ($locales_test_number != $final_Neoalpha_test_number) {
997 die("The delta for \$final_Neoalpha needs to be updated from "
998 . ($final_Neoalpha_test_number - $first_Neoalpha_test_number)
1000 . ($locales_test_number - $first_Neoalpha_test_number)
1027 if (! $is_utf8_locale) {
1030 my ($x, $y) = (1.23, 1.23);
1033 printf ''; # printf used to reset locale to "C"
1038 my $z = sprintf ''; # sprintf used to reset locale to "C"
1045 local $SIG{__WARN__} =
1051 # The == (among other ops) used to warn for locales
1052 # that had something else than "." as the radix character.
1073 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1075 $ok14 = $ok15 = $ok16 = 1; # Skip for non-utf8 locales
1079 use locale ':not_characters';
1081 my ($x, $y) = (1.23, 1.23);
1083 printf ''; # printf used to reset locale to "C"
1088 my $z = sprintf ''; # sprintf used to reset locale to "C"
1094 local $SIG{__WARN__} =
1117 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1120 # Look for non-ASCII error messages, and verify that the first
1121 # such is in UTF-8 (the others almost certainly will be like the
1124 foreach my $err (keys %!) {
1126 $! = eval "&Errno::$err"; # Convert to strerror() output
1127 my $strerror = "$!";
1128 if ("$strerror" =~ /\P{ASCII}/) {
1129 my $utf8_strerror = $strerror;
1130 utf8::upgrade($utf8_strerror);
1132 # If $! was already in UTF-8, the upgrade was a no-op;
1133 # otherwise they will be different byte strings.
1135 $ok14 = $utf8_strerror eq $strerror;
1140 # Similarly, we verify that a non-ASCII radix is in UTF-8. This
1141 # also catches if there is a disparity between sprintf and
1144 my $string_g = "$g";
1146 my $utf8_string_g = "$g";
1147 utf8::upgrade($utf8_string_g);
1149 my $utf8_sprintf_g = sprintf("%g", $g);
1150 utf8::upgrade($utf8_sprintf_g);
1152 $ok15 = $utf8_string_g eq $string_g;
1153 $ok16 = $utf8_sprintf_g eq $string_g;
1157 tryneoalpha($Locale, ++$locales_test_number, $ok1);
1158 $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results';
1159 my $first_a_test = $locales_test_number;
1161 debug "# $first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n";
1163 tryneoalpha($Locale, ++$locales_test_number, $ok2);
1164 $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results';
1166 my $first_c_test = $locales_test_number;
1168 tryneoalpha($Locale, ++$locales_test_number, $ok3);
1169 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant';
1171 tryneoalpha($Locale, ++$locales_test_number, $ok4);
1172 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar';
1174 tryneoalpha($Locale, ++$locales_test_number, $ok5);
1175 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf';
1177 debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n";
1179 tryneoalpha($Locale, ++$locales_test_number, $ok6);
1180 $test_names{$locales_test_number} = 'Verify that can assign stringified under inner no-locale block';
1181 my $first_e_test = $locales_test_number;
1183 tryneoalpha($Locale, ++$locales_test_number, $ok7);
1184 $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale';
1186 tryneoalpha($Locale, ++$locales_test_number, $ok8);
1187 $test_names{$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale';
1189 debug "# $first_e_test..$locales_test_number: \$e = $e, no locale\n";
1191 tryneoalpha($Locale, ++$locales_test_number, $ok9);
1192 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant';
1193 my $first_f_test = $locales_test_number;
1195 tryneoalpha($Locale, ++$locales_test_number, $ok10);
1196 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar';
1198 tryneoalpha($Locale, ++$locales_test_number, $ok11);
1199 $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';
1201 tryneoalpha($Locale, ++$locales_test_number, $ok12);
1202 $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';
1204 tryneoalpha($Locale, ++$locales_test_number, $ok13);
1205 $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot';
1207 tryneoalpha($Locale, ++$locales_test_number, $ok14);
1208 $test_names{$locales_test_number} = 'Verify that non-ASCII UTF-8 error messages are in UTF-8';
1210 tryneoalpha($Locale, ++$locales_test_number, $ok15);
1211 $test_names{$locales_test_number} = 'Verify that a number with a UTF-8 radix has a UTF-8 stringification';
1213 tryneoalpha($Locale, ++$locales_test_number, $ok16);
1214 $test_names{$locales_test_number} = 'Verify that a sprintf of a number with a UTF-8 radix yields UTF-8';
1216 debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n";
1218 # Does taking lc separately differ from taking
1219 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
1220 # The bug was in the caching of the 'o'-magic.
1221 if (! $is_utf8_locale) {
1227 return $lc0 cmp $lc1;
1231 return lc($_[0]) cmp lc($_[1]);
1238 tryneoalpha($Locale, ++$locales_test_number,
1239 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
1240 lcA($x, $z) == 0 && lcB($x, $z) == 0);
1243 use locale ':not_characters';
1248 return $lc0 cmp $lc1;
1252 return lc($_[0]) cmp lc($_[1]);
1259 tryneoalpha($Locale, ++$locales_test_number,
1260 lcC($x, $y) == 1 && lcD($x, $y) == 1 ||
1261 lcC($x, $z) == 0 && lcD($x, $z) == 0);
1263 $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp';
1265 # Does lc of an UPPER (if different from the UPPER) match
1266 # case-insensitively the UPPER, and does the UPPER match
1267 # case-insensitively the lc of the UPPER. And vice versa.
1271 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
1274 ++$locales_test_number;
1275 $test_names{$locales_test_number} = 'Verify case insensitive matching works';
1276 foreach my $x (sort keys %UPPER) {
1277 if (! $is_utf8_locale) {
1279 next unless uc $y eq $x;
1280 print "# UPPER $x lc $y ",
1281 $x =~ /$y/i ? 1 : 0, " ",
1282 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1284 # If $x and $y contain regular expression characters
1285 # AND THEY lowercase (/i) to regular expression characters,
1286 # regcomp() will be mightily confused. No, the \Q doesn't
1287 # help here (maybe regex engine internal lowercasing
1288 # is done after the \Q?) An example of this happening is
1289 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
1290 # the chr(173) (the "[") is the lowercase of the chr(235).
1292 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
1293 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
1294 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
1295 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
1296 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
1297 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
1299 # Similar things can happen even under (bastardised)
1300 # non-EBCDIC locales: in many European countries before the
1301 # advent of ISO 8859-x nationally customised versions of
1302 # ISO 646 were devised, reusing certain punctuation
1303 # characters for modified characters needed by the
1304 # country/language. For example, the "|" might have
1305 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
1307 if ($x =~ $re || $y =~ $re) {
1308 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1311 # With utf8 both will fail since the locale concept
1312 # of upper/lower does not work well in Unicode.
1313 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
1315 # fc is not a locale concept, so Perl uses lc for it.
1316 push @f, $x unless lc $x eq fc $x;
1319 use locale ':not_characters';
1321 next unless uc $y eq $x;
1322 print "# UPPER $x lc $y ",
1323 $x =~ /$y/i ? 1 : 0, " ",
1324 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1326 # Here, we can fully test things, unlike plain 'use locale',
1327 # because this form does work well with Unicode
1328 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1330 # The places where Unicode's lc is different from fc are
1331 # skipped here by virtue of the 'next unless uc...' line above
1332 push @f, $x unless lc $x eq fc $x;
1336 foreach my $x (sort keys %lower) {
1337 if (! $is_utf8_locale) {
1339 next unless lc $y eq $x;
1340 print "# lower $x uc $y ",
1341 $x =~ /$y/i ? 1 : 0, " ",
1342 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1343 if ($x =~ $re || $y =~ $re) { # See above.
1344 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1347 # With utf8 both will fail since the locale concept
1348 # of upper/lower does not work well in Unicode.
1349 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
1351 push @f, $x unless lc $x eq fc $x;
1354 use locale ':not_characters';
1356 next unless lc $y eq $x;
1357 print "# lower $x uc $y ",
1358 $x =~ /$y/i ? 1 : 0, " ",
1359 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1360 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1362 push @f, $x unless lc $x eq fc $x;
1365 tryneoalpha($Locale, $locales_test_number, @f == 0);
1367 print "# failed $locales_test_number locale '$Locale' characters @f\n"
1374 ++$locales_test_number;
1375 $test_names{$locales_test_number} = 'Verify atof with locale radix and negative exponent';
1377 my $radix = POSIX::localeconv()->{decimal_point};
1379 "3.14e+9", "3${radix}14e+9", "3.14e-9", "3${radix}14e-9",
1380 "-3.14e+9", "-3${radix}14e+9", "-3.14e-9", "-3${radix}14e-9",
1383 if (! $is_utf8_locale) {
1385 for my $num (@nums) {
1387 unless sprintf("%g", $num) =~ /3.+14/;
1391 use locale ':not_characters';
1392 for my $num (@nums) {
1394 unless sprintf("%g", $num) =~ /3.+14/;
1398 tryneoalpha($Locale, $locales_test_number, @f == 0);
1400 print "# failed $locales_test_number locale '$Locale' numbers @f\n"
1405 my $final_locales_test_number = $locales_test_number;
1407 # Recount the errors.
1409 foreach ($first_locales_test_number..$final_locales_test_number) {
1410 if (%setlocale_failed) {
1413 elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
1414 if (defined $not_necessarily_a_problem_test_number
1415 && $_ == $not_necessarily_a_problem_test_number)
1417 print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n";
1418 print "# It usually indicates a problem in the environment,\n";
1419 print "# not in Perl itself.\n";
1421 if ($Okay{$_} && ($_ >= $first_casing_test_number
1422 && $_ <= $final_casing_test_number))
1424 my $percent_fail = int(.5 + (100 * scalar(keys $Problem{$_})
1425 / scalar(@{$Okay{$_}})));
1426 if ($percent_fail < $acceptable_fold_failure_percentage) {
1427 $test_names{$_} .= 'TODO';
1428 print "# ", 100 - $percent_fail, "% of locales pass the following test, so it is likely that the failures\n";
1429 print "# are errors in the locale definitions. The test is marked TODO, as the\n";
1430 print "# problem is not likely to be Perl's\n";
1436 if (defined $test_names{$_}) {
1437 # If TODO is in the test name, make it thus
1438 my $todo = $test_names{$_} =~ s/TODO\s*//;
1439 print " $test_names{$_}";
1440 print " # TODO" if $todo;
1445 # Give final advice.
1449 foreach ($first_locales_test_number..$final_locales_test_number) {
1451 my @f = sort keys %{ $Problem{$_} };
1452 my $f = join(" ", @f);
1453 $f =~ s/(.{50,60}) /$1\n#\t/g;
1456 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
1458 "# on your system may have errors because the locale test $_\n",
1459 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
1463 # If your users are not using these locales you are safe for the moment,
1464 # but please report this failure first to perlbug\@perl.com using the
1465 # perlbug script (as described in the INSTALL file) so that the exact
1466 # details of the failures can be sorted out first and then your operating
1467 # system supplier can be alerted about these anomalies.
1474 # Tell which locales were okay and which were not.
1479 foreach my $l (@Locale) {
1481 if ($setlocale_failed{$l}) {
1486 ($first_locales_test_number..$final_locales_test_number)
1488 $p++ if $Problem{$t}{$l};
1491 push @s, $l if $p == 0;
1492 push @F, $l unless $p == 0;
1496 my $s = join(" ", @s);
1497 $s =~ s/(.{50,60}) /$1\n#\t/g;
1500 "# The following locales\n#\n",
1502 "# tested okay.\n#\n",
1504 warn "# None of your locales were fully okay.\n";
1508 my $F = join(" ", @F);
1509 $F =~ s/(.{50,60}) /$1\n#\t/g;
1512 "# The following locales\n#\n",
1514 "# had problems.\n#\n",
1516 warn "# None of your locales were broken.\n";
1520 $test_num = $final_locales_test_number;
1525 local $SIG{__WARN__} = sub {
1526 $warned = $_[0] =~ /uninitialized/;
1528 my $z = "y" . setlocale(&POSIX::LC_ALL, "xyzzy");
1529 ok($warned, "variable set to setlocale(BAD LOCALE) is considered uninitialized");
1532 # Test that tainting and case changing works on utf8 strings. These tests are
1533 # placed last to avoid disturbing the hard-coded test numbers that existed at
1534 # the time these were added above this in this file.
1535 # This also tests that locale overrides unicode_strings in the same scope for
1537 setlocale(LC_ALL, "C");
1540 use feature 'unicode_strings';
1542 foreach my $function ("uc", "ucfirst", "lc", "lcfirst", "fc") {
1543 my @list; # List of code points to test for $function
1545 # Used to calculate the changed case for ASCII characters by using the
1546 # ord, instead of using one of the functions under test.
1547 my $ascii_case_change_delta;
1548 my $above_latin1_case_change_delta; # Same for the specific ords > 255
1551 # We test an ASCII character, which should change case and be tainted;
1552 # a Latin1 character, which shouldn't change case under this C locale,
1554 # an above-Latin1 character that when the case is changed would cross
1555 # the 255/256 boundary, so doesn't change case and isn't tainted
1556 # (the \x{149} is one of these, but changes into 2 characters, the
1557 # first one of which doesn't cross the boundary.
1558 # the final one in each list is an above-Latin1 character whose case
1559 # does change, and shouldn't be tainted. The code below uses its
1560 # position in its list as a marker to indicate that it, unlike the
1561 # other code points above ASCII, has a successful case change
1562 if ($function =~ /^u/) {
1563 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
1564 $ascii_case_change_delta = -32;
1565 $above_latin1_case_change_delta = -1;
1568 @list = ("", "A", "\xC0", "\x{17F}", "\x{100}");
1569 $ascii_case_change_delta = +32;
1570 $above_latin1_case_change_delta = +1;
1572 foreach my $is_utf8_locale (0 .. 1) {
1573 foreach my $j (0 .. $#list) {
1574 my $char = $list[$j];
1576 for my $encoded_in_utf8 (0 .. 1) {
1579 if (! $is_utf8_locale) {
1580 $should_be = ($j == $#list)
1581 ? chr(ord($char) + $above_latin1_case_change_delta)
1582 : (length $char == 0 || ord($char) > 127)
1584 : chr(ord($char) + $ascii_case_change_delta);
1586 # This monstrosity is in order to avoid using an eval,
1587 # which might perturb the results
1588 $changed = ($function eq "uc")
1590 : ($function eq "ucfirst")
1592 : ($function eq "lc")
1594 : ($function eq "lcfirst")
1596 : ($function eq "fc")
1598 : die("Unexpected function \"$function\"");
1604 # For utf8-locales the case changing functions
1605 # should work just like they do outside of locale.
1606 # Can use eval here because not testing it when
1608 $should_be = eval "$function('$char')";
1609 die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if $@;
1612 use locale ':not_characters';
1613 $changed = ($function eq "uc")
1615 : ($function eq "ucfirst")
1617 : ($function eq "lc")
1619 : ($function eq "lcfirst")
1621 : ($function eq "fc")
1623 : die("Unexpected function \"$function\"");
1625 ok($changed eq $should_be,
1626 "$function(\"$char\") in C locale "
1627 . (($is_utf8_locale)
1628 ? "(use locale ':not_characters'"
1630 . (($encoded_in_utf8)
1631 ? "; encoded in utf8)"
1632 : "; not encoded in utf8)")
1633 . " should be \"$should_be\", got \"$changed\"");
1635 # Tainting shouldn't happen for utf8 locales, empty
1636 # strings, or those characters above 255.
1637 (! $is_utf8_locale && length($char) > 0 && ord($char) < 256)
1638 ? check_taint($changed)
1639 : check_taint_not($changed);
1641 # Use UTF-8 next time through the loop
1642 utf8::upgrade($char);
1649 print "1..$test_num\n";