6 require Config; import Config;
7 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
15 my $have_setlocale = 0;
18 import POSIX ':locale_h';
22 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
23 # and mingw32 uses said silly CRT
24 $have_setlocale = 0 if $^O eq 'MSWin32' && $Config{cc} =~ /^(cl|gcc)/i;
26 print "1..", ($have_setlocale ? 102 : 98), "\n";
29 $English $German $French $Spanish
30 @C @English @German @French @Spanish
31 $Locale @Locale %iLocale %UPPER %lower @Neoalpha);
36 my ($n, $result) = @_;
38 print 'not ' unless ($result);
42 # First we'll do a lot of taint checking for locales.
43 # This is the easiest to test, actually, as any locale,
44 # even the default locale will taint under 'use locale'.
46 sub is_tainted { # hello, camel two.
47 local $^W; # no warnings 'undef'
49 not eval { $dummy = join("", @_), kill 0; 1 }
52 sub check_taint ($$) {
53 ok $_[0], is_tainted($_[1]);
56 sub check_taint_not ($$) {
57 ok $_[0], not is_tainted($_[1]);
60 use locale; # engage locale and therefore locale taint.
62 check_taint_not 1, $a;
64 check_taint 2, uc($a);
65 check_taint 3, "\U$a";
66 check_taint 4, ucfirst($a);
67 check_taint 5, "\u$a";
68 check_taint 6, lc($a);
69 check_taint 7, "\L$a";
70 check_taint 8, lcfirst($a);
71 check_taint 9, "\l$a";
73 check_taint 10, sprintf('%e', 123.456);
74 check_taint 11, sprintf('%f', 123.456);
75 check_taint 12, sprintf('%g', 123.456);
76 check_taint_not 13, sprintf('%d', 123.456);
77 check_taint_not 14, sprintf('%x', 123.456);
81 $_ = uc($a); # taint $_
85 /(\w)/; # taint $&, $`, $', $+, $1.
91 check_taint_not 21, $2;
93 /(.)/; # untaint $&, $`, $', $+, $1.
94 check_taint_not 22, $&;
95 check_taint_not 23, $`;
96 check_taint_not 24, $';
97 check_taint_not 25, $+;
98 check_taint_not 26, $1;
99 check_taint_not 27, $2;
101 /(\W)/; # taint $&, $`, $', $+, $1.
107 check_taint_not 33, $2;
109 /(\s)/; # taint $&, $`, $', $+, $1.
115 check_taint_not 39, $2;
117 /(\S)/; # taint $&, $`, $', $+, $1.
123 check_taint_not 45, $2;
125 $_ = $a; # untaint $_
127 check_taint_not 46, $_;
129 /(b)/; # this must not taint
130 check_taint_not 47, $&;
131 check_taint_not 48, $`;
132 check_taint_not 49, $';
133 check_taint_not 50, $+;
134 check_taint_not 51, $1;
135 check_taint_not 52, $2;
137 $_ = $a; # untaint $_
139 check_taint_not 53, $_;
141 $b = uc($a); # taint $b
142 s/(.+)/$b/; # this must taint only the $_
145 check_taint_not 55, $&;
146 check_taint_not 56, $`;
147 check_taint_not 57, $';
148 check_taint_not 58, $+;
149 check_taint_not 59, $1;
150 check_taint_not 60, $2;
152 $_ = $a; # untaint $_
154 s/(.+)/b/; # this must not taint
155 check_taint_not 61, $_;
156 check_taint_not 62, $&;
157 check_taint_not 63, $`;
158 check_taint_not 64, $';
159 check_taint_not 65, $+;
160 check_taint_not 66, $1;
161 check_taint_not 67, $2;
163 $b = $a; # untaint $b
165 ($b = $a) =~ s/\w/$&/;
166 check_taint 68, $b; # $b should be tainted.
167 check_taint_not 69, $a; # $a should be not.
169 $_ = $a; # untaint $_
171 s/(\w)/\l$1/; # this must taint
178 check_taint_not 76, $2;
180 $_ = $a; # untaint $_
182 s/(\w)/\L$1/; # this must taint
189 check_taint_not 83, $2;
191 $_ = $a; # untaint $_
193 s/(\w)/\u$1/; # this must taint
200 check_taint_not 90, $2;
202 $_ = $a; # untaint $_
204 s/(\w)/\U$1/; # this must taint
211 check_taint_not 97, $2;
213 # After all this tainting $a should be cool.
215 check_taint_not 98, $a;
217 # I think we've seen quite enough of taint.
218 # Let us do some *real* locale work now,
219 # unless setlocale() is missing (i.e. minitest).
221 exit unless $have_setlocale;
224 sort grep /\w/, map { chr } 0..255
227 sub locatelocale ($$@) {
228 my ($lcall, $alnum, @try) = @_;
233 local $^W = 0; # suppress "Subroutine LC_ALL redefined"
234 if (setlocale(&LC_ALL, $_)) {
241 @$alnum = () unless (defined $$lcall);
244 # Find some default locale
246 locatelocale(\$Locale, \@Locale, qw(C POSIX));
248 # Find some English locale
250 locatelocale(\$English, \@English,
251 qw(en_US.ISO8859-1 en_GB.ISO8859-1
252 en en_US en_UK en_IE en_CA en_AU en_NZ
253 english english.iso88591
254 american american.iso88591
255 british british.iso88591
258 # Find some German locale
260 locatelocale(\$German, \@German,
261 qw(de_DE.ISO8859-1 de_AT.ISO8859-1 de_CH.ISO8859-1
263 german german.iso88591));
265 # Find some French locale
267 locatelocale(\$French, \@French,
268 qw(fr_FR.ISO8859-1 fr_BE.ISO8859-1 fr_CA.ISO8859-1 fr_CH.ISO8859-1
269 fr fr_FR fr_BE fr_CA fr_CH
270 french french.iso88591));
272 # Find some Spanish locale
274 locatelocale(\$Spanish, \@Spanish,
275 qw(es_AR.ISO8859-1 es_BO.ISO8859-1 es_CL.ISO8859-1
276 es_CO.ISO8859-1 es_CR.ISO8859-1 es_EC.ISO8859-1
277 es_ES.ISO8859-1 es_GT.ISO8859-1 es_MX.ISO8859-1
278 es_NI.ISO8859-1 es_PA.ISO8859-1 es_PE.ISO8859-1
279 es_PY.ISO8859-1 es_SV.ISO8859-1 es_UY.ISO8859-1 es_VE.ISO8859-1
284 es_PY es_SV es_UY es_VE
285 spanish spanish.iso88591));
287 # Select the largest of the alpha(num)bets.
289 ($Locale, @Locale) = ($English, @English)
290 if (@English > @Locale);
291 ($Locale, @Locale) = ($German, @German)
292 if (@German > @Locale);
293 ($Locale, @Locale) = ($French, @French)
294 if (@French > @Locale);
295 ($Locale, @Locale) = ($Spanish, @Spanish)
296 if (@Spanish > @Locale);
300 setlocale(&LC_ALL, $Locale);
303 # Sort it now that LC_ALL has been set.
305 @Locale = sort @Locale;
307 print "# Locale = $Locale\n";
308 print "# Alnum_ = @Locale\n";
318 # Sieve the uppercase and the lowercase.
321 if (/[^\d_]/) { # skip digits and the _
330 # Find the alphabets that are not alphabets in the default locale.
335 for (keys %UPPER, keys %lower) {
336 push(@Neoalpha, $_) if (/\W/);
340 @Neoalpha = sort @Neoalpha;
345 my $word = join('', @Neoalpha);
349 print 'not ' if ($1 ne $word);
353 # Find places where the collation order differs from the default locale.
355 print "# testing 100\n";
362 @k = sort (keys %UPPER, keys %lower);
365 for ($i = 0; $i < @k; $i++) {
366 for ($j = $i + 1; $j < @k; $j++) {
367 if ($iLocale{$k[$j]} < $iLocale{$k[$i]}) {
368 push(@d, [$k[$j], $k[$i]]);
373 # Cross-check those places.
378 print "# failed 100 at:\n";
379 print "# i = $i, j = $j, i ",
380 $i le $j ? 'le' : 'gt', " j\n";
388 # Cross-check whole character set.
390 print "# testing 101\n";
391 for (map { chr } 0..255) {
392 if (/\w/ and /\W/) { print 'not '; last }
393 if (/\d/ and /\D/) { print 'not '; last }
394 if (/\s/ and /\S/) { print 'not '; last }
395 if (/\w/ and /\D/ and not /_/ and
396 not (exists $UPPER{$_} or exists $lower{$_})) {
397 print "# failed 101 at:\n";
398 print "# ", ord($_), " '$_'\n";
405 # Test for read-onlys.
412 print "not " if $a cmp "qwerty";
417 # This test must be the last one because its failure is not fatal.
418 # The @Locale should be internally consistent.
419 # Thanks to Hallvard Furuseth <h.b.furuseth@usit.uio.no>
420 # for inventing a way to test for ordering consistency
421 # without requiring any particular order.
424 print "# testing 103\n";
426 my ($from, $to, $lesser, $greater, @test, %test, $test, $yes, $no, $sign);
430 $from = int(($_*@Locale)/10);
431 $to = $from + int(@Locale/10);
432 $to = $#Locale if ($to > $#Locale);
433 $lesser = join('', @Locale[$from..$to]);
434 # Select a slice one character on.
436 $to = $#Locale if ($to > $#Locale);
437 $greater = join('', @Locale[$from..$to]);
438 ($yes, $no, $sign) = ($lesser lt $greater
440 : ("not ", " ", -1));
441 # all these tests should FAIL (return 0).
444 $no.' ($lesser lt $greater)', # 0
445 $no.' ($lesser le $greater)', # 1
446 'not ($lesser ne $greater)', # 2
447 ' ($lesser eq $greater)', # 3
448 $yes.' ($lesser ge $greater)', # 4
449 $yes.' ($lesser gt $greater)', # 5
450 $yes.' ($greater lt $lesser )', # 6
451 $yes.' ($greater le $lesser )', # 7
452 'not ($greater ne $lesser )', # 8
453 ' ($greater eq $lesser )', # 9
454 $no.' ($greater ge $lesser )', # 10
455 $no.' ($greater gt $lesser )', # 11
456 'not (($lesser cmp $greater) == -$sign)' # 12
458 @test{@test} = 0 x @test;
460 for my $ti (@test) { $test{$ti} = eval $ti ; $test ||= $test{$ti} }
462 print "# failed 103 at:\n";
463 print "# lesser = '$lesser'\n";
464 print "# greater = '$greater'\n";
465 print "# lesser cmp greater = ", $lesser cmp $greater, "\n";
466 print "# greater cmp lesser = ", $greater cmp $lesser, "\n";
467 print "# (greater) from = $from, to = $to\n";
469 printf("# %-40s %-4s", $ti,
470 $test{$ti} ? 'FAIL' : 'ok');
471 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
472 printf("(%s == %4d)", $1, eval $1);
477 warn "The locale definition on your system may have errors.\n";