This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
lib/locale.t: Add, improve debugging output
[perl5.git] / lib / locale.t
CommitLineData
8ebc5c01
PP
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
PP
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
PP
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
PP
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
PP
78
79 print 'not ' unless ($result);
c213d471 80 print "ok " . ++$test_num;
e3a2734b
KW
81 print " $message";
82 print "\n";
8ebc5c01
PP
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
PP
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
PP
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
PP
106}
107
bf3cd0e6
KW
108"\tb\t" =~ /^m?(\s)(.*)\1$/;
109check_taint_not $&, "not tainted outside 'use locale'";
110;
111
8ebc5c01
PP
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
PP
132
133$_ = $a; # untaint $_
134
135$_ = uc($a); # taint $_
136
36a42ae7 137check_taint $_;
8ebc5c01
PP
138
139/(\w)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
140check_taint $&;
141check_taint $`;
142check_taint $';
143check_taint $+;
144check_taint $1;
145check_taint_not $2;
8ebc5c01
PP
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
PP
154
155/(\W)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
156check_taint $&;
157check_taint $`;
158check_taint $';
159check_taint $+;
160check_taint $1;
161check_taint_not $2;
8ebc5c01
PP
162
163/(\s)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
164check_taint $&;
165check_taint $`;
166check_taint $';
167check_taint $+;
168check_taint $1;
169check_taint_not $2;
8ebc5c01
PP
170
171/(\S)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
172check_taint $&;
173check_taint $`;
174check_taint $';
175check_taint $+;
176check_taint $1;
177check_taint_not $2;
8ebc5c01
PP
178
179$_ = $a; # untaint $_
180
36a42ae7 181check_taint_not $_;
8ebc5c01
PP
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
PP
190
191$_ = $a; # untaint $_
192
36a42ae7 193check_taint_not $_;
8ebc5c01
PP
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
PP
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
PP
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
PP
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
PP
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
PP
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
PP
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
PP
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
PP
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;
519c0534 524my @Word_;
a160ac48
KW
525my @Digit_;
526my @Space_;
527my @Alpha_;
528my @Alnum_;
529my @Ascii_;
530my @Blank_;
531my @Cntrl_;
532my @Graph_;
533my @Lower_;
534my @Print_;
535my @Upper_;
536my @Xdigit_;
537my @Cased_;
284102e8 538
284102e8
JH
539sub trylocale {
540 my $locale = shift;
0b9f254b 541 return if grep { $locale eq $_ } @Locale;
a810e350 542 return unless setlocale(&POSIX::LC_ALL, $locale);
e439cacb
KW
543 my $badutf8;
544 {
545 local $SIG{__WARN__} = sub {
546 $badutf8 = $_[0] =~ /Malformed UTF-8/;
547 };
548 $Locale =~ /UTF-?8/i;
284102e8 549 }
e439cacb
KW
550
551 if ($badutf8) {
552 ok(0, "Locale name contains malformed utf8");
553 return;
554 }
555 push @Locale, $locale;
284102e8 556}
8ebc5c01 557
284102e8
JH
558sub decode_encodings {
559 my @enc;
8ebc5c01 560
284102e8
JH
561 foreach (split(/ /, shift)) {
562 if (/^(\d+)$/) {
563 push @enc, "ISO8859-$1";
564 push @enc, "iso8859$1"; # HP
565 if ($1 eq '1') {
566 push @enc, "roman8"; # HP
567 }
568 } else {
569 push @enc, $_;
dd8482fc 570 push @enc, "$_.UTF-8";
8ebc5c01
PP
571 }
572 }
ee50adbe
PP
573 if ($^O eq 'os390') {
574 push @enc, qw(IBM-037 IBM-819 IBM-1047);
575 }
8ebc5c01 576
284102e8 577 return @enc;
8ebc5c01
PP
578}
579
284102e8
JH
580trylocale("C");
581trylocale("POSIX");
582foreach (0..15) {
583 trylocale("ISO8859-$_");
284102e8 584 trylocale("iso8859$_");
097ee67d
JH
585 trylocale("iso8859-$_");
586 trylocale("iso_8859_$_");
587 trylocale("isolatin$_");
588 trylocale("isolatin-$_");
589 trylocale("iso_latin_$_");
8ebc5c01
PP
590}
591
645e49ed
JH
592# Sanitize the environment so that we can run the external 'locale'
593# program without the taint mode getting grumpy.
cce5967e
JH
594
595# $ENV{PATH} is special in VMS.
596delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
597
598# Other subversive stuff.
599delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
dd8482fc 600
21477fb4 601if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
dd8482fc 602 while (<LOCALES>) {
d281a6ac
NC
603 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
604 # ain't great when we're running this testPERL_UNICODE= so that utf8
605 # locales will cause all IO hadles to default to (assume) utf8
606 next unless utf8::valid($_);
dd8482fc
JH
607 chomp;
608 trylocale($_);
284102e8 609 }
dd8482fc 610 close(LOCALES);
a6259068 611} elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
71e5cbb3 612# The SYS$I18N_LOCALE logical name search list was not present on
a6259068
PP
613# VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
614 opendir(LOCALES, "SYS\$I18N_LOCALE:");
615 while ($_ = readdir(LOCALES)) {
616 chomp;
617 trylocale($_);
618 }
619 close(LOCALES);
87e33296
SP
620} elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
621
622 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
623 # is much easier and faster than the last resort method.
624
625 opendir(LOCALES, '/usr/share/locale');
626 while ($_ = readdir(LOCALES)) {
627 chomp;
628 trylocale($_);
629 }
630 close(LOCALES);
dd8482fc
JH
631} else {
632
633 # This is going to be slow.
634
635 foreach my $locale (split(/\n/, $locales)) {
636 my ($locale_name, $language_codes, $country_codes, $encodings) =
637 split(/:/, $locale);
638 my @enc = decode_encodings($encodings);
639 foreach my $loc (split(/ /, $locale_name)) {
640 trylocale($loc);
284102e8 641 foreach my $enc (@enc) {
dd8482fc 642 trylocale("$loc.$enc");
284102e8 643 }
dd8482fc 644 $loc = lc $loc;
284102e8 645 foreach my $enc (@enc) {
dd8482fc
JH
646 trylocale("$loc.$enc");
647 }
648 }
649 foreach my $lang (split(/ /, $language_codes)) {
650 trylocale($lang);
651 foreach my $country (split(/ /, $country_codes)) {
652 my $lc = "${lang}_${country}";
653 trylocale($lc);
654 foreach my $enc (@enc) {
655 trylocale("$lc.$enc");
656 }
657 my $lC = "${lang}_\U${country}";
658 trylocale($lC);
659 foreach my $enc (@enc) {
660 trylocale("$lC.$enc");
661 }
284102e8
JH
662 }
663 }
664 }
665}
4599a1de 666
a810e350 667setlocale(&POSIX::LC_ALL, "C");
d43ce814 668
86f50d7d 669if ($^O eq 'darwin') {
4373e181 670 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
86f50d7d 671 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
4373e181
RGS
672 (my $v) = $Config{osvers} =~ /^(\d+)/;
673 if ($v >= 8 and $v < 10) {
86f50d7d 674 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
a5ec937f 675 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
dfa5c78f 676 } elsif ($v < 12) {
a44d0896
NC
677 debug "# Skipping be_BY locales -- buggy in Darwin\n";
678 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
a5ec937f 679 }
86f50d7d
DD
680}
681
4599a1de
JH
682@Locale = sort @Locale;
683
887ef7ed
PP
684debug "# Locales =\n";
685for ( @Locale ) {
686 debug "# $_\n";
687}
8ebc5c01 688
284102e8 689my %Problem;
2a680da6
JH
690my %Okay;
691my %Testing;
30032ef4 692my @Added_alpha; # Alphas that aren't in the C locale.
c08acc4c 693my %test_names;
284102e8 694
019bf7dd
KW
695sub display_characters {
696 # This returns a display string denoting the input parameter @_, each
697 # entry of which is a single character in the range 0-255. The first part
698 # of the output is a string of the characters in @_ that are ASCII
699 # graphics, and hence unambiguously displayable. They are given by code
700 # point order. The second part is the remaining code points, the ordinals
701 # of which are each displayed as 2-digit hex. Blanks are inserted so as
702 # to keep anything from the first part looking like a 2-digit hex number.
703
704 no locale;
705 my @chars = sort { ord $a <=> ord $b } @_;
706 my $output = "";
707 my $hex = "";
708 my $range_start;
709 my $start_class;
710 push @chars, chr(258); # This sentinel simplifies the loop termination
711 # logic
712 foreach my $i (0 .. @chars - 1) {
713 my $char = $chars[$i];
714 my $range_end;
715 my $class;
716
717 # We avoid using [:posix:] classes, as these are being tested in this
718 # file. Each equivalence class below is for things that can appear in
719 # a range; those that can't be in a range have class -1. 0 for those
720 # which should be output in hex; and >0 for the other ranges
721 if ($char =~ /[A-Z]/) {
722 $class = 2;
723 }
724 elsif ($char =~ /[a-z]/) {
725 $class = 3;
726 }
727 elsif ($char =~ /[0-9]/) {
728 $class = 4;
729 }
730 elsif ($char =~ /[[\]!"#\$\%&\'()*+,.\/:\\;<=>?\@\^_`{|}~-]/) {
731 $class = -1; # Punct never appears in a range
732 }
733 else {
734 $class = 0; # Output in hex
735 }
736
737 if (! defined $range_start) {
738 if ($class < 0) {
739 $output .= $char;
740 }
741 else {
742 $range_start = ord $char;
743 $start_class = $class;
744 }
745 } # A range ends if not consecutive, or the class-type changes
746 elsif (ord $char != ($range_end = ord($chars[$i-1])) + 1
747 || $class != $start_class)
748 {
749
750 # Here, the current character is not in the range. This means the
751 # previous character must have been. Output the range up through
752 # that one.
753 my $range_length = $range_end - $range_start + 1;
754 if ($start_class > 0) {
755 $output .= " " . chr($range_start);
756 $output .= "-" . chr($range_end) if $range_length > 1;
757 }
758 else {
759 $hex .= sprintf(" %02X", $range_start);
760 $hex .= sprintf("-%02X", $range_end) if $range_length > 1;
761 }
762
763 # Handle the new current character, as potentially beginning a new
764 # range
765 undef $range_start;
766 redo;
767 }
768 }
769
770 $output =~ s/^ //;
771 $hex =~ s/^ // if ! length $output;
772 return "$output$hex";
773}
774
30032ef4
KW
775sub report_result {
776 my ($Locale, $i, $pass_fail, $message) = @_;
15bbd6a2
KW
777 $message //= "";
778 $message = " ($message)" if $message;
30032ef4 779 unless ($pass_fail) {
2a680da6 780 $Problem{$i}{$Locale} = 1;
baae13cb 781 debug "# failed $i ($test_names{$i}) with locale '$Locale'$message\n";
2a680da6
JH
782 } else {
783 push @{$Okay{$i}}, $Locale;
784 }
785}
786
7c844d17
KW
787sub report_multi_result {
788 my ($Locale, $i, $results_ref) = @_;
789
790 # $results_ref points to an array, each element of which is a character that was
791 # in error for this test numbered '$i'. If empty, the test passed
792
793 my $message = "";
794 if (@$results_ref) {
019bf7dd 795 $message = join " ", "for", display_characters(@$results_ref);
7c844d17
KW
796 }
797 report_result($Locale, $i, @$results_ref == 0, $message);
798}
799
c4093d7d
KW
800my $first_locales_test_number = $final_without_setlocale + 1;
801my $locales_test_number;
802my $not_necessarily_a_problem_test_number;
6d5d702a
KW
803my $first_casing_test_number;
804my $final_casing_test_number;
c4093d7d
KW
805my %setlocale_failed; # List of locales that setlocale() didn't work on
806
284102e8 807foreach $Locale (@Locale) {
c4093d7d 808 $locales_test_number = $first_locales_test_number - 1;
284102e8 809 debug "# Locale = $Locale\n";
284102e8 810
a810e350 811 unless (setlocale(&POSIX::LC_ALL, $Locale)) {
c4093d7d 812 $setlocale_failed{$Locale} = $Locale;
284102e8 813 next;
8ebc5c01 814 }
8ebc5c01 815
66cbab2c
KW
816 # We test UTF-8 locales only under ':not_characters'; otherwise they have
817 # documented deficiencies. Non- UTF-8 locales are tested only under plain
818 # 'use locale', as otherwise we would have to convert everything in them
819 # to Unicode.
820 my $is_utf8_locale = $Locale =~ /UTF-?8/i;
821
95eaa1bf
KW
822 my %UPPER = (); # All alpha X for which uc(X) == X and lc(X) != X
823 my %lower = (); # All alpha X for which lc(X) == X and uc(X) != X
824 my %BoThCaSe = (); # All alpha X for which uc(X) == lc(X) == X
66cbab2c
KW
825
826 if (! $is_utf8_locale) {
827 use locale;
a160ac48 828 @Word_ = grep /\w/, map { chr } 0..255;
aac995d2 829 @Digit_ = grep /\d/, map { chr } 0..255;
a160ac48
KW
830 @Space_ = grep /\s/, map { chr } 0..255;
831 @Alpha_ = grep /[[:alpha:]]/, map {chr } 0..255;
832 @Alnum_ = grep /[[:alnum:]]/, map {chr } 0..255;
833 @Ascii_ = grep /[[:ascii:]]/, map {chr } 0..255;
834 @Blank_ = grep /[[:blank:]]/, map {chr } 0..255;
835 @Cntrl_ = grep /[[:cntrl:]]/, map {chr } 0..255;
836 @Graph_ = grep /[[:graph:]]/, map {chr } 0..255;
837 @Lower_ = grep /[[:lower:]]/, map {chr } 0..255;
838 @Print_ = grep /[[:print:]]/, map {chr } 0..255;
839 @Upper_ = grep /[[:upper:]]/, map {chr } 0..255;
840 @Xdigit_ = grep /[[:xdigit:]]/, map {chr } 0..255;
841 @Cased_ = grep /[[:upper:]]/i, map {chr } 0..255;
e5272a46 842
71e5cbb3
KW
843 # Sieve the uppercase and the lowercase.
844
519c0534 845 for (@Word_) {
71e5cbb3
KW
846 if (/[^\d_]/) { # skip digits and the _
847 if (uc($_) eq $_) {
848 $UPPER{$_} = $_;
849 }
850 if (lc($_) eq $_) {
851 $lower{$_} = $_;
852 }
853 }
854 }
66cbab2c
KW
855 }
856 else {
857 use locale ':not_characters';
a160ac48 858 @Word_ = grep /\w/, map { chr } 0..255;
aac995d2 859 @Digit_ = grep /\d/, map { chr } 0..255;
a160ac48
KW
860 @Space_ = grep /\s/, map { chr } 0..255;
861 @Alpha_ = grep /[[:alpha:]]/, map {chr } 0..255;
862 @Alnum_ = grep /[[:alnum:]]/, map {chr } 0..255;
863 @Ascii_ = grep /[[:ascii:]]/, map {chr } 0..255;
864 @Blank_ = grep /[[:blank:]]/, map {chr } 0..255;
865 @Cntrl_ = grep /[[:cntrl:]]/, map {chr } 0..255;
866 @Graph_ = grep /[[:graph:]]/, map {chr } 0..255;
867 @Lower_ = grep /[[:lower:]]/, map {chr } 0..255;
868 @Print_ = grep /[[:print:]]/, map {chr } 0..255;
869 @Upper_ = grep /[[:upper:]]/, map {chr } 0..255;
870 @Xdigit_ = grep /[[:xdigit:]]/, map {chr } 0..255;
871 @Cased_ = grep /[[:upper:]]/i, map {chr } 0..255;
519c0534 872 for (@Word_) {
66cbab2c
KW
873 if (/[^\d_]/) { # skip digits and the _
874 if (uc($_) eq $_) {
875 $UPPER{$_} = $_;
876 }
877 if (lc($_) eq $_) {
878 $lower{$_} = $_;
879 }
880 }
881 }
882 }
a160ac48
KW
883
884 debug "# :upper: = ", display_characters(@Upper_), "\n";
885 debug "# :lower: = ", display_characters(@Lower_), "\n";
886 debug "# :cased: = ", display_characters(@Cased_), "\n";
887 debug "# :alpha: = ", display_characters(@Alpha_), "\n";
888 debug "# :alnum: = ", display_characters(@Alnum_), "\n";
889 debug "# w = ", display_characters(@Word_), "\n";
890 debug "# :graph: = ", display_characters(@Graph_), "\n";
891 debug "# :print: = ", display_characters(@Print_), "\n";
892 debug "# d = ", display_characters(@Digit_), "\n";
893 debug "# :xdigit: = ", display_characters(@Xdigit_), "\n";
894 debug "# :blank: = ", display_characters(@Blank_), "\n";
895 debug "# s = ", display_characters(@Space_), "\n";
896 debug "# :cntrl: = ", display_characters(@Cntrl_), "\n";
897 debug "# :ascii: = ", display_characters(@Ascii_), "\n";
898
284102e8 899 foreach (keys %UPPER) {
a160ac48 900
097ee67d 901 $BoThCaSe{$_}++ if exists $lower{$_};
284102e8
JH
902 }
903 foreach (keys %lower) {
097ee67d 904 $BoThCaSe{$_}++ if exists $UPPER{$_};
284102e8 905 }
097ee67d 906 foreach (keys %BoThCaSe) {
284102e8
JH
907 delete $UPPER{$_};
908 delete $lower{$_};
909 }
910
019bf7dd
KW
911 debug "# UPPER = ", display_characters(keys %UPPER), "\n";
912 debug "# lower = ", display_characters(keys %lower), "\n";
913 debug "# BoThCaSe = ", display_characters(keys %BoThCaSe), "\n";
284102e8 914
baa71cfd 915 my @failures;
3da38613 916 my @fold_failures;
baa71cfd
KW
917 foreach my $x (sort keys %UPPER) {
918 my $ok;
3da38613 919 my $fold_ok;
baa71cfd
KW
920 if ($is_utf8_locale) {
921 use locale ':not_characters';
922 $ok = $x =~ /[[:upper:]]/;
3da38613 923 $fold_ok = $x =~ /[[:lower:]]/i;
baa71cfd
KW
924 }
925 else {
926 use locale;
927 $ok = $x =~ /[[:upper:]]/;
3da38613 928 $fold_ok = $x =~ /[[:lower:]]/i;
baa71cfd
KW
929 }
930 push @failures, $x unless $ok;
3da38613 931 push @fold_failures, $x unless $fold_ok;
baa71cfd 932 }
baa71cfd 933 $locales_test_number++;
6d5d702a 934 $first_casing_test_number = $locales_test_number;
95eaa1bf 935 $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/ matches all alpha X for which uc(X) == X and lc(X) != X';
7c844d17 936 report_multi_result($Locale, $locales_test_number, \@failures);
6d5d702a 937
3da38613 938 $locales_test_number++;
6d5d702a 939
95eaa1bf 940 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/i matches all alpha X for which uc(X) == X and lc(X) != X';
7c844d17 941 report_multi_result($Locale, $locales_test_number, \@fold_failures);
baa71cfd 942
baa71cfd 943 undef @failures;
3da38613 944 undef @fold_failures;
baa71cfd
KW
945
946 foreach my $x (sort keys %lower) {
947 my $ok;
3da38613 948 my $fold_ok;
baa71cfd
KW
949 if ($is_utf8_locale) {
950 use locale ':not_characters';
951 $ok = $x =~ /[[:lower:]]/;
3da38613 952 $fold_ok = $x =~ /[[:upper:]]/i;
baa71cfd
KW
953 }
954 else {
955 use locale;
956 $ok = $x =~ /[[:lower:]]/;
3da38613 957 $fold_ok = $x =~ /[[:upper:]]/i;
baa71cfd
KW
958 }
959 push @failures, $x unless $ok;
3da38613 960 push @fold_failures, $x unless $fold_ok;
baa71cfd
KW
961 }
962
963 $locales_test_number++;
95eaa1bf 964 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/ matches all alpha X for which lc(X) == X and uc(X) != X';
7c844d17
KW
965 report_multi_result($Locale, $locales_test_number, \@failures);
966
3da38613 967 $locales_test_number++;
95eaa1bf 968 $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/i matches all alpha X for which lc(X) == X and uc(X) != X';
7c844d17 969 report_multi_result($Locale, $locales_test_number, \@fold_failures);
baa71cfd 970
9445c837
KW
971 { # Find the alphabetic characters that are not considered alphabetics
972 # in the default (C) locale.
8ebc5c01 973
284102e8 974 no locale;
71e5cbb3 975
30032ef4 976 @Added_alpha = ();
5e7a1028 977 for (keys %UPPER, keys %lower, keys %BoThCaSe) {
30032ef4 978 push(@Added_alpha, $_) if (/\W/);
284102e8 979 }
8ebc5c01 980 }
8ebc5c01 981
30032ef4 982 @Added_alpha = sort @Added_alpha;
8ebc5c01 983
019bf7dd 984 debug "# Added_alpha = ", display_characters(@Added_alpha), "\n";
8ebc5c01 985
db31898d 986 # Cross-check the whole 8-bit character set.
8ebc5c01 987
db31898d
KW
988 ++$locales_test_number;
989 my @f;
990 $test_names{$locales_test_number} = 'Verify that \w and [:word:] are identical';
991 for (map { chr } 0..255) {
992 if ($is_utf8_locale) {
993 use locale ':not_characters';
994 push @f, $_ unless /[[:word:]]/ == /\w/;
995 }
996 else {
997 push @f, $_ unless /[[:word:]]/ == /\w/;
998 }
999 }
1000 report_multi_result($Locale, $locales_test_number, \@f);
8ebc5c01 1001
db31898d
KW
1002 ++$locales_test_number;
1003 undef @f;
1004 $test_names{$locales_test_number} = 'Verify that \d and [:digit:] are identical';
1005 for (map { chr } 0..255) {
1006 if ($is_utf8_locale) {
1007 use locale ':not_characters';
1008 push @f, $_ unless /[[:digit:]]/ == /\d/;
1009 }
1010 else {
1011 push @f, $_ unless /[[:digit:]]/ == /\d/;
1012 }
1013 }
1014 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1015
db31898d
KW
1016 ++$locales_test_number;
1017 undef @f;
1018 $test_names{$locales_test_number} = 'Verify that \s and [:space:] are identical';
1019 for (map { chr } 0..255) {
1020 if ($is_utf8_locale) {
1021 use locale ':not_characters';
1022 push @f, $_ unless /[[:space:]]/ == /\s/;
1023 }
1024 else {
1025 push @f, $_ unless /[[:space:]]/ == /\s/;
1026 }
1027 }
1028 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1029
db31898d
KW
1030 ++$locales_test_number;
1031 undef @f;
1032 $test_names{$locales_test_number} = 'Verify that [:posix:] and [:^posix:] are mutually exclusive';
1033 for (map { chr } 0..255) {
1034 if ($is_utf8_locale) {
1035 use locale ':not_characters';
1036 push @f, $_ unless (/[[:alpha:]]/ xor /[[:^alpha:]]/) ||
1037 (/[[:alnum:]]/ xor /[[:^alnum:]]/) ||
1038 (/[[:ascii:]]/ xor /[[:^ascii:]]/) ||
1039 (/[[:blank:]]/ xor /[[:^blank:]]/) ||
1040 (/[[:cntrl:]]/ xor /[[:^cntrl:]]/) ||
1041 (/[[:digit:]]/ xor /[[:^digit:]]/) ||
1042 (/[[:graph:]]/ xor /[[:^graph:]]/) ||
1043 (/[[:lower:]]/ xor /[[:^lower:]]/) ||
1044 (/[[:print:]]/ xor /[[:^print:]]/) ||
1045 (/[[:space:]]/ xor /[[:^space:]]/) ||
1046 (/[[:upper:]]/ xor /[[:^upper:]]/) ||
1047 (/[[:word:]]/ xor /[[:^word:]]/) ||
1048 (/[[:xdigit:]]/ xor /[[:^xdigit:]]/) ||
1049
1050 # effectively is what [:cased:] would be if it existed.
1051 (/[[:upper:]]/i xor /[[:^upper:]]/i);
1052 }
1053 else {
1054 push @f, $_ unless (/[[:alpha:]]/ xor /[[:^alpha:]]/) ||
1055 (/[[:alnum:]]/ xor /[[:^alnum:]]/) ||
1056 (/[[:ascii:]]/ xor /[[:^ascii:]]/) ||
1057 (/[[:blank:]]/ xor /[[:^blank:]]/) ||
1058 (/[[:cntrl:]]/ xor /[[:^cntrl:]]/) ||
1059 (/[[:digit:]]/ xor /[[:^digit:]]/) ||
1060 (/[[:graph:]]/ xor /[[:^graph:]]/) ||
1061 (/[[:lower:]]/ xor /[[:^lower:]]/) ||
1062 (/[[:print:]]/ xor /[[:^print:]]/) ||
1063 (/[[:space:]]/ xor /[[:^space:]]/) ||
1064 (/[[:upper:]]/ xor /[[:^upper:]]/) ||
1065 (/[[:word:]]/ xor /[[:^word:]]/) ||
1066 (/[[:xdigit:]]/ xor /[[:^xdigit:]]/) ||
1067 (/[[:upper:]]/i xor /[[:^upper:]]/i);
1068 }
1069 }
1070 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1071
db31898d
KW
1072 # The rules for the relationships are given in:
1073 # http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html
32e8aa3f 1074
db31898d
KW
1075 ++$locales_test_number;
1076 undef @f;
1077 $test_names{$locales_test_number} = 'Verify that [:lower:] is a subset of [:alpha:]';
1078 for (map { chr } 0..255) {
1079 if ($is_utf8_locale) {
1080 use locale ':not_characters';
1081 push @f, $_ if /[[:lower:]]/ and ! /[[:alpha:]]/;
1082 }
1083 else {
1084 push @f, $_ if /[[:lower:]]/ and ! /[[:alpha:]]/;
1085 }
1086 }
1087 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1088
db31898d
KW
1089 ++$locales_test_number;
1090 undef @f;
1091 $test_names{$locales_test_number} = 'Verify that [:upper:] is a subset of [:alpha:]';
1092 for (map { chr } 0..255) {
1093 if ($is_utf8_locale) {
1094 use locale ':not_characters';
1095 push @f, $_ if /[[:upper:]]/ and ! /[[:alpha:]]/;
1096 }
1097 else {
1098 push @f, $_ if /[[:upper:]]/ and ! /[[:alpha:]]/;
1099 }
1100 }
1101 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1102
db31898d
KW
1103 ++$locales_test_number;
1104 undef @f;
1105 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/i is a subset of [:alpha:]';
1106 for (map { chr } 0..255) {
1107 if ($is_utf8_locale) {
1108 use locale ':not_characters';
1109 push @f, $_ if /[[:lower:]]/i and ! /[[:alpha:]]/;
1110 }
1111 else {
1112 push @f, $_ if /[[:lower:]]/i and ! /[[:alpha:]]/;
1113 }
1114 }
1115 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1116
db31898d
KW
1117 ++$locales_test_number;
1118 undef @f;
1119 $test_names{$locales_test_number} = 'Verify that [:alpha:] is a subset of [:alnum:]';
1120 for (map { chr } 0..255) {
1121 if ($is_utf8_locale) {
1122 use locale ':not_characters';
1123 push @f, $_ if /[[:alpha:]]/ and ! /[[:alnum:]]/;
1124 }
1125 else {
1126 push @f, $_ if /[[:alpha:]]/ and ! /[[:alnum:]]/;
1127 }
1128 }
1129 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1130
db31898d
KW
1131 ++$locales_test_number;
1132 undef @f;
1133 $test_names{$locales_test_number} = 'Verify that [:digit:] is a subset of [:alnum:]';
1134 for (map { chr } 0..255) {
1135 if ($is_utf8_locale) {
1136 use locale ':not_characters';
1137 push @f, $_ if /[[:digit:]]/ and ! /[[:alnum:]]/;
1138 }
1139 else {
1140 push @f, $_ if /[[:digit:]]/ and ! /[[:alnum:]]/;
1141 }
1142 }
1143 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1144
db31898d
KW
1145 ++$locales_test_number;
1146 undef @f;
aac995d2
KW
1147 $test_names{$locales_test_number} = 'Verify that [:digit:] matches either 10 or 20 code points';
1148 report_result($Locale, $locales_test_number, @Digit_ == 10 || @Digit_ ==20);
1149
1150 ++$locales_test_number;
1151 undef @f;
1152 $test_names{$locales_test_number} = 'Verify that [:digit:] (if is 10 code points) is a subset of [:xdigit:]';
1153 if (@Digit_ == 10) {
fc81f5f2
KW
1154 for (map { chr } 0..255) {
1155 if ($is_utf8_locale) {
1156 use locale ':not_characters';
1157 push @f, $_ if /[[:digit:]]/ and ! /[[:xdigit:]]/;
1158 }
1159 else {
1160 push @f, $_ if /[[:digit:]]/ and ! /[[:xdigit:]]/;
1161 }
db31898d
KW
1162 }
1163 }
1164 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1165
db31898d
KW
1166 ++$locales_test_number;
1167 undef @f;
1168 $test_names{$locales_test_number} = 'Verify that [:alnum:] is a subset of [:graph:]';
1169 for (map { chr } 0..255) {
1170 if ($is_utf8_locale) {
1171 use locale ':not_characters';
1172 push @f, $_ if /[[:alnum:]]/ and ! /[[:graph:]]/;
1173 }
1174 else {
1175 push @f, $_ if /[[:alnum:]]/ and ! /[[:graph:]]/;
1176 }
1177 }
1178 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1179
db31898d 1180 # Note that xdigit doesn't have to be a subset of alnum
32e8aa3f 1181
db31898d
KW
1182 ++$locales_test_number;
1183 undef @f;
1184 $test_names{$locales_test_number} = 'Verify that [:xdigit:] is a subset of [:graph:]';
1185 for (map { chr } 0..255) {
1186 if ($is_utf8_locale) {
1187 use locale ':not_characters';
1188 push @f, $_ if /[[:xdigit:]]/ and ! /[[:graph:]]/;
1189 }
1190 else {
1191 push @f, $_ if /[[:xdigit:]]/ and ! /[[:graph:]]/;
1192 }
1193 }
1194 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1195
db31898d
KW
1196 ++$locales_test_number;
1197 undef @f;
1198 $test_names{$locales_test_number} = 'Verify that [:punct:] is a subset of [:graph:]';
1199 for (map { chr } 0..255) {
1200 if ($is_utf8_locale) {
1201 use locale ':not_characters';
1202 push @f, $_ if /[[:punct:]]/ and ! /[[:graph:]]/;
1203 }
1204 else {
1205 push @f, $_ if /[[:punct:]]/ and ! /[[:graph:]]/;
1206 }
1207 }
1208 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1209
db31898d
KW
1210 ++$locales_test_number;
1211 undef @f;
1212 $test_names{$locales_test_number} = 'Verify that [:blank:] is a subset of [:space:]';
1213 for (map { chr } 0..255) {
1214 if ($is_utf8_locale) {
1215 use locale ':not_characters';
1216 push @f, $_ if /[[:blank:]]/ and ! /[[:space:]]/;
1217 }
1218 else {
1219 push @f, $_ if /[[:blank:]]/ and ! /[[:space:]]/;
1220 }
1221 }
1222 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1223
db31898d
KW
1224 ++$locales_test_number;
1225 undef @f;
1226 $test_names{$locales_test_number} = 'Verify that [:graph:] is a subset of [:print:]';
1227 for (map { chr } 0..255) {
1228 if ($is_utf8_locale) {
1229 use locale ':not_characters';
1230 push @f, $_ if /[[:graph:]]/ and ! /[[:print:]]/;
1231 }
1232 else {
1233 push @f, $_ if /[[:graph:]]/ and ! /[[:print:]]/;
1234 }
1235 }
1236 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1237
db31898d
KW
1238 ++$locales_test_number;
1239 undef @f;
1240 $test_names{$locales_test_number} = 'Verify that isn\'t both [:cntrl:] and [:print:]';
1241 for (map { chr } 0..255) {
1242 if ($is_utf8_locale) {
1243 use locale ':not_characters';
1244 push @f, $_ if (/[[:print:]]/ and /[[:cntrl:]]/);
1245 }
1246 else {
1247 push @f, $_ if (/[[:print:]]/ and /[[:cntrl:]]/);
1248 }
1249 }
1250 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1251
db31898d
KW
1252 ++$locales_test_number;
1253 undef @f;
1254 $test_names{$locales_test_number} = 'Verify that isn\'t both [:alnum:] and [:punct:]';
1255 for (map { chr } 0..255) {
1256 if ($is_utf8_locale) {
1257 use locale ':not_characters';
1258 push @f, $_ if /[[:alnum:]]/ and /[[:punct:]]/;
1259 }
1260 else {
1261 push @f, $_ if /[[:alnum:]]/ and /[[:punct:]]/;
1262 }
1263 }
1264 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1265
db31898d
KW
1266 ++$locales_test_number;
1267 undef @f;
1268 $test_names{$locales_test_number} = 'Verify that isn\'t both [:xdigit:] and [:punct:]';
1269 for (map { chr } 0..255) {
1270 if ($is_utf8_locale) {
1271 use locale ':not_characters';
1272 push @f, $_ if (/[[:punct:]]/ and /[[:xdigit:]]/);
1273 }
1274 else {
1275 push @f, $_ if (/[[:punct:]]/ and /[[:xdigit:]]/);
1276 }
1277 }
1278 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1279
db31898d
KW
1280 ++$locales_test_number;
1281 undef @f;
1282 $test_names{$locales_test_number} = 'Verify that isn\'t both [:graph:] and [:space:]';
1283 for (map { chr } 0..255) {
1284 if ($is_utf8_locale) {
1285 use locale ':not_characters';
1286 push @f, $_ if (/[[:graph:]]/ and /[[:space:]]/);
1287 }
1288 else {
1289 push @f, $_ if (/[[:graph:]]/ and /[[:space:]]/);
1290 }
1291 }
1292 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1293
db31898d 1294 $final_casing_test_number = $locales_test_number;
32e8aa3f 1295
db31898d
KW
1296 # Test for read-only scalars' locale vs non-locale comparisons.
1297
1298 {
1299 no locale;
1300 my $ok;
1301 $a = "qwerty";
1302 if ($is_utf8_locale) {
1303 use locale ':not_characters';
1304 $ok = ($a cmp "qwerty") == 0;
1305 }
1306 else {
1307 use locale;
1308 $ok = ($a cmp "qwerty") == 0;
1309 }
1310 report_result($Locale, ++$locales_test_number, $ok);
1311 $test_names{$locales_test_number} = 'Verify that cmp works with a read-only scalar; no- vs locale';
1312 }
8ebc5c01 1313
db31898d
KW
1314 {
1315 my ($from, $to, $lesser, $greater,
1316 @test, %test, $test, $yes, $no, $sign);
284102e8 1317
db31898d
KW
1318 ++$locales_test_number;
1319 $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work';
1320 $not_necessarily_a_problem_test_number = $locales_test_number;
1321 for (0..9) {
1322 # Select a slice.
519c0534
KW
1323 $from = int(($_*@Word_)/10);
1324 $to = $from + int(@Word_/10);
1325 $to = $#Word_ if ($to > $#Word_);
1326 $lesser = join('', @Word_[$from..$to]);
db31898d
KW
1327 # Select a slice one character on.
1328 $from++; $to++;
519c0534
KW
1329 $to = $#Word_ if ($to > $#Word_);
1330 $greater = join('', @Word_[$from..$to]);
66cbab2c
KW
1331 if ($is_utf8_locale) {
1332 use locale ':not_characters';
db31898d
KW
1333 ($yes, $no, $sign) = ($lesser lt $greater
1334 ? (" ", "not ", 1)
1335 : ("not ", " ", -1));
66cbab2c
KW
1336 }
1337 else {
1338 use locale;
db31898d
KW
1339 ($yes, $no, $sign) = ($lesser lt $greater
1340 ? (" ", "not ", 1)
1341 : ("not ", " ", -1));
66cbab2c 1342 }
db31898d
KW
1343 # all these tests should FAIL (return 0). Exact lt or gt cannot
1344 # be tested because in some locales, say, eacute and E may test
1345 # equal.
1346 @test =
1347 (
1348 $no.' ($lesser le $greater)', # 1
1349 'not ($lesser ne $greater)', # 2
1350 ' ($lesser eq $greater)', # 3
1351 $yes.' ($lesser ge $greater)', # 4
1352 $yes.' ($lesser ge $greater)', # 5
1353 $yes.' ($greater le $lesser )', # 7
1354 'not ($greater ne $lesser )', # 8
1355 ' ($greater eq $lesser )', # 9
1356 $no.' ($greater ge $lesser )', # 10
1357 'not (($lesser cmp $greater) == -($sign))' # 11
1358 );
1359 @test{@test} = 0 x @test;
1360 $test = 0;
1361 for my $ti (@test) {
66cbab2c
KW
1362 if ($is_utf8_locale) {
1363 use locale ':not_characters';
db31898d 1364 $test{$ti} = eval $ti;
66cbab2c
KW
1365 }
1366 else {
db31898d
KW
1367 # Already in 'use locale';
1368 $test{$ti} = eval $ti;
66cbab2c 1369 }
db31898d
KW
1370 $test ||= $test{$ti}
1371 }
1372 report_result($Locale, $locales_test_number, $test == 0);
1373 if ($test) {
1374 debug "# lesser = '$lesser'\n";
1375 debug "# greater = '$greater'\n";
1376 debug "# lesser cmp greater = ",
1377 $lesser cmp $greater, "\n";
1378 debug "# greater cmp lesser = ",
1379 $greater cmp $lesser, "\n";
1380 debug "# (greater) from = $from, to = $to\n";
1381 for my $ti (@test) {
1382 debugf("# %-40s %-4s", $ti,
1383 $test{$ti} ? 'FAIL' : 'ok');
1384 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
1385 debugf("(%s == %4d)", $1, eval $1);
66cbab2c 1386 }
db31898d
KW
1387 debug "\n#";
1388 }
1389
1390 last;
1391 }
1392 }
1393 }
c4093d7d 1394
66cbab2c
KW
1395 my $ok1;
1396 my $ok2;
1397 my $ok3;
1398 my $ok4;
1399 my $ok5;
1400 my $ok6;
1401 my $ok7;
1402 my $ok8;
1403 my $ok9;
1404 my $ok10;
1405 my $ok11;
1406 my $ok12;
1407 my $ok13;
1500bd91 1408 my $ok14;
28acfe03
KW
1409 my $ok15;
1410 my $ok16;
66cbab2c
KW
1411
1412 my $c;
1413 my $d;
1414 my $e;
1415 my $f;
1416 my $g;
1417
1418 if (! $is_utf8_locale) {
71e5cbb3 1419 use locale;
6be75cd7 1420
71e5cbb3 1421 my ($x, $y) = (1.23, 1.23);
6be75cd7 1422
71e5cbb3
KW
1423 $a = "$x";
1424 printf ''; # printf used to reset locale to "C"
1425 $b = "$y";
1426 $ok1 = $a eq $b;
6be75cd7 1427
71e5cbb3
KW
1428 $c = "$x";
1429 my $z = sprintf ''; # sprintf used to reset locale to "C"
1430 $d = "$y";
1431 $ok2 = $c eq $d;
1432 {
66cbab2c 1433
71e5cbb3
KW
1434 use warnings;
1435 my $w = 0;
1436 local $SIG{__WARN__} =
1437 sub {
1438 print "# @_\n";
1439 $w++;
1440 };
6be75cd7 1441
71e5cbb3
KW
1442 # The == (among other ops) used to warn for locales
1443 # that had something else than "." as the radix character.
6be75cd7 1444
71e5cbb3
KW
1445 $ok3 = $c == 1.23;
1446 $ok4 = $c == $x;
1447 $ok5 = $c == $d;
1448 {
1449 no locale;
66cbab2c 1450
b79536ea 1451 $e = "$x";
71e5cbb3
KW
1452
1453 $ok6 = $e == 1.23;
1454 $ok7 = $e == $x;
1455 $ok8 = $e == $c;
1456 }
66cbab2c 1457
71e5cbb3
KW
1458 $f = "1.23";
1459 $g = 2.34;
66cbab2c 1460
71e5cbb3
KW
1461 $ok9 = $f == 1.23;
1462 $ok10 = $f == $x;
1463 $ok11 = $f == $c;
1464 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1465 $ok13 = $w == 0;
28acfe03 1466 $ok14 = $ok15 = $ok16 = 1; # Skip for non-utf8 locales
71e5cbb3 1467 }
66cbab2c
KW
1468 }
1469 else {
1470 use locale ':not_characters';
1471
1472 my ($x, $y) = (1.23, 1.23);
1473 $a = "$x";
1474 printf ''; # printf used to reset locale to "C"
1475 $b = "$y";
1476 $ok1 = $a eq $b;
1477
1478 $c = "$x";
1479 my $z = sprintf ''; # sprintf used to reset locale to "C"
1480 $d = "$y";
1481 $ok2 = $c eq $d;
1482 {
1483 use warnings;
1484 my $w = 0;
1485 local $SIG{__WARN__} =
1486 sub {
1487 print "# @_\n";
1488 $w++;
1489 };
1490 $ok3 = $c == 1.23;
1491 $ok4 = $c == $x;
1492 $ok5 = $c == $d;
1493 {
1494 no locale;
b79536ea 1495 $e = "$x";
66cbab2c
KW
1496
1497 $ok6 = $e == 1.23;
1498 $ok7 = $e == $x;
1499 $ok8 = $e == $c;
1500 }
1501
1502 $f = "1.23";
1503 $g = 2.34;
1504
1505 $ok9 = $f == 1.23;
1506 $ok10 = $f == $x;
1507 $ok11 = $f == $c;
1508 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1509 $ok13 = $w == 0;
1500bd91
KW
1510
1511 # Look for non-ASCII error messages, and verify that the first
1512 # such is in UTF-8 (the others almost certainly will be like the
1513 # first).
1514 $ok14 = 1;
1515 foreach my $err (keys %!) {
1516 use Errno;
1517 $! = eval "&Errno::$err"; # Convert to strerror() output
1518 my $strerror = "$!";
1519 if ("$strerror" =~ /\P{ASCII}/) {
1520 my $utf8_strerror = $strerror;
1521 utf8::upgrade($utf8_strerror);
1522
1523 # If $! was already in UTF-8, the upgrade was a no-op;
1524 # otherwise they will be different byte strings.
1525 use bytes;
1526 $ok14 = $utf8_strerror eq $strerror;
1527 last;
1528 }
1529 }
28acfe03
KW
1530
1531 # Similarly, we verify that a non-ASCII radix is in UTF-8. This
1532 # also catches if there is a disparity between sprintf and
1533 # stringification.
1534
1535 my $string_g = "$g";
1536
1537 my $utf8_string_g = "$g";
1538 utf8::upgrade($utf8_string_g);
1539
1540 my $utf8_sprintf_g = sprintf("%g", $g);
1541 utf8::upgrade($utf8_sprintf_g);
1542 use bytes;
1543 $ok15 = $utf8_string_g eq $string_g;
1544 $ok16 = $utf8_sprintf_g eq $string_g;
66cbab2c
KW
1545 }
1546 }
1547
30032ef4 1548 report_result($Locale, ++$locales_test_number, $ok1);
66cbab2c
KW
1549 $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results';
1550 my $first_a_test = $locales_test_number;
1551
1552 debug "# $first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n";
1553
30032ef4 1554 report_result($Locale, ++$locales_test_number, $ok2);
66cbab2c
KW
1555 $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results';
1556
1557 my $first_c_test = $locales_test_number;
1558
30032ef4 1559 report_result($Locale, ++$locales_test_number, $ok3);
71e5cbb3 1560 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant';
6be75cd7 1561
30032ef4 1562 report_result($Locale, ++$locales_test_number, $ok4);
71e5cbb3 1563 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar';
66cbab2c 1564
30032ef4 1565 report_result($Locale, ++$locales_test_number, $ok5);
71e5cbb3 1566 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf';
66cbab2c 1567
71e5cbb3 1568 debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n";
66cbab2c 1569
30032ef4 1570 report_result($Locale, ++$locales_test_number, $ok6);
b79536ea 1571 $test_names{$locales_test_number} = 'Verify that can assign stringified under inner no-locale block';
71e5cbb3 1572 my $first_e_test = $locales_test_number;
6be75cd7 1573
30032ef4 1574 report_result($Locale, ++$locales_test_number, $ok7);
71e5cbb3 1575 $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale';
66cbab2c 1576
30032ef4 1577 report_result($Locale, ++$locales_test_number, $ok8);
71e5cbb3 1578 $test_names{$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale';
c4093d7d 1579
71e5cbb3 1580 debug "# $first_e_test..$locales_test_number: \$e = $e, no locale\n";
2a680da6 1581
30032ef4 1582 report_result($Locale, ++$locales_test_number, $ok9);
71e5cbb3
KW
1583 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant';
1584 my $first_f_test = $locales_test_number;
6be75cd7 1585
30032ef4 1586 report_result($Locale, ++$locales_test_number, $ok10);
71e5cbb3 1587 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar';
66cbab2c 1588
30032ef4 1589 report_result($Locale, ++$locales_test_number, $ok11);
71e5cbb3 1590 $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 1591
30032ef4 1592 report_result($Locale, ++$locales_test_number, $ok12);
71e5cbb3 1593 $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 1594
30032ef4 1595 report_result($Locale, ++$locales_test_number, $ok13);
71e5cbb3 1596 $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot';
c4093d7d 1597
30032ef4 1598 report_result($Locale, ++$locales_test_number, $ok14);
1500bd91
KW
1599 $test_names{$locales_test_number} = 'Verify that non-ASCII UTF-8 error messages are in UTF-8';
1600
30032ef4 1601 report_result($Locale, ++$locales_test_number, $ok15);
28acfe03
KW
1602 $test_names{$locales_test_number} = 'Verify that a number with a UTF-8 radix has a UTF-8 stringification';
1603
30032ef4 1604 report_result($Locale, ++$locales_test_number, $ok16);
28acfe03
KW
1605 $test_names{$locales_test_number} = 'Verify that a sprintf of a number with a UTF-8 radix yields UTF-8';
1606
71e5cbb3 1607 debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n";
906f284f 1608
26d80d95
LC
1609 # Does taking lc separately differ from taking
1610 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
1611 # The bug was in the caching of the 'o'-magic.
66cbab2c 1612 if (! $is_utf8_locale) {
2a680da6 1613 use locale;
6be75cd7 1614
2a680da6
JH
1615 sub lcA {
1616 my $lc0 = lc $_[0];
1617 my $lc1 = lc $_[1];
1618 return $lc0 cmp $lc1;
1619 }
6be75cd7 1620
2a680da6
JH
1621 sub lcB {
1622 return lc($_[0]) cmp lc($_[1]);
1623 }
6be75cd7 1624
2a680da6
JH
1625 my $x = "ab";
1626 my $y = "aa";
1627 my $z = "AB";
6be75cd7 1628
30032ef4 1629 report_result($Locale, ++$locales_test_number,
2a680da6
JH
1630 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
1631 lcA($x, $z) == 0 && lcB($x, $z) == 0);
6be75cd7 1632 }
66cbab2c
KW
1633 else {
1634 use locale ':not_characters';
1635
1636 sub lcC {
1637 my $lc0 = lc $_[0];
1638 my $lc1 = lc $_[1];
1639 return $lc0 cmp $lc1;
1640 }
1641
1642 sub lcD {
1643 return lc($_[0]) cmp lc($_[1]);
1644 }
1645
1646 my $x = "ab";
1647 my $y = "aa";
1648 my $z = "AB";
1649
30032ef4 1650 report_result($Locale, ++$locales_test_number,
66cbab2c
KW
1651 lcC($x, $y) == 1 && lcD($x, $y) == 1 ||
1652 lcC($x, $z) == 0 && lcD($x, $z) == 0);
1653 }
1654 $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp';
d8093b23 1655
26d80d95
LC
1656 # Does lc of an UPPER (if different from the UPPER) match
1657 # case-insensitively the UPPER, and does the UPPER match
1658 # case-insensitively the lc of the UPPER. And vice versa.
3ba0e062 1659 {
ef4a39e5
JH
1660 use locale;
1661 no utf8;
1662 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
1663
1664 my @f = ();
c4093d7d 1665 ++$locales_test_number;
c08acc4c 1666 $test_names{$locales_test_number} = 'Verify case insensitive matching works';
f78d9f29 1667 foreach my $x (sort keys %UPPER) {
66cbab2c 1668 if (! $is_utf8_locale) {
71e5cbb3
KW
1669 my $y = lc $x;
1670 next unless uc $y eq $x;
1671 print "# UPPER $x lc $y ",
faf0c248
KW
1672 $x =~ /$y/i ? 1 : 0, " ",
1673 $y =~ /$x/i ? 1 : 0, "\n" if 0;
71e5cbb3
KW
1674 #
1675 # If $x and $y contain regular expression characters
1676 # AND THEY lowercase (/i) to regular expression characters,
1677 # regcomp() will be mightily confused. No, the \Q doesn't
1678 # help here (maybe regex engine internal lowercasing
1679 # is done after the \Q?) An example of this happening is
1680 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
1681 # the chr(173) (the "[") is the lowercase of the chr(235).
1682 #
1683 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
1684 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
1685 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
1686 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
1687 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
1688 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
1689 #
1690 # Similar things can happen even under (bastardised)
1691 # non-EBCDIC locales: in many European countries before the
1692 # advent of ISO 8859-x nationally customised versions of
1693 # ISO 646 were devised, reusing certain punctuation
1694 # characters for modified characters needed by the
1695 # country/language. For example, the "|" might have
1696 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
1697 #
1698 if ($x =~ $re || $y =~ $re) {
1699 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1700 next;
1701 }
1702 # With utf8 both will fail since the locale concept
1703 # of upper/lower does not work well in Unicode.
1704 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
26c1569f
KW
1705
1706 # fc is not a locale concept, so Perl uses lc for it.
1707 push @f, $x unless lc $x eq fc $x;
66cbab2c
KW
1708 }
1709 else {
1710 use locale ':not_characters';
1711 my $y = lc $x;
1712 next unless uc $y eq $x;
1713 print "# UPPER $x lc $y ",
faf0c248
KW
1714 $x =~ /$y/i ? 1 : 0, " ",
1715 $y =~ /$x/i ? 1 : 0, "\n" if 0;
66cbab2c
KW
1716
1717 # Here, we can fully test things, unlike plain 'use locale',
1718 # because this form does work well with Unicode
1719 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
26c1569f
KW
1720
1721 # The places where Unicode's lc is different from fc are
1722 # skipped here by virtue of the 'next unless uc...' line above
1723 push @f, $x unless lc $x eq fc $x;
66cbab2c 1724 }
c00ff1c7 1725 }
ef4a39e5 1726
f78d9f29 1727 foreach my $x (sort keys %lower) {
66cbab2c 1728 if (! $is_utf8_locale) {
71e5cbb3
KW
1729 my $y = uc $x;
1730 next unless lc $y eq $x;
1731 print "# lower $x uc $y ",
faf0c248
KW
1732 $x =~ /$y/i ? 1 : 0, " ",
1733 $y =~ /$x/i ? 1 : 0, "\n" if 0;
71e5cbb3
KW
1734 if ($x =~ $re || $y =~ $re) { # See above.
1735 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1736 next;
1737 }
1738 # With utf8 both will fail since the locale concept
1739 # of upper/lower does not work well in Unicode.
1740 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
26c1569f
KW
1741
1742 push @f, $x unless lc $x eq fc $x;
66cbab2c
KW
1743 }
1744 else {
1745 use locale ':not_characters';
1746 my $y = uc $x;
1747 next unless lc $y eq $x;
1748 print "# lower $x uc $y ",
faf0c248
KW
1749 $x =~ /$y/i ? 1 : 0, " ",
1750 $y =~ /$x/i ? 1 : 0, "\n" if 0;
66cbab2c 1751 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
26c1569f
KW
1752
1753 push @f, $x unless lc $x eq fc $x;
66cbab2c 1754 }
c00ff1c7 1755 }
7c844d17 1756 report_multi_result($Locale, $locales_test_number, \@f);
d8093b23 1757 }
78787052
JL
1758
1759 # [perl #109318]
1760 {
1761 my @f = ();
1762 ++$locales_test_number;
1763 $test_names{$locales_test_number} = 'Verify atof with locale radix and negative exponent';
1764
1765 my $radix = POSIX::localeconv()->{decimal_point};
1766 my @nums = (
1767 "3.14e+9", "3${radix}14e+9", "3.14e-9", "3${radix}14e-9",
1768 "-3.14e+9", "-3${radix}14e+9", "-3.14e-9", "-3${radix}14e-9",
1769 );
1770
1771 if (! $is_utf8_locale) {
1772 use locale;
1773 for my $num (@nums) {
1774 push @f, $num
1775 unless sprintf("%g", $num) =~ /3.+14/;
1776 }
1777 }
1778 else {
1779 use locale ':not_characters';
1780 for my $num (@nums) {
1781 push @f, $num
1782 unless sprintf("%g", $num) =~ /3.+14/;
1783 }
1784 }
1785
30032ef4 1786 report_result($Locale, $locales_test_number, @f == 0);
78787052
JL
1787 if (@f) {
1788 print "# failed $locales_test_number locale '$Locale' numbers @f\n"
1789 }
1790 }
8ebc5c01 1791}
284102e8 1792
c4093d7d 1793my $final_locales_test_number = $locales_test_number;
6cf0b567 1794
2a680da6
JH
1795# Recount the errors.
1796
c4093d7d
KW
1797foreach ($first_locales_test_number..$final_locales_test_number) {
1798 if (%setlocale_failed) {
1799 print "not ";
1800 }
1801 elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
1802 if (defined $not_necessarily_a_problem_test_number
1803 && $_ == $not_necessarily_a_problem_test_number)
1804 {
1805 print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n";
b4e009be 1806 print "# It usually indicates a problem in the environment,\n";
284102e8
JH
1807 print "# not in Perl itself.\n";
1808 }
6d5d702a
KW
1809 if ($Okay{$_} && ($_ >= $first_casing_test_number
1810 && $_ <= $final_casing_test_number))
1811 {
0a974e2d
KW
1812 # Round to nearest .1%
1813 my $percent_fail = (int(.5 + (1000 * scalar(keys $Problem{$_})
1814 / scalar(@Locale))))
1815 / 10;
f5627fc1
KW
1816 if (! $debug && $percent_fail < $acceptable_fold_failure_percentage)
1817 {
6d5d702a
KW
1818 $test_names{$_} .= 'TODO';
1819 print "# ", 100 - $percent_fail, "% of locales pass the following test, so it is likely that the failures\n";
1820 print "# are errors in the locale definitions. The test is marked TODO, as the\n";
1821 print "# problem is not likely to be Perl's\n";
1822 }
1823 }
f5627fc1
KW
1824 print "#\n";
1825 if ($debug) {
1826 print "# The code points that had this failure are given above. Look for lines\n";
1827 print "# that match 'failed $_'\n";
1828 }
1829 else {
1830 print "# For more details, rerun, with environment variable PERL_DEBUG_FULL_TEST=1.\n";
1831 print "# Then look at that output for lines that match 'failed $_'\n";
108a305e 1832 }
284102e8 1833 print "not ";
8ebc5c01 1834 }
c4093d7d 1835 print "ok $_";
6c2e653d
KW
1836 if (defined $test_names{$_}) {
1837 # If TODO is in the test name, make it thus
1838 my $todo = $test_names{$_} =~ s/TODO\s*//;
1839 print " $test_names{$_}";
1840 print " # TODO" if $todo;
1841 }
c4093d7d 1842 print "\n";
8ebc5c01 1843}
fb73857a 1844
c4093d7d 1845$test_num = $final_locales_test_number;
c213d471 1846
fbd840df
KW
1847{ # perl #115808
1848 use warnings;
1849 my $warned = 0;
1850 local $SIG{__WARN__} = sub {
1851 $warned = $_[0] =~ /uninitialized/;
1852 };
1853 my $z = "y" . setlocale(&POSIX::LC_ALL, "xyzzy");
1854 ok($warned, "variable set to setlocale(BAD LOCALE) is considered uninitialized");
1855}
1856
094a2f8c 1857# Test that tainting and case changing works on utf8 strings. These tests are
1f5852c9
KW
1858# placed last to avoid disturbing the hard-coded test numbers that existed at
1859# the time these were added above this in this file.
0099bb8d
KW
1860# This also tests that locale overrides unicode_strings in the same scope for
1861# non-utf8 strings.
a810e350 1862setlocale(&POSIX::LC_ALL, "C");
094a2f8c
KW
1863{
1864 use locale;
0099bb8d 1865 use feature 'unicode_strings';
094a2f8c 1866
26c1569f 1867 foreach my $function ("uc", "ucfirst", "lc", "lcfirst", "fc") {
094a2f8c
KW
1868 my @list; # List of code points to test for $function
1869
1870 # Used to calculate the changed case for ASCII characters by using the
1871 # ord, instead of using one of the functions under test.
1872 my $ascii_case_change_delta;
1873 my $above_latin1_case_change_delta; # Same for the specific ords > 255
1874 # that we use
1875
1876 # We test an ASCII character, which should change case and be tainted;
1877 # a Latin1 character, which shouldn't change case under this C locale,
1878 # and is tainted.
1879 # an above-Latin1 character that when the case is changed would cross
1880 # the 255/256 boundary, so doesn't change case and isn't tainted
1881 # (the \x{149} is one of these, but changes into 2 characters, the
1882 # first one of which doesn't cross the boundary.
1883 # the final one in each list is an above-Latin1 character whose case
1884 # does change, and shouldn't be tainted. The code below uses its
1885 # position in its list as a marker to indicate that it, unlike the
1886 # other code points above ASCII, has a successful case change
1887 if ($function =~ /^u/) {
094a2f8c
KW
1888 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
1889 $ascii_case_change_delta = -32;
1890 $above_latin1_case_change_delta = -1;
1891 }
1892 else {
1ca267a5 1893 @list = ("", "A", "\xC0", "\x{17F}", "\x{100}");
094a2f8c
KW
1894 $ascii_case_change_delta = +32;
1895 $above_latin1_case_change_delta = +1;
1896 }
66cbab2c 1897 foreach my $is_utf8_locale (0 .. 1) {
71e5cbb3
KW
1898 foreach my $j (0 .. $#list) {
1899 my $char = $list[$j];
0099bb8d
KW
1900
1901 for my $encoded_in_utf8 (0 .. 1) {
faf0c248
KW
1902 my $should_be;
1903 my $changed;
1904 if (! $is_utf8_locale) {
1905 $should_be = ($j == $#list)
1906 ? chr(ord($char) + $above_latin1_case_change_delta)
1907 : (length $char == 0 || ord($char) > 127)
1908 ? $char
1909 : chr(ord($char) + $ascii_case_change_delta);
1910
1911 # This monstrosity is in order to avoid using an eval,
1912 # which might perturb the results
1913 $changed = ($function eq "uc")
1914 ? uc($char)
1915 : ($function eq "ucfirst")
1916 ? ucfirst($char)
1917 : ($function eq "lc")
1918 ? lc($char)
1919 : ($function eq "lcfirst")
1920 ? lcfirst($char)
26c1569f
KW
1921 : ($function eq "fc")
1922 ? fc($char)
faf0c248
KW
1923 : die("Unexpected function \"$function\"");
1924 }
1925 else {
1926 {
1927 no locale;
71e5cbb3 1928
faf0c248
KW
1929 # For utf8-locales the case changing functions
1930 # should work just like they do outside of locale.
1931 # Can use eval here because not testing it when
1932 # not in locale.
1933 $should_be = eval "$function('$char')";
1934 die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if $@;
71e5cbb3 1935
faf0c248
KW
1936 }
1937 use locale ':not_characters';
1938 $changed = ($function eq "uc")
1939 ? uc($char)
1940 : ($function eq "ucfirst")
1941 ? ucfirst($char)
1942 : ($function eq "lc")
1943 ? lc($char)
1944 : ($function eq "lcfirst")
1945 ? lcfirst($char)
26c1569f
KW
1946 : ($function eq "fc")
1947 ? fc($char)
faf0c248 1948 : die("Unexpected function \"$function\"");
71e5cbb3 1949 }
faf0c248
KW
1950 ok($changed eq $should_be,
1951 "$function(\"$char\") in C locale "
1952 . (($is_utf8_locale)
1953 ? "(use locale ':not_characters'"
1954 : "(use locale")
1955 . (($encoded_in_utf8)
1956 ? "; encoded in utf8)"
1957 : "; not encoded in utf8)")
1958 . " should be \"$should_be\", got \"$changed\"");
1959
1960 # Tainting shouldn't happen for utf8 locales, empty
1961 # strings, or those characters above 255.
1962 (! $is_utf8_locale && length($char) > 0 && ord($char) < 256)
1963 ? check_taint($changed)
1964 : check_taint_not($changed);
1965
1966 # Use UTF-8 next time through the loop
1967 utf8::upgrade($char);
0099bb8d 1968 }
66cbab2c 1969 }
094a2f8c
KW
1970 }
1971 }
1972}
1973
1bfe8fea
KW
1974# Give final advice.
1975
1976my $didwarn = 0;
1977
1978foreach ($first_locales_test_number..$final_locales_test_number) {
1979 if ($Problem{$_}) {
1980 my @f = sort keys %{ $Problem{$_} };
1981 my $f = join(" ", @f);
1982 $f =~ s/(.{50,60}) /$1\n#\t/g;
1983 print
1984 "#\n",
1985 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
1986 "#\t", $f, "\n#\n",
1987 "# on your system may have errors because the locale test $_\n",
1988 "# \"$test_names{$_}\"\n",
1989 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
1990 ".\n";
1991 print <<EOW;
1992#
1993# If your users are not using these locales you are safe for the moment,
1994# but please report this failure first to perlbug\@perl.com using the
1995# perlbug script (as described in the INSTALL file) so that the exact
1996# details of the failures can be sorted out first and then your operating
1997# system supplier can be alerted about these anomalies.
1998#
1999EOW
2000 $didwarn = 1;
2001 }
2002}
2003
2004# Tell which locales were okay and which were not.
2005
2006if ($didwarn) {
2007 my (@s, @F);
2008
2009 foreach my $l (@Locale) {
2010 my $p = 0;
2011 if ($setlocale_failed{$l}) {
2012 $p++;
2013 }
2014 else {
2015 foreach my $t
2016 ($first_locales_test_number..$final_locales_test_number)
2017 {
2018 $p++ if $Problem{$t}{$l};
2019 }
2020 }
2021 push @s, $l if $p == 0;
2022 push @F, $l unless $p == 0;
2023 }
2024
2025 if (@s) {
2026 my $s = join(" ", @s);
2027 $s =~ s/(.{50,60}) /$1\n#\t/g;
2028
2029 warn
2030 "# The following locales\n#\n",
2031 "#\t", $s, "\n#\n",
2032 "# tested okay.\n#\n",
2033 } else {
2034 warn "# None of your locales were fully okay.\n";
2035 }
2036
2037 if (@F) {
2038 my $F = join(" ", @F);
2039 $F =~ s/(.{50,60}) /$1\n#\t/g;
2040
2041 warn
2042 "# The following locales\n#\n",
2043 "#\t", $F, "\n#\n",
2044 "# had problems.\n#\n",
f5627fc1 2045 "# For more details, rerun, with environment variable PERL_DEBUG_FULL_TEST=1.\n";
1bfe8fea
KW
2046 } else {
2047 warn "# None of your locales were broken.\n";
2048 }
2049}
2050
fdf053ee 2051print "1..$test_num\n";
906f284f 2052
90248788 2053# eof