This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.t: Start removing hard-coded test numbers
[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
cd19b65c
JH
56# UWIN seems to loop after test 98, just skip for now
57$have_setlocale = 0 if ($^O =~ /^uwin/);
58
98daf176 59my $last_locales = $have_setlocale ? &last_locales : &last_without_setlocale;
906f284f 60my $last = $have_setlocale ? &last : &last_without_setlocale;
26d80d95
LC
61
62print "1..$last\n";
8ebc5c01 63
9a66ea41 64sub LC_ALL ();
8ebc5c01 65
0e053d1e 66$a = 'abc %';
8ebc5c01 67
c213d471
KW
68my $test_num = 0;
69
8ebc5c01 70sub ok {
c213d471 71 my ($result, $message) = @_;
e3a2734b 72 $message = "" unless defined $message;
8ebc5c01
PP
73
74 print 'not ' unless ($result);
c213d471 75 print "ok " . ++$test_num;
e3a2734b
KW
76 print " $message";
77 print "\n";
8ebc5c01
PP
78}
79
80# First we'll do a lot of taint checking for locales.
81# This is the easiest to test, actually, as any locale,
82# even the default locale will taint under 'use locale'.
83
84sub is_tainted { # hello, camel two.
9f1b1f2d 85 no warnings 'uninitialized' ;
8ebc5c01 86 my $dummy;
ba74571d 87 local $@;
8ebc5c01
PP
88 not eval { $dummy = join("", @_), kill 0; 1 }
89}
90
91sub check_taint ($$) {
c213d471 92 ok is_tainted($_[1]), "verify that is tainted";
8ebc5c01
PP
93}
94
95sub check_taint_not ($$) {
c213d471 96 ok((not is_tainted($_[1])), "verify that isn't tainted");
8ebc5c01
PP
97}
98
99use locale; # engage locale and therefore locale taint.
100
101check_taint_not 1, $a;
102
103check_taint 2, uc($a);
104check_taint 3, "\U$a";
105check_taint 4, ucfirst($a);
106check_taint 5, "\u$a";
107check_taint 6, lc($a);
108check_taint 7, "\L$a";
109check_taint 8, lcfirst($a);
110check_taint 9, "\l$a";
111
ff9121f8
JH
112check_taint_not 10, sprintf('%e', 123.456);
113check_taint_not 11, sprintf('%f', 123.456);
114check_taint_not 12, sprintf('%g', 123.456);
8ebc5c01
PP
115check_taint_not 13, sprintf('%d', 123.456);
116check_taint_not 14, sprintf('%x', 123.456);
117
118$_ = $a; # untaint $_
119
120$_ = uc($a); # taint $_
121
122check_taint 15, $_;
123
124/(\w)/; # taint $&, $`, $', $+, $1.
125check_taint 16, $&;
126check_taint 17, $`;
127check_taint 18, $';
128check_taint 19, $+;
129check_taint 20, $1;
130check_taint_not 21, $2;
131
132/(.)/; # untaint $&, $`, $', $+, $1.
133check_taint_not 22, $&;
134check_taint_not 23, $`;
135check_taint_not 24, $';
136check_taint_not 25, $+;
137check_taint_not 26, $1;
138check_taint_not 27, $2;
139
140/(\W)/; # taint $&, $`, $', $+, $1.
141check_taint 28, $&;
142check_taint 29, $`;
143check_taint 30, $';
144check_taint 31, $+;
145check_taint 32, $1;
146check_taint_not 33, $2;
147
148/(\s)/; # taint $&, $`, $', $+, $1.
149check_taint 34, $&;
150check_taint 35, $`;
151check_taint 36, $';
152check_taint 37, $+;
153check_taint 38, $1;
154check_taint_not 39, $2;
155
156/(\S)/; # taint $&, $`, $', $+, $1.
157check_taint 40, $&;
158check_taint 41, $`;
159check_taint 42, $';
160check_taint 43, $+;
161check_taint 44, $1;
162check_taint_not 45, $2;
163
164$_ = $a; # untaint $_
165
166check_taint_not 46, $_;
167
168/(b)/; # this must not taint
169check_taint_not 47, $&;
170check_taint_not 48, $`;
171check_taint_not 49, $';
172check_taint_not 50, $+;
173check_taint_not 51, $1;
174check_taint_not 52, $2;
175
176$_ = $a; # untaint $_
177
178check_taint_not 53, $_;
179
180$b = uc($a); # taint $b
181s/(.+)/$b/; # this must taint only the $_
182
183check_taint 54, $_;
184check_taint_not 55, $&;
185check_taint_not 56, $`;
186check_taint_not 57, $';
187check_taint_not 58, $+;
188check_taint_not 59, $1;
189check_taint_not 60, $2;
190
191$_ = $a; # untaint $_
192
193s/(.+)/b/; # this must not taint
194check_taint_not 61, $_;
195check_taint_not 62, $&;
196check_taint_not 63, $`;
197check_taint_not 64, $';
198check_taint_not 65, $+;
199check_taint_not 66, $1;
200check_taint_not 67, $2;
201
202$b = $a; # untaint $b
203
204($b = $a) =~ s/\w/$&/;
205check_taint 68, $b; # $b should be tainted.
206check_taint_not 69, $a; # $a should be not.
207
208$_ = $a; # untaint $_
209
210s/(\w)/\l$1/; # this must taint
211check_taint 70, $_;
212check_taint 71, $&;
213check_taint 72, $`;
214check_taint 73, $';
215check_taint 74, $+;
216check_taint 75, $1;
217check_taint_not 76, $2;
218
219$_ = $a; # untaint $_
220
221s/(\w)/\L$1/; # this must taint
222check_taint 77, $_;
223check_taint 78, $&;
224check_taint 79, $`;
225check_taint 80, $';
226check_taint 81, $+;
227check_taint 82, $1;
228check_taint_not 83, $2;
229
230$_ = $a; # untaint $_
231
232s/(\w)/\u$1/; # this must taint
233check_taint 84, $_;
234check_taint 85, $&;
235check_taint 86, $`;
236check_taint 87, $';
237check_taint 88, $+;
238check_taint 89, $1;
239check_taint_not 90, $2;
240
241$_ = $a; # untaint $_
242
243s/(\w)/\U$1/; # this must taint
244check_taint 91, $_;
245check_taint 92, $&;
246check_taint 93, $`;
247check_taint 94, $';
248check_taint 95, $+;
249check_taint 96, $1;
250check_taint_not 97, $2;
251
252# After all this tainting $a should be cool.
253
254check_taint_not 98, $a;
255
906f284f
NC
256sub last_without_setlocale { 98 }
257
8ebc5c01
PP
258# I think we've seen quite enough of taint.
259# Let us do some *real* locale work now,
284102e8 260# unless setlocale() is missing (i.e. minitest).
8ebc5c01
PP
261
262exit unless $have_setlocale;
263
284102e8
JH
264# Find locales.
265
6be75cd7
JH
266debug "# Scanning for locales...\n";
267
268# Note that it's okay that some languages have their native names
269# capitalized here even though that's not "right". They are lowercased
270# anyway later during the scanning process (and besides, some clueless
98dc9551 271# vendor might have them capitalized erroneously anyway).
6be75cd7 272
284102e8 273my $locales = <<EOF;
6be75cd7 274Afrikaans:af:za:1 15
284102e8 275Arabic:ar:dz eg sa:6 arabic8
6be75cd7
JH
276Brezhoneg Breton:br:fr:1 15
277Bulgarski Bulgarian:bg:bg:5
dd8482fc 278Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
6be75cd7
JH
279Hrvatski Croatian:hr:hr:2
280Cymraeg Welsh:cy:cy:1 14 15
284102e8 281Czech:cs:cz:2
6be75cd7
JH
282Dansk Danish:dk:da:1 15
283Nederlands Dutch:nl:be nl:1 15
dd8482fc 284English American British:en:au ca gb ie nz us uk zw:1 15 cp850
6be75cd7
JH
285Esperanto:eo:eo:3
286Eesti Estonian:et:ee:4 6 13
287Suomi Finnish:fi:fi:1 15
288Flamish::fl:1 15
6be75cd7
JH
289Deutsch German:de:at be ch de lu:1 15
290Euskaraz Basque:eu:es fr:1 15
6be75cd7
JH
291Galego Galician:gl:es:1 15
292Ellada Greek:el:gr:7 g8
6be75cd7
JH
293Frysk:fy:nl:1 15
294Greenlandic:kl:gl:4 6
284102e8
JH
295Hebrew:iw:il:8 hebrew8
296Hungarian:hu:hu:2
6be75cd7
JH
297Indonesian:in:id:1 15
298Gaeilge Irish:ga:IE:1 14 15
299Italiano Italian:it:ch it:1 15
300Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
284102e8 301Korean:ko:kr:
6be75cd7
JH
302Latine Latin:la:va:1 15
303Latvian:lv:lv:4 6 13
304Lithuanian:lt:lt:4 6 13
305Macedonian:mk:mk:1 15
306Maltese:mt:mt:3
dd8482fc
JH
307Moldovan:mo:mo:2
308Norsk Norwegian:no no\@nynorsk:no:1 15
6be75cd7
JH
309Occitan:oc:es:1 15
310Polski Polish:pl:pl:2
284102e8 311Rumanian:ro:ro:2
a528dad0 312Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
6be75cd7 313Serbski Serbian:sr:yu:5
284102e8 314Slovak:sk:sk:2
6be75cd7 315Slovene Slovenian:sl:si:2
d43ce814
JH
316Sqhip Albanian:sq:sq:1 15
317Svenska Swedish:sv:fi se:1 15
6be75cd7 318Thai:th:th:11 tis620
284102e8 319Turkish:tr:tr:9 turkish8
dd8482fc 320Yiddish:yi::1 15
284102e8
JH
321EOF
322
ee50adbe 323if ($^O eq 'os390') {
dd8482fc 324 # These cause heartburn. Broken locales?
ee50adbe
PP
325 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
326 $locales =~ s/Thai:th:th:11 tis620\n//;
327}
328
ef4a39e5 329sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
f9cbebe1
JH
330
331if (in_utf8) {
8a6cb2cb 332 require "lib/locale/utf8";
f9cbebe1 333} else {
8a6cb2cb 334 require "lib/locale/latin1";
f9cbebe1
JH
335}
336
284102e8
JH
337my @Locale;
338my $Locale;
339my @Alnum_;
340
0e053d1e
JH
341my @utf8locale;
342my %utf8skip;
343
284102e8 344sub getalnum_ {
8ebc5c01
PP
345 sort grep /\w/, map { chr } 0..255
346}
347
284102e8
JH
348sub trylocale {
349 my $locale = shift;
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
PP
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
PP
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
PP
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";
504 @Alnum_ = getalnum_();
db4b7445 505 debug "# w = ", join("",@Alnum_), "\n";
284102e8
JH
506
507 unless (setlocale(LC_ALL, $Locale)) {
508 foreach (99..103) {
509 $Problem{$_}{$Locale} = -1;
8ebc5c01 510 }
284102e8 511 next;
8ebc5c01 512 }
8ebc5c01 513
284102e8
JH
514 # Sieve the uppercase and the lowercase.
515
097ee67d
JH
516 my %UPPER = ();
517 my %lower = ();
518 my %BoThCaSe = ();
284102e8
JH
519 for (@Alnum_) {
520 if (/[^\d_]/) { # skip digits and the _
521 if (uc($_) eq $_) {
522 $UPPER{$_} = $_;
523 }
524 if (lc($_) eq $_) {
525 $lower{$_} = $_;
526 }
527 }
528 }
529 foreach (keys %UPPER) {
097ee67d 530 $BoThCaSe{$_}++ if exists $lower{$_};
284102e8
JH
531 }
532 foreach (keys %lower) {
097ee67d 533 $BoThCaSe{$_}++ if exists $UPPER{$_};
284102e8 534 }
097ee67d 535 foreach (keys %BoThCaSe) {
284102e8
JH
536 delete $UPPER{$_};
537 delete $lower{$_};
538 }
539
db4b7445
A
540 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
541 debug "# lower = ", join("", sort keys %lower ), "\n";
542 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
284102e8
JH
543
544 # Find the alphabets that are not alphabets in the default locale.
8ebc5c01 545
284102e8
JH
546 {
547 no locale;
8ebc5c01 548
284102e8
JH
549 @Neoalpha = ();
550 for (keys %UPPER, keys %lower) {
551 push(@Neoalpha, $_) if (/\W/);
d8093b23 552 $Neoalpha{$_} = $_;
284102e8 553 }
8ebc5c01 554 }
8ebc5c01 555
284102e8 556 @Neoalpha = sort @Neoalpha;
8ebc5c01 557
db4b7445 558 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
8ebc5c01 559
284102e8
JH
560 if (@Neoalpha == 0) {
561 # If we have no Neoalphas the remaining tests are no-ops.
6be75cd7 562 debug "# no Neoalpha, skipping tests 99..102 for locale '$Locale'\n";
a88c3d7c
GS
563 foreach (99..102) {
564 push @{$Okay{$_}}, $Locale;
565 }
6be75cd7 566 } else {
8ebc5c01 567
6be75cd7 568 # Test \w.
284102e8 569
ef4a39e5 570 my $word = join('', @Neoalpha);
8ebc5c01 571
5fef21a3
JH
572 my $badutf8;
573 {
574 local $SIG{__WARN__} = sub {
575 $badutf8 = $_[0] =~ /Malformed UTF-8/;
576 };
577 $Locale =~ /utf-?8/i;
578 }
579
580 if ($badutf8) {
581 debug "# Locale name contains bad UTF-8, skipping test 99 for locale '$Locale'\n";
582 } elsif ($Locale =~ /utf-?8/i) {
ef4a39e5
JH
583 debug "# unknown whether locale and Unicode have the same \\w, skipping test 99 for locale '$Locale'\n";
584 push @{$Okay{99}}, $Locale;
585 } else {
586 if ($word =~ /^(\w+)$/) {
587 tryneoalpha($Locale, 99, 1);
588 } else {
589 tryneoalpha($Locale, 99, 0);
590 }
8ac0d9e6 591 }
ef4a39e5 592
2a680da6 593 # Cross-check the whole 8-bit character set.
8ebc5c01 594
6be75cd7 595 for (map { chr } 0..255) {
2a680da6
JH
596 tryneoalpha($Locale, 100,
597 (/\w/ xor /\W/) ||
598 (/\d/ xor /\D/) ||
599 (/\s/ xor /\S/));
284102e8 600 }
8ebc5c01 601
6be75cd7 602 # Test for read-only scalars' locale vs non-locale comparisons.
284102e8 603
284102e8 604 {
6be75cd7
JH
605 no locale;
606 $a = "qwerty";
607 {
608 use locale;
2a680da6 609 tryneoalpha($Locale, 101, ($a cmp "qwerty") == 0);
8ebc5c01
PP
610 }
611 }
8ebc5c01 612
6be75cd7
JH
613 {
614 my ($from, $to, $lesser, $greater,
615 @test, %test, $test, $yes, $no, $sign);
616
617 for (0..9) {
618 # Select a slice.
619 $from = int(($_*@Alnum_)/10);
620 $to = $from + int(@Alnum_/10);
621 $to = $#Alnum_ if ($to > $#Alnum_);
622 $lesser = join('', @Alnum_[$from..$to]);
623 # Select a slice one character on.
624 $from++; $to++;
625 $to = $#Alnum_ if ($to > $#Alnum_);
626 $greater = join('', @Alnum_[$from..$to]);
627 ($yes, $no, $sign) = ($lesser lt $greater
628 ? (" ", "not ", 1)
629 : ("not ", " ", -1));
630 # all these tests should FAIL (return 0).
631 # Exact lt or gt cannot be tested because
632 # in some locales, say, eacute and E may test equal.
633 @test =
634 (
635 $no.' ($lesser le $greater)', # 1
636 'not ($lesser ne $greater)', # 2
637 ' ($lesser eq $greater)', # 3
638 $yes.' ($lesser ge $greater)', # 4
639 $yes.' ($lesser ge $greater)', # 5
640 $yes.' ($greater le $lesser )', # 7
641 'not ($greater ne $lesser )', # 8
642 ' ($greater eq $lesser )', # 9
643 $no.' ($greater ge $lesser )', # 10
0e053d1e 644 'not (($lesser cmp $greater) == -($sign))' # 11
6be75cd7
JH
645 );
646 @test{@test} = 0 x @test;
647 $test = 0;
284102e8 648 for my $ti (@test) {
6be75cd7
JH
649 $test{$ti} = eval $ti;
650 $test ||= $test{$ti}
284102e8 651 }
2a680da6 652 tryneoalpha($Locale, 102, $test == 0);
6be75cd7 653 if ($test) {
6be75cd7
JH
654 debug "# lesser = '$lesser'\n";
655 debug "# greater = '$greater'\n";
656 debug "# lesser cmp greater = ",
657 $lesser cmp $greater, "\n";
658 debug "# greater cmp lesser = ",
659 $greater cmp $lesser, "\n";
660 debug "# (greater) from = $from, to = $to\n";
661 for my $ti (@test) {
662 debugf("# %-40s %-4s", $ti,
663 $test{$ti} ? 'FAIL' : 'ok');
664 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
665 debugf("(%s == %4d)", $1, eval $1);
666 }
667 debug "\n#";
668 }
284102e8 669
6be75cd7
JH
670 last;
671 }
284102e8 672 }
8ebc5c01
PP
673 }
674 }
6be75cd7
JH
675
676 use locale;
677
678 my ($x, $y) = (1.23, 1.23);
679
0e053d1e 680 $a = "$x";
6be75cd7 681 printf ''; # printf used to reset locale to "C"
0e053d1e 682 $b = "$y";
6be75cd7 683
2a680da6
JH
684 debug "# 103..107: a = $a, b = $b, Locale = $Locale\n";
685
686 tryneoalpha($Locale, 103, $a eq $b);
6be75cd7
JH
687
688 my $c = "$x";
689 my $z = sprintf ''; # sprintf used to reset locale to "C"
690 my $d = "$y";
691
2a680da6 692 debug "# 104..107: c = $c, d = $d, Locale = $Locale\n";
6be75cd7 693
2a680da6 694 tryneoalpha($Locale, 104, $c eq $d);
6be75cd7 695
2a680da6 696 {
9f1b1f2d 697 use warnings;
2a680da6 698 my $w = 0;
0e053d1e
JH
699 local $SIG{__WARN__} =
700 sub {
906f284f 701 print "# @_\n";
0e053d1e
JH
702 $w++;
703 };
6be75cd7 704
0e053d1e
JH
705 # The == (among other ops) used to warn for locales
706 # that had something else than "." as the radix character.
6be75cd7 707
2a680da6 708 tryneoalpha($Locale, 105, $c == 1.23);
6be75cd7 709
2a680da6 710 tryneoalpha($Locale, 106, $c == $x);
6be75cd7 711
2a680da6 712 tryneoalpha($Locale, 107, $c == $d);
6be75cd7 713
2a680da6 714 {
2de3dbcc 715# no locale; # XXX did this ever work correctly?
6be75cd7 716
2a680da6 717 my $e = "$x";
6be75cd7 718
2a680da6 719 debug "# 108..110: e = $e, Locale = $Locale\n";
6be75cd7 720
2a680da6 721 tryneoalpha($Locale, 108, $e == 1.23);
6be75cd7 722
2a680da6
JH
723 tryneoalpha($Locale, 109, $e == $x);
724
725 tryneoalpha($Locale, 110, $e == $c);
6be75cd7 726 }
2a680da6 727
2a680da6 728 my $f = "1.23";
906f284f 729 my $g = 2.34;
2a680da6 730
906f284f 731 debug "# 111..115: f = $f, g = $g, locale = $Locale\n";
2a680da6 732
906f284f 733 tryneoalpha($Locale, 111, $f == 1.23);
6be75cd7 734
906f284f 735 tryneoalpha($Locale, 112, $f == $x);
2a680da6 736
906f284f
NC
737 tryneoalpha($Locale, 113, $f == $c);
738
739 tryneoalpha($Locale, 114, abs(($f + $g) - 3.57) < 0.01);
740
741 tryneoalpha($Locale, 115, $w == 0);
6be75cd7
JH
742 }
743
26d80d95
LC
744 # Does taking lc separately differ from taking
745 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
746 # The bug was in the caching of the 'o'-magic.
2a680da6
JH
747 {
748 use locale;
6be75cd7 749
2a680da6
JH
750 sub lcA {
751 my $lc0 = lc $_[0];
752 my $lc1 = lc $_[1];
753 return $lc0 cmp $lc1;
754 }
6be75cd7 755
2a680da6
JH
756 sub lcB {
757 return lc($_[0]) cmp lc($_[1]);
758 }
6be75cd7 759
2a680da6
JH
760 my $x = "ab";
761 my $y = "aa";
762 my $z = "AB";
6be75cd7 763
906f284f 764 tryneoalpha($Locale, 116,
2a680da6
JH
765 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
766 lcA($x, $z) == 0 && lcB($x, $z) == 0);
6be75cd7 767 }
d8093b23 768
26d80d95
LC
769 # Does lc of an UPPER (if different from the UPPER) match
770 # case-insensitively the UPPER, and does the UPPER match
771 # case-insensitively the lc of the UPPER. And vice versa.
3ba0e062 772 {
ef4a39e5
JH
773 use locale;
774 no utf8;
775 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
776
777 my @f = ();
778 foreach my $x (keys %UPPER) {
779 my $y = lc $x;
780 next unless uc $y eq $x;
781 print "# UPPER $x lc $y ",
782 $x =~ /$y/i ? 1 : 0, " ",
783 $y =~ /$x/i ? 1 : 0, "\n" if 0;
83d38f53 784 #
ef4a39e5
JH
785 # If $x and $y contain regular expression characters
786 # AND THEY lowercase (/i) to regular expression characters,
787 # regcomp() will be mightily confused. No, the \Q doesn't
788 # help here (maybe regex engine internal lowercasing
789 # is done after the \Q?) An example of this happening is
790 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
791 # the chr(173) (the "[") is the lowercase of the chr(235).
83d38f53 792 #
ef4a39e5
JH
793 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
794 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
795 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
796 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
797 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
798 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
83d38f53
JH
799 #
800 # Similar things can happen even under (bastardised)
801 # non-EBCDIC locales: in many European countries before the
802 # advent of ISO 8859-x nationally customised versions of
803 # ISO 646 were devised, reusing certain punctuation
804 # characters for modified characters needed by the
805 # country/language. For example, the "|" might have
806 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
807 #
ef4a39e5
JH
808 if ($x =~ $re || $y =~ $re) {
809 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
810 next;
8ac0d9e6 811 }
ef4a39e5
JH
812 # With utf8 both will fail since the locale concept
813 # of upper/lower does not work well in Unicode.
814 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
c00ff1c7 815 }
ef4a39e5 816
c00ff1c7
DM
817 foreach my $x (keys %lower) {
818 my $y = uc $x;
819 next unless lc $y eq $x;
820 print "# lower $x uc $y ",
821 $x =~ /$y/i ? 1 : 0, " ",
822 $y =~ /$x/i ? 1 : 0, "\n" if 0;
823 if ($x =~ $re || $y =~ $re) { # See above.
824 print "# Regex characters in '$x' or '$y', skipping test 117 for locale '$Locale'\n";
825 next;
8ac0d9e6 826 }
c00ff1c7
DM
827 # With utf8 both will fail since the locale concept
828 # of upper/lower does not work well in Unicode.
829 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
830 }
831 tryneoalpha($Locale, 117, @f == 0);
832 if (@f) {
833 print "# failed 117 locale '$Locale' characters @f\n"
834 }
d8093b23 835 }
8ebc5c01 836}
284102e8 837
2a680da6
JH
838# Recount the errors.
839
98daf176 840foreach (&last_without_setlocale()+1..$last_locales) {
2a680da6 841 if ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
097ee67d
JH
842 if ($_ == 102) {
843 print "# The failure of test 102 is not necessarily fatal.\n";
b4e009be 844 print "# It usually indicates a problem in the environment,\n";
284102e8
JH
845 print "# not in Perl itself.\n";
846 }
847 print "not ";
8ebc5c01 848 }
284102e8 849 print "ok $_\n";
8ebc5c01 850}
fb73857a 851
2a680da6
JH
852# Give final advice.
853
284102e8
JH
854my $didwarn = 0;
855
98daf176 856foreach (99..$last_locales) {
284102e8
JH
857 if ($Problem{$_}) {
858 my @f = sort keys %{ $Problem{$_} };
859 my $f = join(" ", @f);
860 $f =~ s/(.{50,60}) /$1\n#\t/g;
2a680da6
JH
861 print
862 "#\n",
863 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
284102e8
JH
864 "#\t", $f, "\n#\n",
865 "# on your system may have errors because the locale test $_\n",
866 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
867 ".\n";
2a680da6 868 print <<EOW;
284102e8
JH
869#
870# If your users are not using these locales you are safe for the moment,
871# but please report this failure first to perlbug\@perl.com using the
872# perlbug script (as described in the INSTALL file) so that the exact
873# details of the failures can be sorted out first and then your operating
874# system supplier can be alerted about these anomalies.
875#
876EOW
877 $didwarn = 1;
fb73857a
PP
878 }
879}
774d564b 880
26d80d95 881# Tell which locales were okay and which were not.
2a680da6 882
284102e8 883if ($didwarn) {
26d80d95 884 my (@s, @F);
284102e8
JH
885
886 foreach my $l (@Locale) {
887 my $p = 0;
98daf176 888 foreach my $t (102..$last_locales) {
284102e8 889 $p++ if $Problem{$t}{$l};
8ebc5c01 890 }
284102e8 891 push @s, $l if $p == 0;
26d80d95 892 push @F, $l unless $p == 0;
8ebc5c01 893 }
284102e8 894
68d47915
CK
895 if (@s) {
896 my $s = join(" ", @s);
897 $s =~ s/(.{50,60}) /$1\n#\t/g;
898
899 warn
900 "# The following locales\n#\n",
901 "#\t", $s, "\n#\n",
902 "# tested okay.\n#\n",
903 } else {
26d80d95
LC
904 warn "# None of your locales were fully okay.\n";
905 }
906
907 if (@F) {
908 my $F = join(" ", @F);
909 $F =~ s/(.{50,60}) /$1\n#\t/g;
910
911 warn
912 "# The following locales\n#\n",
0e053d1e 913 "#\t", $F, "\n#\n",
26d80d95
LC
914 "# had problems.\n#\n",
915 } else {
916 warn "# None of your locales were broken.\n";
68d47915 917 }
0e053d1e 918
c406fbf4
JH
919 if (@utf8locale) {
920 my $S = join(" ", @utf8locale);
921 $S =~ s/(.{50,60}) /$1\n#\t/g;
922
923 warn "#\n# The following locales\n#\n",
924 "#\t", $S, "\n#\n",
925 "# were skipped for the tests ",
926 join(" ", sort {$a<=>$b} keys %utf8skip), "\n",
927 "# because UTF-8 and locales do not work together in Perl.\n#\n";
928 }
8ebc5c01 929}
90248788 930
98daf176
KW
931sub last_locales { 117 }
932
c213d471
KW
933$test_num = $last_locales;
934my $i = $test_num + 1;
935
094a2f8c
KW
936# Test that tainting and case changing works on utf8 strings. These tests are
937# placed last to avoid disturbing the hard-coded test numbers above this in
938# this file.
939setlocale(LC_ALL, "C");
940{
941 use locale;
942
094a2f8c
KW
943 foreach my $function ("uc", "ucfirst", "lc", "lcfirst") {
944 my @list; # List of code points to test for $function
945
946 # Used to calculate the changed case for ASCII characters by using the
947 # ord, instead of using one of the functions under test.
948 my $ascii_case_change_delta;
949 my $above_latin1_case_change_delta; # Same for the specific ords > 255
950 # that we use
951
952 # We test an ASCII character, which should change case and be tainted;
953 # a Latin1 character, which shouldn't change case under this C locale,
954 # and is tainted.
955 # an above-Latin1 character that when the case is changed would cross
956 # the 255/256 boundary, so doesn't change case and isn't tainted
957 # (the \x{149} is one of these, but changes into 2 characters, the
958 # first one of which doesn't cross the boundary.
959 # the final one in each list is an above-Latin1 character whose case
960 # does change, and shouldn't be tainted. The code below uses its
961 # position in its list as a marker to indicate that it, unlike the
962 # other code points above ASCII, has a successful case change
963 if ($function =~ /^u/) {
964 #@list = ("\xff", "\x{fb00}", "\x{149}", "\x{101}");
965 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
966 $ascii_case_change_delta = -32;
967 $above_latin1_case_change_delta = -1;
968 }
969 else {
970 @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}");
971 $ascii_case_change_delta = +32;
972 $above_latin1_case_change_delta = +1;
973 }
974 $|=1;
975 foreach my $j (0 .. $#list) {
976 my $char = $list[$j];
977 #print STDERR __LINE__, ": $char\n";
978 #check_taint_not($i++, $char);
979 utf8::upgrade($char);
980 #check_taint_not($i++, $char);
981 my $should_be = ($j == $#list)
982 ? chr(ord($char) + $above_latin1_case_change_delta)
983 : (length $char == 0 || ord($char) > 127)
984 ? $char
985 : chr(ord($char) + $ascii_case_change_delta);
986
987 # This monstrosity is in order to avoid using an eval, which might
988 # perturb the results
989 my $changed = ($function eq "uc")
990 ? uc($char)
991 : ($function eq "ucfirst")
992 ? ucfirst($char)
993 : ($function eq "lc")
994 ? lc($char)
995 : ($function eq "lcfirst")
996 ? lcfirst($char)
997 : croak("Unexpected function \"$function\"");
c213d471 998 ok($changed eq $should_be, "$function(\"$char\") in C locale should be \"$should_be\", got \"$changed\"");
094a2f8c
KW
999
1000 # Tainting shouldn't happen for empty strings, or those characters
1001 # above 255.
1002 #print STDERR __LINE__, ": $char\n";
1003 (length($char) > 0 && ord($char) < 256)
1004 ? check_taint($i++, $changed)
1005 : check_taint_not($i++, $changed);
1006 }
1007 }
1008}
1009
1010
1011sub last { 165 }
906f284f 1012
90248788 1013# eof