lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Test program for nl_langinfo() function. |
| 2 | Copyright (C) 2000-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@cygnus.com>. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <langinfo.h> |
| 21 | #include <locale.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | |
| 26 | struct map |
| 27 | { |
| 28 | const char *str; |
| 29 | int val; |
| 30 | } map[] = |
| 31 | { |
| 32 | #define VAL(name) { #name, name } |
| 33 | VAL (ABDAY_1), |
| 34 | VAL (ABDAY_2), |
| 35 | VAL (ABDAY_3), |
| 36 | VAL (ABDAY_4), |
| 37 | VAL (ABDAY_5), |
| 38 | VAL (ABDAY_6), |
| 39 | VAL (ABDAY_7), |
| 40 | VAL (ABMON_1), |
| 41 | VAL (ABMON_10), |
| 42 | VAL (ABMON_11), |
| 43 | VAL (ABMON_12), |
| 44 | VAL (ABMON_2), |
| 45 | VAL (ABMON_3), |
| 46 | VAL (ABMON_4), |
| 47 | VAL (ABMON_5), |
| 48 | VAL (ABMON_6), |
| 49 | VAL (ABMON_7), |
| 50 | VAL (ABMON_8), |
| 51 | VAL (ABMON_9), |
| 52 | VAL (ALT_DIGITS), |
| 53 | VAL (AM_STR), |
| 54 | VAL (CRNCYSTR), |
| 55 | VAL (CURRENCY_SYMBOL), |
| 56 | VAL (DAY_1), |
| 57 | VAL (DAY_2), |
| 58 | VAL (DAY_3), |
| 59 | VAL (DAY_4), |
| 60 | VAL (DAY_5), |
| 61 | VAL (DAY_6), |
| 62 | VAL (DAY_7), |
| 63 | VAL (DECIMAL_POINT), |
| 64 | VAL (D_FMT), |
| 65 | VAL (D_T_FMT), |
| 66 | VAL (ERA), |
| 67 | VAL (ERA_D_FMT), |
| 68 | VAL (ERA_D_T_FMT), |
| 69 | VAL (ERA_T_FMT), |
| 70 | VAL (ERA_YEAR), |
| 71 | VAL (FRAC_DIGITS), |
| 72 | VAL (GROUPING), |
| 73 | VAL (INT_CURR_SYMBOL), |
| 74 | VAL (INT_FRAC_DIGITS), |
| 75 | VAL (MON_1), |
| 76 | VAL (MON_10), |
| 77 | VAL (MON_11), |
| 78 | VAL (MON_12), |
| 79 | VAL (MON_2), |
| 80 | VAL (MON_3), |
| 81 | VAL (MON_4), |
| 82 | VAL (MON_5), |
| 83 | VAL (MON_6), |
| 84 | VAL (MON_7), |
| 85 | VAL (MON_8), |
| 86 | VAL (MON_9), |
| 87 | VAL (MON_DECIMAL_POINT), |
| 88 | VAL (MON_GROUPING), |
| 89 | VAL (MON_THOUSANDS_SEP), |
| 90 | VAL (NEGATIVE_SIGN), |
| 91 | VAL (NOEXPR), |
| 92 | VAL (NOSTR), |
| 93 | VAL (N_CS_PRECEDES), |
| 94 | VAL (N_SEP_BY_SPACE), |
| 95 | VAL (N_SIGN_POSN), |
| 96 | VAL (PM_STR), |
| 97 | VAL (POSITIVE_SIGN), |
| 98 | VAL (P_CS_PRECEDES), |
| 99 | VAL (P_SEP_BY_SPACE), |
| 100 | VAL (P_SIGN_POSN), |
| 101 | VAL (RADIXCHAR), |
| 102 | VAL (THOUSANDS_SEP), |
| 103 | VAL (THOUSEP), |
| 104 | VAL (T_FMT), |
| 105 | VAL (T_FMT_AMPM), |
| 106 | VAL (YESEXPR), |
| 107 | VAL (YESSTR) |
| 108 | }; |
| 109 | |
| 110 | |
| 111 | static int |
| 112 | map_paramstr (const char *str) |
| 113 | { |
| 114 | int low = 0; |
| 115 | int high = sizeof (map) / sizeof (map[0]); |
| 116 | |
| 117 | while (low < high) |
| 118 | { |
| 119 | int med = (low + high) / 2; |
| 120 | int cmpres; |
| 121 | |
| 122 | cmpres = strcmp (str, map[med].str); |
| 123 | if (cmpres == 0) |
| 124 | return map[med].val; |
| 125 | else if (cmpres > 0) |
| 126 | low = med + 1; |
| 127 | else |
| 128 | high = med; |
| 129 | } |
| 130 | |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | #ifdef DEBUG |
| 136 | # define REASON(str) printf ("\"%s\" ignored: %s\n", buf, str) |
| 137 | #else |
| 138 | # define REASON(str) |
| 139 | #endif |
| 140 | |
| 141 | static int |
| 142 | do_test (void) |
| 143 | { |
| 144 | int result = 0; |
| 145 | |
| 146 | while (! feof (stdin)) |
| 147 | { |
| 148 | char buf[1000]; |
| 149 | char *rp; |
| 150 | char *locale; |
| 151 | char *paramstr; |
| 152 | char *expected; |
| 153 | char *actual; |
| 154 | int param; |
| 155 | |
| 156 | if (fgets (buf, sizeof (buf), stdin) == NULL) |
| 157 | break; |
| 158 | |
| 159 | /* Split the fields. There are three is them: |
| 160 | 1. locale |
| 161 | 2. langinfo() parameter |
| 162 | 3. expected result; this can be a string with white space etc. |
| 163 | */ |
| 164 | rp = buf; |
| 165 | while (*rp == ' ' || *rp == '\t') |
| 166 | ++rp; |
| 167 | |
| 168 | if (*rp == '#') |
| 169 | { |
| 170 | /* It's a comment line. Ignore it. */ |
| 171 | REASON ("comment"); |
| 172 | continue; |
| 173 | } |
| 174 | locale = rp; |
| 175 | |
| 176 | while (*rp != '\0' && *rp != ' ' && *rp != '\t' && *rp != '\n') |
| 177 | ++rp; |
| 178 | if (*rp == '\0' || *rp == '\n') |
| 179 | { |
| 180 | /* Incomplete line. */ |
| 181 | REASON ("incomplete line"); |
| 182 | continue; |
| 183 | } |
| 184 | *rp++ = '\0'; |
| 185 | |
| 186 | while (*rp == ' ' || *rp == '\t') |
| 187 | ++rp; |
| 188 | paramstr = rp; |
| 189 | |
| 190 | while (*rp != '\0' && *rp != ' ' && *rp != '\t' && *rp != '\n') |
| 191 | ++rp; |
| 192 | if (*rp == '\0' || *rp == '\n') |
| 193 | { |
| 194 | /* Incomplete line. */ |
| 195 | REASON ("incomplete line"); |
| 196 | continue; |
| 197 | } |
| 198 | *rp++ = '\0'; |
| 199 | |
| 200 | while (*rp == ' ' || *rp == '\t') |
| 201 | ++rp; |
| 202 | |
| 203 | if (*rp == '"') |
| 204 | { |
| 205 | char *wp; |
| 206 | |
| 207 | expected = wp = ++rp; |
| 208 | while (*rp != '"' && *rp != '\n' && *rp != '\0') |
| 209 | { |
| 210 | if (*rp == '\\') |
| 211 | { |
| 212 | ++rp; |
| 213 | if (*rp == '\0') |
| 214 | break; |
| 215 | if (*rp >= '0' && *rp <= '9') |
| 216 | { |
| 217 | int val = *rp - '0'; |
| 218 | if (rp[1] >= '0' && rp[1] <= '9') |
| 219 | { |
| 220 | ++rp; |
| 221 | val *= 10; |
| 222 | val += *rp - '0'; |
| 223 | if (rp[1] >= '0' && rp[1] <= '9') |
| 224 | { |
| 225 | ++rp; |
| 226 | val *= 10; |
| 227 | val += *rp - '0'; |
| 228 | } |
| 229 | } |
| 230 | *rp = val; |
| 231 | } |
| 232 | } |
| 233 | *wp++ = *rp++; |
| 234 | } |
| 235 | |
| 236 | if (*rp != '"') |
| 237 | { |
| 238 | REASON ("missing '\"'"); |
| 239 | continue; |
| 240 | } |
| 241 | |
| 242 | *wp = '\0'; |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | expected = rp; |
| 247 | while (*rp != '\0' && *rp != '\n') |
| 248 | ++rp; |
| 249 | *rp = '\0'; |
| 250 | } |
| 251 | |
| 252 | param = map_paramstr (paramstr); |
| 253 | if (param == -1) |
| 254 | { |
| 255 | /* Invalid parameter. */ |
| 256 | REASON ("invalid parameter"); |
| 257 | continue; |
| 258 | } |
| 259 | |
| 260 | /* Set the locale and check whether it worked. */ |
| 261 | printf ("LC_ALL=%s nl_langinfo(%s)", locale, paramstr); |
| 262 | setlocale (LC_ALL, locale); |
| 263 | if (strcmp (locale, setlocale (LC_ALL, NULL)) != 0) |
| 264 | { |
| 265 | puts (": failed to set locale"); |
| 266 | result = 1; |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | actual = nl_langinfo (param); |
| 271 | printf (" = \"%s\", ", actual); |
| 272 | |
| 273 | if (strcmp (actual, expected) == 0) |
| 274 | puts ("OK"); |
| 275 | else |
| 276 | { |
| 277 | printf ("FAILED (expected: %s)\n", expected); |
| 278 | result = 1; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return result; |
| 283 | } |
| 284 | |
| 285 | #define TEST_FUNCTION do_test () |
| 286 | #include "../test-skeleton.c" |