Back to index

glibc  2.9
tst-C-locale.c
Go to the documentation of this file.
00001 /* Tests of C and POSIX locale contents.
00002    Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <ctype.h>
00022 #include <langinfo.h>
00023 #include <limits.h>
00024 #include <locale.h>
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include <wchar.h>
00028 #include <wctype.h>
00029 
00030 
00031 static int
00032 run_test (const char *locname)
00033 {
00034   struct lconv *lc;
00035   const char *str;
00036   const wchar_t *wstr;
00037   int result = 0;
00038   locale_t loc;
00039 
00040   /* ISO C stuff.  */
00041   lc = localeconv ();
00042   if (lc == NULL)
00043     {
00044       printf ("localeconv failed for locale %s\n", locname);
00045       result = 1;
00046     }
00047   else
00048     {
00049 #define STRTEST(name, exp) \
00050       do                                                             \
00051        if (strcmp (lc->name, exp) != 0)                              \
00052          {                                                           \
00053            printf (#name " in locale %s wrong (is \"%s\", should be \"%s\")\n",\
00054                   locname, lc->name, exp);                                  \
00055            result = 1;                                                      \
00056          }                                                           \
00057       while (0)
00058       STRTEST (decimal_point, ".");
00059       STRTEST (thousands_sep, "");
00060       STRTEST (grouping, "");
00061       STRTEST (mon_decimal_point, "");
00062       STRTEST (mon_thousands_sep, "");
00063       STRTEST (mon_grouping, "");
00064       STRTEST (positive_sign, "");
00065       STRTEST (negative_sign, "");
00066       STRTEST (currency_symbol, "");
00067       STRTEST (int_curr_symbol, "");
00068 
00069 #define CHARTEST(name, exp) \
00070       do                                                             \
00071        if (lc->name != exp)                                          \
00072          {                                                           \
00073            printf (#name " in locale %s wrong (is %d, should be %d)\n",      \
00074                   locname, lc->name, CHAR_MAX);                      \
00075            result = 1;                                                      \
00076          }                                                           \
00077       while (0)
00078       CHARTEST (frac_digits, CHAR_MAX);
00079       CHARTEST (p_cs_precedes, CHAR_MAX);
00080       CHARTEST (n_cs_precedes, CHAR_MAX);
00081       CHARTEST (p_sep_by_space, CHAR_MAX);
00082       CHARTEST (n_sep_by_space, CHAR_MAX);
00083       CHARTEST (p_sign_posn, CHAR_MAX);
00084       CHARTEST (n_sign_posn, CHAR_MAX);
00085       CHARTEST (int_frac_digits, CHAR_MAX);
00086       CHARTEST (int_p_cs_precedes, CHAR_MAX);
00087       CHARTEST (int_n_cs_precedes, CHAR_MAX);
00088       CHARTEST (int_p_sep_by_space, CHAR_MAX);
00089       CHARTEST (int_n_sep_by_space, CHAR_MAX);
00090       CHARTEST (int_p_sign_posn, CHAR_MAX);
00091       CHARTEST (int_n_sign_posn, CHAR_MAX);
00092     }
00093 
00094 #undef STRTEST
00095 #define STRTEST(name, exp) \
00096   str = nl_langinfo (name);                                          \
00097   if (strcmp (str, exp) != 0)                                               \
00098     {                                                                \
00099       printf ("nl_langinfo(" #name ") in locale %s wrong "                  \
00100              "(is \"%s\", should be \"%s\")\n", locname, str, exp);         \
00101       result = 1;                                                    \
00102     }
00103 #define WSTRTEST(name, exp) \
00104   wstr = (wchar_t *) nl_langinfo (name);                             \
00105   if (wcscmp (wstr, exp) != 0)                                              \
00106     {                                                                \
00107       printf ("nl_langinfo(" #name ") in locale %s wrong "                  \
00108              "(is \"%S\", should be \"%S\")\n", locname, wstr, exp);        \
00109       result = 1;                                                    \
00110     }
00111 
00112   /* Unix stuff.  */
00113   STRTEST (ABDAY_1, "Sun");
00114   STRTEST (ABDAY_2, "Mon");
00115   STRTEST (ABDAY_3, "Tue");
00116   STRTEST (ABDAY_4, "Wed");
00117   STRTEST (ABDAY_5, "Thu");
00118   STRTEST (ABDAY_6, "Fri");
00119   STRTEST (ABDAY_7, "Sat");
00120   STRTEST (DAY_1, "Sunday");
00121   STRTEST (DAY_2, "Monday");
00122   STRTEST (DAY_3, "Tuesday");
00123   STRTEST (DAY_4, "Wednesday");
00124   STRTEST (DAY_5, "Thursday");
00125   STRTEST (DAY_6, "Friday");
00126   STRTEST (DAY_7, "Saturday");
00127   STRTEST (ABMON_1, "Jan");
00128   STRTEST (ABMON_2, "Feb");
00129   STRTEST (ABMON_3, "Mar");
00130   STRTEST (ABMON_4, "Apr");
00131   STRTEST (ABMON_5, "May");
00132   STRTEST (ABMON_6, "Jun");
00133   STRTEST (ABMON_7, "Jul");
00134   STRTEST (ABMON_8, "Aug");
00135   STRTEST (ABMON_9, "Sep");
00136   STRTEST (ABMON_10, "Oct");
00137   STRTEST (ABMON_11, "Nov");
00138   STRTEST (ABMON_12, "Dec");
00139   STRTEST (MON_1, "January");
00140   STRTEST (MON_2, "February");
00141   STRTEST (MON_3, "March");
00142   STRTEST (MON_4, "April");
00143   STRTEST (MON_5, "May");
00144   STRTEST (MON_6, "June");
00145   STRTEST (MON_7, "July");
00146   STRTEST (MON_8, "August");
00147   STRTEST (MON_9, "September");
00148   STRTEST (MON_10, "October");
00149   STRTEST (MON_11, "November");
00150   STRTEST (MON_12, "December");
00151   STRTEST (AM_STR, "AM");
00152   STRTEST (PM_STR, "PM");
00153   STRTEST (D_T_FMT, "%a %b %e %H:%M:%S %Y");
00154   STRTEST (D_FMT, "%m/%d/%y");
00155   STRTEST (T_FMT, "%H:%M:%S");
00156   STRTEST (T_FMT_AMPM, "%I:%M:%S %p");
00157   STRTEST (ERA, "");
00158   STRTEST (ERA_D_FMT, "");
00159   STRTEST (ERA_T_FMT, "");
00160   STRTEST (ERA_D_T_FMT, "");
00161   STRTEST (ALT_DIGITS, "");
00162 
00163   STRTEST (RADIXCHAR, ".");
00164   STRTEST (THOUSEP, "");
00165 
00166   STRTEST (YESEXPR, "^[yY]");
00167   STRTEST (NOEXPR, "^[nN]");
00168 
00169   /* Extensions.  */
00170   WSTRTEST (_NL_WABDAY_1, L"Sun");
00171   WSTRTEST (_NL_WABDAY_2, L"Mon");
00172   WSTRTEST (_NL_WABDAY_3, L"Tue");
00173   WSTRTEST (_NL_WABDAY_4, L"Wed");
00174   WSTRTEST (_NL_WABDAY_5, L"Thu");
00175   WSTRTEST (_NL_WABDAY_6, L"Fri");
00176   WSTRTEST (_NL_WABDAY_7, L"Sat");
00177   WSTRTEST (_NL_WDAY_1, L"Sunday");
00178   WSTRTEST (_NL_WDAY_2, L"Monday");
00179   WSTRTEST (_NL_WDAY_3, L"Tuesday");
00180   WSTRTEST (_NL_WDAY_4, L"Wednesday");
00181   WSTRTEST (_NL_WDAY_5, L"Thursday");
00182   WSTRTEST (_NL_WDAY_6, L"Friday");
00183   WSTRTEST (_NL_WDAY_7, L"Saturday");
00184   WSTRTEST (_NL_WABMON_1, L"Jan");
00185   WSTRTEST (_NL_WABMON_2, L"Feb");
00186   WSTRTEST (_NL_WABMON_3, L"Mar");
00187   WSTRTEST (_NL_WABMON_4, L"Apr");
00188   WSTRTEST (_NL_WABMON_5, L"May");
00189   WSTRTEST (_NL_WABMON_6, L"Jun");
00190   WSTRTEST (_NL_WABMON_7, L"Jul");
00191   WSTRTEST (_NL_WABMON_8, L"Aug");
00192   WSTRTEST (_NL_WABMON_9, L"Sep");
00193   WSTRTEST (_NL_WABMON_10, L"Oct");
00194   WSTRTEST (_NL_WABMON_11, L"Nov");
00195   WSTRTEST (_NL_WABMON_12, L"Dec");
00196   WSTRTEST (_NL_WMON_1, L"January");
00197   WSTRTEST (_NL_WMON_2, L"February");
00198   WSTRTEST (_NL_WMON_3, L"March");
00199   WSTRTEST (_NL_WMON_4, L"April");
00200   WSTRTEST (_NL_WMON_5, L"May");
00201   WSTRTEST (_NL_WMON_6, L"June");
00202   WSTRTEST (_NL_WMON_7, L"July");
00203   WSTRTEST (_NL_WMON_8, L"August");
00204   WSTRTEST (_NL_WMON_9, L"September");
00205   WSTRTEST (_NL_WMON_10, L"October");
00206   WSTRTEST (_NL_WMON_11, L"November");
00207   WSTRTEST (_NL_WMON_12, L"December");
00208   WSTRTEST (_NL_WAM_STR, L"AM");
00209   WSTRTEST (_NL_WPM_STR, L"PM");
00210   WSTRTEST (_NL_WD_T_FMT, L"%a %b %e %H:%M:%S %Y");
00211   WSTRTEST (_NL_WD_FMT, L"%m/%d/%y");
00212   WSTRTEST (_NL_WT_FMT, L"%H:%M:%S");
00213   WSTRTEST (_NL_WT_FMT_AMPM, L"%I:%M:%S %p");
00214   WSTRTEST (_NL_WERA_D_FMT, L"");
00215   WSTRTEST (_NL_WERA_T_FMT, L"");
00216   WSTRTEST (_NL_WERA_D_T_FMT, L"");
00217   WSTRTEST (_NL_WALT_DIGITS, L"");
00218 
00219   STRTEST (_DATE_FMT, "%a %b %e %H:%M:%S %Z %Y");
00220   WSTRTEST (_NL_W_DATE_FMT, L"%a %b %e %H:%M:%S %Z %Y");
00221 
00222   STRTEST (INT_CURR_SYMBOL, "");
00223   STRTEST (CURRENCY_SYMBOL, "");
00224   STRTEST (MON_DECIMAL_POINT, "");
00225   STRTEST (MON_THOUSANDS_SEP, "");
00226   STRTEST (MON_GROUPING, "");
00227   STRTEST (POSITIVE_SIGN, "");
00228   STRTEST (NEGATIVE_SIGN, "");
00229   STRTEST (GROUPING, "");
00230 
00231   STRTEST (YESSTR, "");
00232   STRTEST (NOSTR, "");
00233 
00234   /* Test the new locale mechanisms.  */
00235   loc = newlocale (LC_ALL_MASK, locname, NULL);
00236   if (loc == NULL)
00237     {
00238       printf ("cannot create locale object for locale %s\n", locname);
00239       result = 1;
00240     }
00241   else
00242     {
00243       int c;
00244 
00245 #undef STRTEST
00246 #define STRTEST(name, exp) \
00247       str = nl_langinfo_l (name, loc);                               \
00248       if (strcmp (str, exp) != 0)                                    \
00249        {                                                             \
00250          printf ("nl_langinfo_l(" #name ") in locale %s wrong "             \
00251                 "(is \"%s\", should be \"%s\")\n", locname, str, exp);      \
00252          result = 1;                                                 \
00253        }
00254 #undef WSTRTEST
00255 #define WSTRTEST(name, exp) \
00256       wstr = (wchar_t *) nl_langinfo_l (name, loc);                         \
00257       if (wcscmp (wstr, exp) != 0)                                   \
00258        {                                                             \
00259          printf ("nl_langinfo_l(" #name ") in locale %s wrong "             \
00260                 "(is \"%S\", should be \"%S\")\n", locname, wstr, exp);     \
00261          result = 1;                                                 \
00262        }
00263 
00264       /* Unix stuff.  */
00265       STRTEST (ABDAY_1, "Sun");
00266       STRTEST (ABDAY_2, "Mon");
00267       STRTEST (ABDAY_3, "Tue");
00268       STRTEST (ABDAY_4, "Wed");
00269       STRTEST (ABDAY_5, "Thu");
00270       STRTEST (ABDAY_6, "Fri");
00271       STRTEST (ABDAY_7, "Sat");
00272       STRTEST (DAY_1, "Sunday");
00273       STRTEST (DAY_2, "Monday");
00274       STRTEST (DAY_3, "Tuesday");
00275       STRTEST (DAY_4, "Wednesday");
00276       STRTEST (DAY_5, "Thursday");
00277       STRTEST (DAY_6, "Friday");
00278       STRTEST (DAY_7, "Saturday");
00279       STRTEST (ABMON_1, "Jan");
00280       STRTEST (ABMON_2, "Feb");
00281       STRTEST (ABMON_3, "Mar");
00282       STRTEST (ABMON_4, "Apr");
00283       STRTEST (ABMON_5, "May");
00284       STRTEST (ABMON_6, "Jun");
00285       STRTEST (ABMON_7, "Jul");
00286       STRTEST (ABMON_8, "Aug");
00287       STRTEST (ABMON_9, "Sep");
00288       STRTEST (ABMON_10, "Oct");
00289       STRTEST (ABMON_11, "Nov");
00290       STRTEST (ABMON_12, "Dec");
00291       STRTEST (MON_1, "January");
00292       STRTEST (MON_2, "February");
00293       STRTEST (MON_3, "March");
00294       STRTEST (MON_4, "April");
00295       STRTEST (MON_5, "May");
00296       STRTEST (MON_6, "June");
00297       STRTEST (MON_7, "July");
00298       STRTEST (MON_8, "August");
00299       STRTEST (MON_9, "September");
00300       STRTEST (MON_10, "October");
00301       STRTEST (MON_11, "November");
00302       STRTEST (MON_12, "December");
00303       STRTEST (AM_STR, "AM");
00304       STRTEST (PM_STR, "PM");
00305       STRTEST (D_T_FMT, "%a %b %e %H:%M:%S %Y");
00306       STRTEST (D_FMT, "%m/%d/%y");
00307       STRTEST (T_FMT, "%H:%M:%S");
00308       STRTEST (T_FMT_AMPM, "%I:%M:%S %p");
00309       STRTEST (ERA, "");
00310       STRTEST (ERA_D_FMT, "");
00311       STRTEST (ERA_T_FMT, "");
00312       STRTEST (ERA_D_T_FMT, "");
00313       STRTEST (ALT_DIGITS, "");
00314 
00315       STRTEST (RADIXCHAR, ".");
00316       STRTEST (THOUSEP, "");
00317 
00318       STRTEST (YESEXPR, "^[yY]");
00319       STRTEST (NOEXPR, "^[nN]");
00320 
00321       /* Extensions.  */
00322       WSTRTEST (_NL_WABDAY_1, L"Sun");
00323       WSTRTEST (_NL_WABDAY_2, L"Mon");
00324       WSTRTEST (_NL_WABDAY_3, L"Tue");
00325       WSTRTEST (_NL_WABDAY_4, L"Wed");
00326       WSTRTEST (_NL_WABDAY_5, L"Thu");
00327       WSTRTEST (_NL_WABDAY_6, L"Fri");
00328       WSTRTEST (_NL_WABDAY_7, L"Sat");
00329       WSTRTEST (_NL_WDAY_1, L"Sunday");
00330       WSTRTEST (_NL_WDAY_2, L"Monday");
00331       WSTRTEST (_NL_WDAY_3, L"Tuesday");
00332       WSTRTEST (_NL_WDAY_4, L"Wednesday");
00333       WSTRTEST (_NL_WDAY_5, L"Thursday");
00334       WSTRTEST (_NL_WDAY_6, L"Friday");
00335       WSTRTEST (_NL_WDAY_7, L"Saturday");
00336       WSTRTEST (_NL_WABMON_1, L"Jan");
00337       WSTRTEST (_NL_WABMON_2, L"Feb");
00338       WSTRTEST (_NL_WABMON_3, L"Mar");
00339       WSTRTEST (_NL_WABMON_4, L"Apr");
00340       WSTRTEST (_NL_WABMON_5, L"May");
00341       WSTRTEST (_NL_WABMON_6, L"Jun");
00342       WSTRTEST (_NL_WABMON_7, L"Jul");
00343       WSTRTEST (_NL_WABMON_8, L"Aug");
00344       WSTRTEST (_NL_WABMON_9, L"Sep");
00345       WSTRTEST (_NL_WABMON_10, L"Oct");
00346       WSTRTEST (_NL_WABMON_11, L"Nov");
00347       WSTRTEST (_NL_WABMON_12, L"Dec");
00348       WSTRTEST (_NL_WMON_1, L"January");
00349       WSTRTEST (_NL_WMON_2, L"February");
00350       WSTRTEST (_NL_WMON_3, L"March");
00351       WSTRTEST (_NL_WMON_4, L"April");
00352       WSTRTEST (_NL_WMON_5, L"May");
00353       WSTRTEST (_NL_WMON_6, L"June");
00354       WSTRTEST (_NL_WMON_7, L"July");
00355       WSTRTEST (_NL_WMON_8, L"August");
00356       WSTRTEST (_NL_WMON_9, L"September");
00357       WSTRTEST (_NL_WMON_10, L"October");
00358       WSTRTEST (_NL_WMON_11, L"November");
00359       WSTRTEST (_NL_WMON_12, L"December");
00360       WSTRTEST (_NL_WAM_STR, L"AM");
00361       WSTRTEST (_NL_WPM_STR, L"PM");
00362       WSTRTEST (_NL_WD_T_FMT, L"%a %b %e %H:%M:%S %Y");
00363       WSTRTEST (_NL_WD_FMT, L"%m/%d/%y");
00364       WSTRTEST (_NL_WT_FMT, L"%H:%M:%S");
00365       WSTRTEST (_NL_WT_FMT_AMPM, L"%I:%M:%S %p");
00366       WSTRTEST (_NL_WERA_D_FMT, L"");
00367       WSTRTEST (_NL_WERA_T_FMT, L"");
00368       WSTRTEST (_NL_WERA_D_T_FMT, L"");
00369       WSTRTEST (_NL_WALT_DIGITS, L"");
00370 
00371       STRTEST (_DATE_FMT, "%a %b %e %H:%M:%S %Z %Y");
00372       WSTRTEST (_NL_W_DATE_FMT, L"%a %b %e %H:%M:%S %Z %Y");
00373 
00374       STRTEST (INT_CURR_SYMBOL, "");
00375       STRTEST (CURRENCY_SYMBOL, "");
00376       STRTEST (MON_DECIMAL_POINT, "");
00377       STRTEST (MON_THOUSANDS_SEP, "");
00378       STRTEST (MON_GROUPING, "");
00379       STRTEST (POSITIVE_SIGN, "");
00380       STRTEST (NEGATIVE_SIGN, "");
00381       STRTEST (GROUPING, "");
00382 
00383       STRTEST (YESSTR, "");
00384       STRTEST (NOSTR, "");
00385 
00386       /* Character class tests.  */
00387       for (c = 0; c < 128; ++c)
00388        {
00389 #define CLASSTEST(name) \
00390          if (is##name (c) != is##name##_l (c, loc))                         \
00391            {                                                         \
00392              printf ("is%s('\\%o') != is%s_l('\\%o')\n",                    \
00393                     #name, c, #name, c);                             \
00394              result = 1;                                             \
00395            }
00396          CLASSTEST (alnum);
00397          CLASSTEST (alpha);
00398          CLASSTEST (blank);
00399          CLASSTEST (cntrl);
00400          CLASSTEST (digit);
00401          CLASSTEST (lower);
00402          CLASSTEST (graph);
00403          CLASSTEST (print);
00404          CLASSTEST (punct);
00405          CLASSTEST (space);
00406          CLASSTEST (upper);
00407          CLASSTEST (xdigit);
00408 
00409          /* Character mapping tests.  */
00410 #define MAPTEST(name) \
00411          if (to##name (c) != to##name##_l (c, loc))                         \
00412            {                                                         \
00413              printf ("to%s('\\%o') != to%s_l('\\%o'): '\\%o' vs '\\%o'\n", \
00414                     #name, c, #name, c,                              \
00415                     to##name (c), to##name##_l (c, loc));                   \
00416              result = 1;                                             \
00417            }
00418          MAPTEST (lower);
00419          MAPTEST (upper);
00420        }
00421 
00422       /* Character class tests, this time for wide characters.  Note that
00423         this only works because we know that the internal encoding is
00424         UCS4.  */
00425       for (c = 0; c < 128; ++c)
00426        {
00427 #undef CLASSTEST
00428 #define CLASSTEST(name) \
00429          if (isw##name (c) != isw##name##_l (c, loc))                \
00430            {                                                         \
00431              printf ("isw%s('\\%o') != isw%s_l('\\%o')\n",                  \
00432                     #name, c, #name, c);                             \
00433              result = 1;                                             \
00434            }
00435          CLASSTEST (alnum);
00436          CLASSTEST (alpha);
00437          CLASSTEST (blank);
00438          CLASSTEST (cntrl);
00439          CLASSTEST (digit);
00440          CLASSTEST (lower);
00441          CLASSTEST (graph);
00442          CLASSTEST (print);
00443          CLASSTEST (punct);
00444          CLASSTEST (space);
00445          CLASSTEST (upper);
00446          CLASSTEST (xdigit);
00447 
00448          /* Character mapping tests.  Note that
00449             this only works because we know that the internal encoding is
00450             UCS4.  */
00451 #undef MAPTEST
00452 #define MAPTEST(name) \
00453          if (tow##name (c) != tow##name##_l (c, loc))                \
00454            {                                                         \
00455              printf ("tow%s('\\%o') != tow%s_l('\\%o'): '\\%o' vs '\\%o'\n",\
00456                     #name, c, #name, c,                              \
00457                     tow##name (c), tow##name##_l (c, loc));                 \
00458              result = 1;                                             \
00459            }
00460          MAPTEST (lower);
00461          MAPTEST (upper);
00462        }
00463 
00464       freelocale (loc);
00465     }
00466 
00467   return result;
00468 }
00469 
00470 
00471 static int
00472 do_test (void)
00473 {
00474   int result;
00475 
00476   /* First use the name "C".  */
00477   if (setlocale (LC_ALL, "C") == NULL)
00478     {
00479       puts ("cannot set C locale");
00480       result = 1;
00481     }
00482   else
00483     result = run_test ("C");
00484 
00485   /* Then the name "POSIX".  */
00486   if (setlocale (LC_ALL, "POSIX") == NULL)
00487     {
00488       puts ("cannot set POSIX locale");
00489       result = 1;
00490     }
00491   else
00492     result |= run_test ("POSIX");
00493 
00494   return result;
00495 }
00496 
00497 #define TEST_FUNCTION do_test ()
00498 #include "../test-skeleton.c"