5 require './test.pl'; # for fresh_perl_is() etc
11 # These tests are here instead of lib/locale.t because
12 # some bugs depend on in the internal state of the locale
13 # settings and pragma/locale messes up that state pretty badly.
14 # We need "fresh runs".
16 eval { require POSIX; POSIX->import("locale_h") };
18 skip_all("could not load the POSIX module"); # running minitest?
22 my $have_setlocale = $Config{d_setlocale} eq 'define';
23 $have_setlocale = 0 if $@;
24 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
25 # and mingw32 uses said silly CRT
26 $have_setlocale = 0 if (($^O eq 'MSWin32' || $^O eq 'NetWare') && $Config{cc} =~ /^(cl|gcc)/i);
27 skip_all("no setlocale available") unless $have_setlocale;
29 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) {
36 skip_all("no locales available") unless @locales;
39 fresh_perl_is("for (qw(@locales)) {\n" . <<'EOF',
40 use POSIX qw(locale_h);
42 setlocale(LC_NUMERIC, "$_") or next;
43 my $s = sprintf "%g %g", 3.1, 3.1;
44 next if $s eq '3.1 3.1' || $s =~ /^(3.+1) \1$/;
48 "", {}, "no locales where LC_NUMERIC breaks");
50 fresh_perl_is("for (qw(@locales)) {\n" . <<'EOF',
51 use POSIX qw(locale_h);
54 my $s = sprintf "%g", $in; # avoid any constant folding bugs
59 "", {}, "LC_NUMERIC without setlocale() has no effect in any locale");
62 # try to find out a locale where LC_NUMERIC makes a difference
63 my $original_locale = setlocale(LC_NUMERIC);
65 my ($base, $different, $comma, $difference);
66 for ("C", @locales) { # prefer C for the base if available
68 if($Config{d_setlocale}) {
69 require locale; import locale;
72 setlocale(LC_NUMERIC, $_) or next;
73 my $in = 4.2; # avoid any constant folding bugs
74 if ((my $s = sprintf("%g", $in)) eq "4.2") {
79 $comma ||= $_ if localeconv()->{decimal_point} eq ',';
82 last if $base && $different && $comma;
84 setlocale(LC_NUMERIC, $original_locale);
87 skip("no locale available where LC_NUMERIC makes a difference", &last - 2)
89 note("using the '$different' locale for LC_NUMERIC tests");
91 local $ENV{LC_NUMERIC} = $_;
92 local $ENV{LC_ALL}; # so it never overrides LC_NUMERIC
94 fresh_perl_is(<<'EOF', "4.2", {},
101 "format() does not look at LC_NUMERIC without 'use locale'");
104 fresh_perl_is(<<'EOF', $difference, {},
112 "format() looks at LC_NUMERIC with 'use locale'");
116 fresh_perl_is(<<'EOF', "4.2", {},
121 { require locale; import locale; write; }
123 "too late to look at the locale at write() time");
127 fresh_perl_is(<<'EOF', $difference, {},
133 { no locale; write; }
135 "too late to ignore the locale at write() time");
140 # do not let "use 5.000" affect the locale!
141 # this test is to prevent regression of [rt.perl.org #105784]
142 fresh_perl_is(<<"EOF",
144 if("$Config{d_setlocale}") {
145 require locale; import locale;
150 POSIX::setlocale(POSIX::LC_NUMERIC(),"$different");
151 \$a = sprintf("%.2f", \$i);
153 \$b = sprintf("%.2f", \$i);
154 print ".\$a \$b" unless \$a eq \$b
156 "", {}, "version does not clobber version");
158 fresh_perl_is(<<"EOF",
162 POSIX::setlocale(POSIX::LC_NUMERIC(),"$different");
163 \$a = sprintf("%.2f", \$i);
165 \$b = sprintf("%.2f", \$i);
166 print "\$a \$b" unless \$a eq \$b
168 "", {}, "version does not clobber version (via eval)");
172 local $ENV{LC_NUMERIC} = $_;
173 local $ENV{LC_ALL}; # so it never overrides LC_NUMERIC
174 fresh_perl_is(<<'EOF', "$difference "x4, {},
176 use POSIX qw(locale_h);
177 setlocale(LC_NUMERIC, "");
179 printf("%g %g %s %s ", $in, 4.2, sprintf("%g", $in), sprintf("%g", 4.2));
181 "sprintf() and printf() look at LC_NUMERIC regardless of constant folding");
185 skip("no locale available where LC_NUMERIC is a comma", 2);
189 fresh_perl_is(<<"EOF",
194 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
199 "1,5\n1.5", {}, "Radix print properly in locale scope, and without");
201 fresh_perl_is(<<"EOF",
202 my \$i = 1.5; # Should be exactly representable as a base 2
203 # fraction, so can use 'eq' below
206 POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
211 "1,5\n2,5", {}, "Can do math when radix is a comma"); # [perl 115800]