Tests for module 'localename'.
authorBruno Haible <bruno@clisp.org>
Thu, 7 Jun 2007 20:04:27 +0000 (20:04 +0000)
committerBruno Haible <bruno@clisp.org>
Thu, 7 Jun 2007 20:04:27 +0000 (20:04 +0000)
ChangeLog
modules/localename-tests [new file with mode: 0644]
tests/test-localename.c [new file with mode: 0644]

index faed881..dd7c4b1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2007-06-07  Bruno Haible  <bruno@clisp.org>
 
+       * modules/localename-tests: New file.
+       * tests/test-localename.c: New file.
+
        New module 'localename'.
        * lib/localename.h: New file.
        * lib/localename.c: New file, from GNU gettext.
diff --git a/modules/localename-tests b/modules/localename-tests
new file mode 100644 (file)
index 0000000..72e9582
--- /dev/null
@@ -0,0 +1,13 @@
+Files:
+tests/test-localename.c
+
+Depends-on:
+setenv
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-localename
+check_PROGRAMS += test-localename
+test_localename_LDADD = $(LDADD) @INTL_MACOSX_LIBS@
+
diff --git a/tests/test-localename.c b/tests/test-localename.c
new file mode 100644 (file)
index 0000000..9e1f3e9
--- /dev/null
@@ -0,0 +1,124 @@
+/* Test of gl_locale_name function.
+   Copyright (C) 2007 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
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
+
+#include <config.h>
+
+#include "localename.h"
+
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "setenv.h"
+
+#define ASSERT(expr) \
+  do                                                                        \
+    {                                                                       \
+      if (!(expr))                                                          \
+        {                                                                   \
+          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
+          abort ();                                                         \
+        }                                                                   \
+    }                                                                       \
+  while (0)
+
+int
+main ()
+{
+  /* Check that gl_locale_name returns non-NULL.  */
+  ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL);
+
+  /* Check that when all environment variables are unset,
+     gl_locale_name_posix returns NULL.  */
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  unsetenv ("LC_NUMERIC");
+  unsetenv ("LANG");
+  setlocale (LC_ALL, "");
+  ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
+                 gl_locale_name_default ()) == 0);
+  ASSERT (strcmp (gl_locale_name (LC_NUMERIC, "LC_NUMERIC"),
+                 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, "");
+  ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
+                 gl_locale_name_default ()) == 0);
+
+  unsetenv ("LC_ALL");
+  setenv ("LC_CTYPE", "", 1);
+  unsetenv ("LC_MESSAGES");
+  unsetenv ("LANG");
+  setlocale (LC_ALL, "");
+  ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
+                 gl_locale_name_default ()) == 0);
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  setenv ("LC_MESSAGES", "", 1);
+  unsetenv ("LANG");
+  setlocale (LC_ALL, "");
+  ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
+                 gl_locale_name_default ()) == 0);
+
+  unsetenv ("LC_ALL");
+  unsetenv ("LC_CTYPE");
+  unsetenv ("LC_MESSAGES");
+  setenv ("LANG", "", 1);
+  setlocale (LC_ALL, "");
+  ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"),
+                 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, "");
+  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");
+  setenv ("LC_CTYPE", "C", 1);
+  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");
+  unsetenv ("LC_CTYPE");
+  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);
+
+  return 0;
+}