1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
#!./perl
BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
require './test.pl'; # for fresh_perl_is() etc
require './loc_tools.pl'; # to find locales
}
use strict;
########
# These tests are here instead of lib/locale.t because
# some bugs depend on in the internal state of the locale
# settings and pragma/locale messes up that state pretty badly.
# We need "fresh runs".
BEGIN {
eval { require POSIX; POSIX->import("locale_h") };
if ($@) {
skip_all("could not load the POSIX module"); # running minitest?
}
}
use Config;
my $have_strtod = $Config{d_strtod} eq 'define';
my @locales = eval { find_locales( [ &LC_ALL, &LC_CTYPE, &LC_NUMERIC ] ) };
skip_all("no locales available") unless @locales;
plan tests => &last;
fresh_perl_is("for (qw(@locales)) {\n" . <<'EOF',
use POSIX qw(locale_h);
use locale;
setlocale(LC_NUMERIC, "$_") or next;
my $s = sprintf "%g %g", 3.1, 3.1;
next if $s eq '3.1 3.1' || $s =~ /^(3.+1) \1$/;
print "$_ $s\n";
}
EOF
"", {}, "no locales where LC_NUMERIC breaks");
{
local $ENV{LC_NUMERIC};
local $ENV{LC_ALL}; # so it never overrides LC_NUMERIC
fresh_perl_is("for (qw(@locales)) {\n" . <<'EOF',
use POSIX qw(locale_h);
use locale;
my $in = 4.2;
my $s = sprintf "%g", $in; # avoid any constant folding bugs
next if $s eq "4.2";
print "$_ $s\n";
}
EOF
"", {}, "LC_NUMERIC without environment nor setlocale() has no effect in any locale");
}
# try to find out a locale where LC_NUMERIC makes a difference
my $original_locale = setlocale(LC_NUMERIC);
my ($base, $different, $comma, $difference);
for ("C", @locales) { # prefer C for the base if available
BEGIN {
if($Config{d_setlocale}) {
require locale; import locale;
}
}
setlocale(LC_NUMERIC, $_) or next;
my $in = 4.2; # avoid any constant folding bugs
if ((my $s = sprintf("%g", $in)) eq "4.2") {
$base ||= $_;
} else {
$different ||= $_;
$difference ||= $s;
$comma ||= $_ if localeconv()->{decimal_point} eq ',';
}
last if $base && $different && $comma;
}
setlocale(LC_NUMERIC, $original_locale);
SKIP: {
skip("no locale available where LC_NUMERIC makes a difference", &last - 2)
if !$different;
note("using the '$different' locale for LC_NUMERIC tests");
for ($different) {
local $ENV{LC_NUMERIC} = $_;
local $ENV{LC_ALL}; # so it never overrides LC_NUMERIC
fresh_perl_is(<<'EOF', "4.2", {},
format STDOUT =
@.#
4.179
.
write;
EOF
"format() does not look at LC_NUMERIC without 'use locale'");
{
fresh_perl_is(<<'EOF', $difference, {},
use locale;
format STDOUT =
@.#
4.179
.
write;
EOF
"format() looks at LC_NUMERIC with 'use locale'");
}
{
fresh_perl_is(<<'EOF', $difference, {},
use locale ":not_characters";
format STDOUT =
@.#
4.179
.
write;
EOF
"format() looks at LC_NUMERIC with 'use locale \":not_characters\"'");
}
{
fresh_perl_is(<<'EOF', "4.2", {},
format STDOUT =
@.#
4.179
.
{ require locale; import locale; write; }
EOF
"too late to look at the locale at write() time");
}
{
fresh_perl_is(<<'EOF', $difference, {},
use locale;
format STDOUT =
@.#
4.179
.
{ no locale; write; }
EOF
"too late to ignore the locale at write() time");
}
}
{
# do not let "use 5.000" affect the locale!
# this test is to prevent regression of [rt.perl.org #105784]
fresh_perl_is(<<"EOF",
BEGIN {
if("$Config{d_setlocale}") {
require locale; import locale;
}
}
use POSIX;
my \$i = 0.123;
POSIX::setlocale(POSIX::LC_NUMERIC(),"$different");
\$a = sprintf("%.2f", \$i);
require version;
\$b = sprintf("%.2f", \$i);
print ".\$a \$b" unless \$a eq \$b
EOF
"", {}, "version does not clobber version");
fresh_perl_is(<<"EOF",
use locale;
use POSIX;
my \$i = 0.123;
POSIX::setlocale(POSIX::LC_NUMERIC(),"$different");
\$a = sprintf("%.2f", \$i);
eval "use v5.0.0";
\$b = sprintf("%.2f", \$i);
print "\$a \$b" unless \$a eq \$b
EOF
"", {}, "version does not clobber version (via eval)");
}
for ($different) {
local $ENV{LC_NUMERIC} = $_;
local $ENV{LC_ALL}; # so it never overrides LC_NUMERIC
fresh_perl_is(<<'EOF', "$difference "x4, {},
use locale;
use POSIX qw(locale_h);
setlocale(LC_NUMERIC, "");
my $in = 4.2;
printf("%g %g %s %s ", $in, 4.2, sprintf("%g", $in), sprintf("%g", 4.2));
EOF
"sprintf() and printf() look at LC_NUMERIC regardless of constant folding");
}
for ($different) {
local $ENV{LC_NUMERIC} = $_;
local $ENV{LC_ALL}; # so it never overrides LC_NUMERIC
fresh_perl_is(<<"EOF",
use POSIX qw(locale_h);
BEGIN { setlocale(LC_NUMERIC, \"$_\"); };
setlocale(LC_ALL, "C");
use 5.008;
print setlocale(LC_NUMERIC);
EOF
"C", { },
"No compile error on v-strings when setting the locale to non-dot radix at compile time when default environment has non-dot radix");
}
for ($different) {
local $ENV{LC_NUMERIC} = $_;
local $ENV{LC_ALL}; # so it never overrides LC_NUMERIC
fresh_perl_is(<<"EOF",
use POSIX qw(locale_h);
BEGIN { print setlocale(LC_NUMERIC), "\n"; };
EOF
$_, { },
"Passed in LC_NUMERIC is valid at compilation time");
}
unless ($comma) {
skip("no locale available where LC_NUMERIC is a comma", 2);
}
else {
fresh_perl_is(<<"EOF",
my \$i = 1.5;
{
use locale;
use POSIX;
POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
print \$i, "\n";
}
print \$i, "\n";
EOF
"1,5\n1.5", {}, "Radix print properly in locale scope, and without");
fresh_perl_is(<<"EOF",
my \$i = 1.5; # Should be exactly representable as a base 2
# fraction, so can use 'eq' below
use locale;
use POSIX;
POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
print \$i, "\n";
\$i += 1;
print \$i, "\n";
EOF
"1,5\n2,5", {}, "Can do math when radix is a comma"); # [perl 115800]
unless ($have_strtod) {
skip("no strtod()", 1);
}
else {
fresh_perl_is(<<"EOF",
use POSIX;
POSIX::setlocale(POSIX::LC_NUMERIC(),"$comma");
my \$one_point_5 = POSIX::strtod("1,5");
\$one_point_5 =~ s/0+\$//; # Remove any trailing zeros
print \$one_point_5, "\n";
EOF
"1.5", {}, "POSIX::strtod() uses underlying locale");
}
}
{
fresh_perl_is(<<"EOF",
use locale;
use POSIX;
POSIX::setlocale(POSIX::LC_CTYPE(),"C");
print "h" =~ /[g\\w]/i || 0;
print "\\n";
EOF
1, {}, "/il matching of [bracketed] doesn't skip POSIX class if fails individ char");
}
{
fresh_perl_is(<<"EOF",
use locale;
use POSIX;
POSIX::setlocale(POSIX::LC_CTYPE(),"C");
print "0" =~ /[\\d[:punct:]]/l || 0;
print "\\n";
EOF
1, {}, "/l matching of [bracketed] doesn't skip non-first POSIX class");
}
} # SKIP
sub last { 17 }
|