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