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 = eval { find_locales( [ &LC_ALL, &LC_CTYPE, &LC_NUMERIC ],
25 1 # Only return locales that work well with
28 skip_all("no locales available") unless @locales;
30 # reset the locale environment
31 local @ENV{'LANG', (grep /^LC_/, keys %ENV)};
36 foreach my $locale (@locales) {
37 next if $locale eq "C" || $locale eq 'POSIX';
38 $non_C_locale = $locale;
43 skip("no non-C locale available", 2 ) unless $non_C_locale;
44 setlocale(LC_NUMERIC, $non_C_locale);
45 isnt(setlocale(LC_NUMERIC), "C", "retrieving current non-C LC_NUMERIC doesn't give 'C'");
46 setlocale(LC_ALL, $non_C_locale);
47 isnt(setlocale(LC_ALL), "C", "retrieving current non-C LC_ALL doesn't give 'C'");
50 # Skip this locale on these cywgwin versions as the returned radix character
52 my @test_numeric_locales = ($^O ne 'cygwin' || version->new(($Config{'osvers'} =~ /^(\d+(?:\.\d+)+)/)[0]) gt v2.4.1)
54 : grep { $_ !~ m/ps_AF/i } @locales;
56 fresh_perl_is("for (qw(@test_numeric_locales)) {\n" . <<'EOF',
57 use POSIX qw(locale_h);
59 setlocale(LC_NUMERIC, "$_") or next;
60 my $s = sprintf "%g %g", 3.1, 3.1;
61 next if $s eq '3.1 3.1' || $s =~ /^(3.+1) \1$/;
65 "", {}, "no locales where LC_NUMERIC breaks");
68 skip("Windows stores locale defaults in the registry", 1 )
70 fresh_perl_is("for (qw(@locales)) {\n" . <<'EOF',
71 use POSIX qw(locale_h);
74 my $s = sprintf "%g", $in; # avoid any constant folding bugs
79 "", {}, "LC_NUMERIC without environment nor setlocale() has no effect in any locale");
82 # try to find out a locale where LC_NUMERIC makes a difference
83 my $original_locale = setlocale(LC_NUMERIC);
85 my ($base, $different, $comma, $difference, $utf8_radix);
86 my $radix_encoded_as_utf8;
87 for ("C", @locales) { # prefer C for the base if available
89 setlocale(LC_NUMERIC, $_) or next;
90 my $in = 4.2; # avoid any constant folding bugs
91 if ((my $s = sprintf("%g", $in)) eq "4.2") {
96 my $radix = localeconv()->{decimal_point};
98 # For utf8 locales with a non-ascii radix, it should be encoded as
99 # UTF-8 with the internal flag so set.
100 if (! defined $utf8_radix
101 && $radix =~ /[[:^ascii:]]/
102 && is_locale_utf8($_))
105 $radix_encoded_as_utf8 = utf8::is_utf8($radix);
108 $comma ||= $_ if $radix eq ',';
112 last if $base && $different && $comma && $utf8_radix;
114 setlocale(LC_NUMERIC, $original_locale);
117 skip("no UTF-8 locale available where LC_NUMERIC radix isn't ASCII", 1 )
119 ok($radix_encoded_as_utf8 == 1, "UTF-8 locale '$utf8_radix' with non-ASCII"
120 . " radix is marked UTF-8");
124 skip("no locale available where LC_NUMERIC makes a difference", &last - 7 )
125 if !$different; # -7 is 5 tests before this block; 2 after
126 note("using the '$different' locale for LC_NUMERIC tests");
128 local $ENV{LC_NUMERIC} = $different;
130 fresh_perl_is(<<'EOF', "4.2", {},
137 "format() does not look at LC_NUMERIC without 'use locale'");
140 fresh_perl_is(<<'EOF', "$difference\n", {},
149 "format() looks at LC_NUMERIC with 'use locale'");
153 fresh_perl_is(<<'EOF', ",,", {},
154 print localeconv()->{decimal_point};
157 print localeconv()->{decimal_point};
159 "localeconv() looks at LC_NUMERIC with and without 'use locale'");
163 my $categories = ":collate :characters :collate :ctype :monetary :time";
164 fresh_perl_is(<<"EOF", "4.2", {},
165 use locale qw($categories);
172 "format() does not look at LC_NUMERIC with 'use locale qw($categories)'");
176 fresh_perl_is(<<'EOF', $difference, {},
184 "format() looks at LC_NUMERIC with 'use locale'");
187 for my $category (qw(collate characters collate ctype monetary time)) {
188 for my $negation ("!", "not_") {
189 fresh_perl_is(<<"EOF", $difference, {},
190 use locale ":$negation$category";
197 "format() looks at LC_NUMERIC with 'use locale \":"
198 . "$negation$category\"'");
203 fresh_perl_is(<<'EOF', $difference, {},
204 use locale ":numeric";
211 "format() looks at LC_NUMERIC with 'use locale \":numeric\"'");
215 fresh_perl_is(<<'EOF', "4.2", {},
220 { use locale; write; }
222 "too late to look at the locale at write() time");
226 fresh_perl_is(<<'EOF', $difference, {},
232 { no locale; write; }
234 "too late to ignore the locale at write() time");
239 # do not let "use 5.000" affect the locale!
240 # this test is to prevent regression of [rt.perl.org #105784]
241 fresh_perl_is(<<"EOF",
245 POSIX::setlocale(POSIX::LC_NUMERIC(),"$different");
246 \$a = sprintf("%.2f", \$i);
248 \$b = sprintf("%.2f", \$i);
249 print ".\$a \$b" unless \$a eq \$b
251 "", {}, "version does not clobber version");
253 fresh_perl_is(<<"EOF",
257 POSIX::setlocale(POSIX::LC_NUMERIC(),"$different");
258 \$a = sprintf("%.2f", \$i);
260 \$b = sprintf("%.2f", \$i);
261 print "\$a \$b" unless \$a eq \$b
263 "", {}, "version does not clobber version (via eval)");
267 local $ENV{LC_NUMERIC} = $different;
268 fresh_perl_is(<<'EOF', "$difference "x4, {},
270 use POSIX qw(locale_h);
272 printf("%g %g %s %s ", $in, 4.2, sprintf("%g", $in), sprintf("%g", 4.2));
274 "sprintf() and printf() look at LC_NUMERIC regardless of constant folding");
278 local $ENV{LC_NUMERIC} = $different;
279 fresh_perl_is(<<'EOF', "$difference "x4, {},
281 use POSIX qw(locale_h);
283 printf("%g %g %s %s ", $in, 4.2, sprintf("%g", $in), sprintf("%g", 4.2));
285 "Uses the above test to verify that on Windows the system default locale has lower priority than LC_NUMERIC");
289 # within this block, STDERR is closed. This is because fresh_perl_is()
290 # forks a shell, and some shells (like bash) can complain noisily when
291 # LC_ALL or similar is set to an invalid value
294 open my $saved_stderr, ">&STDERR" or die "Can't dup STDERR: $!";
298 local $ENV{LC_ALL} = "invalid";
299 local $ENV{LC_NUMERIC} = "invalid";
300 local $ENV{LANG} = $different;
301 local $ENV{PERL_BADLANG} = 0;
303 if (! fresh_perl_is(<<"EOF", "$difference", { },
304 if (\$ENV{LC_ALL} ne "invalid") {
305 # Make the test pass if the sh didn't accept the ENV set
306 print "$difference\n";
310 use POSIX qw(locale_h);
314 "LANG is used if LC_ALL, LC_NUMERIC are invalid"))
316 note "To see details change this .t, do not close STDERR";
321 if ($^O eq 'MSWin32') {
322 skip("Win32 uses system default locale in preference to \"C\"",
326 local $ENV{LC_ALL} = "invalid";
327 local $ENV{LC_NUMERIC} = "invalid";
328 local $ENV{LANG} = "invalid";
329 local $ENV{PERL_BADLANG} = 0;
331 if (! fresh_perl_is(<<"EOF", 4.2, { },
332 if (\$ENV{LC_ALL} ne "invalid") {
333 print "$difference\n";
337 use POSIX qw(locale_h);
341 'C locale is used if LC_ALL, LC_NUMERIC, LANG are invalid'))
343 note "To see details change this .t, do not close STDERR";
348 open STDERR, ">&", $saved_stderr or die "Can't dup \$saved_stderr: $!";
352 local $ENV{LC_NUMERIC} = $different;
353 fresh_perl_is(<<"EOF",
354 use POSIX qw(locale_h);
356 BEGIN { setlocale(LC_NUMERIC, \"$different\"); };
357 setlocale(LC_ALL, "C");
359 print setlocale(LC_NUMERIC);
362 "No compile error on v-strings when setting the locale to non-dot radix at compile time when default environment has non-dot radix");
366 skip("no locale available where LC_NUMERIC is a comma", 3);
370 fresh_perl_is(<<"EOF",
375 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
380 "1,5\n1.5", {}, "Radix print properly in locale scope, and without");
382 fresh_perl_is(<<"EOF",
383 my \$i = 1.5; # Should be exactly representable as a base 2
384 # fraction, so can use 'eq' below
387 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
392 "1,5\n2,5", {}, "Can do math when radix is a comma"); # [perl 115800]
394 unless ($have_strtod) {
395 skip("no strtod()", 1);
398 fresh_perl_is(<<"EOF",
400 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
401 my \$one_point_5 = POSIX::strtod("1,5");
402 \$one_point_5 =~ s/0+\$//; # Remove any trailing zeros
403 print \$one_point_5, "\n";
405 "1.5", {}, "POSIX::strtod() uses underlying locale");
411 fresh_perl_is(<<"EOF",
414 POSIX::setlocale(POSIX::LC_CTYPE(),"C");
415 print "h" =~ /[g\\w]/i || 0;
418 1, {}, "/il matching of [bracketed] doesn't skip POSIX class if fails individ char");
422 fresh_perl_is(<<"EOF",
425 POSIX::setlocale(POSIX::LC_CTYPE(),"C");
426 print "0" =~ /[\\d[:punct:]]/l || 0;
429 1, {}, "/l matching of [bracketed] doesn't skip non-first POSIX class");
433 # IMPORTANT: When adding tests before the following line, be sure to update
435 # skip("no locale available where LC_NUMERIC makes a difference", ...)