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/) {
29 my $dumper = Dumpvalue->new(
36 my($mess) = join "", @_;
38 print $dumper->stringify($mess,1), "\n";
45 my $have_setlocale = 0;
48 import POSIX ':locale_h';
52 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
53 # and mingw32 uses said silly CRT
54 # This doesn't seem to be an issue any more, at least on Windows XP,
55 # so re-enable the tests for Windows XP onwards.
56 my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
57 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
58 $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
59 $Config{cc} =~ /^(cl|gcc)/i);
61 # UWIN seems to loop after taint tests, just skip for now
62 $have_setlocale = 0 if ($^O =~ /^uwin/);
71 my ($result, $message) = @_;
72 $message = "" unless defined $message;
74 print 'not ' unless ($result);
75 print "ok " . ++$test_num;
80 # First we'll do a lot of taint checking for locales.
81 # This is the easiest to test, actually, as any locale,
82 # even the default locale will taint under 'use locale'.
84 sub is_tainted { # hello, camel two.
85 no warnings 'uninitialized' ;
88 not eval { $dummy = join("", @_), kill 0; 1 }
92 ok is_tainted($_[0]), "verify that is tainted";
95 sub check_taint_not ($) {
96 ok((not is_tainted($_[0])), "verify that isn't tainted");
99 use locale; # engage locale and therefore locale taint.
105 check_taint ucfirst($a);
109 check_taint lcfirst($a);
112 check_taint_not sprintf('%e', 123.456);
113 check_taint_not sprintf('%f', 123.456);
114 check_taint_not sprintf('%g', 123.456);
115 check_taint_not sprintf('%d', 123.456);
116 check_taint_not sprintf('%x', 123.456);
118 $_ = $a; # untaint $_
120 $_ = uc($a); # taint $_
124 /(\w)/; # taint $&, $`, $', $+, $1.
132 /(.)/; # untaint $&, $`, $', $+, $1.
140 /(\W)/; # taint $&, $`, $', $+, $1.
148 /(\s)/; # taint $&, $`, $', $+, $1.
156 /(\S)/; # taint $&, $`, $', $+, $1.
164 $_ = $a; # untaint $_
168 /(b)/; # this must not taint
176 $_ = $a; # untaint $_
180 $b = uc($a); # taint $b
181 s/(.+)/$b/; # this must taint only the $_
191 $_ = $a; # untaint $_
193 s/(.+)/b/; # this must not taint
202 $b = $a; # untaint $b
204 ($b = $a) =~ s/\w/$&/;
205 check_taint $b; # $b should be tainted.
206 check_taint_not $a; # $a should be not.
208 $_ = $a; # untaint $_
210 s/(\w)/\l$1/; # this must taint
219 $_ = $a; # untaint $_
221 s/(\w)/\L$1/; # this must taint
230 $_ = $a; # untaint $_
232 s/(\w)/\u$1/; # this must taint
241 $_ = $a; # untaint $_
243 s/(\w)/\U$1/; # this must taint
252 # After all this tainting $a should be cool.
256 { # This is just the previous tests copied here with a different
257 # compile-time pragma.
259 use locale ':not_characters'; # engage restricted locale with different
264 check_taint_not uc($a);
265 check_taint_not "\U$a";
266 check_taint_not ucfirst($a);
267 check_taint_not "\u$a";
268 check_taint_not lc($a);
269 check_taint_not "\L$a";
270 check_taint_not lcfirst($a);
271 check_taint_not "\l$a";
273 check_taint_not sprintf('%e', 123.456);
274 check_taint_not sprintf('%f', 123.456);
275 check_taint_not sprintf('%g', 123.456);
276 check_taint_not sprintf('%d', 123.456);
277 check_taint_not sprintf('%x', 123.456);
279 $_ = $a; # untaint $_
281 $_ = uc($a); # taint $_
285 /(\w)/; # taint $&, $`, $', $+, $1.
293 /(.)/; # untaint $&, $`, $', $+, $1.
301 /(\W)/; # taint $&, $`, $', $+, $1.
309 /(\s)/; # taint $&, $`, $', $+, $1.
317 /(\S)/; # taint $&, $`, $', $+, $1.
325 $_ = $a; # untaint $_
329 /(b)/; # this must not taint
337 $_ = $a; # untaint $_
341 $b = uc($a); # taint $b
342 s/(.+)/$b/; # this must taint only the $_
352 $_ = $a; # untaint $_
354 s/(.+)/b/; # this must not taint
363 $b = $a; # untaint $b
365 ($b = $a) =~ s/\w/$&/;
366 check_taint_not $b; # $b should be tainted.
367 check_taint_not $a; # $a should be not.
369 $_ = $a; # untaint $_
371 s/(\w)/\l$1/; # this must taint
380 $_ = $a; # untaint $_
382 s/(\w)/\L$1/; # this must taint
391 $_ = $a; # untaint $_
393 s/(\w)/\u$1/; # this must taint
402 $_ = $a; # untaint $_
404 s/(\w)/\U$1/; # this must taint
413 # After all this tainting $a should be cool.
418 # Here are in scope of 'use locale'
420 # I think we've seen quite enough of taint.
421 # Let us do some *real* locale work now,
422 # unless setlocale() is missing (i.e. minitest).
424 unless ($have_setlocale) {
425 print "1..$test_num\n";
429 # The test number before our first setlocale()
430 my $final_without_setlocale = $test_num;
434 debug "# Scanning for locales...\n";
436 # Note that it's okay that some languages have their native names
437 # capitalized here even though that's not "right". They are lowercased
438 # anyway later during the scanning process (and besides, some clueless
439 # vendor might have them capitalized erroneously anyway).
443 Arabic:ar:dz eg sa:6 arabic8
444 Brezhoneg Breton:br:fr:1 15
445 Bulgarski Bulgarian:bg:bg:5
446 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
447 Hrvatski Croatian:hr:hr:2
448 Cymraeg Welsh:cy:cy:1 14 15
450 Dansk Danish:dk:da:1 15
451 Nederlands Dutch:nl:be nl:1 15
452 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
454 Eesti Estonian:et:ee:4 6 13
455 Suomi Finnish:fi:fi:1 15
457 Deutsch German:de:at be ch de lu:1 15
458 Euskaraz Basque:eu:es fr:1 15
459 Galego Galician:gl:es:1 15
460 Ellada Greek:el:gr:7 g8
462 Greenlandic:kl:gl:4 6
463 Hebrew:iw:il:8 hebrew8
465 Indonesian:in:id:1 15
466 Gaeilge Irish:ga:IE:1 14 15
467 Italiano Italian:it:ch it:1 15
468 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
470 Latine Latin:la:va:1 15
472 Lithuanian:lt:lt:4 6 13
473 Macedonian:mk:mk:1 15
476 Norsk Norwegian:no no\@nynorsk:no:1 15
478 Polski Polish:pl:pl:2
480 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
481 Serbski Serbian:sr:yu:5
483 Slovene Slovenian:sl:si:2
484 Sqhip Albanian:sq:sq:1 15
485 Svenska Swedish:sv:fi se:1 15
487 Turkish:tr:tr:9 turkish8
491 if ($^O eq 'os390') {
492 # These cause heartburn. Broken locales?
493 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
494 $locales =~ s/Thai:th:th:11 tis620\n//;
497 sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
500 require "lib/locale/utf8";
502 require "lib/locale/latin1";
511 return if grep { $locale eq $_ } @Locale;
512 return unless setlocale(LC_ALL, $locale);
515 local $SIG{__WARN__} = sub {
516 $badutf8 = $_[0] =~ /Malformed UTF-8/;
518 $Locale =~ /UTF-?8/i;
522 ok(0, "Locale name contains malformed utf8");
525 push @Locale, $locale;
528 sub decode_encodings {
531 foreach (split(/ /, shift)) {
533 push @enc, "ISO8859-$1";
534 push @enc, "iso8859$1"; # HP
536 push @enc, "roman8"; # HP
540 push @enc, "$_.UTF-8";
543 if ($^O eq 'os390') {
544 push @enc, qw(IBM-037 IBM-819 IBM-1047);
553 trylocale("ISO8859-$_");
554 trylocale("iso8859$_");
555 trylocale("iso8859-$_");
556 trylocale("iso_8859_$_");
557 trylocale("isolatin$_");
558 trylocale("isolatin-$_");
559 trylocale("iso_latin_$_");
562 # Sanitize the environment so that we can run the external 'locale'
563 # program without the taint mode getting grumpy.
565 # $ENV{PATH} is special in VMS.
566 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
568 # Other subversive stuff.
569 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
571 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
573 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
574 # ain't great when we're running this testPERL_UNICODE= so that utf8
575 # locales will cause all IO hadles to default to (assume) utf8
576 next unless utf8::valid($_);
581 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
582 # The SYS$I18N_LOCALE logical name search list was not present on
583 # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
584 opendir(LOCALES, "SYS\$I18N_LOCALE:");
585 while ($_ = readdir(LOCALES)) {
590 } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
592 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
593 # is much easier and faster than the last resort method.
595 opendir(LOCALES, '/usr/share/locale');
596 while ($_ = readdir(LOCALES)) {
603 # This is going to be slow.
605 foreach my $locale (split(/\n/, $locales)) {
606 my ($locale_name, $language_codes, $country_codes, $encodings) =
608 my @enc = decode_encodings($encodings);
609 foreach my $loc (split(/ /, $locale_name)) {
611 foreach my $enc (@enc) {
612 trylocale("$loc.$enc");
615 foreach my $enc (@enc) {
616 trylocale("$loc.$enc");
619 foreach my $lang (split(/ /, $language_codes)) {
621 foreach my $country (split(/ /, $country_codes)) {
622 my $lc = "${lang}_${country}";
624 foreach my $enc (@enc) {
625 trylocale("$lc.$enc");
627 my $lC = "${lang}_\U${country}";
629 foreach my $enc (@enc) {
630 trylocale("$lC.$enc");
637 setlocale(LC_ALL, "C");
639 if ($^O eq 'darwin') {
640 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
641 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
642 (my $v) = $Config{osvers} =~ /^(\d+)/;
643 if ($v >= 8 and $v < 10) {
644 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
645 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
647 debug "# Skipping be_BY locales -- buggy in Darwin\n";
648 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
652 @Locale = sort @Locale;
654 debug "# Locales =\n";
662 my @Neoalpha; # Alnums that aren't in the C locale.
666 my ($Locale, $i, $test) = @_;
668 $Problem{$i}{$Locale} = 1;
669 debug "# failed $i with locale '$Locale'\n";
671 push @{$Okay{$i}}, $Locale;
675 my $first_locales_test_number = $final_without_setlocale + 1;
676 my $locales_test_number;
677 my $not_necessarily_a_problem_test_number;
678 my %setlocale_failed; # List of locales that setlocale() didn't work on
680 foreach $Locale (@Locale) {
681 $locales_test_number = $first_locales_test_number - 1;
682 debug "# Locale = $Locale\n";
684 unless (setlocale(LC_ALL, $Locale)) {
685 $setlocale_failed{$Locale} = $Locale;
689 # We test UTF-8 locales only under ':not_characters'; otherwise they have
690 # documented deficiencies. Non- UTF-8 locales are tested only under plain
691 # 'use locale', as otherwise we would have to convert everything in them
693 my $is_utf8_locale = $Locale =~ /UTF-?8/i;
699 if (! $is_utf8_locale) {
701 @Alnum_ = sort grep /\w/, map { chr } 0..255;
703 debug "# w = ", join("",@Alnum_), "\n";
705 # Sieve the uppercase and the lowercase.
708 if (/[^\d_]/) { # skip digits and the _
719 use locale ':not_characters';
720 @Alnum_ = sort grep /\w/, map { chr } 0..255;
721 debug "# w = ", join("",@Alnum_), "\n";
723 if (/[^\d_]/) { # skip digits and the _
733 foreach (keys %UPPER) {
734 $BoThCaSe{$_}++ if exists $lower{$_};
736 foreach (keys %lower) {
737 $BoThCaSe{$_}++ if exists $UPPER{$_};
739 foreach (keys %BoThCaSe) {
744 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
745 debug "# lower = ", join("", sort keys %lower ), "\n";
746 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
748 { # Find the alphabetic characters that are not considered alphabetics
749 # in the default (C) locale.
754 for (keys %UPPER, keys %lower) {
755 push(@Neoalpha, $_) if (/\W/);
759 @Neoalpha = sort @Neoalpha;
761 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
763 my $first_Neoalpha_test_number = $locales_test_number;
764 my $final_Neoalpha_test_number = $first_Neoalpha_test_number + 4;
765 if (@Neoalpha == 0) {
766 # If we have no Neoalphas the remaining tests are no-ops.
767 debug "# no Neoalpha, skipping tests $locales_test_number..$final_Neoalpha_test_number for locale '$Locale'\n";
768 foreach ($locales_test_number+1..$final_Neoalpha_test_number) {
769 push @{$Okay{$_}}, $Locale;
770 $locales_test_number++;
776 my $word = join('', @Neoalpha);
778 ++$locales_test_number;
779 $test_names{$locales_test_number} = 'Verify that alnums outside the C locale match \w';
781 if ($is_utf8_locale) {
782 use locale ':not_characters';
783 $ok = $word =~ /^(\w+)$/;
786 # Already in 'use locale'; this tests that exiting scopes works
787 $ok = $word =~ /^(\w+)$/;
789 tryneoalpha($Locale, $locales_test_number, $ok);
791 # Cross-check the whole 8-bit character set.
793 ++$locales_test_number;
794 $test_names{$locales_test_number} = 'Verify that \w and \W are mutually exclusive, as are \d, \D; \s, \S';
795 for (map { chr } 0..255) {
796 if ($is_utf8_locale) {
797 use locale ':not_characters';
798 $ok = (/\w/ xor /\W/) ||
803 $ok = (/\w/ xor /\W/) ||
807 tryneoalpha($Locale, $locales_test_number, $ok);
810 # Test for read-only scalars' locale vs non-locale comparisons.
815 if ($is_utf8_locale) {
816 use locale ':not_characters';
817 $ok = ($a cmp "qwerty") == 0;
821 $ok = ($a cmp "qwerty") == 0;
823 tryneoalpha($Locale, ++$locales_test_number, $ok);
824 $test_names{$locales_test_number} = 'Verify that cmp works with a read-only scalar; no- vs locale';
828 my ($from, $to, $lesser, $greater,
829 @test, %test, $test, $yes, $no, $sign);
831 ++$locales_test_number;
832 $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work';
833 $not_necessarily_a_problem_test_number = $locales_test_number;
836 $from = int(($_*@Alnum_)/10);
837 $to = $from + int(@Alnum_/10);
838 $to = $#Alnum_ if ($to > $#Alnum_);
839 $lesser = join('', @Alnum_[$from..$to]);
840 # Select a slice one character on.
842 $to = $#Alnum_ if ($to > $#Alnum_);
843 $greater = join('', @Alnum_[$from..$to]);
844 if ($is_utf8_locale) {
845 use locale ':not_characters';
846 ($yes, $no, $sign) = ($lesser lt $greater
848 : ("not ", " ", -1));
852 ($yes, $no, $sign) = ($lesser lt $greater
854 : ("not ", " ", -1));
856 # all these tests should FAIL (return 0).
857 # Exact lt or gt cannot be tested because
858 # in some locales, say, eacute and E may test equal.
861 $no.' ($lesser le $greater)', # 1
862 'not ($lesser ne $greater)', # 2
863 ' ($lesser eq $greater)', # 3
864 $yes.' ($lesser ge $greater)', # 4
865 $yes.' ($lesser ge $greater)', # 5
866 $yes.' ($greater le $lesser )', # 7
867 'not ($greater ne $lesser )', # 8
868 ' ($greater eq $lesser )', # 9
869 $no.' ($greater ge $lesser )', # 10
870 'not (($lesser cmp $greater) == -($sign))' # 11
872 @test{@test} = 0 x @test;
875 if ($is_utf8_locale) {
876 use locale ':not_characters';
877 $test{$ti} = eval $ti;
880 # Already in 'use locale';
881 $test{$ti} = eval $ti;
885 tryneoalpha($Locale, $locales_test_number, $test == 0);
887 debug "# lesser = '$lesser'\n";
888 debug "# greater = '$greater'\n";
889 debug "# lesser cmp greater = ",
890 $lesser cmp $greater, "\n";
891 debug "# greater cmp lesser = ",
892 $greater cmp $lesser, "\n";
893 debug "# (greater) from = $from, to = $to\n";
895 debugf("# %-40s %-4s", $ti,
896 $test{$ti} ? 'FAIL' : 'ok');
897 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
898 debugf("(%s == %4d)", $1, eval $1);
909 if ($locales_test_number != $final_Neoalpha_test_number) {
910 die("The delta for \$final_Neoalpha needs to be updated from "
911 . ($final_Neoalpha_test_number - $first_Neoalpha_test_number)
913 . ($locales_test_number - $first_Neoalpha_test_number)
937 if (! $is_utf8_locale) {
940 my ($x, $y) = (1.23, 1.23);
943 printf ''; # printf used to reset locale to "C"
948 my $z = sprintf ''; # sprintf used to reset locale to "C"
955 local $SIG{__WARN__} =
961 # The == (among other ops) used to warn for locales
962 # that had something else than "." as the radix character.
970 # The earlier test was $e = "$x". But this fails [perl
971 # #108378], and the "no locale" was commented out. But doing
972 # that made all the tests in the block after this one
973 # meaningless, as originally it was testing the nesting of a
974 # "no locale" scope, and how it recovers after that scope is
975 # done. So I (khw) filed a bug report and changed this so it
976 # wouldn't fail. It seemed too much work to add TODOs
977 # instead. Should this be fixed, the following test names
978 # would need to be revised; they mostly don't really test
979 # anything currently.
993 $ok12 = abs(($f + $g) - 3.57) < 0.01;
998 use locale ':not_characters';
1000 my ($x, $y) = (1.23, 1.23);
1002 printf ''; # printf used to reset locale to "C"
1007 my $z = sprintf ''; # sprintf used to reset locale to "C"
1013 local $SIG{__WARN__} =
1036 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1041 tryneoalpha($Locale, ++$locales_test_number, $ok1);
1042 $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results';
1043 my $first_a_test = $locales_test_number;
1045 debug "# $first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n";
1047 tryneoalpha($Locale, ++$locales_test_number, $ok2);
1048 $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results';
1050 my $first_c_test = $locales_test_number;
1052 tryneoalpha($Locale, ++$locales_test_number, $ok3);
1053 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant';
1055 tryneoalpha($Locale, ++$locales_test_number, $ok4);
1056 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar';
1058 tryneoalpha($Locale, ++$locales_test_number, $ok5);
1059 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf';
1061 debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n";
1063 tryneoalpha($Locale, ++$locales_test_number, $ok6);
1064 $test_names{$locales_test_number} = 'Verify that can assign numerically under inner no-locale block';
1065 my $first_e_test = $locales_test_number;
1067 tryneoalpha($Locale, ++$locales_test_number, $ok7);
1068 $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale';
1070 tryneoalpha($Locale, ++$locales_test_number, $ok8);
1071 $test_names{$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale';
1073 debug "# $first_e_test..$locales_test_number: \$e = $e, no locale\n";
1075 tryneoalpha($Locale, ++$locales_test_number, $ok9);
1076 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant';
1077 my $first_f_test = $locales_test_number;
1079 tryneoalpha($Locale, ++$locales_test_number, $ok10);
1080 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar';
1082 tryneoalpha($Locale, ++$locales_test_number, $ok11);
1083 $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';
1085 tryneoalpha($Locale, ++$locales_test_number, $ok12);
1086 $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';
1088 tryneoalpha($Locale, ++$locales_test_number, $ok13);
1089 $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot';
1091 debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n";
1093 # Does taking lc separately differ from taking
1094 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
1095 # The bug was in the caching of the 'o'-magic.
1096 if (! $is_utf8_locale) {
1102 return $lc0 cmp $lc1;
1106 return lc($_[0]) cmp lc($_[1]);
1113 tryneoalpha($Locale, ++$locales_test_number,
1114 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
1115 lcA($x, $z) == 0 && lcB($x, $z) == 0);
1118 use locale ':not_characters';
1123 return $lc0 cmp $lc1;
1127 return lc($_[0]) cmp lc($_[1]);
1134 tryneoalpha($Locale, ++$locales_test_number,
1135 lcC($x, $y) == 1 && lcD($x, $y) == 1 ||
1136 lcC($x, $z) == 0 && lcD($x, $z) == 0);
1138 $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp';
1140 # Does lc of an UPPER (if different from the UPPER) match
1141 # case-insensitively the UPPER, and does the UPPER match
1142 # case-insensitively the lc of the UPPER. And vice versa.
1146 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
1149 ++$locales_test_number;
1150 $test_names{$locales_test_number} = 'Verify case insensitive matching works';
1151 foreach my $x (sort keys %UPPER) {
1152 if (! $is_utf8_locale) {
1154 next unless uc $y eq $x;
1155 print "# UPPER $x lc $y ",
1156 $x =~ /$y/i ? 1 : 0, " ",
1157 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1159 # If $x and $y contain regular expression characters
1160 # AND THEY lowercase (/i) to regular expression characters,
1161 # regcomp() will be mightily confused. No, the \Q doesn't
1162 # help here (maybe regex engine internal lowercasing
1163 # is done after the \Q?) An example of this happening is
1164 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
1165 # the chr(173) (the "[") is the lowercase of the chr(235).
1167 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
1168 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
1169 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
1170 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
1171 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
1172 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
1174 # Similar things can happen even under (bastardised)
1175 # non-EBCDIC locales: in many European countries before the
1176 # advent of ISO 8859-x nationally customised versions of
1177 # ISO 646 were devised, reusing certain punctuation
1178 # characters for modified characters needed by the
1179 # country/language. For example, the "|" might have
1180 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
1182 if ($x =~ $re || $y =~ $re) {
1183 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1186 # With utf8 both will fail since the locale concept
1187 # of upper/lower does not work well in Unicode.
1188 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
1191 use locale ':not_characters';
1193 next unless uc $y eq $x;
1194 print "# UPPER $x lc $y ",
1195 $x =~ /$y/i ? 1 : 0, " ",
1196 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1198 # Here, we can fully test things, unlike plain 'use locale',
1199 # because this form does work well with Unicode
1200 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1204 foreach my $x (sort keys %lower) {
1205 if (! $is_utf8_locale) {
1207 next unless lc $y eq $x;
1208 print "# lower $x uc $y ",
1209 $x =~ /$y/i ? 1 : 0, " ",
1210 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1211 if ($x =~ $re || $y =~ $re) { # See above.
1212 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1215 # With utf8 both will fail since the locale concept
1216 # of upper/lower does not work well in Unicode.
1217 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
1220 use locale ':not_characters';
1222 next unless lc $y eq $x;
1223 print "# lower $x uc $y ",
1224 $x =~ /$y/i ? 1 : 0, " ",
1225 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1226 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1229 tryneoalpha($Locale, $locales_test_number, @f == 0);
1231 print "# failed $locales_test_number locale '$Locale' characters @f\n"
1236 my $final_locales_test_number = $locales_test_number;
1238 # Recount the errors.
1240 foreach ($first_locales_test_number..$final_locales_test_number) {
1241 if (%setlocale_failed) {
1244 elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
1245 if (defined $not_necessarily_a_problem_test_number
1246 && $_ == $not_necessarily_a_problem_test_number)
1248 print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n";
1249 print "# It usually indicates a problem in the environment,\n";
1250 print "# not in Perl itself.\n";
1255 print " $test_names{$_}" if defined $test_names{$_};
1259 # Give final advice.
1263 foreach ($first_locales_test_number..$final_locales_test_number) {
1265 my @f = sort keys %{ $Problem{$_} };
1266 my $f = join(" ", @f);
1267 $f =~ s/(.{50,60}) /$1\n#\t/g;
1270 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
1272 "# on your system may have errors because the locale test $_\n",
1273 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
1277 # If your users are not using these locales you are safe for the moment,
1278 # but please report this failure first to perlbug\@perl.com using the
1279 # perlbug script (as described in the INSTALL file) so that the exact
1280 # details of the failures can be sorted out first and then your operating
1281 # system supplier can be alerted about these anomalies.
1288 # Tell which locales were okay and which were not.
1293 foreach my $l (@Locale) {
1295 if ($setlocale_failed{$l}) {
1300 ($first_locales_test_number..$final_locales_test_number)
1302 $p++ if $Problem{$t}{$l};
1305 push @s, $l if $p == 0;
1306 push @F, $l unless $p == 0;
1310 my $s = join(" ", @s);
1311 $s =~ s/(.{50,60}) /$1\n#\t/g;
1314 "# The following locales\n#\n",
1316 "# tested okay.\n#\n",
1318 warn "# None of your locales were fully okay.\n";
1322 my $F = join(" ", @F);
1323 $F =~ s/(.{50,60}) /$1\n#\t/g;
1326 "# The following locales\n#\n",
1328 "# had problems.\n#\n",
1330 warn "# None of your locales were broken.\n";
1334 $test_num = $final_locales_test_number;
1336 # Test that tainting and case changing works on utf8 strings. These tests are
1337 # placed last to avoid disturbing the hard-coded test numbers that existed at
1338 # the time these were added above this in this file.
1339 # This also tests that locale overrides unicode_strings in the same scope for
1341 setlocale(LC_ALL, "C");
1344 use feature 'unicode_strings';
1346 foreach my $function ("uc", "ucfirst", "lc", "lcfirst") {
1347 my @list; # List of code points to test for $function
1349 # Used to calculate the changed case for ASCII characters by using the
1350 # ord, instead of using one of the functions under test.
1351 my $ascii_case_change_delta;
1352 my $above_latin1_case_change_delta; # Same for the specific ords > 255
1355 # We test an ASCII character, which should change case and be tainted;
1356 # a Latin1 character, which shouldn't change case under this C locale,
1358 # an above-Latin1 character that when the case is changed would cross
1359 # the 255/256 boundary, so doesn't change case and isn't tainted
1360 # (the \x{149} is one of these, but changes into 2 characters, the
1361 # first one of which doesn't cross the boundary.
1362 # the final one in each list is an above-Latin1 character whose case
1363 # does change, and shouldn't be tainted. The code below uses its
1364 # position in its list as a marker to indicate that it, unlike the
1365 # other code points above ASCII, has a successful case change
1366 if ($function =~ /^u/) {
1367 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
1368 $ascii_case_change_delta = -32;
1369 $above_latin1_case_change_delta = -1;
1372 @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}");
1373 $ascii_case_change_delta = +32;
1374 $above_latin1_case_change_delta = +1;
1376 foreach my $is_utf8_locale (0 .. 1) {
1377 foreach my $j (0 .. $#list) {
1378 my $char = $list[$j];
1380 for my $encoded_in_utf8 (0 .. 1) {
1383 if (! $is_utf8_locale) {
1384 $should_be = ($j == $#list)
1385 ? chr(ord($char) + $above_latin1_case_change_delta)
1386 : (length $char == 0 || ord($char) > 127)
1388 : chr(ord($char) + $ascii_case_change_delta);
1390 # This monstrosity is in order to avoid using an eval,
1391 # which might perturb the results
1392 $changed = ($function eq "uc")
1394 : ($function eq "ucfirst")
1396 : ($function eq "lc")
1398 : ($function eq "lcfirst")
1400 : die("Unexpected function \"$function\"");
1406 # For utf8-locales the case changing functions
1407 # should work just like they do outside of locale.
1408 # Can use eval here because not testing it when
1410 $should_be = eval "$function('$char')";
1411 die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if $@;
1414 use locale ':not_characters';
1415 $changed = ($function eq "uc")
1417 : ($function eq "ucfirst")
1419 : ($function eq "lc")
1421 : ($function eq "lcfirst")
1423 : die("Unexpected function \"$function\"");
1425 ok($changed eq $should_be,
1426 "$function(\"$char\") in C locale "
1427 . (($is_utf8_locale)
1428 ? "(use locale ':not_characters'"
1430 . (($encoded_in_utf8)
1431 ? "; encoded in utf8)"
1432 : "; not encoded in utf8)")
1433 . " should be \"$should_be\", got \"$changed\"");
1435 # Tainting shouldn't happen for utf8 locales, empty
1436 # strings, or those characters above 255.
1437 (! $is_utf8_locale && length($char) > 0 && ord($char) < 256)
1438 ? check_taint($changed)
1439 : check_taint_not($changed);
1441 # Use UTF-8 next time through the loop
1442 utf8::upgrade($char);
1449 print "1..$test_num\n";