More localename tests.
authorBruno Haible <bruno@clisp.org>
Wed, 23 Dec 2009 12:30:50 +0000 (13:30 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 23 Dec 2009 12:30:50 +0000 (13:30 +0100)
ChangeLog
modules/localename-tests
tests/test-localename.c

index 8ec7958..1a70eb7 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2009-12-23  Bruno Haible  <bruno@clisp.org>
 
+       * tests/test-localename.c (test_locale_name): New function, extracted
+       from main. Also test mixed situations.
+       (test_locale_name_posix, test_locale_name_environ,
+       test_locale_name_default): New functions.
+       (main): Invoke them all.
+       * modules/localename-tests (configure.ac): Test for newlocale.
+
+2009-12-23  Bruno Haible  <bruno@clisp.org>
+
        unistd: Ensure getcwd gets declared before being overridden.
        * lib/unistd.in.h: Conditionally include <io.h>.
 
index ae22d85..51972e7 100644 (file)
@@ -7,6 +7,7 @@ setenv
 unsetenv
 
 configure.ac:
+AC_CHECK_FUNCS_ONCE([newlocale])
 
 Makefile.am:
 TESTS += test-localename
index e278e19..93569e8 100644 (file)
@@ -1,5 +1,5 @@
-/* Test of gl_locale_name function.
-   Copyright (C) 2007, 2008 Free Software Foundation, Inc.
+/* Test of gl_locale_name function and its variants.
+   Copyright (C) 2007-2009 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
     }                                                                        \
   while (0)
 
-int
-main ()
+/* Test the gl_locale_name() function.  */
+static void
+test_locale_name (void)
 {
+  const char *name;
+
   /* Check that gl_locale_name returns non-NULL.  */
   ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL);
 
+  /* Get into a defined state,  */
+  setlocale (LC_ALL, "en_US.UTF-8");
+#if HAVE_NEWLOCALE
+  uselocale (LC_GLOBAL_LOCALE);
+#endif
+
   /* Check that when all environment variables are unset,
-     gl_locale_name_posix returns NULL.  */
+     gl_locale_name returns the default locale.  */
   unsetenv ("LC_ALL");
   unsetenv ("LC_CTYPE");
   unsetenv ("LC_MESSAGES");
@@ -99,7 +108,6 @@ main ()
   unsetenv ("LC_MESSAGES");
   unsetenv ("LANG");
   setlocale (LC_ALL, "");
-  ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
 
   unsetenv ("LC_ALL");
@@ -107,7 +115,6 @@ main ()
   setenv ("LC_MESSAGES", "C", 1);
   unsetenv ("LANG");
   setlocale (LC_ALL, "");
-  ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
 
   unsetenv ("LC_ALL");
@@ -115,8 +122,265 @@ main ()
   unsetenv ("LC_MESSAGES");
   setenv ("LANG", "C", 1);
   setlocale (LC_ALL, "");
-  ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
   ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0);
 
+  /* Check mixed situations.  */
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
+  setenv ("LANG", "de_DE.UTF-8", 1);
+  if (setlocale (LC_ALL, "") != NULL)
+    {
+      name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
+      ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
+      name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
+      ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
+    }
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
+  unsetenv ("LANG");
+  if (setlocale (LC_ALL, "") != NULL)
+    {
+      name = gl_locale_name (LC_CTYPE, "LC_CTYPE");
+      ASSERT (strcmp (name, gl_locale_name_default ()) == 0);
+      name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES");
+      ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
+    }
+}
+
+/* Test the gl_locale_name_posix() function.  */
+static void
+test_locale_name_posix (void)
+{
+  const char *name;
+
+  /* Get into a defined state,  */
+  setlocale (LC_ALL, "en_US.UTF-8");
+#if HAVE_NEWLOCALE
+  uselocale (LC_GLOBAL_LOCALE);
+#endif
+
+  /* Check that when all environment variables are unset,
+     gl_locale_name_posix returns either NULL or the default locale.  */
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  unsetenv ("LC_NUMERIC");
+  unsetenv ("LANG");
+  setlocale (LC_ALL, "");
+  name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
+  name = gl_locale_name_posix (LC_NUMERIC, "LC_NUMERIC");
+  ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
+
+  /* Check that an empty environment variable is treated like an unset
+     environment variable.  */
+
+  setenv ("LC_ALL", "", 1);
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  unsetenv ("LANG");
+  setlocale (LC_ALL, "");
+  name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
+
+  unsetenv ("LC_ALL");
+  setenv ("LC_CTYPE", "", 1);
+  unsetenv ("LC_MESSAGES");
+  unsetenv ("LANG");
+  setlocale (LC_ALL, "");
+  name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  setenv ("LC_MESSAGES", "", 1);
+  unsetenv ("LANG");
+  setlocale (LC_ALL, "");
+  name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  setenv ("LANG", "", 1);
+  setlocale (LC_ALL, "");
+  name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
+
+  /* Check that LC_ALL overrides the others, and LANG is overridden by the
+     others.  */
+
+  setenv ("LC_ALL", "C", 1);
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  unsetenv ("LANG");
+  setlocale (LC_ALL, "");
+  name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (strcmp (name, "C") == 0);
+
+  unsetenv ("LC_ALL");
+  setenv ("LC_CTYPE", "C", 1);
+  setenv ("LC_MESSAGES", "C", 1);
+  unsetenv ("LANG");
+  setlocale (LC_ALL, "");
+  name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (strcmp (name, "C") == 0);
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  setenv ("LANG", "C", 1);
+  setlocale (LC_ALL, "");
+  name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (strcmp (name, "C") == 0);
+
+  /* Check mixed situations.  */
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
+  setenv ("LANG", "de_DE.UTF-8", 1);
+  if (setlocale (LC_ALL, "") != NULL)
+    {
+      name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
+      ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
+      name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
+      ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
+    }
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
+  unsetenv ("LANG");
+  if (setlocale (LC_ALL, "") != NULL)
+    {
+      name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE");
+      ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0);
+      name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES");
+      ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
+    }
+}
+
+/* Test the gl_locale_name_environ() function.  */
+static void
+test_locale_name_environ (void)
+{
+  const char *name;
+
+  /* Get into a defined state,  */
+  setlocale (LC_ALL, "en_US.UTF-8");
+#if HAVE_NEWLOCALE
+  uselocale (LC_GLOBAL_LOCALE);
+#endif
+
+  /* Check that when all environment variables are unset,
+     gl_locale_name_environ returns NULL.  */
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  unsetenv ("LC_NUMERIC");
+  unsetenv ("LANG");
+  ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
+  ASSERT (gl_locale_name_environ (LC_NUMERIC, "LC_NUMERIC") == NULL);
+
+  /* Check that an empty environment variable is treated like an unset
+     environment variable.  */
+
+  setenv ("LC_ALL", "", 1);
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  unsetenv ("LANG");
+  ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
+
+  unsetenv ("LC_ALL");
+  setenv ("LC_CTYPE", "", 1);
+  unsetenv ("LC_MESSAGES");
+  unsetenv ("LANG");
+  ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  setenv ("LC_MESSAGES", "", 1);
+  unsetenv ("LANG");
+  ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  setenv ("LANG", "", 1);
+  ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL);
+
+  /* Check that LC_ALL overrides the others, and LANG is overridden by the
+     others.  */
+
+  setenv ("LC_ALL", "C", 1);
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  unsetenv ("LANG");
+  name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (strcmp (name, "C") == 0);
+
+  unsetenv ("LC_ALL");
+  setenv ("LC_CTYPE", "C", 1);
+  setenv ("LC_MESSAGES", "C", 1);
+  unsetenv ("LANG");
+  name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (strcmp (name, "C") == 0);
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  setenv ("LANG", "C", 1);
+  name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (strcmp (name, "C") == 0);
+
+  /* Check mixed situations.  */
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
+  setenv ("LANG", "de_DE.UTF-8", 1);
+  name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
+  ASSERT (strcmp (name, "de_DE.UTF-8") == 0);
+  name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1);
+  unsetenv ("LANG");
+  name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE");
+  ASSERT (name == NULL);
+  name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES");
+  ASSERT (strcmp (name, "fr_FR.UTF-8") == 0);
+}
+
+/* Test the gl_locale_name_default() function.  */
+static void
+test_locale_name_default (void)
+{
+  const char *name = gl_locale_name_default ();
+
+  ASSERT (name != NULL);
+
+  /* Only MacOS X and Windows have a facility for the user to set the default
+     locale.  */
+#if !((defined __APPLE__ && defined __MACH__) || (defined _WIN32 || defined __WIN32__ || defined __CYGWIN__))
+  ASSERT (strcmp (name, "C") == 0);
+#endif
+}
+
+int
+main ()
+{
+  test_locale_name ();
+  test_locale_name_posix ();
+  test_locale_name_environ ();
+  test_locale_name_default ();
+
   return 0;
 }