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");
61 # try to find out a locale where LC_NUMERIC makes a difference
62 my $original_locale = setlocale(LC_NUMERIC);
64 my ($base, $different, $difference);
65 for ("C", @locales) { # prefer C for the base if available
67 setlocale(LC_NUMERIC, $_) or next;
68 my $in = 4.2; # avoid any constant folding bugs
69 if ((my $s = sprintf("%g", $in)) eq "4.2") {
76 last if $base && $different;
78 setlocale(LC_NUMERIC, $original_locale);
81 skip("no locale available where LC_NUMERIC makes a difference", &last - 2)
83 note("using the '$different' locale for LC_NUMERIC tests");
85 local $ENV{LC_NUMERIC} = $_;
86 local $ENV{LC_ALL}; # so it never overrides LC_NUMERIC
88 fresh_perl_is(<<'EOF', "4.2", {},
95 "format() does not look at LC_NUMERIC without 'use locale'");
98 fresh_perl_is(<<'EOF', $difference, {},
106 "format() looks at LC_NUMERIC with 'use locale'");
110 fresh_perl_is(<<'EOF', "4.2", {},
115 { use locale; write; }
117 "too late to look at the locale at write() time");
121 fresh_perl_is(<<'EOF', $difference, {},
122 use locale; format STDOUT =
126 { no locale; write; }
128 "too late to ignore the locale at write() time");
133 local $ENV{LC_NUMERIC} = $_;
134 local $ENV{LC_ALL}; # so it never overrides LC_NUMERIC
135 fresh_perl_is(<<'EOF', "$difference "x4, {},
137 use POSIX qw(locale_h);
138 setlocale(LC_NUMERIC, "");
140 printf("%g %g %s %s ", $in, 4.2, sprintf("%g", $in), sprintf("%g", 4.2));
142 "sprintf() and printf() look at LC_NUMERIC regardless of constant folding");