5 unshift @INC, '../lib';
6 require Config; import Config;
7 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
17 my $have_setlocale = 0;
20 import POSIX ':locale_h';
26 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
27 # and mingw32 uses said silly CRT
28 $have_setlocale = 0 if $^O eq 'MSWin32' && $Config{cc} =~ /^(cl|gcc)/i;
30 # 103 (the last test) may fail but that is sort-of okay.
31 # (It indicates something broken in the environment, not Perl)
33 print "1..", ($have_setlocale ? 103 : 98), "\n";
36 $English $German $French $Spanish
37 @C @English @German @French @Spanish
38 $Locale @Locale %UPPER %lower %bothcase @Neoalpha);
43 my ($n, $result) = @_;
45 print 'not ' unless ($result);
49 # First we'll do a lot of taint checking for locales.
50 # This is the easiest to test, actually, as any locale,
51 # even the default locale will taint under 'use locale'.
53 sub is_tainted { # hello, camel two.
54 local $^W; # no warnings 'undef'
56 not eval { $dummy = join("", @_), kill 0; 1 }
59 sub check_taint ($$) {
60 ok $_[0], is_tainted($_[1]);
63 sub check_taint_not ($$) {
64 ok $_[0], not is_tainted($_[1]);
67 use locale; # engage locale and therefore locale taint.
69 check_taint_not 1, $a;
71 check_taint 2, uc($a);
72 check_taint 3, "\U$a";
73 check_taint 4, ucfirst($a);
74 check_taint 5, "\u$a";
75 check_taint 6, lc($a);
76 check_taint 7, "\L$a";
77 check_taint 8, lcfirst($a);
78 check_taint 9, "\l$a";
80 check_taint 10, sprintf('%e', 123.456);
81 check_taint 11, sprintf('%f', 123.456);
82 check_taint 12, sprintf('%g', 123.456);
83 check_taint_not 13, sprintf('%d', 123.456);
84 check_taint_not 14, sprintf('%x', 123.456);
88 $_ = uc($a); # taint $_
92 /(\w)/; # taint $&, $`, $', $+, $1.
98 check_taint_not 21, $2;
100 /(.)/; # untaint $&, $`, $', $+, $1.
101 check_taint_not 22, $&;
102 check_taint_not 23, $`;
103 check_taint_not 24, $';
104 check_taint_not 25, $+;
105 check_taint_not 26, $1;
106 check_taint_not 27, $2;
108 /(\W)/; # taint $&, $`, $', $+, $1.
114 check_taint_not 33, $2;
116 /(\s)/; # taint $&, $`, $', $+, $1.
122 check_taint_not 39, $2;
124 /(\S)/; # taint $&, $`, $', $+, $1.
130 check_taint_not 45, $2;
132 $_ = $a; # untaint $_
134 check_taint_not 46, $_;
136 /(b)/; # this must not taint
137 check_taint_not 47, $&;
138 check_taint_not 48, $`;
139 check_taint_not 49, $';
140 check_taint_not 50, $+;
141 check_taint_not 51, $1;
142 check_taint_not 52, $2;
144 $_ = $a; # untaint $_
146 check_taint_not 53, $_;
148 $b = uc($a); # taint $b
149 s/(.+)/$b/; # this must taint only the $_
152 check_taint_not 55, $&;
153 check_taint_not 56, $`;
154 check_taint_not 57, $';
155 check_taint_not 58, $+;
156 check_taint_not 59, $1;
157 check_taint_not 60, $2;
159 $_ = $a; # untaint $_
161 s/(.+)/b/; # this must not taint
162 check_taint_not 61, $_;
163 check_taint_not 62, $&;
164 check_taint_not 63, $`;
165 check_taint_not 64, $';
166 check_taint_not 65, $+;
167 check_taint_not 66, $1;
168 check_taint_not 67, $2;
170 $b = $a; # untaint $b
172 ($b = $a) =~ s/\w/$&/;
173 check_taint 68, $b; # $b should be tainted.
174 check_taint_not 69, $a; # $a should be not.
176 $_ = $a; # untaint $_
178 s/(\w)/\l$1/; # this must taint
185 check_taint_not 76, $2;
187 $_ = $a; # untaint $_
189 s/(\w)/\L$1/; # this must taint
196 check_taint_not 83, $2;
198 $_ = $a; # untaint $_
200 s/(\w)/\u$1/; # this must taint
207 check_taint_not 90, $2;
209 $_ = $a; # untaint $_
211 s/(\w)/\U$1/; # this must taint
218 check_taint_not 97, $2;
220 # After all this tainting $a should be cool.
222 check_taint_not 98, $a;
224 # I think we've seen quite enough of taint.
225 # Let us do some *real* locale work now,
226 # unless setlocale() is missing (i.e. minitest).
228 exit unless $have_setlocale;
233 Arabic:ar:dz eg sa:6 arabic8
235 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW tw.EUC
241 English American British:en:au ca gb ie nz us uk:1 cp850
244 French:fr:be ca ch fr:1
247 Hebrew:iw:il:8 hebrew8
251 Japanese:ja:jp:euc eucJP jp.EUC sjis
257 Portuguese:po:po br:1
259 Russian:ru:ru su:5 koi8 koi8r koi8u cp1251
262 Spanish:es:ar bo cl co cr ec es gt mx ni pa pe py sv uy ve:1
265 Turkish:tr:tr:9 turkish8
273 sort grep /\w/, map { chr } 0..255
278 if (setlocale(LC_ALL, $locale)) {
279 push @Locale, $locale;
283 sub decode_encodings {
286 foreach (split(/ /, shift)) {
288 push @enc, "ISO8859-$1";
289 push @enc, "iso8859$1"; # HP
291 push @enc, "roman8"; # HP
304 trylocale("ISO8859-$_");
305 trylocale("iso_8859_$_");
306 trylocale("iso8859$_");
309 foreach my $locale (split(/\n/, $locales)) {
310 my ($locale_name, $language_codes, $country_codes, $encodings) =
312 my @enc = decode_encodings($encodings);
313 foreach my $loc (split(/ /, $locale_name)) {
315 foreach my $enc (@enc) {
316 trylocale("$loc.$enc");
319 foreach my $enc (@enc) {
320 trylocale("$loc.$enc");
323 foreach my $lang (split(/ /, $language_codes)) {
325 foreach my $country (split(/ /, $country_codes)) {
326 my $lc = "${lang}_${country}";
328 foreach my $enc (@enc) {
329 trylocale("$lc.$enc");
331 my $lC = "${lang}_\U${country}";
333 foreach my $enc (@enc) {
334 trylocale("$lC.$enc");
340 @Locale = sort @Locale;
350 debug "# Locales = @Locale\n";
354 foreach $Locale (@Locale) {
355 debug "# Locale = $Locale\n";
356 @Alnum_ = getalnum_();
357 debug "# \\w = @Alnum_\n";
359 unless (setlocale(LC_ALL, $Locale)) {
361 $Problem{$_}{$Locale} = -1;
366 # Sieve the uppercase and the lowercase.
368 %UPPER = %lower = %bothcase = ();
370 if (/[^\d_]/) { # skip digits and the _
379 foreach (keys %UPPER) {
380 $bothcase{$_}++ if exists $lower{$_};
382 foreach (keys %lower) {
383 $bothcase{$_}++ if exists $UPPER{$_};
385 foreach (keys %bothcase) {
390 debug "# UPPER = ", join(" ", sort keys %UPPER ), "\n";
391 debug "# lower = ", join(" ", sort keys %lower ), "\n";
392 debug "# bothcase = ", join(" ", sort keys %bothcase), "\n";
394 # Find the alphabets that are not alphabets in the default locale.
400 for (keys %UPPER, keys %lower) {
401 push(@Neoalpha, $_) if (/\W/);
405 @Neoalpha = sort @Neoalpha;
407 debug "# Neoalpha = @Neoalpha\n";
409 if (@Neoalpha == 0) {
410 # If we have no Neoalphas the remaining tests are no-ops.
411 debug "# no Neoalpha, skipping tests 99..103 for locale '$Locale'\n";
417 debug "# testing 99 with locale '$Locale'\n";
419 my $word = join('', @Neoalpha);
424 $Problem{99}{$Locale} = 1;
425 debug "# failed 99 ($1 vs $word)\n";
429 # Test #100 removed but to preserve historical test number
430 # consistency we do not renumber the remaining tests.
432 # Cross-check whole character set.
434 debug "# testing 101 with locale '$Locale'\n";
435 for (map { chr } 0..255) {
436 if ((/\w/ and /\W/) or (/\d/ and /\D/) or (/\s/ and /\S/)) {
437 $Problem{101}{$Locale} = 1;
438 debug "# failed 101\n";
443 # Test for read-only scalars' locale vs non-locale comparisons.
445 debug "# testing 102 with locale '$Locale'\n";
451 if ($a cmp "qwerty") {
452 $Problem{102}{$Locale} = 1;
453 debug "# failed 102\n";
458 # This test must be the last one because its failure is not fatal.
459 # The @Alnum_ should be internally consistent.
460 # Thanks to Hallvard Furuseth <h.b.furuseth@usit.uio.no>
461 # for inventing a way to test for ordering consistency
462 # without requiring any particular order.
465 debug "# testing 103 with locale '$Locale'\n";
467 my ($from, $to, $lesser, $greater,
468 @test, %test, $test, $yes, $no, $sign);
472 $from = int(($_*@Alnum_)/10);
473 $to = $from + int(@Alnum_/10);
474 $to = $#Alnum_ if ($to > $#Alnum_);
475 $lesser = join('', @Alnum_[$from..$to]);
476 # Select a slice one character on.
478 $to = $#Alnum_ if ($to > $#Alnum_);
479 $greater = join('', @Alnum_[$from..$to]);
480 ($yes, $no, $sign) = ($lesser lt $greater
482 : ("not ", " ", -1));
483 # all these tests should FAIL (return 0).
484 # Exact lt or gt cannot be tested because
485 # in some locales, say, eacute and E may test equal.
488 $no.' ($lesser le $greater)', # 1
489 'not ($lesser ne $greater)', # 2
490 ' ($lesser eq $greater)', # 3
491 $yes.' ($lesser ge $greater)', # 4
492 $yes.' ($lesser ge $greater)', # 5
493 $yes.' ($greater le $lesser )', # 7
494 'not ($greater ne $lesser )', # 8
495 ' ($greater eq $lesser )', # 9
496 $no.' ($greater ge $lesser )', # 10
497 'not (($lesser cmp $greater) == -$sign)' # 12
499 @test{@test} = 0 x @test;
501 for my $ti (@test) { $test{$ti} = eval $ti ; $test ||= $test{$ti} }
503 $Problem{103}{$Locale} = 1;
504 debug "# failed 103 at:\n";
505 debug "# lesser = '$lesser'\n";
506 debug "# greater = '$greater'\n";
507 debug "# lesser cmp greater = ", $lesser cmp $greater, "\n";
508 debug "# greater cmp lesser = ", $greater cmp $lesser, "\n";
509 debug "# (greater) from = $from, to = $to\n";
511 debugf("# %-40s %-4s", $ti,
512 $test{$ti} ? 'FAIL' : 'ok');
513 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
514 debugf("(%s == %4d)", $1, eval $1);
530 print "# The failure of test 103 is not necessarily fatal.\n";
531 print "# It usually indicates a problem in the enviroment,\n";
532 print "# not in Perl itself.\n";
543 my @f = sort keys %{ $Problem{$_} };
544 my $f = join(" ", @f);
545 $f =~ s/(.{50,60}) /$1\n#\t/g;
547 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
549 "# on your system may have errors because the locale test $_\n",
550 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
554 # If your users are not using these locales you are safe for the moment,
555 # but please report this failure first to perlbug\@perl.com using the
556 # perlbug script (as described in the INSTALL file) so that the exact
557 # details of the failures can be sorted out first and then your operating
558 # system supplier can be alerted about these anomalies.
568 foreach my $l (@Locale) {
570 foreach my $t (99..103) {
571 $p++ if $Problem{$t}{$l};
573 push @s, $l if $p == 0;
576 my $s = join(" ", @s);
577 $s =~ s/(.{50,60}) /$1\n#\t/g;
580 "# The following locales\n#\n",
582 "# tested okay.\n#\n",