NPTL: Remove gratuitous Linuxisms from gai_misc.h.
[glibc.git] / locale / tst-C-locale.c
blobafe4dc9a88ee4f6262d61876f71c7a9efd24c542
1 /* Tests of C and POSIX locale contents.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
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.
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.
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/>. */
20 #include <ctype.h>
21 #include <langinfo.h>
22 #include <limits.h>
23 #include <locale.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <wchar.h>
27 #include <wctype.h>
30 static int
31 run_test (const char *locname)
33 struct lconv *lc;
34 const char *str;
35 const wchar_t *wstr;
36 int result = 0;
37 locale_t loc;
39 /* ISO C stuff. */
40 lc = localeconv ();
41 if (lc == NULL)
43 printf ("localeconv failed for locale %s\n", locname);
44 result = 1;
46 else
48 #define STRTEST(name, exp) \
49 do \
50 if (strcmp (lc->name, exp) != 0) \
51 { \
52 printf (#name " in locale %s wrong (is \"%s\", should be \"%s\")\n",\
53 locname, lc->name, exp); \
54 result = 1; \
55 } \
56 while (0)
57 STRTEST (decimal_point, ".");
58 STRTEST (thousands_sep, "");
59 STRTEST (grouping, "");
60 STRTEST (mon_decimal_point, "");
61 STRTEST (mon_thousands_sep, "");
62 STRTEST (mon_grouping, "");
63 STRTEST (positive_sign, "");
64 STRTEST (negative_sign, "");
65 STRTEST (currency_symbol, "");
66 STRTEST (int_curr_symbol, "");
68 #define CHARTEST(name, exp) \
69 do \
70 if (lc->name != exp) \
71 { \
72 printf (#name " in locale %s wrong (is %d, should be %d)\n", \
73 locname, lc->name, CHAR_MAX); \
74 result = 1; \
75 } \
76 while (0)
77 CHARTEST (frac_digits, CHAR_MAX);
78 CHARTEST (p_cs_precedes, CHAR_MAX);
79 CHARTEST (n_cs_precedes, CHAR_MAX);
80 CHARTEST (p_sep_by_space, CHAR_MAX);
81 CHARTEST (n_sep_by_space, CHAR_MAX);
82 CHARTEST (p_sign_posn, CHAR_MAX);
83 CHARTEST (n_sign_posn, CHAR_MAX);
84 CHARTEST (int_frac_digits, CHAR_MAX);
85 CHARTEST (int_p_cs_precedes, CHAR_MAX);
86 CHARTEST (int_n_cs_precedes, CHAR_MAX);
87 CHARTEST (int_p_sep_by_space, CHAR_MAX);
88 CHARTEST (int_n_sep_by_space, CHAR_MAX);
89 CHARTEST (int_p_sign_posn, CHAR_MAX);
90 CHARTEST (int_n_sign_posn, CHAR_MAX);
93 #undef STRTEST
94 #define STRTEST(name, exp) \
95 str = nl_langinfo (name); \
96 if (strcmp (str, exp) != 0) \
97 { \
98 printf ("nl_langinfo(" #name ") in locale %s wrong " \
99 "(is \"%s\", should be \"%s\")\n", locname, str, exp); \
100 result = 1; \
102 #define WSTRTEST(name, exp) \
103 wstr = (wchar_t *) nl_langinfo (name); \
104 if (wcscmp (wstr, exp) != 0) \
106 printf ("nl_langinfo(" #name ") in locale %s wrong " \
107 "(is \"%S\", should be \"%S\")\n", locname, wstr, exp); \
108 result = 1; \
111 /* Unix stuff. */
112 STRTEST (ABDAY_1, "Sun");
113 STRTEST (ABDAY_2, "Mon");
114 STRTEST (ABDAY_3, "Tue");
115 STRTEST (ABDAY_4, "Wed");
116 STRTEST (ABDAY_5, "Thu");
117 STRTEST (ABDAY_6, "Fri");
118 STRTEST (ABDAY_7, "Sat");
119 STRTEST (DAY_1, "Sunday");
120 STRTEST (DAY_2, "Monday");
121 STRTEST (DAY_3, "Tuesday");
122 STRTEST (DAY_4, "Wednesday");
123 STRTEST (DAY_5, "Thursday");
124 STRTEST (DAY_6, "Friday");
125 STRTEST (DAY_7, "Saturday");
126 STRTEST (ABMON_1, "Jan");
127 STRTEST (ABMON_2, "Feb");
128 STRTEST (ABMON_3, "Mar");
129 STRTEST (ABMON_4, "Apr");
130 STRTEST (ABMON_5, "May");
131 STRTEST (ABMON_6, "Jun");
132 STRTEST (ABMON_7, "Jul");
133 STRTEST (ABMON_8, "Aug");
134 STRTEST (ABMON_9, "Sep");
135 STRTEST (ABMON_10, "Oct");
136 STRTEST (ABMON_11, "Nov");
137 STRTEST (ABMON_12, "Dec");
138 STRTEST (MON_1, "January");
139 STRTEST (MON_2, "February");
140 STRTEST (MON_3, "March");
141 STRTEST (MON_4, "April");
142 STRTEST (MON_5, "May");
143 STRTEST (MON_6, "June");
144 STRTEST (MON_7, "July");
145 STRTEST (MON_8, "August");
146 STRTEST (MON_9, "September");
147 STRTEST (MON_10, "October");
148 STRTEST (MON_11, "November");
149 STRTEST (MON_12, "December");
150 STRTEST (AM_STR, "AM");
151 STRTEST (PM_STR, "PM");
152 STRTEST (D_T_FMT, "%a %b %e %H:%M:%S %Y");
153 STRTEST (D_FMT, "%m/%d/%y");
154 STRTEST (T_FMT, "%H:%M:%S");
155 STRTEST (T_FMT_AMPM, "%I:%M:%S %p");
156 STRTEST (ERA, "");
157 STRTEST (ERA_D_FMT, "");
158 STRTEST (ERA_T_FMT, "");
159 STRTEST (ERA_D_T_FMT, "");
160 STRTEST (ALT_DIGITS, "");
162 STRTEST (RADIXCHAR, ".");
163 STRTEST (THOUSEP, "");
165 STRTEST (YESEXPR, "^[yY]");
166 STRTEST (NOEXPR, "^[nN]");
168 /* Extensions. */
169 WSTRTEST (_NL_WABDAY_1, L"Sun");
170 WSTRTEST (_NL_WABDAY_2, L"Mon");
171 WSTRTEST (_NL_WABDAY_3, L"Tue");
172 WSTRTEST (_NL_WABDAY_4, L"Wed");
173 WSTRTEST (_NL_WABDAY_5, L"Thu");
174 WSTRTEST (_NL_WABDAY_6, L"Fri");
175 WSTRTEST (_NL_WABDAY_7, L"Sat");
176 WSTRTEST (_NL_WDAY_1, L"Sunday");
177 WSTRTEST (_NL_WDAY_2, L"Monday");
178 WSTRTEST (_NL_WDAY_3, L"Tuesday");
179 WSTRTEST (_NL_WDAY_4, L"Wednesday");
180 WSTRTEST (_NL_WDAY_5, L"Thursday");
181 WSTRTEST (_NL_WDAY_6, L"Friday");
182 WSTRTEST (_NL_WDAY_7, L"Saturday");
183 WSTRTEST (_NL_WABMON_1, L"Jan");
184 WSTRTEST (_NL_WABMON_2, L"Feb");
185 WSTRTEST (_NL_WABMON_3, L"Mar");
186 WSTRTEST (_NL_WABMON_4, L"Apr");
187 WSTRTEST (_NL_WABMON_5, L"May");
188 WSTRTEST (_NL_WABMON_6, L"Jun");
189 WSTRTEST (_NL_WABMON_7, L"Jul");
190 WSTRTEST (_NL_WABMON_8, L"Aug");
191 WSTRTEST (_NL_WABMON_9, L"Sep");
192 WSTRTEST (_NL_WABMON_10, L"Oct");
193 WSTRTEST (_NL_WABMON_11, L"Nov");
194 WSTRTEST (_NL_WABMON_12, L"Dec");
195 WSTRTEST (_NL_WMON_1, L"January");
196 WSTRTEST (_NL_WMON_2, L"February");
197 WSTRTEST (_NL_WMON_3, L"March");
198 WSTRTEST (_NL_WMON_4, L"April");
199 WSTRTEST (_NL_WMON_5, L"May");
200 WSTRTEST (_NL_WMON_6, L"June");
201 WSTRTEST (_NL_WMON_7, L"July");
202 WSTRTEST (_NL_WMON_8, L"August");
203 WSTRTEST (_NL_WMON_9, L"September");
204 WSTRTEST (_NL_WMON_10, L"October");
205 WSTRTEST (_NL_WMON_11, L"November");
206 WSTRTEST (_NL_WMON_12, L"December");
207 WSTRTEST (_NL_WAM_STR, L"AM");
208 WSTRTEST (_NL_WPM_STR, L"PM");
209 WSTRTEST (_NL_WD_T_FMT, L"%a %b %e %H:%M:%S %Y");
210 WSTRTEST (_NL_WD_FMT, L"%m/%d/%y");
211 WSTRTEST (_NL_WT_FMT, L"%H:%M:%S");
212 WSTRTEST (_NL_WT_FMT_AMPM, L"%I:%M:%S %p");
213 WSTRTEST (_NL_WERA_D_FMT, L"");
214 WSTRTEST (_NL_WERA_T_FMT, L"");
215 WSTRTEST (_NL_WERA_D_T_FMT, L"");
216 WSTRTEST (_NL_WALT_DIGITS, L"");
218 STRTEST (_DATE_FMT, "%a %b %e %H:%M:%S %Z %Y");
219 WSTRTEST (_NL_W_DATE_FMT, L"%a %b %e %H:%M:%S %Z %Y");
221 STRTEST (INT_CURR_SYMBOL, "");
222 STRTEST (CURRENCY_SYMBOL, "");
223 STRTEST (MON_DECIMAL_POINT, "");
224 STRTEST (MON_THOUSANDS_SEP, "");
225 STRTEST (MON_GROUPING, "");
226 STRTEST (POSITIVE_SIGN, "");
227 STRTEST (NEGATIVE_SIGN, "");
228 STRTEST (GROUPING, "");
230 STRTEST (YESSTR, "");
231 STRTEST (NOSTR, "");
233 /* Test the new locale mechanisms. */
234 loc = newlocale (LC_ALL_MASK, locname, NULL);
235 if (loc == NULL)
237 printf ("cannot create locale object for locale %s\n", locname);
238 result = 1;
240 else
242 int c;
244 #undef STRTEST
245 #define STRTEST(name, exp) \
246 str = nl_langinfo_l (name, loc); \
247 if (strcmp (str, exp) != 0) \
249 printf ("nl_langinfo_l(" #name ") in locale %s wrong " \
250 "(is \"%s\", should be \"%s\")\n", locname, str, exp); \
251 result = 1; \
253 #undef WSTRTEST
254 #define WSTRTEST(name, exp) \
255 wstr = (wchar_t *) nl_langinfo_l (name, loc); \
256 if (wcscmp (wstr, exp) != 0) \
258 printf ("nl_langinfo_l(" #name ") in locale %s wrong " \
259 "(is \"%S\", should be \"%S\")\n", locname, wstr, exp); \
260 result = 1; \
263 /* Unix stuff. */
264 STRTEST (ABDAY_1, "Sun");
265 STRTEST (ABDAY_2, "Mon");
266 STRTEST (ABDAY_3, "Tue");
267 STRTEST (ABDAY_4, "Wed");
268 STRTEST (ABDAY_5, "Thu");
269 STRTEST (ABDAY_6, "Fri");
270 STRTEST (ABDAY_7, "Sat");
271 STRTEST (DAY_1, "Sunday");
272 STRTEST (DAY_2, "Monday");
273 STRTEST (DAY_3, "Tuesday");
274 STRTEST (DAY_4, "Wednesday");
275 STRTEST (DAY_5, "Thursday");
276 STRTEST (DAY_6, "Friday");
277 STRTEST (DAY_7, "Saturday");
278 STRTEST (ABMON_1, "Jan");
279 STRTEST (ABMON_2, "Feb");
280 STRTEST (ABMON_3, "Mar");
281 STRTEST (ABMON_4, "Apr");
282 STRTEST (ABMON_5, "May");
283 STRTEST (ABMON_6, "Jun");
284 STRTEST (ABMON_7, "Jul");
285 STRTEST (ABMON_8, "Aug");
286 STRTEST (ABMON_9, "Sep");
287 STRTEST (ABMON_10, "Oct");
288 STRTEST (ABMON_11, "Nov");
289 STRTEST (ABMON_12, "Dec");
290 STRTEST (MON_1, "January");
291 STRTEST (MON_2, "February");
292 STRTEST (MON_3, "March");
293 STRTEST (MON_4, "April");
294 STRTEST (MON_5, "May");
295 STRTEST (MON_6, "June");
296 STRTEST (MON_7, "July");
297 STRTEST (MON_8, "August");
298 STRTEST (MON_9, "September");
299 STRTEST (MON_10, "October");
300 STRTEST (MON_11, "November");
301 STRTEST (MON_12, "December");
302 STRTEST (AM_STR, "AM");
303 STRTEST (PM_STR, "PM");
304 STRTEST (D_T_FMT, "%a %b %e %H:%M:%S %Y");
305 STRTEST (D_FMT, "%m/%d/%y");
306 STRTEST (T_FMT, "%H:%M:%S");
307 STRTEST (T_FMT_AMPM, "%I:%M:%S %p");
308 STRTEST (ERA, "");
309 STRTEST (ERA_D_FMT, "");
310 STRTEST (ERA_T_FMT, "");
311 STRTEST (ERA_D_T_FMT, "");
312 STRTEST (ALT_DIGITS, "");
314 STRTEST (RADIXCHAR, ".");
315 STRTEST (THOUSEP, "");
317 STRTEST (YESEXPR, "^[yY]");
318 STRTEST (NOEXPR, "^[nN]");
320 /* Extensions. */
321 WSTRTEST (_NL_WABDAY_1, L"Sun");
322 WSTRTEST (_NL_WABDAY_2, L"Mon");
323 WSTRTEST (_NL_WABDAY_3, L"Tue");
324 WSTRTEST (_NL_WABDAY_4, L"Wed");
325 WSTRTEST (_NL_WABDAY_5, L"Thu");
326 WSTRTEST (_NL_WABDAY_6, L"Fri");
327 WSTRTEST (_NL_WABDAY_7, L"Sat");
328 WSTRTEST (_NL_WDAY_1, L"Sunday");
329 WSTRTEST (_NL_WDAY_2, L"Monday");
330 WSTRTEST (_NL_WDAY_3, L"Tuesday");
331 WSTRTEST (_NL_WDAY_4, L"Wednesday");
332 WSTRTEST (_NL_WDAY_5, L"Thursday");
333 WSTRTEST (_NL_WDAY_6, L"Friday");
334 WSTRTEST (_NL_WDAY_7, L"Saturday");
335 WSTRTEST (_NL_WABMON_1, L"Jan");
336 WSTRTEST (_NL_WABMON_2, L"Feb");
337 WSTRTEST (_NL_WABMON_3, L"Mar");
338 WSTRTEST (_NL_WABMON_4, L"Apr");
339 WSTRTEST (_NL_WABMON_5, L"May");
340 WSTRTEST (_NL_WABMON_6, L"Jun");
341 WSTRTEST (_NL_WABMON_7, L"Jul");
342 WSTRTEST (_NL_WABMON_8, L"Aug");
343 WSTRTEST (_NL_WABMON_9, L"Sep");
344 WSTRTEST (_NL_WABMON_10, L"Oct");
345 WSTRTEST (_NL_WABMON_11, L"Nov");
346 WSTRTEST (_NL_WABMON_12, L"Dec");
347 WSTRTEST (_NL_WMON_1, L"January");
348 WSTRTEST (_NL_WMON_2, L"February");
349 WSTRTEST (_NL_WMON_3, L"March");
350 WSTRTEST (_NL_WMON_4, L"April");
351 WSTRTEST (_NL_WMON_5, L"May");
352 WSTRTEST (_NL_WMON_6, L"June");
353 WSTRTEST (_NL_WMON_7, L"July");
354 WSTRTEST (_NL_WMON_8, L"August");
355 WSTRTEST (_NL_WMON_9, L"September");
356 WSTRTEST (_NL_WMON_10, L"October");
357 WSTRTEST (_NL_WMON_11, L"November");
358 WSTRTEST (_NL_WMON_12, L"December");
359 WSTRTEST (_NL_WAM_STR, L"AM");
360 WSTRTEST (_NL_WPM_STR, L"PM");
361 WSTRTEST (_NL_WD_T_FMT, L"%a %b %e %H:%M:%S %Y");
362 WSTRTEST (_NL_WD_FMT, L"%m/%d/%y");
363 WSTRTEST (_NL_WT_FMT, L"%H:%M:%S");
364 WSTRTEST (_NL_WT_FMT_AMPM, L"%I:%M:%S %p");
365 WSTRTEST (_NL_WERA_D_FMT, L"");
366 WSTRTEST (_NL_WERA_T_FMT, L"");
367 WSTRTEST (_NL_WERA_D_T_FMT, L"");
368 WSTRTEST (_NL_WALT_DIGITS, L"");
370 STRTEST (_DATE_FMT, "%a %b %e %H:%M:%S %Z %Y");
371 WSTRTEST (_NL_W_DATE_FMT, L"%a %b %e %H:%M:%S %Z %Y");
373 STRTEST (INT_CURR_SYMBOL, "");
374 STRTEST (CURRENCY_SYMBOL, "");
375 STRTEST (MON_DECIMAL_POINT, "");
376 STRTEST (MON_THOUSANDS_SEP, "");
377 STRTEST (MON_GROUPING, "");
378 STRTEST (POSITIVE_SIGN, "");
379 STRTEST (NEGATIVE_SIGN, "");
380 STRTEST (GROUPING, "");
382 STRTEST (YESSTR, "");
383 STRTEST (NOSTR, "");
385 /* Character class tests. */
386 for (c = 0; c < 128; ++c)
388 #define CLASSTEST(name) \
389 if (is##name (c) != is##name##_l (c, loc)) \
391 printf ("is%s('\\%o') != is%s_l('\\%o')\n", \
392 #name, c, #name, c); \
393 result = 1; \
395 CLASSTEST (alnum);
396 CLASSTEST (alpha);
397 CLASSTEST (blank);
398 CLASSTEST (cntrl);
399 CLASSTEST (digit);
400 CLASSTEST (lower);
401 CLASSTEST (graph);
402 CLASSTEST (print);
403 CLASSTEST (punct);
404 CLASSTEST (space);
405 CLASSTEST (upper);
406 CLASSTEST (xdigit);
408 /* Character mapping tests. */
409 #define MAPTEST(name) \
410 if (to##name (c) != to##name##_l (c, loc)) \
412 printf ("to%s('\\%o') != to%s_l('\\%o'): '\\%o' vs '\\%o'\n", \
413 #name, c, #name, c, \
414 to##name (c), to##name##_l (c, loc)); \
415 result = 1; \
417 MAPTEST (lower);
418 MAPTEST (upper);
421 /* Character class tests, this time for wide characters. Note that
422 this only works because we know that the internal encoding is
423 UCS4. */
424 for (c = 0; c < 128; ++c)
426 #undef CLASSTEST
427 #define CLASSTEST(name) \
428 if (isw##name (c) != isw##name##_l (c, loc)) \
430 printf ("isw%s('\\%o') != isw%s_l('\\%o')\n", \
431 #name, c, #name, c); \
432 result = 1; \
434 CLASSTEST (alnum);
435 CLASSTEST (alpha);
436 CLASSTEST (blank);
437 CLASSTEST (cntrl);
438 CLASSTEST (digit);
439 CLASSTEST (lower);
440 CLASSTEST (graph);
441 CLASSTEST (print);
442 CLASSTEST (punct);
443 CLASSTEST (space);
444 CLASSTEST (upper);
445 CLASSTEST (xdigit);
447 /* Character mapping tests. Note that
448 this only works because we know that the internal encoding is
449 UCS4. */
450 #undef MAPTEST
451 #define MAPTEST(name) \
452 if (tow##name (c) != tow##name##_l (c, loc)) \
454 printf ("tow%s('\\%o') != tow%s_l('\\%o'): '\\%o' vs '\\%o'\n",\
455 #name, c, #name, c, \
456 tow##name (c), tow##name##_l (c, loc)); \
457 result = 1; \
459 MAPTEST (lower);
460 MAPTEST (upper);
463 freelocale (loc);
466 return result;
470 static int
471 do_test (void)
473 int result;
475 /* First use the name "C". */
476 if (setlocale (LC_ALL, "C") == NULL)
478 puts ("cannot set C locale");
479 result = 1;
481 else
482 result = run_test ("C");
484 /* Then the name "POSIX". */
485 if (setlocale (LC_ALL, "POSIX") == NULL)
487 puts ("cannot set POSIX locale");
488 result = 1;
490 else
491 result |= run_test ("POSIX");
493 return result;
496 #define TEST_FUNCTION do_test ()
497 #include "../test-skeleton.c"