Blame intl/tst-gettext.c

Packit 6c4009
/* Test of the gettext functions.
Packit 6c4009
   Copyright (C) 2000-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <locale.h>
Packit 6c4009
#include <mcheck.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <error.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
const struct
Packit 6c4009
{
Packit 6c4009
  const char *msgid;
Packit 6c4009
  const char *msgstr;
Packit 6c4009
} msgs[] =
Packit 6c4009
{
Packit 6c4009
#define INPUT(Str) { Str,
Packit 6c4009
#define OUTPUT(Str) Str },
Packit 6c4009
#include TESTSTRS_H
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
const char *catname[] =
Packit 6c4009
{
Packit 6c4009
  [LC_MESSAGES] = "LC_MESSAGES",
Packit 6c4009
  [LC_TIME] = "LC_TIME",
Packit 6c4009
  [LC_NUMERIC] = "LC_NUMERIC"
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int positive_gettext_test (void);
Packit 6c4009
static int negative_gettext_test (void);
Packit 6c4009
static int positive_dgettext_test (const char *domain);
Packit 6c4009
static int positive_dcgettext_test (const char *domain, int category);
Packit 6c4009
static int negative_dcgettext_test (const char *domain, int category);
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define check_setlocale(cat, name) do {					      \
Packit 6c4009
    if (setlocale (cat, name) == NULL)					      \
Packit 6c4009
      {									      \
Packit 6c4009
	printf ("%s:%u: setlocale (%s, \"%s\"): %m\n",			      \
Packit 6c4009
		__FILE__, __LINE__, #cat, name);			      \
Packit 6c4009
	result = 1;							      \
Packit 6c4009
      }									      \
Packit 6c4009
  } while (0)
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
main (int argc, char *argv[])
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  /* For debugging.  */
Packit 6c4009
  mtrace ();
Packit 6c4009
Packit 6c4009
  /* This is the place where the .mo files are placed.  */
Packit 6c4009
  if (argc > 1)
Packit 6c4009
    {
Packit 6c4009
      bindtextdomain ("existing-domain", argv[1]);
Packit 6c4009
      bindtextdomain ("existing-time-domain", argv[1]);
Packit 6c4009
      bindtextdomain ("non-existing-domain", argv[1]);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* The locale the catalog is created for is "existing-category".  Now
Packit 6c4009
     set the various variables in question to this value and run the
Packit 6c4009
     test.  */
Packit 6c4009
  setenv ("LANGUAGE", "existing-locale", 1);
Packit 6c4009
  setenv ("LC_ALL", "non-existing-locale", 1);
Packit 6c4009
  setenv ("LC_MESSAGES", "non-existing-locale", 1);
Packit 6c4009
  setenv ("LC_CTYPE", "non-existing-locale", 1);
Packit 6c4009
  setenv ("LANG", "non-existing-locale", 1);
Packit 6c4009
  check_setlocale (LC_CTYPE, "de_DE.UTF-8");
Packit 6c4009
  check_setlocale (LC_MESSAGES, "de_DE.UTF-8");
Packit 6c4009
  unsetenv ("OUTPUT_CHARSET");
Packit 6c4009
  /* This is the name of the existing domain with a catalog for the
Packit 6c4009
     LC_MESSAGES category.  */
Packit 6c4009
  textdomain ("existing-domain");
Packit 6c4009
  puts ("test `gettext' with LANGUAGE set");
Packit 6c4009
  if (positive_gettext_test () != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  /* This is the name of a non-existing domain with a catalog for the
Packit 6c4009
     LC_MESSAGES category.  We leave this value set for the `dgettext'
Packit 6c4009
     and `dcgettext' tests.  */
Packit 6c4009
  textdomain ("non-existing-domain");
Packit 6c4009
  puts ("test `gettext' with LANGUAGE set");
Packit 6c4009
  if (negative_gettext_test () != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  puts ("test `dgettext' with LANGUAGE set");
Packit 6c4009
  if (positive_dgettext_test ("existing-domain") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now the same tests with LC_ALL deciding.  */
Packit 6c4009
  unsetenv ("LANGUAGE");
Packit 6c4009
  setenv ("LC_ALL", "existing-locale", 1);
Packit 6c4009
  check_setlocale (LC_ALL, "");
Packit 6c4009
  puts ("test `gettext' with LC_ALL set");
Packit 6c4009
  /* This is the name of the existing domain with a catalog for the
Packit 6c4009
     LC_MESSAGES category.  */
Packit 6c4009
  textdomain ("existing-domain");
Packit 6c4009
  if (positive_gettext_test () != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  /* This is the name of a non-existing domain with a catalog for the
Packit 6c4009
     LC_MESSAGES category.  We leave this value set for the `dgettext'
Packit 6c4009
     and `dcgettext' tests.  */
Packit 6c4009
  textdomain ("non-existing-domain");
Packit 6c4009
  puts ("test `gettext' with LC_ALL deciding");
Packit 6c4009
  if (negative_gettext_test () != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  puts ("test `dgettext' with LC_ALL deciding");
Packit 6c4009
  if (positive_dgettext_test ("existing-domain") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now the same tests with LC_MESSAGES deciding.  */
Packit 6c4009
  unsetenv ("LC_ALL");
Packit 6c4009
  setenv ("LC_MESSAGES", "existing-locale", 1);
Packit 6c4009
  check_setlocale (LC_MESSAGES, "");
Packit 6c4009
  setenv ("LC_TIME", "existing-locale", 1);
Packit 6c4009
  check_setlocale (LC_TIME, "");
Packit 6c4009
  setenv ("LC_NUMERIC", "non-existing-locale", 1);
Packit 6c4009
  char *what = setlocale (LC_NUMERIC, "");
Packit 6c4009
  if (what != NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("setlocale succeeded (%s), expected failure\n", what);
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  puts ("test `gettext' with LC_MESSAGES set");
Packit 6c4009
  /* This is the name of the existing domain with a catalog for the
Packit 6c4009
     LC_MESSAGES category.  */
Packit 6c4009
  textdomain ("existing-domain");
Packit 6c4009
  if (positive_gettext_test () != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  /* This is the name of a non-existing domain with a catalog for the
Packit 6c4009
     LC_MESSAGES category.  We leave this value set for the `dgettext'
Packit 6c4009
     and `dcgettext' tests.  */
Packit 6c4009
  textdomain ("non-existing-domain");
Packit 6c4009
  puts ("test `gettext' with LC_MESSAGES deciding");
Packit 6c4009
  if (negative_gettext_test () != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  puts ("test `dgettext' with LC_MESSAGES deciding");
Packit 6c4009
  if (positive_dgettext_test ("existing-domain") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  puts ("test `dcgettext' with category == LC_MESSAGES");
Packit 6c4009
  if (positive_dcgettext_test ("existing-domain", LC_MESSAGES) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  /* Try a different category.  For this we also switch the domain.  */
Packit 6c4009
  puts ("test `dcgettext' with LANGUAGE == LC_TIME");
Packit 6c4009
  if (positive_dcgettext_test ("existing-time-domain", LC_TIME) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  /* This time use a category for which there is no catalog.  */
Packit 6c4009
  puts ("test `dcgettext' with LANGUAGE == LC_NUMERIC");
Packit 6c4009
  if (negative_dcgettext_test ("existing-domain", LC_NUMERIC) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now the same tests with LANG deciding.  */
Packit 6c4009
  unsetenv ("LC_MESSAGES");
Packit 6c4009
  unsetenv ("LC_CTYPE");
Packit 6c4009
  unsetenv ("LC_TIME");
Packit 6c4009
  unsetenv ("LC_NUMERIC");
Packit 6c4009
  setenv ("LANG", "existing-locale", 1);
Packit 6c4009
  check_setlocale (LC_ALL, "");
Packit 6c4009
  /* This is the name of the existing domain with a catalog for the
Packit 6c4009
     LC_MESSAGES category.  */
Packit 6c4009
  textdomain ("existing-domain");
Packit 6c4009
  puts ("test `gettext' with LANG set");
Packit 6c4009
  if (positive_gettext_test () != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  /* This is the name of a non-existing domain with a catalog for the
Packit 6c4009
     LC_MESSAGES category.  We leave this value set for the `dgettext'
Packit 6c4009
     and `dcgettext' tests.  */
Packit 6c4009
  textdomain ("non-existing-domain");
Packit 6c4009
  puts ("test `gettext' with LANG set");
Packit 6c4009
  if (negative_gettext_test () != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  puts ("test `dgettext' with LANG set");
Packit 6c4009
  if (positive_dgettext_test ("existing-domain") != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("FAILED");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  check_setlocale (LC_ALL, "C");
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
positive_gettext_test (void)
Packit 6c4009
{
Packit 6c4009
  size_t cnt;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
Packit 6c4009
    {
Packit 6c4009
      const char *found = gettext (msgs[cnt].msgid);
Packit 6c4009
Packit 6c4009
      if (found == NULL
Packit 6c4009
	  || (msgs[cnt].msgstr[0] != '\0'
Packit 6c4009
	      && strcmp (found, msgs[cnt].msgstr) != 0))
Packit 6c4009
	{
Packit 6c4009
	  /* Oops, shouldn't happen.  */
Packit 6c4009
	  printf ("\
Packit 6c4009
  gettext (\"%s\") failed, returned \"%s\", expected \"%s\"\n",
Packit 6c4009
		  msgs[cnt].msgid, found, msgs[cnt].msgstr);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
negative_gettext_test (void)
Packit 6c4009
{
Packit 6c4009
  size_t cnt;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
Packit 6c4009
    {
Packit 6c4009
      const char *found = gettext (msgs[cnt].msgid);
Packit 6c4009
Packit 6c4009
      if (found != msgs[cnt].msgid)
Packit 6c4009
	{
Packit 6c4009
	  /* Oops, shouldn't happen.  */
Packit 6c4009
	  printf ("  gettext (\"%s\") failed\n", msgs[cnt].msgid);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
positive_dgettext_test (const char *domain)
Packit 6c4009
{
Packit 6c4009
  size_t cnt;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
Packit 6c4009
    {
Packit 6c4009
      const char *found = dgettext (domain, msgs[cnt].msgid);
Packit 6c4009
Packit 6c4009
      if (found == NULL
Packit 6c4009
	  || (msgs[cnt].msgstr[0] != '\0'
Packit 6c4009
	      && strcmp (found, msgs[cnt].msgstr) != 0))
Packit 6c4009
	{
Packit 6c4009
	  /* Oops, shouldn't happen.  */
Packit 6c4009
	  printf ("\
Packit 6c4009
  dgettext (\"%s\", \"%s\") failed, returned \"%s\", expected \"%s\"\n",
Packit 6c4009
		  domain, msgs[cnt].msgid, found, msgs[cnt].msgstr);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
positive_dcgettext_test (const char *domain, int category)
Packit 6c4009
{
Packit 6c4009
  size_t cnt;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
Packit 6c4009
    {
Packit 6c4009
      const char *found = dcgettext (domain, msgs[cnt].msgid, category);
Packit 6c4009
Packit 6c4009
      if (found == NULL
Packit 6c4009
	  || (msgs[cnt].msgstr[0] != '\0'
Packit 6c4009
	      && strcmp (found, msgs[cnt].msgstr) != 0))
Packit 6c4009
	{
Packit 6c4009
	  /* Oops, shouldn't happen.  */
Packit 6c4009
	  printf ("\
Packit 6c4009
  dcgettext (\"%s\", \"%s\", %s) failed, returned \"%s\", expected \"%s\"\n",
Packit 6c4009
		  domain, msgs[cnt].msgid, catname[category], found,
Packit 6c4009
		  msgs[cnt].msgstr);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
negative_dcgettext_test (const char *domain, int category)
Packit 6c4009
{
Packit 6c4009
  size_t cnt;
Packit 6c4009
  int result = 0;
Packit 6c4009
Packit 6c4009
  for (cnt = 0; cnt < sizeof (msgs) / sizeof (msgs[0]); ++cnt)
Packit 6c4009
    {
Packit 6c4009
      const char *found = dcgettext (domain, msgs[cnt].msgid, category);
Packit 6c4009
Packit 6c4009
      if (found != msgs[cnt].msgid)
Packit 6c4009
	{
Packit 6c4009
	  /* Oops, shouldn't happen.  */
Packit 6c4009
	  printf ("  dcgettext (\"%s\", \"%s\", %s) failed\n",
Packit 6c4009
		  domain, msgs[cnt].msgid, catname[category]);
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}