Spice up locale.t.
[p5sagit/p5-mst-13.2.git] / t / pragma / locale.t
1 #!./perl -wT
2
3 BEGIN {
4     chdir 't' if -d 't';
5     unshift @INC, '../lib';
6     require Config; import Config;
7     if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
8         print "1..0\n";
9         exit;
10     }
11 }
12
13 use strict;
14
15 my $debug = 1;
16
17 sub debug {
18     print @_ if $debug;
19 }
20
21 sub debugf {
22     printf @_ if $debug;
23 }
24
25 my $have_setlocale = 0;
26 eval {
27     require POSIX;
28     import POSIX ':locale_h';
29     $have_setlocale++;
30 };
31
32 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
33 # and mingw32 uses said silly CRT
34 $have_setlocale = 0 if $^O eq 'MSWin32' && $Config{cc} =~ /^(cl|gcc)/i;
35
36 print "1..", ($have_setlocale ? 114 : 98), "\n";
37
38 use vars qw(&LC_ALL);
39
40 my $a = 'abc %';
41
42 sub ok {
43     my ($n, $result) = @_;
44
45     print 'not ' unless ($result);
46     print "ok $n\n";
47 }
48
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'.
52
53 sub is_tainted { # hello, camel two.
54     local $^W;  # no warnings 'undef'
55     my $dummy;
56     not eval { $dummy = join("", @_), kill 0; 1 }
57 }
58
59 sub check_taint ($$) {
60     ok $_[0], is_tainted($_[1]);
61 }
62
63 sub check_taint_not ($$) {
64     ok $_[0], not is_tainted($_[1]);
65 }
66
67 use locale;     # engage locale and therefore locale taint.
68
69 check_taint_not   1, $a;
70
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";
79
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);
85
86 $_ = $a;        # untaint $_
87
88 $_ = uc($a);    # taint $_
89
90 check_taint      15, $_;
91
92 /(\w)/; # taint $&, $`, $', $+, $1.
93 check_taint      16, $&;
94 check_taint      17, $`;
95 check_taint      18, $';
96 check_taint      19, $+;
97 check_taint      20, $1;
98 check_taint_not  21, $2;
99
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;
107
108 /(\W)/; # taint $&, $`, $', $+, $1.
109 check_taint      28, $&;
110 check_taint      29, $`;
111 check_taint      30, $';
112 check_taint      31, $+;
113 check_taint      32, $1;
114 check_taint_not  33, $2;
115
116 /(\s)/; # taint $&, $`, $', $+, $1.
117 check_taint      34, $&;
118 check_taint      35, $`;
119 check_taint      36, $';
120 check_taint      37, $+;
121 check_taint      38, $1;
122 check_taint_not  39, $2;
123
124 /(\S)/; # taint $&, $`, $', $+, $1.
125 check_taint      40, $&;
126 check_taint      41, $`;
127 check_taint      42, $';
128 check_taint      43, $+;
129 check_taint      44, $1;
130 check_taint_not  45, $2;
131
132 $_ = $a;        # untaint $_
133
134 check_taint_not  46, $_;
135
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;
143
144 $_ = $a;        # untaint $_
145
146 check_taint_not  53, $_;
147
148 $b = uc($a);    # taint $b
149 s/(.+)/$b/;     # this must taint only the $_
150
151 check_taint      54, $_;
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;
158
159 $_ = $a;        # untaint $_
160
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;
169
170 $b = $a;        # untaint $b
171
172 ($b = $a) =~ s/\w/$&/;
173 check_taint      68, $b;        # $b should be tainted.
174 check_taint_not  69, $a;        # $a should be not.
175
176 $_ = $a;        # untaint $_
177
178 s/(\w)/\l$1/;   # this must taint
179 check_taint      70, $_;
180 check_taint      71, $&;
181 check_taint      72, $`;
182 check_taint      73, $';
183 check_taint      74, $+;
184 check_taint      75, $1;
185 check_taint_not  76, $2;
186
187 $_ = $a;        # untaint $_
188
189 s/(\w)/\L$1/;   # this must taint
190 check_taint      77, $_;
191 check_taint      78, $&;
192 check_taint      79, $`;
193 check_taint      80, $';
194 check_taint      81, $+;
195 check_taint      82, $1;
196 check_taint_not  83, $2;
197
198 $_ = $a;        # untaint $_
199
200 s/(\w)/\u$1/;   # this must taint
201 check_taint      84, $_;
202 check_taint      85, $&;
203 check_taint      86, $`;
204 check_taint      87, $';
205 check_taint      88, $+;
206 check_taint      89, $1;
207 check_taint_not  90, $2;
208
209 $_ = $a;        # untaint $_
210
211 s/(\w)/\U$1/;   # this must taint
212 check_taint      91, $_;
213 check_taint      92, $&;
214 check_taint      93, $`;
215 check_taint      94, $';
216 check_taint      95, $+;
217 check_taint      96, $1;
218 check_taint_not  97, $2;
219
220 # After all this tainting $a should be cool.
221
222 check_taint_not  98, $a;
223
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).
227
228 exit unless $have_setlocale;
229
230 # Find locales.
231
232 debug "# Scanning for locales...\n";
233
234 # Note that it's okay that some languages have their native names
235 # capitalized here even though that's not "right".  They are lowercased
236 # anyway later during the scanning process (and besides, some clueless
237 # vendor might have them capitalized errorneously anyway).
238
239 my $locales = <<EOF;
240 Afrikaans:af:za:1 15
241 Arabic:ar:dz eg sa:6 arabic8
242 Brezhoneg Breton:br:fr:1 15
243 Bulgarski Bulgarian:bg:bg:5
244 Català Catalan:ca:es:1 15
245 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW GB2312 tw.EUC
246 Hrvatski Croatian:hr:hr:2
247 Cymraeg Welsh:cy:cy:1 14 15
248 Czech:cs:cz:2
249 Dansk Danish:dk:da:1 15
250 Nederlands Dutch:nl:be nl:1 15
251 English American British:en:au ca gb ie nz us uk:1 15 cp850
252 Esperanto:eo:eo:3
253 Eesti Estonian:et:ee:4 6 13
254 Suomi Finnish:fi:fi:1 15
255 Flamish::fl:1 15
256 français French:fr:be ca ch fr lu:1 15
257 Deutsch German:de:at be ch de lu:1 15
258 Euskaraz Basque:eu:es fr:1 15
259 Gáidhlig Gaelic:gd:gb uk:1 14 15
260 Galego Galician:gl:es:1 15
261 Ellada Greek:el:gr:7 g8
262 Føroyskt Faroese:fo:fo:1 15
263 Frysk:fy:nl:1 15
264 Greenlandic:kl:gl:4 6
265 Hebrew:iw:il:8 hebrew8
266 Hungarian:hu:hu:2
267 Íslensku Icelandic:is:is:1 15
268 Indonesian:in:id:1 15
269 Gaeilge Irish:ga:IE:1 14 15
270 Italiano Italian:it:ch it:1 15
271 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
272 Korean:ko:kr:
273 Sámi Lappish:::4 6 13
274 Latine Latin:la:va:1 15
275 Latvian:lv:lv:4 6 13
276 Lithuanian:lt:lt:4 6 13
277 Macedonian:mk:mk:1 15
278 Maltese:mt:mt:3
279 norsk Norwegian:no:no:1
280 Occitan:oc:es:1 15
281 Polski Polish:pl:pl:2
282 Português Portuguese:po:po br:1 15
283 Rumanian:ro:ro:2
284 Russki Russian:ru:ru su ua:5 koi8 koi8r koi8u cp1251
285 Serbski Serbian:sr:yu:5
286 Slovak:sk:sk:2
287 Slovene Slovenian:sl:si:2
288 Espanõl Spanish:es:ar bo cl co cr do ec es gt hn mx ni pa pe py sv uy ve:1 15
289 Sqhip:sq:sq:1 15
290 Swedish:sv:fi se:1 15
291 Thai:th:th:11 tis620
292 Turkish:tr:tr:9 turkish8
293 Yiddish:::1 15
294 EOF
295
296 my @Locale;
297 my $Locale;
298 my @Alnum_;
299
300 sub getalnum_ {
301     sort grep /\w/, map { chr } 0..255
302 }
303
304 sub trylocale {
305     my $locale = shift;
306     if (setlocale(LC_ALL, $locale)) {
307         push @Locale, $locale;
308     }
309 }
310
311 sub decode_encodings {
312     my @enc;
313
314     foreach (split(/ /, shift)) {
315         if (/^(\d+)$/) {
316             push @enc, "ISO8859-$1";
317             push @enc, "iso8859$1";     # HP
318             if ($1 eq '1') {
319                  push @enc, "roman8";   # HP
320             }
321         } else {
322             push @enc, $_;
323         }
324     }
325
326     return @enc;
327 }
328
329 trylocale("C");
330 trylocale("POSIX");
331 foreach (0..15) {
332     trylocale("ISO8859-$_");
333     trylocale("iso8859$_");
334     trylocale("iso8859-$_");
335     trylocale("iso_8859_$_");
336     trylocale("isolatin$_");
337     trylocale("isolatin-$_");
338     trylocale("iso_latin_$_");
339 }
340
341 foreach my $locale (split(/\n/, $locales)) {
342     my ($locale_name, $language_codes, $country_codes, $encodings) =
343         split(/:/, $locale);
344     my @enc = decode_encodings($encodings);
345     foreach my $loc (split(/ /, $locale_name)) {
346         trylocale($loc);
347         foreach my $enc (@enc) {
348             trylocale("$loc.$enc");
349         }
350         $loc = lc $loc;
351         foreach my $enc (@enc) {
352             trylocale("$loc.$enc");
353         }
354     }
355     foreach my $lang (split(/ /, $language_codes)) {
356         trylocale($lang);
357         foreach my $country (split(/ /, $country_codes)) {
358             my $lc = "${lang}_${country}";
359             trylocale($lc);
360             foreach my $enc (@enc) {
361                 trylocale("$lc.$enc");
362             }
363             my $lC = "${lang}_\U${country}";
364             trylocale($lC);
365             foreach my $enc (@enc) {
366                 trylocale("$lC.$enc");
367             }
368         }
369     }
370 }
371
372 @Locale = sort @Locale;
373
374 debug "# Locales = @Locale\n";
375
376 my %Problem;
377 my @Neoalpha;
378
379 foreach $Locale (@Locale) {
380     debug "# Locale = $Locale\n";
381     @Alnum_ = getalnum_();
382     debug "# \\w = @Alnum_\n";
383
384     unless (setlocale(LC_ALL, $Locale)) {
385         foreach (99..103) {
386             $Problem{$_}{$Locale} = -1;
387         }
388         next;
389     }
390
391     # Sieve the uppercase and the lowercase.
392     
393     my %UPPER = ();
394     my %lower = ();
395     my %BoThCaSe = ();
396     for (@Alnum_) {
397         if (/[^\d_]/) { # skip digits and the _
398             if (uc($_) eq $_) {
399                 $UPPER{$_} = $_;
400             }
401             if (lc($_) eq $_) {
402                 $lower{$_} = $_;
403             }
404         }
405     }
406     foreach (keys %UPPER) {
407         $BoThCaSe{$_}++ if exists $lower{$_};
408     }
409     foreach (keys %lower) {
410         $BoThCaSe{$_}++ if exists $UPPER{$_};
411     }
412     foreach (keys %BoThCaSe) {
413         delete $UPPER{$_};
414         delete $lower{$_};
415     }
416
417     debug "# UPPER    = ", join(" ", sort keys %UPPER   ), "\n";
418     debug "# lower    = ", join(" ", sort keys %lower   ), "\n";
419     debug "# BoThCaSe = ", join(" ", sort keys %BoThCaSe), "\n";
420
421     # Find the alphabets that are not alphabets in the default locale.
422
423     {
424         no locale;
425     
426         @Neoalpha = ();
427         for (keys %UPPER, keys %lower) {
428             push(@Neoalpha, $_) if (/\W/);
429         }
430     }
431
432     @Neoalpha = sort @Neoalpha;
433
434     debug "# Neoalpha = @Neoalpha\n";
435
436     if (@Neoalpha == 0) {
437         # If we have no Neoalphas the remaining tests are no-ops.
438         debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
439     } else {
440
441         # Test \w.
442     
443         debug "# testing 99 with locale '$Locale'\n";
444         {
445             my $word = join('', @Neoalpha);
446
447             $word =~ /^(\w+)$/;
448
449             if ($1 ne $word) {
450                 $Problem{99}{$Locale} = 1;
451                 debug "# failed 99 ($1 vs $word)\n";
452             }
453         }
454
455         # Cross-check whole character set.
456
457         debug "# testing 100 with locale '$Locale'\n";
458         for (map { chr } 0..255) {
459             if ((/\w/ and /\W/) or (/\d/ and /\D/) or (/\s/ and /\S/)) {
460                 $Problem{100}{$Locale} = 1;
461                 debug "# failed 100 for chr(", ord(), ")\n";
462             }
463         }
464
465         # Test for read-only scalars' locale vs non-locale comparisons.
466
467         debug "# testing 101 with locale '$Locale'\n";
468         {
469             no locale;
470             $a = "qwerty";
471             {
472                 use locale;
473                 if ($a cmp "qwerty") {
474                     $Problem{101}{$Locale} = 1;
475                     debug "# failed 101\n";
476                 }
477             }
478         }
479
480         debug "# testing 102 with locale '$Locale'\n";
481         {
482             my ($from, $to, $lesser, $greater,
483                 @test, %test, $test, $yes, $no, $sign);
484
485             for (0..9) {
486                 # Select a slice.
487                 $from = int(($_*@Alnum_)/10);
488                 $to = $from + int(@Alnum_/10);
489                 $to = $#Alnum_ if ($to > $#Alnum_);
490                 $lesser  = join('', @Alnum_[$from..$to]);
491                 # Select a slice one character on.
492                 $from++; $to++;
493                 $to = $#Alnum_ if ($to > $#Alnum_);
494                 $greater = join('', @Alnum_[$from..$to]);
495                 ($yes, $no, $sign) = ($lesser lt $greater
496                                       ? ("    ", "not ", 1)
497                                       : ("not ", "    ", -1));
498                 # all these tests should FAIL (return 0).
499                 # Exact lt or gt cannot be tested because
500                 # in some locales, say, eacute and E may test equal.
501                 @test = 
502                     (
503                      $no.'    ($lesser  le $greater)',  # 1
504                      'not      ($lesser  ne $greater)', # 2
505                      '         ($lesser  eq $greater)', # 3
506                      $yes.'    ($lesser  ge $greater)', # 4
507                      $yes.'    ($lesser  ge $greater)', # 5
508                      $yes.'    ($greater le $lesser )', # 7
509                      'not      ($greater ne $lesser )', # 8
510                      '         ($greater eq $lesser )', # 9
511                      $no.'     ($greater ge $lesser )', # 10
512                      'not (($lesser cmp $greater) == -$sign)' # 12
513                      );
514                 @test{@test} = 0 x @test;
515                 $test = 0;
516                 for my $ti (@test) {
517                     $test{$ti} = eval $ti;
518                     $test ||= $test{$ti}
519                 }
520                 if ($test) {
521                     $Problem{102}{$Locale} = 1;
522                     debug "# failed 102 at:\n";
523                     debug "# lesser  = '$lesser'\n";
524                     debug "# greater = '$greater'\n";
525                     debug "# lesser cmp greater = ",
526                           $lesser cmp $greater, "\n";
527                     debug "# greater cmp lesser = ",
528                           $greater cmp $lesser, "\n";
529                     debug "# (greater) from = $from, to = $to\n";
530                     for my $ti (@test) {
531                         debugf("# %-40s %-4s", $ti,
532                                $test{$ti} ? 'FAIL' : 'ok');
533                         if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
534                             debugf("(%s == %4d)", $1, eval $1);
535                         }
536                         debug "\n#";
537                     }
538
539                     last;
540                 }
541             }
542         }
543     }
544
545     use locale;
546
547     my ($x, $y) = (1.23, 1.23);
548
549     my $a = "$x";
550     printf ''; # printf used to reset locale to "C"
551     my $b = "$y";
552
553     debug "# testing 103 with locale '$Locale'\n";
554     unless ($a eq $b) {
555         $Problem{103}{$Locale} = 1;
556         debug "# failed 103\n";
557     }
558
559     my $c = "$x";
560     my $z = sprintf ''; # sprintf used to reset locale to "C"
561     my $d = "$y";
562
563     debug "# 103..107: a = $a, b = $b, c = $c, d = $d, Locale = $Locale\n";
564
565     debug "# testing 104 with locale '$Locale'\n";
566     unless ($c eq $d) {
567         $Problem{104}{$Locale} = 1;
568         debug "# failed 104\n";
569     }
570
571     my $w = 0;
572     local $SIG{__WARN__} = sub { $w++ };
573     local $^W = 1;
574
575     # the == (among other things) used to warn for locales
576     # that had something else than "." as the radix character
577
578     debug "# testing 105 with locale '$Locale'\n";
579     unless ($c == 1.23) {
580         $Problem{105}{$Locale} = 1;
581         debug "# failed 105\n";
582     }
583
584     debug "# testing 106 with locale '$Locale'\n";
585     unless ($c == $x) {
586         $Problem{106}{$Locale} = 1;
587         debug "# failed 106\n";
588     }
589
590     debug "# testing 107 with locale '$Locale'\n";
591     unless ($c == $d) {
592         $Problem{107}{$Locale} = 1;
593         debug "# failed 107\n";
594     }
595
596     {
597         no locale;
598         
599         my $e = "$x";
600
601         debug "# 108..110: e = $e, Locale = $Locale\n";
602
603         debug "# testing 108 with locale '$Locale'\n";
604         unless ($e == 1.23) {
605             $Problem{108}{$Locale} = 1;
606             debug "# failed 108\n";
607         }
608
609         debug "# testing 109 with locale '$Locale'\n";
610         unless ($e == $x) {
611             $Problem{109}{$Locale} = 1;
612             debug "# failed 109\n";
613         }
614
615         debug "# testing 110 with locale '$Locale'\n";
616         unless ($e == $c) {
617             $Problem{110}{$Locale} = 1;
618             debug "# failed 110\n";
619         }
620     }
621
622     debug "# testing 111 with locale '$Locale'\n";
623     unless ($w == 0) {
624         $Problem{110}{$Locale} = 1;
625         debug "# failed 111\n";
626     }
627
628     my $f = "1.23";
629
630     debug "# 112..114: f = $f, locale = $Locale\n";
631
632     debug "# testing 112 with locale '$Locale'\n";
633     unless ($f == 1.23) {
634         $Problem{112}{$Locale} = 1;
635         debug "# failed 112\n";
636     }
637
638     debug "# testing 113 with locale '$Locale'\n";
639     unless ($f == $x) {
640         $Problem{113}{$Locale} = 1;
641         debug "# failed 113\n";
642     }
643
644     debug "# testing 114 with locale '$Locale'\n";
645     unless ($f == $c) {
646         $Problem{114}{$Locale} = 1;
647         debug "# failed 114\n";
648     }
649 }
650
651 foreach (99..114) {
652     if ($Problem{$_}) {
653         if ($_ == 102) {
654             print "# The failure of test 102 is not necessarily fatal.\n";
655             print "# It usually indicates a problem in the enviroment,\n";
656             print "# not in Perl itself.\n";
657         }
658         print "not ";
659     }
660     print "ok $_\n";
661 }
662
663 my $didwarn = 0;
664
665 foreach (99..114) {
666     if ($Problem{$_}) {
667         my @f = sort keys %{ $Problem{$_} };
668         my $f = join(" ", @f);
669         $f =~ s/(.{50,60}) /$1\n#\t/g;
670         warn
671             "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
672             "#\t", $f, "\n#\n",
673             "# on your system may have errors because the locale test $_\n",
674             "# failed in ", (@f == 1 ? "that locale" : "those locales"),
675             ".\n";
676         warn <<EOW;
677 #
678 # If your users are not using these locales you are safe for the moment,
679 # but please report this failure first to perlbug\@perl.com using the
680 # perlbug script (as described in the INSTALL file) so that the exact
681 # details of the failures can be sorted out first and then your operating
682 # system supplier can be alerted about these anomalies.
683 #
684 EOW
685         $didwarn = 1;
686     }
687 }
688
689 if ($didwarn) {
690     my @s;
691     
692     foreach my $l (@Locale) {
693         my $p = 0;
694         foreach my $t (102..102) {
695             $p++ if $Problem{$t}{$l};
696         }
697         push @s, $l if $p == 0;
698     }
699     
700     my $s = join(" ", @s);
701     $s =~ s/(.{50,60}) /$1\n#\t/g;
702
703     warn
704         "# The following locales\n#\n",
705         "#\t", $s, "\n#\n",
706         "# tested okay.\n#\n",
707 }
708
709 {
710     use locale;
711
712 }
713
714 # eof