File: locale.t

package info (click to toggle)
perl 5.42.0-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (272 lines) | stat: -rw-r--r-- 10,954 bytes parent folder | download | duplicates (3)
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
BEGIN {
    require '../../t/test.pl';
    require '../../t/loc_tools.pl'; # to find locales
}

use XS::APItest;
use Config;

skip_all("locales not available") unless locales_enabled();

my @locales = eval { find_locales( &LC_NUMERIC ) };

if (@locales) {
    use POSIX;
    no warnings;
    use warnings 'locale';
    my $warning = "";
    local $SIG{__WARN__} = sub { $warning = shift; };
                   # Choose a number unlikely to be a legal category
    ok(! setlocale(1114112, $locales[0]),
                   "Fails to set an illegal category to a legal locale");
    like($warning, qr/Unknown locale category/i,
         "And warns about the illegal category, using the proper warning"
       . " category");
}

my $comma_locale;
for my $locale (@locales) {
    use POSIX;
    use locale;
    setlocale(LC_NUMERIC, $locale) or next;
    my $in = 4.2; # avoid any constant folding bugs
    my $s = sprintf("%g", $in);
    if ($s eq "4,2")  {
        $comma_locale = $locale;
        last;
    }
}

SKIP: {
          if ($Config{usequadmath}) {
              skip "no gconvert with usequadmath", 2;
          }
          is(test_Gconvert(4.179, 2), "4.2", "Gconvert doesn't recognize underlying locale outside 'use locale'");
          use locale;
          is(test_Gconvert(4.179, 2), "4.2", "Gconvert doesn't recognize underlying locale inside 'use locale'");
      }

sub check_in_bounds($$$) {
    my ($value, $lower, $upper) = @_;

    $value >= $lower && $value <= $upper
}

SKIP: {
    # This checks that when switching to the global locale, the service that
    # Perl provides of transparently dealing with locales that have a non-dot
    # radix is turned off, but gets turned on again after a sync_locale();

    skip "no locale with a comma radix available", 5 unless $comma_locale;

    my $global_locale = switch_to_global_and_setlocale(LC_NUMERIC,
                                                       $comma_locale);
    # Can't do a compare of $global_locale and $comma_locale because what the
    # system returns may be an alias.  ALl we can do is test for
    # success/failure
    if (ok($global_locale, "Successfully switched to $comma_locale")) {
        is(newSvNV("4.888"), 4,
           "dot not recognized in global comma locale for SvNV");

        no warnings 'numeric';  # Otherwise get "Argument isn't numeric in
                                # subroutine entry"

        is(check_in_bounds(newSvNV("4,888"), 4.88, 4.89), 1,
           "comma recognized in global comma locale for SvNV");
        isnt(sync_locale, 0,
             "sync_locale() returns that was in the global locale");

        is(check_in_bounds(newSvNV("4.888"), 4.88, 4.89), 1,
        "dot recognized in perl-controlled comma locale for SvNV");
    }
    else {
        skip "Couldn't switch to $comma_locale", 4;
    }
}

my %correct_C_responses = (
        # Entries that are undef could have varying returns
                            CODESET => undef,
                            CRNCYSTR => undef,
                            NOEXPR => undef,
                            NOSTR => undef,
                            RADIXCHAR => '.',
                            THOUSEP => '',
                            YESEXPR => undef,
                            YESSTR => undef,
                            ABDAY_1 => 'Sun',
                            ABDAY_2 => 'Mon',
                            ABDAY_3 => 'Tue',
                            ABDAY_4 => 'Wed',
                            ABDAY_5 => 'Thu',
                            ABDAY_6 => 'Fri',
                            ABDAY_7 => 'Sat',
                            ABMON_1 => 'Jan',
                            ABMON_2 => 'Feb',
                            ABMON_3 => 'Mar',
                            ABMON_4 => 'Apr',
                            ABMON_5 => 'May',
                            ABMON_6 => 'Jun',
                            ABMON_7 => 'Jul',
                            ABMON_8 => 'Aug',
                            ABMON_9 => 'Sep',
                            ABMON_10 => 'Oct',
                            ABMON_11 => 'Nov',
                            ABMON_12 => 'Dec',
                            ALT_DIGITS => undef,
                            AM_STR => 'AM',
                            CODESET => undef,
                            CRNCYSTR => undef,
                            DAY_1 => 'Sunday',
                            DAY_2 => 'Monday',
                            DAY_3 => 'Tuesday',
                            DAY_4 => 'Wednesday',
                            DAY_5 => 'Thursday',
                            DAY_6 => 'Friday',
                            DAY_7 => 'Saturday',
                            D_FMT => undef,
                            D_T_FMT => undef,
                            ERA => '',
                            ERA_D_FMT => undef,
                            ERA_D_T_FMT => undef,
                            ERA_T_FMT => undef,
                            MON_1 => 'January',
                            MON_2 => 'February',
                            MON_3 => 'March',
                            MON_4 => 'April',
                            MON_5 => 'May',
                            MON_6 => 'June',
                            MON_7 => 'July',
                            MON_8 => 'August',
                            MON_9 => 'September',
                            MON_10 => 'October',
                            MON_11 => 'November',
                            MON_12 => 'December',
                            NOEXPR => undef,
                            NOSTR => undef,
                            PM_STR => 'PM',
                            RADIXCHAR => '.',
                            THOUSEP => '',
                            T_FMT => undef,
                            T_FMT_AMPM => undef,
                            YESEXPR => undef,
                            YESSTR => undef,
                            _NL_ADDRESS_POSTAL_FMT => undef,
                            _NL_ADDRESS_COUNTRY_NAME => undef,
                            _NL_ADDRESS_COUNTRY_POST => undef,
                            _NL_ADDRESS_COUNTRY_AB2 => undef,
                            _NL_ADDRESS_COUNTRY_AB3 => undef,
                            _NL_ADDRESS_COUNTRY_CAR => undef,
                            _NL_ADDRESS_COUNTRY_NUM => 0,
                            _NL_ADDRESS_COUNTRY_ISBN => undef,
                            _NL_ADDRESS_LANG_NAME => undef,
                            _NL_ADDRESS_LANG_AB => undef,
                            _NL_ADDRESS_LANG_TERM => undef,
                            _NL_ADDRESS_LANG_LIB => undef,
                            _NL_IDENTIFICATION_TITLE => undef,
                            _NL_IDENTIFICATION_SOURCE => undef,
                            _NL_IDENTIFICATION_ADDRESS => undef,
                            _NL_IDENTIFICATION_CONTACT => undef,
                            _NL_IDENTIFICATION_EMAIL => undef,
                            _NL_IDENTIFICATION_TEL => undef,
                            _NL_IDENTIFICATION_FAX => undef,
                            _NL_IDENTIFICATION_LANGUAGE => undef,
                            _NL_IDENTIFICATION_TERRITORY => "ISO",
                            _NL_IDENTIFICATION_AUDIENCE => undef,
                            _NL_IDENTIFICATION_APPLICATION => undef,
                            _NL_IDENTIFICATION_ABBREVIATION => undef,
                            _NL_IDENTIFICATION_REVISION => undef,
                            _NL_IDENTIFICATION_DATE => undef,
                            _NL_IDENTIFICATION_CATEGORY => undef,
                            _NL_MEASUREMENT_MEASUREMENT => undef,
                            _NL_NAME_NAME_FMT => undef,
                            _NL_NAME_NAME_GEN => undef,
                            _NL_NAME_NAME_MR => undef,
                            _NL_NAME_NAME_MRS => undef,
                            _NL_NAME_NAME_MISS => undef,
                            _NL_NAME_NAME_MS => undef,
                            _NL_PAPER_HEIGHT => undef,
                            _NL_PAPER_WIDTH => undef,
                            _NL_TELEPHONE_TEL_INT_FMT => undef,
                            _NL_TELEPHONE_TEL_DOM_FMT => undef,
                            _NL_TELEPHONE_INT_SELECT => undef,
                            _NL_TELEPHONE_INT_PREFIX => undef,
                        );

my $hdr = "../../perl_langinfo.h";
open my $fh, "<", $hdr;
$|=1;

SKIP: {
    skip "No LC_ALL", 1 unless locales_enabled('LC_ALL');

    use POSIX;
    setlocale(LC_ALL, "C");
    eval "use I18N::Langinfo qw(langinfo RADIXCHAR); langinfo(RADIXCHAR)";
    my $has_nl_langinfo = $@ eq "";

    skip "Can't open $hdr for reading: $!", 1 unless $fh;

    my %items;

    # Find all the current items from the header, and their values.
    # For non-nl_langinfo systems, those values are arbitrary negative numbers
    # set in the header.  Otherwise they are the nl_langinfo approved values,
    # which for the moment is the item name.
    # The relevant lines look like: #  define YESSTR -54
    while (<$fh>) {
        chomp;
        next unless / - \d+ $ /x;
        s/ ^ \# \s* define \s*//x;
        m/ (\S+) \s+ (.*) /x;
        $items{$1} = ($has_nl_langinfo)
                     ? $1       # Yields 'YESSTR'
                     : $2;      # Yields -54
    }

    # Get the translation from item name to numeric value.
    I18N::Langinfo->import(keys %items) if $has_nl_langinfo;

    foreach my $formal_item (sort keys %items) {
      SKIP:
        if (exists $correct_C_responses{$formal_item}) {
            my $correct = $correct_C_responses{$formal_item};
            my $item = eval $items{$formal_item};
            skip "This platform apparently doesn't support $formal_item", 1 if $@;
            my $result = test_Perl_langinfo($item);
            if (defined $correct) {
                is ($result, $correct,
                    "Returns expected value" . "('$correct') for $formal_item");
            }
            elsif (defined $result) {
                pass("Returns a value (in this case '$result') for $formal_item");
            }
            else {
                fail("Returned undef for $formal_item");
            }
        }
    }
}

@locales = eval { find_locales( &LC_TIME ) };

SKIP: {
    skip("no LC_TIME locales available") unless @locales;

    for my $locale (@locales) {
        use POSIX 'strftime';
        use locale;
        setlocale(LC_TIME, $locale) or next;

        # This isn't guaranteed to find failing locales, as it is impractical
        # to test all possible dates.  But it is much better than no test at
        # all
        if (strftime('%c', 0, 0, , 12, 18, 11, 87) eq "") {
            fail('strftime() built-in expansion factor works for all locales');
            diag("Failed for locale $locale");
            last;
        }
    }
}

done_testing();