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