This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
locale.t: Add tests for IN_UNI_8_BIT
[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 581} elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
71e5cbb3 582# The SYS$I18N_LOCALE logical name search list was not present on
a6259068
PP
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;
71e5cbb3 701 @Alnum_ = sort grep /\w/, map { chr } 0..255;
f07538a5 702
71e5cbb3 703 debug "# w = ", join("",@Alnum_), "\n";
e5272a46 704
71e5cbb3
KW
705 # Sieve the uppercase and the lowercase.
706
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;
71e5cbb3 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.
71e5cbb3 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;
71e5cbb3 852 ($yes, $no, $sign) = ($lesser lt $greater
6be75cd7
JH
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.
71e5cbb3 859 @test =
6be75cd7
JH
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';
71e5cbb3 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) {
71e5cbb3 938 use locale;
6be75cd7 939
71e5cbb3 940 my ($x, $y) = (1.23, 1.23);
6be75cd7 941
71e5cbb3
KW
942 $a = "$x";
943 printf ''; # printf used to reset locale to "C"
944 $b = "$y";
945 $ok1 = $a eq $b;
6be75cd7 946
71e5cbb3
KW
947 $c = "$x";
948 my $z = sprintf ''; # sprintf used to reset locale to "C"
949 $d = "$y";
950 $ok2 = $c eq $d;
951 {
66cbab2c 952
71e5cbb3
KW
953 use warnings;
954 my $w = 0;
955 local $SIG{__WARN__} =
956 sub {
957 print "# @_\n";
958 $w++;
959 };
6be75cd7 960
71e5cbb3
KW
961 # The == (among other ops) used to warn for locales
962 # that had something else than "." as the radix character.
6be75cd7 963
71e5cbb3
KW
964 $ok3 = $c == 1.23;
965 $ok4 = $c == $x;
966 $ok5 = $c == $d;
967 {
968 no locale;
66cbab2c 969
71e5cbb3
KW
970 # The earlier test was $e = "$x". But this fails [perl
971 # #108378], and the "no locale" was commented out. But doing
972 # that made all the tests in the block after this one
973 # meaningless, as originally it was testing the nesting of a
974 # "no locale" scope, and how it recovers after that scope is
975 # done. So I (khw) filed a bug report and changed this so it
976 # wouldn't fail. It seemed too much work to add TODOs
977 # instead. Should this be fixed, the following test names
978 # would need to be revised; they mostly don't really test
979 # anything currently.
980 $e = $x;
981
982 $ok6 = $e == 1.23;
983 $ok7 = $e == $x;
984 $ok8 = $e == $c;
985 }
66cbab2c 986
71e5cbb3
KW
987 $f = "1.23";
988 $g = 2.34;
66cbab2c 989
71e5cbb3
KW
990 $ok9 = $f == 1.23;
991 $ok10 = $f == $x;
992 $ok11 = $f == $c;
993 $ok12 = abs(($f + $g) - 3.57) < 0.01;
994 $ok13 = $w == 0;
995 }
66cbab2c
KW
996 }
997 else {
998 use locale ':not_characters';
999
1000 my ($x, $y) = (1.23, 1.23);
1001 $a = "$x";
1002 printf ''; # printf used to reset locale to "C"
1003 $b = "$y";
1004 $ok1 = $a eq $b;
1005
1006 $c = "$x";
1007 my $z = sprintf ''; # sprintf used to reset locale to "C"
1008 $d = "$y";
1009 $ok2 = $c eq $d;
1010 {
1011 use warnings;
1012 my $w = 0;
1013 local $SIG{__WARN__} =
1014 sub {
1015 print "# @_\n";
1016 $w++;
1017 };
1018 $ok3 = $c == 1.23;
1019 $ok4 = $c == $x;
1020 $ok5 = $c == $d;
1021 {
1022 no locale;
1023 $e = $x;
1024
1025 $ok6 = $e == 1.23;
1026 $ok7 = $e == $x;
1027 $ok8 = $e == $c;
1028 }
1029
1030 $f = "1.23";
1031 $g = 2.34;
1032
1033 $ok9 = $f == 1.23;
1034 $ok10 = $f == $x;
1035 $ok11 = $f == $c;
1036 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1037 $ok13 = $w == 0;
1038 }
1039 }
1040
1041 tryneoalpha($Locale, ++$locales_test_number, $ok1);
1042 $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results';
1043 my $first_a_test = $locales_test_number;
1044
1045 debug "# $first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n";
1046
1047 tryneoalpha($Locale, ++$locales_test_number, $ok2);
1048 $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results';
1049
1050 my $first_c_test = $locales_test_number;
1051
71e5cbb3
KW
1052 tryneoalpha($Locale, ++$locales_test_number, $ok3);
1053 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant';
6be75cd7 1054
71e5cbb3
KW
1055 tryneoalpha($Locale, ++$locales_test_number, $ok4);
1056 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar';
66cbab2c 1057
71e5cbb3
KW
1058 tryneoalpha($Locale, ++$locales_test_number, $ok5);
1059 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf';
66cbab2c 1060
71e5cbb3 1061 debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n";
66cbab2c 1062
71e5cbb3
KW
1063 tryneoalpha($Locale, ++$locales_test_number, $ok6);
1064 $test_names{$locales_test_number} = 'Verify that can assign numerically under inner no-locale block';
1065 my $first_e_test = $locales_test_number;
6be75cd7 1066
71e5cbb3
KW
1067 tryneoalpha($Locale, ++$locales_test_number, $ok7);
1068 $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale';
66cbab2c 1069
71e5cbb3
KW
1070 tryneoalpha($Locale, ++$locales_test_number, $ok8);
1071 $test_names{$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale';
c4093d7d 1072
71e5cbb3 1073 debug "# $first_e_test..$locales_test_number: \$e = $e, no locale\n";
2a680da6 1074
71e5cbb3
KW
1075 tryneoalpha($Locale, ++$locales_test_number, $ok9);
1076 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant';
1077 my $first_f_test = $locales_test_number;
6be75cd7 1078
71e5cbb3
KW
1079 tryneoalpha($Locale, ++$locales_test_number, $ok10);
1080 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar';
66cbab2c 1081
71e5cbb3
KW
1082 tryneoalpha($Locale, ++$locales_test_number, $ok11);
1083 $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 1084
71e5cbb3
KW
1085 tryneoalpha($Locale, ++$locales_test_number, $ok12);
1086 $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 1087
71e5cbb3
KW
1088 tryneoalpha($Locale, ++$locales_test_number, $ok13);
1089 $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot';
c4093d7d 1090
71e5cbb3 1091 debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n";
906f284f 1092
26d80d95
LC
1093 # Does taking lc separately differ from taking
1094 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
1095 # The bug was in the caching of the 'o'-magic.
66cbab2c 1096 if (! $is_utf8_locale) {
2a680da6 1097 use locale;
6be75cd7 1098
2a680da6
JH
1099 sub lcA {
1100 my $lc0 = lc $_[0];
1101 my $lc1 = lc $_[1];
1102 return $lc0 cmp $lc1;
1103 }
6be75cd7 1104
2a680da6
JH
1105 sub lcB {
1106 return lc($_[0]) cmp lc($_[1]);
1107 }
6be75cd7 1108
2a680da6
JH
1109 my $x = "ab";
1110 my $y = "aa";
1111 my $z = "AB";
6be75cd7 1112
c4093d7d 1113 tryneoalpha($Locale, ++$locales_test_number,
2a680da6
JH
1114 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
1115 lcA($x, $z) == 0 && lcB($x, $z) == 0);
6be75cd7 1116 }
66cbab2c
KW
1117 else {
1118 use locale ':not_characters';
1119
1120 sub lcC {
1121 my $lc0 = lc $_[0];
1122 my $lc1 = lc $_[1];
1123 return $lc0 cmp $lc1;
1124 }
1125
1126 sub lcD {
1127 return lc($_[0]) cmp lc($_[1]);
1128 }
1129
1130 my $x = "ab";
1131 my $y = "aa";
1132 my $z = "AB";
1133
1134 tryneoalpha($Locale, ++$locales_test_number,
1135 lcC($x, $y) == 1 && lcD($x, $y) == 1 ||
1136 lcC($x, $z) == 0 && lcD($x, $z) == 0);
1137 }
1138 $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp';
d8093b23 1139
26d80d95
LC
1140 # Does lc of an UPPER (if different from the UPPER) match
1141 # case-insensitively the UPPER, and does the UPPER match
1142 # case-insensitively the lc of the UPPER. And vice versa.
3ba0e062 1143 {
ef4a39e5
JH
1144 use locale;
1145 no utf8;
1146 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
1147
1148 my @f = ();
c4093d7d 1149 ++$locales_test_number;
c08acc4c 1150 $test_names{$locales_test_number} = 'Verify case insensitive matching works';
ef4a39e5 1151 foreach my $x (keys %UPPER) {
66cbab2c 1152 if (! $is_utf8_locale) {
71e5cbb3
KW
1153 my $y = lc $x;
1154 next unless uc $y eq $x;
1155 print "# UPPER $x lc $y ",
1156 $x =~ /$y/i ? 1 : 0, " ",
1157 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1158 #
1159 # If $x and $y contain regular expression characters
1160 # AND THEY lowercase (/i) to regular expression characters,
1161 # regcomp() will be mightily confused. No, the \Q doesn't
1162 # help here (maybe regex engine internal lowercasing
1163 # is done after the \Q?) An example of this happening is
1164 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
1165 # the chr(173) (the "[") is the lowercase of the chr(235).
1166 #
1167 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
1168 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
1169 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
1170 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
1171 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
1172 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
1173 #
1174 # Similar things can happen even under (bastardised)
1175 # non-EBCDIC locales: in many European countries before the
1176 # advent of ISO 8859-x nationally customised versions of
1177 # ISO 646 were devised, reusing certain punctuation
1178 # characters for modified characters needed by the
1179 # country/language. For example, the "|" might have
1180 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
1181 #
1182 if ($x =~ $re || $y =~ $re) {
1183 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1184 next;
1185 }
1186 # With utf8 both will fail since the locale concept
1187 # of upper/lower does not work well in Unicode.
1188 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
66cbab2c
KW
1189 }
1190 else {
1191 use locale ':not_characters';
1192 my $y = lc $x;
1193 next unless uc $y eq $x;
1194 print "# UPPER $x lc $y ",
1195 $x =~ /$y/i ? 1 : 0, " ",
1196 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1197
1198 # Here, we can fully test things, unlike plain 'use locale',
1199 # because this form does work well with Unicode
1200 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1201 }
c00ff1c7 1202 }
ef4a39e5 1203
c00ff1c7 1204 foreach my $x (keys %lower) {
66cbab2c 1205 if (! $is_utf8_locale) {
71e5cbb3
KW
1206 my $y = uc $x;
1207 next unless lc $y eq $x;
1208 print "# lower $x uc $y ",
1209 $x =~ /$y/i ? 1 : 0, " ",
1210 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1211 if ($x =~ $re || $y =~ $re) { # See above.
1212 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
1213 next;
1214 }
1215 # With utf8 both will fail since the locale concept
1216 # of upper/lower does not work well in Unicode.
1217 push @f, $x unless $x =~ /$y/i == $y =~ /$x/i;
66cbab2c
KW
1218 }
1219 else {
1220 use locale ':not_characters';
1221 my $y = uc $x;
1222 next unless lc $y eq $x;
1223 print "# lower $x uc $y ",
1224 $x =~ /$y/i ? 1 : 0, " ",
1225 $y =~ /$x/i ? 1 : 0, "\n" if 0;
1226 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1227 }
c00ff1c7 1228 }
c4093d7d 1229 tryneoalpha($Locale, $locales_test_number, @f == 0);
c00ff1c7 1230 if (@f) {
c4093d7d 1231 print "# failed $locales_test_number locale '$Locale' characters @f\n"
c00ff1c7 1232 }
d8093b23 1233 }
8ebc5c01 1234}
284102e8 1235
c4093d7d 1236my $final_locales_test_number = $locales_test_number;
6cf0b567 1237
2a680da6
JH
1238# Recount the errors.
1239
c4093d7d
KW
1240foreach ($first_locales_test_number..$final_locales_test_number) {
1241 if (%setlocale_failed) {
1242 print "not ";
1243 }
1244 elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
1245 if (defined $not_necessarily_a_problem_test_number
1246 && $_ == $not_necessarily_a_problem_test_number)
1247 {
1248 print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n";
b4e009be 1249 print "# It usually indicates a problem in the environment,\n";
284102e8
JH
1250 print "# not in Perl itself.\n";
1251 }
1252 print "not ";
8ebc5c01 1253 }
c4093d7d 1254 print "ok $_";
c08acc4c 1255 print " $test_names{$_}" if defined $test_names{$_};
c4093d7d 1256 print "\n";
8ebc5c01 1257}
fb73857a 1258
2a680da6
JH
1259# Give final advice.
1260
284102e8
JH
1261my $didwarn = 0;
1262
c4093d7d 1263foreach ($first_locales_test_number..$final_locales_test_number) {
284102e8
JH
1264 if ($Problem{$_}) {
1265 my @f = sort keys %{ $Problem{$_} };
1266 my $f = join(" ", @f);
1267 $f =~ s/(.{50,60}) /$1\n#\t/g;
2a680da6
JH
1268 print
1269 "#\n",
1270 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
284102e8
JH
1271 "#\t", $f, "\n#\n",
1272 "# on your system may have errors because the locale test $_\n",
1273 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
1274 ".\n";
2a680da6 1275 print <<EOW;
284102e8
JH
1276#
1277# If your users are not using these locales you are safe for the moment,
1278# but please report this failure first to perlbug\@perl.com using the
1279# perlbug script (as described in the INSTALL file) so that the exact
1280# details of the failures can be sorted out first and then your operating
1281# system supplier can be alerted about these anomalies.
1282#
1283EOW
1284 $didwarn = 1;
fb73857a
PP
1285 }
1286}
774d564b 1287
26d80d95 1288# Tell which locales were okay and which were not.
2a680da6 1289
284102e8 1290if ($didwarn) {
26d80d95 1291 my (@s, @F);
71e5cbb3 1292
284102e8
JH
1293 foreach my $l (@Locale) {
1294 my $p = 0;
c4093d7d
KW
1295 if ($setlocale_failed{$l}) {
1296 $p++;
1297 }
1298 else {
1f5852c9
KW
1299 foreach my $t
1300 ($first_locales_test_number..$final_locales_test_number)
1301 {
1302 $p++ if $Problem{$t}{$l};
1303 }
c4093d7d 1304 }
284102e8 1305 push @s, $l if $p == 0;
9445c837 1306 push @F, $l unless $p == 0;
8ebc5c01 1307 }
71e5cbb3 1308
68d47915
CK
1309 if (@s) {
1310 my $s = join(" ", @s);
1311 $s =~ s/(.{50,60}) /$1\n#\t/g;
1312
1313 warn
1314 "# The following locales\n#\n",
1315 "#\t", $s, "\n#\n",
1316 "# tested okay.\n#\n",
1317 } else {
26d80d95
LC
1318 warn "# None of your locales were fully okay.\n";
1319 }
1320
1321 if (@F) {
1322 my $F = join(" ", @F);
1323 $F =~ s/(.{50,60}) /$1\n#\t/g;
1324
1325 warn
1326 "# The following locales\n#\n",
0e053d1e 1327 "#\t", $F, "\n#\n",
26d80d95
LC
1328 "# had problems.\n#\n",
1329 } else {
1330 warn "# None of your locales were broken.\n";
68d47915 1331 }
8ebc5c01 1332}
90248788 1333
c4093d7d 1334$test_num = $final_locales_test_number;
c213d471 1335
094a2f8c 1336# Test that tainting and case changing works on utf8 strings. These tests are
1f5852c9
KW
1337# placed last to avoid disturbing the hard-coded test numbers that existed at
1338# the time these were added above this in this file.
0099bb8d
KW
1339# This also tests that locale overrides unicode_strings in the same scope for
1340# non-utf8 strings.
094a2f8c
KW
1341setlocale(LC_ALL, "C");
1342{
1343 use locale;
0099bb8d 1344 use feature 'unicode_strings';
094a2f8c 1345
094a2f8c
KW
1346 foreach my $function ("uc", "ucfirst", "lc", "lcfirst") {
1347 my @list; # List of code points to test for $function
1348
1349 # Used to calculate the changed case for ASCII characters by using the
1350 # ord, instead of using one of the functions under test.
1351 my $ascii_case_change_delta;
1352 my $above_latin1_case_change_delta; # Same for the specific ords > 255
1353 # that we use
1354
1355 # We test an ASCII character, which should change case and be tainted;
1356 # a Latin1 character, which shouldn't change case under this C locale,
1357 # and is tainted.
1358 # an above-Latin1 character that when the case is changed would cross
1359 # the 255/256 boundary, so doesn't change case and isn't tainted
1360 # (the \x{149} is one of these, but changes into 2 characters, the
1361 # first one of which doesn't cross the boundary.
1362 # the final one in each list is an above-Latin1 character whose case
1363 # does change, and shouldn't be tainted. The code below uses its
1364 # position in its list as a marker to indicate that it, unlike the
1365 # other code points above ASCII, has a successful case change
1366 if ($function =~ /^u/) {
094a2f8c
KW
1367 @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
1368 $ascii_case_change_delta = -32;
1369 $above_latin1_case_change_delta = -1;
1370 }
1371 else {
1372 @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}");
1373 $ascii_case_change_delta = +32;
1374 $above_latin1_case_change_delta = +1;
1375 }
66cbab2c 1376 foreach my $is_utf8_locale (0 .. 1) {
71e5cbb3
KW
1377 foreach my $j (0 .. $#list) {
1378 my $char = $list[$j];
0099bb8d
KW
1379
1380 for my $encoded_in_utf8 (0 .. 1) {
71e5cbb3
KW
1381 my $should_be;
1382 my $changed;
1383 if (! $is_utf8_locale) {
1384 $should_be = ($j == $#list)
1385 ? chr(ord($char) + $above_latin1_case_change_delta)
1386 : (length $char == 0 || ord($char) > 127)
1387 ? $char
1388 : chr(ord($char) + $ascii_case_change_delta);
1389
ed222852
KW
1390 # This monstrosity is in order to avoid using an eval,
1391 # which might perturb the results
1392 $changed = ($function eq "uc")
1393 ? uc($char)
1394 : ($function eq "ucfirst")
1395 ? ucfirst($char)
1396 : ($function eq "lc")
1397 ? lc($char)
1398 : ($function eq "lcfirst")
1399 ? lcfirst($char)
1400 : die("Unexpected function \"$function\"");
71e5cbb3
KW
1401 }
1402 else {
1403 {
1404 no locale;
1405
1406 # For utf8-locales the case changing functions should
1407 # work just like they do outside of locale. Can use
1408 # eval here because not testing it when not in locale.
1409 $should_be = eval "$function('$char')";
1410 die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if $@;
1411
1412 }
1413 use locale ':not_characters';
1414 $changed = ($function eq "uc")
1415 ? uc($char)
1416 : ($function eq "ucfirst")
1417 ? ucfirst($char)
1418 : ($function eq "lc")
1419 ? lc($char)
1420 : ($function eq "lcfirst")
1421 ? lcfirst($char)
1422 : die("Unexpected function \"$function\"");
1423 }
1424 ok($changed eq $should_be, "$function(\"$char\") in C locale "
1425 . (($is_utf8_locale)
0099bb8d
KW
1426 ? "(use locale ':not_characters'"
1427 : "(use locale")
1428 . (($encoded_in_utf8) ? "; encoded in utf8)" : "; not encoded in utf8)")
71e5cbb3
KW
1429 . " should be \"$should_be\", got \"$changed\"");
1430
1431 # Tainting shouldn't happen for utf8 locales, empty strings,
1432 # or those characters above 255.
1433 (! $is_utf8_locale && length($char) > 0 && ord($char) < 256)
1434 ? check_taint($changed)
1435 : check_taint_not($changed);
0099bb8d
KW
1436
1437 # Use UTF-8 next time through the loop
1438 utf8::upgrade($char);
1439 }
66cbab2c 1440 }
094a2f8c
KW
1441 }
1442 }
1443}
1444
fdf053ee 1445print "1..$test_num\n";
906f284f 1446
90248788 1447# eof