This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Skip casing for high code points
[perl5.git] / lib / locale.t
CommitLineData
8ebc5c01 1#!./perl -wT
2
66cbab2c
KW
3# This tests plain 'use locale' and adorned 'use locale ":not_characters"'
4# Because these pragmas are compile time, and I (khw) am trying to test
5# without using 'eval' as much as possible, which might cloud the issue, the
6# crucial parts of the code are duplicated in a block for each pragma.
7
6c2e653d
KW
8# To make a TODO test, add the string 'TODO' to its %test_names value
9
9de25f67
KW
10my $is_ebcdic = ord("A") == 193;
11
02d357f9
KW
12no warnings 'locale'; # We test even weird locales; and do some scary things
13 # in ok locales
14
e3a2734b
KW
15binmode STDOUT, ':utf8';
16binmode STDERR, ':utf8';
17
8ebc5c01 18BEGIN {
19 chdir 't' if -d 't';
20822f61 20 @INC = '../lib';
f9cbebe1 21 unshift @INC, '.';
ef9d5242
KW
22 require './loc_tools.pl';
23 unless (locales_enabled('LC_CTYPE')) {
b002077a
CS
24 print "1..0\n";
25 exit;
26 }
2de3dbcc 27 $| = 1;
ef9d5242 28 require Config; import Config;
8ebc5c01 29}
30
31use strict;
26230909 32use feature 'fc', 'postderef';
8ebc5c01 33
92891c66 34# =1 adds debugging output; =2 increases the verbosity somewhat
108a305e 35my $debug = $ENV{PERL_DEBUG_FULL_TEST} // 0;
284102e8 36
6d5d702a
KW
37# Certain tests have been shown to be problematical for a few locales. Don't
38# fail them unless at least this percentage of the tested locales fail.
4874c8de 39# On AIX machines, many locales call a no-break space a graphic.
df33771d 40# (There aren't 1000 locales currently in existence, so 99.9 works)
4874c8de 41my $acceptable_failure_percentage = ($^O =~ / ^ ( AIX ) $ /ix)
31f658ea
KW
42 ? 99.9
43 : 5;
6d5d702a 44
7493b8f2 45# The list of test numbers of the problematic tests.
5b1f18a7 46my %problematical_tests;
7493b8f2 47
852b6749
KW
48# If any %problematical_tests fails in one of these locales, it is
49# considered a TODO.
50my %known_bad_locales = (
51 irix => qr/ ^ (?: cs | hu | sk ) $/x,
52 darwin => qr/ ^ lt_LT.ISO8859 /ix,
53 os390 => qr/ ^ italian /ix,
54 );
7493b8f2 55
db4b7445
A
56use Dumpvalue;
57
58my $dumper = Dumpvalue->new(
59 tick => qq{"},
60 quoteHighBit => 0,
61 unctrl => "quote"
62 );
6be75cd7 63sub debug {
db4b7445 64 return unless $debug;
46973bb2 65 my($mess) = join "", '# ', @_;
d8f8a481 66 chomp $mess;
bd083ee5 67 print STDERR $dumper->stringify($mess,1), "\n";
6be75cd7
JH
68}
69
92891c66
KW
70sub debug_more {
71 return unless $debug > 1;
72 return debug(@_);
73}
74
6be75cd7 75sub debugf {
bd083ee5 76 printf STDERR @_ if $debug;
6be75cd7
JH
77}
78
40205cab 79$a = 'abc %9';
8ebc5c01 80
c213d471
KW
81my $test_num = 0;
82
8ebc5c01 83sub ok {
c213d471 84 my ($result, $message) = @_;
e3a2734b 85 $message = "" unless defined $message;
8ebc5c01 86
87 print 'not ' unless ($result);
c213d471 88 print "ok " . ++$test_num;
e3a2734b
KW
89 print " $message";
90 print "\n";
cf82bb84 91 return ($result) ? 1 : 0;
8ebc5c01 92}
93
94# First we'll do a lot of taint checking for locales.
95# This is the easiest to test, actually, as any locale,
96# even the default locale will taint under 'use locale'.
97
98sub is_tainted { # hello, camel two.
9f1b1f2d 99 no warnings 'uninitialized' ;
8ebc5c01 100 my $dummy;
ba74571d 101 local $@;
8ebc5c01 102 not eval { $dummy = join("", @_), kill 0; 1 }
103}
104
a9b7c637
KW
105sub check_taint ($;$) {
106 my $message_tail = $_[1] // "";
2575405e
KW
107
108 # Extra blanks are so aligns with taint_not output
109 $message_tail = ": $message_tail" if $message_tail;
a9b7c637 110 ok is_tainted($_[0]), "verify that is tainted$message_tail";
8ebc5c01 111}
112
a9b7c637
KW
113sub check_taint_not ($;$) {
114 my $message_tail = $_[1] // "";
2575405e 115 $message_tail = ": $message_tail" if $message_tail;
a9b7c637 116 ok((not is_tainted($_[0])), "verify that isn't tainted$message_tail");
8ebc5c01 117}
118
308482c2
KW
119foreach my $category (qw(ALL COLLATE CTYPE MESSAGES MONETARY NUMERIC TIME)) {
120 my $short_result = locales_enabled($category);
121 ok ($short_result == 0 || $short_result == 1,
122 "Verify locales_enabled('$category') returns 0 or 1");
123 debug("locales_enabled('$category') returned '$short_result'");
124 my $long_result = locales_enabled("LC_$category");
125 if (! ok ($long_result == $short_result,
126 " and locales_enabled('LC_$category') returns "
127 . "the same value")
128 ) {
129 debug("locales_enabled('LC_$category') returned $long_result");
130 }
131}
132
bf3cd0e6
KW
133"\tb\t" =~ /^m?(\s)(.*)\1$/;
134check_taint_not $&, "not tainted outside 'use locale'";
135;
136
8ebc5c01 137use locale; # engage locale and therefore locale taint.
138
b6fb4de7
KW
139# BE SURE TO COPY ANYTHING YOU ADD to these tests to the block below for
140# ":notcharacters"
2575405e
KW
141
142check_taint_not $a, '$a';
143
144check_taint uc($a), 'uc($a)';
145check_taint "\U$a", '"\U$a"';
146check_taint ucfirst($a), 'ucfirst($a)';
147check_taint "\u$a", '"\u$a"';
148check_taint lc($a), 'lc($a)';
149check_taint fc($a), 'fc($a)';
150check_taint "\L$a", '"\L$a"';
151check_taint "\F$a", '"\F$a"';
152check_taint lcfirst($a), 'lcfirst($a)';
153check_taint "\l$a", '"\l$a"';
154
155check_taint_not sprintf('%e', 123.456), "sprintf('%e', 123.456)";
156check_taint_not sprintf('%f', 123.456), "sprintf('%f', 123.456)";
157check_taint_not sprintf('%g', 123.456), "sprintf('%g', 123.456)";
158check_taint_not sprintf('%d', 123.456), "sprintf('%d', 123.456)";
159check_taint_not sprintf('%x', 123.456), "sprintf('%x', 123.456)";
8ebc5c01 160
161$_ = $a; # untaint $_
162
163$_ = uc($a); # taint $_
164
2575405e 165check_taint $_, '$_ = uc($a)';
8ebc5c01 166
167/(\w)/; # taint $&, $`, $', $+, $1.
2575405e 168check_taint $&, "\$& from /(\\w)/";
fdf73a7f
KW
169check_taint $`, "\t\$`";
170check_taint $', "\t\$'";
171check_taint $+, "\t\$+";
172check_taint $1, "\t\$1";
173check_taint_not $2, "\t\$2";
8ebc5c01 174
175/(.)/; # untaint $&, $`, $', $+, $1.
2575405e 176check_taint_not $&, "\$& from /(.)/";
fdf73a7f
KW
177check_taint_not $`, "\t\$`";
178check_taint_not $', "\t\$'";
179check_taint_not $+, "\t\$+";
180check_taint_not $1, "\t\$1";
181check_taint_not $2, "\t\$2";
8ebc5c01 182
183/(\W)/; # taint $&, $`, $', $+, $1.
2575405e 184check_taint $&, "\$& from /(\\W)/";
fdf73a7f
KW
185check_taint $`, "\t\$`";
186check_taint $', "\t\$'";
187check_taint $+, "\t\$+";
188check_taint $1, "\t\$1";
189check_taint_not $2, "\t\$2";
8ebc5c01 190
9486279c 191/(.)/; # untaint $&, $`, $', $+, $1.
2575405e 192check_taint_not $&, "\$& from /(.)/";
fdf73a7f
KW
193check_taint_not $`, "\t\$`";
194check_taint_not $', "\t\$'";
195check_taint_not $+, "\t\$+";
196check_taint_not $1, "\t\$1";
197check_taint_not $2, "\t\$2";
9486279c 198
8ebc5c01 199/(\s)/; # taint $&, $`, $', $+, $1.
2575405e 200check_taint $&, "\$& from /(\\s)/";
fdf73a7f
KW
201check_taint $`, "\t\$`";
202check_taint $', "\t\$'";
203check_taint $+, "\t\$+";
204check_taint $1, "\t\$1";
205check_taint_not $2, "\t\$2";
8ebc5c01 206
9486279c 207/(.)/; # untaint $&, $`, $', $+, $1.
2575405e 208check_taint_not $&, "\$& from /(.)/";
9486279c 209
8ebc5c01 210/(\S)/; # taint $&, $`, $', $+, $1.
2575405e 211check_taint $&, "\$& from /(\\S)/";
fdf73a7f
KW
212check_taint $`, "\t\$`";
213check_taint $', "\t\$'";
214check_taint $+, "\t\$+";
215check_taint $1, "\t\$1";
216check_taint_not $2, "\t\$2";
8ebc5c01 217
63baef57 218/(.)/; # untaint $&, $`, $', $+, $1.
2575405e 219check_taint_not $&, "\$& from /(.)/";
63baef57 220
40205cab
KW
221"0" =~ /(\d)/; # taint $&, $`, $', $+, $1.
222check_taint $&, "\$& from /(\\d)/";
223check_taint $`, "\t\$`";
224check_taint $', "\t\$'";
225check_taint $+, "\t\$+";
226check_taint $1, "\t\$1";
227check_taint_not $2, "\t\$2";
228
229/(.)/; # untaint $&, $`, $', $+, $1.
230check_taint_not $&, "\$& from /(.)/";
231
232/(\D)/; # taint $&, $`, $', $+, $1.
233check_taint $&, "\$& from /(\\D)/";
234check_taint $`, "\t\$`";
235check_taint $', "\t\$'";
236check_taint $+, "\t\$+";
237check_taint $1, "\t\$1";
238check_taint_not $2, "\t\$2";
239
240/(.)/; # untaint $&, $`, $', $+, $1.
241check_taint_not $&, "\$& from /(.)/";
242
243/([[:alnum:]])/; # taint $&, $`, $', $+, $1.
244check_taint $&, "\$& from /([[:alnum:]])/";
245check_taint $`, "\t\$`";
246check_taint $', "\t\$'";
247check_taint $+, "\t\$+";
248check_taint $1, "\t\$1";
249check_taint_not $2, "\t\$2";
250
251/(.)/; # untaint $&, $`, $', $+, $1.
252check_taint_not $&, "\$& from /(.)/";
253
254/([[:^alnum:]])/; # taint $&, $`, $', $+, $1.
255check_taint $&, "\$& from /([[:^alnum:]])/";
256check_taint $`, "\t\$`";
257check_taint $', "\t\$'";
258check_taint $+, "\t\$+";
259check_taint $1, "\t\$1";
260check_taint_not $2, "\t\$2";
261
63baef57 262"a" =~ /(a)|(\w)/; # taint $&, $`, $', $+, $1.
2575405e 263check_taint $&, "\$& from /(a)|(\\w)/";
63baef57
KW
264check_taint $`, "\t\$`";
265check_taint $', "\t\$'";
266check_taint $+, "\t\$+";
267check_taint $1, "\t\$1";
2575405e
KW
268ok($1 eq 'a', ("\t" x 5) . "\$1 is 'a'");
269ok(! defined $2, ("\t" x 5) . "\$2 is undefined");
63baef57
KW
270check_taint_not $2, "\t\$2";
271check_taint_not $3, "\t\$3";
272
273/(.)/; # untaint $&, $`, $', $+, $1.
2575405e 274check_taint_not $&, "\$& from /(.)/";
63baef57
KW
275
276"\N{CYRILLIC SMALL LETTER A}" =~ /(\N{CYRILLIC CAPITAL LETTER A})/i; # no tainting because no locale dependence
2575405e 277check_taint_not $&, "\$& from /(\\N{CYRILLIC CAPITAL LETTER A})/i";
63baef57
KW
278check_taint_not $`, "\t\$`";
279check_taint_not $', "\t\$'";
280check_taint_not $+, "\t\$+";
281check_taint_not $1, "\t\$1";
2575405e 282ok($1 eq "\N{CYRILLIC SMALL LETTER A}", ("\t" x 4) . "\t\$1 is 'small cyrillic a'");
63baef57
KW
283check_taint_not $2, "\t\$2";
284
285/(.)/; # untaint $&, $`, $', $+, $1.
2575405e 286check_taint_not $&, "\$& from /./";
63baef57 287
40205cab
KW
288"(\N{KELVIN SIGN})" =~ /(\N{KELVIN SIGN})/i; # taints because depends on locale
289check_taint $&, "\$& from /(\\N{KELVIN SIGN})/i";
290check_taint $`, "\t\$`";
291check_taint $', "\t\$'";
292check_taint $+, "\t\$+";
293check_taint $1, "\t\$1";
294check_taint_not $2, "\t\$2";
295
63baef57 296/(.)/; # untaint $&, $`, $', $+, $1.
2575405e 297check_taint_not $&, "\$& from /(.)/";
63baef57
KW
298
299"a:" =~ /(.)\b(.)/; # taint $&, $`, $', $+, $1.
2575405e 300check_taint $&, "\$& from /(.)\\b(.)/";
63baef57
KW
301check_taint $`, "\t\$`";
302check_taint $', "\t\$'";
303check_taint $+, "\t\$+";
304check_taint $1, "\t\$1";
305check_taint $2, "\t\$2";
306check_taint_not $3, "\t\$3";
307
308/(.)/; # untaint $&, $`, $', $+, $1.
2575405e 309check_taint_not $&, "\$& from /./";
63baef57
KW
310
311"aa" =~ /(.)\B(.)/; # taint $&, $`, $', $+, $1.
2575405e 312check_taint $&, "\$& from /(.)\\B(.)/";
63baef57
KW
313check_taint $`, "\t\$`";
314check_taint $', "\t\$'";
315check_taint $+, "\t\$+";
316check_taint $1, "\t\$1";
317check_taint $2, "\t\$2";
318check_taint_not $3, "\t\$3";
319
320/(.)/; # untaint $&, $`, $', $+, $1.
2575405e 321check_taint_not $&, "\$& from /./";
63baef57
KW
322
323"aaa" =~ /(.).(\1)/i; # notaint because not locale dependent
2575405e 324check_taint_not $&, "\$ & from /(.).(\\1)/";
63baef57
KW
325check_taint_not $`, "\t\$`";
326check_taint_not $', "\t\$'";
327check_taint_not $+, "\t\$+";
328check_taint_not $1, "\t\$1";
329check_taint_not $2, "\t\$2";
2575405e 330check_taint_not $3, "\t\$3";
63baef57
KW
331
332/(.)/; # untaint $&, $`, $', $+, $1.
2575405e 333check_taint_not $&, "\$ & from /./";
63baef57 334
8ebc5c01 335$_ = $a; # untaint $_
336
2575405e 337check_taint_not $_, 'untainting $_ works';
8ebc5c01 338
339/(b)/; # this must not taint
2575405e 340check_taint_not $&, "\$ & from /(b)/";
fdf73a7f
KW
341check_taint_not $`, "\t\$`";
342check_taint_not $', "\t\$'";
343check_taint_not $+, "\t\$+";
344check_taint_not $1, "\t\$1";
345check_taint_not $2, "\t\$2";
8ebc5c01 346
347$_ = $a; # untaint $_
348
2575405e 349check_taint_not $_, 'untainting $_ works';
8ebc5c01 350
351$b = uc($a); # taint $b
352s/(.+)/$b/; # this must taint only the $_
353
2575405e 354check_taint $_, '$_ (wasn\'t tainted) from s/(.+)/$b/ where $b is tainted';
fdf73a7f
KW
355check_taint_not $&, "\t\$&";
356check_taint_not $`, "\t\$`";
357check_taint_not $', "\t\$'";
358check_taint_not $+, "\t\$+";
359check_taint_not $1, "\t\$1";
360check_taint_not $2, "\t\$2";
8ebc5c01 361
362$_ = $a; # untaint $_
363
364s/(.+)/b/; # this must not taint
2575405e 365check_taint_not $_, '$_ (wasn\'t tainted) from s/(.+)/b/';
fdf73a7f
KW
366check_taint_not $&, "\t\$&";
367check_taint_not $`, "\t\$`";
368check_taint_not $', "\t\$'";
369check_taint_not $+, "\t\$+";
370check_taint_not $1, "\t\$1";
371check_taint_not $2, "\t\$2";
8ebc5c01 372
373$b = $a; # untaint $b
374
375($b = $a) =~ s/\w/$&/;
2575405e
KW
376check_taint $b, '$b from ($b = $a) =~ s/\w/$&/'; # $b should be tainted.
377check_taint_not $a, '$a from ($b = $a) =~ s/\w/$&/'; # $a should be not.
8ebc5c01 378
379$_ = $a; # untaint $_
380
381s/(\w)/\l$1/; # this must taint
2575405e 382check_taint $_, '$_ (wasn\'t tainted) from s/(\w)/\l$1/,'; # this must taint
fdf73a7f
KW
383check_taint $&, "\t\$&";
384check_taint $`, "\t\$`";
385check_taint $', "\t\$'";
386check_taint $+, "\t\$+";
387check_taint $1, "\t\$1";
388check_taint_not $2, "\t\$2";
8ebc5c01 389
390$_ = $a; # untaint $_
391
392s/(\w)/\L$1/; # this must taint
2575405e 393check_taint $_, '$_ (wasn\'t tainted) from s/(\w)/\L$1/,';
fdf73a7f
KW
394check_taint $&, "\t\$&";
395check_taint $`, "\t\$`";
396check_taint $', "\t\$'";
397check_taint $+, "\t\$+";
398check_taint $1, "\t\$1";
399check_taint_not $2, "\t\$2";
8ebc5c01 400
401$_ = $a; # untaint $_
402
403s/(\w)/\u$1/; # this must taint
2575405e 404check_taint $_, '$_ (wasn\'t tainted) from s/(\w)/\u$1/';
fdf73a7f
KW
405check_taint $&, "\t\$&";
406check_taint $`, "\t\$`";
407check_taint $', "\t\$'";
408check_taint $+, "\t\$+";
409check_taint $1, "\t\$1";
410check_taint_not $2, "\t\$2";
8ebc5c01 411
412$_ = $a; # untaint $_
413
414s/(\w)/\U$1/; # this must taint
2575405e 415check_taint $_, '$_ (wasn\'t tainted) from s/(\w)/\U$1/';
fdf73a7f
KW
416check_taint $&, "\t\$&";
417check_taint $`, "\t\$`";
418check_taint $', "\t\$'";
419check_taint $+, "\t\$+";
420check_taint $1, "\t\$1";
421check_taint_not $2, "\t\$2";
8ebc5c01 422
faab3281
KW
423# After all this tainting $a should be cool.
424
2575405e 425check_taint_not $a, '$a still not tainted';
faab3281 426
b99851e1
KW
427"a" =~ /([a-z])/;
428check_taint_not $1, '"a" =~ /([a-z])/';
429"foo.bar_baz" =~ /^(.*)[._](.*?)$/; # Bug 120675
430check_taint_not $1, '"foo.bar_baz" =~ /^(.*)[._](.*?)$/';
431
7a9d1c02 432# BE SURE TO COPY ANYTHING YOU ADD to the block below
8ebc5c01 433
66cbab2c
KW
434{ # This is just the previous tests copied here with a different
435 # compile-time pragma.
436
437 use locale ':not_characters'; # engage restricted locale with different
438 # tainting rules
b6fb4de7
KW
439 check_taint_not $a, '$a';
440
441 check_taint_not uc($a), 'uc($a)';
442 check_taint_not "\U$a", '"\U$a"';
443 check_taint_not ucfirst($a), 'ucfirst($a)';
444 check_taint_not "\u$a", '"\u$a"';
445 check_taint_not lc($a), 'lc($a)';
446 check_taint_not fc($a), 'fc($a)';
447 check_taint_not "\L$a", '"\L$a"';
448 check_taint_not "\F$a", '"\F$a"';
449 check_taint_not lcfirst($a), 'lcfirst($a)';
450 check_taint_not "\l$a", '"\l$a"';
451
452 check_taint_not sprintf('%e', 123.456), "sprintf('%e', 123.456)";
453 check_taint_not sprintf('%f', 123.456), "sprintf('%f', 123.456)";
454 check_taint_not sprintf('%g', 123.456), "sprintf('%g', 123.456)";
455 check_taint_not sprintf('%d', 123.456), "sprintf('%d', 123.456)";
456 check_taint_not sprintf('%x', 123.456), "sprintf('%x', 123.456)";
66cbab2c
KW
457
458 $_ = $a; # untaint $_
459
b6fb4de7 460 $_ = uc($a);
66cbab2c 461
b6fb4de7 462 check_taint_not $_, '$_ = uc($a)';
66cbab2c 463
b6fb4de7
KW
464 /(\w)/;
465 check_taint_not $&, "\$& from /(\\w)/";
466 check_taint_not $`, "\t\$`";
467 check_taint_not $', "\t\$'";
468 check_taint_not $+, "\t\$+";
469 check_taint_not $1, "\t\$1";
470 check_taint_not $2, "\t\$2";
66cbab2c
KW
471
472 /(.)/; # untaint $&, $`, $', $+, $1.
b6fb4de7
KW
473 check_taint_not $&, "\$& from /(.)/";
474 check_taint_not $`, "\t\$`";
475 check_taint_not $', "\t\$'";
476 check_taint_not $+, "\t\$+";
477 check_taint_not $1, "\t\$1";
478 check_taint_not $2, "\t\$2";
479
480 /(\W)/;
481 check_taint_not $&, "\$& from /(\\W)/";
482 check_taint_not $`, "\t\$`";
483 check_taint_not $', "\t\$'";
484 check_taint_not $+, "\t\$+";
485 check_taint_not $1, "\t\$1";
486 check_taint_not $2, "\t\$2";
66cbab2c 487
b6fb4de7
KW
488 /(.)/; # untaint $&, $`, $', $+, $1.
489 check_taint_not $&, "\$& from /(.)/";
490 check_taint_not $`, "\t\$`";
491 check_taint_not $', "\t\$'";
492 check_taint_not $+, "\t\$+";
493 check_taint_not $1, "\t\$1";
494 check_taint_not $2, "\t\$2";
495
496 /(\s)/;
497 check_taint_not $&, "\$& from /(\\s)/";
498 check_taint_not $`, "\t\$`";
499 check_taint_not $', "\t\$'";
500 check_taint_not $+, "\t\$+";
501 check_taint_not $1, "\t\$1";
502 check_taint_not $2, "\t\$2";
66cbab2c 503
b6fb4de7
KW
504 /(.)/; # untaint $&, $`, $', $+, $1.
505 check_taint_not $&, "\$& from /(.)/";
66cbab2c 506
b6fb4de7
KW
507 /(\S)/;
508 check_taint_not $&, "\$& from /(\\S)/";
509 check_taint_not $`, "\t\$`";
510 check_taint_not $', "\t\$'";
511 check_taint_not $+, "\t\$+";
512 check_taint_not $1, "\t\$1";
513 check_taint_not $2, "\t\$2";
66cbab2c 514
b6fb4de7
KW
515 /(.)/; # untaint $&, $`, $', $+, $1.
516 check_taint_not $&, "\$& from /(.)/";
517
518 "0" =~ /(\d)/;
519 check_taint_not $&, "\$& from /(\\d)/";
520 check_taint_not $`, "\t\$`";
521 check_taint_not $', "\t\$'";
522 check_taint_not $+, "\t\$+";
523 check_taint_not $1, "\t\$1";
524 check_taint_not $2, "\t\$2";
525
526 /(.)/; # untaint $&, $`, $', $+, $1.
527 check_taint_not $&, "\$& from /(.)/";
66cbab2c 528
b6fb4de7
KW
529 /(\D)/;
530 check_taint_not $&, "\$& from /(\\D)/";
531 check_taint_not $`, "\t\$`";
532 check_taint_not $', "\t\$'";
533 check_taint_not $+, "\t\$+";
534 check_taint_not $1, "\t\$1";
535 check_taint_not $2, "\t\$2";
66cbab2c 536
b6fb4de7
KW
537 /(.)/; # untaint $&, $`, $', $+, $1.
538 check_taint_not $&, "\$& from /(.)/";
539
540 /([[:alnum:]])/;
541 check_taint_not $&, "\$& from /([[:alnum:]])/";
542 check_taint_not $`, "\t\$`";
543 check_taint_not $', "\t\$'";
544 check_taint_not $+, "\t\$+";
545 check_taint_not $1, "\t\$1";
546 check_taint_not $2, "\t\$2";
547
548 /(.)/; # untaint $&, $`, $', $+, $1.
549 check_taint_not $&, "\$& from /(.)/";
550
551 /([[:^alnum:]])/;
552 check_taint_not $&, "\$& from /([[:^alnum:]])/";
553 check_taint_not $`, "\t\$`";
554 check_taint_not $', "\t\$'";
555 check_taint_not $+, "\t\$+";
556 check_taint_not $1, "\t\$1";
557 check_taint_not $2, "\t\$2";
558
559 "a" =~ /(a)|(\w)/;
560 check_taint_not $&, "\$& from /(a)|(\\w)/";
561 check_taint_not $`, "\t\$`";
562 check_taint_not $', "\t\$'";
563 check_taint_not $+, "\t\$+";
564 check_taint_not $1, "\t\$1";
565 ok($1 eq 'a', ("\t" x 5) . "\$1 is 'a'");
566 ok(! defined $2, ("\t" x 5) . "\$2 is undefined");
567 check_taint_not $2, "\t\$2";
568 check_taint_not $3, "\t\$3";
569
570 /(.)/; # untaint $&, $`, $', $+, $1.
571 check_taint_not $&, "\$& from /(.)/";
572
573 "\N{CYRILLIC SMALL LETTER A}" =~ /(\N{CYRILLIC CAPITAL LETTER A})/i;
574 check_taint_not $&, "\$& from /(\\N{CYRILLIC CAPITAL LETTER A})/i";
575 check_taint_not $`, "\t\$`";
576 check_taint_not $', "\t\$'";
577 check_taint_not $+, "\t\$+";
578 check_taint_not $1, "\t\$1";
579 ok($1 eq "\N{CYRILLIC SMALL LETTER A}", ("\t" x 4) . "\t\$1 is 'small cyrillic a'");
580 check_taint_not $2, "\t\$2";
581
582 /(.)/; # untaint $&, $`, $', $+, $1.
583 check_taint_not $&, "\$& from /./";
66cbab2c 584
b6fb4de7
KW
585 "(\N{KELVIN SIGN})" =~ /(\N{KELVIN SIGN})/i;
586 check_taint_not $&, "\$& from /(\\N{KELVIN SIGN})/i";
587 check_taint_not $`, "\t\$`";
588 check_taint_not $', "\t\$'";
589 check_taint_not $+, "\t\$+";
590 check_taint_not $1, "\t\$1";
591 check_taint_not $2, "\t\$2";
592
593 /(.)/; # untaint $&, $`, $', $+, $1.
594 check_taint_not $&, "\$& from /(.)/";
595
596 "a:" =~ /(.)\b(.)/;
597 check_taint_not $&, "\$& from /(.)\\b(.)/";
598 check_taint_not $`, "\t\$`";
599 check_taint_not $', "\t\$'";
600 check_taint_not $+, "\t\$+";
601 check_taint_not $1, "\t\$1";
602 check_taint_not $2, "\t\$2";
603 check_taint_not $3, "\t\$3";
604
605 /(.)/; # untaint $&, $`, $', $+, $1.
606 check_taint_not $&, "\$& from /./";
607
608 "aa" =~ /(.)\B(.)/;
609 check_taint_not $&, "\$& from /(.)\\B(.)/";
610 check_taint_not $`, "\t\$`";
611 check_taint_not $', "\t\$'";
612 check_taint_not $+, "\t\$+";
613 check_taint_not $1, "\t\$1";
614 check_taint_not $2, "\t\$2";
615 check_taint_not $3, "\t\$3";
616
617 /(.)/; # untaint $&, $`, $', $+, $1.
618 check_taint_not $&, "\$& from /./";
619
620 "aaa" =~ /(.).(\1)/i; # notaint because not locale dependent
621 check_taint_not $&, "\$ & from /(.).(\\1)/";
622 check_taint_not $`, "\t\$`";
623 check_taint_not $', "\t\$'";
624 check_taint_not $+, "\t\$+";
625 check_taint_not $1, "\t\$1";
626 check_taint_not $2, "\t\$2";
627 check_taint_not $3, "\t\$3";
628
629 /(.)/; # untaint $&, $`, $', $+, $1.
630 check_taint_not $&, "\$ & from /./";
631
632 $_ = $a; # untaint $_
633
634 check_taint_not $_, 'untainting $_ works';
635
636 /(b)/;
637 check_taint_not $&, "\$ & from /(b)/";
638 check_taint_not $`, "\t\$`";
639 check_taint_not $', "\t\$'";
640 check_taint_not $+, "\t\$+";
641 check_taint_not $1, "\t\$1";
642 check_taint_not $2, "\t\$2";
66cbab2c
KW
643
644 $_ = $a; # untaint $_
645
b6fb4de7
KW
646 check_taint_not $_, 'untainting $_ works';
647
648 s/(.+)/b/;
649 check_taint_not $_, '$_ (wasn\'t tainted) from s/(.+)/b/';
650 check_taint_not $&, "\t\$&";
651 check_taint_not $`, "\t\$`";
652 check_taint_not $', "\t\$'";
653 check_taint_not $+, "\t\$+";
654 check_taint_not $1, "\t\$1";
655 check_taint_not $2, "\t\$2";
66cbab2c
KW
656
657 $b = $a; # untaint $b
658
659 ($b = $a) =~ s/\w/$&/;
b6fb4de7
KW
660 check_taint_not $b, '$b from ($b = $a) =~ s/\w/$&/';
661 check_taint_not $a, '$a from ($b = $a) =~ s/\w/$&/';
66cbab2c
KW
662
663 $_ = $a; # untaint $_
664
b6fb4de7
KW
665 s/(\w)/\l$1/;
666 check_taint_not $_, '$_ (wasn\'t tainted) from s/(\w)/\l$1/,'; # this must taint
667 check_taint_not $&, "\t\$&";
668 check_taint_not $`, "\t\$`";
669 check_taint_not $', "\t\$'";
670 check_taint_not $+, "\t\$+";
671 check_taint_not $1, "\t\$1";
672 check_taint_not $2, "\t\$2";
66cbab2c
KW
673
674 $_ = $a; # untaint $_
675
b6fb4de7
KW
676 s/(\w)/\L$1/;
677 check_taint_not $_, '$_ (wasn\'t tainted) from s/(\w)/\L$1/,';
678 check_taint_not $&, "\t\$&";
679 check_taint_not $`, "\t\$`";
680 check_taint_not $', "\t\$'";
681 check_taint_not $+, "\t\$+";
682 check_taint_not $1, "\t\$1";
683 check_taint_not $2, "\t\$2";
66cbab2c
KW
684
685 $_ = $a; # untaint $_
686
b6fb4de7
KW
687 s/(\w)/\u$1/;
688 check_taint_not $_, '$_ (wasn\'t tainted) from s/(\w)/\u$1/';
689 check_taint_not $&, "\t\$&";
690 check_taint_not $`, "\t\$`";
691 check_taint_not $', "\t\$'";
692 check_taint_not $+, "\t\$+";
693 check_taint_not $1, "\t\$1";
694 check_taint_not $2, "\t\$2";
66cbab2c
KW
695
696 $_ = $a; # untaint $_
697
b6fb4de7
KW
698 s/(\w)/\U$1/;
699 check_taint_not $_, '$_ (wasn\'t tainted) from s/(\w)/\U$1/';
700 check_taint_not $&, "\t\$&";
701 check_taint_not $`, "\t\$`";
702 check_taint_not $', "\t\$'";
703 check_taint_not $+, "\t\$+";
704 check_taint_not $1, "\t\$1";
705 check_taint_not $2, "\t\$2";
66cbab2c
KW
706
707 # After all this tainting $a should be cool.
708
b6fb4de7 709 check_taint_not $a, '$a still not tainted';
7a9d1c02
KW
710
711 "a" =~ /([a-z])/;
712 check_taint_not $1, '"a" =~ /([a-z])/';
713 "foo.bar_baz" =~ /^(.*)[._](.*?)$/; # Bug 120675
714 check_taint_not $1, '"foo.bar_baz" =~ /^(.*)[._](.*?)$/';
b6fb4de7 715
66cbab2c
KW
716}
717
718# Here are in scope of 'use locale'
719
8ebc5c01 720# I think we've seen quite enough of taint.
721# Let us do some *real* locale work now,
284102e8 722# unless setlocale() is missing (i.e. minitest).
8ebc5c01 723
6cf0b567 724# The test number before our first setlocale()
66330f13 725my $final_without_setlocale = $test_num;
6cf0b567 726
284102e8
JH
727# Find locales.
728
46973bb2 729debug "Scanning for locales...\n";
6be75cd7 730
9b0711ee
KW
731require POSIX; import POSIX ':locale_h';
732
3d9e170f 733my @Locale = find_locales([ &POSIX::LC_CTYPE, &POSIX::LC_NUMERIC, &POSIX::LC_ALL ]);
4599a1de 734
46973bb2 735debug "Locales =\n";
887ef7ed 736for ( @Locale ) {
46973bb2 737 debug "$_\n";
887ef7ed 738}
8ebc5c01 739
d369fd5b
KW
740unless (@Locale) {
741 print "1..$test_num\n";
742 exit;
743}
744
d369fd5b
KW
745
746setlocale(&POSIX::LC_ALL, "C");
747
73fc293b
KW
748my %posixes;
749
284102e8 750my %Problem;
2a680da6 751my %Okay;
852b6749 752my %Known_bad_locale; # Failed test for a locale known to be bad
2a680da6 753my %Testing;
30032ef4 754my @Added_alpha; # Alphas that aren't in the C locale.
c08acc4c 755my %test_names;
284102e8 756
705af3af 757sub disp_chars {
019bf7dd
KW
758 # This returns a display string denoting the input parameter @_, each
759 # entry of which is a single character in the range 0-255. The first part
760 # of the output is a string of the characters in @_ that are ASCII
761 # graphics, and hence unambiguously displayable. They are given by code
762 # point order. The second part is the remaining code points, the ordinals
763 # of which are each displayed as 2-digit hex. Blanks are inserted so as
764 # to keep anything from the first part looking like a 2-digit hex number.
765
766 no locale;
767 my @chars = sort { ord $a <=> ord $b } @_;
768 my $output = "";
019bf7dd
KW
769 my $range_start;
770 my $start_class;
771 push @chars, chr(258); # This sentinel simplifies the loop termination
772 # logic
773 foreach my $i (0 .. @chars - 1) {
774 my $char = $chars[$i];
775 my $range_end;
776 my $class;
777
778 # We avoid using [:posix:] classes, as these are being tested in this
779 # file. Each equivalence class below is for things that can appear in
780 # a range; those that can't be in a range have class -1. 0 for those
781 # which should be output in hex; and >0 for the other ranges
782 if ($char =~ /[A-Z]/) {
783 $class = 2;
784 }
785 elsif ($char =~ /[a-z]/) {
786 $class = 3;
787 }
788 elsif ($char =~ /[0-9]/) {
789 $class = 4;
790 }
78fbfaf5
KW
791 # Uncomment to get literal punctuation displayed instead of hex
792 #elsif ($char =~ /[[\]!"#\$\%&\'()*+,.\/:\\;<=>?\@\^_`{|}~-]/) {
793 # $class = -1; # Punct never appears in a range
794 #}
019bf7dd
KW
795 else {
796 $class = 0; # Output in hex
797 }
798
799 if (! defined $range_start) {
800 if ($class < 0) {
78fbfaf5 801 $output .= " " . $char;
019bf7dd
KW
802 }
803 else {
804 $range_start = ord $char;
805 $start_class = $class;
806 }
807 } # A range ends if not consecutive, or the class-type changes
808 elsif (ord $char != ($range_end = ord($chars[$i-1])) + 1
809 || $class != $start_class)
810 {
811
812 # Here, the current character is not in the range. This means the
813 # previous character must have been. Output the range up through
814 # that one.
815 my $range_length = $range_end - $range_start + 1;
816 if ($start_class > 0) {
817 $output .= " " . chr($range_start);
818 $output .= "-" . chr($range_end) if $range_length > 1;
819 }
820 else {
78fbfaf5
KW
821 $output .= sprintf(" %02X", $range_start);
822 $output .= sprintf("-%02X", $range_end) if $range_length > 1;
019bf7dd
KW
823 }
824
825 # Handle the new current character, as potentially beginning a new
826 # range
827 undef $range_start;
828 redo;
829 }
830 }
831
832 $output =~ s/^ //;
78fbfaf5 833 return $output;
019bf7dd
KW
834}
835
010b53a6
KW
836sub disp_str ($) {
837 my $string = shift;
838
839 # Displays the string unambiguously. ASCII printables are always output
840 # as-is, though perhaps separated by blanks from other characters. If
841 # entirely printable ASCII, just returns the string. Otherwise if valid
842 # UTF-8 it uses the character names for non-printable-ASCII. Otherwise it
843 # outputs hex for each non-ASCII-printable byte.
844
845 return $string if $string =~ / ^ [[:print:]]* $/xa;
846
847 my $result = "";
848 my $prev_was_punct = 1; # Beginning is considered punct
849 if (utf8::valid($string) && utf8::is_utf8($string)) {
850 use charnames ();
851 foreach my $char (split "", $string) {
852
853 # Keep punctuation adjacent to other characters; otherwise
854 # separate them with a blank
855 if ($char =~ /[[:punct:]]/a) {
856 $result .= $char;
857 $prev_was_punct = 1;
858 }
859 elsif ($char =~ /[[:print:]]/a) {
860 $result .= " " unless $prev_was_punct;
861 $result .= $char;
862 $prev_was_punct = 0;
863 }
864 else {
865 $result .= " " unless $prev_was_punct;
e82d2a11
KW
866 my $name = charnames::viacode(ord $char);
867 $result .= (defined $name) ? $name : ':unknown:';
010b53a6
KW
868 $prev_was_punct = 0;
869 }
870 }
871 }
872 else {
873 use bytes;
874 foreach my $char (split "", $string) {
875 if ($char =~ /[[:punct:]]/a) {
876 $result .= $char;
877 $prev_was_punct = 1;
878 }
879 elsif ($char =~ /[[:print:]]/a) {
880 $result .= " " unless $prev_was_punct;
881 $result .= $char;
882 $prev_was_punct = 0;
883 }
884 else {
885 $result .= " " unless $prev_was_punct;
886 $result .= sprintf("%02X", ord $char);
887 $prev_was_punct = 0;
888 }
889 }
890 }
891
892 return $result;
893}
894
30032ef4
KW
895sub report_result {
896 my ($Locale, $i, $pass_fail, $message) = @_;
15bbd6a2
KW
897 $message //= "";
898 $message = " ($message)" if $message;
852b6749
KW
899 if ($pass_fail) {
900 push @{$Okay{$i}}, $Locale;
901 }
902 else {
903 $Known_bad_locale{$i}{$Locale} = 1 if exists $known_bad_locales{$^O}
904 && $Locale =~ $known_bad_locales{$^O};
2a680da6 905 $Problem{$i}{$Locale} = 1;
46973bb2 906 debug "failed $i ($test_names{$i}) with locale '$Locale'$message\n";
2a680da6
JH
907 }
908}
909
7c844d17
KW
910sub report_multi_result {
911 my ($Locale, $i, $results_ref) = @_;
912
913 # $results_ref points to an array, each element of which is a character that was
914 # in error for this test numbered '$i'. If empty, the test passed
915
916 my $message = "";
917 if (@$results_ref) {
705af3af 918 $message = join " ", "for", disp_chars(@$results_ref);
7c844d17
KW
919 }
920 report_result($Locale, $i, @$results_ref == 0, $message);
921}
922
c4093d7d
KW
923my $first_locales_test_number = $final_without_setlocale + 1;
924my $locales_test_number;
925my $not_necessarily_a_problem_test_number;
6d5d702a 926my $first_casing_test_number;
c4093d7d
KW
927my %setlocale_failed; # List of locales that setlocale() didn't work on
928
73fc293b 929foreach my $Locale (@Locale) {
c4093d7d 930 $locales_test_number = $first_locales_test_number - 1;
46973bb2
KW
931 debug "\n";
932 debug "Locale = $Locale\n";
284102e8 933
a810e350 934 unless (setlocale(&POSIX::LC_ALL, $Locale)) {
c4093d7d 935 $setlocale_failed{$Locale} = $Locale;
284102e8 936 next;
8ebc5c01 937 }
8ebc5c01 938
31f05a37
KW
939 # We test UTF-8 locales only under ':not_characters'; It is easier to
940 # test them in other test files than here. Non- UTF-8 locales are tested
941 # only under plain 'use locale', as otherwise we would have to convert
942 # everything in them to Unicode.
66cbab2c 943
95eaa1bf
KW
944 my %UPPER = (); # All alpha X for which uc(X) == X and lc(X) != X
945 my %lower = (); # All alpha X for which lc(X) == X and uc(X) != X
946 my %BoThCaSe = (); # All alpha X for which uc(X) == lc(X) == X
66cbab2c 947
ab8b8bcc 948 my $is_utf8_locale = is_locale_utf8($Locale);
b9df08e4 949
46973bb2 950 debug "is utf8 locale? = $is_utf8_locale\n";
b9df08e4 951
68886934 952 debug "radix = " . disp_str(localeconv()->{decimal_point}) . "\n";
b9df08e4 953
66cbab2c
KW
954 if (! $is_utf8_locale) {
955 use locale;
fb844330
KW
956 @{$posixes{'word'}} = grep /\w/, map { chr } 0..255;
957 @{$posixes{'digit'}} = grep /\d/, map { chr } 0..255;
958 @{$posixes{'space'}} = grep /\s/, map { chr } 0..255;
959 @{$posixes{'alpha'}} = grep /[[:alpha:]]/, map {chr } 0..255;
960 @{$posixes{'alnum'}} = grep /[[:alnum:]]/, map {chr } 0..255;
961 @{$posixes{'ascii'}} = grep /[[:ascii:]]/, map {chr } 0..255;
962 @{$posixes{'blank'}} = grep /[[:blank:]]/, map {chr } 0..255;
963 @{$posixes{'cntrl'}} = grep /[[:cntrl:]]/, map {chr } 0..255;
964 @{$posixes{'graph'}} = grep /[[:graph:]]/, map {chr } 0..255;
965 @{$posixes{'lower'}} = grep /[[:lower:]]/, map {chr } 0..255;
966 @{$posixes{'print'}} = grep /[[:print:]]/, map {chr } 0..255;
98ed56e7 967 @{$posixes{'punct'}} = grep /[[:punct:]]/, map {chr } 0..255;
fb844330
KW
968 @{$posixes{'upper'}} = grep /[[:upper:]]/, map {chr } 0..255;
969 @{$posixes{'xdigit'}} = grep /[[:xdigit:]]/, map {chr } 0..255;
970 @{$posixes{'cased'}} = grep /[[:upper:]]/i, map {chr } 0..255;
e5272a46 971
71e5cbb3
KW
972 # Sieve the uppercase and the lowercase.
973
fb844330 974 for (@{$posixes{'word'}}) {
71e5cbb3
KW
975 if (/[^\d_]/) { # skip digits and the _
976 if (uc($_) eq $_) {
977 $UPPER{$_} = $_;
978 }
979 if (lc($_) eq $_) {
980 $lower{$_} = $_;
981 }
982 }
983 }
66cbab2c
KW
984 }
985 else {
986 use locale ':not_characters';
fb844330
KW
987 @{$posixes{'word'}} = grep /\w/, map { chr } 0..255;
988 @{$posixes{'digit'}} = grep /\d/, map { chr } 0..255;
989 @{$posixes{'space'}} = grep /\s/, map { chr } 0..255;
990 @{$posixes{'alpha'}} = grep /[[:alpha:]]/, map {chr } 0..255;
991 @{$posixes{'alnum'}} = grep /[[:alnum:]]/, map {chr } 0..255;
992 @{$posixes{'ascii'}} = grep /[[:ascii:]]/, map {chr } 0..255;
993 @{$posixes{'blank'}} = grep /[[:blank:]]/, map {chr } 0..255;
994 @{$posixes{'cntrl'}} = grep /[[:cntrl:]]/, map {chr } 0..255;
995 @{$posixes{'graph'}} = grep /[[:graph:]]/, map {chr } 0..255;
996 @{$posixes{'lower'}} = grep /[[:lower:]]/, map {chr } 0..255;
997 @{$posixes{'print'}} = grep /[[:print:]]/, map {chr } 0..255;
98ed56e7 998 @{$posixes{'punct'}} = grep /[[:punct:]]/, map {chr } 0..255;
fb844330
KW
999 @{$posixes{'upper'}} = grep /[[:upper:]]/, map {chr } 0..255;
1000 @{$posixes{'xdigit'}} = grep /[[:xdigit:]]/, map {chr } 0..255;
1001 @{$posixes{'cased'}} = grep /[[:upper:]]/i, map {chr } 0..255;
1002 for (@{$posixes{'word'}}) {
66cbab2c
KW
1003 if (/[^\d_]/) { # skip digits and the _
1004 if (uc($_) eq $_) {
1005 $UPPER{$_} = $_;
1006 }
1007 if (lc($_) eq $_) {
1008 $lower{$_} = $_;
1009 }
1010 }
1011 }
1012 }
a160ac48 1013
fb844330
KW
1014 # Ordered, where possible, in groups of "this is a subset of the next
1015 # one"
46973bb2
KW
1016 debug ":upper: = ", disp_chars(@{$posixes{'upper'}}), "\n";
1017 debug ":lower: = ", disp_chars(@{$posixes{'lower'}}), "\n";
1018 debug ":cased: = ", disp_chars(@{$posixes{'cased'}}), "\n";
1019 debug ":alpha: = ", disp_chars(@{$posixes{'alpha'}}), "\n";
1020 debug ":alnum: = ", disp_chars(@{$posixes{'alnum'}}), "\n";
1021 debug " w = ", disp_chars(@{$posixes{'word'}}), "\n";
1022 debug ":graph: = ", disp_chars(@{$posixes{'graph'}}), "\n";
1023 debug ":print: = ", disp_chars(@{$posixes{'print'}}), "\n";
1024 debug " d = ", disp_chars(@{$posixes{'digit'}}), "\n";
1025 debug ":xdigit: = ", disp_chars(@{$posixes{'xdigit'}}), "\n";
1026 debug ":blank: = ", disp_chars(@{$posixes{'blank'}}), "\n";
1027 debug " s = ", disp_chars(@{$posixes{'space'}}), "\n";
1028 debug ":punct: = ", disp_chars(@{$posixes{'punct'}}), "\n";
1029 debug ":cntrl: = ", disp_chars(@{$posixes{'cntrl'}}), "\n";
1030 debug ":ascii: = ", disp_chars(@{$posixes{'ascii'}}), "\n";
a160ac48 1031
284102e8 1032 foreach (keys %UPPER) {
a160ac48 1033
097ee67d 1034 $BoThCaSe{$_}++ if exists $lower{$_};
284102e8
JH
1035 }
1036 foreach (keys %lower) {
097ee67d 1037 $BoThCaSe{$_}++ if exists $UPPER{$_};
284102e8 1038 }
097ee67d 1039 foreach (keys %BoThCaSe) {
284102e8
JH
1040 delete $UPPER{$_};
1041 delete $lower{$_};
1042 }
1043
e5cc0528
KW
1044 my %Unassigned;
1045 foreach my $ord ( 0 .. 255 ) {
1046 $Unassigned{chr $ord} = 1;
1047 }
1048 foreach my $class (keys %posixes) {
1049 foreach my $char (@{$posixes{$class}}) {
1050 delete $Unassigned{$char};
1051 }
1052 }
1053
46973bb2
KW
1054 debug "UPPER = ", disp_chars(sort { ord $a <=> ord $b } keys %UPPER), "\n";
1055 debug "lower = ", disp_chars(sort { ord $a <=> ord $b } keys %lower), "\n";
1056 debug "BoThCaSe = ", disp_chars(sort { ord $a <=> ord $b } keys %BoThCaSe), "\n";
1057 debug "Unassigned = ", disp_chars(sort { ord $a <=> ord $b } keys %Unassigned), "\n";
284102e8 1058
baa71cfd 1059 my @failures;
3da38613 1060 my @fold_failures;
b9df08e4 1061 foreach my $x (sort { ord $a <=> ord $b } keys %UPPER) {
baa71cfd 1062 my $ok;
3da38613 1063 my $fold_ok;
baa71cfd
KW
1064 if ($is_utf8_locale) {
1065 use locale ':not_characters';
1066 $ok = $x =~ /[[:upper:]]/;
3da38613 1067 $fold_ok = $x =~ /[[:lower:]]/i;
baa71cfd
KW
1068 }
1069 else {
1070 use locale;
1071 $ok = $x =~ /[[:upper:]]/;
3da38613 1072 $fold_ok = $x =~ /[[:lower:]]/i;
baa71cfd
KW
1073 }
1074 push @failures, $x unless $ok;
3da38613 1075 push @fold_failures, $x unless $fold_ok;
baa71cfd 1076 }
baa71cfd 1077 $locales_test_number++;
6d5d702a 1078 $first_casing_test_number = $locales_test_number;
95eaa1bf 1079 $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/ matches all alpha X for which uc(X) == X and lc(X) != X';
7c844d17 1080 report_multi_result($Locale, $locales_test_number, \@failures);
6d5d702a 1081
3da38613 1082 $locales_test_number++;
6d5d702a 1083
95eaa1bf 1084 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/i matches all alpha X for which uc(X) == X and lc(X) != X';
7c844d17 1085 report_multi_result($Locale, $locales_test_number, \@fold_failures);
baa71cfd 1086
baa71cfd 1087 undef @failures;
3da38613 1088 undef @fold_failures;
baa71cfd 1089
b9df08e4 1090 foreach my $x (sort { ord $a <=> ord $b } keys %lower) {
baa71cfd 1091 my $ok;
3da38613 1092 my $fold_ok;
baa71cfd
KW
1093 if ($is_utf8_locale) {
1094 use locale ':not_characters';
1095 $ok = $x =~ /[[:lower:]]/;
3da38613 1096 $fold_ok = $x =~ /[[:upper:]]/i;
baa71cfd
KW
1097 }
1098 else {
1099 use locale;
1100 $ok = $x =~ /[[:lower:]]/;
3da38613 1101 $fold_ok = $x =~ /[[:upper:]]/i;
baa71cfd
KW
1102 }
1103 push @failures, $x unless $ok;
3da38613 1104 push @fold_failures, $x unless $fold_ok;
baa71cfd
KW
1105 }
1106
1107 $locales_test_number++;
95eaa1bf 1108 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/ matches all alpha X for which lc(X) == X and uc(X) != X';
7c844d17
KW
1109 report_multi_result($Locale, $locales_test_number, \@failures);
1110
3da38613 1111 $locales_test_number++;
95eaa1bf 1112 $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/i matches all alpha X for which lc(X) == X and uc(X) != X';
7c844d17 1113 report_multi_result($Locale, $locales_test_number, \@fold_failures);
baa71cfd 1114
9445c837
KW
1115 { # Find the alphabetic characters that are not considered alphabetics
1116 # in the default (C) locale.
8ebc5c01 1117
284102e8 1118 no locale;
71e5cbb3 1119
30032ef4 1120 @Added_alpha = ();
5e7a1028 1121 for (keys %UPPER, keys %lower, keys %BoThCaSe) {
30032ef4 1122 push(@Added_alpha, $_) if (/\W/);
284102e8 1123 }
8ebc5c01 1124 }
8ebc5c01 1125
b9df08e4 1126 @Added_alpha = sort { ord $a <=> ord $b } @Added_alpha;
8ebc5c01 1127
46973bb2 1128 debug "Added_alpha = ", disp_chars(@Added_alpha), "\n";
8ebc5c01 1129
db31898d 1130 # Cross-check the whole 8-bit character set.
8ebc5c01 1131
db31898d
KW
1132 ++$locales_test_number;
1133 my @f;
1134 $test_names{$locales_test_number} = 'Verify that \w and [:word:] are identical';
1135 for (map { chr } 0..255) {
1136 if ($is_utf8_locale) {
1137 use locale ':not_characters';
1138 push @f, $_ unless /[[:word:]]/ == /\w/;
1139 }
1140 else {
1141 push @f, $_ unless /[[:word:]]/ == /\w/;
1142 }
1143 }
1144 report_multi_result($Locale, $locales_test_number, \@f);
8ebc5c01 1145
db31898d
KW
1146 ++$locales_test_number;
1147 undef @f;
1148 $test_names{$locales_test_number} = 'Verify that \d and [:digit:] are identical';
1149 for (map { chr } 0..255) {
1150 if ($is_utf8_locale) {
1151 use locale ':not_characters';
1152 push @f, $_ unless /[[:digit:]]/ == /\d/;
1153 }
1154 else {
1155 push @f, $_ unless /[[:digit:]]/ == /\d/;
1156 }
1157 }
1158 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1159
db31898d
KW
1160 ++$locales_test_number;
1161 undef @f;
1162 $test_names{$locales_test_number} = 'Verify that \s and [:space:] are identical';
1163 for (map { chr } 0..255) {
1164 if ($is_utf8_locale) {
1165 use locale ':not_characters';
1166 push @f, $_ unless /[[:space:]]/ == /\s/;
1167 }
1168 else {
1169 push @f, $_ unless /[[:space:]]/ == /\s/;
1170 }
1171 }
1172 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1173
db31898d
KW
1174 ++$locales_test_number;
1175 undef @f;
1176 $test_names{$locales_test_number} = 'Verify that [:posix:] and [:^posix:] are mutually exclusive';
1177 for (map { chr } 0..255) {
1178 if ($is_utf8_locale) {
1179 use locale ':not_characters';
1180 push @f, $_ unless (/[[:alpha:]]/ xor /[[:^alpha:]]/) ||
1181 (/[[:alnum:]]/ xor /[[:^alnum:]]/) ||
1182 (/[[:ascii:]]/ xor /[[:^ascii:]]/) ||
1183 (/[[:blank:]]/ xor /[[:^blank:]]/) ||
1184 (/[[:cntrl:]]/ xor /[[:^cntrl:]]/) ||
1185 (/[[:digit:]]/ xor /[[:^digit:]]/) ||
1186 (/[[:graph:]]/ xor /[[:^graph:]]/) ||
1187 (/[[:lower:]]/ xor /[[:^lower:]]/) ||
1188 (/[[:print:]]/ xor /[[:^print:]]/) ||
1189 (/[[:space:]]/ xor /[[:^space:]]/) ||
1190 (/[[:upper:]]/ xor /[[:^upper:]]/) ||
1191 (/[[:word:]]/ xor /[[:^word:]]/) ||
1192 (/[[:xdigit:]]/ xor /[[:^xdigit:]]/) ||
1193
1194 # effectively is what [:cased:] would be if it existed.
1195 (/[[:upper:]]/i xor /[[:^upper:]]/i);
1196 }
1197 else {
1198 push @f, $_ unless (/[[:alpha:]]/ xor /[[:^alpha:]]/) ||
1199 (/[[:alnum:]]/ xor /[[:^alnum:]]/) ||
1200 (/[[:ascii:]]/ xor /[[:^ascii:]]/) ||
1201 (/[[:blank:]]/ xor /[[:^blank:]]/) ||
1202 (/[[:cntrl:]]/ xor /[[:^cntrl:]]/) ||
1203 (/[[:digit:]]/ xor /[[:^digit:]]/) ||
1204 (/[[:graph:]]/ xor /[[:^graph:]]/) ||
1205 (/[[:lower:]]/ xor /[[:^lower:]]/) ||
1206 (/[[:print:]]/ xor /[[:^print:]]/) ||
1207 (/[[:space:]]/ xor /[[:^space:]]/) ||
1208 (/[[:upper:]]/ xor /[[:^upper:]]/) ||
1209 (/[[:word:]]/ xor /[[:^word:]]/) ||
1210 (/[[:xdigit:]]/ xor /[[:^xdigit:]]/) ||
1211 (/[[:upper:]]/i xor /[[:^upper:]]/i);
1212 }
1213 }
1214 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1215
db31898d
KW
1216 # The rules for the relationships are given in:
1217 # http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap07.html
32e8aa3f 1218
660c7bcc
KW
1219
1220 ++$locales_test_number;
1221 undef @f;
1222 $test_names{$locales_test_number} = 'Verify that [:lower:] contains at least a-z';
1223 for ('a' .. 'z') {
1224 if ($is_utf8_locale) {
1225 use locale ':not_characters';
1226 push @f, $_ unless /[[:lower:]]/;
1227 }
1228 else {
1229 push @f, $_ unless /[[:lower:]]/;
1230 }
1231 }
1232 report_multi_result($Locale, $locales_test_number, \@f);
1233
db31898d
KW
1234 ++$locales_test_number;
1235 undef @f;
1236 $test_names{$locales_test_number} = 'Verify that [:lower:] is a subset of [:alpha:]';
1237 for (map { chr } 0..255) {
1238 if ($is_utf8_locale) {
1239 use locale ':not_characters';
1240 push @f, $_ if /[[:lower:]]/ and ! /[[:alpha:]]/;
1241 }
1242 else {
1243 push @f, $_ if /[[:lower:]]/ and ! /[[:alpha:]]/;
1244 }
1245 }
1246 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1247
db31898d
KW
1248 ++$locales_test_number;
1249 undef @f;
660c7bcc
KW
1250 $test_names{$locales_test_number} = 'Verify that [:upper:] contains at least A-Z';
1251 for ('A' .. 'Z') {
1252 if ($is_utf8_locale) {
1253 use locale ':not_characters';
1254 push @f, $_ unless /[[:upper:]]/;
1255 }
1256 else {
1257 push @f, $_ unless /[[:upper:]]/;
1258 }
1259 }
1260 report_multi_result($Locale, $locales_test_number, \@f);
1261
1262 ++$locales_test_number;
1263 undef @f;
db31898d
KW
1264 $test_names{$locales_test_number} = 'Verify that [:upper:] is a subset of [:alpha:]';
1265 for (map { chr } 0..255) {
1266 if ($is_utf8_locale) {
1267 use locale ':not_characters';
1268 push @f, $_ if /[[:upper:]]/ and ! /[[:alpha:]]/;
1269 }
1270 else {
1271 push @f, $_ if /[[:upper:]]/ and ! /[[:alpha:]]/;
1272 }
1273 }
1274 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1275
db31898d
KW
1276 ++$locales_test_number;
1277 undef @f;
1278 $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/i is a subset of [:alpha:]';
1279 for (map { chr } 0..255) {
1280 if ($is_utf8_locale) {
1281 use locale ':not_characters';
1282 push @f, $_ if /[[:lower:]]/i and ! /[[:alpha:]]/;
1283 }
1284 else {
1285 push @f, $_ if /[[:lower:]]/i and ! /[[:alpha:]]/;
1286 }
1287 }
1288 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1289
db31898d
KW
1290 ++$locales_test_number;
1291 undef @f;
1292 $test_names{$locales_test_number} = 'Verify that [:alpha:] is a subset of [:alnum:]';
1293 for (map { chr } 0..255) {
1294 if ($is_utf8_locale) {
1295 use locale ':not_characters';
1296 push @f, $_ if /[[:alpha:]]/ and ! /[[:alnum:]]/;
1297 }
1298 else {
1299 push @f, $_ if /[[:alpha:]]/ and ! /[[:alnum:]]/;
1300 }
1301 }
1302 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1303
db31898d
KW
1304 ++$locales_test_number;
1305 undef @f;
660c7bcc
KW
1306 $test_names{$locales_test_number} = 'Verify that [:digit:] contains at least 0-9';
1307 for ('0' .. '9') {
1308 if ($is_utf8_locale) {
1309 use locale ':not_characters';
1310 push @f, $_ unless /[[:digit:]]/;
1311 }
1312 else {
1313 push @f, $_ unless /[[:digit:]]/;
1314 }
1315 }
1316 report_multi_result($Locale, $locales_test_number, \@f);
1317
1318 ++$locales_test_number;
1319 undef @f;
db31898d
KW
1320 $test_names{$locales_test_number} = 'Verify that [:digit:] is a subset of [:alnum:]';
1321 for (map { chr } 0..255) {
1322 if ($is_utf8_locale) {
1323 use locale ':not_characters';
1324 push @f, $_ if /[[:digit:]]/ and ! /[[:alnum:]]/;
1325 }
1326 else {
1327 push @f, $_ if /[[:digit:]]/ and ! /[[:alnum:]]/;
1328 }
1329 }
1330 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1331
db31898d
KW
1332 ++$locales_test_number;
1333 undef @f;
aac995d2 1334 $test_names{$locales_test_number} = 'Verify that [:digit:] matches either 10 or 20 code points';
fb844330 1335 report_result($Locale, $locales_test_number, @{$posixes{'digit'}} == 10 || @{$posixes{'digit'}} == 20);
aac995d2
KW
1336
1337 ++$locales_test_number;
1338 undef @f;
660c7bcc
KW
1339 $test_names{$locales_test_number} = 'Verify that if there is a second set of digits in [:digit:], they are consecutive';
1340 if (@{$posixes{'digit'}} == 20) {
1341 my $previous_ord;
fc81f5f2 1342 for (map { chr } 0..255) {
660c7bcc
KW
1343 next unless /[[:digit:]]/;
1344 next if /[0-9]/;
1345 if (defined $previous_ord) {
1346 if ($is_utf8_locale) {
1347 use locale ':not_characters';
1348 push @f, $_ if ord $_ != $previous_ord + 1;
1349 }
1350 else {
1351 push @f, $_ if ord $_ != $previous_ord + 1;
1352 }
fc81f5f2 1353 }
660c7bcc 1354 $previous_ord = ord $_;
db31898d
KW
1355 }
1356 }
1357 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1358
db31898d
KW
1359 ++$locales_test_number;
1360 undef @f;
d458c02c
KW
1361 my @xdigit_digits; # :digit: & :xdigit:
1362 $test_names{$locales_test_number} = 'Verify that [:xdigit:] contains one or two blocks of 10 consecutive [:digit:] chars';
db31898d
KW
1363 for (map { chr } 0..255) {
1364 if ($is_utf8_locale) {
1365 use locale ':not_characters';
d458c02c
KW
1366 # For utf8 locales, we actually use a stricter test: that :digit:
1367 # is a subset of :xdigit:, as we know that only 0-9 should match
1368 push @f, $_ if /[[:digit:]]/ and ! /[[:xdigit:]]/;
db31898d
KW
1369 }
1370 else {
d458c02c 1371 push @xdigit_digits, $_ if /[[:digit:]]/ and /[[:xdigit:]]/;
db31898d
KW
1372 }
1373 }
d458c02c
KW
1374 if (! $is_utf8_locale) {
1375
1376 # For non-utf8 locales, @xdigit_digits is a list of the characters
1377 # that are both :xdigit: and :digit:. Because :digit: is stored in
1378 # increasing code point order (unless the tests above failed),
1379 # @xdigit_digits is as well. There should be exactly 10 or
1380 # 20 of these.
1381 if (@xdigit_digits != 10 && @xdigit_digits != 20) {
1382 @f = @xdigit_digits;
1383 }
1384 else {
1385
1386 # Look for contiguity in the series, adding any wrong ones to @f
1387 my @temp = @xdigit_digits;
1388 while (@temp > 1) {
1389 push @f, $temp[1] if ($temp[0] != $temp[1] - 1)
1390
1391 # Skip this test for the 0th character of
1392 # the second block of 10, as it won't be
1393 # contiguous with the previous block
1394 && (! defined $xdigit_digits[10]
1395 || $temp[1] != $xdigit_digits[10]);
1396 shift @temp;
1397 }
1398 }
1399 }
1400
db31898d 1401 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1402
660c7bcc
KW
1403 ++$locales_test_number;
1404 undef @f;
1405 $test_names{$locales_test_number} = 'Verify that [:xdigit:] contains at least A-F, a-f';
1406 for ('A' .. 'F', 'a' .. 'f') {
1407 if ($is_utf8_locale) {
1408 use locale ':not_characters';
1409 push @f, $_ unless /[[:xdigit:]]/;
1410 }
1411 else {
1412 push @f, $_ unless /[[:xdigit:]]/;
1413 }
1414 }
1415 report_multi_result($Locale, $locales_test_number, \@f);
1416
1417 ++$locales_test_number;
1418 undef @f;
1419 $test_names{$locales_test_number} = 'Verify that any additional members of [:xdigit:], are in groups of 6 consecutive code points';
1420 my $previous_ord;
1421 my $count = 0;
d458c02c
KW
1422 for my $chr (map { chr } 0..255) {
1423 next unless $chr =~ /[[:xdigit:]]/;
1424 if ($is_utf8_locale) {
1425 next if $chr =~ /[[:digit:]]/;
1426 }
1427 else {
1428 next if grep { $chr eq $_ } @xdigit_digits;
1429 }
1430 next if $chr =~ /[A-Fa-f]/;
660c7bcc
KW
1431 if (defined $previous_ord) {
1432 if ($is_utf8_locale) {
1433 use locale ':not_characters';
d458c02c 1434 push @f, $chr if ord $chr != $previous_ord + 1;
660c7bcc
KW
1435 }
1436 else {
d458c02c 1437 push @f, $chr if ord $chr != $previous_ord + 1;
660c7bcc
KW
1438 }
1439 }
1440 $count++;
1441 if ($count == 6) {
1442 undef $previous_ord;
1443 }
1444 else {
d458c02c 1445 $previous_ord = ord $chr;
660c7bcc
KW
1446 }
1447 }
1448 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1449
db31898d
KW
1450 ++$locales_test_number;
1451 undef @f;
1452 $test_names{$locales_test_number} = 'Verify that [:xdigit:] is a subset of [:graph:]';
1453 for (map { chr } 0..255) {
1454 if ($is_utf8_locale) {
1455 use locale ':not_characters';
1456 push @f, $_ if /[[:xdigit:]]/ and ! /[[:graph:]]/;
1457 }
1458 else {
1459 push @f, $_ if /[[:xdigit:]]/ and ! /[[:graph:]]/;
1460 }
1461 }
1462 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1463
660c7bcc
KW
1464 # Note that xdigit doesn't have to be a subset of alnum
1465
db31898d
KW
1466 ++$locales_test_number;
1467 undef @f;
1468 $test_names{$locales_test_number} = 'Verify that [:punct:] is a subset of [:graph:]';
1469 for (map { chr } 0..255) {
1470 if ($is_utf8_locale) {
1471 use locale ':not_characters';
1472 push @f, $_ if /[[:punct:]]/ and ! /[[:graph:]]/;
1473 }
1474 else {
1475 push @f, $_ if /[[:punct:]]/ and ! /[[:graph:]]/;
1476 }
1477 }
1478 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1479
db31898d
KW
1480 ++$locales_test_number;
1481 undef @f;
660c7bcc
KW
1482 $test_names{$locales_test_number} = 'Verify that the space character is not in [:graph:]';
1483 if ($is_utf8_locale) {
1484 use locale ':not_characters';
1485 push @f, " " if " " =~ /[[:graph:]]/;
1486 }
1487 else {
1488 push @f, " " if " " =~ /[[:graph:]]/;
1489 }
1490 report_multi_result($Locale, $locales_test_number, \@f);
1491
1492 ++$locales_test_number;
1493 undef @f;
1494 $test_names{$locales_test_number} = 'Verify that [:space:] contains at least [\f\n\r\t\cK ]';
1495 for (' ', "\f", "\n", "\r", "\t", "\cK") {
1496 if ($is_utf8_locale) {
1497 use locale ':not_characters';
1498 push @f, $_ unless /[[:space:]]/;
1499 }
1500 else {
1501 push @f, $_ unless /[[:space:]]/;
1502 }
1503 }
1504 report_multi_result($Locale, $locales_test_number, \@f);
1505
1506 ++$locales_test_number;
1507 undef @f;
1508 $test_names{$locales_test_number} = 'Verify that [:blank:] contains at least [\t ]';
1509 for (' ', "\t") {
1510 if ($is_utf8_locale) {
1511 use locale ':not_characters';
1512 push @f, $_ unless /[[:blank:]]/;
1513 }
1514 else {
1515 push @f, $_ unless /[[:blank:]]/;
1516 }
1517 }
1518 report_multi_result($Locale, $locales_test_number, \@f);
1519
1520 ++$locales_test_number;
1521 undef @f;
db31898d
KW
1522 $test_names{$locales_test_number} = 'Verify that [:blank:] is a subset of [:space:]';
1523 for (map { chr } 0..255) {
1524 if ($is_utf8_locale) {
1525 use locale ':not_characters';
1526 push @f, $_ if /[[:blank:]]/ and ! /[[:space:]]/;
1527 }
1528 else {
1529 push @f, $_ if /[[:blank:]]/ and ! /[[:space:]]/;
1530 }
1531 }
1532 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1533
db31898d
KW
1534 ++$locales_test_number;
1535 undef @f;
1536 $test_names{$locales_test_number} = 'Verify that [:graph:] is a subset of [:print:]';
1537 for (map { chr } 0..255) {
1538 if ($is_utf8_locale) {
1539 use locale ':not_characters';
1540 push @f, $_ if /[[:graph:]]/ and ! /[[:print:]]/;
1541 }
1542 else {
1543 push @f, $_ if /[[:graph:]]/ and ! /[[:print:]]/;
1544 }
1545 }
1546 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1547
db31898d
KW
1548 ++$locales_test_number;
1549 undef @f;
660c7bcc
KW
1550 $test_names{$locales_test_number} = 'Verify that the space character is in [:print:]';
1551 if ($is_utf8_locale) {
1552 use locale ':not_characters';
1553 push @f, " " if " " !~ /[[:print:]]/;
1554 }
1555 else {
1556 push @f, " " if " " !~ /[[:print:]]/;
1557 }
1558 report_multi_result($Locale, $locales_test_number, \@f);
1559
1560 ++$locales_test_number;
1561 undef @f;
db31898d
KW
1562 $test_names{$locales_test_number} = 'Verify that isn\'t both [:cntrl:] and [:print:]';
1563 for (map { chr } 0..255) {
1564 if ($is_utf8_locale) {
1565 use locale ':not_characters';
1566 push @f, $_ if (/[[:print:]]/ and /[[:cntrl:]]/);
1567 }
1568 else {
1569 push @f, $_ if (/[[:print:]]/ and /[[:cntrl:]]/);
1570 }
1571 }
1572 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1573
db31898d
KW
1574 ++$locales_test_number;
1575 undef @f;
660c7bcc
KW
1576 $test_names{$locales_test_number} = 'Verify that isn\'t both [:alpha:] and [:digit:]';
1577 for (map { chr } 0..255) {
1578 if ($is_utf8_locale) {
1579 use locale ':not_characters';
1580 push @f, $_ if /[[:alpha:]]/ and /[[:digit:]]/;
1581 }
1582 else {
1583 push @f, $_ if /[[:alpha:]]/ and /[[:digit:]]/;
1584 }
1585 }
1586 report_multi_result($Locale, $locales_test_number, \@f);
1587
1588 ++$locales_test_number;
1589 undef @f;
db31898d
KW
1590 $test_names{$locales_test_number} = 'Verify that isn\'t both [:alnum:] and [:punct:]';
1591 for (map { chr } 0..255) {
1592 if ($is_utf8_locale) {
1593 use locale ':not_characters';
1594 push @f, $_ if /[[:alnum:]]/ and /[[:punct:]]/;
1595 }
1596 else {
1597 push @f, $_ if /[[:alnum:]]/ and /[[:punct:]]/;
1598 }
1599 }
1600 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1601
db31898d
KW
1602 ++$locales_test_number;
1603 undef @f;
1604 $test_names{$locales_test_number} = 'Verify that isn\'t both [:xdigit:] and [:punct:]';
1605 for (map { chr } 0..255) {
1606 if ($is_utf8_locale) {
1607 use locale ':not_characters';
1608 push @f, $_ if (/[[:punct:]]/ and /[[:xdigit:]]/);
1609 }
1610 else {
1611 push @f, $_ if (/[[:punct:]]/ and /[[:xdigit:]]/);
1612 }
1613 }
1614 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1615
db31898d
KW
1616 ++$locales_test_number;
1617 undef @f;
1618 $test_names{$locales_test_number} = 'Verify that isn\'t both [:graph:] and [:space:]';
1619 for (map { chr } 0..255) {
1620 if ($is_utf8_locale) {
1621 use locale ':not_characters';
1622 push @f, $_ if (/[[:graph:]]/ and /[[:space:]]/);
1623 }
1624 else {
1625 push @f, $_ if (/[[:graph:]]/ and /[[:space:]]/);
1626 }
1627 }
1628 report_multi_result($Locale, $locales_test_number, \@f);
32e8aa3f 1629
7493b8f2 1630 foreach ($first_casing_test_number..$locales_test_number) {
5b1f18a7 1631 $problematical_tests{$_} = 1;
7493b8f2
KW
1632 }
1633
32e8aa3f 1634
db31898d
KW
1635 # Test for read-only scalars' locale vs non-locale comparisons.
1636
1637 {
1638 no locale;
1639 my $ok;
1640 $a = "qwerty";
1641 if ($is_utf8_locale) {
1642 use locale ':not_characters';
1643 $ok = ($a cmp "qwerty") == 0;
1644 }
1645 else {
1646 use locale;
1647 $ok = ($a cmp "qwerty") == 0;
1648 }
1649 report_result($Locale, ++$locales_test_number, $ok);
1650 $test_names{$locales_test_number} = 'Verify that cmp works with a read-only scalar; no- vs locale';
1651 }
8ebc5c01 1652
db31898d
KW
1653 {
1654 my ($from, $to, $lesser, $greater,
1655 @test, %test, $test, $yes, $no, $sign);
284102e8 1656
db31898d
KW
1657 ++$locales_test_number;
1658 $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work';
1659 $not_necessarily_a_problem_test_number = $locales_test_number;
1660 for (0..9) {
1661 # Select a slice.
fb844330
KW
1662 $from = int(($_*@{$posixes{'word'}})/10);
1663 $to = $from + int(@{$posixes{'word'}}/10);
1664 $to = $#{$posixes{'word'}} if ($to > $#{$posixes{'word'}});
1665 $lesser = join('', @{$posixes{'word'}}[$from..$to]);
db31898d
KW
1666 # Select a slice one character on.
1667 $from++; $to++;
fb844330
KW
1668 $to = $#{$posixes{'word'}} if ($to > $#{$posixes{'word'}});
1669 $greater = join('', @{$posixes{'word'}}[$from..$to]);
66cbab2c
KW
1670 if ($is_utf8_locale) {
1671 use locale ':not_characters';
db31898d
KW
1672 ($yes, $no, $sign) = ($lesser lt $greater
1673 ? (" ", "not ", 1)
1674 : ("not ", " ", -1));
66cbab2c
KW
1675 }
1676 else {
1677 use locale;
db31898d
KW
1678 ($yes, $no, $sign) = ($lesser lt $greater
1679 ? (" ", "not ", 1)
1680 : ("not ", " ", -1));
66cbab2c 1681 }
db31898d
KW
1682 # all these tests should FAIL (return 0). Exact lt or gt cannot
1683 # be tested because in some locales, say, eacute and E may test
1684 # equal.
1685 @test =
1686 (
1687 $no.' ($lesser le $greater)', # 1
1688 'not ($lesser ne $greater)', # 2
1689 ' ($lesser eq $greater)', # 3
1690 $yes.' ($lesser ge $greater)', # 4
1691 $yes.' ($lesser ge $greater)', # 5
1692 $yes.' ($greater le $lesser )', # 7
1693 'not ($greater ne $lesser )', # 8
1694 ' ($greater eq $lesser )', # 9
1695 $no.' ($greater ge $lesser )', # 10
1696 'not (($lesser cmp $greater) == -($sign))' # 11
1697 );
1698 @test{@test} = 0 x @test;
1699 $test = 0;
1700 for my $ti (@test) {
66cbab2c
KW
1701 if ($is_utf8_locale) {
1702 use locale ':not_characters';
db31898d 1703 $test{$ti} = eval $ti;
66cbab2c
KW
1704 }
1705 else {
db31898d
KW
1706 # Already in 'use locale';
1707 $test{$ti} = eval $ti;
66cbab2c 1708 }
db31898d
KW
1709 $test ||= $test{$ti}
1710 }
1711 report_result($Locale, $locales_test_number, $test == 0);
1712 if ($test) {
46973bb2
KW
1713 debug "lesser = '$lesser'\n";
1714 debug "greater = '$greater'\n";
1715 debug "lesser cmp greater = ",
db31898d 1716 $lesser cmp $greater, "\n";
46973bb2 1717 debug "greater cmp lesser = ",
db31898d 1718 $greater cmp $lesser, "\n";
46973bb2 1719 debug "(greater) from = $from, to = $to\n";
db31898d
KW
1720 for my $ti (@test) {
1721 debugf("# %-40s %-4s", $ti,
1722 $test{$ti} ? 'FAIL' : 'ok');
1723 if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) {
1724 debugf("(%s == %4d)", $1, eval $1);
66cbab2c 1725 }
46973bb2 1726 debugf("\n#");
db31898d
KW
1727 }
1728
1729 last;
1730 }
1731 }
1732 }
c4093d7d 1733
66cbab2c
KW
1734 my $ok1;
1735 my $ok2;
1736 my $ok3;
1737 my $ok4;
1738 my $ok5;
1739 my $ok6;
1740 my $ok7;
1741 my $ok8;
1742 my $ok9;
1743 my $ok10;
1744 my $ok11;
1745 my $ok12;
1746 my $ok13;
1500bd91 1747 my $ok14;
2c6ee1a7 1748 my $ok14_5;
28acfe03
KW
1749 my $ok15;
1750 my $ok16;
e46375fa
KW
1751 my $ok17;
1752 my $ok18;
f406a445 1753 my $ok19;
9717af6d 1754 my $ok20;
8ee4b769 1755 my $ok21;
66cbab2c
KW
1756
1757 my $c;
1758 my $d;
1759 my $e;
1760 my $f;
1761 my $g;
e46375fa
KW
1762 my $h;
1763 my $i;
1764 my $j;
66cbab2c
KW
1765
1766 if (! $is_utf8_locale) {
71e5cbb3 1767 use locale;
6be75cd7 1768
71e5cbb3 1769 my ($x, $y) = (1.23, 1.23);
6be75cd7 1770
71e5cbb3
KW
1771 $a = "$x";
1772 printf ''; # printf used to reset locale to "C"
1773 $b = "$y";
1774 $ok1 = $a eq $b;
6be75cd7 1775
71e5cbb3
KW
1776 $c = "$x";
1777 my $z = sprintf ''; # sprintf used to reset locale to "C"
1778 $d = "$y";
1779 $ok2 = $c eq $d;
1780 {
66cbab2c 1781
71e5cbb3
KW
1782 use warnings;
1783 my $w = 0;
1784 local $SIG{__WARN__} =
1785 sub {
1786 print "# @_\n";
1787 $w++;
1788 };
6be75cd7 1789
71e5cbb3
KW
1790 # The == (among other ops) used to warn for locales
1791 # that had something else than "." as the radix character.
6be75cd7 1792
71e5cbb3
KW
1793 $ok3 = $c == 1.23;
1794 $ok4 = $c == $x;
1795 $ok5 = $c == $d;
1796 {
1797 no locale;
66cbab2c 1798
b79536ea 1799 $e = "$x";
71e5cbb3
KW
1800
1801 $ok6 = $e == 1.23;
1802 $ok7 = $e == $x;
1803 $ok8 = $e == $c;
1804 }
66cbab2c 1805
71e5cbb3
KW
1806 $f = "1.23";
1807 $g = 2.34;
e46375fa
KW
1808 $h = 1.5;
1809 $i = 1.25;
1810 $j = "$h:$i";
66cbab2c 1811
71e5cbb3
KW
1812 $ok9 = $f == 1.23;
1813 $ok10 = $f == $x;
1814 $ok11 = $f == $c;
1815 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1816 $ok13 = $w == 0;
2c6ee1a7 1817 $ok14 = $ok14_5 = $ok15 = $ok16 = 1; # Skip for non-utf8 locales
71e5cbb3 1818 }
e46375fa
KW
1819 {
1820 no locale;
1821 $ok17 = "1.5:1.25" eq sprintf("%g:%g", $h, $i);
1822 }
1823 $ok18 = $j eq sprintf("%g:%g", $h, $i);
66cbab2c
KW
1824 }
1825 else {
1826 use locale ':not_characters';
1827
1828 my ($x, $y) = (1.23, 1.23);
1829 $a = "$x";
1830 printf ''; # printf used to reset locale to "C"
1831 $b = "$y";
1832 $ok1 = $a eq $b;
1833
1834 $c = "$x";
1835 my $z = sprintf ''; # sprintf used to reset locale to "C"
1836 $d = "$y";
1837 $ok2 = $c eq $d;
1838 {
1839 use warnings;
1840 my $w = 0;
1841 local $SIG{__WARN__} =
1842 sub {
1843 print "# @_\n";
1844 $w++;
1845 };
1846 $ok3 = $c == 1.23;
1847 $ok4 = $c == $x;
1848 $ok5 = $c == $d;
1849 {
1850 no locale;
b79536ea 1851 $e = "$x";
66cbab2c
KW
1852
1853 $ok6 = $e == 1.23;
1854 $ok7 = $e == $x;
1855 $ok8 = $e == $c;
1856 }
1857
1858 $f = "1.23";
1859 $g = 2.34;
e46375fa
KW
1860 $h = 1.5;
1861 $i = 1.25;
1862 $j = "$h:$i";
66cbab2c
KW
1863
1864 $ok9 = $f == 1.23;
1865 $ok10 = $f == $x;
1866 $ok11 = $f == $c;
1867 $ok12 = abs(($f + $g) - 3.57) < 0.01;
1868 $ok13 = $w == 0;
1500bd91
KW
1869
1870 # Look for non-ASCII error messages, and verify that the first
5320b60d 1871 # such is in UTF-8 (the others almost certainly will be like the
5af2752a 1872 # first). This is only done if the current locale has LC_MESSAGES
1500bd91 1873 $ok14 = 1;
2c6ee1a7 1874 $ok14_5 = 1;
ef9d5242
KW
1875 if ( locales_enabled('LC_MESSAGES')
1876 && setlocale(&POSIX::LC_MESSAGES, $Locale))
1877 {
9bc33472
KW
1878 foreach my $err (keys %!) {
1879 use Errno;
1880 $! = eval "&Errno::$err"; # Convert to strerror() output
1881 my $strerror = "$!";
1882 if ("$strerror" =~ /\P{ASCII}/) {
1883 $ok14 = utf8::is_utf8($strerror);
1884 no locale;
1885 $ok14_5 = "$!" !~ /\P{ASCII}/;
1886 last;
1887 }
1500bd91
KW
1888 }
1889 }
28acfe03
KW
1890
1891 # Similarly, we verify that a non-ASCII radix is in UTF-8. This
1892 # also catches if there is a disparity between sprintf and
1893 # stringification.
1894
1895 my $string_g = "$g";
2ef12107 1896 my $sprintf_g = sprintf("%g", $g);
28acfe03 1897
2ef12107
KW
1898 $ok15 = $string_g =~ / ^ \p{ASCII}+ $ /x || utf8::is_utf8($string_g);
1899 $ok16 = $sprintf_g eq $string_g;
66cbab2c 1900 }
e46375fa
KW
1901 {
1902 no locale;
1903 $ok17 = "1.5:1.25" eq sprintf("%g:%g", $h, $i);
1904 }
1905 $ok18 = $j eq sprintf("%g:%g", $h, $i);
57ba90dd
KW
1906 }
1907
d6bfff6d
KW
1908 $ok19 = $ok20 = 1;
1909 if (setlocale(&POSIX::LC_TIME, $Locale)) { # These tests aren't affected by
1910 # :not_characters
57ba90dd
KW
1911 my @times = CORE::localtime();
1912
1913 use locale;
1914 $ok19 = POSIX::strftime("%p", @times) ne "%p"; # [perl #119425]
dc5a2383
KW
1915 my $date = POSIX::strftime("'%A' '%B' '%Z' '%p'", @times);
1916 debug("'Day' 'Month' 'TZ' 'am/pm' = ", disp_str($date));
57ba90dd
KW
1917
1918 # If there is any non-ascii, it better be UTF-8 in a UTF-8 locale, and
1919 # not UTF-8 if the locale isn't UTF-8.
1920 $ok20 = $date =~ / ^ \p{ASCII}+ $ /x
1921 || $is_utf8_locale == utf8::is_utf8($date);
66cbab2c
KW
1922 }
1923
8ee4b769 1924 $ok21 = 1;
ef9d5242 1925 if (locales_enabled('LC_MESSAGES')) {
a2917a7e
KW
1926 foreach my $err (keys %!) {
1927 no locale;
1928 use Errno;
1929 $! = eval "&Errno::$err"; # Convert to strerror() output
1930 my $strerror = "$!";
1931 if ("$strerror" =~ /\P{ASCII}/) {
1932 $ok21 = 0;
1933 last;
ef9d5242 1934 }
8ee4b769
KW
1935 }
1936 }
1937
30032ef4 1938 report_result($Locale, ++$locales_test_number, $ok1);
66cbab2c
KW
1939 $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results';
1940 my $first_a_test = $locales_test_number;
1941
46973bb2 1942 debug "$first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n";
66cbab2c 1943
30032ef4 1944 report_result($Locale, ++$locales_test_number, $ok2);
66cbab2c
KW
1945 $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results';
1946
1947 my $first_c_test = $locales_test_number;
1948
435847ca
JH
1949 $test_names{++$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant';
1950 if ($Config{usequadmath}) {
1951 print "# Skip: no locale radix with usequadmath ($test_names{$locales_test_number})\n";
1952 report_result($Locale, $locales_test_number, 1);
1953 } else {
1954 report_result($Locale, $locales_test_number, $ok3);
1955 $problematical_tests{$locales_test_number} = 1;
1956 }
6be75cd7 1957
435847ca
JH
1958 $test_names{++$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar';
1959 if ($Config{usequadmath}) {
1960 print "# Skip: no locale radix with usequadmath ($test_names{$locales_test_number})\n";
1961 report_result($Locale, $locales_test_number, 1);
1962 } else {
1963 report_result($Locale, $locales_test_number, $ok4);
1964 $problematical_tests{$locales_test_number} = 1;
1965 }
66cbab2c 1966
30032ef4 1967 report_result($Locale, ++$locales_test_number, $ok5);
71e5cbb3 1968 $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf';
b057411d 1969 $problematical_tests{$locales_test_number} = 1;
66cbab2c 1970
46973bb2 1971 debug "$first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n";
66cbab2c 1972
30032ef4 1973 report_result($Locale, ++$locales_test_number, $ok6);
b79536ea 1974 $test_names{$locales_test_number} = 'Verify that can assign stringified under inner no-locale block';
71e5cbb3 1975 my $first_e_test = $locales_test_number;
6be75cd7 1976
30032ef4 1977 report_result($Locale, ++$locales_test_number, $ok7);
71e5cbb3 1978 $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale';
66cbab2c 1979
435847ca
JH
1980 $test_names{++$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale';
1981 if ($Config{usequadmath}) {
1982 print "# Skip: no locale radix with usequadmath ($test_names{$locales_test_number})\n";
1983 report_result($Locale, $locales_test_number, 1);
1984 } else {
1985 report_result($Locale, $locales_test_number, $ok8);
1986 $problematical_tests{$locales_test_number} = 1;
1987 }
c4093d7d 1988
46973bb2 1989 debug "$first_e_test..$locales_test_number: \$e = $e, no locale\n";
2a680da6 1990
30032ef4 1991 report_result($Locale, ++$locales_test_number, $ok9);
71e5cbb3 1992 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant';
b057411d 1993 $problematical_tests{$locales_test_number} = 1;
71e5cbb3 1994 my $first_f_test = $locales_test_number;
6be75cd7 1995
30032ef4 1996 report_result($Locale, ++$locales_test_number, $ok10);
71e5cbb3 1997 $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar';
b057411d 1998 $problematical_tests{$locales_test_number} = 1;
66cbab2c 1999
435847ca
JH
2000 $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';
2001 if ($Config{usequadmath}) {
2002 print "# Skip: no locale radix with usequadmath ($test_names{$locales_test_number})\n";
2003 report_result($Locale, $locales_test_number, 1);
2004 } else {
2005 report_result($Locale, $locales_test_number, $ok11);
2006 $problematical_tests{$locales_test_number} = 1;
2007 }
906f284f 2008
30032ef4 2009 report_result($Locale, ++$locales_test_number, $ok12);
71e5cbb3 2010 $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';
b057411d 2011 $problematical_tests{$locales_test_number} = 1;
c4093d7d 2012
30032ef4 2013 report_result($Locale, ++$locales_test_number, $ok13);
71e5cbb3 2014 $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot';
b057411d 2015 $problematical_tests{$locales_test_number} = 1;
c4093d7d 2016
30032ef4 2017 report_result($Locale, ++$locales_test_number, $ok14);
5320b60d 2018 $test_names{$locales_test_number} = 'Verify that non-ASCII UTF-8 error messages are in UTF-8';
1500bd91 2019
2c6ee1a7
KW
2020 report_result($Locale, ++$locales_test_number, $ok14_5);
2021 $test_names{$locales_test_number} = '... and are ASCII outside "use locale"';
2022
30032ef4 2023 report_result($Locale, ++$locales_test_number, $ok15);
28acfe03
KW
2024 $test_names{$locales_test_number} = 'Verify that a number with a UTF-8 radix has a UTF-8 stringification';
2025
30032ef4 2026 report_result($Locale, ++$locales_test_number, $ok16);
28acfe03
KW
2027 $test_names{$locales_test_number} = 'Verify that a sprintf of a number with a UTF-8 radix yields UTF-8';
2028
e46375fa
KW
2029 report_result($Locale, ++$locales_test_number, $ok17);
2030 $test_names{$locales_test_number} = 'Verify that a sprintf of a number outside locale scope uses a dot radix';
2031
2032 report_result($Locale, ++$locales_test_number, $ok18);
2033 $test_names{$locales_test_number} = 'Verify that a sprintf of a number back within locale scope uses locale radix';
2034
f406a445
KW
2035 report_result($Locale, ++$locales_test_number, $ok19);
2036 $test_names{$locales_test_number} = 'Verify that strftime doesn\'t return "%p" in locales where %p is empty';
2037
9717af6d
KW
2038 report_result($Locale, ++$locales_test_number, $ok20);
2039 $test_names{$locales_test_number} = 'Verify that strftime returns date with UTF-8 flag appropriately set';
6a78954a
KW
2040 $problematical_tests{$locales_test_number} = 1; # This is broken in
2041 # OS X 10.9.3
9717af6d 2042
8ee4b769
KW
2043 report_result($Locale, ++$locales_test_number, $ok21);
2044 $test_names{$locales_test_number} = '"$!" is ASCII only outside of locale scope';
2045
46973bb2 2046 debug "$first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n";
906f284f 2047
26d80d95
LC
2048 # Does taking lc separately differ from taking
2049 # the lc "in-line"? (This was the bug 19990704.002, change #3568.)
2050 # The bug was in the caching of the 'o'-magic.
66cbab2c 2051 if (! $is_utf8_locale) {
2a680da6 2052 use locale;
6be75cd7 2053
2a680da6
JH
2054 sub lcA {
2055 my $lc0 = lc $_[0];
2056 my $lc1 = lc $_[1];
2057 return $lc0 cmp $lc1;
2058 }
6be75cd7 2059
2a680da6
JH
2060 sub lcB {
2061 return lc($_[0]) cmp lc($_[1]);
2062 }
6be75cd7 2063
2a680da6
JH
2064 my $x = "ab";
2065 my $y = "aa";
2066 my $z = "AB";
6be75cd7 2067
30032ef4 2068 report_result($Locale, ++$locales_test_number,
2a680da6
JH
2069 lcA($x, $y) == 1 && lcB($x, $y) == 1 ||
2070 lcA($x, $z) == 0 && lcB($x, $z) == 0);
6be75cd7 2071 }
66cbab2c
KW
2072 else {
2073 use locale ':not_characters';
2074
2075 sub lcC {
2076 my $lc0 = lc $_[0];
2077 my $lc1 = lc $_[1];
2078 return $lc0 cmp $lc1;
2079 }
2080
2081 sub lcD {
2082 return lc($_[0]) cmp lc($_[1]);
2083 }
2084
2085 my $x = "ab";
2086 my $y = "aa";
2087 my $z = "AB";
2088
30032ef4 2089 report_result($Locale, ++$locales_test_number,
66cbab2c
KW
2090 lcC($x, $y) == 1 && lcD($x, $y) == 1 ||
2091 lcC($x, $z) == 0 && lcD($x, $z) == 0);
2092 }
2093 $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp';
d8093b23 2094
26d80d95
LC
2095 # Does lc of an UPPER (if different from the UPPER) match
2096 # case-insensitively the UPPER, and does the UPPER match
2097 # case-insensitively the lc of the UPPER. And vice versa.
3ba0e062 2098 {
ef4a39e5
JH
2099 use locale;
2100 no utf8;
2101 my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/;
2102
2103 my @f = ();
c4093d7d 2104 ++$locales_test_number;
c08acc4c 2105 $test_names{$locales_test_number} = 'Verify case insensitive matching works';
b9df08e4 2106 foreach my $x (sort { ord $a <=> ord $b } keys %UPPER) {
66cbab2c 2107 if (! $is_utf8_locale) {
71e5cbb3
KW
2108 my $y = lc $x;
2109 next unless uc $y eq $x;
46973bb2 2110 debug_more( "UPPER=", disp_chars(($x)),
92891c66 2111 "; lc=", disp_chars(($y)), "; ",
4eac893c 2112 "; fc=", disp_chars((fc $x)), "; ",
92891c66 2113 disp_chars(($x)), "=~/", disp_chars(($y)), "/i=",
4d2de105 2114 $x =~ /\Q$y/i ? 1 : 0,
92891c66
KW
2115 "; ",
2116 disp_chars(($y)), "=~/", disp_chars(($x)), "/i=",
4d2de105 2117 $y =~ /\Q$x/i ? 1 : 0,
92891c66 2118 "\n");
71e5cbb3
KW
2119 #
2120 # If $x and $y contain regular expression characters
2121 # AND THEY lowercase (/i) to regular expression characters,
2122 # regcomp() will be mightily confused. No, the \Q doesn't
2123 # help here (maybe regex engine internal lowercasing
2124 # is done after the \Q?) An example of this happening is
2125 # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS):
2126 # the chr(173) (the "[") is the lowercase of the chr(235).
2127 #
2128 # Similarly losing EBCDIC locales include cs_cz, cs_CZ,
2129 # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!),
2130 # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT,
2131 # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037,
2132 # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU,
2133 # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR.
2134 #
2135 # Similar things can happen even under (bastardised)
2136 # non-EBCDIC locales: in many European countries before the
2137 # advent of ISO 8859-x nationally customised versions of
2138 # ISO 646 were devised, reusing certain punctuation
2139 # characters for modified characters needed by the
2140 # country/language. For example, the "|" might have
2141 # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS.
2142 #
2143 if ($x =~ $re || $y =~ $re) {
2144 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
2145 next;
2146 }
4d2de105 2147 push @f, $x unless $x =~ /\Q$y/i && $y =~ /\Q$x/i;
26c1569f
KW
2148
2149 # fc is not a locale concept, so Perl uses lc for it.
2150 push @f, $x unless lc $x eq fc $x;
66cbab2c
KW
2151 }
2152 else {
2153 use locale ':not_characters';
2154 my $y = lc $x;
2155 next unless uc $y eq $x;
46973bb2 2156 debug_more( "UPPER=", disp_chars(($x)),
92891c66 2157 "; lc=", disp_chars(($y)), "; ",
4eac893c 2158 "; fc=", disp_chars((fc $x)), "; ",
92891c66 2159 disp_chars(($x)), "=~/", disp_chars(($y)), "/i=",
4d2de105 2160 $x =~ /\Q$y/i ? 1 : 0,
92891c66
KW
2161 "; ",
2162 disp_chars(($y)), "=~/", disp_chars(($x)), "/i=",
4d2de105 2163 $y =~ /\Q$x/i ? 1 : 0,
92891c66 2164 "\n");
66cbab2c 2165
4d2de105 2166 push @f, $x unless $x =~ /\Q$y/i && $y =~ /\Q$x/i;
26c1569f
KW
2167
2168 # The places where Unicode's lc is different from fc are
2169 # skipped here by virtue of the 'next unless uc...' line above
2170 push @f, $x unless lc $x eq fc $x;
66cbab2c 2171 }
c00ff1c7 2172 }
ef4a39e5 2173
b9df08e4 2174 foreach my $x (sort { ord $a <=> ord $b } keys %lower) {
66cbab2c 2175 if (! $is_utf8_locale) {
71e5cbb3
KW
2176 my $y = uc $x;
2177 next unless lc $y eq $x;
46973bb2 2178 debug_more( "lower=", disp_chars(($x)),
92891c66 2179 "; uc=", disp_chars(($y)), "; ",
4eac893c 2180 "; fc=", disp_chars((fc $x)), "; ",
92891c66 2181 disp_chars(($x)), "=~/", disp_chars(($y)), "/i=",
4d2de105 2182 $x =~ /\Q$y/i ? 1 : 0,
92891c66
KW
2183 "; ",
2184 disp_chars(($y)), "=~/", disp_chars(($x)), "/i=",
4d2de105 2185 $y =~ /\Q$x/i ? 1 : 0,
92891c66 2186 "\n");
71e5cbb3
KW
2187 if ($x =~ $re || $y =~ $re) { # See above.
2188 print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n";
2189 next;
2190 }
4d2de105 2191 push @f, $x unless $x =~ /\Q$y/i && $y =~ /\Q$x/i;
26c1569f
KW
2192
2193 push @f, $x unless lc $x eq fc $x;
66cbab2c
KW
2194 }
2195 else {
2196 use locale ':not_characters';
2197 my $y = uc $x;
2198 next unless lc $y eq $x;
46973bb2 2199 debug_more( "lower=", disp_chars(($x)),
92891c66 2200 "; uc=", disp_chars(($y)), "; ",
4eac893c 2201 "; fc=", disp_chars((fc $x)), "; ",
92891c66 2202 disp_chars(($x)), "=~/", disp_chars(($y)), "/i=",
4d2de105 2203 $x =~ /\Q$y/i ? 1 : 0,
92891c66
KW
2204 "; ",
2205 disp_chars(($y)), "=~/", disp_chars(($x)), "/i=",
4d2de105 2206 $y =~ /\Q$x/i ? 1 : 0,
92891c66 2207 "\n");
4d2de105 2208 push @f, $x unless $x =~ /\Q$y/i && $y =~ /\Q$x/i;
26c1569f
KW
2209
2210 push @f, $x unless lc $x eq fc $x;
66cbab2c 2211 }
c00ff1c7 2212 }
7c844d17 2213 report_multi_result($Locale, $locales_test_number, \@f);
5b1f18a7 2214 $problematical_tests{$locales_test_number} = 1;
d8093b23 2215 }
78787052
JL
2216
2217 # [perl #109318]
2218 {
2219 my @f = ();
2220 ++$locales_test_number;
2221 $test_names{$locales_test_number} = 'Verify atof with locale radix and negative exponent';
b057411d 2222 $problematical_tests{$locales_test_number} = 1;
78787052
JL
2223
2224 my $radix = POSIX::localeconv()->{decimal_point};
2225 my @nums = (
2226 "3.14e+9", "3${radix}14e+9", "3.14e-9", "3${radix}14e-9",
2227 "-3.14e+9", "-3${radix}14e+9", "-3.14e-9", "-3${radix}14e-9",
2228 );
2229
2230 if (! $is_utf8_locale) {
2231 use locale;
2232 for my $num (@nums) {
2233 push @f, $num
2234 unless sprintf("%g", $num) =~ /3.+14/;
2235 }
2236 }
2237 else {
2238 use locale ':not_characters';
2239 for my $num (@nums) {
2240 push @f, $num
2241 unless sprintf("%g", $num) =~ /3.+14/;
2242 }
2243 }
2244
435847ca
JH
2245 if ($Config{usequadmath}) {
2246 print "# Skip: no locale radix with usequadmath ($Locale)\n";
2247 report_result($Locale, $locales_test_number, 1);
2248 } else {
2249 report_result($Locale, $locales_test_number, @f == 0);
2250 if (@f) {
2251 print "# failed $locales_test_number locale '$Locale' numbers @f\n"
2252 }
78787052
JL
2253 }
2254 }
8ebc5c01 2255}
284102e8 2256
c4093d7d 2257my $final_locales_test_number = $locales_test_number;
6cf0b567 2258
2a680da6
JH
2259# Recount the errors.
2260
852b6749 2261TEST_NUM:
0d3899b1 2262foreach $test_num ($first_locales_test_number..$final_locales_test_number) {
c4093d7d
KW
2263 if (%setlocale_failed) {
2264 print "not ";
2265 }
852b6749
KW
2266 elsif ($Problem{$test_num}
2267 || ! defined $Okay{$test_num}
2268 || ! @{$Okay{$test_num}})
2269 {
c4093d7d 2270 if (defined $not_necessarily_a_problem_test_number
0d3899b1 2271 && $test_num == $not_necessarily_a_problem_test_number)
c4093d7d
KW
2272 {
2273 print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n";
b4e009be 2274 print "# It usually indicates a problem in the environment,\n";
284102e8
JH
2275 print "# not in Perl itself.\n";
2276 }
852b6749
KW
2277
2278 # If there are any locales that pass this test, or are known-bad, it
2279 # may be that there are enough passes that we TODO the failure.
2280 if (($Okay{$test_num} || $Known_bad_locale{$test_num})
2281 && grep { $_ == $test_num } keys %problematical_tests)
2282 {
26230909 2283 no warnings 'experimental::postderef';
852b6749
KW
2284
2285 # Don't count the known-bad failures when calculating the
2286 # percentage that fail.
2287 my $known_failures = (exists $Known_bad_locale{$test_num})
26230909 2288 ? scalar(keys $Known_bad_locale{$test_num}->%*)
852b6749 2289 : 0;
26230909 2290 my $adjusted_failures = scalar(keys $Problem{$test_num}->%*)
852b6749
KW
2291 - $known_failures;
2292
2293 # Specially handle failures where only known-bad locales fail.
2294 # This makes the diagnositics clearer.
2295 if ($adjusted_failures <= 0) {
2296 print "not ok $test_num $test_names{$test_num} # TODO fails only on ",
2297 "known bad locales: ",
26230909 2298 join " ", keys $Known_bad_locale{$test_num}->%*, "\n";
852b6749
KW
2299 next TEST_NUM;
2300 }
2301
0a974e2d 2302 # Round to nearest .1%
852b6749 2303 my $percent_fail = (int(.5 + (1000 * $adjusted_failures
0a974e2d
KW
2304 / scalar(@Locale))))
2305 / 10;
a20d09fc
KW
2306 if ($percent_fail < $acceptable_failure_percentage) {
2307 if (! $debug) {
2308 $test_names{$test_num} .= 'TODO';
852b6749
KW
2309 print "# ", 100 - $percent_fail, "% of locales not known to be problematic on this platform\n";
2310 print "# pass the following test, so it is likely that the failures\n";
a20d09fc
KW
2311 print "# are errors in the locale definitions. The test is marked TODO, as the\n";
2312 print "# problem is not likely to be Perl's\n";
2313 }
2314 }
30962f68 2315 if ($debug) {
a20d09fc 2316 print "# $percent_fail% of locales (",
26230909 2317 scalar(keys $Problem{$test_num}->%*),
a20d09fc
KW
2318 " of ",
2319 scalar(@Locale),
30962f68
KW
2320 ") fail the above test (TODO cut-off is ",
2321 $acceptable_failure_percentage,
2322 "%)\n";
6d5d702a
KW
2323 }
2324 }
f5627fc1
KW
2325 print "#\n";
2326 if ($debug) {
2327 print "# The code points that had this failure are given above. Look for lines\n";
0d3899b1 2328 print "# that match 'failed $test_num'\n";
f5627fc1
KW
2329 }
2330 else {
2331 print "# For more details, rerun, with environment variable PERL_DEBUG_FULL_TEST=1.\n";
0d3899b1 2332 print "# Then look at that output for lines that match 'failed $test_num'\n";
108a305e 2333 }
284102e8 2334 print "not ";
8ebc5c01 2335 }
0d3899b1
KW
2336 print "ok $test_num";
2337 if (defined $test_names{$test_num}) {
6c2e653d 2338 # If TODO is in the test name, make it thus
0d3899b1
KW
2339 my $todo = $test_names{$test_num} =~ s/TODO\s*//;
2340 print " $test_names{$test_num}";
6c2e653d
KW
2341 print " # TODO" if $todo;
2342 }
c4093d7d 2343 print "\n";
8ebc5c01 2344}
fb73857a 2345
c4093d7d 2346$test_num = $final_locales_test_number;
c213d471 2347
4bc67723 2348unless ( $^O =~ m!^(dragonfly|openbsd|bitrig|mirbsd)$! ) {
083ace8f 2349 # perl #115808
fbd840df
KW
2350 use warnings;
2351 my $warned = 0;
2352 local $SIG{__WARN__} = sub {
2353 $warned = $_[0] =~ /uninitialized/;
2354 };
2355 my $z = "y" . setlocale(&POSIX::LC_ALL, "xyzzy");
2356 ok($warned, "variable set to setlocale(BAD LOCALE) is considered uninitialized");
2357}
2358
094a2f8c 2359# Test that tainting and case changing works on utf8 strings. These tests are
1f5852c9
KW
2360# placed last to avoid disturbing the hard-coded test numbers that existed at
2361# the time these were added above this in this file.
0099bb8d
KW
2362# This also tests that locale overrides unicode_strings in the same scope for
2363# non-utf8 strings.
a810e350 2364setlocale(&POSIX::LC_ALL, "C");
094a2f8c
KW
2365{
2366 use locale;
0099bb8d 2367 use feature 'unicode_strings';
094a2f8c 2368
26c1569f 2369 foreach my $function ("uc", "ucfirst", "lc", "lcfirst", "fc") {
094a2f8c
KW
2370 my @list; # List of code points to test for $function
2371
2372 # Used to calculate the changed case for ASCII characters by using the
2373 # ord, instead of using one of the functions under test.
2374 my $ascii_case_change_delta;
2375 my $above_latin1_case_change_delta; # Same for the specific ords > 255
2376 # that we use
2377
445bf929 2378 # We test an ASCII character, which should change case;
094a2f8c 2379 # a Latin1 character, which shouldn't change case under this C locale,
094a2f8c 2380 # an above-Latin1 character that when the case is changed would cross
445bf929
KW
2381 # the 255/256 boundary, so doesn't change case
2382 # (the \x{149} is one of these, but changes into 2 characters, the
094a2f8c
KW
2383 # first one of which doesn't cross the boundary.
2384 # the final one in each list is an above-Latin1 character whose case
445bf929
KW
2385 # does change. The code below uses its position in its list as a
2386 # marker to indicate that it, unlike the other code points above
2387 # ASCII, has a successful case change
2388 #
2389 # All casing operations under locale (but not :not_characters) should
2390 # taint
094a2f8c 2391 if ($function =~ /^u/) {
9de25f67
KW
2392 @list = ("", "a",
2393 chr(utf8::unicode_to_native(0xe0)),
2394 chr(utf8::unicode_to_native(0xff)),
2395 "\x{fb00}", "\x{149}", "\x{101}");
2396 $ascii_case_change_delta = ($is_ebcdic) ? +64 : -32;
094a2f8c
KW
2397 $above_latin1_case_change_delta = -1;
2398 }
2399 else {
9de25f67
KW
2400 @list = ("", "A",
2401 chr(utf8::unicode_to_native(0xC0)),
2402 "\x{17F}", "\x{100}");
2403 $ascii_case_change_delta = ($is_ebcdic) ? -64 : +32;
094a2f8c
KW
2404 $above_latin1_case_change_delta = +1;
2405 }
66cbab2c 2406 foreach my $is_utf8_locale (0 .. 1) {
71e5cbb3
KW
2407 foreach my $j (0 .. $#list) {
2408 my $char = $list[$j];
0099bb8d
KW
2409
2410 for my $encoded_in_utf8 (0 .. 1) {
faf0c248
KW
2411 my $should_be;
2412 my $changed;
2413 if (! $is_utf8_locale) {
ab0b796c 2414 no warnings 'locale';
faf0c248
KW
2415 $should_be = ($j == $#list)
2416 ? chr(ord($char) + $above_latin1_case_change_delta)
9de25f67
KW
2417 : (length $char == 0 || utf8::native_to_unicode(ord($char)) > 127)
2418 ? $char
2419 : chr(ord($char) + $ascii_case_change_delta);
faf0c248
KW
2420
2421 # This monstrosity is in order to avoid using an eval,
2422 # which might perturb the results
2423 $changed = ($function eq "uc")
2424 ? uc($char)
2425 : ($function eq "ucfirst")
2426 ? ucfirst($char)
2427 : ($function eq "lc")
2428 ? lc($char)
2429 : ($function eq "lcfirst")
2430 ? lcfirst($char)
26c1569f
KW
2431 : ($function eq "fc")
2432 ? fc($char)
faf0c248
KW
2433 : die("Unexpected function \"$function\"");
2434 }
2435 else {
2436 {
2437 no locale;
71e5cbb3 2438
faf0c248
KW
2439 # For utf8-locales the case changing functions
2440 # should work just like they do outside of locale.
2441 # Can use eval here because not testing it when
2442 # not in locale.
2443 $should_be = eval "$function('$char')";
2444 die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if $@;
71e5cbb3 2445
faf0c248
KW
2446 }
2447 use locale ':not_characters';
2448 $changed = ($function eq "uc")
2449 ? uc($char)
2450 : ($function eq "ucfirst")
2451 ? ucfirst($char)
2452 : ($function eq "lc")
2453 ? lc($char)
2454 : ($function eq "lcfirst")
2455 ? lcfirst($char)
26c1569f
KW
2456 : ($function eq "fc")
2457 ? fc($char)
faf0c248 2458 : die("Unexpected function \"$function\"");
71e5cbb3 2459 }
faf0c248
KW
2460 ok($changed eq $should_be,
2461 "$function(\"$char\") in C locale "
2462 . (($is_utf8_locale)
2463 ? "(use locale ':not_characters'"
2464 : "(use locale")
2465 . (($encoded_in_utf8)
2466 ? "; encoded in utf8)"
2467 : "; not encoded in utf8)")
2468 . " should be \"$should_be\", got \"$changed\"");
2469
445bf929
KW
2470 # Tainting shouldn't happen for use locale :not_character
2471 # (a utf8 locale)
2472 (! $is_utf8_locale)
faf0c248
KW
2473 ? check_taint($changed)
2474 : check_taint_not($changed);
2475
2476 # Use UTF-8 next time through the loop
2477 utf8::upgrade($char);
0099bb8d 2478 }
66cbab2c 2479 }
094a2f8c
KW
2480 }
2481 }
2482}
2483
1bfe8fea
KW
2484# Give final advice.
2485
2486my $didwarn = 0;
2487
2488foreach ($first_locales_test_number..$final_locales_test_number) {
2489 if ($Problem{$_}) {
2490 my @f = sort keys %{ $Problem{$_} };
852b6749
KW
2491
2492 # Don't list the failures caused by known-bad locales.
2493 if (exists $known_bad_locales{$^O}) {
2494 @f = grep { $_ !~ $known_bad_locales{$^O} } @f;
2495 next unless @f;
2496 }
1bfe8fea
KW
2497 my $f = join(" ", @f);
2498 $f =~ s/(.{50,60}) /$1\n#\t/g;
2499 print
2500 "#\n",
2501 "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n",
2502 "#\t", $f, "\n#\n",
2503 "# on your system may have errors because the locale test $_\n",
2504 "# \"$test_names{$_}\"\n",
2505 "# failed in ", (@f == 1 ? "that locale" : "those locales"),
2506 ".\n";
2507 print <<EOW;
2508#
2509# If your users are not using these locales you are safe for the moment,
2510# but please report this failure first to perlbug\@perl.com using the
2511# perlbug script (as described in the INSTALL file) so that the exact
2512# details of the failures can be sorted out first and then your operating
2513# system supplier can be alerted about these anomalies.
2514#
2515EOW
2516 $didwarn = 1;
2517 }
2518}
2519
2520# Tell which locales were okay and which were not.
2521
2522if ($didwarn) {
2523 my (@s, @F);
2524
2525 foreach my $l (@Locale) {
2526 my $p = 0;
2527 if ($setlocale_failed{$l}) {
2528 $p++;
2529 }
2530 else {
2531 foreach my $t
2532 ($first_locales_test_number..$final_locales_test_number)
2533 {
2534 $p++ if $Problem{$t}{$l};
2535 }
2536 }
2537 push @s, $l if $p == 0;
2538 push @F, $l unless $p == 0;
2539 }
2540
2541 if (@s) {
2542 my $s = join(" ", @s);
2543 $s =~ s/(.{50,60}) /$1\n#\t/g;
2544
a5cf558a 2545 print
1bfe8fea
KW
2546 "# The following locales\n#\n",
2547 "#\t", $s, "\n#\n",
2548 "# tested okay.\n#\n",
2549 } else {
a5cf558a 2550 print "# None of your locales were fully okay.\n";
1bfe8fea
KW
2551 }
2552
2553 if (@F) {
2554 my $F = join(" ", @F);
2555 $F =~ s/(.{50,60}) /$1\n#\t/g;
2556
c7e7f6b7
KW
2557 my $details = "";
2558 unless ($debug) {
2559 $details = "# For more details, rerun, with environment variable PERL_DEBUG_FULL_TEST=1.\n";
2560 }
2561 elsif ($debug == 1) {
2562 $details = "# For even more details, rerun, with environment variable PERL_DEBUG_FULL_TEST=2.\n";
2563 }
2564
a5cf558a 2565 print
1bfe8fea
KW
2566 "# The following locales\n#\n",
2567 "#\t", $F, "\n#\n",
2568 "# had problems.\n#\n",
c7e7f6b7 2569 $details;
1bfe8fea 2570 } else {
a5cf558a 2571 print "# None of your locales were broken.\n";
1bfe8fea
KW
2572 }
2573}
2574
852b6749
KW
2575if (exists $known_bad_locales{$^O} && ! %Known_bad_locale) {
2576 $test_num++;
2577 print "ok $test_num $^O no longer has known bad locales # TODO\n";
2578}
2579
fdf053ee 2580print "1..$test_num\n";
906f284f 2581
90248788 2582# eof