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