setlocale tests: Add unit test for LC_MESSAGES handling.
authorBruno Haible <bruno@clisp.org>
Mon, 23 Dec 2024 15:51:51 +0000 (16:51 +0100)
committerBruno Haible <bruno@clisp.org>
Mon, 23 Dec 2024 15:51:51 +0000 (16:51 +0100)
* tests/test-setlocale-w32.c: New file.
* modules/setlocale-tests (Files): Add it.
(Makefile.am): Arrange to compile and run test-setlocale-w32.

ChangeLog
modules/setlocale-tests
tests/test-setlocale-w32.c [new file with mode: 0644]

index ceab98b058821bfa1842ffc530c0b11bfefabc14..c2948988288000fec333d02fc74333e73dd9244b 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2024-12-23  Bruno Haible  <bruno@clisp.org>
 
+       setlocale tests: Add unit test for LC_MESSAGES handling.
+       * tests/test-setlocale-w32.c: New file.
+       * modules/setlocale-tests (Files): Add it.
+       (Makefile.am): Arrange to compile and run test-setlocale-w32.
+
        setlocale: Handle LC_MESSAGES correctly on native Windows.
        * lib/setlocale.c (setlocale_improved): Revamp the LC_ALL case if
        LC_MESSAGES is 1729.
index 78060d1ccc7a8e76c62da3d81dde1895066abaea..ad0a536bc664d41f8a083c1bc1f4c727c919d88a 100644 (file)
@@ -3,6 +3,7 @@ tests/test-setlocale1.sh
 tests/test-setlocale1.c
 tests/test-setlocale2.sh
 tests/test-setlocale2.c
+tests/test-setlocale-w32.c
 tests/signature.h
 tests/macros.h
 m4/locale-fr.m4
@@ -20,12 +21,13 @@ gt_LOCALE_JA
 gt_LOCALE_ZH_CN
 
 Makefile.am:
-TESTS += test-setlocale1.sh test-setlocale2.sh
+TESTS += test-setlocale1.sh test-setlocale2.sh test-setlocale-w32
 TESTS_ENVIRONMENT += \
   LOCALE_FR='@LOCALE_FR@' \
   LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \
   LOCALE_JA='@LOCALE_JA@' \
   LOCALE_ZH_CN='@LOCALE_ZH_CN@'
-check_PROGRAMS += test-setlocale1 test-setlocale2
+check_PROGRAMS += test-setlocale1 test-setlocale2 test-setlocale-w32
 test_setlocale1_LDADD = $(LDADD) @SETLOCALE_LIB@
 test_setlocale2_LDADD = $(LDADD) @SETLOCALE_LIB@
+test_setlocale_w32_LDADD = $(LDADD) @SETLOCALE_LIB@
diff --git a/tests/test-setlocale-w32.c b/tests/test-setlocale-w32.c
new file mode 100644 (file)
index 0000000..5dd1d8c
--- /dev/null
@@ -0,0 +1,145 @@
+/* Test of setting the current locale.
+   Copyright (C) 2024 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 3 of the License, 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, see <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <locale.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "macros.h"
+
+#if defined _WIN32 && !defined __CYGWIN__
+
+# define ENGLISH   "English_United States"
+# define FRENCH    "French_France"
+# define GERMAN    "German_Germany"
+# define BRAZILIAN "Portuguese_Brazil"
+# define CATALAN   "Catalan_Spain"
+# define ENCODING ".1252"
+
+# define LOCALE1 GERMAN ENCODING
+# define LOCALE2 FRENCH ENCODING
+# define LOCALE3 ENGLISH ENCODING
+# define LOCALE4 BRAZILIAN ENCODING
+# define LOCALE5 CATALAN ENCODING
+
+int
+main ()
+{
+  const char *ret;
+  const char *ret_all;
+
+  /* Set a mixed locale.  */
+  if (setlocale (LC_ALL, LOCALE1) == NULL)
+    {
+      fprintf (stderr, "Setting locale %s failed.\n", LOCALE1);
+      return 1;
+    }
+  if (setlocale (LC_NUMERIC, LOCALE2) == NULL)
+    {
+      fprintf (stderr, "Setting locale %s failed.\n", LOCALE2);
+      return 1;
+    }
+  if (setlocale (LC_TIME, LOCALE3) == NULL)
+    {
+      fprintf (stderr, "Setting locale %s failed.\n", LOCALE3);
+      return 1;
+    }
+  if (setlocale (LC_MESSAGES, LOCALE4) == NULL)
+    {
+      fprintf (stderr, "Setting locale %s failed.\n", LOCALE4);
+      return 1;
+    }
+  if (setlocale (LC_MONETARY, LOCALE5) == NULL)
+    {
+      fprintf (stderr, "Setting locale %s failed.\n", LOCALE5);
+      return 1;
+    }
+
+  /* Check the individual categories.  */
+  ret = setlocale (LC_COLLATE, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE1) == 0);
+  ret = setlocale (LC_CTYPE, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE1) == 0);
+  ret = setlocale (LC_MONETARY, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE5) == 0);
+  ret = setlocale (LC_NUMERIC, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE2) == 0);
+  ret = setlocale (LC_TIME, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE3) == 0);
+  ret = setlocale (LC_MESSAGES, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE4) == 0);
+  ret = setlocale (LC_ALL, NULL);
+  ASSERT (ret != NULL
+          && strcmp (ret, "LC_COLLATE=" LOCALE1 ";"
+                          "LC_CTYPE=" LOCALE1 ";"
+                          "LC_MONETARY=" LOCALE5 ";"
+                          "LC_NUMERIC=" LOCALE2 ";"
+                          "LC_TIME=" LOCALE3 ";"
+                          "LC_MESSAGES=" LOCALE4)
+             == 0);
+  ret_all = _strdup (ret);
+
+  /* Reset the locale.  */
+  ASSERT (setlocale (LC_ALL, "C") != NULL);
+
+  ret = setlocale (LC_ALL, NULL);
+  ASSERT (ret != NULL && strcmp (ret, "C") == 0);
+  ret = setlocale (LC_MESSAGES, NULL);
+  ASSERT (ret != NULL && strcmp (ret, "C") == 0);
+
+  /* Restore the locale.  */
+  ret = setlocale (LC_ALL, ret_all);
+  ASSERT (ret != NULL && strcmp (ret, ret_all) == 0);
+
+  /* Check the individual categories again.  */
+  ret = setlocale (LC_COLLATE, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE1) == 0);
+  ret = setlocale (LC_CTYPE, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE1) == 0);
+  ret = setlocale (LC_MONETARY, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE5) == 0);
+  ret = setlocale (LC_NUMERIC, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE2) == 0);
+  ret = setlocale (LC_TIME, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE3) == 0);
+  ret = setlocale (LC_MESSAGES, NULL);
+  ASSERT (ret != NULL && strcmp (ret, LOCALE4) == 0);
+  ret = setlocale (LC_ALL, NULL);
+  ASSERT (ret != NULL
+          && strcmp (ret, "LC_COLLATE=" LOCALE1 ";"
+                          "LC_CTYPE=" LOCALE1 ";"
+                          "LC_MONETARY=" LOCALE5 ";"
+                          "LC_NUMERIC=" LOCALE2 ";"
+                          "LC_TIME=" LOCALE3 ";"
+                          "LC_MESSAGES=" LOCALE4)
+             == 0);
+
+  return test_exit_status;
+}
+
+#else
+
+int
+main ()
+{
+  fputs ("Skipping test: not a native Windows system\n", stderr);
+  return 77;
+}
+
+#endif