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