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