]> Savannah Git Hosting - gnulib.git/commitdiff
c32srtombs: Add tests.
authorBruno Haible <bruno@clisp.org>
Thu, 9 Jan 2020 15:21:53 +0000 (16:21 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 9 Jan 2020 15:21:53 +0000 (16:21 +0100)
* tests/test-c32srtombs.c: New file, based on tests/test-wcsrtombs.c.
* tests/test-c32srtombs-1.sh: New file, based on
tests/test-wcsrtombs1.sh.
* tests/test-c32srtombs-2.sh: New file, based on
tests/test-wcsrtombs2.sh.
* tests/test-c32srtombs-3.sh: New file, based on
tests/test-wcsrtombs3.sh.
* tests/test-c32srtombs-4.sh: New file, based on
tests/test-wcsrtombs4.sh.
* modules/c32srtombs-tests: New file, based on modules/wcsrtombs-tests.

ChangeLog
modules/c32srtombs-tests [new file with mode: 0644]
tests/test-c32srtombs-1.sh [new file with mode: 0755]
tests/test-c32srtombs-2.sh [new file with mode: 0755]
tests/test-c32srtombs-3.sh [new file with mode: 0755]
tests/test-c32srtombs-4.sh [new file with mode: 0755]
tests/test-c32srtombs.c [new file with mode: 0644]

index 9d940e5134aee5f7486846d6e60c630f61620d4a..53a1fb43562098387713779555b5cf5c54b340e0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2020-01-09  Bruno Haible  <bruno@clisp.org>
 
+       c32srtombs: Add tests.
+       * tests/test-c32srtombs.c: New file, based on tests/test-wcsrtombs.c.
+       * tests/test-c32srtombs-1.sh: New file, based on
+       tests/test-wcsrtombs1.sh.
+       * tests/test-c32srtombs-2.sh: New file, based on
+       tests/test-wcsrtombs2.sh.
+       * tests/test-c32srtombs-3.sh: New file, based on
+       tests/test-wcsrtombs3.sh.
+       * tests/test-c32srtombs-4.sh: New file, based on
+       tests/test-wcsrtombs4.sh.
+       * modules/c32srtombs-tests: New file, based on modules/wcsrtombs-tests.
+
        c32srtombs: New module.
        * lib/uchar.in.h (c32srtombs): New declaration.
        * lib/wcsrtombs-impl.h: Parameterize: Use macros FUNC, SCHAR_T,
diff --git a/modules/c32srtombs-tests b/modules/c32srtombs-tests
new file mode 100644 (file)
index 0000000..2501ba3
--- /dev/null
@@ -0,0 +1,32 @@
+Files:
+tests/test-c32srtombs-1.sh
+tests/test-c32srtombs-2.sh
+tests/test-c32srtombs-3.sh
+tests/test-c32srtombs-4.sh
+tests/test-c32srtombs.c
+tests/signature.h
+tests/macros.h
+m4/locale-fr.m4
+m4/locale-ja.m4
+m4/locale-zh.m4
+m4/codeset.m4
+
+Depends-on:
+setlocale
+mbstoc32s
+
+configure.ac:
+gt_LOCALE_FR
+gt_LOCALE_FR_UTF8
+gt_LOCALE_JA
+gt_LOCALE_ZH_CN
+
+Makefile.am:
+TESTS += test-c32srtombs-1.sh test-c32srtombs-2.sh test-c32srtombs-3.sh test-c32srtombs-4.sh
+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-c32srtombs
+test_c32srtombs_LDADD = $(LDADD) $(LIB_SETLOCALE)
diff --git a/tests/test-c32srtombs-1.sh b/tests/test-c32srtombs-1.sh
new file mode 100755 (executable)
index 0000000..4228174
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Test in an ISO-8859-1 or ISO-8859-15 locale.
+: ${LOCALE_FR=fr_FR}
+if test $LOCALE_FR = none; then
+  if test -f /usr/bin/localedef; then
+    echo "Skipping test: no traditional french locale is installed"
+  else
+    echo "Skipping test: no traditional french locale is supported"
+  fi
+  exit 77
+fi
+
+LC_ALL=$LOCALE_FR \
+${CHECKER} ./test-c32srtombs${EXEEXT} 1
diff --git a/tests/test-c32srtombs-2.sh b/tests/test-c32srtombs-2.sh
new file mode 100755 (executable)
index 0000000..c0dd172
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Test whether a specific UTF-8 locale is installed.
+: ${LOCALE_FR_UTF8=fr_FR.UTF-8}
+if test $LOCALE_FR_UTF8 = none; then
+  if test -f /usr/bin/localedef; then
+    echo "Skipping test: no french Unicode locale is installed"
+  else
+    echo "Skipping test: no french Unicode locale is supported"
+  fi
+  exit 77
+fi
+
+LC_ALL=$LOCALE_FR_UTF8 \
+${CHECKER} ./test-c32srtombs${EXEEXT} 2
diff --git a/tests/test-c32srtombs-3.sh b/tests/test-c32srtombs-3.sh
new file mode 100755 (executable)
index 0000000..7e59e86
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Test whether a specific EUC-JP locale is installed.
+: ${LOCALE_JA=ja_JP}
+if test $LOCALE_JA = none; then
+  if test -f /usr/bin/localedef; then
+    echo "Skipping test: no traditional japanese locale is installed"
+  else
+    echo "Skipping test: no traditional japanese locale is supported"
+  fi
+  exit 77
+fi
+
+LC_ALL=$LOCALE_JA \
+${CHECKER} ./test-c32srtombs${EXEEXT} 3
diff --git a/tests/test-c32srtombs-4.sh b/tests/test-c32srtombs-4.sh
new file mode 100755 (executable)
index 0000000..97f76a8
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+# Test whether a specific GB18030 locale is installed.
+: ${LOCALE_ZH_CN=zh_CN.GB18030}
+if test $LOCALE_ZH_CN = none; then
+  if test -f /usr/bin/localedef; then
+    echo "Skipping test: no transitional chinese locale is installed"
+  else
+    echo "Skipping test: no transitional chinese locale is supported"
+  fi
+  exit 77
+fi
+
+LC_ALL=$LOCALE_ZH_CN \
+${CHECKER} ./test-c32srtombs${EXEEXT} 4
diff --git a/tests/test-c32srtombs.c b/tests/test-c32srtombs.c
new file mode 100644 (file)
index 0000000..6178900
--- /dev/null
@@ -0,0 +1,206 @@
+/* Test of conversion of 32-bit wide string to string.
+   Copyright (C) 2008-2020 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/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
+
+#include <config.h>
+
+#include <uchar.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (c32srtombs, size_t,
+                 (char *, const char32_t **, size_t, mbstate_t *));
+
+#include <locale.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "macros.h"
+
+int
+main (int argc, char *argv[])
+{
+  /* configure should already have checked that the locale is supported.  */
+  if (setlocale (LC_ALL, "") == NULL)
+    return 1;
+
+  if (argc > 1)
+    {
+      char32_t input[10];
+      size_t n;
+      const char32_t *src;
+      #define BUFSIZE 20
+      char buf[BUFSIZE];
+      size_t ret;
+
+      {
+        size_t i;
+        for (i = 0; i < BUFSIZE; i++)
+          buf[i] = '_';
+      }
+
+      switch (argv[1][0])
+        {
+        case '1':
+          /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
+          {
+            const char original[] = "B\374\337er"; /* "Büßer" */
+
+            ret = mbstoc32s (input, original, 10);
+            ASSERT (ret == 5);
+
+            for (n = 0; n < 10; n++)
+              {
+                src = input;
+                ret = c32srtombs (NULL, &src, n, NULL);
+                ASSERT (ret == 5);
+                ASSERT (src == input);
+
+                src = input;
+                ret = c32srtombs (buf, &src, n, NULL);
+                ASSERT (ret == (n <= 5 ? n : 5));
+                ASSERT (src == (n <= 5 ? input + n : NULL));
+                ASSERT (memcmp (buf, original, ret) == 0);
+                if (src == NULL)
+                  ASSERT (buf[ret] == '\0');
+                ASSERT (buf[ret + (src == NULL) + 0] == '_');
+                ASSERT (buf[ret + (src == NULL) + 1] == '_');
+                ASSERT (buf[ret + (src == NULL) + 2] == '_');
+              }
+          }
+          break;
+
+        case '2':
+          /* Locale encoding is UTF-8.  */
+          {
+            const char original[] = "s\303\274\303\237\360\237\230\213!"; /* "süß😋!" */
+
+            ret = mbstoc32s (input, original, 10);
+            ASSERT (ret == 5);
+
+            for (n = 0; n < 15; n++)
+              {
+                src = input;
+                ret = c32srtombs (NULL, &src, n, NULL);
+                ASSERT (ret == 10);
+                ASSERT (src == input);
+
+                src = input;
+                ret = c32srtombs (buf, &src, n, NULL);
+                ASSERT (ret == (n < 1 ? n :
+                                n < 3 ? 1 :
+                                n < 5 ? 3 :
+                                n < 9 ? 5 :
+                                n <= 10 ? n : 10));
+                ASSERT (src == (n < 1 ? input + n :
+                                n < 3 ? input + 1 :
+                                n < 5 ? input + 2 :
+                                n < 9 ? input + 3 :
+                                n <= 10 ? input + (n - 5) : NULL));
+                ASSERT (memcmp (buf, original, ret) == 0);
+                if (src == NULL)
+                  ASSERT (buf[ret] == '\0');
+                ASSERT (buf[ret + (src == NULL) + 0] == '_');
+                ASSERT (buf[ret + (src == NULL) + 1] == '_');
+                ASSERT (buf[ret + (src == NULL) + 2] == '_');
+              }
+          }
+          break;
+
+        case '3':
+          /* Locale encoding is EUC-JP.  */
+          {
+            const char original[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
+
+            ret = mbstoc32s (input, original, 10);
+            ASSERT (ret == 5);
+
+            for (n = 0; n < 10; n++)
+              {
+                src = input;
+                ret = c32srtombs (NULL, &src, n, NULL);
+                ASSERT (ret == 8);
+                ASSERT (src == input);
+
+                src = input;
+                ret = c32srtombs (buf, &src, n, NULL);
+                ASSERT (ret == (n < 1 ? n :
+                                n < 3 ? 1 :
+                                n < 5 ? 3 :
+                                n < 7 ? 5 :
+                                n <= 8 ? n : 8));
+                ASSERT (src == (n < 1 ? input + n :
+                                n < 3 ? input + 1 :
+                                n < 5 ? input + 2 :
+                                n < 7 ? input + 3 :
+                                n <= 8 ? input + (n - 3) : NULL));
+                ASSERT (memcmp (buf, original, ret) == 0);
+                if (src == NULL)
+                  ASSERT (buf[ret] == '\0');
+                ASSERT (buf[ret + (src == NULL) + 0] == '_');
+                ASSERT (buf[ret + (src == NULL) + 1] == '_');
+                ASSERT (buf[ret + (src == NULL) + 2] == '_');
+              }
+          }
+          break;
+
+
+        case '4':
+          /* Locale encoding is GB18030.  */
+          {
+            const char original[] = "s\250\271\201\060\211\070\224\071\375\067!"; /* "süß😋!" */
+
+            ret = mbstoc32s (input, original, 10);
+            ASSERT (ret == 5);
+
+            for (n = 0; n < 15; n++)
+              {
+                src = input;
+                ret = c32srtombs (NULL, &src, n, NULL);
+                ASSERT (ret == 12);
+                ASSERT (src == input);
+
+                src = input;
+                ret = c32srtombs (buf, &src, n, NULL);
+                ASSERT (ret == (n < 1 ? n :
+                                n < 3 ? 1 :
+                                n < 7 ? 3 :
+                                n < 11 ? 7 :
+                                n <= 12 ? n : 12));
+                ASSERT (src == (n < 1 ? input + n :
+                                n < 3 ? input + 1 :
+                                n < 7 ? input + 2 :
+                                n < 11 ? input + 3 :
+                                n <= 12 ? input + (n - 7) : NULL));
+                ASSERT (memcmp (buf, original, ret) == 0);
+                if (src == NULL)
+                  ASSERT (buf[ret] == '\0');
+                ASSERT (buf[ret + (src == NULL) + 0] == '_');
+                ASSERT (buf[ret + (src == NULL) + 1] == '_');
+                ASSERT (buf[ret + (src == NULL) + 2] == '_');
+              }
+          }
+          break;
+
+        default:
+          return 1;
+        }
+
+      return 0;
+    }
+
+  return 1;
+}