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