This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.t: Don't bother testing locales with malformed names
[perl5.git] / lib / locale.t
CommitLineData
8ebc5c01
PP
1#!./perl -wT
2
e3a2734b
KW
3binmode STDOUT, ':utf8';
4binmode STDERR, ':utf8';
5
8ebc5c01
PP
6BEGIN {
7 chdir 't' if -d 't';
20822f61 8 @INC = '../lib';
f9cbebe1 9 unshift @INC, '.';
b002077a 10 require Config; import Config;
97a0514d 11 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
b002077a
CS
12 print "1..0\n";
13 exit;
14 }
2de3dbcc 15 $| = 1;
8ebc5c01
PP
16}
17
18use strict;
19
284102e8
JH
20my $debug = 1;
21
db4b7445
A
22use Dumpvalue;
23
24my $dumper = Dumpvalue->new(
25 tick => qq{"},
26 quoteHighBit => 0,
27 unctrl => "quote"
28 );
6be75cd7 29sub debug {
db4b7445
A
30 return unless $debug;
31 my($mess) = join "", @_;
32 chop $mess;
33 print $dumper->stringify($mess,1), "\n";
6be75cd7
JH
34}
35
36sub debugf {
37 printf @_ if $debug;
38}
39
8ebc5c01
PP
40my $have_setlocale = 0;
41eval {
42 require POSIX;
43 import POSIX ':locale_h';
44 $have_setlocale++;
45};
46
6dead956 47# Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
f6c6487a 48# and mingw32 uses said silly CRT
3a2d1764
SH
49# This doesn't seem to be an issue any more, at least on Windows XP,
50# so re-enable the tests for Windows XP onwards.
51my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
52 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
53$have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
54 $Config{cc} =~ /^(cl|gcc)/i);
6dead956 55
36a42ae7 56# UWIN seems to loop after taint tests, just skip for now
cd19b65c
JH
57$have_setlocale = 0 if ($^O =~ /^uwin/);
58
9a66ea41 59sub LC_ALL ();
8ebc5c01 60
0e053d1e 61$a = 'abc %';
8ebc5c01 62
c213d471
KW
63my $test_num = 0;
64
8ebc5c01 65sub ok {
c213d471 66 my ($result, $message) = @_;
e3a2734b 67 $message = "" unless defined $message;
8ebc5c01
PP
68
69 print 'not ' unless ($result);
c213d471 70 print "ok " . ++$test_num;
e3a2734b
KW
71 print " $message";
72 print "\n";
8ebc5c01
PP
73}
74
75# First we'll do a lot of taint checking for locales.
76# This is the easiest to test, actually, as any locale,
77# even the default locale will taint under 'use locale'.
78
79sub is_tainted { # hello, camel two.
9f1b1f2d 80 no warnings 'uninitialized' ;
8ebc5c01 81 my $dummy;
ba74571d 82 local $@;
8ebc5c01
PP
83 not eval { $dummy = join("", @_), kill 0; 1 }
84}
85
36a42ae7
KW
86sub check_taint ($) {
87 ok is_tainted($_[0]), "verify that is tainted";
8ebc5c01
PP
88}
89
36a42ae7
KW
90sub check_taint_not ($) {
91 ok((not is_tainted($_[0])), "verify that isn't tainted");
8ebc5c01
PP
92}
93
94use locale; # engage locale and therefore locale taint.
95
36a42ae7 96check_taint_not $a;
8ebc5c01 97
36a42ae7
KW
98check_taint uc($a);
99check_taint "\U$a";
100check_taint ucfirst($a);
101check_taint "\u$a";
102check_taint lc($a);
103check_taint "\L$a";
104check_taint lcfirst($a);
105check_taint "\l$a";
8ebc5c01 106
36a42ae7
KW
107check_taint_not sprintf('%e', 123.456);
108check_taint_not sprintf('%f', 123.456);
109check_taint_not sprintf('%g', 123.456);
110check_taint_not sprintf('%d', 123.456);
111check_taint_not sprintf('%x', 123.456);
8ebc5c01
PP
112
113$_ = $a; # untaint $_
114
115$_ = uc($a); # taint $_
116
36a42ae7 117check_taint $_;
8ebc5c01
PP
118
119/(\w)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
120check_taint $&;
121check_taint $`;
122check_taint $';
123check_taint $+;
124check_taint $1;
125check_taint_not $2;
8ebc5c01
PP
126
127/(.)/; # untaint $&, $`, $', $+, $1.
36a42ae7
KW
128check_taint_not $&;
129check_taint_not $`;
130check_taint_not $';
131check_taint_not $+;
132check_taint_not $1;
133check_taint_not $2;
8ebc5c01
PP
134
135/(\W)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
136check_taint $&;
137check_taint $`;
138check_taint $';
139check_taint $+;
140check_taint $1;
141check_taint_not $2;
8ebc5c01
PP
142
143/(\s)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
144check_taint $&;
145check_taint $`;
146check_taint $';
147check_taint $+;
148check_taint $1;
149check_taint_not $2;
8ebc5c01
PP
150
151/(\S)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
152check_taint $&;
153check_taint $`;
154check_taint $';
155check_taint $+;
156check_taint $1;
157check_taint_not $2;
8ebc5c01
PP
158
159$_ = $a; # untaint $_
160
36a42ae7 161check_taint_not $_;
8ebc5c01
PP
162
163/(b)/; # this must not taint
36a42ae7
KW
164check_taint_not $&;
165check_taint_not $`;
166check_taint_not $';
167check_taint_not $+;
168check_taint_not $1;
169check_taint_not $2;
8ebc5c01
PP
170
171$_ = $a; # untaint $_
172
36a42ae7 173check_taint_not $_;
8ebc5c01
PP
174
175$b = uc($a); # taint $b
176s/(.+)/$b/; # this must taint only the $_
177
36a42ae7
KW
178check_taint $_;
179check_taint_not $&;
180check_taint_not $`;
181check_taint_not $';
182check_taint_not $+;
183check_taint_not $1;
184check_taint_not $2;
8ebc5c01
PP
185
186$_ = $a; # untaint $_
187
188s/(.+)/b/; # this must not taint
36a42ae7
KW
189check_taint_not $_;
190check_taint_not $&;
191check_taint_not $`;
192check_taint_not $';
193check_taint_not $+;
194check_taint_not $1;
195check_taint_not $2;
8ebc5c01
PP
196
197$b = $a; # untaint $b
198
199($b = $a) =~ s/\w/$&/;
36a42ae7
KW
200check_taint $b; # $b should be tainted.
201check_taint_not $a; # $a should be not.
8ebc5c01
PP
202
203$_ = $a; # untaint $_
204
205s/(\w)/\l$1/; # this must taint
36a42ae7
KW
206check_taint $_;
207check_taint $&;
208check_taint $`;
209check_taint $';
210check_taint $+;
211check_taint $1;
212check_taint_not $2;
8ebc5c01
PP
213
214$_ = $a; # untaint $_
215
216s/(\w)/\L$1/; # this must taint
36a42ae7
KW
217check_taint $_;
218check_taint $&;
219check_taint $`;
220check_taint $';
221check_taint $+;
222check_taint $1;
223check_taint_not $2;
8ebc5c01
PP
224
225$_ = $a; # untaint $_
226
227s/(\w)/\u$1/; # this must taint
36a42ae7
KW
228check_taint $_;
229check_taint $&;
230check_taint $`;
231check_taint $';
232check_taint $+;
233check_taint $1;
234check_taint_not $2;
8ebc5c01
PP
235
236$_ = $a; # untaint $_
237
238s/(\w)/\U$1/; # this must taint
36a42ae7
KW
239check_taint $_;
240check_taint $&;
241check_taint $`;
242check_taint $';
243check_taint $+;
244check_taint $1;
245check_taint_not $2;
8ebc5c01
PP
246
247# After all this tainting $a should be cool.
248
36a42ae7 249check_taint_not $a;
8ebc5c01
PP
250
251# I think we've seen quite enough of taint.
252# Let us do some *real* locale work now,
284102e8 253# unless setlocale() is missing (i.e. minitest).
8ebc5c01 254
fdf053ee
KW
255unless ($have_setlocale) {
256 print "1..$test_num\n";
257 exit;
258}
8ebc5c01 259
6cf0b567 260# The test number before our first setlocale()
66330f13 261my $final_without_setlocale = $test_num;
6cf0b567 262
284102e8
JH
263# Find locales.
264
6be75cd7
JH
265debug "# Scanning for locales...\n";
266
267# Note that it's okay that some languages have their native names
268# capitalized here even though that's not "right". They are lowercased
269# anyway later during the scanning process (and besides, some clueless
98dc9551 270# vendor might have them capitalized erroneously anyway).
6be75cd7 271
284102e8 272my $locales = <<EOF;
6be75cd7 273Afrikaans:af:za:1 15
284102e8 274Arabic:ar:dz eg sa:6 arabic8
6be75cd7
JH
275Brezhoneg Breton:br:fr:1 15
276Bulgarski Bulgarian:bg:bg:5
dd8482fc 277Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
6be75cd7
JH
278Hrvatski Croatian:hr:hr:2
279Cymraeg Welsh:cy:cy:1 14 15
284102e8 280Czech:cs:cz:2
6be75cd7
JH
281Dansk Danish:dk:da:1 15
282Nederlands Dutch:nl:be nl:1 15
dd8482fc 283English American British:en:au ca gb ie nz us uk zw:1 15 cp850
6be75cd7
JH
284Esperanto:eo:eo:3
285Eesti Estonian:et:ee:4 6 13
286Suomi Finnish:fi:fi:1 15
287Flamish::fl:1 15
6be75cd7
JH
288Deutsch German:de:at be ch de lu:1 15
289Euskaraz Basque:eu:es fr:1 15
6be75cd7
JH
290Galego Galician:gl:es:1 15
291Ellada Greek:el:gr:7 g8
6be75cd7
JH
292Frysk:fy:nl:1 15
293Greenlandic:kl:gl:4 6
284102e8
JH
294Hebrew:iw:il:8 hebrew8
295Hungarian:hu:hu:2
6be75cd7
JH
296Indonesian:in:id:1 15
297Gaeilge Irish:ga:IE:1 14 15
298Italiano Italian:it:ch it:1 15
299Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
284102e8 300Korean:ko:kr:
6be75cd7
JH
301Latine Latin:la:va:1 15
302Latvian:lv:lv:4 6 13
303Lithuanian:lt:lt:4 6 13
304Macedonian:mk:mk:1 15
305Maltese:mt:mt:3
dd8482fc
JH
306Moldovan:mo:mo:2
307Norsk Norwegian:no no\@nynorsk:no:1 15
6be75cd7
JH
308Occitan:oc:es:1 15
309Polski Polish:pl:pl:2
284102e8 310Rumanian:ro:ro:2
a528dad0 311Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
6be75cd7 312Serbski Serbian:sr:yu:5
284102e8 313Slovak:sk:sk:2
6be75cd7 314Slovene Slovenian:sl:si:2
d43ce814
JH
315Sqhip Albanian:sq:sq:1 15
316Svenska Swedish:sv:fi se:1 15
6be75cd7 317Thai:th:th:11 tis620
284102e8 318Turkish:tr:tr:9 turkish8
dd8482fc 319Yiddish:yi::1 15
284102e8
JH
320EOF
321
ee50adbe 322if ($^O eq 'os390') {
dd8482fc 323 # These cause heartburn. Broken locales?
ee50adbe
PP
324 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
325 $locales =~ s/Thai:th:th:11 tis620\n//;
326}
327
ef4a39e5 328sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
f9cbebe1
JH
329
330if (in_utf8) {
8a6cb2cb 331 require "lib/locale/utf8";
f9cbebe1 332} else {
8a6cb2cb 333 require "lib/locale/latin1";
f9cbebe1
JH
334}
335
284102e8
JH
336my @Locale;
337my $Locale;
338my @Alnum_;
339
284102e8
JH
340sub trylocale {
341 my $locale = shift;
0b9f254b 342 return if grep { $locale eq $_ } @Locale;
e439cacb
KW
343 return unless setlocale(LC_ALL, $locale);
344 my $badutf8;
345 {
346 local $SIG{__WARN__} = sub {
347 $badutf8 = $_[0] =~ /Malformed UTF-8/;
348 };
349 $Locale =~ /UTF-?8/i;
284102e8 350 }
e439cacb
KW
351
352 if ($badutf8) {
353 ok(0, "Locale name contains malformed utf8");
354 return;
355 }
356 push @Locale, $locale;
284102e8 357}
8ebc5c01 358
284102e8
JH
359sub decode_encodings {
360 my @enc;
8ebc5c01 361
284102e8
JH
362 foreach (split(/ /, shift)) {
363 if (/^(\d+)$/) {
364 push @enc, "ISO8859-$1";
365 push @enc, "iso8859$1"; # HP
366 if ($1 eq '1') {
367 push @enc, "roman8"; # HP
368 }
369 } else {
370 push @enc, $_;
dd8482fc 371 push @enc, "$_.UTF-8";
8ebc5c01
PP
372 }
373 }
ee50adbe
PP
374 if ($^O eq 'os390') {
375 push @enc, qw(IBM-037 IBM-819 IBM-1047);
376 }
8ebc5c01 377
284102e8 378 return @enc;
8ebc5c01
PP
379}
380
284102e8
JH
381trylocale("C");
382trylocale("POSIX");
383foreach (0..15) {
384 trylocale("ISO8859-$_");
284102e8 385 trylocale("iso8859$_");
097ee67d
JH
386 trylocale("iso8859-$_");
387 trylocale("iso_8859_$_");
388 trylocale("isolatin$_");
389 trylocale("isolatin-$_");
390 trylocale("iso_latin_$_");
8ebc5c01
PP
391}
392
645e49ed
JH
393# Sanitize the environment so that we can run the external 'locale'
394# program without the taint mode getting grumpy.
cce5967e
JH
395
396# $ENV{PATH} is special in VMS.
397delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
398
399# Other subversive stuff.
400delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
dd8482fc 401
21477fb4 402if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
dd8482fc 403 while (<LOCALES>) {
d281a6ac
NC
404 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
405 # ain't great when we're running this testPERL_UNICODE= so that utf8
406 # locales will cause all IO hadles to default to (assume) utf8
407 next unless utf8::valid($_);
dd8482fc
JH
408 chomp;
409 trylocale($_);
284102e8 410 }
dd8482fc 411 close(LOCALES);
a6259068
PP
412} elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
413# The SYS$I18N_LOCALE logical name search list was not present on
414# VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
415 opendir(LOCALES, "SYS\$I18N_LOCALE:");
416 while ($_ = readdir(LOCALES)) {
417 chomp;
418 trylocale($_);
419 }
420 close(LOCALES);
87e33296
SP
421} elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
422
423 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
424 # is much easier and faster than the last resort method.
425
426 opendir(LOCALES, '/usr/share/locale');
427 while ($_ = readdir(LOCALES)) {
428 chomp;
429 trylocale($_);
430 }
431 close(LOCALES);
dd8482fc
JH
432} else {
433
434 # This is going to be slow.
435
436 foreach my $locale (split(/\n/, $locales)) {
437 my ($locale_name, $language_codes, $country_codes, $encodings) =
438 split(/:/, $locale);
439 my @enc = decode_encodings($encodings);
440 foreach my $loc (split(/ /, $locale_name)) {
441 trylocale($loc);
284102e8 442 foreach my $enc (@enc) {
dd8482fc 443 trylocale("$loc.$enc");
284102e8 444 }
dd8482fc 445 $loc = lc $loc;
284102e8 446 foreach my $enc (@enc) {
dd8482fc
JH
447 trylocale("$loc.$enc");
448 }
449 }
450 foreach my $lang (split(/ /, $language_codes)) {
451 trylocale($lang);
452 foreach my $country (split(/ /, $country_codes)) {
453 my $lc = "${lang}_${country}";
454 trylocale($lc);
455 foreach my $enc (@enc) {
456 trylocale("$lc.$enc");
457 }
458 my $lC = "${lang}_\U${country}";
459 trylocale($lC);
460 foreach my $enc (@enc) {
461 trylocale("$lC.$enc");
462 }
284102e8
JH
463 }
464 }
465 }
466}
4599a1de 467
d43ce814
JH
468setlocale(LC_ALL, "C");
469
86f50d7d 470if ($^O eq 'darwin') {
4373e181 471 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
86f50d7d 472 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
4373e181
RGS
473 (my $v) = $Config{osvers} =~ /^(\d+)/;
474 if ($v >= 8 and $v < 10) {
86f50d7d 475 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
a5ec937f 476 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
dfa5c78f 477 } elsif ($v < 12) {
a44d0896
NC
478 debug "# Skipping be_BY locales -- buggy in Darwin\n";
479 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
a5ec937f 480 }
86f50d7d
DD
481}
482
4599a1de
JH
483@Locale = sort @Locale;
484
887ef7ed
PP
485debug "# Locales =\n";
486for ( @Locale ) {
487 debug "# $_\n";
488}
8ebc5c01 489
284102e8 490my %Problem;
2a680da6
JH
491my %Okay;
492my %Testing;
9445c837 493my @Neoalpha; # Alnums that aren't in the C locale.
c08acc4c 494my %test_names;
284102e8 495
2a680da6
JH
496sub tryneoalpha {
497 my ($Locale, $i, $test) = @_;
2a680da6
JH
498 unless ($test) {
499 $Problem{$i}{$Locale} = 1;
500 debug "# failed $i with locale '$Locale'\n";
501 } else {
502 push @{$Okay{$i}}, $Locale;
503 }
504}
505
c4093d7d
KW
506my $first_locales_test_number = $final_without_setlocale + 1;
507my $locales_test_number;
508my $not_necessarily_a_problem_test_number;
509my %setlocale_failed; # List of locales that setlocale() didn't work on
510
284102e8 511foreach $Locale (@Locale) {
c4093d7d 512 $locales_test_number = $first_locales_test_number - 1;
284102e8 513 debug "# Locale = $Locale\n";
284102e8
JH
514
515 unless (setlocale(LC_ALL, $Locale)) {
c4093d7d 516 $setlocale_failed{$Locale} = $Locale;
284102e8 517 next;
8ebc5c01 518 }
8ebc5c01 519
f07538a5
KW
520 @Alnum_ = sort grep /\w/, map { chr } 0..255;
521
e5272a46
KW
522 debug "# w = ", join("",@Alnum_), "\n";
523
284102e8
JH
524 # Sieve the uppercase and the lowercase.
525
097ee67d
JH
526 my %UPPER = ();
527 my %lower = ();
528 my %BoThCaSe = ();
284102e8
JH
529 for (@Alnum_) {
530 if (/[^\d_]/) { # skip digits and the _
531 if (uc($_) eq $_) {
532 $UPPER{$_} = $_;
533 }
534 if (lc($_) eq $_) {
535 $lower{$_} = $_;
536 }
537 }
538 }
539 foreach (keys %UPPER) {
097ee67d 540 $BoThCaSe{$_}++ if exists $lower{$_};
284102e8
JH
541 }
542 foreach (keys %lower) {
097ee67d 543 $BoThCaSe{$_}++ if exists $UPPER{$_};
284102e8 544 }
097ee67d 545 foreach (keys %BoThCaSe) {
284102e8
JH
546 delete $UPPER{$_};
547 delete $lower{$_};
548 }
549
db4b7445
A
550 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
551 debug "# lower = ", join("", sort keys %lower ), "\n";
552 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
284102e8 553
9445c837
KW
554 { # Find the alphabetic characters that are not considered alphabetics
555 # in the default (C) locale.
8ebc5c01 556
284102e8 557 no locale;
8ebc5c01 558
284102e8
JH
559 @Neoalpha = ();
560 for (keys %UPPER, keys %lower) {
561 push(@Neoalpha, $_) if (/\W/);
562 }
8ebc5c01 563 }
8ebc5c01 564
284102e8 565 @Neoalpha = sort @Neoalpha;
8ebc5c01 566
db4b7445 567 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
8ebc5c01 568
c4093d7d
KW
569 my $first_Neoalpha_test_number = $locales_test_number;
570 my $final_Neoalpha_test_number = $first_Neoalpha_test_number + 4;
284102e8
JH
571 if (@Neoalpha == 0) {
572 # If we have no Neoalphas the remaining tests are no-ops.
c4093d7d
KW
573 debug "# no Neoalpha, skipping tests $locales_test_number..$final_Neoalpha_test_number for locale '$Locale'\n";
574 foreach ($locales_test_number+1..$final_Neoalpha_test_number) {
a88c3d7c 575 push @{$Okay{$_}}, $Locale;
c4093d7d 576 $locales_test_number++;
a88c3d7c 577 }
6be75cd7 578 } else {
8ebc5c01 579
6be75cd7 580 # Test \w.
284102e8 581
ef4a39e5 582 my $word = join('', @Neoalpha);
8ebc5c01 583
c4093d7d 584 ++$locales_test_number;
c08acc4c 585 $test_names{$locales_test_number} = 'Verify that alnums outside the C locale match \w';
e439cacb 586 if ($Locale =~ /utf-?8/i) {
c4093d7d
KW
587 push @{$Okay{$locales_test_number}}, $Locale;
588 debug "# unknown whether locale and Unicode have the same \\w, skipping test $locales_test_number for locale '$Locale'\n";
ef4a39e5
JH
589 } else {
590 if ($word =~ /^(\w+)$/) {
c4093d7d 591 tryneoalpha($Locale, $locales_test_number, 1);
ef4a39e5 592 } else {
c4093d7d 593 tryneoalpha($Locale, $locales_test_number, 0);
ef4a39e5 594 }
8ac0d9e6 595 }
ef4a39e5 596
2a680da6 597 # Cross-check the whole 8-bit character set.
8ebc5c01 598
c4093d7d 599 ++$locales_test_number;
c08acc4c 600 $test_names{$locales_test_number} = 'Verify that \w and \W are mutually exclusive, as are \d, \D; \s, \S';
6be75cd7 601 for (map { chr } 0..255) {
c4093d7d 602 tryneoalpha($Locale, $locales_test_number,
2a680da6
JH
603 (/\w/ xor /\W/) ||
604 (/\d/ xor /\D/) ||
605 (/\s/ xor /\S/));
284102e8 606 }
8ebc5c01 607
6be75cd7 608 # Test for read-only scalars' locale vs non-locale comparisons.
284102e8 609
284102e8 610 {
6be75cd7
JH
611 no locale;
612 $a = "qwerty";
613 {
614 use locale;
c4093d7d 615 tryneoalpha($Locale, ++$locales_test_number, ($a cmp "qwerty") == 0);
c08acc4c 616 $test_names{$locales_test_number} = 'Verify that cmp works with a read-only scalar; no- vs locale';
8ebc5c01
PP
617 }
618 }
8ebc5c01 619
6be75cd7
JH
620 {
621 my ($from, $to, $lesser, $greater,
622 @test, %test, $test, $yes, $no, $sign);
623
c4093d7d 624 ++$locales_test_number;
c08acc4c 625 $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work';
c4093d7d 626 $not_necessarily_a_problem_test_number = $locales_test_number;
6be75cd7
JH
627 for (0..9) {
628 # Select a slice.
629 $from = int(($_*@Alnum_)/10);
630 $to = $from + int(@Alnum_/10);
631 $to = $#Alnum_ if ($to > $#Alnum_);
632 $lesser = join('', @Alnum_[$from..$to]);
633 # Select a slice one character on.
634 $from++; $to++;
635 $to = $#Alnum_ if ($to > $#Alnum_);
636 $greater = join('', @Alnum_[$from..$to]);
637 ($yes, $no, $sign) = ($lesser lt $greater
638 ? (" ", "not ", 1)
639 : ("not ", " ", -1));
640 # all these tests should FAIL (return 0).
641 # Exact lt or gt cannot be tested because
642 # in some locales, say, eacute and E may test equal.
643 @test =
644 (
645 $no.' ($lesser le $greater)', # 1
646 'not ($lesser ne $greater)', # 2
647 ' ($lesser eq $greater)', # 3
648 $yes.' ($lesser ge $greater)', # 4
649 $yes.' ($lesser ge $greater)', # 5
650 $yes.' ($greater le $lesser )', # 7
651 'not ($greater ne $lesser )', # 8
652 ' ($greater eq $lesser )', # 9
653 $no.' ($greater ge $lesser )', # 10
0e053d1e 654 'not (($lesser cmp $greater) == -($sign))' # 11
6be75cd7
JH
655 );
656 @test{@test} = 0 x @test;
657 $test = 0;
284102e8 658 for my $ti (@test) {
6be75cd7
JH
659 $test{$ti} = eval $ti;
660 $test ||= $test{$ti}
284102e8 661 }
c4093d7d 662 tryneoalpha($Locale, $locales_test_number, $test == 0);
6be75cd7 663 if ($test) {
6be75cd7
JH
664 debug "# lesser = '$lesser'\n";
665 debug "# greater = '$greater'\n";
666 debug "# lesser cmp greater = ",
667 $lesser cmp $greater, "\n";
668 debug "# greater cmp lesser = ",
669 $greater cmp $lesser, "\n";
670 debug "# (greater) from = $from, to = $to\n";
671 for my $ti (@test) {
672 debugf("# %-40s %-4s", $ti,
673 $test{$ti} ? 'FAIL' : 'ok');
674 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
675 debugf("(%s == %4d)", $1, eval $1);
676 }
677 debug "\n#";
678 }
284102e8 679
6be75cd7
JH
680 last;
681 }
284102e8 682 }
8ebc5c01
PP
683 }
684 }
6be75cd7 685
c4093d7d
KW
686 if ($locales_test_number != $final_Neoalpha_test_number) {
687 die("The delta for \$final_Neoalpha needs to be updated from "
688 . ($final_Neoalpha_test_number - $first_Neoalpha_test_number)
689 . " to "
690 . ($locales_test_number - $first_Neoalpha_test_number)
691 );
692 }
693
6be75cd7
JH
694 use locale;
695
696 my ($x, $y) = (1.23, 1.23);
697
0e053d1e 698 $a = "$x";
6be75cd7 699 printf ''; # printf used to reset locale to "C"
0e053d1e 700 $b = "$y";
6be75cd7 701
c4093d7d 702 tryneoalpha($Locale, ++$locales_test_number, $a eq $b);
c08acc4c 703 $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results';
c4093d7d 704 my $first_a_test = $locales_test_number;
2a680da6 705
c4093d7d 706 debug "# $first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n";
6be75cd7
JH
707
708 my $c = "$x";
709 my $z = sprintf ''; # sprintf used to reset locale to "C"
710 my $d = "$y";
711
6be75cd7 712
c4093d7d 713 tryneoalpha($Locale, ++$locales_test_number, $c eq $d);
c08acc4c 714 $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results';
c4093d7d 715 my $first_c_test = $locales_test_number;
6be75cd7 716
2a680da6 717 {
9f1b1f2d 718 use warnings;
2a680da6 719 my $w = 0;
0e053d1e
JH
720 local $SIG{__WARN__} =
721 sub {
906f284f 722 print "# @_\n";
0e053d1e
JH
723 $w++;
724 };
6be75cd7 725
0e053d1e
JH
726 # The == (among other ops) used to warn for locales
727 # that had something else than "." as the radix character.
6be75cd7 728
c4093d7d 729 tryneoalpha($Locale, ++$locales_test_number, $c == 1.23);
c08acc4c 730 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant';
6be75cd7 731
c4093d7d 732 tryneoalpha($Locale, ++$locales_test_number, $c == $x);
c08acc4c 733 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar';
6be75cd7 734
c4093d7d 735 tryneoalpha($Locale, ++$locales_test_number, $c == $d);
c08acc4c 736 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf';
c4093d7d
KW
737
738 debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n";
6be75cd7 739
2a680da6 740 {
8161897a 741 no locale;
6be75cd7 742
8161897a
KW
743 # The earlier test was $e = "$x". But this fails [perl #108378],
744 # and the "no locale" was commented out. But doing that made all
745 # the tests in the block after this one meaningless, as originally
746 # it was testing the nesting of a "no locale" scope, and how it
747 # recovers after that scope is done. So I (khw) filed a bug
748 # report and changed this so it wouldn't fail. It seemed too much
c08acc4c
KW
749 # work to add TODOs instead. Should this be fixed, the following
750 # test names would need to be revised; they mostly don't really
751 # test anything currently.
8161897a 752 my $e = $x;
6be75cd7 753
c4093d7d 754 tryneoalpha($Locale, ++$locales_test_number, $e == 1.23);
c08acc4c 755 $test_names{$locales_test_number} = 'Verify that can assign numerically under inner no-locale block';
c4093d7d 756 my $first_e_test = $locales_test_number;
6be75cd7 757
c4093d7d 758 tryneoalpha($Locale, ++$locales_test_number, $e == $x);
c08acc4c 759 $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale';
2a680da6 760
c4093d7d 761 tryneoalpha($Locale, ++$locales_test_number, $e == $c);
c08acc4c 762 $test_names{$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale';
c4093d7d
KW
763
764 debug "# $first_e_test..$locales_test_number: e = \$e, no locale\n";
6be75cd7 765 }
2a680da6 766
2a680da6 767 my $f = "1.23";
906f284f 768 my $g = 2.34;
2a680da6 769
c4093d7d 770 tryneoalpha($Locale, ++$locales_test_number, $f == 1.23);
c08acc4c 771 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant';
c4093d7d 772 my $first_f_test = $locales_test_number;
6be75cd7 773
c4093d7d 774 tryneoalpha($Locale, ++$locales_test_number, $f == $x);
c08acc4c 775 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar';
2a680da6 776
c4093d7d 777 tryneoalpha($Locale, ++$locales_test_number, $f == $c);
c08acc4c 778 $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';
906f284f 779
c4093d7d 780 tryneoalpha($Locale, ++$locales_test_number, abs(($f + $g) - 3.57) < 0.01);
c08acc4c 781 $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';
c4093d7d
KW
782
783 tryneoalpha($Locale, ++$locales_test_number, $w == 0);
c08acc4c 784 $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot';
c4093d7d
KW
785
786 debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n";
906f284f 787
6be75cd7
JH
788 }
789
26d80d95
LC
790 # Does taking lc separately differ from taking
791 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
792 # The bug was in the caching of the 'o'-magic.
2a680da6
JH
793 {
794 use locale;
6be75cd7 795
2a680da6
JH
796 sub lcA {
797 my $lc0 = lc $_[0];
798 my $lc1 = lc $_[1];
799 return $lc0 cmp $lc1;
800 }
6be75cd7 801
2a680da6
JH
802 sub lcB {
803 return lc($_[0]) cmp lc($_[1]);
804 }
6be75cd7 805
2a680da6
JH
806 my $x = "ab";
807 my $y = "aa";
808 my $z = "AB";
6be75cd7 809
c4093d7d 810 tryneoalpha($Locale, ++$locales_test_number,
2a680da6
JH
811 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
812 lcA($x, $z) == 0 && lcB($x, $z) == 0);
c08acc4c 813 $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp';
6be75cd7 814 }
d8093b23 815
26d80d95
LC
816 # Does lc of an UPPER (if different from the UPPER) match
817 # case-insensitively the UPPER, and does the UPPER match
818 # case-insensitively the lc of the UPPER. And vice versa.
3ba0e062 819 {
ef4a39e5
JH
820 use locale;
821 no utf8;
822 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
823
824 my @f = ();
c4093d7d 825 ++$locales_test_number;
c08acc4c 826 $test_names{$locales_test_number} = 'Verify case insensitive matching works';
ef4a39e5
JH
827 foreach my $x (keys %UPPER) {
828 my $y = lc $x;
829 next unless uc $y eq $x;
830 print "# UPPER $x lc $y ",
831 $x =~ /$y/i ? 1 : 0, " ",
832 $y =~ /$x/i ? 1 : 0, "\n" if 0;
83d38f53 833 #
ef4a39e5
JH
834 # If $x and $y contain regular expression characters
835 # AND THEY lowercase (/i) to regular expression characters,
836 # regcomp() will be mightily confused. No, the \Q doesn't
837 # help here (maybe regex engine internal lowercasing
838 # is done after the \Q?) An example of this happening is
839 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
840 # the chr(173) (the "[") is the lowercase of the chr(235).
83d38f53 841 #
ef4a39e5
JH
842 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
843 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
844 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
845 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
846 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
847 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
83d38f53
JH
848 #
849 # Similar things can happen even under (bastardised)
850 # non-EBCDIC locales: in many European countries before the
851 # advent of ISO 8859-x nationally customised versions of
852 # ISO 646 were devised, reusing certain punctuation
853 # characters for modified characters needed by the
854 # country/language. For example, the "|" might have
855 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
856 #
ef4a39e5 857 if ($x =~ $re || $y =~ $re) {
c4093d7d 858 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
ef4a39e5 859 next;
8ac0d9e6 860 }
ef4a39e5
JH
861 # With utf8 both will fail since the locale concept
862 # of upper/lower does not work well in Unicode.
863 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
c00ff1c7 864 }
ef4a39e5 865
c00ff1c7
DM
866 foreach my $x (keys %lower) {
867 my $y = uc $x;
868 next unless lc $y eq $x;
869 print "# lower $x uc $y ",
870 $x =~ /$y/i ? 1 : 0, " ",
871 $y =~ /$x/i ? 1 : 0, "\n" if 0;
872 if ($x =~ $re || $y =~ $re) { # See above.
c4093d7d 873 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
c00ff1c7 874 next;
8ac0d9e6 875 }
c00ff1c7
DM
876 # With utf8 both will fail since the locale concept
877 # of upper/lower does not work well in Unicode.
878 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
879 }
c4093d7d 880 tryneoalpha($Locale, $locales_test_number, @f == 0);
c00ff1c7 881 if (@f) {
c4093d7d 882 print "# failed $locales_test_number locale '$Locale' characters @f\n"
c00ff1c7 883 }
d8093b23 884 }
8ebc5c01 885}
284102e8 886
c4093d7d 887my $final_locales_test_number = $locales_test_number;
6cf0b567 888
2a680da6
JH
889# Recount the errors.
890
c4093d7d
KW
891foreach ($first_locales_test_number..$final_locales_test_number) {
892 if (%setlocale_failed) {
893 print "not ";
894 }
895 elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
896 if (defined $not_necessarily_a_problem_test_number
897 && $_ == $not_necessarily_a_problem_test_number)
898 {
899 print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n";
b4e009be 900 print "# It usually indicates a problem in the environment,\n";
284102e8
JH
901 print "# not in Perl itself.\n";
902 }
903 print "not ";
8ebc5c01 904 }
c4093d7d 905 print "ok $_";
c08acc4c 906 print " $test_names{$_}" if defined $test_names{$_};
c4093d7d 907 print "\n";
8ebc5c01 908}
fb73857a 909
2a680da6
JH
910# Give final advice.
911
284102e8
JH
912my $didwarn = 0;
913
c4093d7d 914foreach ($first_locales_test_number..$final_locales_test_number) {
284102e8
JH
915 if ($Problem{$_}) {
916 my @f = sort keys %{ $Problem{$_} };
917 my $f = join(" ", @f);
918 $f =~ s/(.{50,60}) /$1\n#\t/g;
2a680da6
JH
919 print
920 "#\n",
921 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
284102e8
JH
922 "#\t", $f, "\n#\n",
923 "# on your system may have errors because the locale test $_\n",
924 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
925 ".\n";
2a680da6 926 print <<EOW;
284102e8
JH
927#
928# If your users are not using these locales you are safe for the moment,
929# but please report this failure first to perlbug\@perl.com using the
930# perlbug script (as described in the INSTALL file) so that the exact
931# details of the failures can be sorted out first and then your operating
932# system supplier can be alerted about these anomalies.
933#
934EOW
935 $didwarn = 1;
fb73857a
PP
936 }
937}
774d564b 938
26d80d95 939# Tell which locales were okay and which were not.
2a680da6 940
284102e8 941if ($didwarn) {
26d80d95 942 my (@s, @F);
284102e8
JH
943
944 foreach my $l (@Locale) {
945 my $p = 0;
c4093d7d
KW
946 if ($setlocale_failed{$l}) {
947 $p++;
948 }
949 else {
1f5852c9
KW
950 foreach my $t
951 ($first_locales_test_number..$final_locales_test_number)
952 {
953 $p++ if $Problem{$t}{$l};
954 }
c4093d7d 955 }
284102e8 956 push @s, $l if $p == 0;
9445c837 957 push @F, $l unless $p == 0;
8ebc5c01 958 }
284102e8 959
68d47915
CK
960 if (@s) {
961 my $s = join(" ", @s);
962 $s =~ s/(.{50,60}) /$1\n#\t/g;
963
964 warn
965 "# The following locales\n#\n",
966 "#\t", $s, "\n#\n",
967 "# tested okay.\n#\n",
968 } else {
26d80d95
LC
969 warn "# None of your locales were fully okay.\n";
970 }
971
972 if (@F) {
973 my $F = join(" ", @F);
974 $F =~ s/(.{50,60}) /$1\n#\t/g;
975
976 warn
977 "# The following locales\n#\n",
0e053d1e 978 "#\t", $F, "\n#\n",
26d80d95
LC
979 "# had problems.\n#\n",
980 } else {
981 warn "# None of your locales were broken.\n";
68d47915 982 }
8ebc5c01 983}
90248788 984
c4093d7d 985$test_num = $final_locales_test_number;
c213d471 986
094a2f8c 987# Test that tainting and case changing works on utf8 strings. These tests are
1f5852c9
KW
988# placed last to avoid disturbing the hard-coded test numbers that existed at
989# the time these were added above this in this file.
094a2f8c
KW
990setlocale(LC_ALL, "C");
991{
992 use locale;
993
094a2f8c
KW
994 foreach my $function ("uc", "ucfirst", "lc", "lcfirst") {
995 my @list; # List of code points to test for $function
996
997 # Used to calculate the changed case for ASCII characters by using the
998 # ord, instead of using one of the functions under test.
999 my $ascii_case_change_delta;
1000 my $above_latin1_case_change_delta; # Same for the specific ords > 255
1001 # that we use
1002
1003 # We test an ASCII character, which should change case and be tainted;
1004 # a Latin1 character, which shouldn't change case under this C locale,
1005 # and is tainted.
1006 # an above-Latin1 character that when the case is changed would cross
1007 # the 255/256 boundary, so doesn't change case and isn't tainted
1008 # (the \x{149} is one of these, but changes into 2 characters, the
1009 # first one of which doesn't cross the boundary.
1010 # the final one in each list is an above-Latin1 character whose case
1011 # does change, and shouldn't be tainted. The code below uses its
1012 # position in its list as a marker to indicate that it, unlike the
1013 # other code points above ASCII, has a successful case change
1014 if ($function =~ /^u/) {
094a2f8c
KW
1015 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
1016 $ascii_case_change_delta = -32;
1017 $above_latin1_case_change_delta = -1;
1018 }
1019 else {
1020 @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}");
1021 $ascii_case_change_delta = +32;
1022 $above_latin1_case_change_delta = +1;
1023 }
094a2f8c
KW
1024 foreach my $j (0 .. $#list) {
1025 my $char = $list[$j];
094a2f8c 1026 utf8::upgrade($char);
094a2f8c
KW
1027 my $should_be = ($j == $#list)
1028 ? chr(ord($char) + $above_latin1_case_change_delta)
1029 : (length $char == 0 || ord($char) > 127)
1030 ? $char
1031 : chr(ord($char) + $ascii_case_change_delta);
1032
1033 # This monstrosity is in order to avoid using an eval, which might
1034 # perturb the results
1035 my $changed = ($function eq "uc")
1036 ? uc($char)
1037 : ($function eq "ucfirst")
1038 ? ucfirst($char)
1039 : ($function eq "lc")
1040 ? lc($char)
1041 : ($function eq "lcfirst")
1042 ? lcfirst($char)
9c11bb3c 1043 : die("Unexpected function \"$function\"");
c213d471 1044 ok($changed eq $should_be, "$function(\"$char\") in C locale should be \"$should_be\", got \"$changed\"");
094a2f8c
KW
1045
1046 # Tainting shouldn't happen for empty strings, or those characters
1047 # above 255.
094a2f8c 1048 (length($char) > 0 && ord($char) < 256)
36a42ae7
KW
1049 ? check_taint($changed)
1050 : check_taint_not($changed);
094a2f8c
KW
1051 }
1052 }
1053}
1054
fdf053ee 1055print "1..$test_num\n";
906f284f 1056
90248788 1057# eof