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