This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Make comment more accurate
[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 binmode STDOUT, ':utf8';
9 binmode STDERR, ':utf8';
10
11 BEGIN {
12     chdir 't' if -d 't';
13     @INC = '../lib';
14     unshift @INC, '.';
15     require Config; import Config;
16     if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) {
17         print "1..0\n";
18         exit;
19     }
20     $| = 1;
21 }
22
23 use strict;
24 use feature 'fc';
25
26 my $debug = 0;
27
28 use Dumpvalue;
29
30 my $dumper = Dumpvalue->new(
31                             tick => qq{"},
32                             quoteHighBit => 0,
33                             unctrl => "quote"
34                            );
35 sub debug {
36   return unless $debug;
37   my($mess) = join "", @_;
38   chop $mess;
39   print $dumper->stringify($mess,1), "\n";
40 }
41
42 sub debugf {
43     printf @_ if $debug;
44 }
45
46 my $have_setlocale = 0;
47 eval {
48     require POSIX;
49     import POSIX ':locale_h';
50     $have_setlocale++;
51 };
52
53 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
54 # and mingw32 uses said silly CRT
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.
57 my $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);
61
62 # UWIN seems to loop after taint tests, just skip for now
63 $have_setlocale = 0 if ($^O =~ /^uwin/);
64
65 sub LC_ALL ();
66
67 $a = 'abc %';
68
69 my $test_num = 0;
70
71 sub ok {
72     my ($result, $message) = @_;
73     $message = "" unless defined $message;
74
75     print 'not ' unless ($result);
76     print "ok " . ++$test_num;
77     print " $message";
78     print "\n";
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
85 sub is_tainted { # hello, camel two.
86     no warnings 'uninitialized' ;
87     my $dummy;
88     local $@;
89     not eval { $dummy = join("", @_), kill 0; 1 }
90 }
91
92 sub check_taint ($) {
93     ok is_tainted($_[0]), "verify that is tainted";
94 }
95
96 sub check_taint_not ($) {
97     ok((not is_tainted($_[0])), "verify that isn't tainted");
98 }
99
100 use locale;     # engage locale and therefore locale taint.
101
102 check_taint_not   $a;
103
104 check_taint       uc($a);
105 check_taint       "\U$a";
106 check_taint       ucfirst($a);
107 check_taint       "\u$a";
108 check_taint       lc($a);
109 check_taint       fc($a);
110 check_taint       "\L$a";
111 check_taint       "\F$a";
112 check_taint       lcfirst($a);
113 check_taint       "\l$a";
114
115 check_taint_not  sprintf('%e', 123.456);
116 check_taint_not  sprintf('%f', 123.456);
117 check_taint_not  sprintf('%g', 123.456);
118 check_taint_not  sprintf('%d', 123.456);
119 check_taint_not  sprintf('%x', 123.456);
120
121 $_ = $a;        # untaint $_
122
123 $_ = uc($a);    # taint $_
124
125 check_taint      $_;
126
127 /(\w)/; # taint $&, $`, $', $+, $1.
128 check_taint      $&;
129 check_taint      $`;
130 check_taint      $';
131 check_taint      $+;
132 check_taint      $1;
133 check_taint_not  $2;
134
135 /(.)/;  # untaint $&, $`, $', $+, $1.
136 check_taint_not  $&;
137 check_taint_not  $`;
138 check_taint_not  $';
139 check_taint_not  $+;
140 check_taint_not  $1;
141 check_taint_not  $2;
142
143 /(\W)/; # taint $&, $`, $', $+, $1.
144 check_taint      $&;
145 check_taint      $`;
146 check_taint      $';
147 check_taint      $+;
148 check_taint      $1;
149 check_taint_not  $2;
150
151 /(\s)/; # taint $&, $`, $', $+, $1.
152 check_taint      $&;
153 check_taint      $`;
154 check_taint      $';
155 check_taint      $+;
156 check_taint      $1;
157 check_taint_not  $2;
158
159 /(\S)/; # taint $&, $`, $', $+, $1.
160 check_taint      $&;
161 check_taint      $`;
162 check_taint      $';
163 check_taint      $+;
164 check_taint      $1;
165 check_taint_not  $2;
166
167 $_ = $a;        # untaint $_
168
169 check_taint_not  $_;
170
171 /(b)/;          # this must not taint
172 check_taint_not  $&;
173 check_taint_not  $`;
174 check_taint_not  $';
175 check_taint_not  $+;
176 check_taint_not  $1;
177 check_taint_not  $2;
178
179 $_ = $a;        # untaint $_
180
181 check_taint_not  $_;
182
183 $b = uc($a);    # taint $b
184 s/(.+)/$b/;     # this must taint only the $_
185
186 check_taint      $_;
187 check_taint_not  $&;
188 check_taint_not  $`;
189 check_taint_not  $';
190 check_taint_not  $+;
191 check_taint_not  $1;
192 check_taint_not  $2;
193
194 $_ = $a;        # untaint $_
195
196 s/(.+)/b/;      # this must not taint
197 check_taint_not  $_;
198 check_taint_not  $&;
199 check_taint_not  $`;
200 check_taint_not  $';
201 check_taint_not  $+;
202 check_taint_not  $1;
203 check_taint_not  $2;
204
205 $b = $a;        # untaint $b
206
207 ($b = $a) =~ s/\w/$&/;
208 check_taint      $b;    # $b should be tainted.
209 check_taint_not  $a;    # $a should be not.
210
211 $_ = $a;        # untaint $_
212
213 s/(\w)/\l$1/;   # this must taint
214 check_taint      $_;
215 check_taint      $&;
216 check_taint      $`;
217 check_taint      $';
218 check_taint      $+;
219 check_taint      $1;
220 check_taint_not  $2;
221
222 $_ = $a;        # untaint $_
223
224 s/(\w)/\L$1/;   # this must taint
225 check_taint      $_;
226 check_taint      $&;
227 check_taint      $`;
228 check_taint      $';
229 check_taint      $+;
230 check_taint      $1;
231 check_taint_not  $2;
232
233 $_ = $a;        # untaint $_
234
235 s/(\w)/\u$1/;   # this must taint
236 check_taint      $_;
237 check_taint      $&;
238 check_taint      $`;
239 check_taint      $';
240 check_taint      $+;
241 check_taint      $1;
242 check_taint_not  $2;
243
244 $_ = $a;        # untaint $_
245
246 s/(\w)/\U$1/;   # this must taint
247 check_taint      $_;
248 check_taint      $&;
249 check_taint      $`;
250 check_taint      $';
251 check_taint      $+;
252 check_taint      $1;
253 check_taint_not  $2;
254
255 # After all this tainting $a should be cool.
256
257 check_taint_not  $a;
258
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);
272     check_taint_not     fc($a);
273     check_taint_not     "\L$a";
274     check_taint_not     "\F$a";
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
425 # I think we've seen quite enough of taint.
426 # Let us do some *real* locale work now,
427 # unless setlocale() is missing (i.e. minitest).
428
429 unless ($have_setlocale) {
430     print "1..$test_num\n";
431     exit;
432 }
433
434 # The test number before our first setlocale()
435 my $final_without_setlocale = $test_num;
436
437 # Find locales.
438
439 debug "# 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
444 # vendor might have them capitalized erroneously anyway).
445
446 my $locales = <<EOF;
447 Afrikaans:af:za:1 15
448 Arabic:ar:dz eg sa:6 arabic8
449 Brezhoneg Breton:br:fr:1 15
450 Bulgarski Bulgarian:bg:bg:5
451 Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC
452 Hrvatski Croatian:hr:hr:2
453 Cymraeg Welsh:cy:cy:1 14 15
454 Czech:cs:cz:2
455 Dansk Danish:da:dk:1 15
456 Nederlands Dutch:nl:be nl:1 15
457 English American British:en:au ca gb ie nz us uk zw:1 15 cp850
458 Esperanto:eo:eo:3
459 Eesti Estonian:et:ee:4 6 13
460 Suomi Finnish:fi:fi:1 15
461 Flamish::fl:1 15
462 Deutsch German:de:at be ch de lu:1 15
463 Euskaraz Basque:eu:es fr:1 15
464 Galego Galician:gl:es:1 15
465 Ellada Greek:el:gr:7 g8
466 Frysk:fy:nl:1 15
467 Greenlandic:kl:gl:4 6
468 Hebrew:iw:il:8 hebrew8
469 Hungarian:hu:hu:2
470 Indonesian:id:id:1 15
471 Gaeilge Irish:ga:IE:1 14 15
472 Italiano Italian:it:ch it:1 15
473 Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis
474 Korean:ko:kr:
475 Latine Latin:la:va:1 15
476 Latvian:lv:lv:4 6 13
477 Lithuanian:lt:lt:4 6 13
478 Macedonian:mk:mk:1 15
479 Maltese:mt:mt:3
480 Moldovan:mo:mo:2
481 Norsk Norwegian:no no\@nynorsk nb nn:no:1 15
482 Occitan:oc:es:1 15
483 Polski Polish:pl:pl:2
484 Rumanian:ro:ro:2
485 Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866
486 Serbski Serbian:sr:yu:5
487 Slovak:sk:sk:2
488 Slovene Slovenian:sl:si:2
489 Sqhip Albanian:sq:sq:1 15
490 Svenska Swedish:sv:fi se:1 15
491 Thai:th:th:11 tis620
492 Turkish:tr:tr:9 turkish8
493 Yiddish:yi::1 15
494 EOF
495
496 if ($^O eq 'os390') {
497     # These cause heartburn.  Broken locales?
498     $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//;
499     $locales =~ s/Thai:th:th:11 tis620\n//;
500 }
501
502 sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ }
503
504 if (in_utf8) {
505     require "lib/locale/utf8";
506 } else {
507     require "lib/locale/latin1";
508 }
509
510 my @Locale;
511 my $Locale;
512 my @Alnum_;
513
514 sub trylocale {
515     my $locale = shift;
516     return if grep { $locale eq $_ } @Locale;
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;
524     }
525
526     if ($badutf8) {
527         ok(0, "Locale name contains malformed utf8");
528         return;
529     }
530     push @Locale, $locale;
531 }
532
533 sub decode_encodings {
534     my @enc;
535
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, $_;
545             push @enc, "$_.UTF-8";
546         }
547     }
548     if ($^O eq 'os390') {
549         push @enc, qw(IBM-037 IBM-819 IBM-1047);
550     }
551
552     return @enc;
553 }
554
555 trylocale("C");
556 trylocale("POSIX");
557 foreach (0..15) {
558     trylocale("ISO8859-$_");
559     trylocale("iso8859$_");
560     trylocale("iso8859-$_");
561     trylocale("iso_8859_$_");
562     trylocale("isolatin$_");
563     trylocale("isolatin-$_");
564     trylocale("iso_latin_$_");
565 }
566
567 # Sanitize the environment so that we can run the external 'locale'
568 # program without the taint mode getting grumpy.
569
570 # $ENV{PATH} is special in VMS.
571 delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv};
572
573 # Other subversive stuff.
574 delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
575
576 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
577     while (<LOCALES>) {
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($_);
582         chomp;
583         trylocale($_);
584     }
585     close(LOCALES);
586 } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') {
587 # The SYS$I18N_LOCALE logical name search list was not present on
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);
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);
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);
616             foreach my $enc (@enc) {
617                 trylocale("$loc.$enc");
618             }
619             $loc = lc $loc;
620             foreach my $enc (@enc) {
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                 }
637             }
638         }
639     }
640 }
641
642 setlocale(LC_ALL, "C");
643
644 if ($^O eq 'darwin') {
645     # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895,
646     # Apple bug ID# 4139653. It also has a problem in Byelorussian.
647     (my $v) = $Config{osvers} =~ /^(\d+)/;
648     if ($v >= 8 and $v < 10) {
649         debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n";
650         @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale;
651     } elsif ($v < 12) {
652         debug "# Skipping be_BY locales -- buggy in Darwin\n";
653         @Locale = grep ! m/^be_BY\.CP1131$/, @Locale;
654     }
655 }
656
657 @Locale = sort @Locale;
658
659 debug "# Locales =\n";
660 for ( @Locale ) {
661     debug "# $_\n";
662 }
663
664 my %Problem;
665 my %Okay;
666 my %Testing;
667 my @Neoalpha;   # Alnums that aren't in the C locale.
668 my %test_names;
669
670 sub tryneoalpha {
671     my ($Locale, $i, $test) = @_;
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
680 my $first_locales_test_number = $final_without_setlocale + 1;
681 my $locales_test_number;
682 my $not_necessarily_a_problem_test_number;
683 my %setlocale_failed;   # List of locales that setlocale() didn't work on
684
685 foreach $Locale (@Locale) {
686     $locales_test_number = $first_locales_test_number - 1;
687     debug "# Locale = $Locale\n";
688
689     unless (setlocale(LC_ALL, $Locale)) {
690         $setlocale_failed{$Locale} = $Locale;
691         next;
692     }
693
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;
706         @Alnum_ = sort grep /\w/, map { chr } 0..255;
707
708         debug "# w = ", join("",@Alnum_), "\n";
709
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         }
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     }
738     foreach (keys %UPPER) {
739         $BoThCaSe{$_}++ if exists $lower{$_};
740     }
741     foreach (keys %lower) {
742         $BoThCaSe{$_}++ if exists $UPPER{$_};
743     }
744     foreach (keys %BoThCaSe) {
745         delete $UPPER{$_};
746         delete $lower{$_};
747     }
748
749     debug "# UPPER    = ", join("", sort keys %UPPER   ), "\n";
750     debug "# lower    = ", join("", sort keys %lower   ), "\n";
751     debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n";
752
753     {   # Find the alphabetic characters that are not considered alphabetics
754         # in the default (C) locale.
755
756         no locale;
757
758         @Neoalpha = ();
759         for (keys %UPPER, keys %lower) {
760             push(@Neoalpha, $_) if (/\W/);
761         }
762     }
763
764     @Neoalpha = sort @Neoalpha;
765
766     debug "# Neoalpha = ", join("",@Neoalpha), "\n";
767
768     my $first_Neoalpha_test_number =  $locales_test_number;
769     my $final_Neoalpha_test_number =  $first_Neoalpha_test_number + 4;
770     if (@Neoalpha == 0) {
771         # If we have no Neoalphas the remaining tests are no-ops.
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) {
774             push @{$Okay{$_}}, $Locale;
775             $locales_test_number++;
776         }
777     } else {
778
779         # Test \w.
780
781         my $word = join('', @Neoalpha);
782
783         ++$locales_test_number;
784         $test_names{$locales_test_number} = 'Verify that alnums outside the C locale match \w';
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);
795
796         # Cross-check the whole 8-bit character set.
797
798         ++$locales_test_number;
799         $test_names{$locales_test_number} = 'Verify that \w and \W are mutually exclusive, as are \d, \D; \s, \S';
800         for (map { chr } 0..255) {
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/) ||
809                         (/\d/ xor /\D/) ||
810                         (/\s/ xor /\S/);
811             }
812             tryneoalpha($Locale, $locales_test_number, $ok);
813         }
814
815         # Test for read-only scalars' locale vs non-locale comparisons.
816
817         {
818             no locale;
819             $a = "qwerty";
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';
830         }
831
832         {
833             my ($from, $to, $lesser, $greater,
834                 @test, %test, $test, $yes, $no, $sign);
835
836             ++$locales_test_number;
837             $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work';
838             $not_necessarily_a_problem_test_number = $locales_test_number;
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]);
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;
857                     ($yes, $no, $sign) = ($lesser lt $greater
858                                       ? ("    ", "not ", 1)
859                                       : ("not ", "    ", -1));
860                 }
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.
864                 @test =
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
875                      'not (($lesser cmp $greater) == -($sign))' # 11
876                      );
877                 @test{@test} = 0 x @test;
878                 $test = 0;
879                 for my $ti (@test) {
880                     if ($is_utf8_locale) {
881                         use locale ':not_characters';
882                         $test{$ti} = eval $ti;
883                     }
884                     else {
885                         # Already in 'use locale';
886                         $test{$ti} = eval $ti;
887                     }
888                     $test ||= $test{$ti}
889                 }
890                 tryneoalpha($Locale, $locales_test_number, $test == 0);
891                 if ($test) {
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                     }
907
908                     last;
909                 }
910             }
911         }
912     }
913
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
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) {
943         use locale;
944
945         my ($x, $y) = (1.23, 1.23);
946
947         $a = "$x";
948         printf ''; # printf used to reset locale to "C"
949         $b = "$y";
950         $ok1 = $a eq $b;
951
952         $c = "$x";
953         my $z = sprintf ''; # sprintf used to reset locale to "C"
954         $d = "$y";
955         $ok2 = $c eq $d;
956         {
957
958             use warnings;
959             my $w = 0;
960             local $SIG{__WARN__} =
961                 sub {
962                     print "# @_\n";
963                     $w++;
964                 };
965
966             # The == (among other ops) used to warn for locales
967             # that had something else than "." as the radix character.
968
969             $ok3 = $c == 1.23;
970             $ok4 = $c == $x;
971             $ok5 = $c == $d;
972             {
973                 no locale;
974
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             }
991
992             $f = "1.23";
993             $g = 2.34;
994
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         }
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
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';
1059
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';
1062
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';
1065
1066     debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n";
1067
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;
1071
1072     tryneoalpha($Locale, ++$locales_test_number, $ok7);
1073     $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale';
1074
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';
1077
1078     debug "# $first_e_test..$locales_test_number: \$e = $e, no locale\n";
1079
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;
1083
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';
1086
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';
1089
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';
1092
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';
1095
1096     debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n";
1097
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.
1101     if (! $is_utf8_locale) {
1102         use locale;
1103
1104         sub lcA {
1105             my $lc0 = lc $_[0];
1106             my $lc1 = lc $_[1];
1107             return $lc0 cmp $lc1;
1108         }
1109
1110         sub lcB {
1111             return lc($_[0]) cmp lc($_[1]);
1112         }
1113
1114         my $x = "ab";
1115         my $y = "aa";
1116         my $z = "AB";
1117
1118         tryneoalpha($Locale, ++$locales_test_number,
1119                     lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
1120                     lcA($x, $z) == 0 && lcB($x, $z) == 0);
1121     }
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';
1144
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.
1148     {
1149         use locale;
1150         no utf8;
1151         my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
1152
1153         my @f = ();
1154         ++$locales_test_number;
1155         $test_names{$locales_test_number} = 'Verify case insensitive matching works';
1156         foreach my $x (sort keys %UPPER) {
1157             if (! $is_utf8_locale) {
1158                 my $y = lc $x;
1159                 next unless uc $y eq $x;
1160                 print "# UPPER $x lc $y ",
1161                         $x =~ /$y/i ? 1 : 0, " ",
1162                         $y =~ /$x/i ? 1 : 0, "\n" if 0;
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;
1194
1195                 # fc is not a locale concept, so Perl uses lc for it.
1196                 push @f, $x unless lc $x eq fc $x;
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 ",
1203                         $x =~ /$y/i ? 1 : 0, " ",
1204                         $y =~ /$x/i ? 1 : 0, "\n" if 0;
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;
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;
1213             }
1214         }
1215
1216         foreach my $x (sort keys %lower) {
1217             if (! $is_utf8_locale) {
1218                 my $y = uc $x;
1219                 next unless lc $y eq $x;
1220                 print "# lower $x uc $y ",
1221                     $x =~ /$y/i ? 1 : 0, " ",
1222                     $y =~ /$x/i ? 1 : 0, "\n" if 0;
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;
1230
1231                 push @f, $x unless lc $x eq fc $x;
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 ",
1238                         $x =~ /$y/i ? 1 : 0, " ",
1239                         $y =~ /$x/i ? 1 : 0, "\n" if 0;
1240                 push @f, $x unless $x =~ /$y/i && $y =~ /$x/i;
1241
1242                 push @f, $x unless lc $x eq fc $x;
1243             }
1244         }
1245         tryneoalpha($Locale, $locales_test_number, @f == 0);
1246         if (@f) {
1247             print "# failed $locales_test_number locale '$Locale' characters @f\n"
1248         }
1249     }
1250
1251     # [perl #109318]
1252     {
1253         my @f = ();
1254         ++$locales_test_number;
1255         $test_names{$locales_test_number} = 'Verify atof with locale radix and negative exponent';
1256
1257         my $radix = POSIX::localeconv()->{decimal_point};
1258         my @nums = (
1259              "3.14e+9",  "3${radix}14e+9",  "3.14e-9",  "3${radix}14e-9",
1260             "-3.14e+9", "-3${radix}14e+9", "-3.14e-9", "-3${radix}14e-9",
1261         );
1262
1263         if (! $is_utf8_locale) {
1264             use locale;
1265             for my $num (@nums) {
1266                 push @f, $num
1267                     unless sprintf("%g", $num) =~ /3.+14/;
1268             }
1269         }
1270         else {
1271             use locale ':not_characters';
1272             for my $num (@nums) {
1273                 push @f, $num
1274                     unless sprintf("%g", $num) =~ /3.+14/;
1275             }
1276         }
1277
1278         tryneoalpha($Locale, $locales_test_number, @f == 0);
1279         if (@f) {
1280             print "# failed $locales_test_number locale '$Locale' numbers @f\n"
1281         }
1282     }
1283 }
1284
1285 my $final_locales_test_number = $locales_test_number;
1286
1287 # Recount the errors.
1288
1289 foreach ($first_locales_test_number..$final_locales_test_number) {
1290     if (%setlocale_failed) {
1291         print "not ";
1292     }
1293     elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) {
1294         if (defined $not_necessarily_a_problem_test_number
1295             && $_ == $not_necessarily_a_problem_test_number)
1296         {
1297             print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n";
1298             print "# It usually indicates a problem in the environment,\n";
1299             print "# not in Perl itself.\n";
1300         }
1301         print "not ";
1302     }
1303     print "ok $_";
1304     print " $test_names{$_}" if defined $test_names{$_};
1305     print "\n";
1306 }
1307
1308 # Give final advice.
1309
1310 my $didwarn = 0;
1311
1312 foreach ($first_locales_test_number..$final_locales_test_number) {
1313     if ($Problem{$_}) {
1314         my @f = sort keys %{ $Problem{$_} };
1315         my $f = join(" ", @f);
1316         $f =~ s/(.{50,60}) /$1\n#\t/g;
1317         print
1318             "#\n",
1319             "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
1320             "#\t", $f, "\n#\n",
1321             "# on your system may have errors because the locale test $_\n",
1322             "# failed in ", (@f == 1 ? "that locale" : "those locales"),
1323             ".\n";
1324         print <<EOW;
1325 #
1326 # If your users are not using these locales you are safe for the moment,
1327 # but please report this failure first to perlbug\@perl.com using the
1328 # perlbug script (as described in the INSTALL file) so that the exact
1329 # details of the failures can be sorted out first and then your operating
1330 # system supplier can be alerted about these anomalies.
1331 #
1332 EOW
1333         $didwarn = 1;
1334     }
1335 }
1336
1337 # Tell which locales were okay and which were not.
1338
1339 if ($didwarn) {
1340     my (@s, @F);
1341
1342     foreach my $l (@Locale) {
1343         my $p = 0;
1344         if ($setlocale_failed{$l}) {
1345             $p++;
1346         }
1347         else {
1348             foreach my $t
1349                         ($first_locales_test_number..$final_locales_test_number)
1350             {
1351                 $p++ if $Problem{$t}{$l};
1352             }
1353         }
1354         push @s, $l if $p == 0;
1355         push @F, $l unless $p == 0;
1356     }
1357
1358     if (@s) {
1359         my $s = join(" ", @s);
1360         $s =~ s/(.{50,60}) /$1\n#\t/g;
1361
1362         warn
1363             "# The following locales\n#\n",
1364             "#\t", $s, "\n#\n",
1365             "# tested okay.\n#\n",
1366     } else {
1367         warn "# None of your locales were fully okay.\n";
1368     }
1369
1370     if (@F) {
1371         my $F = join(" ", @F);
1372         $F =~ s/(.{50,60}) /$1\n#\t/g;
1373
1374         warn
1375           "# The following locales\n#\n",
1376           "#\t", $F, "\n#\n",
1377           "# had problems.\n#\n",
1378     } else {
1379         warn "# None of your locales were broken.\n";
1380     }
1381 }
1382
1383 $test_num = $final_locales_test_number;
1384
1385 # Test that tainting and case changing works on utf8 strings.  These tests are
1386 # placed last to avoid disturbing the hard-coded test numbers that existed at
1387 # the time these were added above this in this file.
1388 # This also tests that locale overrides unicode_strings in the same scope for
1389 # non-utf8 strings.
1390 setlocale(LC_ALL, "C");
1391 {
1392     use locale;
1393     use feature 'unicode_strings';
1394
1395     foreach my $function ("uc", "ucfirst", "lc", "lcfirst", "fc") {
1396         my @list;   # List of code points to test for $function
1397
1398         # Used to calculate the changed case for ASCII characters by using the
1399         # ord, instead of using one of the functions under test.
1400         my $ascii_case_change_delta;
1401         my $above_latin1_case_change_delta; # Same for the specific ords > 255
1402                                             # that we use
1403
1404         # We test an ASCII character, which should change case and be tainted;
1405         # a Latin1 character, which shouldn't change case under this C locale,
1406         #   and is tainted.
1407         # an above-Latin1 character that when the case is changed would cross
1408         #   the 255/256 boundary, so doesn't change case and isn't tainted
1409         # (the \x{149} is one of these, but changes into 2 characters, the
1410         #   first one of which doesn't cross the boundary.
1411         # the final one in each list is an above-Latin1 character whose case
1412         #   does change, and shouldn't be tainted.  The code below uses its
1413         #   position in its list as a marker to indicate that it, unlike the
1414         #   other code points above ASCII, has a successful case change
1415         if ($function =~ /^u/) {
1416             @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}");
1417             $ascii_case_change_delta = -32;
1418             $above_latin1_case_change_delta = -1;
1419         }
1420         else {
1421             @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}");
1422             $ascii_case_change_delta = +32;
1423             $above_latin1_case_change_delta = +1;
1424         }
1425         foreach my $is_utf8_locale (0 .. 1) {
1426             foreach my $j (0 .. $#list) {
1427                 my $char = $list[$j];
1428
1429                 for my $encoded_in_utf8 (0 .. 1) {
1430                     my $should_be;
1431                     my $changed;
1432                     if (! $is_utf8_locale) {
1433                         $should_be = ($j == $#list)
1434                             ? chr(ord($char) + $above_latin1_case_change_delta)
1435                             : (length $char == 0 || ord($char) > 127)
1436                             ? $char
1437                             : chr(ord($char) + $ascii_case_change_delta);
1438
1439                         # This monstrosity is in order to avoid using an eval,
1440                         # which might perturb the results
1441                         $changed = ($function eq "uc")
1442                                     ? uc($char)
1443                                     : ($function eq "ucfirst")
1444                                       ? ucfirst($char)
1445                                       : ($function eq "lc")
1446                                         ? lc($char)
1447                                         : ($function eq "lcfirst")
1448                                           ? lcfirst($char)
1449                                           : ($function eq "fc")
1450                                             ? fc($char)
1451                                             : die("Unexpected function \"$function\"");
1452                     }
1453                     else {
1454                         {
1455                             no locale;
1456
1457                             # For utf8-locales the case changing functions
1458                             # should work just like they do outside of locale.
1459                             # Can use eval here because not testing it when
1460                             # not in locale.
1461                             $should_be = eval "$function('$char')";
1462                             die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if  $@;
1463
1464                         }
1465                         use locale ':not_characters';
1466                         $changed = ($function eq "uc")
1467                                     ? uc($char)
1468                                     : ($function eq "ucfirst")
1469                                       ? ucfirst($char)
1470                                       : ($function eq "lc")
1471                                         ? lc($char)
1472                                         : ($function eq "lcfirst")
1473                                           ? lcfirst($char)
1474                                           : ($function eq "fc")
1475                                             ? fc($char)
1476                                             : die("Unexpected function \"$function\"");
1477                     }
1478                     ok($changed eq $should_be,
1479                         "$function(\"$char\") in C locale "
1480                         . (($is_utf8_locale)
1481                             ? "(use locale ':not_characters'"
1482                             : "(use locale")
1483                         . (($encoded_in_utf8)
1484                             ? "; encoded in utf8)"
1485                             : "; not encoded in utf8)")
1486                         . " should be \"$should_be\", got \"$changed\"");
1487
1488                     # Tainting shouldn't happen for utf8 locales, empty
1489                     # strings, or those characters above 255.
1490                     (! $is_utf8_locale && length($char) > 0 && ord($char) < 256)
1491                     ? check_taint($changed)
1492                     : check_taint_not($changed);
1493
1494                     # Use UTF-8 next time through the loop
1495                     utf8::upgrade($char);
1496                 }
1497             }
1498         }
1499     }
1500 }
1501
1502 print "1..$test_num\n";
1503
1504 # eof