Back to index

glibc  2.9
tst-gettext.c
Go to the documentation of this file.
00001 /* Test of the gettext functions.
00002    Copyright (C) 2000, 2002, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.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 <libintl.h>
00022 #include <locale.h>
00023 #include <mcheck.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <error.h>
00028 #include <errno.h>
00029 
00030 
00031 const struct
00032 {
00033   const char *msgid;
00034   const char *msgstr;
00035 } msgs[] =
00036 {
00037 #define INPUT(Str) { Str,
00038 #define OUTPUT(Str) Str },
00039 #include TESTSTRS_H
00040 };
00041 
00042 const char *catname[] =
00043 {
00044   [LC_MESSAGES] = "LC_MESSAGES",
00045   [LC_TIME] = "LC_TIME",
00046   [LC_NUMERIC] = "LC_NUMERIC"
00047 };
00048 
00049 
00050 static int positive_gettext_test (void);
00051 static int negative_gettext_test (void);
00052 static int positive_dgettext_test (const char *domain);
00053 static int positive_dcgettext_test (const char *domain, int category);
00054 static int negative_dcgettext_test (const char *domain, int category);
00055 
00056 
00057 #define check_setlocale(cat, name) do {                                     \
00058     if (setlocale (cat, name) == NULL)                                      \
00059       {                                                                     \
00060        printf ("%s:%u: setlocale (%s, \"%s\"): %m\n",                       \
00061               __FILE__, __LINE__, #cat, name);                       \
00062        result = 1;                                                   \
00063       }                                                                     \
00064   } while (0)
00065 
00066 int
00067 main (int argc, char *argv[])
00068 {
00069   int result = 0;
00070 
00071   /* For debugging.  */
00072   mtrace ();
00073 
00074   /* This is the place where the .mo files are placed.  */
00075   if (argc > 1)
00076     {
00077       bindtextdomain ("existing-domain", argv[1]);
00078       bindtextdomain ("existing-time-domain", argv[1]);
00079       bindtextdomain ("non-existing-domain", argv[1]);
00080     }
00081 
00082   /* The locale the catalog is created for is "existing-category".  Now
00083      set the various variables in question to this value and run the
00084      test.  */
00085   setenv ("LANGUAGE", "existing-locale", 1);
00086   setenv ("LC_ALL", "non-existing-locale", 1);
00087   setenv ("LC_MESSAGES", "non-existing-locale", 1);
00088   setenv ("LC_CTYPE", "non-existing-locale", 1);
00089   setenv ("LANG", "non-existing-locale", 1);
00090   check_setlocale (LC_CTYPE, "de_DE.UTF-8");
00091   check_setlocale (LC_MESSAGES, "de_DE.UTF-8");
00092   unsetenv ("OUTPUT_CHARSET");
00093   /* This is the name of the existing domain with a catalog for the
00094      LC_MESSAGES category.  */
00095   textdomain ("existing-domain");
00096   puts ("test `gettext' with LANGUAGE set");
00097   if (positive_gettext_test () != 0)
00098     {
00099       puts ("FAILED");
00100       result = 1;
00101     }
00102   /* This is the name of a non-existing domain with a catalog for the
00103      LC_MESSAGES category.  We leave this value set for the `dgettext'
00104      and `dcgettext' tests.  */
00105   textdomain ("non-existing-domain");
00106   puts ("test `gettext' with LANGUAGE set");
00107   if (negative_gettext_test () != 0)
00108     {
00109       puts ("FAILED");
00110       result = 1;
00111     }
00112   puts ("test `dgettext' with LANGUAGE set");
00113   if (positive_dgettext_test ("existing-domain") != 0)
00114     {
00115       puts ("FAILED");
00116       result = 1;
00117     }
00118 
00119   /* Now the same tests with LC_ALL deciding.  */
00120   unsetenv ("LANGUAGE");
00121   setenv ("LC_ALL", "existing-locale", 1);
00122   check_setlocale (LC_ALL, "");
00123   puts ("test `gettext' with LC_ALL set");
00124   /* This is the name of the existing domain with a catalog for the
00125      LC_MESSAGES category.  */
00126   textdomain ("existing-domain");
00127   if (positive_gettext_test () != 0)
00128     {
00129       puts ("FAILED");
00130       result = 1;
00131     }
00132   /* This is the name of a non-existing domain with a catalog for the
00133      LC_MESSAGES category.  We leave this value set for the `dgettext'
00134      and `dcgettext' tests.  */
00135   textdomain ("non-existing-domain");
00136   puts ("test `gettext' with LC_ALL deciding");
00137   if (negative_gettext_test () != 0)
00138     {
00139       puts ("FAILED");
00140       result = 1;
00141     }
00142   puts ("test `dgettext' with LC_ALL deciding");
00143   if (positive_dgettext_test ("existing-domain") != 0)
00144     {
00145       puts ("FAILED");
00146       result = 1;
00147     }
00148 
00149   /* Now the same tests with LC_MESSAGES deciding.  */
00150   unsetenv ("LC_ALL");
00151   setenv ("LC_MESSAGES", "existing-locale", 1);
00152   check_setlocale (LC_MESSAGES, "");
00153   setenv ("LC_TIME", "existing-locale", 1);
00154   check_setlocale (LC_TIME, "");
00155   setenv ("LC_NUMERIC", "non-existing-locale", 1);
00156   char *what = setlocale (LC_NUMERIC, "");
00157   if (what != NULL)
00158     {
00159       printf ("setlocale succeeded (%s), expected failure\n", what);
00160       result = 1;
00161     }
00162 
00163   puts ("test `gettext' with LC_MESSAGES set");
00164   /* This is the name of the existing domain with a catalog for the
00165      LC_MESSAGES category.  */
00166   textdomain ("existing-domain");
00167   if (positive_gettext_test () != 0)
00168     {
00169       puts ("FAILED");
00170       result = 1;
00171     }
00172   /* This is the name of a non-existing domain with a catalog for the
00173      LC_MESSAGES category.  We leave this value set for the `dgettext'
00174      and `dcgettext' tests.  */
00175   textdomain ("non-existing-domain");
00176   puts ("test `gettext' with LC_MESSAGES deciding");
00177   if (negative_gettext_test () != 0)
00178     {
00179       puts ("FAILED");
00180       result = 1;
00181     }
00182   puts ("test `dgettext' with LC_MESSAGES deciding");
00183   if (positive_dgettext_test ("existing-domain") != 0)
00184     {
00185       puts ("FAILED");
00186       result = 1;
00187     }
00188   puts ("test `dcgettext' with category == LC_MESSAGES");
00189   if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0)
00190     {
00191       puts ("FAILED");
00192       result = 1;
00193     }
00194   /* Try a different category.  For this we also switch the domain.  */
00195   puts ("test `dcgettext' with LANGUAGE == LC_TIME");
00196   if (positive_dcgettext_test ("existing-time-domain", LC_TIME) != 0)
00197     {
00198       puts ("FAILED");
00199       result = 1;
00200     }
00201   /* This time use a category for which there is no catalog.  */
00202   puts ("test `dcgettext' with LANGUAGE == LC_NUMERIC");
00203   if (negative_dcgettext_test ("existing-domain", LC_NUMERIC) != 0)
00204     {
00205       puts ("FAILED");
00206       result = 1;
00207     }
00208 
00209   /* Now the same tests with LANG deciding.  */
00210   unsetenv ("LC_MESSAGES");
00211   unsetenv ("LC_CTYPE");
00212   unsetenv ("LC_TIME");
00213   unsetenv ("LC_NUMERIC");
00214   setenv ("LANG", "existing-locale", 1);
00215   check_setlocale (LC_ALL, "");
00216   /* This is the name of the existing domain with a catalog for the
00217      LC_MESSAGES category.  */
00218   textdomain ("existing-domain");
00219   puts ("test `gettext' with LANG set");
00220   if (positive_gettext_test () != 0)
00221     {
00222       puts ("FAILED");
00223       result = 1;
00224     }
00225   /* This is the name of a non-existing domain with a catalog for the
00226      LC_MESSAGES category.  We leave this value set for the `dgettext'
00227      and `dcgettext' tests.  */
00228   textdomain ("non-existing-domain");
00229   puts ("test `gettext' with LANG set");
00230   if (negative_gettext_test () != 0)
00231     {
00232       puts ("FAILED");
00233       result = 1;
00234     }
00235   puts ("test `dgettext' with LANG set");
00236   if (positive_dgettext_test ("existing-domain") != 0)
00237     {
00238       puts ("FAILED");
00239       result = 1;
00240     }
00241 
00242   check_setlocale (LC_ALL, "C");
00243 
00244   return result;
00245 }
00246 
00247 
00248 static int
00249 positive_gettext_test (void)
00250 {
00251   size_t cnt;
00252   int result = 0;
00253 
00254   for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
00255     {
00256       const char *found = gettext (msgs[cnt].msgid);
00257 
00258       if (found == NULL
00259          || (msgs[cnt].msgstr[0] != '\0'
00260              && strcmp (found, msgs[cnt].msgstr) != 0))
00261        {
00262          /* Oops, shouldn't happen.  */
00263          printf ("\
00264   gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
00265                 msgs[cnt].msgid, found, msgs[cnt].msgstr);
00266          result = 1;
00267        }
00268     }
00269 
00270   return result;
00271 }
00272 
00273 
00274 static int
00275 negative_gettext_test (void)
00276 {
00277   size_t cnt;
00278   int result = 0;
00279 
00280   for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
00281     {
00282       const char *found = gettext (msgs[cnt].msgid);
00283 
00284       if (found != msgs[cnt].msgid)
00285        {
00286          /* Oops, shouldn't happen.  */
00287          printf ("  gettext (\"%s\") failed\n", msgs[cnt].msgid);
00288          result = 1;
00289        }
00290     }
00291 
00292   return result;
00293 }
00294 
00295 
00296 static int
00297 positive_dgettext_test (const char *domain)
00298 {
00299   size_t cnt;
00300   int result = 0;
00301 
00302   for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
00303     {
00304       const char *found = dgettext (domain, msgs[cnt].msgid);
00305 
00306       if (found == NULL
00307          || (msgs[cnt].msgstr[0] != '\0'
00308              && strcmp (found, msgs[cnt].msgstr) != 0))
00309        {
00310          /* Oops, shouldn't happen.  */
00311          printf ("\
00312   dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
00313                 domain, msgs[cnt].msgid, found, msgs[cnt].msgstr);
00314          result = 1;
00315        }
00316     }
00317 
00318   return result;
00319 }
00320 
00321 
00322 static int
00323 positive_dcgettext_test (const char *domain, int category)
00324 {
00325   size_t cnt;
00326   int result = 0;
00327 
00328   for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
00329     {
00330       const char *found = dcgettext (domain, msgs[cnt].msgid, category);
00331 
00332       if (found == NULL
00333          || (msgs[cnt].msgstr[0] != '\0'
00334              && strcmp (found, msgs[cnt].msgstr) != 0))
00335        {
00336          /* Oops, shouldn't happen.  */
00337          printf ("\
00338   dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n",
00339                 domain, msgs[cnt].msgid, catname[category], found,
00340                 msgs[cnt].msgstr);
00341          result = 1;
00342        }
00343     }
00344 
00345   return result;
00346 }
00347 
00348 
00349 static int
00350 negative_dcgettext_test (const char *domain, int category)
00351 {
00352   size_t cnt;
00353   int result = 0;
00354 
00355   for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
00356     {
00357       const char *found = dcgettext (domain, msgs[cnt].msgid, category);
00358 
00359       if (found != msgs[cnt].msgid)
00360        {
00361          /* Oops, shouldn't happen.  */
00362          printf ("  dcgettext (\"%s\", \"%s\", %s) failed\n",
00363                 domain, msgs[cnt].msgid, catname[category]);
00364          result = 1;
00365        }
00366     }
00367 
00368   return result;
00369 }