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