5 require './test.pl'; # for fresh_perl_is() etc
6 require './loc_tools.pl'; # to find locales
12 # These tests are here instead of lib/locale.t because
13 # some bugs depend on the internal state of the locale
14 # settings and pragma/locale messes up that state pretty badly.
15 # We need "fresh runs".
17 eval { require POSIX; POSIX->import("locale_h") };
19 skip_all("could not load the POSIX module"); # running minitest?
23 my $have_strtod = $Config{d_strtod} eq 'define';
24 my @locales = find_locales( [ 'LC_ALL', 'LC_CTYPE', 'LC_NUMERIC' ]);
25 skip_all("no locales available") unless @locales;
26 note("locales available: @locales");
30 if (defined $ARGV[0] && $ARGV[0] ne "") {
31 if ($ARGV[0] ne 'debug') {
32 print STDERR "Usage: $0 [ debug ]\n";
36 $switches = "switches => [ '-DLv' ]";
39 # reset the locale environment
40 delete local @ENV{'LANG', (grep /^LC_[A-Z]+$/, keys %ENV)};
42 # If user wants this to happen, they set the environment variable AND use
44 delete local $ENV{'PERL_DEBUG_LOCALE_INIT'} unless $debug;
47 fresh_perl_is(<<"EOF",
50 POSIX::setlocale(POSIX::LC_CTYPE(),"C");
51 print "h" =~ /[g\\w]/i || 0;
54 1, { stderr => 'devnull' }, "/il matching of [bracketed] doesn't skip POSIX class if fails individ char");
58 fresh_perl_is(<<"EOF",
61 POSIX::setlocale(POSIX::LC_CTYPE(),"C");
62 print "0" =~ /[\\d[:punct:]]/l || 0;
65 1, { stderr => 'devnull' }, "/l matching of [bracketed] doesn't skip non-first POSIX class");
70 foreach my $locale (@locales) {
71 next if $locale eq "C" || $locale eq 'POSIX' || $locale eq "C.UTF-8";
72 $non_C_locale = $locale;
77 note("using non-C locale '$non_C_locale'");
78 setlocale(LC_NUMERIC, $non_C_locale);
79 isnt(setlocale(LC_NUMERIC), "C", "retrieving current non-C LC_NUMERIC doesn't give 'C'");
80 setlocale(LC_ALL, $non_C_locale);
81 isnt(setlocale(LC_ALL), "C", "retrieving current non-C LC_ALL doesn't give 'C'");
83 my @test_numeric_locales = @locales;
85 # Skip this locale on these cygwin versions as the returned radix character
88 && version->new(($Config{'osvers'} =~ /^(\d+(?:\.\d+)+)/)[0]) le v2.4.1)
90 @test_numeric_locales = grep { $_ !~ m/ps_AF/i } @test_numeric_locales;
93 # Similarly the arabic locales on solaris don't work right on the
94 # multi-byte radix character, generating malformed UTF-8.
95 if ($^O eq 'solaris') {
96 @test_numeric_locales = grep { $_ !~ m/ ^ ( ar_ | pa_ ) /x }
97 @test_numeric_locales;
100 fresh_perl_is("for (qw(@test_numeric_locales)) {\n" . <<'EOF',
101 use POSIX qw(locale_h);
103 setlocale(LC_NUMERIC, "$_") or next;
104 my $s = sprintf "%g %g", 3.1, 3.1;
105 next if $s eq '3.1 3.1' || $s =~ /^(3.+1) \1$/;
110 "", { eval $switches }, "no locales where LC_NUMERIC breaks");
113 skip("Windows stores locale defaults in the registry", 1 )
115 fresh_perl_is("for (qw(@locales)) {\n" . <<'EOF',
116 use POSIX qw(locale_h);
119 my $s = sprintf "%g", $in; # avoid any constant folding bugs
125 "", { eval $switches }, "LC_NUMERIC without environment nor setlocale() has no effect in any locale");
128 # try to find out a locale where LC_NUMERIC makes a difference
129 my $original_locale = setlocale(LC_NUMERIC);
131 my ($base, $different, $comma, $difference, $utf8_radix);
132 my $radix_encoded_as_utf8;
133 for ("C", @locales) { # prefer C for the base if available
135 setlocale(LC_NUMERIC, $_) or next;
136 my $in = 4.2; # avoid any constant folding bugs
137 if ((my $s = sprintf("%g", $in)) eq "4.2") {
142 my $radix = localeconv()->{decimal_point};
144 # For utf8 locales with a non-ascii radix, it should be encoded as
145 # UTF-8 with the internal flag so set.
146 if (! defined $utf8_radix
147 && $radix =~ /[[:^ascii:]]/u # /u because /l can raise warnings
148 && is_locale_utf8($_))
151 $radix_encoded_as_utf8 = utf8::is_utf8($radix);
154 $comma ||= $_ if $radix eq ',';
158 last if $base && $different && $comma && $utf8_radix;
160 setlocale(LC_NUMERIC, $original_locale);
163 skip("no UTF-8 locale available where LC_NUMERIC radix isn't ASCII", 1 )
165 ok($radix_encoded_as_utf8 == 1, "UTF-8 locale '$utf8_radix' with non-ASCII"
166 . " radix is marked UTF-8");
170 skip("no locale available where LC_NUMERIC radix isn't '.'", 30) unless $different;
171 note("using the '$different' locale for LC_NUMERIC tests");
173 local $ENV{LC_NUMERIC} = $different;
175 fresh_perl_is(<<'EOF', "4.2", { eval $switches },
182 "format() does not look at LC_NUMERIC without 'use locale'");
185 fresh_perl_is(<<'EOF', "$difference\n", { eval $switches },
194 "format() looks at LC_NUMERIC with 'use locale'");
198 fresh_perl_is(<<'EOF', ",,", { eval $switches },
201 print localeconv()->{decimal_point};
203 print localeconv()->{decimal_point};
205 "localeconv() looks at LC_NUMERIC with and without 'use locale'");
209 my $categories = ":collate :characters :collate :ctype :monetary :time";
210 fresh_perl_is(<<"EOF", "4.2", { eval $switches },
211 use locale qw($categories);
218 "format() does not look at LC_NUMERIC with 'use locale qw($categories)'");
222 fresh_perl_is(<<'EOF', $difference, { eval $switches },
230 "format() looks at LC_NUMERIC with 'use locale'");
233 for my $category (qw(collate characters collate ctype monetary time)) {
234 for my $negation ("!", "not_") {
235 fresh_perl_is(<<"EOF", $difference, { eval $switches },
236 use locale ":$negation$category";
243 "format() looks at LC_NUMERIC with 'use locale \":"
244 . "$negation$category\"'");
249 fresh_perl_is(<<'EOF', $difference, { eval $switches },
250 use locale ":numeric";
257 "format() looks at LC_NUMERIC with 'use locale \":numeric\"'");
261 fresh_perl_is(<<'EOF', "4.2", { eval $switches },
266 { use locale; write; }
268 "too late to look at the locale at write() time");
272 fresh_perl_is(<<'EOF', $difference, { eval $switches },
278 { no locale; write; }
280 "too late to ignore the locale at write() time");
285 # do not let "use 5.000" affect the locale!
286 # this test is to prevent regression of [rt.perl.org #105784]
287 fresh_perl_is(<<"EOF",
291 POSIX::setlocale(POSIX::LC_NUMERIC(),"$different");
292 \$a = sprintf("%.2f", \$i);
294 \$b = sprintf("%.2f", \$i);
296 print ".\$a \$b" unless \$a eq \$b
298 "", { eval $switches }, "version does not clobber version");
300 fresh_perl_is(<<"EOF",
304 POSIX::setlocale(POSIX::LC_NUMERIC(),"$different");
305 \$a = sprintf("%.2f", \$i);
307 \$b = sprintf("%.2f", \$i);
309 print "\$a \$b" unless \$a eq \$b
311 "", { eval $switches }, "version does not clobber version (via eval)");
315 local $ENV{LC_NUMERIC} = $different;
316 fresh_perl_is(<<'EOF', "$difference "x4, { eval $switches },
318 use POSIX qw(locale_h);
320 printf("%g %g %s %s ", $in, 4.2, sprintf("%g", $in), sprintf("%g", 4.2));
322 "sprintf() and printf() look at LC_NUMERIC regardless of constant folding");
326 local $ENV{LC_NUMERIC} = $different;
327 fresh_perl_is(<<'EOF', "$difference "x4, { eval $switches },
329 use POSIX qw(locale_h);
331 printf("%g %g %s %s ", $in, 4.2, sprintf("%g", $in), sprintf("%g", 4.2));
333 "Uses the above test to verify that on Windows the system default locale has lower priority than LC_NUMERIC");
337 # within this block, STDERR is closed. This is because fresh_perl_is()
338 # forks a shell, and some shells (like bash) can complain noisily when
339 # LC_ALL or similar is set to an invalid value
342 open my $saved_stderr, ">&STDERR" or die "Can't dup STDERR: $!";
346 local $ENV{LC_ALL} = "invalid";
347 local $ENV{LC_NUMERIC} = "invalid";
348 local $ENV{LANG} = $different;
349 local $ENV{PERL_BADLANG} = 0;
351 if (! fresh_perl_is(<<"EOF", "$difference", { eval $switches },
352 if (\$ENV{LC_ALL} ne "invalid") {
353 # Make the test pass if the sh didn't accept the ENV set
355 print "$difference\n";
359 use POSIX qw(locale_h);
363 "LANG is used if LC_ALL, LC_NUMERIC are invalid"))
365 note "To see details change this .t, do not close STDERR";
370 if ($^O eq 'MSWin32') {
371 skip("Win32 uses system default locale in preference to \"C\"",
375 local $ENV{LC_ALL} = "invalid";
376 local $ENV{LC_NUMERIC} = "invalid";
377 local $ENV{LANG} = "invalid";
378 local $ENV{PERL_BADLANG} = 0;
380 if (! fresh_perl_is(<<"EOF", 4.2, { eval $switches },
381 if (\$ENV{LC_ALL} ne "invalid") {
383 print "$difference\n";
387 use POSIX qw(locale_h);
391 'C locale is used if LC_ALL, LC_NUMERIC, LANG are invalid'))
393 note "To see details change this .t, do not close STDERR";
398 open STDERR, ">&", $saved_stderr or die "Can't dup \$saved_stderr: $!";
402 local $ENV{LC_NUMERIC} = $different;
403 fresh_perl_is(<<"EOF",
404 use POSIX qw(locale_h);
406 BEGIN { setlocale(LC_NUMERIC, \"$different\"); };
407 setlocale(LC_ALL, "C");
409 print setlocale(LC_NUMERIC);
411 "C", { stderr => 'devnull' },
412 "No compile error on v-strings when setting the locale to non-dot radix at compile time when default environment has non-dot radix");
416 skip("no locale available where LC_NUMERIC is a comma", 3);
420 fresh_perl_is(<<"EOF",
425 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
430 "1,5\n1.5", { stderr => 'devnull' }, "Radix print properly in locale scope, and without");
432 fresh_perl_is(<<"EOF",
433 my \$i = 1.5; # Should be exactly representable as a base 2
434 # fraction, so can use 'eq' below
437 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
442 "1,5\n2,5", { stderr => 'devnull' }, "Can do math when radix is a comma"); # [perl 115800]
445 unless ($have_strtod) {
446 skip("no strtod()", 1);
449 fresh_perl_is(<<"EOF",
451 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
452 my \$one_point_5 = POSIX::strtod("1,5");
453 \$one_point_5 =~ s/0+\$//; # Remove any trailing zeros
454 print \$one_point_5, "\n";
456 "1.5", { stderr => 'devnull' }, "POSIX::strtod() uses underlying locale");
463 # Note: the setlocale Configure probe could be enhanced to give us the
464 # syntax to use, but khw doesn't think it's worth it at this time, as
465 # the current outliers seem to be skipped by the test just below
466 # anyway. If the POSIX 2008 locale functions are being used, the
467 # syntax becomes mostly irrelevant, so do the test anyway if they are.
468 # It's a lot of trouble to figure out in a perl script.
469 if ($Config{d_setlocale_accepts_any_locale_name} eq 'true')
471 skip("Can't distinguish between valid and invalid locale names on this system", 2);
474 my @valid_categories = valid_locale_categories();
476 my $valid_string = "";
477 my $invalid_string = "";
479 # Deliberately don't include all categories, so as to test this situation
480 for my $i (0 .. @valid_categories - 2) {
481 my $category = $valid_categories[$i];
482 if ($category ne "LC_ALL") {
483 $invalid_string .= ";" if $invalid_string ne "";
484 $invalid_string .= "$category=foo_BAR";
486 next unless $non_C_locale;
487 $valid_string .= ";" if $valid_string ne "";
488 $valid_string .= "$category=$non_C_locale";
495 POSIX::setlocale(LC_ALL, "$invalid_string");
498 is ($?, 0, "In setting complicated invalid LC_ALL, final individ category doesn't need a \';'");
500 skip("no non-C locale available", 1 ) unless $non_C_locale;
504 POSIX::setlocale(LC_ALL, "$valid_string");
507 is ($?, 0, "In setting complicated valid LC_ALL, final individ category doesn't need a \';'");