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 binmode STDOUT, ':utf8';
9 binmode STDERR, ':utf8';
15 require Config; import Config;
16 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
30 my $dumper = Dumpvalue->new(
37 my($mess) = join "", @_;
39 print $dumper->stringify($mess,1), "\n";
46 my $have_setlocale = 0;
49 import POSIX ':locale_h';
53 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
54 # and mingw32 uses said silly CRT
55 # This doesn't seem to be an issue any more, at least on Windows XP,
56 # so re-enable the tests for Windows XP onwards.
57 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
58 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
59 $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
60 $Config{cc} =~ /^(cl|gcc)/i);
62 # UWIN seems to loop after taint tests, just skip for now
63 $have_setlocale = 0 if ($^O =~ /^uwin/);
72 my ($result, $message) = @_;
73 $message = "" unless defined $message;
75 print 'not ' unless ($result);
76 print "ok " . ++$test_num;
81 # First we'll do a lot of taint checking for locales.
82 # This is the easiest to test, actually, as any locale,
83 # even the default locale will taint under 'use locale'.
85 sub is_tainted { # hello, camel two.
86 no warnings 'uninitialized' ;
89 not eval { $dummy = join("", @_), kill 0; 1 }
93 ok is_tainted($_[0]), "verify that is tainted";
96 sub check_taint_not ($) {
97 ok((not is_tainted($_[0])), "verify that isn't tainted");
100 use locale; # engage locale and therefore locale taint.
106 check_taint ucfirst($a);
112 check_taint lcfirst($a);
115 check_taint_not sprintf('%e', 123.456);
116 check_taint_not sprintf('%f', 123.456);
117 check_taint_not sprintf('%g', 123.456);
118 check_taint_not sprintf('%d', 123.456);
119 check_taint_not sprintf('%x', 123.456);
121 $_ = $a; # untaint $_
123 $_ = uc($a); # taint $_
127 /(\w)/; # taint $&, $`, $', $+, $1.
135 /(.)/; # untaint $&, $`, $', $+, $1.
143 /(\W)/; # taint $&, $`, $', $+, $1.
151 /(\s)/; # taint $&, $`, $', $+, $1.
159 /(\S)/; # taint $&, $`, $', $+, $1.
167 $_ = $a; # untaint $_
171 /(b)/; # this must not taint
179 $_ = $a; # untaint $_
183 $b = uc($a); # taint $b
184 s/(.+)/$b/; # this must taint only the $_
194 $_ = $a; # untaint $_
196 s/(.+)/b/; # this must not taint
205 $b = $a; # untaint $b
207 ($b = $a) =~ s/\w/$&/;
208 check_taint $b; # $b should be tainted.
209 check_taint_not $a; # $a should be not.
211 $_ = $a; # untaint $_
213 s/(\w)/\l$1/; # this must taint
222 $_ = $a; # untaint $_
224 s/(\w)/\L$1/; # this must taint
233 $_ = $a; # untaint $_
235 s/(\w)/\u$1/; # this must taint
244 $_ = $a; # untaint $_
246 s/(\w)/\U$1/; # this must taint
255 # After all this tainting $a should be cool.
259 { # This is just the previous tests copied here with a different
260 # compile-time pragma.
262 use locale ':not_characters'; # engage restricted locale with different
267 check_taint_not uc($a);
268 check_taint_not "\U$a";
269 check_taint_not ucfirst($a);
270 check_taint_not "\u$a";
271 check_taint_not lc($a);
272 check_taint_not fc($a);
273 check_taint_not "\L$a";
274 check_taint_not "\F$a";
275 check_taint_not lcfirst($a);
276 check_taint_not "\l$a";
278 check_taint_not sprintf('%e', 123.456);
279 check_taint_not sprintf('%f', 123.456);
280 check_taint_not sprintf('%g', 123.456);
281 check_taint_not sprintf('%d', 123.456);
282 check_taint_not sprintf('%x', 123.456);
284 $_ = $a; # untaint $_
286 $_ = uc($a); # taint $_
290 /(\w)/; # taint $&, $`, $', $+, $1.
298 /(.)/; # untaint $&, $`, $', $+, $1.
306 /(\W)/; # taint $&, $`, $', $+, $1.
314 /(\s)/; # taint $&, $`, $', $+, $1.
322 /(\S)/; # taint $&, $`, $', $+, $1.
330 $_ = $a; # untaint $_
334 /(b)/; # this must not taint
342 $_ = $a; # untaint $_
346 $b = uc($a); # taint $b
347 s/(.+)/$b/; # this must taint only the $_
357 $_ = $a; # untaint $_
359 s/(.+)/b/; # this must not taint
368 $b = $a; # untaint $b
370 ($b = $a) =~ s/\w/$&/;
371 check_taint_not $b; # $b should be tainted.
372 check_taint_not $a; # $a should be not.
374 $_ = $a; # untaint $_
376 s/(\w)/\l$1/; # this must taint
385 $_ = $a; # untaint $_
387 s/(\w)/\L$1/; # this must taint
396 $_ = $a; # untaint $_
398 s/(\w)/\u$1/; # this must taint
407 $_ = $a; # untaint $_
409 s/(\w)/\U$1/; # this must taint
418 # After all this tainting $a should be cool.
423 # Here are in scope of 'use locale'
425 # I think we've seen quite enough of taint.
426 # Let us do some *real* locale work now,
427 # unless setlocale() is missing (i.e. minitest).
429 unless ($have_setlocale) {
430 print "1..$test_num\n";
434 # The test number before our first setlocale()
435 my $final_without_setlocale = $test_num;
439 debug "# Scanning for locales...\n";
441 # Note that it's okay that some languages have their native names
442 # capitalized here even though that's not "right". They are lowercased
443 # anyway later during the scanning process (and besides, some clueless
444 # vendor might have them capitalized erroneously anyway).
448 Arabic:ar:dz eg sa:6 arabic8
449 Brezhoneg Breton:br:fr:1 15
450 Bulgarski Bulgarian:bg:bg:5
451 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
452 Hrvatski Croatian:hr:hr:2
453 Cymraeg Welsh:cy:cy:1 14 15
455 Dansk Danish:da:dk:1 15
456 Nederlands Dutch:nl:be nl:1 15
457 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
459 Eesti Estonian:et:ee:4 6 13
460 Suomi Finnish:fi:fi:1 15
462 Deutsch German:de:at be ch de lu:1 15
463 Euskaraz Basque:eu:es fr:1 15
464 Galego Galician:gl:es:1 15
465 Ellada Greek:el:gr:7 g8
467 Greenlandic:kl:gl:4 6
468 Hebrew:iw:il:8 hebrew8
470 Indonesian:id:id:1 15
471 Gaeilge Irish:ga:IE:1 14 15
472 Italiano Italian:it:ch it:1 15
473 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
475 Latine Latin:la:va:1 15
477 Lithuanian:lt:lt:4 6 13
478 Macedonian:mk:mk:1 15
481 Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
483 Polski Polish:pl:pl:2
485 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
486 Serbski Serbian:sr:yu:5
488 Slovene Slovenian:sl:si:2
489 Sqhip Albanian:sq:sq:1 15
490 Svenska Swedish:sv:fi se:1 15
492 Turkish:tr:tr:9 turkish8
496 if ($^O eq 'os390') {
497 # These cause heartburn. Broken locales?
498 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
499 $locales =~ s/Thai:th:th:11 tis620\n//;
502 sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
505 require "lib/locale/utf8";
507 require "lib/locale/latin1";
516 return if grep { $locale eq $_ } @Locale;
517 return unless setlocale(LC_ALL, $locale);
520 local $SIG{__WARN__} = sub {
521 $badutf8 = $_[0] =~ /Malformed UTF-8/;
523 $Locale =~ /UTF-?8/i;
527 ok(0, "Locale name contains malformed utf8");
530 push @Locale, $locale;
533 sub decode_encodings {
536 foreach (split(/ /, shift)) {
538 push @enc, "ISO8859-$1";
539 push @enc, "iso8859$1"; # HP
541 push @enc, "roman8"; # HP
545 push @enc, "$_.UTF-8";
548 if ($^O eq 'os390') {
549 push @enc, qw(IBM-037 IBM-819 IBM-1047);
558 trylocale("ISO8859-$_");
559 trylocale("iso8859$_");
560 trylocale("iso8859-$_");
561 trylocale("iso_8859_$_");
562 trylocale("isolatin$_");
563 trylocale("isolatin-$_");
564 trylocale("iso_latin_$_");
567 # Sanitize the environment so that we can run the external 'locale'
568 # program without the taint mode getting grumpy.
570 # $ENV{PATH} is special in VMS.
571 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
573 # Other subversive stuff.
574 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
576 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
578 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
579 # ain't great when we're running this testPERL_UNICODE= so that utf8
580 # locales will cause all IO hadles to default to (assume) utf8
581 next unless utf8::valid($_);
586 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
587 # The SYS$I18N_LOCALE logical name search list was not present on
588 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
589 opendir(LOCALES, "SYS\$I18N_LOCALE:");
590 while ($_ = readdir(LOCALES)) {
595 } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
597 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
598 # is much easier and faster than the last resort method.
600 opendir(LOCALES, '/usr/share/locale');
601 while ($_ = readdir(LOCALES)) {
608 # This is going to be slow.
610 foreach my $locale (split(/\n/, $locales)) {
611 my ($locale_name, $language_codes, $country_codes, $encodings) =
613 my @enc = decode_encodings($encodings);
614 foreach my $loc (split(/ /, $locale_name)) {
616 foreach my $enc (@enc) {
617 trylocale("$loc.$enc");
620 foreach my $enc (@enc) {
621 trylocale("$loc.$enc");
624 foreach my $lang (split(/ /, $language_codes)) {
626 foreach my $country (split(/ /, $country_codes)) {
627 my $lc = "${lang}_${country}";
629 foreach my $enc (@enc) {
630 trylocale("$lc.$enc");
632 my $lC = "${lang}_\U${country}";
634 foreach my $enc (@enc) {
635 trylocale("$lC.$enc");
642 setlocale(LC_ALL, "C");
644 if ($^O eq 'darwin') {
645 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
646 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
647 (my $v) = $Config{osvers} =~ /^(\d+)/;
648 if ($v >= 8 and $v < 10) {
649 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
650 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
652 debug "# Skipping be_BY locales -- buggy in Darwin\n";
653 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
657 @Locale = sort @Locale;
659 debug "# Locales =\n";
667 my @Neoalpha; # Alnums that aren't in the C locale.
671 my ($Locale, $i, $test) = @_;
673 $Problem{$i}{$Locale} = 1;
674 debug "# failed $i with locale '$Locale'\n";
676 push @{$Okay{$i}}, $Locale;
680 my $first_locales_test_number = $final_without_setlocale + 1;
681 my $locales_test_number;
682 my $not_necessarily_a_problem_test_number;
683 my %setlocale_failed; # List of locales that setlocale() didn't work on
685 foreach $Locale (@Locale) {
686 $locales_test_number = $first_locales_test_number - 1;
687 debug "# Locale = $Locale\n";
689 unless (setlocale(LC_ALL, $Locale)) {
690 $setlocale_failed{$Locale} = $Locale;
694 # We test UTF-8 locales only under ':not_characters'; otherwise they have
695 # documented deficiencies. Non- UTF-8 locales are tested only under plain
696 # 'use locale', as otherwise we would have to convert everything in them
698 my $is_utf8_locale = $Locale =~ /UTF-?8/i;
704 if (! $is_utf8_locale) {
706 @Alnum_ = sort grep /\w/, map { chr } 0..255;
708 debug "# w = ", join("",@Alnum_), "\n";
710 # Sieve the uppercase and the lowercase.
713 if (/[^\d_]/) { # skip digits and the _
724 use locale ':not_characters';
725 @Alnum_ = sort grep /\w/, map { chr } 0..255;
726 debug "# w = ", join("",@Alnum_), "\n";
728 if (/[^\d_]/) { # skip digits and the _
738 foreach (keys %UPPER) {
739 $BoThCaSe{$_}++ if exists $lower{$_};
741 foreach (keys %lower) {
742 $BoThCaSe{$_}++ if exists $UPPER{$_};
744 foreach (keys %BoThCaSe) {
749 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
750 debug "# lower = ", join("", sort keys %lower ), "\n";
751 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
753 { # Find the alphabetic characters that are not considered alphabetics
754 # in the default (C) locale.
759 for (keys %UPPER, keys %lower) {
760 push(@Neoalpha, $_) if (/\W/);
764 @Neoalpha = sort @Neoalpha;
766 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
768 my $first_Neoalpha_test_number = $locales_test_number;
769 my $final_Neoalpha_test_number = $first_Neoalpha_test_number + 4;
770 if (@Neoalpha == 0) {
771 # If we have no Neoalphas the remaining tests are no-ops.
772 debug "# no Neoalpha, skipping tests $locales_test_number..$final_Neoalpha_test_number for locale '$Locale'\n";
773 foreach ($locales_test_number+1..$final_Neoalpha_test_number) {
774 push @{$Okay{$_}}, $Locale;
775 $locales_test_number++;
781 my $word = join('', @Neoalpha);
783 ++$locales_test_number;
784 $test_names{$locales_test_number} = 'Verify that alnums outside the C locale match \w';
786 if ($is_utf8_locale) {
787 use locale ':not_characters';
788 $ok = $word =~ /^(\w+)$/;
791 # Already in 'use locale'; this tests that exiting scopes works
792 $ok = $word =~ /^(\w+)$/;
794 tryneoalpha($Locale, $locales_test_number, $ok);
796 # Cross-check the whole 8-bit character set.
798 ++$locales_test_number;
799 $test_names{$locales_test_number} = 'Verify that \w and \W are mutually exclusive, as are \d, \D; \s, \S';
800 for (map { chr } 0..255) {
801 if ($is_utf8_locale) {
802 use locale ':not_characters';
803 $ok = (/\w/ xor /\W/) ||
808 $ok = (/\w/ xor /\W/) ||
812 tryneoalpha($Locale, $locales_test_number, $ok);
815 # Test for read-only scalars' locale vs non-locale comparisons.
820 if ($is_utf8_locale) {
821 use locale ':not_characters';
822 $ok = ($a cmp "qwerty") == 0;
826 $ok = ($a cmp "qwerty") == 0;
828 tryneoalpha($Locale, ++$locales_test_number, $ok);
829 $test_names{$locales_test_number} = 'Verify that cmp works with a read-only scalar; no- vs locale';
833 my ($from, $to, $lesser, $greater,
834 @test, %test, $test, $yes, $no, $sign);
836 ++$locales_test_number;
837 $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work';
838 $not_necessarily_a_problem_test_number = $locales_test_number;
841 $from = int(($_*@Alnum_)/10);
842 $to = $from + int(@Alnum_/10);
843 $to = $#Alnum_ if ($to > $#Alnum_);
844 $lesser = join('', @Alnum_[$from..$to]);
845 # Select a slice one character on.
847 $to = $#Alnum_ if ($to > $#Alnum_);
848 $greater = join('', @Alnum_[$from..$to]);
849 if ($is_utf8_locale) {
850 use locale ':not_characters';
851 ($yes, $no, $sign) = ($lesser lt $greater
853 : ("not ", " ", -1));
857 ($yes, $no, $sign) = ($lesser lt $greater
859 : ("not ", " ", -1));
861 # all these tests should FAIL (return 0).
862 # Exact lt or gt cannot be tested because
863 # in some locales, say, eacute and E may test equal.
866 $no.' ($lesser le $greater)', # 1
867 'not ($lesser ne $greater)', # 2
868 ' ($lesser eq $greater)', # 3
869 $yes.' ($lesser ge $greater)', # 4
870 $yes.' ($lesser ge $greater)', # 5
871 $yes.' ($greater le $lesser )', # 7
872 'not ($greater ne $lesser )', # 8
873 ' ($greater eq $lesser )', # 9
874 $no.' ($greater ge $lesser )', # 10
875 'not (($lesser cmp $greater) == -($sign))' # 11
877 @test{@test} = 0 x @test;
880 if ($is_utf8_locale) {
881 use locale ':not_characters';
882 $test{$ti} = eval $ti;
885 # Already in 'use locale';
886 $test{$ti} = eval $ti;
890 tryneoalpha($Locale, $locales_test_number, $test == 0);
892 debug "# lesser = '$lesser'\n";
893 debug "# greater = '$greater'\n";
894 debug "# lesser cmp greater = ",
895 $lesser cmp $greater, "\n";
896 debug "# greater cmp lesser = ",
897 $greater cmp $lesser, "\n";
898 debug "# (greater) from = $from, to = $to\n";
900 debugf("# %-40s %-4s", $ti,
901 $test{$ti} ? 'FAIL' : 'ok');
902 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
903 debugf("(%s == %4d)", $1, eval $1);
914 if ($locales_test_number != $final_Neoalpha_test_number) {
915 die("The delta for \$final_Neoalpha needs to be updated from "
916 . ($final_Neoalpha_test_number - $first_Neoalpha_test_number)
918 . ($locales_test_number - $first_Neoalpha_test_number)
942 if (! $is_utf8_locale) {
945 my ($x, $y) = (1.23, 1.23);
948 printf ''; # printf used to reset locale to "C"
953 my $z = sprintf ''; # sprintf used to reset locale to "C"
960 local $SIG{__WARN__} =
966 # The == (among other ops) used to warn for locales
967 # that had something else than "." as the radix character.
975 # The earlier test was $e = "$x". But this fails [perl
976 # #108378], and the "no locale" was commented out. But doing
977 # that made all the tests in the block after this one
978 # meaningless, as originally it was testing the nesting of a
979 # "no locale" scope, and how it recovers after that scope is
980 # done. So I (khw) filed a bug report and changed this so it
981 # wouldn't fail. It seemed too much work to add TODOs
982 # instead. Should this be fixed, the following test names
983 # would need to be revised; they mostly don't really test
984 # anything currently.
998 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1003 use locale ':not_characters';
1005 my ($x, $y) = (1.23, 1.23);
1007 printf ''; # printf used to reset locale to "C"
1012 my $z = sprintf ''; # sprintf used to reset locale to "C"
1018 local $SIG{__WARN__} =
1041 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1046 tryneoalpha($Locale, ++$locales_test_number, $ok1);
1047 $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results';
1048 my $first_a_test = $locales_test_number;
1050 debug "# $first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n";
1052 tryneoalpha($Locale, ++$locales_test_number, $ok2);
1053 $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results';
1055 my $first_c_test = $locales_test_number;
1057 tryneoalpha($Locale, ++$locales_test_number, $ok3);
1058 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant';
1060 tryneoalpha($Locale, ++$locales_test_number, $ok4);
1061 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar';
1063 tryneoalpha($Locale, ++$locales_test_number, $ok5);
1064 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf';
1066 debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n";
1068 tryneoalpha($Locale, ++$locales_test_number, $ok6);
1069 $test_names{$locales_test_number} = 'Verify that can assign numerically under inner no-locale block';
1070 my $first_e_test = $locales_test_number;
1072 tryneoalpha($Locale, ++$locales_test_number, $ok7);
1073 $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale';
1075 tryneoalpha($Locale, ++$locales_test_number, $ok8);
1076 $test_names{$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale';
1078 debug "# $first_e_test..$locales_test_number: \$e = $e, no locale\n";
1080 tryneoalpha($Locale, ++$locales_test_number, $ok9);
1081 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant';
1082 my $first_f_test = $locales_test_number;
1084 tryneoalpha($Locale, ++$locales_test_number, $ok10);
1085 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar';
1087 tryneoalpha($Locale, ++$locales_test_number, $ok11);
1088 $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';
1090 tryneoalpha($Locale, ++$locales_test_number, $ok12);
1091 $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';
1093 tryneoalpha($Locale, ++$locales_test_number, $ok13);
1094 $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot';
1096 debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n";
1098 # Does taking lc separately differ from taking
1099 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
1100 # The bug was in the caching of the 'o'-magic.
1101 if (! $is_utf8_locale) {
1107 return $lc0 cmp $lc1;
1111 return lc($_[0]) cmp lc($_[1]);
1118 tryneoalpha($Locale, ++$locales_test_number,
1119 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
1120 lcA($x, $z) == 0 && lcB($x, $z) == 0);
1123 use locale ':not_characters';
1128 return $lc0 cmp $lc1;
1132 return lc($_[0]) cmp lc($_[1]);
1139 tryneoalpha($Locale, ++$locales_test_number,
1140 lcC($x, $y) == 1 && lcD($x, $y) == 1 ||
1141 lcC($x, $z) == 0 && lcD($x, $z) == 0);
1143 $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp';
1145 # Does lc of an UPPER (if different from the UPPER) match
1146 # case-insensitively the UPPER, and does the UPPER match
1147 # case-insensitively the lc of the UPPER. And vice versa.
1151 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
1154 ++$locales_test_number;
1155 $test_names{$locales_test_number} = 'Verify case insensitive matching works';
1156 foreach my $x (sort keys %UPPER) {
1157 if (! $is_utf8_locale) {
1159 next unless uc $y eq $x;
1160 print "# UPPER $x lc $y ",
1161 $x =~ /$y/i ? 1 : 0, " ",
1162 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1164 # If $x and $y contain regular expression characters
1165 # AND THEY lowercase (/i) to regular expression characters,
1166 # regcomp() will be mightily confused. No, the \Q doesn't
1167 # help here (maybe regex engine internal lowercasing
1168 # is done after the \Q?) An example of this happening is
1169 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
1170 # the chr(173) (the "[") is the lowercase of the chr(235).
1172 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
1173 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
1174 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
1175 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
1176 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
1177 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
1179 # Similar things can happen even under (bastardised)
1180 # non-EBCDIC locales: in many European countries before the
1181 # advent of ISO 8859-x nationally customised versions of
1182 # ISO 646 were devised, reusing certain punctuation
1183 # characters for modified characters needed by the
1184 # country/language. For example, the "|" might have
1185 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
1187 if ($x =~ $re || $y =~ $re) {
1188 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1191 # With utf8 both will fail since the locale concept
1192 # of upper/lower does not work well in Unicode.
1193 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
1195 # fc is not a locale concept, so Perl uses lc for it.
1196 push @f, $x unless lc $x eq fc $x;
1199 use locale ':not_characters';
1201 next unless uc $y eq $x;
1202 print "# UPPER $x lc $y ",
1203 $x =~ /$y/i ? 1 : 0, " ",
1204 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1206 # Here, we can fully test things, unlike plain 'use locale',
1207 # because this form does work well with Unicode
1208 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1210 # The places where Unicode's lc is different from fc are
1211 # skipped here by virtue of the 'next unless uc...' line above
1212 push @f, $x unless lc $x eq fc $x;
1216 foreach my $x (sort keys %lower) {
1217 if (! $is_utf8_locale) {
1219 next unless lc $y eq $x;
1220 print "# lower $x uc $y ",
1221 $x =~ /$y/i ? 1 : 0, " ",
1222 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1223 if ($x =~ $re || $y =~ $re) { # See above.
1224 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1227 # With utf8 both will fail since the locale concept
1228 # of upper/lower does not work well in Unicode.
1229 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
1231 push @f, $x unless lc $x eq fc $x;
1234 use locale ':not_characters';
1236 next unless lc $y eq $x;
1237 print "# lower $x uc $y ",
1238 $x =~ /$y/i ? 1 : 0, " ",
1239 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1240 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1242 push @f, $x unless lc $x eq fc $x;
1245 tryneoalpha($Locale, $locales_test_number, @f == 0);
1247 print "# failed $locales_test_number locale '$Locale' characters @f\n"
1254 ++$locales_test_number;
1255 $test_names{$locales_test_number} = 'Verify atof with locale radix and negative exponent';
1257 my $radix = POSIX::localeconv()->{decimal_point};
1259 "3.14e+9", "3${radix}14e+9", "3.14e-9", "3${radix}14e-9",
1260 "-3.14e+9", "-3${radix}14e+9", "-3.14e-9", "-3${radix}14e-9",
1263 if (! $is_utf8_locale) {
1265 for my $num (@nums) {
1267 unless sprintf("%g", $num) =~ /3.+14/;
1271 use locale ':not_characters';
1272 for my $num (@nums) {
1274 unless sprintf("%g", $num) =~ /3.+14/;
1278 tryneoalpha($Locale, $locales_test_number, @f == 0);
1280 print "# failed $locales_test_number locale '$Locale' numbers @f\n"
1285 my $final_locales_test_number = $locales_test_number;
1287 # Recount the errors.
1289 foreach ($first_locales_test_number..$final_locales_test_number) {
1290 if (%setlocale_failed) {
1293 elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
1294 if (defined $not_necessarily_a_problem_test_number
1295 && $_ == $not_necessarily_a_problem_test_number)
1297 print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n";
1298 print "# It usually indicates a problem in the environment,\n";
1299 print "# not in Perl itself.\n";
1304 print " $test_names{$_}" if defined $test_names{$_};
1308 # Give final advice.
1312 foreach ($first_locales_test_number..$final_locales_test_number) {
1314 my @f = sort keys %{ $Problem{$_} };
1315 my $f = join(" ", @f);
1316 $f =~ s/(.{50,60}) /$1\n#\t/g;
1319 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
1321 "# on your system may have errors because the locale test $_\n",
1322 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
1326 # If your users are not using these locales you are safe for the moment,
1327 # but please report this failure first to perlbug\@perl.com using the
1328 # perlbug script (as described in the INSTALL file) so that the exact
1329 # details of the failures can be sorted out first and then your operating
1330 # system supplier can be alerted about these anomalies.
1337 # Tell which locales were okay and which were not.
1342 foreach my $l (@Locale) {
1344 if ($setlocale_failed{$l}) {
1349 ($first_locales_test_number..$final_locales_test_number)
1351 $p++ if $Problem{$t}{$l};
1354 push @s, $l if $p == 0;
1355 push @F, $l unless $p == 0;
1359 my $s = join(" ", @s);
1360 $s =~ s/(.{50,60}) /$1\n#\t/g;
1363 "# The following locales\n#\n",
1365 "# tested okay.\n#\n",
1367 warn "# None of your locales were fully okay.\n";
1371 my $F = join(" ", @F);
1372 $F =~ s/(.{50,60}) /$1\n#\t/g;
1375 "# The following locales\n#\n",
1377 "# had problems.\n#\n",
1379 warn "# None of your locales were broken.\n";
1383 $test_num = $final_locales_test_number;
1385 # Test that tainting and case changing works on utf8 strings. These tests are
1386 # placed last to avoid disturbing the hard-coded test numbers that existed at
1387 # the time these were added above this in this file.
1388 # This also tests that locale overrides unicode_strings in the same scope for
1390 setlocale(LC_ALL, "C");
1393 use feature 'unicode_strings';
1395 foreach my $function ("uc", "ucfirst", "lc", "lcfirst", "fc") {
1396 my @list; # List of code points to test for $function
1398 # Used to calculate the changed case for ASCII characters by using the
1399 # ord, instead of using one of the functions under test.
1400 my $ascii_case_change_delta;
1401 my $above_latin1_case_change_delta; # Same for the specific ords > 255
1404 # We test an ASCII character, which should change case and be tainted;
1405 # a Latin1 character, which shouldn't change case under this C locale,
1407 # an above-Latin1 character that when the case is changed would cross
1408 # the 255/256 boundary, so doesn't change case and isn't tainted
1409 # (the \x{149} is one of these, but changes into 2 characters, the
1410 # first one of which doesn't cross the boundary.
1411 # the final one in each list is an above-Latin1 character whose case
1412 # does change, and shouldn't be tainted. The code below uses its
1413 # position in its list as a marker to indicate that it, unlike the
1414 # other code points above ASCII, has a successful case change
1415 if ($function =~ /^u/) {
1416 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
1417 $ascii_case_change_delta = -32;
1418 $above_latin1_case_change_delta = -1;
1421 @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}");
1422 $ascii_case_change_delta = +32;
1423 $above_latin1_case_change_delta = +1;
1425 foreach my $is_utf8_locale (0 .. 1) {
1426 foreach my $j (0 .. $#list) {
1427 my $char = $list[$j];
1429 for my $encoded_in_utf8 (0 .. 1) {
1432 if (! $is_utf8_locale) {
1433 $should_be = ($j == $#list)
1434 ? chr(ord($char) + $above_latin1_case_change_delta)
1435 : (length $char == 0 || ord($char) > 127)
1437 : chr(ord($char) + $ascii_case_change_delta);
1439 # This monstrosity is in order to avoid using an eval,
1440 # which might perturb the results
1441 $changed = ($function eq "uc")
1443 : ($function eq "ucfirst")
1445 : ($function eq "lc")
1447 : ($function eq "lcfirst")
1449 : ($function eq "fc")
1451 : die("Unexpected function \"$function\"");
1457 # For utf8-locales the case changing functions
1458 # should work just like they do outside of locale.
1459 # Can use eval here because not testing it when
1461 $should_be = eval "$function('$char')";
1462 die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if $@;
1465 use locale ':not_characters';
1466 $changed = ($function eq "uc")
1468 : ($function eq "ucfirst")
1470 : ($function eq "lc")
1472 : ($function eq "lcfirst")
1474 : ($function eq "fc")
1476 : die("Unexpected function \"$function\"");
1478 ok($changed eq $should_be,
1479 "$function(\"$char\") in C locale "
1480 . (($is_utf8_locale)
1481 ? "(use locale ':not_characters'"
1483 . (($encoded_in_utf8)
1484 ? "; encoded in utf8)"
1485 : "; not encoded in utf8)")
1486 . " should be \"$should_be\", got \"$changed\"");
1488 # Tainting shouldn't happen for utf8 locales, empty
1489 # strings, or those characters above 255.
1490 (! $is_utf8_locale && length($char) > 0 && ord($char) < 256)
1491 ? check_taint($changed)
1492 : check_taint_not($changed);
1494 # Use UTF-8 next time through the loop
1495 utf8::upgrade($char);
1502 print "1..$test_num\n";