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;
29 if (defined $ARGV[0] && $ARGV[0] ne "") {
30 if ($ARGV[0] ne 'debug') {
31 print STDERR "Usage: $0 [ debug ]\n";
35 $switches = "switches => [ '-DLv' ]";
38 # reset the locale environment
39 delete local @ENV{'LANG', (grep /^LC_[A-Z]+$/, keys %ENV)};
41 # If user wants this to happen, they set the environment variable AND use
43 delete local $ENV{'PERL_DEBUG_LOCALE_INIT'} unless $debug;
46 fresh_perl_is(<<"EOF",
49 POSIX::setlocale(POSIX::LC_CTYPE(),"C");
50 print "h" =~ /[g\\w]/i || 0;
53 1, { stderr => 'devnull' }, "/il matching of [bracketed] doesn't skip POSIX class if fails individ char");
57 fresh_perl_is(<<"EOF",
60 POSIX::setlocale(POSIX::LC_CTYPE(),"C");
61 print "0" =~ /[\\d[:punct:]]/l || 0;
64 1, { stderr => 'devnull' }, "/l matching of [bracketed] doesn't skip non-first POSIX class");
69 foreach my $locale (@locales) {
70 next if $locale eq "C" || $locale eq 'POSIX';
71 $non_C_locale = $locale;
76 setlocale(LC_NUMERIC, $non_C_locale);
77 isnt(setlocale(LC_NUMERIC), "C", "retrieving current non-C LC_NUMERIC doesn't give 'C'");
78 setlocale(LC_ALL, $non_C_locale);
79 isnt(setlocale(LC_ALL), "C", "retrieving current non-C LC_ALL doesn't give 'C'");
81 my @test_numeric_locales = @locales;
83 # Skip this locale on these cywgwin versions as the returned radix character
86 && version->new(($Config{'osvers'} =~ /^(\d+(?:\.\d+)+)/)[0]) le v2.4.1)
88 @test_numeric_locales = grep { $_ !~ m/ps_AF/i } @test_numeric_locales;
91 # Similarly the arabic locales on solaris don't work right on the
92 # multi-byte radix character, generating malformed UTF-8.
93 if ($^O eq 'solaris') {
94 @test_numeric_locales = grep { $_ !~ m/ ^ ( ar_ | pa_ ) /x }
95 @test_numeric_locales;
98 fresh_perl_is("for (qw(@test_numeric_locales)) {\n" . <<'EOF',
99 use POSIX qw(locale_h);
101 setlocale(LC_NUMERIC, "$_") or next;
102 my $s = sprintf "%g %g", 3.1, 3.1;
103 next if $s eq '3.1 3.1' || $s =~ /^(3.+1) \1$/;
108 "", { eval $switches }, "no locales where LC_NUMERIC breaks");
111 skip("Windows stores locale defaults in the registry", 1 )
113 fresh_perl_is("for (qw(@locales)) {\n" . <<'EOF',
114 use POSIX qw(locale_h);
117 my $s = sprintf "%g", $in; # avoid any constant folding bugs
123 "", { eval $switches }, "LC_NUMERIC without environment nor setlocale() has no effect in any locale");
126 # try to find out a locale where LC_NUMERIC makes a difference
127 my $original_locale = setlocale(LC_NUMERIC);
129 my ($base, $different, $comma, $difference, $utf8_radix);
130 my $radix_encoded_as_utf8;
131 for ("C", @locales) { # prefer C for the base if available
133 setlocale(LC_NUMERIC, $_) or next;
134 my $in = 4.2; # avoid any constant folding bugs
135 if ((my $s = sprintf("%g", $in)) eq "4.2") {
140 my $radix = localeconv()->{decimal_point};
142 # For utf8 locales with a non-ascii radix, it should be encoded as
143 # UTF-8 with the internal flag so set.
144 if (! defined $utf8_radix
145 && $radix =~ /[[:^ascii:]]/u # /u because /l can raise warnings
146 && is_locale_utf8($_))
149 $radix_encoded_as_utf8 = utf8::is_utf8($radix);
152 $comma ||= $_ if $radix eq ',';
156 last if $base && $different && $comma && $utf8_radix;
158 setlocale(LC_NUMERIC, $original_locale);
161 skip("no UTF-8 locale available where LC_NUMERIC radix isn't ASCII", 1 )
163 ok($radix_encoded_as_utf8 == 1, "UTF-8 locale '$utf8_radix' with non-ASCII"
164 . " radix is marked UTF-8");
168 note("using the '$different' locale for LC_NUMERIC tests");
170 local $ENV{LC_NUMERIC} = $different;
172 fresh_perl_is(<<'EOF', "4.2", { eval $switches },
179 "format() does not look at LC_NUMERIC without 'use locale'");
182 fresh_perl_is(<<'EOF', "$difference\n", { eval $switches },
191 "format() looks at LC_NUMERIC with 'use locale'");
195 fresh_perl_is(<<'EOF', ",,", { eval $switches },
198 print localeconv()->{decimal_point};
200 print localeconv()->{decimal_point};
202 "localeconv() looks at LC_NUMERIC with and without 'use locale'");
206 my $categories = ":collate :characters :collate :ctype :monetary :time";
207 fresh_perl_is(<<"EOF", "4.2", { eval $switches },
208 use locale qw($categories);
215 "format() does not look at LC_NUMERIC with 'use locale qw($categories)'");
219 fresh_perl_is(<<'EOF', $difference, { eval $switches },
227 "format() looks at LC_NUMERIC with 'use locale'");
230 for my $category (qw(collate characters collate ctype monetary time)) {
231 for my $negation ("!", "not_") {
232 fresh_perl_is(<<"EOF", $difference, { eval $switches },
233 use locale ":$negation$category";
240 "format() looks at LC_NUMERIC with 'use locale \":"
241 . "$negation$category\"'");
246 fresh_perl_is(<<'EOF', $difference, { eval $switches },
247 use locale ":numeric";
254 "format() looks at LC_NUMERIC with 'use locale \":numeric\"'");
258 fresh_perl_is(<<'EOF', "4.2", { eval $switches },
263 { use locale; write; }
265 "too late to look at the locale at write() time");
269 fresh_perl_is(<<'EOF', $difference, { eval $switches },
275 { no locale; write; }
277 "too late to ignore the locale at write() time");
282 # do not let "use 5.000" affect the locale!
283 # this test is to prevent regression of [rt.perl.org #105784]
284 fresh_perl_is(<<"EOF",
288 POSIX::setlocale(POSIX::LC_NUMERIC(),"$different");
289 \$a = sprintf("%.2f", \$i);
291 \$b = sprintf("%.2f", \$i);
293 print ".\$a \$b" unless \$a eq \$b
295 "", { eval $switches }, "version does not clobber version");
297 fresh_perl_is(<<"EOF",
301 POSIX::setlocale(POSIX::LC_NUMERIC(),"$different");
302 \$a = sprintf("%.2f", \$i);
304 \$b = sprintf("%.2f", \$i);
306 print "\$a \$b" unless \$a eq \$b
308 "", { eval $switches }, "version does not clobber version (via eval)");
312 local $ENV{LC_NUMERIC} = $different;
313 fresh_perl_is(<<'EOF', "$difference "x4, { eval $switches },
315 use POSIX qw(locale_h);
317 printf("%g %g %s %s ", $in, 4.2, sprintf("%g", $in), sprintf("%g", 4.2));
319 "sprintf() and printf() look at LC_NUMERIC regardless of constant folding");
323 local $ENV{LC_NUMERIC} = $different;
324 fresh_perl_is(<<'EOF', "$difference "x4, { eval $switches },
326 use POSIX qw(locale_h);
328 printf("%g %g %s %s ", $in, 4.2, sprintf("%g", $in), sprintf("%g", 4.2));
330 "Uses the above test to verify that on Windows the system default locale has lower priority than LC_NUMERIC");
334 # within this block, STDERR is closed. This is because fresh_perl_is()
335 # forks a shell, and some shells (like bash) can complain noisily when
336 # LC_ALL or similar is set to an invalid value
339 open my $saved_stderr, ">&STDERR" or die "Can't dup STDERR: $!";
343 local $ENV{LC_ALL} = "invalid";
344 local $ENV{LC_NUMERIC} = "invalid";
345 local $ENV{LANG} = $different;
346 local $ENV{PERL_BADLANG} = 0;
348 if (! fresh_perl_is(<<"EOF", "$difference", { eval $switches },
349 if (\$ENV{LC_ALL} ne "invalid") {
350 # Make the test pass if the sh didn't accept the ENV set
352 print "$difference\n";
356 use POSIX qw(locale_h);
360 "LANG is used if LC_ALL, LC_NUMERIC are invalid"))
362 note "To see details change this .t, do not close STDERR";
367 if ($^O eq 'MSWin32') {
368 skip("Win32 uses system default locale in preference to \"C\"",
372 local $ENV{LC_ALL} = "invalid";
373 local $ENV{LC_NUMERIC} = "invalid";
374 local $ENV{LANG} = "invalid";
375 local $ENV{PERL_BADLANG} = 0;
377 if (! fresh_perl_is(<<"EOF", 4.2, { eval $switches },
378 if (\$ENV{LC_ALL} ne "invalid") {
380 print "$difference\n";
384 use POSIX qw(locale_h);
388 'C locale is used if LC_ALL, LC_NUMERIC, LANG are invalid'))
390 note "To see details change this .t, do not close STDERR";
395 open STDERR, ">&", $saved_stderr or die "Can't dup \$saved_stderr: $!";
399 local $ENV{LC_NUMERIC} = $different;
400 fresh_perl_is(<<"EOF",
401 use POSIX qw(locale_h);
403 BEGIN { setlocale(LC_NUMERIC, \"$different\"); };
404 setlocale(LC_ALL, "C");
406 print setlocale(LC_NUMERIC);
408 "C", { stderr => 'devnull' },
409 "No compile error on v-strings when setting the locale to non-dot radix at compile time when default environment has non-dot radix");
413 skip("no locale available where LC_NUMERIC is a comma", 3);
417 fresh_perl_is(<<"EOF",
422 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
427 "1,5\n1.5", { stderr => 'devnull' }, "Radix print properly in locale scope, and without");
429 fresh_perl_is(<<"EOF",
430 my \$i = 1.5; # Should be exactly representable as a base 2
431 # fraction, so can use 'eq' below
434 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
439 "1,5\n2,5", { stderr => 'devnull' }, "Can do math when radix is a comma"); # [perl 115800]
442 unless ($have_strtod) {
443 skip("no strtod()", 1);
446 fresh_perl_is(<<"EOF",
448 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
449 my \$one_point_5 = POSIX::strtod("1,5");
450 \$one_point_5 =~ s/0+\$//; # Remove any trailing zeros
451 print \$one_point_5, "\n";
453 "1.5", { stderr => 'devnull' }, "POSIX::strtod() uses underlying locale");
460 my @valid_categories = valid_locale_categories();
462 my $valid_string = "";
463 my $invalid_string = "";
465 # Deliberately don't include all categories, so as to test this situation
466 for my $i (0 .. @valid_categories - 2) {
467 my $category = $valid_categories[$i];
468 if ($category ne "LC_ALL") {
469 $invalid_string .= ";" if $invalid_string ne "";
470 $invalid_string .= "$category=foo_BAR";
472 next unless $non_C_locale;
473 $valid_string .= ";" if $valid_string ne "";
474 $valid_string .= "$category=$non_C_locale";
481 POSIX::setlocale(LC_ALL, "$invalid_string");
484 is ($?, 0, "In setting complicated invalid LC_ALL, final individ category doesn't need a \';'");
486 skip("no non-C locale available", 1 ) unless $non_C_locale;
490 POSIX::setlocale(LC_ALL, "$valid_string");
493 is ($?, 0, "In setting complicated valid LC_ALL, final individ category doesn't need a \';'");