This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add :not_characters parameter to 'use locale'
[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
e3a2734b
KW
8binmode STDOUT, ':utf8';
9binmode STDERR, ':utf8';
10
8ebc5c01
PP
11BEGIN {
12 chdir 't' if -d 't';
20822f61 13 @INC = '../lib';
f9cbebe1 14 unshift @INC, '.';
b002077a 15 require Config; import Config;
97a0514d 16 if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
b002077a
CS
17 print "1..0\n";
18 exit;
19 }
2de3dbcc 20 $| = 1;
8ebc5c01
PP
21}
22
23use strict;
24
284102e8
JH
25my $debug = 1;
26
db4b7445
A
27use Dumpvalue;
28
29my $dumper = Dumpvalue->new(
30 tick => qq{"},
31 quoteHighBit => 0,
32 unctrl => "quote"
33 );
6be75cd7 34sub debug {
db4b7445
A
35 return unless $debug;
36 my($mess) = join "", @_;
37 chop $mess;
38 print $dumper->stringify($mess,1), "\n";
6be75cd7
JH
39}
40
41sub debugf {
42 printf @_ if $debug;
43}
44
8ebc5c01
PP
45my $have_setlocale = 0;
46eval {
47 require POSIX;
48 import POSIX ':locale_h';
49 $have_setlocale++;
50};
51
6dead956 52# Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
f6c6487a 53# and mingw32 uses said silly CRT
3a2d1764
SH
54# This doesn't seem to be an issue any more, at least on Windows XP,
55# so re-enable the tests for Windows XP onwards.
56my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion &&
57 join('.', (Win32::GetOSVersion())[1..2]) >= 5.1);
58$have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') &&
59 $Config{cc} =~ /^(cl|gcc)/i);
6dead956 60
36a42ae7 61# UWIN seems to loop after taint tests, just skip for now
cd19b65c
JH
62$have_setlocale = 0 if ($^O =~ /^uwin/);
63
9a66ea41 64sub LC_ALL ();
8ebc5c01 65
0e053d1e 66$a = 'abc %';
8ebc5c01 67
c213d471
KW
68my $test_num = 0;
69
8ebc5c01 70sub ok {
c213d471 71 my ($result, $message) = @_;
e3a2734b 72 $message = "" unless defined $message;
8ebc5c01
PP
73
74 print 'not ' unless ($result);
c213d471 75 print "ok " . ++$test_num;
e3a2734b
KW
76 print " $message";
77 print "\n";
8ebc5c01
PP
78}
79
80# First we'll do a lot of taint checking for locales.
81# This is the easiest to test, actually, as any locale,
82# even the default locale will taint under 'use locale'.
83
84sub is_tainted { # hello, camel two.
9f1b1f2d 85 no warnings 'uninitialized' ;
8ebc5c01 86 my $dummy;
ba74571d 87 local $@;
8ebc5c01
PP
88 not eval { $dummy = join("", @_), kill 0; 1 }
89}
90
36a42ae7
KW
91sub check_taint ($) {
92 ok is_tainted($_[0]), "verify that is tainted";
8ebc5c01
PP
93}
94
36a42ae7
KW
95sub check_taint_not ($) {
96 ok((not is_tainted($_[0])), "verify that isn't tainted");
8ebc5c01
PP
97}
98
99use locale; # engage locale and therefore locale taint.
100
36a42ae7 101check_taint_not $a;
8ebc5c01 102
36a42ae7
KW
103check_taint uc($a);
104check_taint "\U$a";
105check_taint ucfirst($a);
106check_taint "\u$a";
107check_taint lc($a);
108check_taint "\L$a";
109check_taint lcfirst($a);
110check_taint "\l$a";
8ebc5c01 111
36a42ae7
KW
112check_taint_not sprintf('%e', 123.456);
113check_taint_not sprintf('%f', 123.456);
114check_taint_not sprintf('%g', 123.456);
115check_taint_not sprintf('%d', 123.456);
116check_taint_not sprintf('%x', 123.456);
8ebc5c01
PP
117
118$_ = $a; # untaint $_
119
120$_ = uc($a); # taint $_
121
36a42ae7 122check_taint $_;
8ebc5c01
PP
123
124/(\w)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
125check_taint $&;
126check_taint $`;
127check_taint $';
128check_taint $+;
129check_taint $1;
130check_taint_not $2;
8ebc5c01
PP
131
132/(.)/; # untaint $&, $`, $', $+, $1.
36a42ae7
KW
133check_taint_not $&;
134check_taint_not $`;
135check_taint_not $';
136check_taint_not $+;
137check_taint_not $1;
138check_taint_not $2;
8ebc5c01
PP
139
140/(\W)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
141check_taint $&;
142check_taint $`;
143check_taint $';
144check_taint $+;
145check_taint $1;
146check_taint_not $2;
8ebc5c01
PP
147
148/(\s)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
149check_taint $&;
150check_taint $`;
151check_taint $';
152check_taint $+;
153check_taint $1;
154check_taint_not $2;
8ebc5c01
PP
155
156/(\S)/; # taint $&, $`, $', $+, $1.
36a42ae7
KW
157check_taint $&;
158check_taint $`;
159check_taint $';
160check_taint $+;
161check_taint $1;
162check_taint_not $2;
8ebc5c01
PP
163
164$_ = $a; # untaint $_
165
36a42ae7 166check_taint_not $_;
8ebc5c01
PP
167
168/(b)/; # this must not taint
36a42ae7
KW
169check_taint_not $&;
170check_taint_not $`;
171check_taint_not $';
172check_taint_not $+;
173check_taint_not $1;
174check_taint_not $2;
8ebc5c01
PP
175
176$_ = $a; # untaint $_
177
36a42ae7 178check_taint_not $_;
8ebc5c01
PP
179
180$b = uc($a); # taint $b
181s/(.+)/$b/; # this must taint only the $_
182
36a42ae7
KW
183check_taint $_;
184check_taint_not $&;
185check_taint_not $`;
186check_taint_not $';
187check_taint_not $+;
188check_taint_not $1;
189check_taint_not $2;
8ebc5c01
PP
190
191$_ = $a; # untaint $_
192
193s/(.+)/b/; # this must not taint
36a42ae7
KW
194check_taint_not $_;
195check_taint_not $&;
196check_taint_not $`;
197check_taint_not $';
198check_taint_not $+;
199check_taint_not $1;
200check_taint_not $2;
8ebc5c01
PP
201
202$b = $a; # untaint $b
203
204($b = $a) =~ s/\w/$&/;
36a42ae7
KW
205check_taint $b; # $b should be tainted.
206check_taint_not $a; # $a should be not.
8ebc5c01
PP
207
208$_ = $a; # untaint $_
209
210s/(\w)/\l$1/; # this must taint
36a42ae7
KW
211check_taint $_;
212check_taint $&;
213check_taint $`;
214check_taint $';
215check_taint $+;
216check_taint $1;
217check_taint_not $2;
8ebc5c01
PP
218
219$_ = $a; # untaint $_
220
221s/(\w)/\L$1/; # this must taint
36a42ae7
KW
222check_taint $_;
223check_taint $&;
224check_taint $`;
225check_taint $';
226check_taint $+;
227check_taint $1;
228check_taint_not $2;
8ebc5c01
PP
229
230$_ = $a; # untaint $_
231
232s/(\w)/\u$1/; # this must taint
36a42ae7
KW
233check_taint $_;
234check_taint $&;
235check_taint $`;
236check_taint $';
237check_taint $+;
238check_taint $1;
239check_taint_not $2;
8ebc5c01
PP
240
241$_ = $a; # untaint $_
242
243s/(\w)/\U$1/; # this must taint
36a42ae7
KW
244check_taint $_;
245check_taint $&;
246check_taint $`;
247check_taint $';
248check_taint $+;
249check_taint $1;
250check_taint_not $2;
8ebc5c01
PP
251
252# After all this tainting $a should be cool.
253
36a42ae7 254check_taint_not $a;
8ebc5c01 255
66cbab2c
KW
256{ # This is just the previous tests copied here with a different
257 # compile-time pragma.
258
259 use locale ':not_characters'; # engage restricted locale with different
260 # tainting rules
261
262 check_taint_not $a;
263
264 check_taint_not uc($a);
265 check_taint_not "\U$a";
266 check_taint_not ucfirst($a);
267 check_taint_not "\u$a";
268 check_taint_not lc($a);
269 check_taint_not "\L$a";
270 check_taint_not lcfirst($a);
271 check_taint_not "\l$a";
272
273 check_taint_not sprintf('%e', 123.456);
274 check_taint_not sprintf('%f', 123.456);
275 check_taint_not sprintf('%g', 123.456);
276 check_taint_not sprintf('%d', 123.456);
277 check_taint_not sprintf('%x', 123.456);
278
279 $_ = $a; # untaint $_
280
281 $_ = uc($a); # taint $_
282
283 check_taint_not $_;
284
285 /(\w)/; # taint $&, $`, $', $+, $1.
286 check_taint_not $&;
287 check_taint_not $`;
288 check_taint_not $';
289 check_taint_not $+;
290 check_taint_not $1;
291 check_taint_not $2;
292
293 /(.)/; # untaint $&, $`, $', $+, $1.
294 check_taint_not $&;
295 check_taint_not $`;
296 check_taint_not $';
297 check_taint_not $+;
298 check_taint_not $1;
299 check_taint_not $2;
300
301 /(\W)/; # taint $&, $`, $', $+, $1.
302 check_taint_not $&;
303 check_taint_not $`;
304 check_taint_not $';
305 check_taint_not $+;
306 check_taint_not $1;
307 check_taint_not $2;
308
309 /(\s)/; # taint $&, $`, $', $+, $1.
310 check_taint_not $&;
311 check_taint_not $`;
312 check_taint_not $';
313 check_taint_not $+;
314 check_taint_not $1;
315 check_taint_not $2;
316
317 /(\S)/; # taint $&, $`, $', $+, $1.
318 check_taint_not $&;
319 check_taint_not $`;
320 check_taint_not $';
321 check_taint_not $+;
322 check_taint_not $1;
323 check_taint_not $2;
324
325 $_ = $a; # untaint $_
326
327 check_taint_not $_;
328
329 /(b)/; # this must not taint
330 check_taint_not $&;
331 check_taint_not $`;
332 check_taint_not $';
333 check_taint_not $+;
334 check_taint_not $1;
335 check_taint_not $2;
336
337 $_ = $a; # untaint $_
338
339 check_taint_not $_;
340
341 $b = uc($a); # taint $b
342 s/(.+)/$b/; # this must taint only the $_
343
344 check_taint_not $_;
345 check_taint_not $&;
346 check_taint_not $`;
347 check_taint_not $';
348 check_taint_not $+;
349 check_taint_not $1;
350 check_taint_not $2;
351
352 $_ = $a; # untaint $_
353
354 s/(.+)/b/; # this must not taint
355 check_taint_not $_;
356 check_taint_not $&;
357 check_taint_not $`;
358 check_taint_not $';
359 check_taint_not $+;
360 check_taint_not $1;
361 check_taint_not $2;
362
363 $b = $a; # untaint $b
364
365 ($b = $a) =~ s/\w/$&/;
366 check_taint_not $b; # $b should be tainted.
367 check_taint_not $a; # $a should be not.
368
369 $_ = $a; # untaint $_
370
371 s/(\w)/\l$1/; # this must taint
372 check_taint_not $_;
373 check_taint_not $&;
374 check_taint_not $`;
375 check_taint_not $';
376 check_taint_not $+;
377 check_taint_not $1;
378 check_taint_not $2;
379
380 $_ = $a; # untaint $_
381
382 s/(\w)/\L$1/; # this must taint
383 check_taint_not $_;
384 check_taint_not $&;
385 check_taint_not $`;
386 check_taint_not $';
387 check_taint_not $+;
388 check_taint_not $1;
389 check_taint_not $2;
390
391 $_ = $a; # untaint $_
392
393 s/(\w)/\u$1/; # this must taint
394 check_taint_not $_;
395 check_taint_not $&;
396 check_taint_not $`;
397 check_taint_not $';
398 check_taint_not $+;
399 check_taint_not $1;
400 check_taint_not $2;
401
402 $_ = $a; # untaint $_
403
404 s/(\w)/\U$1/; # this must taint
405 check_taint_not $_;
406 check_taint_not $&;
407 check_taint_not $`;
408 check_taint_not $';
409 check_taint_not $+;
410 check_taint_not $1;
411 check_taint_not $2;
412
413 # After all this tainting $a should be cool.
414
415 check_taint_not $a;
416}
417
418# Here are in scope of 'use locale'
419
8ebc5c01
PP
420# I think we've seen quite enough of taint.
421# Let us do some *real* locale work now,
284102e8 422# unless setlocale() is missing (i.e. minitest).
8ebc5c01 423
fdf053ee
KW
424unless ($have_setlocale) {
425 print "1..$test_num\n";
426 exit;
427}
8ebc5c01 428
6cf0b567 429# The test number before our first setlocale()
66330f13 430my $final_without_setlocale = $test_num;
6cf0b567 431
284102e8
JH
432# Find locales.
433
6be75cd7
JH
434debug "# Scanning for locales...\n";
435
436# Note that it's okay that some languages have their native names
437# capitalized here even though that's not "right". They are lowercased
438# anyway later during the scanning process (and besides, some clueless
98dc9551 439# vendor might have them capitalized erroneously anyway).
6be75cd7 440
284102e8 441my $locales = <<EOF;
6be75cd7 442Afrikaans:af:za:1 15
284102e8 443Arabic:ar:dz eg sa:6 arabic8
6be75cd7
JH
444Brezhoneg Breton:br:fr:1 15
445Bulgarski Bulgarian:bg:bg:5
dd8482fc 446Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
6be75cd7
JH
447Hrvatski Croatian:hr:hr:2
448Cymraeg Welsh:cy:cy:1 14 15
284102e8 449Czech:cs:cz:2
6be75cd7
JH
450Dansk Danish:dk:da:1 15
451Nederlands Dutch:nl:be nl:1 15
dd8482fc 452English American British:en:au ca gb ie nz us uk zw:1 15 cp850
6be75cd7
JH
453Esperanto:eo:eo:3
454Eesti Estonian:et:ee:4 6 13
455Suomi Finnish:fi:fi:1 15
456Flamish::fl:1 15
6be75cd7
JH
457Deutsch German:de:at be ch de lu:1 15
458Euskaraz Basque:eu:es fr:1 15
6be75cd7
JH
459Galego Galician:gl:es:1 15
460Ellada Greek:el:gr:7 g8
6be75cd7
JH
461Frysk:fy:nl:1 15
462Greenlandic:kl:gl:4 6
284102e8
JH
463Hebrew:iw:il:8 hebrew8
464Hungarian:hu:hu:2
6be75cd7
JH
465Indonesian:in:id:1 15
466Gaeilge Irish:ga:IE:1 14 15
467Italiano Italian:it:ch it:1 15
468Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
284102e8 469Korean:ko:kr:
6be75cd7
JH
470Latine Latin:la:va:1 15
471Latvian:lv:lv:4 6 13
472Lithuanian:lt:lt:4 6 13
473Macedonian:mk:mk:1 15
474Maltese:mt:mt:3
dd8482fc
JH
475Moldovan:mo:mo:2
476Norsk Norwegian:no no\@nynorsk:no:1 15
6be75cd7
JH
477Occitan:oc:es:1 15
478Polski Polish:pl:pl:2
284102e8 479Rumanian:ro:ro:2
a528dad0 480Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
6be75cd7 481Serbski Serbian:sr:yu:5
284102e8 482Slovak:sk:sk:2
6be75cd7 483Slovene Slovenian:sl:si:2
d43ce814
JH
484Sqhip Albanian:sq:sq:1 15
485Svenska Swedish:sv:fi se:1 15
6be75cd7 486Thai:th:th:11 tis620
284102e8 487Turkish:tr:tr:9 turkish8
dd8482fc 488Yiddish:yi::1 15
284102e8
JH
489EOF
490
ee50adbe 491if ($^O eq 'os390') {
dd8482fc 492 # These cause heartburn. Broken locales?
ee50adbe
PP
493 $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
494 $locales =~ s/Thai:th:th:11 tis620\n//;
495}
496
ef4a39e5 497sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
f9cbebe1
JH
498
499if (in_utf8) {
8a6cb2cb 500 require "lib/locale/utf8";
f9cbebe1 501} else {
8a6cb2cb 502 require "lib/locale/latin1";
f9cbebe1
JH
503}
504
284102e8
JH
505my @Locale;
506my $Locale;
507my @Alnum_;
508
284102e8
JH
509sub trylocale {
510 my $locale = shift;
0b9f254b 511 return if grep { $locale eq $_ } @Locale;
e439cacb
KW
512 return unless setlocale(LC_ALL, $locale);
513 my $badutf8;
514 {
515 local $SIG{__WARN__} = sub {
516 $badutf8 = $_[0] =~ /Malformed UTF-8/;
517 };
518 $Locale =~ /UTF-?8/i;
284102e8 519 }
e439cacb
KW
520
521 if ($badutf8) {
522 ok(0, "Locale name contains malformed utf8");
523 return;
524 }
525 push @Locale, $locale;
284102e8 526}
8ebc5c01 527
284102e8
JH
528sub decode_encodings {
529 my @enc;
8ebc5c01 530
284102e8
JH
531 foreach (split(/ /, shift)) {
532 if (/^(\d+)$/) {
533 push @enc, "ISO8859-$1";
534 push @enc, "iso8859$1"; # HP
535 if ($1 eq '1') {
536 push @enc, "roman8"; # HP
537 }
538 } else {
539 push @enc, $_;
dd8482fc 540 push @enc, "$_.UTF-8";
8ebc5c01
PP
541 }
542 }
ee50adbe
PP
543 if ($^O eq 'os390') {
544 push @enc, qw(IBM-037 IBM-819 IBM-1047);
545 }
8ebc5c01 546
284102e8 547 return @enc;
8ebc5c01
PP
548}
549
284102e8
JH
550trylocale("C");
551trylocale("POSIX");
552foreach (0..15) {
553 trylocale("ISO8859-$_");
284102e8 554 trylocale("iso8859$_");
097ee67d
JH
555 trylocale("iso8859-$_");
556 trylocale("iso_8859_$_");
557 trylocale("isolatin$_");
558 trylocale("isolatin-$_");
559 trylocale("iso_latin_$_");
8ebc5c01
PP
560}
561
645e49ed
JH
562# Sanitize the environment so that we can run the external 'locale'
563# program without the taint mode getting grumpy.
cce5967e
JH
564
565# $ENV{PATH} is special in VMS.
566delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
567
568# Other subversive stuff.
569delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
dd8482fc 570
21477fb4 571if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
dd8482fc 572 while (<LOCALES>) {
d281a6ac
NC
573 # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which
574 # ain't great when we're running this testPERL_UNICODE= so that utf8
575 # locales will cause all IO hadles to default to (assume) utf8
576 next unless utf8::valid($_);
dd8482fc
JH
577 chomp;
578 trylocale($_);
284102e8 579 }
dd8482fc 580 close(LOCALES);
a6259068
PP
581} elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
582# The SYS$I18N_LOCALE logical name search list was not present on
583# VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions.
584 opendir(LOCALES, "SYS\$I18N_LOCALE:");
585 while ($_ = readdir(LOCALES)) {
586 chomp;
587 trylocale($_);
588 }
589 close(LOCALES);
87e33296
SP
590} elsif ($^O eq 'openbsd' && -e '/usr/share/locale') {
591
592 # OpenBSD doesn't have a locale executable, so reading /usr/share/locale
593 # is much easier and faster than the last resort method.
594
595 opendir(LOCALES, '/usr/share/locale');
596 while ($_ = readdir(LOCALES)) {
597 chomp;
598 trylocale($_);
599 }
600 close(LOCALES);
dd8482fc
JH
601} else {
602
603 # This is going to be slow.
604
605 foreach my $locale (split(/\n/, $locales)) {
606 my ($locale_name, $language_codes, $country_codes, $encodings) =
607 split(/:/, $locale);
608 my @enc = decode_encodings($encodings);
609 foreach my $loc (split(/ /, $locale_name)) {
610 trylocale($loc);
284102e8 611 foreach my $enc (@enc) {
dd8482fc 612 trylocale("$loc.$enc");
284102e8 613 }
dd8482fc 614 $loc = lc $loc;
284102e8 615 foreach my $enc (@enc) {
dd8482fc
JH
616 trylocale("$loc.$enc");
617 }
618 }
619 foreach my $lang (split(/ /, $language_codes)) {
620 trylocale($lang);
621 foreach my $country (split(/ /, $country_codes)) {
622 my $lc = "${lang}_${country}";
623 trylocale($lc);
624 foreach my $enc (@enc) {
625 trylocale("$lc.$enc");
626 }
627 my $lC = "${lang}_\U${country}";
628 trylocale($lC);
629 foreach my $enc (@enc) {
630 trylocale("$lC.$enc");
631 }
284102e8
JH
632 }
633 }
634 }
635}
4599a1de 636
d43ce814
JH
637setlocale(LC_ALL, "C");
638
86f50d7d 639if ($^O eq 'darwin') {
4373e181 640 # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
86f50d7d 641 # Apple bug ID# 4139653. It also has a problem in Byelorussian.
4373e181
RGS
642 (my $v) = $Config{osvers} =~ /^(\d+)/;
643 if ($v >= 8 and $v < 10) {
86f50d7d 644 debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
a5ec937f 645 @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
dfa5c78f 646 } elsif ($v < 12) {
a44d0896
NC
647 debug "# Skipping be_BY locales -- buggy in Darwin\n";
648 @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
a5ec937f 649 }
86f50d7d
DD
650}
651
4599a1de
JH
652@Locale = sort @Locale;
653
887ef7ed
PP
654debug "# Locales =\n";
655for ( @Locale ) {
656 debug "# $_\n";
657}
8ebc5c01 658
284102e8 659my %Problem;
2a680da6
JH
660my %Okay;
661my %Testing;
9445c837 662my @Neoalpha; # Alnums that aren't in the C locale.
c08acc4c 663my %test_names;
284102e8 664
2a680da6
JH
665sub tryneoalpha {
666 my ($Locale, $i, $test) = @_;
2a680da6
JH
667 unless ($test) {
668 $Problem{$i}{$Locale} = 1;
669 debug "# failed $i with locale '$Locale'\n";
670 } else {
671 push @{$Okay{$i}}, $Locale;
672 }
673}
674
c4093d7d
KW
675my $first_locales_test_number = $final_without_setlocale + 1;
676my $locales_test_number;
677my $not_necessarily_a_problem_test_number;
678my %setlocale_failed; # List of locales that setlocale() didn't work on
679
284102e8 680foreach $Locale (@Locale) {
c4093d7d 681 $locales_test_number = $first_locales_test_number - 1;
284102e8 682 debug "# Locale = $Locale\n";
284102e8
JH
683
684 unless (setlocale(LC_ALL, $Locale)) {
c4093d7d 685 $setlocale_failed{$Locale} = $Locale;
284102e8 686 next;
8ebc5c01 687 }
8ebc5c01 688
66cbab2c
KW
689 # We test UTF-8 locales only under ':not_characters'; otherwise they have
690 # documented deficiencies. Non- UTF-8 locales are tested only under plain
691 # 'use locale', as otherwise we would have to convert everything in them
692 # to Unicode.
693 my $is_utf8_locale = $Locale =~ /UTF-?8/i;
694
695 my %UPPER = ();
696 my %lower = ();
697 my %BoThCaSe = ();
698
699 if (! $is_utf8_locale) {
700 use locale;
f07538a5
KW
701 @Alnum_ = sort grep /\w/, map { chr } 0..255;
702
e5272a46
KW
703 debug "# w = ", join("",@Alnum_), "\n";
704
284102e8
JH
705 # Sieve the uppercase and the lowercase.
706
284102e8
JH
707 for (@Alnum_) {
708 if (/[^\d_]/) { # skip digits and the _
709 if (uc($_) eq $_) {
710 $UPPER{$_} = $_;
711 }
712 if (lc($_) eq $_) {
713 $lower{$_} = $_;
714 }
715 }
716 }
66cbab2c
KW
717 }
718 else {
719 use locale ':not_characters';
720 @Alnum_ = sort grep /\w/, map { chr } 0..255;
721 debug "# w = ", join("",@Alnum_), "\n";
722 for (@Alnum_) {
723 if (/[^\d_]/) { # skip digits and the _
724 if (uc($_) eq $_) {
725 $UPPER{$_} = $_;
726 }
727 if (lc($_) eq $_) {
728 $lower{$_} = $_;
729 }
730 }
731 }
732 }
284102e8 733 foreach (keys %UPPER) {
097ee67d 734 $BoThCaSe{$_}++ if exists $lower{$_};
284102e8
JH
735 }
736 foreach (keys %lower) {
097ee67d 737 $BoThCaSe{$_}++ if exists $UPPER{$_};
284102e8 738 }
097ee67d 739 foreach (keys %BoThCaSe) {
284102e8
JH
740 delete $UPPER{$_};
741 delete $lower{$_};
742 }
743
db4b7445
A
744 debug "# UPPER = ", join("", sort keys %UPPER ), "\n";
745 debug "# lower = ", join("", sort keys %lower ), "\n";
746 debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
284102e8 747
9445c837
KW
748 { # Find the alphabetic characters that are not considered alphabetics
749 # in the default (C) locale.
8ebc5c01 750
284102e8 751 no locale;
8ebc5c01 752
284102e8
JH
753 @Neoalpha = ();
754 for (keys %UPPER, keys %lower) {
755 push(@Neoalpha, $_) if (/\W/);
756 }
8ebc5c01 757 }
8ebc5c01 758
284102e8 759 @Neoalpha = sort @Neoalpha;
8ebc5c01 760
db4b7445 761 debug "# Neoalpha = ", join("",@Neoalpha), "\n";
8ebc5c01 762
c4093d7d
KW
763 my $first_Neoalpha_test_number = $locales_test_number;
764 my $final_Neoalpha_test_number = $first_Neoalpha_test_number + 4;
284102e8
JH
765 if (@Neoalpha == 0) {
766 # If we have no Neoalphas the remaining tests are no-ops.
c4093d7d
KW
767 debug "# no Neoalpha, skipping tests $locales_test_number..$final_Neoalpha_test_number for locale '$Locale'\n";
768 foreach ($locales_test_number+1..$final_Neoalpha_test_number) {
a88c3d7c 769 push @{$Okay{$_}}, $Locale;
c4093d7d 770 $locales_test_number++;
a88c3d7c 771 }
6be75cd7 772 } else {
8ebc5c01 773
6be75cd7 774 # Test \w.
284102e8 775
ef4a39e5 776 my $word = join('', @Neoalpha);
8ebc5c01 777
c4093d7d 778 ++$locales_test_number;
c08acc4c 779 $test_names{$locales_test_number} = 'Verify that alnums outside the C locale match \w';
66cbab2c
KW
780 my $ok;
781 if ($is_utf8_locale) {
782 use locale ':not_characters';
783 $ok = $word =~ /^(\w+)$/;
784 }
785 else {
786 # Already in 'use locale'; this tests that exiting scopes works
787 $ok = $word =~ /^(\w+)$/;
788 }
789 tryneoalpha($Locale, $locales_test_number, $ok);
ef4a39e5 790
2a680da6 791 # Cross-check the whole 8-bit character set.
8ebc5c01 792
c4093d7d 793 ++$locales_test_number;
c08acc4c 794 $test_names{$locales_test_number} = 'Verify that \w and \W are mutually exclusive, as are \d, \D; \s, \S';
6be75cd7 795 for (map { chr } 0..255) {
66cbab2c
KW
796 if ($is_utf8_locale) {
797 use locale ':not_characters';
798 $ok = (/\w/ xor /\W/) ||
799 (/\d/ xor /\D/) ||
800 (/\s/ xor /\S/);
801 }
802 else {
803 $ok = (/\w/ xor /\W/) ||
2a680da6 804 (/\d/ xor /\D/) ||
66cbab2c
KW
805 (/\s/ xor /\S/);
806 }
807 tryneoalpha($Locale, $locales_test_number, $ok);
284102e8 808 }
8ebc5c01 809
6be75cd7 810 # Test for read-only scalars' locale vs non-locale comparisons.
284102e8 811
284102e8 812 {
6be75cd7
JH
813 no locale;
814 $a = "qwerty";
66cbab2c
KW
815 if ($is_utf8_locale) {
816 use locale ':not_characters';
817 $ok = ($a cmp "qwerty") == 0;
818 }
819 else {
820 use locale;
821 $ok = ($a cmp "qwerty") == 0;
822 }
823 tryneoalpha($Locale, ++$locales_test_number, $ok);
824 $test_names{$locales_test_number} = 'Verify that cmp works with a read-only scalar; no- vs locale';
8ebc5c01 825 }
8ebc5c01 826
6be75cd7
JH
827 {
828 my ($from, $to, $lesser, $greater,
829 @test, %test, $test, $yes, $no, $sign);
830
c4093d7d 831 ++$locales_test_number;
c08acc4c 832 $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work';
c4093d7d 833 $not_necessarily_a_problem_test_number = $locales_test_number;
6be75cd7
JH
834 for (0..9) {
835 # Select a slice.
836 $from = int(($_*@Alnum_)/10);
837 $to = $from + int(@Alnum_/10);
838 $to = $#Alnum_ if ($to > $#Alnum_);
839 $lesser = join('', @Alnum_[$from..$to]);
840 # Select a slice one character on.
841 $from++; $to++;
842 $to = $#Alnum_ if ($to > $#Alnum_);
843 $greater = join('', @Alnum_[$from..$to]);
66cbab2c
KW
844 if ($is_utf8_locale) {
845 use locale ':not_characters';
846 ($yes, $no, $sign) = ($lesser lt $greater
847 ? (" ", "not ", 1)
848 : ("not ", " ", -1));
849 }
850 else {
851 use locale;
6be75cd7
JH
852 ($yes, $no, $sign) = ($lesser lt $greater
853 ? (" ", "not ", 1)
854 : ("not ", " ", -1));
66cbab2c 855 }
6be75cd7
JH
856 # all these tests should FAIL (return 0).
857 # Exact lt or gt cannot be tested because
858 # in some locales, say, eacute and E may test equal.
859 @test =
860 (
861 $no.' ($lesser le $greater)', # 1
862 'not ($lesser ne $greater)', # 2
863 ' ($lesser eq $greater)', # 3
864 $yes.' ($lesser ge $greater)', # 4
865 $yes.' ($lesser ge $greater)', # 5
866 $yes.' ($greater le $lesser )', # 7
867 'not ($greater ne $lesser )', # 8
868 ' ($greater eq $lesser )', # 9
869 $no.' ($greater ge $lesser )', # 10
0e053d1e 870 'not (($lesser cmp $greater) == -($sign))' # 11
6be75cd7
JH
871 );
872 @test{@test} = 0 x @test;
873 $test = 0;
284102e8 874 for my $ti (@test) {
66cbab2c
KW
875 if ($is_utf8_locale) {
876 use locale ':not_characters';
877 $test{$ti} = eval $ti;
878 }
879 else {
880 # Already in 'use locale';
6be75cd7 881 $test{$ti} = eval $ti;
66cbab2c 882 }
6be75cd7 883 $test ||= $test{$ti}
284102e8 884 }
c4093d7d 885 tryneoalpha($Locale, $locales_test_number, $test == 0);
6be75cd7 886 if ($test) {
6be75cd7
JH
887 debug "# lesser = '$lesser'\n";
888 debug "# greater = '$greater'\n";
889 debug "# lesser cmp greater = ",
890 $lesser cmp $greater, "\n";
891 debug "# greater cmp lesser = ",
892 $greater cmp $lesser, "\n";
893 debug "# (greater) from = $from, to = $to\n";
894 for my $ti (@test) {
895 debugf("# %-40s %-4s", $ti,
896 $test{$ti} ? 'FAIL' : 'ok');
897 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
898 debugf("(%s == %4d)", $1, eval $1);
899 }
900 debug "\n#";
901 }
284102e8 902
6be75cd7
JH
903 last;
904 }
284102e8 905 }
8ebc5c01
PP
906 }
907 }
6be75cd7 908
c4093d7d
KW
909 if ($locales_test_number != $final_Neoalpha_test_number) {
910 die("The delta for \$final_Neoalpha needs to be updated from "
911 . ($final_Neoalpha_test_number - $first_Neoalpha_test_number)
912 . " to "
913 . ($locales_test_number - $first_Neoalpha_test_number)
914 );
915 }
916
66cbab2c
KW
917 my $ok1;
918 my $ok2;
919 my $ok3;
920 my $ok4;
921 my $ok5;
922 my $ok6;
923 my $ok7;
924 my $ok8;
925 my $ok9;
926 my $ok10;
927 my $ok11;
928 my $ok12;
929 my $ok13;
930
931 my $c;
932 my $d;
933 my $e;
934 my $f;
935 my $g;
936
937 if (! $is_utf8_locale) {
6be75cd7
JH
938 use locale;
939
940 my ($x, $y) = (1.23, 1.23);
941
0e053d1e 942 $a = "$x";
6be75cd7 943 printf ''; # printf used to reset locale to "C"
0e053d1e 944 $b = "$y";
66cbab2c 945 $ok1 = $a eq $b;
6be75cd7 946
66cbab2c 947 $c = "$x";
6be75cd7 948 my $z = sprintf ''; # sprintf used to reset locale to "C"
66cbab2c
KW
949 $d = "$y";
950 $ok2 = $c eq $d;
2a680da6 951 {
66cbab2c 952
9f1b1f2d 953 use warnings;
2a680da6 954 my $w = 0;
0e053d1e
JH
955 local $SIG{__WARN__} =
956 sub {
906f284f 957 print "# @_\n";
0e053d1e
JH
958 $w++;
959 };
6be75cd7 960
0e053d1e
JH
961 # The == (among other ops) used to warn for locales
962 # that had something else than "." as the radix character.
6be75cd7 963
66cbab2c
KW
964 $ok3 = $c == 1.23;
965 $ok4 = $c == $x;
966 $ok5 = $c == $d;
2a680da6 967 {
8161897a 968 no locale;
66cbab2c 969
8161897a
KW
970 # The earlier test was $e = "$x". But this fails [perl #108378],
971 # and the "no locale" was commented out. But doing that made all
972 # the tests in the block after this one meaningless, as originally
973 # it was testing the nesting of a "no locale" scope, and how it
974 # recovers after that scope is done. So I (khw) filed a bug
975 # report and changed this so it wouldn't fail. It seemed too much
c08acc4c
KW
976 # work to add TODOs instead. Should this be fixed, the following
977 # test names would need to be revised; they mostly don't really
978 # test anything currently.
66cbab2c
KW
979 $e = $x;
980
981 $ok6 = $e == 1.23;
982 $ok7 = $e == $x;
983 $ok8 = $e == $c;
984 }
985
986 $f = "1.23";
987 $g = 2.34;
988
989 $ok9 = $f == 1.23;
990 $ok10 = $f == $x;
991 $ok11 = $f == $c;
992 $ok12 = abs(($f + $g) - 3.57) < 0.01;
993 $ok13 = $w == 0;
994 }
995 }
996 else {
997 use locale ':not_characters';
998
999 my ($x, $y) = (1.23, 1.23);
1000 $a = "$x";
1001 printf ''; # printf used to reset locale to "C"
1002 $b = "$y";
1003 $ok1 = $a eq $b;
1004
1005 $c = "$x";
1006 my $z = sprintf ''; # sprintf used to reset locale to "C"
1007 $d = "$y";
1008 $ok2 = $c eq $d;
1009 {
1010 use warnings;
1011 my $w = 0;
1012 local $SIG{__WARN__} =
1013 sub {
1014 print "# @_\n";
1015 $w++;
1016 };
1017 $ok3 = $c == 1.23;
1018 $ok4 = $c == $x;
1019 $ok5 = $c == $d;
1020 {
1021 no locale;
1022 $e = $x;
1023
1024 $ok6 = $e == 1.23;
1025 $ok7 = $e == $x;
1026 $ok8 = $e == $c;
1027 }
1028
1029 $f = "1.23";
1030 $g = 2.34;
1031
1032 $ok9 = $f == 1.23;
1033 $ok10 = $f == $x;
1034 $ok11 = $f == $c;
1035 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1036 $ok13 = $w == 0;
1037 }
1038 }
1039
1040 tryneoalpha($Locale, ++$locales_test_number, $ok1);
1041 $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results';
1042 my $first_a_test = $locales_test_number;
1043
1044 debug "# $first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n";
1045
1046 tryneoalpha($Locale, ++$locales_test_number, $ok2);
1047 $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results';
1048
1049 my $first_c_test = $locales_test_number;
1050
1051 tryneoalpha($Locale, ++$locales_test_number, $ok3);
1052 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant';
6be75cd7 1053
66cbab2c
KW
1054 tryneoalpha($Locale, ++$locales_test_number, $ok4);
1055 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar';
1056
1057 tryneoalpha($Locale, ++$locales_test_number, $ok5);
1058 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf';
1059
1060 debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n";
1061
1062 tryneoalpha($Locale, ++$locales_test_number, $ok6);
c08acc4c 1063 $test_names{$locales_test_number} = 'Verify that can assign numerically under inner no-locale block';
c4093d7d 1064 my $first_e_test = $locales_test_number;
6be75cd7 1065
66cbab2c 1066 tryneoalpha($Locale, ++$locales_test_number, $ok7);
c08acc4c 1067 $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale';
66cbab2c
KW
1068
1069 tryneoalpha($Locale, ++$locales_test_number, $ok8);
c08acc4c 1070 $test_names{$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale';
c4093d7d 1071
66cbab2c 1072 debug "# $first_e_test..$locales_test_number: \$e = $e, no locale\n";
2a680da6 1073
66cbab2c 1074 tryneoalpha($Locale, ++$locales_test_number, $ok9);
c08acc4c 1075 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant';
c4093d7d 1076 my $first_f_test = $locales_test_number;
6be75cd7 1077
66cbab2c 1078 tryneoalpha($Locale, ++$locales_test_number, $ok10);
c08acc4c 1079 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar';
66cbab2c
KW
1080
1081 tryneoalpha($Locale, ++$locales_test_number, $ok11);
c08acc4c 1082 $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 1083
66cbab2c 1084 tryneoalpha($Locale, ++$locales_test_number, $ok12);
c08acc4c 1085 $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 1086
66cbab2c 1087 tryneoalpha($Locale, ++$locales_test_number, $ok13);
c08acc4c 1088 $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot';
c4093d7d
KW
1089
1090 debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n";
906f284f 1091
26d80d95
LC
1092 # Does taking lc separately differ from taking
1093 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
1094 # The bug was in the caching of the 'o'-magic.
66cbab2c 1095 if (! $is_utf8_locale) {
2a680da6 1096 use locale;
6be75cd7 1097
2a680da6
JH
1098 sub lcA {
1099 my $lc0 = lc $_[0];
1100 my $lc1 = lc $_[1];
1101 return $lc0 cmp $lc1;
1102 }
6be75cd7 1103
2a680da6
JH
1104 sub lcB {
1105 return lc($_[0]) cmp lc($_[1]);
1106 }
6be75cd7 1107
2a680da6
JH
1108 my $x = "ab";
1109 my $y = "aa";
1110 my $z = "AB";
6be75cd7 1111
c4093d7d 1112 tryneoalpha($Locale, ++$locales_test_number,
2a680da6
JH
1113 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
1114 lcA($x, $z) == 0 && lcB($x, $z) == 0);
6be75cd7 1115 }
66cbab2c
KW
1116 else {
1117 use locale ':not_characters';
1118
1119 sub lcC {
1120 my $lc0 = lc $_[0];
1121 my $lc1 = lc $_[1];
1122 return $lc0 cmp $lc1;
1123 }
1124
1125 sub lcD {
1126 return lc($_[0]) cmp lc($_[1]);
1127 }
1128
1129 my $x = "ab";
1130 my $y = "aa";
1131 my $z = "AB";
1132
1133 tryneoalpha($Locale, ++$locales_test_number,
1134 lcC($x, $y) == 1 && lcD($x, $y) == 1 ||
1135 lcC($x, $z) == 0 && lcD($x, $z) == 0);
1136 }
1137 $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp';
d8093b23 1138
26d80d95
LC
1139 # Does lc of an UPPER (if different from the UPPER) match
1140 # case-insensitively the UPPER, and does the UPPER match
1141 # case-insensitively the lc of the UPPER. And vice versa.
3ba0e062 1142 {
ef4a39e5
JH
1143 use locale;
1144 no utf8;
1145 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
1146
1147 my @f = ();
c4093d7d 1148 ++$locales_test_number;
c08acc4c 1149 $test_names{$locales_test_number} = 'Verify case insensitive matching works';
ef4a39e5 1150 foreach my $x (keys %UPPER) {
66cbab2c 1151 if (! $is_utf8_locale) {
ef4a39e5
JH
1152 my $y = lc $x;
1153 next unless uc $y eq $x;
1154 print "# UPPER $x lc $y ",
1155 $x =~ /$y/i ? 1 : 0, " ",
1156 $y =~ /$x/i ? 1 : 0, "\n" if 0;
83d38f53 1157 #
ef4a39e5
JH
1158 # If $x and $y contain regular expression characters
1159 # AND THEY lowercase (/i) to regular expression characters,
1160 # regcomp() will be mightily confused. No, the \Q doesn't
1161 # help here (maybe regex engine internal lowercasing
1162 # is done after the \Q?) An example of this happening is
1163 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
1164 # the chr(173) (the "[") is the lowercase of the chr(235).
83d38f53 1165 #
ef4a39e5
JH
1166 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
1167 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
1168 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
1169 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
1170 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
1171 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
83d38f53
JH
1172 #
1173 # Similar things can happen even under (bastardised)
1174 # non-EBCDIC locales: in many European countries before the
1175 # advent of ISO 8859-x nationally customised versions of
1176 # ISO 646 were devised, reusing certain punctuation
1177 # characters for modified characters needed by the
1178 # country/language. For example, the "|" might have
1179 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
1180 #
ef4a39e5 1181 if ($x =~ $re || $y =~ $re) {
c4093d7d 1182 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
ef4a39e5 1183 next;
8ac0d9e6 1184 }
ef4a39e5
JH
1185 # With utf8 both will fail since the locale concept
1186 # of upper/lower does not work well in Unicode.
1187 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
66cbab2c
KW
1188 }
1189 else {
1190 use locale ':not_characters';
1191 my $y = lc $x;
1192 next unless uc $y eq $x;
1193 print "# UPPER $x lc $y ",
1194 $x =~ /$y/i ? 1 : 0, " ",
1195 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1196
1197 # Here, we can fully test things, unlike plain 'use locale',
1198 # because this form does work well with Unicode
1199 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1200 }
c00ff1c7 1201 }
ef4a39e5 1202
c00ff1c7 1203 foreach my $x (keys %lower) {
66cbab2c 1204 if (! $is_utf8_locale) {
c00ff1c7
DM
1205 my $y = uc $x;
1206 next unless lc $y eq $x;
1207 print "# lower $x uc $y ",
1208 $x =~ /$y/i ? 1 : 0, " ",
1209 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1210 if ($x =~ $re || $y =~ $re) { # See above.
c4093d7d 1211 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
c00ff1c7 1212 next;
8ac0d9e6 1213 }
c00ff1c7
DM
1214 # With utf8 both will fail since the locale concept
1215 # of upper/lower does not work well in Unicode.
1216 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
66cbab2c
KW
1217 }
1218 else {
1219 use locale ':not_characters';
1220 my $y = uc $x;
1221 next unless lc $y eq $x;
1222 print "# lower $x uc $y ",
1223 $x =~ /$y/i ? 1 : 0, " ",
1224 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1225 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1226 }
c00ff1c7 1227 }
c4093d7d 1228 tryneoalpha($Locale, $locales_test_number, @f == 0);
c00ff1c7 1229 if (@f) {
c4093d7d 1230 print "# failed $locales_test_number locale '$Locale' characters @f\n"
c00ff1c7 1231 }
d8093b23 1232 }
8ebc5c01 1233}
284102e8 1234
c4093d7d 1235my $final_locales_test_number = $locales_test_number;
6cf0b567 1236
2a680da6
JH
1237# Recount the errors.
1238
c4093d7d
KW
1239foreach ($first_locales_test_number..$final_locales_test_number) {
1240 if (%setlocale_failed) {
1241 print "not ";
1242 }
1243 elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
1244 if (defined $not_necessarily_a_problem_test_number
1245 && $_ == $not_necessarily_a_problem_test_number)
1246 {
1247 print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n";
b4e009be 1248 print "# It usually indicates a problem in the environment,\n";
284102e8
JH
1249 print "# not in Perl itself.\n";
1250 }
1251 print "not ";
8ebc5c01 1252 }
c4093d7d 1253 print "ok $_";
c08acc4c 1254 print " $test_names{$_}" if defined $test_names{$_};
c4093d7d 1255 print "\n";
8ebc5c01 1256}
fb73857a 1257
2a680da6
JH
1258# Give final advice.
1259
284102e8
JH
1260my $didwarn = 0;
1261
c4093d7d 1262foreach ($first_locales_test_number..$final_locales_test_number) {
284102e8
JH
1263 if ($Problem{$_}) {
1264 my @f = sort keys %{ $Problem{$_} };
1265 my $f = join(" ", @f);
1266 $f =~ s/(.{50,60}) /$1\n#\t/g;
2a680da6
JH
1267 print
1268 "#\n",
1269 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
284102e8
JH
1270 "#\t", $f, "\n#\n",
1271 "# on your system may have errors because the locale test $_\n",
1272 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
1273 ".\n";
2a680da6 1274 print <<EOW;
284102e8
JH
1275#
1276# If your users are not using these locales you are safe for the moment,
1277# but please report this failure first to perlbug\@perl.com using the
1278# perlbug script (as described in the INSTALL file) so that the exact
1279# details of the failures can be sorted out first and then your operating
1280# system supplier can be alerted about these anomalies.
1281#
1282EOW
1283 $didwarn = 1;
fb73857a
PP
1284 }
1285}
774d564b 1286
26d80d95 1287# Tell which locales were okay and which were not.
2a680da6 1288
284102e8 1289if ($didwarn) {
26d80d95 1290 my (@s, @F);
284102e8
JH
1291
1292 foreach my $l (@Locale) {
1293 my $p = 0;
c4093d7d
KW
1294 if ($setlocale_failed{$l}) {
1295 $p++;
1296 }
1297 else {
1f5852c9
KW
1298 foreach my $t
1299 ($first_locales_test_number..$final_locales_test_number)
1300 {
1301 $p++ if $Problem{$t}{$l};
1302 }
c4093d7d 1303 }
284102e8 1304 push @s, $l if $p == 0;
9445c837 1305 push @F, $l unless $p == 0;
8ebc5c01 1306 }
284102e8 1307
68d47915
CK
1308 if (@s) {
1309 my $s = join(" ", @s);
1310 $s =~ s/(.{50,60}) /$1\n#\t/g;
1311
1312 warn
1313 "# The following locales\n#\n",
1314 "#\t", $s, "\n#\n",
1315 "# tested okay.\n#\n",
1316 } else {
26d80d95
LC
1317 warn "# None of your locales were fully okay.\n";
1318 }
1319
1320 if (@F) {
1321 my $F = join(" ", @F);
1322 $F =~ s/(.{50,60}) /$1\n#\t/g;
1323
1324 warn
1325 "# The following locales\n#\n",
0e053d1e 1326 "#\t", $F, "\n#\n",
26d80d95
LC
1327 "# had problems.\n#\n",
1328 } else {
1329 warn "# None of your locales were broken.\n";
68d47915 1330 }
8ebc5c01 1331}
90248788 1332
c4093d7d 1333$test_num = $final_locales_test_number;
c213d471 1334
094a2f8c 1335# Test that tainting and case changing works on utf8 strings. These tests are
1f5852c9
KW
1336# placed last to avoid disturbing the hard-coded test numbers that existed at
1337# the time these were added above this in this file.
094a2f8c
KW
1338setlocale(LC_ALL, "C");
1339{
1340 use locale;
1341
094a2f8c
KW
1342 foreach my $function ("uc", "ucfirst", "lc", "lcfirst") {
1343 my @list; # List of code points to test for $function
1344
1345 # Used to calculate the changed case for ASCII characters by using the
1346 # ord, instead of using one of the functions under test.
1347 my $ascii_case_change_delta;
1348 my $above_latin1_case_change_delta; # Same for the specific ords > 255
1349 # that we use
1350
1351 # We test an ASCII character, which should change case and be tainted;
1352 # a Latin1 character, which shouldn't change case under this C locale,
1353 # and is tainted.
1354 # an above-Latin1 character that when the case is changed would cross
1355 # the 255/256 boundary, so doesn't change case and isn't tainted
1356 # (the \x{149} is one of these, but changes into 2 characters, the
1357 # first one of which doesn't cross the boundary.
1358 # the final one in each list is an above-Latin1 character whose case
1359 # does change, and shouldn't be tainted. The code below uses its
1360 # position in its list as a marker to indicate that it, unlike the
1361 # other code points above ASCII, has a successful case change
1362 if ($function =~ /^u/) {
094a2f8c
KW
1363 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
1364 $ascii_case_change_delta = -32;
1365 $above_latin1_case_change_delta = -1;
1366 }
1367 else {
1368 @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}");
1369 $ascii_case_change_delta = +32;
1370 $above_latin1_case_change_delta = +1;
1371 }
66cbab2c 1372 foreach my $is_utf8_locale (0 .. 1) {
094a2f8c
KW
1373 foreach my $j (0 .. $#list) {
1374 my $char = $list[$j];
094a2f8c 1375 utf8::upgrade($char);
66cbab2c
KW
1376 my $should_be;
1377 my $changed;
1378 if (! $is_utf8_locale) {
1379 $should_be = ($j == $#list)
094a2f8c
KW
1380 ? chr(ord($char) + $above_latin1_case_change_delta)
1381 : (length $char == 0 || ord($char) > 127)
1382 ? $char
1383 : chr(ord($char) + $ascii_case_change_delta);
1384
1385 # This monstrosity is in order to avoid using an eval, which might
1386 # perturb the results
66cbab2c 1387 $changed = ($function eq "uc")
094a2f8c
KW
1388 ? uc($char)
1389 : ($function eq "ucfirst")
1390 ? ucfirst($char)
1391 : ($function eq "lc")
1392 ? lc($char)
1393 : ($function eq "lcfirst")
1394 ? lcfirst($char)
9c11bb3c 1395 : die("Unexpected function \"$function\"");
66cbab2c
KW
1396 }
1397 else {
1398 {
1399 no locale;
1400
1401 # For utf8-locales the case changing functions should work
1402 # just like they do outside of locale. Can use eval here
1403 # because not testing it when not in locale.
1404 $should_be = eval "$function('$char')";
1405 die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if $@;
1406
1407 }
1408 use locale ':not_characters';
1409 $changed = ($function eq "uc")
1410 ? uc($char)
1411 : ($function eq "ucfirst")
1412 ? ucfirst($char)
1413 : ($function eq "lc")
1414 ? lc($char)
1415 : ($function eq "lcfirst")
1416 ? lcfirst($char)
1417 : die("Unexpected function \"$function\"");
1418 }
1419 ok($changed eq $should_be, "$function(\"$char\") in C locale "
1420 . (($is_utf8_locale)
1421 ? "(use locale ':not_characters')"
1422 : "(use locale)")
1423 . " should be \"$should_be\", got \"$changed\"");
094a2f8c
KW
1424
1425 # Tainting shouldn't happen for empty strings, or those characters
1426 # above 255.
66cbab2c 1427 (! $is_utf8_locale && length($char) > 0 && ord($char) < 256)
36a42ae7
KW
1428 ? check_taint($changed)
1429 : check_taint_not($changed);
094a2f8c
KW
1430 }
1431 }
66cbab2c 1432 }
094a2f8c
KW
1433}
1434
fdf053ee 1435print "1..$test_num\n";
906f284f 1436
90248788 1437# eof