This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
lib/locale.t: Change variable name for accuracy
[perl5.git] / lib / locale.t
CommitLineData
8ebc5c01 1#!./perl -wT
2
66cbab2c
KW
3# This tests plain 'use locale' and adorned 'use locale ":not_characters"'
4# Because these pragmas are compile time, and I (khw) am trying to test
5# without using 'eval' as much as possible, which might cloud the issue, the
6# crucial parts of the code are duplicated in a block for each pragma.
7
6c2e653d
KW
8# To make a TODO test, add the string 'TODO' to its %test_names value
9
e3a2734b
KW
10binmode STDOUT, ':utf8';
11binmode STDERR, ':utf8';
12
8ebc5c01 13BEGIN {
14 chdir 't' if -d 't';
20822f61 15 @INC = '../lib';
f9cbebe1 16 unshift @INC, '.';
b002077a 17 require Config; import Config;
97a0514d 18 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
b002077a
CS
19 print "1..0\n";
20 exit;
21 }
2de3dbcc 22 $| = 1;
8ebc5c01 23}
24
25use strict;
26c1569f 26use feature 'fc';
8ebc5c01 27
108a305e 28my $debug = $ENV{PERL_DEBUG_FULL_TEST} // 0;
284102e8 29
6d5d702a
KW
30# Certain tests have been shown to be problematical for a few locales. Don't
31# fail them unless at least this percentage of the tested locales fail.
32my $acceptable_fold_failure_percentage = 5;
33
db4b7445
A
34use Dumpvalue;
35
36my $dumper = Dumpvalue->new(
37 tick => qq{"},
38 quoteHighBit => 0,
39 unctrl => "quote"
40 );
6be75cd7 41sub debug {
db4b7445
A
42 return unless $debug;
43 my($mess) = join "", @_;
44 chop $mess;
45 print $dumper->stringify($mess,1), "\n";
6be75cd7
JH
46}
47
48sub debugf {
49 printf @_ if $debug;
50}
51
8ebc5c01 52my $have_setlocale = 0;
53eval {
54 require POSIX;
55 import POSIX ':locale_h';
56 $have_setlocale++;
57};
58
6dead956 59# Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
f6c6487a 60# and mingw32 uses said silly CRT
3a2d1764
SH
61# This doesn't seem to be an issue any more, at least on Windows XP,
62# so re-enable the tests for Windows XP onwards.
63my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
64 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
65$have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
66 $Config{cc} =~ /^(cl|gcc)/i);
6dead956 67
36a42ae7 68# UWIN seems to loop after taint tests, just skip for now
cd19b65c
JH
69$have_setlocale = 0 if ($^O =~ /^uwin/);
70
0e053d1e 71$a = 'abc %';
8ebc5c01 72
c213d471
KW
73my $test_num = 0;
74
8ebc5c01 75sub ok {
c213d471 76 my ($result, $message) = @_;
e3a2734b 77 $message = "" unless defined $message;
8ebc5c01 78
79 print 'not ' unless ($result);
c213d471 80 print "ok " . ++$test_num;
e3a2734b
KW
81 print " $message";
82 print "\n";
8ebc5c01 83}
84
85# First we'll do a lot of taint checking for locales.
86# This is the easiest to test, actually, as any locale,
87# even the default locale will taint under 'use locale'.
88
89sub is_tainted { # hello, camel two.
9f1b1f2d 90 no warnings 'uninitialized' ;
8ebc5c01 91 my $dummy;
ba74571d 92 local $@;
8ebc5c01 93 not eval { $dummy = join("", @_), kill 0; 1 }
94}
95
a9b7c637
KW
96sub check_taint ($;$) {
97 my $message_tail = $_[1] // "";
98 $message_tail = ": $message_tail" if $message_tail;
99 ok is_tainted($_[0]), "verify that is tainted$message_tail";
8ebc5c01 100}
101
a9b7c637
KW
102sub check_taint_not ($;$) {
103 my $message_tail = $_[1] // "";
104 $message_tail = ": $message_tail" if $message_tail;
105 ok((not is_tainted($_[0])), "verify that isn't tainted$message_tail");
8ebc5c01 106}
107
bf3cd0e6
KW
108"\tb\t" =~ /^m?(\s)(.*)\1$/;
109check_taint_not $&, "not tainted outside 'use locale'";
110;
111
8ebc5c01 112use locale; # engage locale and therefore locale taint.
113
36a42ae7 114check_taint_not $a;
8ebc5c01 115
36a42ae7
KW
116check_taint uc($a);
117check_taint "\U$a";
118check_taint ucfirst($a);
119check_taint "\u$a";
120check_taint lc($a);
26c1569f 121check_taint fc($a);
36a42ae7 122check_taint "\L$a";
26c1569f 123check_taint "\F$a";
36a42ae7
KW
124check_taint lcfirst($a);
125check_taint "\l$a";
8ebc5c01 126
36a42ae7
KW
127check_taint_not sprintf('%e', 123.456);
128check_taint_not sprintf('%f', 123.456);
129check_taint_not sprintf('%g', 123.456);
130check_taint_not sprintf('%d', 123.456);
131check_taint_not sprintf('%x', 123.456);
8ebc5c01 132
133$_ = $a; # untaint $_
134
135$_ = uc($a); # taint $_
136
36a42ae7 137check_taint $_;
8ebc5c01 138
139/(\w)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
140check_taint $&;
141check_taint $`;
142check_taint $';
143check_taint $+;
144check_taint $1;
145check_taint_not $2;
8ebc5c01 146
147/(.)/; # untaint $&, $`, $', $+, $1.
36a42ae7
KW
148check_taint_not $&;
149check_taint_not $`;
150check_taint_not $';
151check_taint_not $+;
152check_taint_not $1;
153check_taint_not $2;
8ebc5c01 154
155/(\W)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
156check_taint $&;
157check_taint $`;
158check_taint $';
159check_taint $+;
160check_taint $1;
161check_taint_not $2;
8ebc5c01 162
163/(\s)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
164check_taint $&;
165check_taint $`;
166check_taint $';
167check_taint $+;
168check_taint $1;
169check_taint_not $2;
8ebc5c01 170
171/(\S)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
172check_taint $&;
173check_taint $`;
174check_taint $';
175check_taint $+;
176check_taint $1;
177check_taint_not $2;
8ebc5c01 178
179$_ = $a; # untaint $_
180
36a42ae7 181check_taint_not $_;
8ebc5c01 182
183/(b)/; # this must not taint
36a42ae7
KW
184check_taint_not $&;
185check_taint_not $`;
186check_taint_not $';
187check_taint_not $+;
188check_taint_not $1;
189check_taint_not $2;
8ebc5c01 190
191$_ = $a; # untaint $_
192
36a42ae7 193check_taint_not $_;
8ebc5c01 194
195$b = uc($a); # taint $b
196s/(.+)/$b/; # this must taint only the $_
197
36a42ae7
KW
198check_taint $_;
199check_taint_not $&;
200check_taint_not $`;
201check_taint_not $';
202check_taint_not $+;
203check_taint_not $1;
204check_taint_not $2;
8ebc5c01 205
206$_ = $a; # untaint $_
207
208s/(.+)/b/; # this must not taint
36a42ae7
KW
209check_taint_not $_;
210check_taint_not $&;
211check_taint_not $`;
212check_taint_not $';
213check_taint_not $+;
214check_taint_not $1;
215check_taint_not $2;
8ebc5c01 216
217$b = $a; # untaint $b
218
219($b = $a) =~ s/\w/$&/;
36a42ae7
KW
220check_taint $b; # $b should be tainted.
221check_taint_not $a; # $a should be not.
8ebc5c01 222
223$_ = $a; # untaint $_
224
225s/(\w)/\l$1/; # this must taint
36a42ae7
KW
226check_taint $_;
227check_taint $&;
228check_taint $`;
229check_taint $';
230check_taint $+;
231check_taint $1;
232check_taint_not $2;
8ebc5c01 233
234$_ = $a; # untaint $_
235
236s/(\w)/\L$1/; # this must taint
36a42ae7
KW
237check_taint $_;
238check_taint $&;
239check_taint $`;
240check_taint $';
241check_taint $+;
242check_taint $1;
243check_taint_not $2;
8ebc5c01 244
245$_ = $a; # untaint $_
246
247s/(\w)/\u$1/; # this must taint
36a42ae7
KW
248check_taint $_;
249check_taint $&;
250check_taint $`;
251check_taint $';
252check_taint $+;
253check_taint $1;
254check_taint_not $2;
8ebc5c01 255
256$_ = $a; # untaint $_
257
258s/(\w)/\U$1/; # this must taint
36a42ae7
KW
259check_taint $_;
260check_taint $&;
261check_taint $`;
262check_taint $';
263check_taint $+;
264check_taint $1;
265check_taint_not $2;
8ebc5c01 266
267# After all this tainting $a should be cool.
268
36a42ae7 269check_taint_not $a;
8ebc5c01 270
66cbab2c
KW
271{ # This is just the previous tests copied here with a different
272 # compile-time pragma.
273
274 use locale ':not_characters'; # engage restricted locale with different
275 # tainting rules
276
277 check_taint_not $a;
278
279 check_taint_not uc($a);
280 check_taint_not "\U$a";
281 check_taint_not ucfirst($a);
282 check_taint_not "\u$a";
283 check_taint_not lc($a);
26c1569f 284 check_taint_not fc($a);
66cbab2c 285 check_taint_not "\L$a";
26c1569f 286 check_taint_not "\F$a";
66cbab2c
KW
287 check_taint_not lcfirst($a);
288 check_taint_not "\l$a";
289
290 check_taint_not sprintf('%e', 123.456);
291 check_taint_not sprintf('%f', 123.456);
292 check_taint_not sprintf('%g', 123.456);
293 check_taint_not sprintf('%d', 123.456);
294 check_taint_not sprintf('%x', 123.456);
295
296 $_ = $a; # untaint $_
297
298 $_ = uc($a); # taint $_
299
300 check_taint_not $_;
301
302 /(\w)/; # taint $&, $`, $', $+, $1.
303 check_taint_not $&;
304 check_taint_not $`;
305 check_taint_not $';
306 check_taint_not $+;
307 check_taint_not $1;
308 check_taint_not $2;
309
310 /(.)/; # untaint $&, $`, $', $+, $1.
311 check_taint_not $&;
312 check_taint_not $`;
313 check_taint_not $';
314 check_taint_not $+;
315 check_taint_not $1;
316 check_taint_not $2;
317
318 /(\W)/; # taint $&, $`, $', $+, $1.
319 check_taint_not $&;
320 check_taint_not $`;
321 check_taint_not $';
322 check_taint_not $+;
323 check_taint_not $1;
324 check_taint_not $2;
325
326 /(\s)/; # taint $&, $`, $', $+, $1.
327 check_taint_not $&;
328 check_taint_not $`;
329 check_taint_not $';
330 check_taint_not $+;
331 check_taint_not $1;
332 check_taint_not $2;
333
334 /(\S)/; # taint $&, $`, $', $+, $1.
335 check_taint_not $&;
336 check_taint_not $`;
337 check_taint_not $';
338 check_taint_not $+;
339 check_taint_not $1;
340 check_taint_not $2;
341
342 $_ = $a; # untaint $_
343
344 check_taint_not $_;
345
346 /(b)/; # this must not taint
347 check_taint_not $&;
348 check_taint_not $`;
349 check_taint_not $';
350 check_taint_not $+;
351 check_taint_not $1;
352 check_taint_not $2;
353
354 $_ = $a; # untaint $_
355
356 check_taint_not $_;
357
358 $b = uc($a); # taint $b
359 s/(.+)/$b/; # this must taint only the $_
360
361 check_taint_not $_;
362 check_taint_not $&;
363 check_taint_not $`;
364 check_taint_not $';
365 check_taint_not $+;
366 check_taint_not $1;
367 check_taint_not $2;
368
369 $_ = $a; # untaint $_
370
371 s/(.+)/b/; # this must not taint
372 check_taint_not $_;
373 check_taint_not $&;
374 check_taint_not $`;
375 check_taint_not $';
376 check_taint_not $+;
377 check_taint_not $1;
378 check_taint_not $2;
379
380 $b = $a; # untaint $b
381
382 ($b = $a) =~ s/\w/$&/;
383 check_taint_not $b; # $b should be tainted.
384 check_taint_not $a; # $a should be not.
385
386 $_ = $a; # untaint $_
387
388 s/(\w)/\l$1/; # this must taint
389 check_taint_not $_;
390 check_taint_not $&;
391 check_taint_not $`;
392 check_taint_not $';
393 check_taint_not $+;
394 check_taint_not $1;
395 check_taint_not $2;
396
397 $_ = $a; # untaint $_
398
399 s/(\w)/\L$1/; # this must taint
400 check_taint_not $_;
401 check_taint_not $&;
402 check_taint_not $`;
403 check_taint_not $';
404 check_taint_not $+;
405 check_taint_not $1;
406 check_taint_not $2;
407
408 $_ = $a; # untaint $_
409
410 s/(\w)/\u$1/; # this must taint
411 check_taint_not $_;
412 check_taint_not $&;
413 check_taint_not $`;
414 check_taint_not $';
415 check_taint_not $+;
416 check_taint_not $1;
417 check_taint_not $2;
418
419 $_ = $a; # untaint $_
420
421 s/(\w)/\U$1/; # this must taint
422 check_taint_not $_;
423 check_taint_not $&;
424 check_taint_not $`;
425 check_taint_not $';
426 check_taint_not $+;
427 check_taint_not $1;
428 check_taint_not $2;
429
430 # After all this tainting $a should be cool.
431
432 check_taint_not $a;
433}
434
435# Here are in scope of 'use locale'
436
8ebc5c01 437# I think we've seen quite enough of taint.
438# Let us do some *real* locale work now,
284102e8 439# unless setlocale() is missing (i.e. minitest).
8ebc5c01 440
fdf053ee
KW
441unless ($have_setlocale) {
442 print "1..$test_num\n";
443 exit;
444}
8ebc5c01 445
6cf0b567 446# The test number before our first setlocale()
66330f13 447my $final_without_setlocale = $test_num;
6cf0b567 448
284102e8
JH
449# Find locales.
450
6be75cd7
JH
451debug "# Scanning for locales...\n";
452
453# Note that it's okay that some languages have their native names
454# capitalized here even though that's not "right". They are lowercased
455# anyway later during the scanning process (and besides, some clueless
98dc9551 456# vendor might have them capitalized erroneously anyway).
6be75cd7 457
284102e8 458my $locales = <<EOF;
6be75cd7 459Afrikaans:af:za:1 15
284102e8 460Arabic:ar:dz eg sa:6 arabic8
6be75cd7
JH
461Brezhoneg Breton:br:fr:1 15
462Bulgarski Bulgarian:bg:bg:5
dd8482fc 463Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
6be75cd7
JH
464Hrvatski Croatian:hr:hr:2
465Cymraeg Welsh:cy:cy:1 14 15
284102e8 466Czech:cs:cz:2
df8a53a3 467Dansk Danish:da:dk:1 15
6be75cd7 468Nederlands Dutch:nl:be nl:1 15
dd8482fc 469English American British:en:au ca gb ie nz us uk zw:1 15 cp850
6be75cd7
JH
470Esperanto:eo:eo:3
471Eesti Estonian:et:ee:4 6 13
472Suomi Finnish:fi:fi:1 15
473Flamish::fl:1 15
6be75cd7
JH
474Deutsch German:de:at be ch de lu:1 15
475Euskaraz Basque:eu:es fr:1 15
6be75cd7
JH
476Galego Galician:gl:es:1 15
477Ellada Greek:el:gr:7 g8
6be75cd7
JH
478Frysk:fy:nl:1 15
479Greenlandic:kl:gl:4 6
284102e8
JH
480Hebrew:iw:il:8 hebrew8
481Hungarian:hu:hu:2
df8a53a3 482Indonesian:id:id:1 15
6be75cd7
JH
483Gaeilge Irish:ga:IE:1 14 15
484Italiano Italian:it:ch it:1 15
485Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
284102e8 486Korean:ko:kr:
6be75cd7
JH
487Latine Latin:la:va:1 15
488Latvian:lv:lv:4 6 13
489Lithuanian:lt:lt:4 6 13
490Macedonian:mk:mk:1 15
491Maltese:mt:mt:3
dd8482fc 492Moldovan:mo:mo:2
df8a53a3 493Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
6be75cd7
JH
494Occitan:oc:es:1 15
495Polski Polish:pl:pl:2
284102e8 496Rumanian:ro:ro:2
a528dad0 497Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
6be75cd7 498Serbski Serbian:sr:yu:5
284102e8 499Slovak:sk:sk:2
6be75cd7 500Slovene Slovenian:sl:si:2
d43ce814
JH
501Sqhip Albanian:sq:sq:1 15
502Svenska Swedish:sv:fi se:1 15
6be75cd7 503Thai:th:th:11 tis620
284102e8 504Turkish:tr:tr:9 turkish8
dd8482fc 505Yiddish:yi::1 15
284102e8
JH
506EOF
507
ee50adbe 508if ($^O eq 'os390') {
dd8482fc 509 # These cause heartburn. Broken locales?
ee50adbe
PP
510 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
511 $locales =~ s/Thai:th:th:11 tis620\n//;
512}
513
ef4a39e5 514sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
f9cbebe1
JH
515
516if (in_utf8) {
8a6cb2cb 517 require "lib/locale/utf8";
f9cbebe1 518} else {
8a6cb2cb 519 require "lib/locale/latin1";
f9cbebe1
JH
520}
521
284102e8
JH
522my @Locale;
523my $Locale;
aac995d2 524my @Digit_;
519c0534 525my @Word_;
284102e8 526
284102e8
JH
527sub trylocale {
528 my $locale = shift;
0b9f254b 529 return if grep { $locale eq $_ } @Locale;
a810e350 530 return unless setlocale(&POSIX::LC_ALL, $locale);
e439cacb
KW
531 my $badutf8;
532 {
533 local $SIG{__WARN__} = sub {
534 $badutf8 = $_[0] =~ /Malformed UTF-8/;
535 };
536 $Locale =~ /UTF-?8/i;
284102e8 537 }
e439cacb
KW
538
539 if ($badutf8) {
540 ok(0, "Locale name contains malformed utf8");
541 return;
542 }
543 push @Locale, $locale;
284102e8 544}
8ebc5c01 545
284102e8
JH
546sub decode_encodings {
547 my @enc;
8ebc5c01 548
284102e8
JH
549 foreach (split(/ /, shift)) {
550 if (/^(\d+)$/) {
551 push @enc, "ISO8859-$1";
552 push @enc, "iso8859$1"; # HP
553 if ($1 eq '1') {
554 push @enc, "roman8"; # HP
555 }
556 } else {
557 push @enc, $_;
dd8482fc 558 push @enc, "$_.UTF-8";
8ebc5c01 559 }
560 }
ee50adbe
PP
561 if ($^O eq 'os390') {
562 push @enc, qw(IBM-037 IBM-819 IBM-1047);
563 }
8ebc5c01 564
284102e8 565 return @enc;
8ebc5c01 566}
567
284102e8
JH
568trylocale("C");
569trylocale("POSIX");
570foreach (0..15) {
571 trylocale("ISO8859-$_");
284102e8 572 trylocale("iso8859$_");
097ee67d
JH
573 trylocale("iso8859-$_");
574 trylocale("iso_8859_$_");
575 trylocale("isolatin$_");
576 trylocale("isolatin-$_");
577 trylocale("iso_latin_$_");
8ebc5c01 578}
579
645e49ed
JH
580# Sanitize the environment so that we can run the external 'locale'
581# program without the taint mode getting grumpy.
cce5967e
JH
582
583# $ENV{PATH} is special in VMS.
584delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
585
586# Other subversive stuff.
587delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
dd8482fc 588
21477fb4 589if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
dd8482fc 590 while (<LOCALES>) {
d281a6ac
NC
591 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
592 # ain't great when we're running this testPERL_UNICODE= so that utf8
593 # locales will cause all IO hadles to default to (assume) utf8
594 next unless utf8::valid($_);
dd8482fc
JH
595 chomp;
596 trylocale($_);
284102e8 597 }
dd8482fc 598 close(LOCALES);
a6259068 599} elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
71e5cbb3 600# The SYS$I18N_LOCALE logical name search list was not present on
a6259068
PP
601# VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
602 opendir(LOCALES, "SYS\$I18N_LOCALE:");
603 while ($_ = readdir(LOCALES)) {
604 chomp;
605 trylocale($_);
606 }
607 close(LOCALES);
87e33296
SP
608} elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
609
610 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
611 # is much easier and faster than the last resort method.
612
613 opendir(LOCALES, '/usr/share/locale');
614 while ($_ = readdir(LOCALES)) {
615 chomp;
616 trylocale($_);
617 }
618 close(LOCALES);
dd8482fc
JH
619} else {
620
621 # This is going to be slow.
622
623 foreach my $locale (split(/\n/, $locales)) {
624 my ($locale_name, $language_codes, $country_codes, $encodings) =
625 split(/:/, $locale);
626 my @enc = decode_encodings($encodings);
627 foreach my $loc (split(/ /, $locale_name)) {
628 trylocale($loc);
284102e8 629 foreach my $enc (@enc) {
dd8482fc 630 trylocale("$loc.$enc");
284102e8 631 }
dd8482fc 632 $loc = lc $loc;
284102e8 633 foreach my $enc (@enc) {
dd8482fc
JH
634 trylocale("$loc.$enc");
635 }
636 }
637 foreach my $lang (split(/ /, $language_codes)) {
638 trylocale($lang);
639 foreach my $country (split(/ /, $country_codes)) {
640 my $lc = "${lang}_${country}";
641 trylocale($lc);
642 foreach my $enc (@enc) {
643 trylocale("$lc.$enc");
644 }
645 my $lC = "${lang}_\U${country}";
646 trylocale($lC);
647 foreach my $enc (@enc) {
648 trylocale("$lC.$enc");
649 }
284102e8
JH
650 }
651 }
652 }
653}
4599a1de 654
a810e350 655setlocale(&POSIX::LC_ALL, "C");
d43ce814 656
86f50d7d 657if ($^O eq 'darwin') {
4373e181 658 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
86f50d7d 659 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
4373e181
RGS
660 (my $v) = $Config{osvers} =~ /^(\d+)/;
661 if ($v >= 8 and $v < 10) {
86f50d7d 662 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
a5ec937f 663 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
dfa5c78f 664 } elsif ($v < 12) {
a44d0896
NC
665 debug "# Skipping be_BY locales -- buggy in Darwin\n";
666 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
a5ec937f 667 }
86f50d7d
DD
668}
669
4599a1de
JH
670@Locale = sort @Locale;
671
887ef7ed
PP
672debug "# Locales =\n";
673for ( @Locale ) {
674 debug "# $_\n";
675}
8ebc5c01 676
284102e8 677my %Problem;
2a680da6
JH
678my %Okay;
679my %Testing;
30032ef4 680my @Added_alpha; # Alphas that aren't in the C locale.
c08acc4c 681my %test_names;
284102e8 682
019bf7dd
KW
683sub display_characters {
684 # This returns a display string denoting the input parameter @_, each
685 # entry of which is a single character in the range 0-255. The first part
686 # of the output is a string of the characters in @_ that are ASCII
687 # graphics, and hence unambiguously displayable. They are given by code
688 # point order. The second part is the remaining code points, the ordinals
689 # of which are each displayed as 2-digit hex. Blanks are inserted so as
690 # to keep anything from the first part looking like a 2-digit hex number.
691
692 no locale;
693 my @chars = sort { ord $a <=> ord $b } @_;
694 my $output = "";
695 my $hex = "";
696 my $range_start;
697 my $start_class;
698 push @chars, chr(258); # This sentinel simplifies the loop termination
699 # logic
700 foreach my $i (0 .. @chars - 1) {
701 my $char = $chars[$i];
702 my $range_end;
703 my $class;
704
705 # We avoid using [:posix:] classes, as these are being tested in this
706 # file. Each equivalence class below is for things that can appear in
707 # a range; those that can't be in a range have class -1. 0 for those
708 # which should be output in hex; and >0 for the other ranges
709 if ($char =~ /[A-Z]/) {
710 $class = 2;
711 }
712 elsif ($char =~ /[a-z]/) {
713 $class = 3;
714 }
715 elsif ($char =~ /[0-9]/) {
716 $class = 4;
717 }
718 elsif ($char =~ /[[\]!"#\$\%&\'()*+,.\/:\\;<=>?\@\^_`{|}~-]/) {
719 $class = -1; # Punct never appears in a range
720 }
721 else {
722 $class = 0; # Output in hex
723 }
724
725 if (! defined $range_start) {
726 if ($class < 0) {
727 $output .= $char;
728 }
729 else {
730 $range_start = ord $char;
731 $start_class = $class;
732 }
733 } # A range ends if not consecutive, or the class-type changes
734 elsif (ord $char != ($range_end = ord($chars[$i-1])) + 1
735 || $class != $start_class)
736 {
737
738 # Here, the current character is not in the range. This means the
739 # previous character must have been. Output the range up through
740 # that one.
741 my $range_length = $range_end - $range_start + 1;
742 if ($start_class > 0) {
743 $output .= " " . chr($range_start);
744 $output .= "-" . chr($range_end) if $range_length > 1;
745 }
746 else {
747 $hex .= sprintf(" %02X", $range_start);
748 $hex .= sprintf("-%02X", $range_end) if $range_length > 1;
749 }
750
751 # Handle the new current character, as potentially beginning a new
752 # range
753 undef $range_start;
754 redo;
755 }
756 }
757
758 $output =~ s/^ //;
759 $hex =~ s/^ // if ! length $output;
760 return "$output$hex";
761}
762
30032ef4
KW
763sub report_result {
764 my ($Locale, $i, $pass_fail, $message) = @_;
15bbd6a2
KW
765 $message //= "";
766 $message = " ($message)" if $message;
30032ef4 767 unless ($pass_fail) {
2a680da6 768 $Problem{$i}{$Locale} = 1;
baae13cb 769 debug "# failed $i ($test_names{$i}) with locale '$Locale'$message\n";
2a680da6
JH
770 } else {
771 push @{$Okay{$i}}, $Locale;
772 }
773}
774
7c844d17
KW
775sub report_multi_result {
776 my ($Locale, $i, $results_ref) = @_;
777
778 # $results_ref points to an array, each element of which is a character that was
779 # in error for this test numbered '$i'. If empty, the test passed
780
781 my $message = "";
782 if (@$results_ref) {
019bf7dd 783 $message = join " ", "for", display_characters(@$results_ref);
7c844d17
KW
784 }
785 report_result($Locale, $i, @$results_ref == 0, $message);
786}
787
c4093d7d
KW
788my $first_locales_test_number = $final_without_setlocale + 1;
789my $locales_test_number;
790my $not_necessarily_a_problem_test_number;
6d5d702a
KW
791my $first_casing_test_number;
792my $final_casing_test_number;
c4093d7d
KW
793my %setlocale_failed; # List of locales that setlocale() didn't work on
794
284102e8 795foreach $Locale (@Locale) {
c4093d7d 796 $locales_test_number = $first_locales_test_number - 1;
284102e8 797 debug "# Locale = $Locale\n";
284102e8 798
a810e350 799 unless (setlocale(&POSIX::LC_ALL, $Locale)) {
c4093d7d 800 $setlocale_failed{$Locale} = $Locale;
284102e8 801 next;
8ebc5c01 802 }
8ebc5c01 803
66cbab2c
KW
804 # We test UTF-8 locales only under ':not_characters'; otherwise they have
805 # documented deficiencies. Non- UTF-8 locales are tested only under plain
806 # 'use locale', as otherwise we would have to convert everything in them
807 # to Unicode.
808 my $is_utf8_locale = $Locale =~ /UTF-?8/i;
809
95eaa1bf
KW
810 my %UPPER = (); # All alpha X for which uc(X) == X and lc(X) != X
811 my %lower = (); # All alpha X for which lc(X) == X and uc(X) != X
812 my %BoThCaSe = (); # All alpha X for which uc(X) == lc(X) == X
66cbab2c
KW
813
814 if (! $is_utf8_locale) {
815 use locale;
519c0534
KW
816 @Word_ = sort grep /\w/, map { chr } 0..255;
817 debug "# w = ", join("",@Word_), "\n";
aac995d2 818 @Digit_ = grep /\d/, map { chr } 0..255;
e5272a46 819
71e5cbb3
KW
820 # Sieve the uppercase and the lowercase.
821
519c0534 822 for (@Word_) {
71e5cbb3
KW
823 if (/[^\d_]/) { # skip digits and the _
824 if (uc($_) eq $_) {
825 $UPPER{$_} = $_;
826 }
827 if (lc($_) eq $_) {
828 $lower{$_} = $_;
829 }
830 }
831 }
66cbab2c
KW
832 }
833 else {
834 use locale ':not_characters';
519c0534 835 @Word_ = sort grep /\w/, map { chr } 0..255;
aac995d2 836 @Digit_ = grep /\d/, map { chr } 0..255;
519c0534
KW
837 debug "# w = ", join("",@Word_), "\n";
838 for (@Word_) {
66cbab2c
KW
839 if (/[^\d_]/) { # skip digits and the _
840 if (uc($_) eq $_) {
841 $UPPER{$_} = $_;
842 }
843 if (lc($_) eq $_) {
844 $lower{$_} = $_;
845 }
846 }
847 }
848 }
284102e8 849 foreach (keys %UPPER) {
097ee67d 850 $BoThCaSe{$_}++ if exists $lower{$_};
284102e8
JH
851 }
852 foreach (keys %lower) {
097ee67d 853 $BoThCaSe{$_}++ if exists $UPPER{$_};
284102e8 854 }
097ee67d 855 foreach (keys %BoThCaSe) {
284102e8
JH
856 delete $UPPER{$_};
857 delete $lower{$_};
858 }
859
019bf7dd
KW
860 debug "# UPPER = ", display_characters(keys %UPPER), "\n";
861 debug "# lower = ", display_characters(keys %lower), "\n";
862 debug "# BoThCaSe = ", display_characters(keys %BoThCaSe), "\n";
284102e8 863
baa71cfd 864 my @failures;
3da38613 865 my @fold_failures;
baa71cfd
KW
866 foreach my $x (sort keys %UPPER) {
867 my $ok;
3da38613 868 my $fold_ok;
baa71cfd
KW
869 if ($is_utf8_locale) {
870 use locale ':not_characters';
871 $ok = $x =~ /[[:upper:]]/;
3da38613 872 $fold_ok = $x =~ /[[:lower:]]/i;
baa71cfd
KW
873 }
874 else {
875 use locale;
876 $ok = $x =~ /[[:upper:]]/;
3da38613 877 $fold_ok = $x =~ /[[:lower:]]/i;
baa71cfd
KW
878 }
879 push @failures, $x unless $ok;
3da38613 880 push @fold_failures, $x unless $fold_ok;
baa71cfd 881 }
baa71cfd 882 $locales_test_number++;
6d5d702a 883 $first_casing_test_number = $locales_test_number;
95eaa1bf 884 $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/ matches all alpha X for which uc(X) == X and lc(X) != X';
7c844d17 885 report_multi_result($Locale, $locales_test_number, \@failures);
6d5d702a 886
3da38613 887 $locales_test_number++;
6d5d702a 888
95eaa1bf 889 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/i matches all alpha X for which uc(X) == X and lc(X) != X';
7c844d17 890 report_multi_result($Locale, $locales_test_number, \@fold_failures);
baa71cfd 891
baa71cfd 892 undef @failures;
3da38613 893 undef @fold_failures;
baa71cfd
KW
894
895 foreach my $x (sort keys %lower) {
896 my $ok;
3da38613 897 my $fold_ok;
baa71cfd
KW
898 if ($is_utf8_locale) {
899 use locale ':not_characters';
900 $ok = $x =~ /[[:lower:]]/;
3da38613 901 $fold_ok = $x =~ /[[:upper:]]/i;
baa71cfd
KW
902 }
903 else {
904 use locale;
905 $ok = $x =~ /[[:lower:]]/;
3da38613 906 $fold_ok = $x =~ /[[:upper:]]/i;
baa71cfd
KW
907 }
908 push @failures, $x unless $ok;
3da38613 909 push @fold_failures, $x unless $fold_ok;
baa71cfd
KW
910 }
911
912 $locales_test_number++;
95eaa1bf 913 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/ matches all alpha X for which lc(X) == X and uc(X) != X';
7c844d17
KW
914 report_multi_result($Locale, $locales_test_number, \@failures);
915
3da38613 916 $locales_test_number++;
95eaa1bf 917 $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/i matches all alpha X for which lc(X) == X and uc(X) != X';
7c844d17 918 report_multi_result($Locale, $locales_test_number, \@fold_failures);
baa71cfd 919
9445c837
KW
920 { # Find the alphabetic characters that are not considered alphabetics
921 # in the default (C) locale.
8ebc5c01 922
284102e8 923 no locale;
71e5cbb3 924
30032ef4 925 @Added_alpha = ();
5e7a1028 926 for (keys %UPPER, keys %lower, keys %BoThCaSe) {
30032ef4 927 push(@Added_alpha, $_) if (/\W/);
284102e8 928 }
8ebc5c01 929 }
8ebc5c01 930
30032ef4 931 @Added_alpha = sort @Added_alpha;
8ebc5c01 932
019bf7dd 933 debug "# Added_alpha = ", display_characters(@Added_alpha), "\n";
8ebc5c01 934
db31898d 935 # Cross-check the whole 8-bit character set.
8ebc5c01 936
db31898d
KW
937 ++$locales_test_number;
938 my @f;
939 $test_names{$locales_test_number} = 'Verify that \w and [:word:] are identical';
940 for (map { chr } 0..255) {
941 if ($is_utf8_locale) {
942 use locale ':not_characters';
943 push @f, $_ unless /[[:word:]]/ == /\w/;
944 }
945 else {
946 push @f, $_ unless /[[:word:]]/ == /\w/;
947 }
948 }
949 report_multi_result($Locale, $locales_test_number, \@f);
8ebc5c01 950
db31898d
KW
951 ++$locales_test_number;
952 undef @f;
953 $test_names{$locales_test_number} = 'Verify that \d and [:digit:] are identical';
954 for (map { chr } 0..255) {
955 if ($is_utf8_locale) {
956 use locale ':not_characters';
957 push @f, $_ unless /[[:digit:]]/ == /\d/;
958 }
959 else {
960 push @f, $_ unless /[[:digit:]]/ == /\d/;
961 }
962 }
963 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 964
db31898d
KW
965 ++$locales_test_number;
966 undef @f;
967 $test_names{$locales_test_number} = 'Verify that \s and [:space:] are identical';
968 for (map { chr } 0..255) {
969 if ($is_utf8_locale) {
970 use locale ':not_characters';
971 push @f, $_ unless /[[:space:]]/ == /\s/;
972 }
973 else {
974 push @f, $_ unless /[[:space:]]/ == /\s/;
975 }
976 }
977 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 978
db31898d
KW
979 ++$locales_test_number;
980 undef @f;
981 $test_names{$locales_test_number} = 'Verify that [:posix:] and [:^posix:] are mutually exclusive';
982 for (map { chr } 0..255) {
983 if ($is_utf8_locale) {
984 use locale ':not_characters';
985 push @f, $_ unless (/[[:alpha:]]/ xor /[[:^alpha:]]/) ||
986 (/[[:alnum:]]/ xor /[[:^alnum:]]/) ||
987 (/[[:ascii:]]/ xor /[[:^ascii:]]/) ||
988 (/[[:blank:]]/ xor /[[:^blank:]]/) ||
989 (/[[:cntrl:]]/ xor /[[:^cntrl:]]/) ||
990 (/[[:digit:]]/ xor /[[:^digit:]]/) ||
991 (/[[:graph:]]/ xor /[[:^graph:]]/) ||
992 (/[[:lower:]]/ xor /[[:^lower:]]/) ||
993 (/[[:print:]]/ xor /[[:^print:]]/) ||
994 (/[[:space:]]/ xor /[[:^space:]]/) ||
995 (/[[:upper:]]/ xor /[[:^upper:]]/) ||
996 (/[[:word:]]/ xor /[[:^word:]]/) ||
997 (/[[:xdigit:]]/ xor /[[:^xdigit:]]/) ||
998
999 # effectively is what [:cased:] would be if it existed.
1000 (/[[:upper:]]/i xor /[[:^upper:]]/i);
1001 }
1002 else {
1003 push @f, $_ unless (/[[:alpha:]]/ xor /[[:^alpha:]]/) ||
1004 (/[[:alnum:]]/ xor /[[:^alnum:]]/) ||
1005 (/[[:ascii:]]/ xor /[[:^ascii:]]/) ||
1006 (/[[:blank:]]/ xor /[[:^blank:]]/) ||
1007 (/[[:cntrl:]]/ xor /[[:^cntrl:]]/) ||
1008 (/[[:digit:]]/ xor /[[:^digit:]]/) ||
1009 (/[[:graph:]]/ xor /[[:^graph:]]/) ||
1010 (/[[:lower:]]/ xor /[[:^lower:]]/) ||
1011 (/[[:print:]]/ xor /[[:^print:]]/) ||
1012 (/[[:space:]]/ xor /[[:^space:]]/) ||
1013 (/[[:upper:]]/ xor /[[:^upper:]]/) ||
1014 (/[[:word:]]/ xor /[[:^word:]]/) ||
1015 (/[[:xdigit:]]/ xor /[[:^xdigit:]]/) ||
1016 (/[[:upper:]]/i xor /[[:^upper:]]/i);
1017 }
1018 }
1019 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1020
db31898d
KW
1021 # The rules for the relationships are given in:
1022 # http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html
32e8aa3f 1023
db31898d
KW
1024 ++$locales_test_number;
1025 undef @f;
1026 $test_names{$locales_test_number} = 'Verify that [:lower:] is a subset of [:alpha:]';
1027 for (map { chr } 0..255) {
1028 if ($is_utf8_locale) {
1029 use locale ':not_characters';
1030 push @f, $_ if /[[:lower:]]/ and ! /[[:alpha:]]/;
1031 }
1032 else {
1033 push @f, $_ if /[[:lower:]]/ and ! /[[:alpha:]]/;
1034 }
1035 }
1036 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1037
db31898d
KW
1038 ++$locales_test_number;
1039 undef @f;
1040 $test_names{$locales_test_number} = 'Verify that [:upper:] is a subset of [:alpha:]';
1041 for (map { chr } 0..255) {
1042 if ($is_utf8_locale) {
1043 use locale ':not_characters';
1044 push @f, $_ if /[[:upper:]]/ and ! /[[:alpha:]]/;
1045 }
1046 else {
1047 push @f, $_ if /[[:upper:]]/ and ! /[[:alpha:]]/;
1048 }
1049 }
1050 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1051
db31898d
KW
1052 ++$locales_test_number;
1053 undef @f;
1054 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/i is a subset of [:alpha:]';
1055 for (map { chr } 0..255) {
1056 if ($is_utf8_locale) {
1057 use locale ':not_characters';
1058 push @f, $_ if /[[:lower:]]/i and ! /[[:alpha:]]/;
1059 }
1060 else {
1061 push @f, $_ if /[[:lower:]]/i and ! /[[:alpha:]]/;
1062 }
1063 }
1064 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1065
db31898d
KW
1066 ++$locales_test_number;
1067 undef @f;
1068 $test_names{$locales_test_number} = 'Verify that [:alpha:] is a subset of [:alnum:]';
1069 for (map { chr } 0..255) {
1070 if ($is_utf8_locale) {
1071 use locale ':not_characters';
1072 push @f, $_ if /[[:alpha:]]/ and ! /[[:alnum:]]/;
1073 }
1074 else {
1075 push @f, $_ if /[[:alpha:]]/ and ! /[[:alnum:]]/;
1076 }
1077 }
1078 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1079
db31898d
KW
1080 ++$locales_test_number;
1081 undef @f;
1082 $test_names{$locales_test_number} = 'Verify that [:digit:] is a subset of [:alnum:]';
1083 for (map { chr } 0..255) {
1084 if ($is_utf8_locale) {
1085 use locale ':not_characters';
1086 push @f, $_ if /[[:digit:]]/ and ! /[[:alnum:]]/;
1087 }
1088 else {
1089 push @f, $_ if /[[:digit:]]/ and ! /[[:alnum:]]/;
1090 }
1091 }
1092 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1093
db31898d
KW
1094 ++$locales_test_number;
1095 undef @f;
aac995d2
KW
1096 $test_names{$locales_test_number} = 'Verify that [:digit:] matches either 10 or 20 code points';
1097 report_result($Locale, $locales_test_number, @Digit_ == 10 || @Digit_ ==20);
1098
1099 ++$locales_test_number;
1100 undef @f;
1101 $test_names{$locales_test_number} = 'Verify that [:digit:] (if is 10 code points) is a subset of [:xdigit:]';
1102 if (@Digit_ == 10) {
fc81f5f2
KW
1103 for (map { chr } 0..255) {
1104 if ($is_utf8_locale) {
1105 use locale ':not_characters';
1106 push @f, $_ if /[[:digit:]]/ and ! /[[:xdigit:]]/;
1107 }
1108 else {
1109 push @f, $_ if /[[:digit:]]/ and ! /[[:xdigit:]]/;
1110 }
db31898d
KW
1111 }
1112 }
1113 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1114
db31898d
KW
1115 ++$locales_test_number;
1116 undef @f;
1117 $test_names{$locales_test_number} = 'Verify that [:alnum:] is a subset of [:graph:]';
1118 for (map { chr } 0..255) {
1119 if ($is_utf8_locale) {
1120 use locale ':not_characters';
1121 push @f, $_ if /[[:alnum:]]/ and ! /[[:graph:]]/;
1122 }
1123 else {
1124 push @f, $_ if /[[:alnum:]]/ and ! /[[:graph:]]/;
1125 }
1126 }
1127 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1128
db31898d 1129 # Note that xdigit doesn't have to be a subset of alnum
32e8aa3f 1130
db31898d
KW
1131 ++$locales_test_number;
1132 undef @f;
1133 $test_names{$locales_test_number} = 'Verify that [:xdigit:] is a subset of [:graph:]';
1134 for (map { chr } 0..255) {
1135 if ($is_utf8_locale) {
1136 use locale ':not_characters';
1137 push @f, $_ if /[[:xdigit:]]/ and ! /[[:graph:]]/;
1138 }
1139 else {
1140 push @f, $_ if /[[:xdigit:]]/ and ! /[[:graph:]]/;
1141 }
1142 }
1143 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1144
db31898d
KW
1145 ++$locales_test_number;
1146 undef @f;
1147 $test_names{$locales_test_number} = 'Verify that [:punct:] is a subset of [:graph:]';
1148 for (map { chr } 0..255) {
1149 if ($is_utf8_locale) {
1150 use locale ':not_characters';
1151 push @f, $_ if /[[:punct:]]/ and ! /[[:graph:]]/;
1152 }
1153 else {
1154 push @f, $_ if /[[:punct:]]/ and ! /[[:graph:]]/;
1155 }
1156 }
1157 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1158
db31898d
KW
1159 ++$locales_test_number;
1160 undef @f;
1161 $test_names{$locales_test_number} = 'Verify that [:blank:] is a subset of [:space:]';
1162 for (map { chr } 0..255) {
1163 if ($is_utf8_locale) {
1164 use locale ':not_characters';
1165 push @f, $_ if /[[:blank:]]/ and ! /[[:space:]]/;
1166 }
1167 else {
1168 push @f, $_ if /[[:blank:]]/ and ! /[[:space:]]/;
1169 }
1170 }
1171 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1172
db31898d
KW
1173 ++$locales_test_number;
1174 undef @f;
1175 $test_names{$locales_test_number} = 'Verify that [:graph:] is a subset of [:print:]';
1176 for (map { chr } 0..255) {
1177 if ($is_utf8_locale) {
1178 use locale ':not_characters';
1179 push @f, $_ if /[[:graph:]]/ and ! /[[:print:]]/;
1180 }
1181 else {
1182 push @f, $_ if /[[:graph:]]/ and ! /[[:print:]]/;
1183 }
1184 }
1185 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1186
db31898d
KW
1187 ++$locales_test_number;
1188 undef @f;
1189 $test_names{$locales_test_number} = 'Verify that isn\'t both [:cntrl:] and [:print:]';
1190 for (map { chr } 0..255) {
1191 if ($is_utf8_locale) {
1192 use locale ':not_characters';
1193 push @f, $_ if (/[[:print:]]/ and /[[:cntrl:]]/);
1194 }
1195 else {
1196 push @f, $_ if (/[[:print:]]/ and /[[:cntrl:]]/);
1197 }
1198 }
1199 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1200
db31898d
KW
1201 ++$locales_test_number;
1202 undef @f;
1203 $test_names{$locales_test_number} = 'Verify that isn\'t both [:alnum:] and [:punct:]';
1204 for (map { chr } 0..255) {
1205 if ($is_utf8_locale) {
1206 use locale ':not_characters';
1207 push @f, $_ if /[[:alnum:]]/ and /[[:punct:]]/;
1208 }
1209 else {
1210 push @f, $_ if /[[:alnum:]]/ and /[[:punct:]]/;
1211 }
1212 }
1213 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1214
db31898d
KW
1215 ++$locales_test_number;
1216 undef @f;
1217 $test_names{$locales_test_number} = 'Verify that isn\'t both [:xdigit:] and [:punct:]';
1218 for (map { chr } 0..255) {
1219 if ($is_utf8_locale) {
1220 use locale ':not_characters';
1221 push @f, $_ if (/[[:punct:]]/ and /[[:xdigit:]]/);
1222 }
1223 else {
1224 push @f, $_ if (/[[:punct:]]/ and /[[:xdigit:]]/);
1225 }
1226 }
1227 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1228
db31898d
KW
1229 ++$locales_test_number;
1230 undef @f;
1231 $test_names{$locales_test_number} = 'Verify that isn\'t both [:graph:] and [:space:]';
1232 for (map { chr } 0..255) {
1233 if ($is_utf8_locale) {
1234 use locale ':not_characters';
1235 push @f, $_ if (/[[:graph:]]/ and /[[:space:]]/);
1236 }
1237 else {
1238 push @f, $_ if (/[[:graph:]]/ and /[[:space:]]/);
1239 }
1240 }
1241 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1242
db31898d 1243 $final_casing_test_number = $locales_test_number;
32e8aa3f 1244
db31898d
KW
1245 # Test for read-only scalars' locale vs non-locale comparisons.
1246
1247 {
1248 no locale;
1249 my $ok;
1250 $a = "qwerty";
1251 if ($is_utf8_locale) {
1252 use locale ':not_characters';
1253 $ok = ($a cmp "qwerty") == 0;
1254 }
1255 else {
1256 use locale;
1257 $ok = ($a cmp "qwerty") == 0;
1258 }
1259 report_result($Locale, ++$locales_test_number, $ok);
1260 $test_names{$locales_test_number} = 'Verify that cmp works with a read-only scalar; no- vs locale';
1261 }
8ebc5c01 1262
db31898d
KW
1263 {
1264 my ($from, $to, $lesser, $greater,
1265 @test, %test, $test, $yes, $no, $sign);
284102e8 1266
db31898d
KW
1267 ++$locales_test_number;
1268 $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work';
1269 $not_necessarily_a_problem_test_number = $locales_test_number;
1270 for (0..9) {
1271 # Select a slice.
519c0534
KW
1272 $from = int(($_*@Word_)/10);
1273 $to = $from + int(@Word_/10);
1274 $to = $#Word_ if ($to > $#Word_);
1275 $lesser = join('', @Word_[$from..$to]);
db31898d
KW
1276 # Select a slice one character on.
1277 $from++; $to++;
519c0534
KW
1278 $to = $#Word_ if ($to > $#Word_);
1279 $greater = join('', @Word_[$from..$to]);
66cbab2c
KW
1280 if ($is_utf8_locale) {
1281 use locale ':not_characters';
db31898d
KW
1282 ($yes, $no, $sign) = ($lesser lt $greater
1283 ? (" ", "not ", 1)
1284 : ("not ", " ", -1));
66cbab2c
KW
1285 }
1286 else {
1287 use locale;
db31898d
KW
1288 ($yes, $no, $sign) = ($lesser lt $greater
1289 ? (" ", "not ", 1)
1290 : ("not ", " ", -1));
66cbab2c 1291 }
db31898d
KW
1292 # all these tests should FAIL (return 0). Exact lt or gt cannot
1293 # be tested because in some locales, say, eacute and E may test
1294 # equal.
1295 @test =
1296 (
1297 $no.' ($lesser le $greater)', # 1
1298 'not ($lesser ne $greater)', # 2
1299 ' ($lesser eq $greater)', # 3
1300 $yes.' ($lesser ge $greater)', # 4
1301 $yes.' ($lesser ge $greater)', # 5
1302 $yes.' ($greater le $lesser )', # 7
1303 'not ($greater ne $lesser )', # 8
1304 ' ($greater eq $lesser )', # 9
1305 $no.' ($greater ge $lesser )', # 10
1306 'not (($lesser cmp $greater) == -($sign))' # 11
1307 );
1308 @test{@test} = 0 x @test;
1309 $test = 0;
1310 for my $ti (@test) {
66cbab2c
KW
1311 if ($is_utf8_locale) {
1312 use locale ':not_characters';
db31898d 1313 $test{$ti} = eval $ti;
66cbab2c
KW
1314 }
1315 else {
db31898d
KW
1316 # Already in 'use locale';
1317 $test{$ti} = eval $ti;
66cbab2c 1318 }
db31898d
KW
1319 $test ||= $test{$ti}
1320 }
1321 report_result($Locale, $locales_test_number, $test == 0);
1322 if ($test) {
1323 debug "# lesser = '$lesser'\n";
1324 debug "# greater = '$greater'\n";
1325 debug "# lesser cmp greater = ",
1326 $lesser cmp $greater, "\n";
1327 debug "# greater cmp lesser = ",
1328 $greater cmp $lesser, "\n";
1329 debug "# (greater) from = $from, to = $to\n";
1330 for my $ti (@test) {
1331 debugf("# %-40s %-4s", $ti,
1332 $test{$ti} ? 'FAIL' : 'ok');
1333 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
1334 debugf("(%s == %4d)", $1, eval $1);
66cbab2c 1335 }
db31898d
KW
1336 debug "\n#";
1337 }
1338
1339 last;
1340 }
1341 }
1342 }
c4093d7d 1343
66cbab2c
KW
1344 my $ok1;
1345 my $ok2;
1346 my $ok3;
1347 my $ok4;
1348 my $ok5;
1349 my $ok6;
1350 my $ok7;
1351 my $ok8;
1352 my $ok9;
1353 my $ok10;
1354 my $ok11;
1355 my $ok12;
1356 my $ok13;
1500bd91 1357 my $ok14;
28acfe03
KW
1358 my $ok15;
1359 my $ok16;
66cbab2c
KW
1360
1361 my $c;
1362 my $d;
1363 my $e;
1364 my $f;
1365 my $g;
1366
1367 if (! $is_utf8_locale) {
71e5cbb3 1368 use locale;
6be75cd7 1369
71e5cbb3 1370 my ($x, $y) = (1.23, 1.23);
6be75cd7 1371
71e5cbb3
KW
1372 $a = "$x";
1373 printf ''; # printf used to reset locale to "C"
1374 $b = "$y";
1375 $ok1 = $a eq $b;
6be75cd7 1376
71e5cbb3
KW
1377 $c = "$x";
1378 my $z = sprintf ''; # sprintf used to reset locale to "C"
1379 $d = "$y";
1380 $ok2 = $c eq $d;
1381 {
66cbab2c 1382
71e5cbb3
KW
1383 use warnings;
1384 my $w = 0;
1385 local $SIG{__WARN__} =
1386 sub {
1387 print "# @_\n";
1388 $w++;
1389 };
6be75cd7 1390
71e5cbb3
KW
1391 # The == (among other ops) used to warn for locales
1392 # that had something else than "." as the radix character.
6be75cd7 1393
71e5cbb3
KW
1394 $ok3 = $c == 1.23;
1395 $ok4 = $c == $x;
1396 $ok5 = $c == $d;
1397 {
1398 no locale;
66cbab2c 1399
b79536ea 1400 $e = "$x";
71e5cbb3
KW
1401
1402 $ok6 = $e == 1.23;
1403 $ok7 = $e == $x;
1404 $ok8 = $e == $c;
1405 }
66cbab2c 1406
71e5cbb3
KW
1407 $f = "1.23";
1408 $g = 2.34;
66cbab2c 1409
71e5cbb3
KW
1410 $ok9 = $f == 1.23;
1411 $ok10 = $f == $x;
1412 $ok11 = $f == $c;
1413 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1414 $ok13 = $w == 0;
28acfe03 1415 $ok14 = $ok15 = $ok16 = 1; # Skip for non-utf8 locales
71e5cbb3 1416 }
66cbab2c
KW
1417 }
1418 else {
1419 use locale ':not_characters';
1420
1421 my ($x, $y) = (1.23, 1.23);
1422 $a = "$x";
1423 printf ''; # printf used to reset locale to "C"
1424 $b = "$y";
1425 $ok1 = $a eq $b;
1426
1427 $c = "$x";
1428 my $z = sprintf ''; # sprintf used to reset locale to "C"
1429 $d = "$y";
1430 $ok2 = $c eq $d;
1431 {
1432 use warnings;
1433 my $w = 0;
1434 local $SIG{__WARN__} =
1435 sub {
1436 print "# @_\n";
1437 $w++;
1438 };
1439 $ok3 = $c == 1.23;
1440 $ok4 = $c == $x;
1441 $ok5 = $c == $d;
1442 {
1443 no locale;
b79536ea 1444 $e = "$x";
66cbab2c
KW
1445
1446 $ok6 = $e == 1.23;
1447 $ok7 = $e == $x;
1448 $ok8 = $e == $c;
1449 }
1450
1451 $f = "1.23";
1452 $g = 2.34;
1453
1454 $ok9 = $f == 1.23;
1455 $ok10 = $f == $x;
1456 $ok11 = $f == $c;
1457 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1458 $ok13 = $w == 0;
1500bd91
KW
1459
1460 # Look for non-ASCII error messages, and verify that the first
1461 # such is in UTF-8 (the others almost certainly will be like the
1462 # first).
1463 $ok14 = 1;
1464 foreach my $err (keys %!) {
1465 use Errno;
1466 $! = eval "&Errno::$err"; # Convert to strerror() output
1467 my $strerror = "$!";
1468 if ("$strerror" =~ /\P{ASCII}/) {
1469 my $utf8_strerror = $strerror;
1470 utf8::upgrade($utf8_strerror);
1471
1472 # If $! was already in UTF-8, the upgrade was a no-op;
1473 # otherwise they will be different byte strings.
1474 use bytes;
1475 $ok14 = $utf8_strerror eq $strerror;
1476 last;
1477 }
1478 }
28acfe03
KW
1479
1480 # Similarly, we verify that a non-ASCII radix is in UTF-8. This
1481 # also catches if there is a disparity between sprintf and
1482 # stringification.
1483
1484 my $string_g = "$g";
1485
1486 my $utf8_string_g = "$g";
1487 utf8::upgrade($utf8_string_g);
1488
1489 my $utf8_sprintf_g = sprintf("%g", $g);
1490 utf8::upgrade($utf8_sprintf_g);
1491 use bytes;
1492 $ok15 = $utf8_string_g eq $string_g;
1493 $ok16 = $utf8_sprintf_g eq $string_g;
66cbab2c
KW
1494 }
1495 }
1496
30032ef4 1497 report_result($Locale, ++$locales_test_number, $ok1);
66cbab2c
KW
1498 $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results';
1499 my $first_a_test = $locales_test_number;
1500
1501 debug "# $first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n";
1502
30032ef4 1503 report_result($Locale, ++$locales_test_number, $ok2);
66cbab2c
KW
1504 $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results';
1505
1506 my $first_c_test = $locales_test_number;
1507
30032ef4 1508 report_result($Locale, ++$locales_test_number, $ok3);
71e5cbb3 1509 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant';
6be75cd7 1510
30032ef4 1511 report_result($Locale, ++$locales_test_number, $ok4);
71e5cbb3 1512 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar';
66cbab2c 1513
30032ef4 1514 report_result($Locale, ++$locales_test_number, $ok5);
71e5cbb3 1515 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf';
66cbab2c 1516
71e5cbb3 1517 debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n";
66cbab2c 1518
30032ef4 1519 report_result($Locale, ++$locales_test_number, $ok6);
b79536ea 1520 $test_names{$locales_test_number} = 'Verify that can assign stringified under inner no-locale block';
71e5cbb3 1521 my $first_e_test = $locales_test_number;
6be75cd7 1522
30032ef4 1523 report_result($Locale, ++$locales_test_number, $ok7);
71e5cbb3 1524 $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale';
66cbab2c 1525
30032ef4 1526 report_result($Locale, ++$locales_test_number, $ok8);
71e5cbb3 1527 $test_names{$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale';
c4093d7d 1528
71e5cbb3 1529 debug "# $first_e_test..$locales_test_number: \$e = $e, no locale\n";
2a680da6 1530
30032ef4 1531 report_result($Locale, ++$locales_test_number, $ok9);
71e5cbb3
KW
1532 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant';
1533 my $first_f_test = $locales_test_number;
6be75cd7 1534
30032ef4 1535 report_result($Locale, ++$locales_test_number, $ok10);
71e5cbb3 1536 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar';
66cbab2c 1537
30032ef4 1538 report_result($Locale, ++$locales_test_number, $ok11);
71e5cbb3 1539 $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 1540
30032ef4 1541 report_result($Locale, ++$locales_test_number, $ok12);
71e5cbb3 1542 $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 1543
30032ef4 1544 report_result($Locale, ++$locales_test_number, $ok13);
71e5cbb3 1545 $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot';
c4093d7d 1546
30032ef4 1547 report_result($Locale, ++$locales_test_number, $ok14);
1500bd91
KW
1548 $test_names{$locales_test_number} = 'Verify that non-ASCII UTF-8 error messages are in UTF-8';
1549
30032ef4 1550 report_result($Locale, ++$locales_test_number, $ok15);
28acfe03
KW
1551 $test_names{$locales_test_number} = 'Verify that a number with a UTF-8 radix has a UTF-8 stringification';
1552
30032ef4 1553 report_result($Locale, ++$locales_test_number, $ok16);
28acfe03
KW
1554 $test_names{$locales_test_number} = 'Verify that a sprintf of a number with a UTF-8 radix yields UTF-8';
1555
71e5cbb3 1556 debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n";
906f284f 1557
26d80d95
LC
1558 # Does taking lc separately differ from taking
1559 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
1560 # The bug was in the caching of the 'o'-magic.
66cbab2c 1561 if (! $is_utf8_locale) {
2a680da6 1562 use locale;
6be75cd7 1563
2a680da6
JH
1564 sub lcA {
1565 my $lc0 = lc $_[0];
1566 my $lc1 = lc $_[1];
1567 return $lc0 cmp $lc1;
1568 }
6be75cd7 1569
2a680da6
JH
1570 sub lcB {
1571 return lc($_[0]) cmp lc($_[1]);
1572 }
6be75cd7 1573
2a680da6
JH
1574 my $x = "ab";
1575 my $y = "aa";
1576 my $z = "AB";
6be75cd7 1577
30032ef4 1578 report_result($Locale, ++$locales_test_number,
2a680da6
JH
1579 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
1580 lcA($x, $z) == 0 && lcB($x, $z) == 0);
6be75cd7 1581 }
66cbab2c
KW
1582 else {
1583 use locale ':not_characters';
1584
1585 sub lcC {
1586 my $lc0 = lc $_[0];
1587 my $lc1 = lc $_[1];
1588 return $lc0 cmp $lc1;
1589 }
1590
1591 sub lcD {
1592 return lc($_[0]) cmp lc($_[1]);
1593 }
1594
1595 my $x = "ab";
1596 my $y = "aa";
1597 my $z = "AB";
1598
30032ef4 1599 report_result($Locale, ++$locales_test_number,
66cbab2c
KW
1600 lcC($x, $y) == 1 && lcD($x, $y) == 1 ||
1601 lcC($x, $z) == 0 && lcD($x, $z) == 0);
1602 }
1603 $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp';
d8093b23 1604
26d80d95
LC
1605 # Does lc of an UPPER (if different from the UPPER) match
1606 # case-insensitively the UPPER, and does the UPPER match
1607 # case-insensitively the lc of the UPPER. And vice versa.
3ba0e062 1608 {
ef4a39e5
JH
1609 use locale;
1610 no utf8;
1611 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
1612
1613 my @f = ();
c4093d7d 1614 ++$locales_test_number;
c08acc4c 1615 $test_names{$locales_test_number} = 'Verify case insensitive matching works';
f78d9f29 1616 foreach my $x (sort keys %UPPER) {
66cbab2c 1617 if (! $is_utf8_locale) {
71e5cbb3
KW
1618 my $y = lc $x;
1619 next unless uc $y eq $x;
1620 print "# UPPER $x lc $y ",
faf0c248
KW
1621 $x =~ /$y/i ? 1 : 0, " ",
1622 $y =~ /$x/i ? 1 : 0, "\n" if 0;
71e5cbb3
KW
1623 #
1624 # If $x and $y contain regular expression characters
1625 # AND THEY lowercase (/i) to regular expression characters,
1626 # regcomp() will be mightily confused. No, the \Q doesn't
1627 # help here (maybe regex engine internal lowercasing
1628 # is done after the \Q?) An example of this happening is
1629 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
1630 # the chr(173) (the "[") is the lowercase of the chr(235).
1631 #
1632 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
1633 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
1634 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
1635 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
1636 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
1637 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
1638 #
1639 # Similar things can happen even under (bastardised)
1640 # non-EBCDIC locales: in many European countries before the
1641 # advent of ISO 8859-x nationally customised versions of
1642 # ISO 646 were devised, reusing certain punctuation
1643 # characters for modified characters needed by the
1644 # country/language. For example, the "|" might have
1645 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
1646 #
1647 if ($x =~ $re || $y =~ $re) {
1648 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1649 next;
1650 }
1651 # With utf8 both will fail since the locale concept
1652 # of upper/lower does not work well in Unicode.
1653 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
26c1569f
KW
1654
1655 # fc is not a locale concept, so Perl uses lc for it.
1656 push @f, $x unless lc $x eq fc $x;
66cbab2c
KW
1657 }
1658 else {
1659 use locale ':not_characters';
1660 my $y = lc $x;
1661 next unless uc $y eq $x;
1662 print "# UPPER $x lc $y ",
faf0c248
KW
1663 $x =~ /$y/i ? 1 : 0, " ",
1664 $y =~ /$x/i ? 1 : 0, "\n" if 0;
66cbab2c
KW
1665
1666 # Here, we can fully test things, unlike plain 'use locale',
1667 # because this form does work well with Unicode
1668 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
26c1569f
KW
1669
1670 # The places where Unicode's lc is different from fc are
1671 # skipped here by virtue of the 'next unless uc...' line above
1672 push @f, $x unless lc $x eq fc $x;
66cbab2c 1673 }
c00ff1c7 1674 }
ef4a39e5 1675
f78d9f29 1676 foreach my $x (sort keys %lower) {
66cbab2c 1677 if (! $is_utf8_locale) {
71e5cbb3
KW
1678 my $y = uc $x;
1679 next unless lc $y eq $x;
1680 print "# lower $x uc $y ",
faf0c248
KW
1681 $x =~ /$y/i ? 1 : 0, " ",
1682 $y =~ /$x/i ? 1 : 0, "\n" if 0;
71e5cbb3
KW
1683 if ($x =~ $re || $y =~ $re) { # See above.
1684 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1685 next;
1686 }
1687 # With utf8 both will fail since the locale concept
1688 # of upper/lower does not work well in Unicode.
1689 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
26c1569f
KW
1690
1691 push @f, $x unless lc $x eq fc $x;
66cbab2c
KW
1692 }
1693 else {
1694 use locale ':not_characters';
1695 my $y = uc $x;
1696 next unless lc $y eq $x;
1697 print "# lower $x uc $y ",
faf0c248
KW
1698 $x =~ /$y/i ? 1 : 0, " ",
1699 $y =~ /$x/i ? 1 : 0, "\n" if 0;
66cbab2c 1700 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
26c1569f
KW
1701
1702 push @f, $x unless lc $x eq fc $x;
66cbab2c 1703 }
c00ff1c7 1704 }
7c844d17 1705 report_multi_result($Locale, $locales_test_number, \@f);
d8093b23 1706 }
78787052
JL
1707
1708 # [perl #109318]
1709 {
1710 my @f = ();
1711 ++$locales_test_number;
1712 $test_names{$locales_test_number} = 'Verify atof with locale radix and negative exponent';
1713
1714 my $radix = POSIX::localeconv()->{decimal_point};
1715 my @nums = (
1716 "3.14e+9", "3${radix}14e+9", "3.14e-9", "3${radix}14e-9",
1717 "-3.14e+9", "-3${radix}14e+9", "-3.14e-9", "-3${radix}14e-9",
1718 );
1719
1720 if (! $is_utf8_locale) {
1721 use locale;
1722 for my $num (@nums) {
1723 push @f, $num
1724 unless sprintf("%g", $num) =~ /3.+14/;
1725 }
1726 }
1727 else {
1728 use locale ':not_characters';
1729 for my $num (@nums) {
1730 push @f, $num
1731 unless sprintf("%g", $num) =~ /3.+14/;
1732 }
1733 }
1734
30032ef4 1735 report_result($Locale, $locales_test_number, @f == 0);
78787052
JL
1736 if (@f) {
1737 print "# failed $locales_test_number locale '$Locale' numbers @f\n"
1738 }
1739 }
8ebc5c01 1740}
284102e8 1741
c4093d7d 1742my $final_locales_test_number = $locales_test_number;
6cf0b567 1743
2a680da6
JH
1744# Recount the errors.
1745
c4093d7d
KW
1746foreach ($first_locales_test_number..$final_locales_test_number) {
1747 if (%setlocale_failed) {
1748 print "not ";
1749 }
1750 elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
1751 if (defined $not_necessarily_a_problem_test_number
1752 && $_ == $not_necessarily_a_problem_test_number)
1753 {
1754 print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n";
b4e009be 1755 print "# It usually indicates a problem in the environment,\n";
284102e8
JH
1756 print "# not in Perl itself.\n";
1757 }
6d5d702a
KW
1758 if ($Okay{$_} && ($_ >= $first_casing_test_number
1759 && $_ <= $final_casing_test_number))
1760 {
0a974e2d
KW
1761 # Round to nearest .1%
1762 my $percent_fail = (int(.5 + (1000 * scalar(keys $Problem{$_})
1763 / scalar(@Locale))))
1764 / 10;
f5627fc1
KW
1765 if (! $debug && $percent_fail < $acceptable_fold_failure_percentage)
1766 {
6d5d702a
KW
1767 $test_names{$_} .= 'TODO';
1768 print "# ", 100 - $percent_fail, "% of locales pass the following test, so it is likely that the failures\n";
1769 print "# are errors in the locale definitions. The test is marked TODO, as the\n";
1770 print "# problem is not likely to be Perl's\n";
1771 }
1772 }
f5627fc1
KW
1773 print "#\n";
1774 if ($debug) {
1775 print "# The code points that had this failure are given above. Look for lines\n";
1776 print "# that match 'failed $_'\n";
1777 }
1778 else {
1779 print "# For more details, rerun, with environment variable PERL_DEBUG_FULL_TEST=1.\n";
1780 print "# Then look at that output for lines that match 'failed $_'\n";
108a305e 1781 }
284102e8 1782 print "not ";
8ebc5c01 1783 }
c4093d7d 1784 print "ok $_";
6c2e653d
KW
1785 if (defined $test_names{$_}) {
1786 # If TODO is in the test name, make it thus
1787 my $todo = $test_names{$_} =~ s/TODO\s*//;
1788 print " $test_names{$_}";
1789 print " # TODO" if $todo;
1790 }
c4093d7d 1791 print "\n";
8ebc5c01 1792}
fb73857a 1793
c4093d7d 1794$test_num = $final_locales_test_number;
c213d471 1795
fbd840df
KW
1796{ # perl #115808
1797 use warnings;
1798 my $warned = 0;
1799 local $SIG{__WARN__} = sub {
1800 $warned = $_[0] =~ /uninitialized/;
1801 };
1802 my $z = "y" . setlocale(&POSIX::LC_ALL, "xyzzy");
1803 ok($warned, "variable set to setlocale(BAD LOCALE) is considered uninitialized");
1804}
1805
094a2f8c 1806# Test that tainting and case changing works on utf8 strings. These tests are
1f5852c9
KW
1807# placed last to avoid disturbing the hard-coded test numbers that existed at
1808# the time these were added above this in this file.
0099bb8d
KW
1809# This also tests that locale overrides unicode_strings in the same scope for
1810# non-utf8 strings.
a810e350 1811setlocale(&POSIX::LC_ALL, "C");
094a2f8c
KW
1812{
1813 use locale;
0099bb8d 1814 use feature 'unicode_strings';
094a2f8c 1815
26c1569f 1816 foreach my $function ("uc", "ucfirst", "lc", "lcfirst", "fc") {
094a2f8c
KW
1817 my @list; # List of code points to test for $function
1818
1819 # Used to calculate the changed case for ASCII characters by using the
1820 # ord, instead of using one of the functions under test.
1821 my $ascii_case_change_delta;
1822 my $above_latin1_case_change_delta; # Same for the specific ords > 255
1823 # that we use
1824
1825 # We test an ASCII character, which should change case and be tainted;
1826 # a Latin1 character, which shouldn't change case under this C locale,
1827 # and is tainted.
1828 # an above-Latin1 character that when the case is changed would cross
1829 # the 255/256 boundary, so doesn't change case and isn't tainted
1830 # (the \x{149} is one of these, but changes into 2 characters, the
1831 # first one of which doesn't cross the boundary.
1832 # the final one in each list is an above-Latin1 character whose case
1833 # does change, and shouldn't be tainted. The code below uses its
1834 # position in its list as a marker to indicate that it, unlike the
1835 # other code points above ASCII, has a successful case change
1836 if ($function =~ /^u/) {
094a2f8c
KW
1837 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
1838 $ascii_case_change_delta = -32;
1839 $above_latin1_case_change_delta = -1;
1840 }
1841 else {
1ca267a5 1842 @list = ("", "A", "\xC0", "\x{17F}", "\x{100}");
094a2f8c
KW
1843 $ascii_case_change_delta = +32;
1844 $above_latin1_case_change_delta = +1;
1845 }
66cbab2c 1846 foreach my $is_utf8_locale (0 .. 1) {
71e5cbb3
KW
1847 foreach my $j (0 .. $#list) {
1848 my $char = $list[$j];
0099bb8d
KW
1849
1850 for my $encoded_in_utf8 (0 .. 1) {
faf0c248
KW
1851 my $should_be;
1852 my $changed;
1853 if (! $is_utf8_locale) {
1854 $should_be = ($j == $#list)
1855 ? chr(ord($char) + $above_latin1_case_change_delta)
1856 : (length $char == 0 || ord($char) > 127)
1857 ? $char
1858 : chr(ord($char) + $ascii_case_change_delta);
1859
1860 # This monstrosity is in order to avoid using an eval,
1861 # which might perturb the results
1862 $changed = ($function eq "uc")
1863 ? uc($char)
1864 : ($function eq "ucfirst")
1865 ? ucfirst($char)
1866 : ($function eq "lc")
1867 ? lc($char)
1868 : ($function eq "lcfirst")
1869 ? lcfirst($char)
26c1569f
KW
1870 : ($function eq "fc")
1871 ? fc($char)
faf0c248
KW
1872 : die("Unexpected function \"$function\"");
1873 }
1874 else {
1875 {
1876 no locale;
71e5cbb3 1877
faf0c248
KW
1878 # For utf8-locales the case changing functions
1879 # should work just like they do outside of locale.
1880 # Can use eval here because not testing it when
1881 # not in locale.
1882 $should_be = eval "$function('$char')";
1883 die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if $@;
71e5cbb3 1884
faf0c248
KW
1885 }
1886 use locale ':not_characters';
1887 $changed = ($function eq "uc")
1888 ? uc($char)
1889 : ($function eq "ucfirst")
1890 ? ucfirst($char)
1891 : ($function eq "lc")
1892 ? lc($char)
1893 : ($function eq "lcfirst")
1894 ? lcfirst($char)
26c1569f
KW
1895 : ($function eq "fc")
1896 ? fc($char)
faf0c248 1897 : die("Unexpected function \"$function\"");
71e5cbb3 1898 }
faf0c248
KW
1899 ok($changed eq $should_be,
1900 "$function(\"$char\") in C locale "
1901 . (($is_utf8_locale)
1902 ? "(use locale ':not_characters'"
1903 : "(use locale")
1904 . (($encoded_in_utf8)
1905 ? "; encoded in utf8)"
1906 : "; not encoded in utf8)")
1907 . " should be \"$should_be\", got \"$changed\"");
1908
1909 # Tainting shouldn't happen for utf8 locales, empty
1910 # strings, or those characters above 255.
1911 (! $is_utf8_locale && length($char) > 0 && ord($char) < 256)
1912 ? check_taint($changed)
1913 : check_taint_not($changed);
1914
1915 # Use UTF-8 next time through the loop
1916 utf8::upgrade($char);
0099bb8d 1917 }
66cbab2c 1918 }
094a2f8c
KW
1919 }
1920 }
1921}
1922
1bfe8fea
KW
1923# Give final advice.
1924
1925my $didwarn = 0;
1926
1927foreach ($first_locales_test_number..$final_locales_test_number) {
1928 if ($Problem{$_}) {
1929 my @f = sort keys %{ $Problem{$_} };
1930 my $f = join(" ", @f);
1931 $f =~ s/(.{50,60}) /$1\n#\t/g;
1932 print
1933 "#\n",
1934 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
1935 "#\t", $f, "\n#\n",
1936 "# on your system may have errors because the locale test $_\n",
1937 "# \"$test_names{$_}\"\n",
1938 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
1939 ".\n";
1940 print <<EOW;
1941#
1942# If your users are not using these locales you are safe for the moment,
1943# but please report this failure first to perlbug\@perl.com using the
1944# perlbug script (as described in the INSTALL file) so that the exact
1945# details of the failures can be sorted out first and then your operating
1946# system supplier can be alerted about these anomalies.
1947#
1948EOW
1949 $didwarn = 1;
1950 }
1951}
1952
1953# Tell which locales were okay and which were not.
1954
1955if ($didwarn) {
1956 my (@s, @F);
1957
1958 foreach my $l (@Locale) {
1959 my $p = 0;
1960 if ($setlocale_failed{$l}) {
1961 $p++;
1962 }
1963 else {
1964 foreach my $t
1965 ($first_locales_test_number..$final_locales_test_number)
1966 {
1967 $p++ if $Problem{$t}{$l};
1968 }
1969 }
1970 push @s, $l if $p == 0;
1971 push @F, $l unless $p == 0;
1972 }
1973
1974 if (@s) {
1975 my $s = join(" ", @s);
1976 $s =~ s/(.{50,60}) /$1\n#\t/g;
1977
1978 warn
1979 "# The following locales\n#\n",
1980 "#\t", $s, "\n#\n",
1981 "# tested okay.\n#\n",
1982 } else {
1983 warn "# None of your locales were fully okay.\n";
1984 }
1985
1986 if (@F) {
1987 my $F = join(" ", @F);
1988 $F =~ s/(.{50,60}) /$1\n#\t/g;
1989
1990 warn
1991 "# The following locales\n#\n",
1992 "#\t", $F, "\n#\n",
1993 "# had problems.\n#\n",
f5627fc1 1994 "# For more details, rerun, with environment variable PERL_DEBUG_FULL_TEST=1.\n";
1bfe8fea
KW
1995 } else {
1996 warn "# None of your locales were broken.\n";
1997 }
1998}
1999
fdf053ee 2000print "1..$test_num\n";
906f284f 2001
90248788 2002# eof