This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.t: Calculate locale values AFTER setting the locale
[perl5.git] / lib / locale.t
CommitLineData
8ebc5c01 1#!./perl -wT
2
e3a2734b
KW
3binmode STDOUT, ':utf8';
4binmode STDERR, ':utf8';
5
8ebc5c01 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 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 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 68
69 print 'not ' unless ($result);
c213d471 70 print "ok " . ++$test_num;
e3a2734b
KW
71 print " $message";
72 print "\n";
8ebc5c01 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 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 88}
89
36a42ae7
KW
90sub check_taint_not ($) {
91 ok((not is_tainted($_[0])), "verify that isn't tainted");
8ebc5c01 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 112
113$_ = $a; # untaint $_
114
115$_ = uc($a); # taint $_
116
36a42ae7 117check_taint $_;
8ebc5c01 118
119/(\w)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
120check_taint $&;
121check_taint $`;
122check_taint $';
123check_taint $+;
124check_taint $1;
125check_taint_not $2;
8ebc5c01 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 134
135/(\W)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
136check_taint $&;
137check_taint $`;
138check_taint $';
139check_taint $+;
140check_taint $1;
141check_taint_not $2;
8ebc5c01 142
143/(\s)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
144check_taint $&;
145check_taint $`;
146check_taint $';
147check_taint $+;
148check_taint $1;
149check_taint_not $2;
8ebc5c01 150
151/(\S)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
152check_taint $&;
153check_taint $`;
154check_taint $';
155check_taint $+;
156check_taint $1;
157check_taint_not $2;
8ebc5c01 158
159$_ = $a; # untaint $_
160
36a42ae7 161check_taint_not $_;
8ebc5c01 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 170
171$_ = $a; # untaint $_
172
36a42ae7 173check_taint_not $_;
8ebc5c01 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 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 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 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 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 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 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 246
247# After all this tainting $a should be cool.
248
36a42ae7 249check_taint_not $a;
8ebc5c01 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
KW
260# The test number before our first setlocale()
261my $last_without_setlocale = $test_num;
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
0e053d1e
JH
340my @utf8locale;
341my %utf8skip;
342
284102e8 343sub getalnum_ {
8ebc5c01 344 sort grep /\w/, map { chr } 0..255
345}
346
284102e8
JH
347sub trylocale {
348 my $locale = shift;
0b9f254b 349 return if grep { $locale eq $_ } @Locale;
284102e8
JH
350 if (setlocale(LC_ALL, $locale)) {
351 push @Locale, $locale;
352 }
353}
8ebc5c01 354
284102e8
JH
355sub decode_encodings {
356 my @enc;
8ebc5c01 357
284102e8
JH
358 foreach (split(/ /, shift)) {
359 if (/^(\d+)$/) {
360 push @enc, "ISO8859-$1";
361 push @enc, "iso8859$1"; # HP
362 if ($1 eq '1') {
363 push @enc, "roman8"; # HP
364 }
365 } else {
366 push @enc, $_;
dd8482fc 367 push @enc, "$_.UTF-8";
8ebc5c01 368 }
369 }
ee50adbe
PP
370 if ($^O eq 'os390') {
371 push @enc, qw(IBM-037 IBM-819 IBM-1047);
372 }
8ebc5c01 373
284102e8 374 return @enc;
8ebc5c01 375}
376
284102e8
JH
377trylocale("C");
378trylocale("POSIX");
379foreach (0..15) {
380 trylocale("ISO8859-$_");
284102e8 381 trylocale("iso8859$_");
097ee67d
JH
382 trylocale("iso8859-$_");
383 trylocale("iso_8859_$_");
384 trylocale("isolatin$_");
385 trylocale("isolatin-$_");
386 trylocale("iso_latin_$_");
8ebc5c01 387}
388
645e49ed
JH
389# Sanitize the environment so that we can run the external 'locale'
390# program without the taint mode getting grumpy.
cce5967e
JH
391
392# $ENV{PATH} is special in VMS.
393delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
394
395# Other subversive stuff.
396delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
dd8482fc 397
21477fb4 398if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
dd8482fc 399 while (<LOCALES>) {
d281a6ac
NC
400 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
401 # ain't great when we're running this testPERL_UNICODE= so that utf8
402 # locales will cause all IO hadles to default to (assume) utf8
403 next unless utf8::valid($_);
dd8482fc
JH
404 chomp;
405 trylocale($_);
284102e8 406 }
dd8482fc 407 close(LOCALES);
a6259068
PP
408} elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
409# The SYS$I18N_LOCALE logical name search list was not present on
410# VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
411 opendir(LOCALES, "SYS\$I18N_LOCALE:");
412 while ($_ = readdir(LOCALES)) {
413 chomp;
414 trylocale($_);
415 }
416 close(LOCALES);
87e33296
SP
417} elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
418
419 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
420 # is much easier and faster than the last resort method.
421
422 opendir(LOCALES, '/usr/share/locale');
423 while ($_ = readdir(LOCALES)) {
424 chomp;
425 trylocale($_);
426 }
427 close(LOCALES);
dd8482fc
JH
428} else {
429
430 # This is going to be slow.
431
432 foreach my $locale (split(/\n/, $locales)) {
433 my ($locale_name, $language_codes, $country_codes, $encodings) =
434 split(/:/, $locale);
435 my @enc = decode_encodings($encodings);
436 foreach my $loc (split(/ /, $locale_name)) {
437 trylocale($loc);
284102e8 438 foreach my $enc (@enc) {
dd8482fc 439 trylocale("$loc.$enc");
284102e8 440 }
dd8482fc 441 $loc = lc $loc;
284102e8 442 foreach my $enc (@enc) {
dd8482fc
JH
443 trylocale("$loc.$enc");
444 }
445 }
446 foreach my $lang (split(/ /, $language_codes)) {
447 trylocale($lang);
448 foreach my $country (split(/ /, $country_codes)) {
449 my $lc = "${lang}_${country}";
450 trylocale($lc);
451 foreach my $enc (@enc) {
452 trylocale("$lc.$enc");
453 }
454 my $lC = "${lang}_\U${country}";
455 trylocale($lC);
456 foreach my $enc (@enc) {
457 trylocale("$lC.$enc");
458 }
284102e8
JH
459 }
460 }
461 }
462}
4599a1de 463
d43ce814
JH
464setlocale(LC_ALL, "C");
465
86f50d7d 466if ($^O eq 'darwin') {
4373e181 467 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
86f50d7d 468 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
4373e181
RGS
469 (my $v) = $Config{osvers} =~ /^(\d+)/;
470 if ($v >= 8 and $v < 10) {
86f50d7d 471 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
a5ec937f 472 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
dfa5c78f 473 } elsif ($v < 12) {
a44d0896
NC
474 debug "# Skipping be_BY locales -- buggy in Darwin\n";
475 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
a5ec937f 476 }
86f50d7d
DD
477}
478
4599a1de
JH
479@Locale = sort @Locale;
480
887ef7ed
PP
481debug "# Locales =\n";
482for ( @Locale ) {
483 debug "# $_\n";
484}
8ebc5c01 485
284102e8 486my %Problem;
2a680da6
JH
487my %Okay;
488my %Testing;
097ee67d 489my @Neoalpha;
d8093b23 490my %Neoalpha;
284102e8 491
2a680da6
JH
492sub tryneoalpha {
493 my ($Locale, $i, $test) = @_;
2a680da6
JH
494 unless ($test) {
495 $Problem{$i}{$Locale} = 1;
496 debug "# failed $i with locale '$Locale'\n";
497 } else {
498 push @{$Okay{$i}}, $Locale;
499 }
500}
501
284102e8
JH
502foreach $Locale (@Locale) {
503 debug "# Locale = $Locale\n";
284102e8
JH
504
505 unless (setlocale(LC_ALL, $Locale)) {
506 foreach (99..103) {
507 $Problem{$_}{$Locale} = -1;
8ebc5c01 508 }
284102e8 509 next;
8ebc5c01 510 }
8ebc5c01 511
e5272a46
KW
512 @Alnum_ = getalnum_();
513 debug "# w = ", join("",@Alnum_), "\n";
514
284102e8
JH
515 # Sieve the uppercase and the lowercase.
516
097ee67d
JH
517 my %UPPER = ();
518 my %lower = ();
519 my %BoThCaSe = ();
284102e8
JH
520 for (@Alnum_) {
521 if (/[^\d_]/) { # skip digits and the _
522 if (uc($_) eq $_) {
523 $UPPER{$_} = $_;
524 }
525 if (lc($_) eq $_) {
526 $lower{$_} = $_;
527 }
528 }
529 }
530 foreach (keys %UPPER) {
097ee67d 531 $BoThCaSe{$_}++ if exists $lower{$_};
284102e8
JH
532 }
533 foreach (keys %lower) {
097ee67d 534 $BoThCaSe{$_}++ if exists $UPPER{$_};
284102e8 535 }
097ee67d 536 foreach (keys %BoThCaSe) {
284102e8
JH
537 delete $UPPER{$_};
538 delete $lower{$_};
539 }
540
db4b7445
A
541 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
542 debug "# lower = ", join("", sort keys %lower ), "\n";
543 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
284102e8
JH
544
545 # Find the alphabets that are not alphabets in the default locale.
8ebc5c01 546
284102e8
JH
547 {
548 no locale;
8ebc5c01 549
284102e8
JH
550 @Neoalpha = ();
551 for (keys %UPPER, keys %lower) {
552 push(@Neoalpha, $_) if (/\W/);
d8093b23 553 $Neoalpha{$_} = $_;
284102e8 554 }
8ebc5c01 555 }
8ebc5c01 556
284102e8 557 @Neoalpha = sort @Neoalpha;
8ebc5c01 558
db4b7445 559 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
8ebc5c01 560
284102e8
JH
561 if (@Neoalpha == 0) {
562 # If we have no Neoalphas the remaining tests are no-ops.
6be75cd7 563 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
a88c3d7c
GS
564 foreach (99..102) {
565 push @{$Okay{$_}}, $Locale;
566 }
6be75cd7 567 } else {
8ebc5c01 568
6be75cd7 569 # Test \w.
284102e8 570
ef4a39e5 571 my $word = join('', @Neoalpha);
8ebc5c01 572
5fef21a3
JH
573 my $badutf8;
574 {
575 local $SIG{__WARN__} = sub {
576 $badutf8 = $_[0] =~ /Malformed UTF-8/;
577 };
578 $Locale =~ /utf-?8/i;
579 }
580
581 if ($badutf8) {
582 debug "# Locale name contains bad UTF-8, skipping test 99 for locale '$Locale'\n";
583 } elsif ($Locale =~ /utf-?8/i) {
ef4a39e5
JH
584 debug "# unknown whether locale and Unicode have the same \\w, skipping test 99 for locale '$Locale'\n";
585 push @{$Okay{99}}, $Locale;
586 } else {
587 if ($word =~ /^(\w+)$/) {
588 tryneoalpha($Locale, 99, 1);
589 } else {
590 tryneoalpha($Locale, 99, 0);
591 }
8ac0d9e6 592 }
ef4a39e5 593
2a680da6 594 # Cross-check the whole 8-bit character set.
8ebc5c01 595
6be75cd7 596 for (map { chr } 0..255) {
2a680da6
JH
597 tryneoalpha($Locale, 100,
598 (/\w/ xor /\W/) ||
599 (/\d/ xor /\D/) ||
600 (/\s/ xor /\S/));
284102e8 601 }
8ebc5c01 602
6be75cd7 603 # Test for read-only scalars' locale vs non-locale comparisons.
284102e8 604
284102e8 605 {
6be75cd7
JH
606 no locale;
607 $a = "qwerty";
608 {
609 use locale;
2a680da6 610 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
8ebc5c01 611 }
612 }
8ebc5c01 613
6be75cd7
JH
614 {
615 my ($from, $to, $lesser, $greater,
616 @test, %test, $test, $yes, $no, $sign);
617
618 for (0..9) {
619 # Select a slice.
620 $from = int(($_*@Alnum_)/10);
621 $to = $from + int(@Alnum_/10);
622 $to = $#Alnum_ if ($to > $#Alnum_);
623 $lesser = join('', @Alnum_[$from..$to]);
624 # Select a slice one character on.
625 $from++; $to++;
626 $to = $#Alnum_ if ($to > $#Alnum_);
627 $greater = join('', @Alnum_[$from..$to]);
628 ($yes, $no, $sign) = ($lesser lt $greater
629 ? (" ", "not ", 1)
630 : ("not ", " ", -1));
631 # all these tests should FAIL (return 0).
632 # Exact lt or gt cannot be tested because
633 # in some locales, say, eacute and E may test equal.
634 @test =
635 (
636 $no.' ($lesser le $greater)', # 1
637 'not ($lesser ne $greater)', # 2
638 ' ($lesser eq $greater)', # 3
639 $yes.' ($lesser ge $greater)', # 4
640 $yes.' ($lesser ge $greater)', # 5
641 $yes.' ($greater le $lesser )', # 7
642 'not ($greater ne $lesser )', # 8
643 ' ($greater eq $lesser )', # 9
644 $no.' ($greater ge $lesser )', # 10
0e053d1e 645 'not (($lesser cmp $greater) == -($sign))' # 11
6be75cd7
JH
646 );
647 @test{@test} = 0 x @test;
648 $test = 0;
284102e8 649 for my $ti (@test) {
6be75cd7
JH
650 $test{$ti} = eval $ti;
651 $test ||= $test{$ti}
284102e8 652 }
2a680da6 653 tryneoalpha($Locale, 102, $test == 0);
6be75cd7 654 if ($test) {
6be75cd7
JH
655 debug "# lesser = '$lesser'\n";
656 debug "# greater = '$greater'\n";
657 debug "# lesser cmp greater = ",
658 $lesser cmp $greater, "\n";
659 debug "# greater cmp lesser = ",
660 $greater cmp $lesser, "\n";
661 debug "# (greater) from = $from, to = $to\n";
662 for my $ti (@test) {
663 debugf("# %-40s %-4s", $ti,
664 $test{$ti} ? 'FAIL' : 'ok');
665 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
666 debugf("(%s == %4d)", $1, eval $1);
667 }
668 debug "\n#";
669 }
284102e8 670
6be75cd7
JH
671 last;
672 }
284102e8 673 }
8ebc5c01 674 }
675 }
6be75cd7
JH
676
677 use locale;
678
679 my ($x, $y) = (1.23, 1.23);
680
0e053d1e 681 $a = "$x";
6be75cd7 682 printf ''; # printf used to reset locale to "C"
0e053d1e 683 $b = "$y";
6be75cd7 684
2a680da6
JH
685 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
686
687 tryneoalpha($Locale, 103, $a eq $b);
6be75cd7
JH
688
689 my $c = "$x";
690 my $z = sprintf ''; # sprintf used to reset locale to "C"
691 my $d = "$y";
692
2a680da6 693 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
6be75cd7 694
2a680da6 695 tryneoalpha($Locale, 104, $c eq $d);
6be75cd7 696
2a680da6 697 {
9f1b1f2d 698 use warnings;
2a680da6 699 my $w = 0;
0e053d1e
JH
700 local $SIG{__WARN__} =
701 sub {
906f284f 702 print "# @_\n";
0e053d1e
JH
703 $w++;
704 };
6be75cd7 705
0e053d1e
JH
706 # The == (among other ops) used to warn for locales
707 # that had something else than "." as the radix character.
6be75cd7 708
2a680da6 709 tryneoalpha($Locale, 105, $c == 1.23);
6be75cd7 710
2a680da6 711 tryneoalpha($Locale, 106, $c == $x);
6be75cd7 712
2a680da6 713 tryneoalpha($Locale, 107, $c == $d);
6be75cd7 714
2a680da6 715 {
2de3dbcc 716# no locale; # XXX did this ever work correctly?
6be75cd7 717
2a680da6 718 my $e = "$x";
6be75cd7 719
2a680da6 720 debug "# 108..110: e = $e, Locale = $Locale\n";
6be75cd7 721
2a680da6 722 tryneoalpha($Locale, 108, $e == 1.23);
6be75cd7 723
2a680da6
JH
724 tryneoalpha($Locale, 109, $e == $x);
725
726 tryneoalpha($Locale, 110, $e == $c);
6be75cd7 727 }
2a680da6 728
2a680da6 729 my $f = "1.23";
906f284f 730 my $g = 2.34;
2a680da6 731
906f284f 732 debug "# 111..115: f = $f, g = $g, locale = $Locale\n";
2a680da6 733
906f284f 734 tryneoalpha($Locale, 111, $f == 1.23);
6be75cd7 735
906f284f 736 tryneoalpha($Locale, 112, $f == $x);
2a680da6 737
906f284f
NC
738 tryneoalpha($Locale, 113, $f == $c);
739
740 tryneoalpha($Locale, 114, abs(($f + $g) - 3.57) < 0.01);
741
742 tryneoalpha($Locale, 115, $w == 0);
6be75cd7
JH
743 }
744
26d80d95
LC
745 # Does taking lc separately differ from taking
746 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
747 # The bug was in the caching of the 'o'-magic.
2a680da6
JH
748 {
749 use locale;
6be75cd7 750
2a680da6
JH
751 sub lcA {
752 my $lc0 = lc $_[0];
753 my $lc1 = lc $_[1];
754 return $lc0 cmp $lc1;
755 }
6be75cd7 756
2a680da6
JH
757 sub lcB {
758 return lc($_[0]) cmp lc($_[1]);
759 }
6be75cd7 760
2a680da6
JH
761 my $x = "ab";
762 my $y = "aa";
763 my $z = "AB";
6be75cd7 764
906f284f 765 tryneoalpha($Locale, 116,
2a680da6
JH
766 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
767 lcA($x, $z) == 0 && lcB($x, $z) == 0);
6be75cd7 768 }
d8093b23 769
26d80d95
LC
770 # Does lc of an UPPER (if different from the UPPER) match
771 # case-insensitively the UPPER, and does the UPPER match
772 # case-insensitively the lc of the UPPER. And vice versa.
3ba0e062 773 {
ef4a39e5
JH
774 use locale;
775 no utf8;
776 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
777
778 my @f = ();
779 foreach my $x (keys %UPPER) {
780 my $y = lc $x;
781 next unless uc $y eq $x;
782 print "# UPPER $x lc $y ",
783 $x =~ /$y/i ? 1 : 0, " ",
784 $y =~ /$x/i ? 1 : 0, "\n" if 0;
83d38f53 785 #
ef4a39e5
JH
786 # If $x and $y contain regular expression characters
787 # AND THEY lowercase (/i) to regular expression characters,
788 # regcomp() will be mightily confused. No, the \Q doesn't
789 # help here (maybe regex engine internal lowercasing
790 # is done after the \Q?) An example of this happening is
791 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
792 # the chr(173) (the "[") is the lowercase of the chr(235).
83d38f53 793 #
ef4a39e5
JH
794 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
795 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
796 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
797 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
798 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
799 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
83d38f53
JH
800 #
801 # Similar things can happen even under (bastardised)
802 # non-EBCDIC locales: in many European countries before the
803 # advent of ISO 8859-x nationally customised versions of
804 # ISO 646 were devised, reusing certain punctuation
805 # characters for modified characters needed by the
806 # country/language. For example, the "|" might have
807 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
808 #
ef4a39e5
JH
809 if ($x =~ $re || $y =~ $re) {
810 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
811 next;
8ac0d9e6 812 }
ef4a39e5
JH
813 # With utf8 both will fail since the locale concept
814 # of upper/lower does not work well in Unicode.
815 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
c00ff1c7 816 }
ef4a39e5 817
c00ff1c7
DM
818 foreach my $x (keys %lower) {
819 my $y = uc $x;
820 next unless lc $y eq $x;
821 print "# lower $x uc $y ",
822 $x =~ /$y/i ? 1 : 0, " ",
823 $y =~ /$x/i ? 1 : 0, "\n" if 0;
824 if ($x =~ $re || $y =~ $re) { # See above.
825 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
826 next;
8ac0d9e6 827 }
c00ff1c7
DM
828 # With utf8 both will fail since the locale concept
829 # of upper/lower does not work well in Unicode.
830 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
831 }
832 tryneoalpha($Locale, 117, @f == 0);
833 if (@f) {
834 print "# failed 117 locale '$Locale' characters @f\n"
835 }
d8093b23 836 }
8ebc5c01 837}
284102e8 838
6cf0b567
KW
839my $last_locales = $have_setlocale ? &last_locales : $last_without_setlocale;
840
2a680da6
JH
841# Recount the errors.
842
6cf0b567 843foreach ($last_without_setlocale+1..$last_locales) {
2a680da6 844 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
097ee67d
JH
845 if ($_ == 102) {
846 print "# The failure of test 102 is not necessarily fatal.\n";
b4e009be 847 print "# It usually indicates a problem in the environment,\n";
284102e8
JH
848 print "# not in Perl itself.\n";
849 }
850 print "not ";
8ebc5c01 851 }
284102e8 852 print "ok $_\n";
8ebc5c01 853}
fb73857a 854
2a680da6
JH
855# Give final advice.
856
284102e8
JH
857my $didwarn = 0;
858
98daf176 859foreach (99..$last_locales) {
284102e8
JH
860 if ($Problem{$_}) {
861 my @f = sort keys %{ $Problem{$_} };
862 my $f = join(" ", @f);
863 $f =~ s/(.{50,60}) /$1\n#\t/g;
2a680da6
JH
864 print
865 "#\n",
866 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
284102e8
JH
867 "#\t", $f, "\n#\n",
868 "# on your system may have errors because the locale test $_\n",
869 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
870 ".\n";
2a680da6 871 print <<EOW;
284102e8
JH
872#
873# If your users are not using these locales you are safe for the moment,
874# but please report this failure first to perlbug\@perl.com using the
875# perlbug script (as described in the INSTALL file) so that the exact
876# details of the failures can be sorted out first and then your operating
877# system supplier can be alerted about these anomalies.
878#
879EOW
880 $didwarn = 1;
fb73857a 881 }
882}
774d564b 883
26d80d95 884# Tell which locales were okay and which were not.
2a680da6 885
284102e8 886if ($didwarn) {
26d80d95 887 my (@s, @F);
284102e8
JH
888
889 foreach my $l (@Locale) {
890 my $p = 0;
98daf176 891 foreach my $t (102..$last_locales) {
284102e8 892 $p++ if $Problem{$t}{$l};
8ebc5c01 893 }
284102e8 894 push @s, $l if $p == 0;
26d80d95 895 push @F, $l unless $p == 0;
8ebc5c01 896 }
284102e8 897
68d47915
CK
898 if (@s) {
899 my $s = join(" ", @s);
900 $s =~ s/(.{50,60}) /$1\n#\t/g;
901
902 warn
903 "# The following locales\n#\n",
904 "#\t", $s, "\n#\n",
905 "# tested okay.\n#\n",
906 } else {
26d80d95
LC
907 warn "# None of your locales were fully okay.\n";
908 }
909
910 if (@F) {
911 my $F = join(" ", @F);
912 $F =~ s/(.{50,60}) /$1\n#\t/g;
913
914 warn
915 "# The following locales\n#\n",
0e053d1e 916 "#\t", $F, "\n#\n",
26d80d95
LC
917 "# had problems.\n#\n",
918 } else {
919 warn "# None of your locales were broken.\n";
68d47915 920 }
0e053d1e 921
c406fbf4
JH
922 if (@utf8locale) {
923 my $S = join(" ", @utf8locale);
924 $S =~ s/(.{50,60}) /$1\n#\t/g;
925
926 warn "#\n# The following locales\n#\n",
927 "#\t", $S, "\n#\n",
928 "# were skipped for the tests ",
929 join(" ", sort {$a<=>$b} keys %utf8skip), "\n",
930 "# because UTF-8 and locales do not work together in Perl.\n#\n";
931 }
8ebc5c01 932}
90248788 933
98daf176
KW
934sub last_locales { 117 }
935
c213d471 936$test_num = $last_locales;
c213d471 937
094a2f8c
KW
938# Test that tainting and case changing works on utf8 strings. These tests are
939# placed last to avoid disturbing the hard-coded test numbers above this in
940# this file.
941setlocale(LC_ALL, "C");
942{
943 use locale;
944
094a2f8c
KW
945 foreach my $function ("uc", "ucfirst", "lc", "lcfirst") {
946 my @list; # List of code points to test for $function
947
948 # Used to calculate the changed case for ASCII characters by using the
949 # ord, instead of using one of the functions under test.
950 my $ascii_case_change_delta;
951 my $above_latin1_case_change_delta; # Same for the specific ords > 255
952 # that we use
953
954 # We test an ASCII character, which should change case and be tainted;
955 # a Latin1 character, which shouldn't change case under this C locale,
956 # and is tainted.
957 # an above-Latin1 character that when the case is changed would cross
958 # the 255/256 boundary, so doesn't change case and isn't tainted
959 # (the \x{149} is one of these, but changes into 2 characters, the
960 # first one of which doesn't cross the boundary.
961 # the final one in each list is an above-Latin1 character whose case
962 # does change, and shouldn't be tainted. The code below uses its
963 # position in its list as a marker to indicate that it, unlike the
964 # other code points above ASCII, has a successful case change
965 if ($function =~ /^u/) {
966 #@list = ("\xff", "\x{fb00}", "\x{149}", "\x{101}");
967 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
968 $ascii_case_change_delta = -32;
969 $above_latin1_case_change_delta = -1;
970 }
971 else {
972 @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}");
973 $ascii_case_change_delta = +32;
974 $above_latin1_case_change_delta = +1;
975 }
976 $|=1;
977 foreach my $j (0 .. $#list) {
978 my $char = $list[$j];
979 #print STDERR __LINE__, ": $char\n";
980 #check_taint_not($i++, $char);
981 utf8::upgrade($char);
982 #check_taint_not($i++, $char);
983 my $should_be = ($j == $#list)
984 ? chr(ord($char) + $above_latin1_case_change_delta)
985 : (length $char == 0 || ord($char) > 127)
986 ? $char
987 : chr(ord($char) + $ascii_case_change_delta);
988
989 # This monstrosity is in order to avoid using an eval, which might
990 # perturb the results
991 my $changed = ($function eq "uc")
992 ? uc($char)
993 : ($function eq "ucfirst")
994 ? ucfirst($char)
995 : ($function eq "lc")
996 ? lc($char)
997 : ($function eq "lcfirst")
998 ? lcfirst($char)
999 : croak("Unexpected function \"$function\"");
c213d471 1000 ok($changed eq $should_be, "$function(\"$char\") in C locale should be \"$should_be\", got \"$changed\"");
094a2f8c
KW
1001
1002 # Tainting shouldn't happen for empty strings, or those characters
1003 # above 255.
1004 #print STDERR __LINE__, ": $char\n";
1005 (length($char) > 0 && ord($char) < 256)
36a42ae7
KW
1006 ? check_taint($changed)
1007 : check_taint_not($changed);
094a2f8c
KW
1008 }
1009 }
1010}
1011
1012
fdf053ee 1013print "1..$test_num\n";
906f284f 1014
90248788 1015# eof