]> Savannah Git Hosting - gnulib.git/commitdiff
mbrlen: Add tests.
authorBruno Haible <bruno@clisp.org>
Thu, 30 Mar 2023 15:55:31 +0000 (17:55 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 30 Mar 2023 17:39:46 +0000 (19:39 +0200)
* tests/test-mbrlen1.sh: New file, based on tests/test-mbrtowc1.sh.
* tests/test-mbrlen2.sh: New file, based on tests/test-mbrtowc2.sh.
* tests/test-mbrlen3.sh: New file, based on tests/test-mbrtowc3.sh.
* tests/test-mbrlen4.sh: New file, based on tests/test-mbrtowc4.sh.
* tests/test-mbrlen5.sh: New file, based on tests/test-mbrtowc5.sh.
* tests/test-mbrlen.c: New file, based on tests/test-mbrtowc.c.
* tests/test-mbrlen-w32-1.sh: New file, based on
tests/test-mbrtowc-w32-1.sh.
* tests/test-mbrlen-w32-2.sh: New file, based on
tests/test-mbrtowc-w32-2.sh.
* tests/test-mbrlen-w32-3.sh: New file, based on
tests/test-mbrtowc-w32-3.sh.
* tests/test-mbrlen-w32-4.sh: New file, based on
tests/test-mbrtowc-w32-4.sh.
* tests/test-mbrlen-w32-5.sh: New file, based on
tests/test-mbrtowc-w32-5.sh.
* tests/test-mbrlen-w32-6.sh: New file, based on
tests/test-mbrtowc-w32-6.sh.
* tests/test-mbrlen-w32-7.sh: New file, based on
tests/test-mbrtowc-w32-7.sh.
* tests/test-mbrlen-w32.c: New file, based on tests/test-mbrtowc-w32.c.
* modules/mbrlen-tests: New file, based on modules/mbrtowc-tests.
* doc/posix-functions/mbrlen.texi: Update.

17 files changed:
ChangeLog
doc/posix-functions/mbrlen.texi
modules/mbrlen-tests [new file with mode: 0644]
tests/test-mbrlen-w32-1.sh [new file with mode: 0755]
tests/test-mbrlen-w32-2.sh [new file with mode: 0755]
tests/test-mbrlen-w32-3.sh [new file with mode: 0755]
tests/test-mbrlen-w32-4.sh [new file with mode: 0755]
tests/test-mbrlen-w32-5.sh [new file with mode: 0755]
tests/test-mbrlen-w32-6.sh [new file with mode: 0755]
tests/test-mbrlen-w32-7.sh [new file with mode: 0755]
tests/test-mbrlen-w32.c [new file with mode: 0644]
tests/test-mbrlen.c [new file with mode: 0644]
tests/test-mbrlen1.sh [new file with mode: 0755]
tests/test-mbrlen2.sh [new file with mode: 0755]
tests/test-mbrlen3.sh [new file with mode: 0755]
tests/test-mbrlen4.sh [new file with mode: 0755]
tests/test-mbrlen5.sh [new file with mode: 0755]

index a4af96ed5eaefe902c1e4fda032013c82e0aa67a..fba5f444f6ea42ec9200f02336778364304e1f6c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,30 @@
+2023-03-30  Bruno Haible  <bruno@clisp.org>
+
+       mbrlen: Add tests.
+       * tests/test-mbrlen1.sh: New file, based on tests/test-mbrtowc1.sh.
+       * tests/test-mbrlen2.sh: New file, based on tests/test-mbrtowc2.sh.
+       * tests/test-mbrlen3.sh: New file, based on tests/test-mbrtowc3.sh.
+       * tests/test-mbrlen4.sh: New file, based on tests/test-mbrtowc4.sh.
+       * tests/test-mbrlen5.sh: New file, based on tests/test-mbrtowc5.sh.
+       * tests/test-mbrlen.c: New file, based on tests/test-mbrtowc.c.
+       * tests/test-mbrlen-w32-1.sh: New file, based on
+       tests/test-mbrtowc-w32-1.sh.
+       * tests/test-mbrlen-w32-2.sh: New file, based on
+       tests/test-mbrtowc-w32-2.sh.
+       * tests/test-mbrlen-w32-3.sh: New file, based on
+       tests/test-mbrtowc-w32-3.sh.
+       * tests/test-mbrlen-w32-4.sh: New file, based on
+       tests/test-mbrtowc-w32-4.sh.
+       * tests/test-mbrlen-w32-5.sh: New file, based on
+       tests/test-mbrtowc-w32-5.sh.
+       * tests/test-mbrlen-w32-6.sh: New file, based on
+       tests/test-mbrtowc-w32-6.sh.
+       * tests/test-mbrlen-w32-7.sh: New file, based on
+       tests/test-mbrtowc-w32-7.sh.
+       * tests/test-mbrlen-w32.c: New file, based on tests/test-mbrtowc-w32.c.
+       * modules/mbrlen-tests: New file, based on modules/mbrtowc-tests.
+       * doc/posix-functions/mbrlen.texi: Update.
+
 2023-03-30  Bruno Haible  <bruno@clisp.org>
 
        btowc: Fix behaviour in the C locale.
index 7d0db72db5b068f953f32dfbda0432778582bcf0..0c55ddae2159448ed3f20d804b4a01d31883a33a 100644 (file)
@@ -14,7 +14,7 @@ Minix 3.1.8, HP-UX 11.00, IRIX 6.5, mingw.
 @item
 In the C or POSIX locales, this function can return @code{(size_t) -1}
 and set @code{errno} to @code{EILSEQ}:
-glibc 2.23.
+glibc 2.35.
 @item
 This function returns 0 instead of @code{(size_t) -2} when the input
 is empty:
diff --git a/modules/mbrlen-tests b/modules/mbrlen-tests
new file mode 100644 (file)
index 0000000..bd2091b
--- /dev/null
@@ -0,0 +1,48 @@
+Files:
+tests/test-mbrlen1.sh
+tests/test-mbrlen2.sh
+tests/test-mbrlen3.sh
+tests/test-mbrlen4.sh
+tests/test-mbrlen5.sh
+tests/test-mbrlen.c
+tests/test-mbrlen-w32-1.sh
+tests/test-mbrlen-w32-2.sh
+tests/test-mbrlen-w32-3.sh
+tests/test-mbrlen-w32-4.sh
+tests/test-mbrlen-w32-5.sh
+tests/test-mbrlen-w32-6.sh
+tests/test-mbrlen-w32-7.sh
+tests/test-mbrlen-w32.c
+tests/signature.h
+tests/macros.h
+m4/locale-fr.m4
+m4/locale-ja.m4
+m4/locale-zh.m4
+m4/codeset.m4
+
+Depends-on:
+mbsinit
+wctob
+setlocale
+localcharset
+
+configure.ac:
+gt_LOCALE_FR
+gt_LOCALE_FR_UTF8
+gt_LOCALE_JA
+gt_LOCALE_ZH_CN
+
+Makefile.am:
+TESTS += \
+  test-mbrlen1.sh test-mbrlen2.sh test-mbrlen3.sh test-mbrlen4.sh \
+  test-mbrlen5.sh \
+  test-mbrlen-w32-1.sh test-mbrlen-w32-2.sh test-mbrlen-w32-3.sh \
+  test-mbrlen-w32-4.sh test-mbrlen-w32-5.sh test-mbrlen-w32-6.sh \
+  test-mbrlen-w32-7.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-mbrlen test-mbrlen-w32
+test_mbrlen_LDADD = $(LDADD) $(SETLOCALE_LIB) $(MBRTOWC_LIB)
diff --git a/tests/test-mbrlen-w32-1.sh b/tests/test-mbrlen-w32-1.sh
new file mode 100755 (executable)
index 0000000..ccf4c53
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Test a CP1252 locale.
+${CHECKER} ./test-mbrlen-w32${EXEEXT} French_France 1252
diff --git a/tests/test-mbrlen-w32-2.sh b/tests/test-mbrlen-w32-2.sh
new file mode 100755 (executable)
index 0000000..22c5388
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Test a CP1256 locale.
+${CHECKER} ./test-mbrlen-w32${EXEEXT} "Arabic_Saudi Arabia" 1256
diff --git a/tests/test-mbrlen-w32-3.sh b/tests/test-mbrlen-w32-3.sh
new file mode 100755 (executable)
index 0000000..12c2857
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Test a CP932 locale.
+${CHECKER} ./test-mbrlen-w32${EXEEXT} Japanese_Japan 932
diff --git a/tests/test-mbrlen-w32-4.sh b/tests/test-mbrlen-w32-4.sh
new file mode 100755 (executable)
index 0000000..0746b24
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Test a CP950 locale.
+${CHECKER} ./test-mbrlen-w32${EXEEXT} Chinese_Taiwan 950
diff --git a/tests/test-mbrlen-w32-5.sh b/tests/test-mbrlen-w32-5.sh
new file mode 100755 (executable)
index 0000000..eb2cec2
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Test a CP936 locale.
+${CHECKER} ./test-mbrlen-w32${EXEEXT} Chinese_China 936
diff --git a/tests/test-mbrlen-w32-6.sh b/tests/test-mbrlen-w32-6.sh
new file mode 100755 (executable)
index 0000000..d586b7d
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Test a GB18030 locale.
+${CHECKER} ./test-mbrlen-w32${EXEEXT} Chinese_China 54936
diff --git a/tests/test-mbrlen-w32-7.sh b/tests/test-mbrlen-w32-7.sh
new file mode 100755 (executable)
index 0000000..dbf82d4
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/sh
+
+# Test some UTF-8 locales.
+${CHECKER} ./test-mbrlen-w32${EXEEXT} French_France Japanese_Japan Chinese_Taiwan Chinese_China 65001
diff --git a/tests/test-mbrlen-w32.c b/tests/test-mbrlen-w32.c
new file mode 100644 (file)
index 0000000..2916cf1
--- /dev/null
@@ -0,0 +1,565 @@
+/* Test of conversion of multibyte character to wide character.
+   Copyright (C) 2008-2023 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 <wchar.h>
+
+#include <errno.h>
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "localcharset.h"
+#include "macros.h"
+
+#if defined _WIN32 && !defined __CYGWIN__
+
+static int
+test_one_locale (const char *name, int codepage)
+{
+  mbstate_t state;
+  size_t ret;
+
+# if 1
+  /* Portable code to set the locale.  */
+  {
+    char name_with_codepage[1024];
+
+    sprintf (name_with_codepage, "%s.%d", name, codepage);
+
+    /* Set the locale.  */
+    if (setlocale (LC_ALL, name_with_codepage) == NULL)
+      return 77;
+  }
+# else
+  /* Hacky way to set a locale.codepage combination that setlocale() refuses
+     to set.  */
+  {
+    /* Codepage of the current locale, set with setlocale().
+       Not necessarily the same as GetACP().  */
+    extern __declspec(dllimport) unsigned int __lc_codepage;
+
+    /* Set the locale.  */
+    if (setlocale (LC_ALL, name) == NULL)
+      return 77;
+
+    /* Clobber the codepage and MB_CUR_MAX, both set by setlocale().  */
+    __lc_codepage = codepage;
+    switch (codepage)
+      {
+      case 1252:
+      case 1256:
+        MB_CUR_MAX = 1;
+        break;
+      case 932:
+      case 950:
+      case 936:
+        MB_CUR_MAX = 2;
+        break;
+      case 54936:
+      case 65001:
+        MB_CUR_MAX = 4;
+        break;
+      }
+
+    /* Test whether the codepage is really available.  */
+    memset (&state, '\0', sizeof (mbstate_t));
+    if (mbrlen (" ", 1, &state) == (size_t)(-1))
+      return 77;
+  }
+# endif
+
+  /* Test zero-length input.  */
+  {
+    memset (&state, '\0', sizeof (mbstate_t));
+    ret = mbrlen ("x", 0, &state);
+    /* gnulib's implementation returns (size_t)(-2).
+       The AIX 5.1 implementation returns (size_t)(-1).
+       glibc's implementation returns 0.  */
+    ASSERT (ret == (size_t)(-2) || ret == (size_t)(-1) || ret == 0);
+    ASSERT (mbsinit (&state));
+  }
+
+  /* Test NUL byte input.  */
+  {
+    memset (&state, '\0', sizeof (mbstate_t));
+    ret = mbrlen ("", 1, &state);
+    ASSERT (ret == 0);
+    ASSERT (mbsinit (&state));
+  }
+
+  /* Test single-byte input.  */
+  {
+    int c;
+    char buf[1];
+
+    memset (&state, '\0', sizeof (mbstate_t));
+    for (c = 0; c < 0x100; c++)
+      switch (c)
+        {
+        case '\t': case '\v': case '\f':
+        case ' ': case '!': case '"': case '#': case '%':
+        case '&': case '\'': case '(': case ')': case '*':
+        case '+': case ',': case '-': case '.': case '/':
+        case '0': case '1': case '2': case '3': case '4':
+        case '5': case '6': case '7': case '8': case '9':
+        case ':': case ';': case '<': case '=': case '>':
+        case '?':
+        case 'A': case 'B': case 'C': case 'D': case 'E':
+        case 'F': case 'G': case 'H': case 'I': case 'J':
+        case 'K': case 'L': case 'M': case 'N': case 'O':
+        case 'P': case 'Q': case 'R': case 'S': case 'T':
+        case 'U': case 'V': case 'W': case 'X': case 'Y':
+        case 'Z':
+        case '[': case '\\': case ']': case '^': case '_':
+        case 'a': case 'b': case 'c': case 'd': case 'e':
+        case 'f': case 'g': case 'h': case 'i': case 'j':
+        case 'k': case 'l': case 'm': case 'n': case 'o':
+        case 'p': case 'q': case 'r': case 's': case 't':
+        case 'u': case 'v': case 'w': case 'x': case 'y':
+        case 'z': case '{': case '|': case '}': case '~':
+          /* c is in the ISO C "basic character set".  */
+          buf[0] = c;
+          ret = mbrlen (buf, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          break;
+        }
+  }
+
+  /* Test special calling convention, passing a NULL pointer.  */
+  {
+    memset (&state, '\0', sizeof (mbstate_t));
+    ret = mbrlen (NULL, 5, &state);
+    ASSERT (ret == 0);
+    ASSERT (mbsinit (&state));
+  }
+
+  switch (codepage)
+    {
+    case 1252:
+      /* Locale encoding is CP1252, an extension of ISO-8859-1.  */
+      {
+        char input[] = "B\374\337er"; /* "Büßer" */
+        memset (&state, '\0', sizeof (mbstate_t));
+
+        ret = mbrlen (input, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[0] = '\0';
+
+        ret = mbrlen (input + 1, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[1] = '\0';
+
+        ret = mbrlen (input + 2, 3, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[2] = '\0';
+
+        ret = mbrlen (input + 3, 2, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[3] = '\0';
+
+        ret = mbrlen (input + 4, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+      }
+      return 0;
+
+    case 1256:
+      /* Locale encoding is CP1256, not the same as ISO-8859-6.  */
+      {
+        char input[] = "x\302\341\346y"; /* "xآلوy" */
+        memset (&state, '\0', sizeof (mbstate_t));
+
+        ret = mbrlen (input, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[0] = '\0';
+
+        ret = mbrlen (input + 1, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[1] = '\0';
+
+        ret = mbrlen (input + 2, 3, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[2] = '\0';
+
+        ret = mbrlen (input + 3, 2, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[3] = '\0';
+
+        ret = mbrlen (input + 4, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+      }
+      return 0;
+
+    case 932:
+      /* Locale encoding is CP932, similar to Shift_JIS.  */
+      {
+        char input[] = "<\223\372\226\173\214\352>"; /* "<日本語>" */
+        memset (&state, '\0', sizeof (mbstate_t));
+
+        ret = mbrlen (input, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[0] = '\0';
+
+        ret = mbrlen (input + 1, 2, &state);
+        ASSERT (ret == 2);
+        ASSERT (mbsinit (&state));
+        input[1] = '\0';
+        input[2] = '\0';
+
+        ret = mbrlen (input + 3, 1, &state);
+        ASSERT (ret == (size_t)(-2));
+        ASSERT (!mbsinit (&state));
+        input[3] = '\0';
+
+        ret = mbrlen (input + 4, 4, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[4] = '\0';
+
+        ret = mbrlen (input + 5, 3, &state);
+        ASSERT (ret == 2);
+        ASSERT (mbsinit (&state));
+        input[5] = '\0';
+        input[6] = '\0';
+
+        ret = mbrlen (input + 7, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+
+        /* Test some invalid input.  */
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\377", 1, &state); /* 0xFF */
+        ASSERT ((ret == (size_t)-1 && errno == EILSEQ) || ret == (size_t)-2);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\225\377", 2, &state); /* 0x95 0xFF */
+        ASSERT ((ret == (size_t)-1 && errno == EILSEQ) || ret == 2);
+      }
+      return 0;
+
+    case 950:
+      /* Locale encoding is CP950, similar to Big5.  */
+      {
+        char input[] = "<\244\351\245\273\273\171>"; /* "<日本語>" */
+        memset (&state, '\0', sizeof (mbstate_t));
+
+        ret = mbrlen (input, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[0] = '\0';
+
+        ret = mbrlen (input + 1, 2, &state);
+        ASSERT (ret == 2);
+        ASSERT (mbsinit (&state));
+        input[1] = '\0';
+        input[2] = '\0';
+
+        ret = mbrlen (input + 3, 1, &state);
+        ASSERT (ret == (size_t)(-2));
+        ASSERT (!mbsinit (&state));
+        input[3] = '\0';
+
+        ret = mbrlen (input + 4, 4, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[4] = '\0';
+
+        ret = mbrlen (input + 5, 3, &state);
+        ASSERT (ret == 2);
+        ASSERT (mbsinit (&state));
+        input[5] = '\0';
+        input[6] = '\0';
+
+        ret = mbrlen (input + 7, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+
+        /* Test some invalid input.  */
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\377", 1, &state); /* 0xFF */
+        ASSERT ((ret == (size_t)-1 && errno == EILSEQ) || ret == (size_t)-2);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\225\377", 2, &state); /* 0x95 0xFF */
+        ASSERT ((ret == (size_t)-1 && errno == EILSEQ) || ret == 2);
+      }
+      return 0;
+
+    case 936:
+      /* Locale encoding is CP936 = GBK, an extension of GB2312.  */
+      {
+        char input[] = "<\310\325\261\276\325\132>"; /* "<日本語>" */
+        memset (&state, '\0', sizeof (mbstate_t));
+
+        ret = mbrlen (input, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[0] = '\0';
+
+        ret = mbrlen (input + 1, 2, &state);
+        ASSERT (ret == 2);
+        ASSERT (mbsinit (&state));
+        input[1] = '\0';
+        input[2] = '\0';
+
+        ret = mbrlen (input + 3, 1, &state);
+        ASSERT (ret == (size_t)(-2));
+        ASSERT (!mbsinit (&state));
+        input[3] = '\0';
+
+        ret = mbrlen (input + 4, 4, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[4] = '\0';
+
+        ret = mbrlen (input + 5, 3, &state);
+        ASSERT (ret == 2);
+        ASSERT (mbsinit (&state));
+        input[5] = '\0';
+        input[6] = '\0';
+
+        ret = mbrlen (input + 7, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+
+        /* Test some invalid input.  */
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\377", 1, &state); /* 0xFF */
+        ASSERT ((ret == (size_t)-1 && errno == EILSEQ) || ret == (size_t)-2);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\225\377", 2, &state); /* 0x95 0xFF */
+        ASSERT ((ret == (size_t)-1 && errno == EILSEQ) || ret == 2);
+      }
+      return 0;
+
+    case 54936:
+      /* Locale encoding is CP54936 = GB18030.  */
+      if (strcmp (locale_charset (), "GB18030") != 0)
+        return 77;
+      {
+        char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
+        memset (&state, '\0', sizeof (mbstate_t));
+
+        ret = mbrlen (input, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[0] = '\0';
+
+        ret = mbrlen (input + 1, 1, &state);
+        ASSERT (ret == (size_t)(-2));
+        ASSERT (!mbsinit (&state));
+        input[1] = '\0';
+
+        ret = mbrlen (input + 2, 7, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[2] = '\0';
+
+        ret = mbrlen (input + 3, 6, &state);
+        ASSERT (ret == 4);
+        ASSERT (mbsinit (&state));
+        input[3] = '\0';
+        input[4] = '\0';
+        input[5] = '\0';
+        input[6] = '\0';
+
+        ret = mbrlen (input + 7, 2, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[7] = '\0';
+
+        ret = mbrlen (input + 8, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+
+        /* Test some invalid input.  */
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\377", 1, &state); /* 0xFF */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\225\377", 2, &state); /* 0x95 0xFF */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\201\045", 2, &state); /* 0x81 0x25 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\201\060\377", 3, &state); /* 0x81 0x30 0xFF */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\201\060\377\064", 4, &state); /* 0x81 0x30 0xFF 0x34 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\201\060\211\072", 4, &state); /* 0x81 0x30 0x89 0x3A */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+      }
+      return 0;
+
+    case 65001:
+      /* Locale encoding is CP65001 = UTF-8.  */
+      if (strcmp (locale_charset (), "UTF-8") != 0)
+        return 77;
+      {
+        char input[] = "B\303\274\303\237er"; /* "Büßer" */
+        memset (&state, '\0', sizeof (mbstate_t));
+
+        ret = mbrlen (input, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[0] = '\0';
+
+        ret = mbrlen (input + 1, 1, &state);
+        ASSERT (ret == (size_t)(-2));
+        ASSERT (!mbsinit (&state));
+        input[1] = '\0';
+
+        ret = mbrlen (input + 2, 5, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[2] = '\0';
+
+        ret = mbrlen (input + 3, 4, &state);
+        ASSERT (ret == 2);
+        ASSERT (mbsinit (&state));
+        input[3] = '\0';
+        input[4] = '\0';
+
+        ret = mbrlen (input + 5, 2, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+        input[5] = '\0';
+
+        ret = mbrlen (input + 6, 1, &state);
+        ASSERT (ret == 1);
+        ASSERT (mbsinit (&state));
+
+        /* Test some invalid input.  */
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\377", 1, &state); /* 0xFF */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\303\300", 2, &state); /* 0xC3 0xC0 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\343\300", 2, &state); /* 0xE3 0xC0 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\343\300\200", 3, &state); /* 0xE3 0xC0 0x80 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\343\200\300", 3, &state); /* 0xE3 0x80 0xC0 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\363\300", 2, &state); /* 0xF3 0xC0 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\363\300\200\200", 4, &state); /* 0xF3 0xC0 0x80 0x80 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\363\200\300", 3, &state); /* 0xF3 0x80 0xC0 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\363\200\300\200", 4, &state); /* 0xF3 0x80 0xC0 0x80 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+
+        memset (&state, '\0', sizeof (mbstate_t));
+        ret = mbrlen ("\363\200\200\300", 4, &state); /* 0xF3 0x80 0x80 0xC0 */
+        ASSERT (ret == (size_t)-1);
+        ASSERT (errno == EILSEQ);
+      }
+      return 0;
+
+    default:
+      return 1;
+    }
+}
+
+int
+main (int argc, char *argv[])
+{
+  int codepage = atoi (argv[argc - 1]);
+  int result;
+  int i;
+
+  result = 77;
+  for (i = 1; i < argc - 1; i++)
+    {
+      int ret = test_one_locale (argv[i], codepage);
+
+      if (ret != 77)
+        result = ret;
+    }
+
+  if (result == 77)
+    {
+      fprintf (stderr, "Skipping test: found no locale with codepage %d\n",
+               codepage);
+    }
+  return result;
+}
+
+#else
+
+int
+main (int argc, char *argv[])
+{
+  fputs ("Skipping test: not a native Windows system\n", stderr);
+  return 77;
+}
+
+#endif
diff --git a/tests/test-mbrlen.c b/tests/test-mbrlen.c
new file mode 100644 (file)
index 0000000..005d0c8
--- /dev/null
@@ -0,0 +1,297 @@
+/* Test of conversion of multibyte character to wide character.
+   Copyright (C) 2008-2023 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>, 2023.  */
+
+#include <config.h>
+
+#include <wchar.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (mbrlen, size_t, (char const *, size_t, mbstate_t *));
+
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "macros.h"
+
+int
+main (int argc, char *argv[])
+{
+  mbstate_t state;
+  size_t ret;
+
+  /* configure should already have checked that the locale is supported.  */
+  if (setlocale (LC_ALL, "") == NULL)
+    return 1;
+
+  /* Test zero-length input.  */
+  {
+    memset (&state, '\0', sizeof (mbstate_t));
+    ret = mbrlen ("x", 0, &state);
+    ASSERT (ret == (size_t)(-2));
+    ASSERT (mbsinit (&state));
+  }
+
+  /* Test NUL byte input.  */
+  {
+    memset (&state, '\0', sizeof (mbstate_t));
+    ret = mbrlen ("", 1, &state);
+    ASSERT (ret == 0);
+    ASSERT (mbsinit (&state));
+  }
+
+  /* Test single-byte input.  */
+  {
+    int c;
+    char buf[1];
+
+    memset (&state, '\0', sizeof (mbstate_t));
+    for (c = 0; c < 0x100; c++)
+      switch (c)
+        {
+        case '\t': case '\v': case '\f':
+        case ' ': case '!': case '"': case '#': case '%':
+        case '&': case '\'': case '(': case ')': case '*':
+        case '+': case ',': case '-': case '.': case '/':
+        case '0': case '1': case '2': case '3': case '4':
+        case '5': case '6': case '7': case '8': case '9':
+        case ':': case ';': case '<': case '=': case '>':
+        case '?':
+        case 'A': case 'B': case 'C': case 'D': case 'E':
+        case 'F': case 'G': case 'H': case 'I': case 'J':
+        case 'K': case 'L': case 'M': case 'N': case 'O':
+        case 'P': case 'Q': case 'R': case 'S': case 'T':
+        case 'U': case 'V': case 'W': case 'X': case 'Y':
+        case 'Z':
+        case '[': case '\\': case ']': case '^': case '_':
+        case 'a': case 'b': case 'c': case 'd': case 'e':
+        case 'f': case 'g': case 'h': case 'i': case 'j':
+        case 'k': case 'l': case 'm': case 'n': case 'o':
+        case 'p': case 'q': case 'r': case 's': case 't':
+        case 'u': case 'v': case 'w': case 'x': case 'y':
+        case 'z': case '{': case '|': case '}': case '~':
+          /* c is in the ISO C "basic character set".  */
+          ASSERT (c < 0x80);
+          /* c is an ASCII character.  */
+          buf[0] = c;
+
+          ret = mbrlen (buf, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+
+          break;
+        default:
+          break;
+        }
+  }
+
+  /* Test special calling convention, passing a NULL pointer.  */
+  {
+    memset (&state, '\0', sizeof (mbstate_t));
+    ret = mbrlen (NULL, 5, &state);
+    ASSERT (ret == 0);
+    ASSERT (mbsinit (&state));
+  }
+
+#ifdef __ANDROID__
+  /* On Android ≥ 5.0, the default locale is the "C.UTF-8" locale, not the
+     "C" locale.  Furthermore, when you attempt to set the "C" or "POSIX"
+     locale via setlocale(), what you get is a "C" locale with UTF-8 encoding,
+     that is, effectively the "C.UTF-8" locale.  */
+  if (argc > 1 && strcmp (argv[1], "5") == 0 && MB_CUR_MAX > 1)
+    argv[1] = "2";
+#endif
+
+  if (argc > 1)
+    switch (argv[1][0])
+      {
+      case '1':
+        /* Locale encoding is ISO-8859-1 or ISO-8859-15.  */
+        {
+          char input[] = "B\374\337er"; /* "Büßer" */
+          memset (&state, '\0', sizeof (mbstate_t));
+
+          ret = mbrlen (input, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[0] = '\0';
+
+          ret = mbrlen (input + 1, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[1] = '\0';
+
+          ret = mbrlen (input + 2, 3, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[2] = '\0';
+
+          ret = mbrlen (input + 3, 2, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[3] = '\0';
+
+          ret = mbrlen (input + 4, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+        }
+        return 0;
+
+      case '2':
+        /* Locale encoding is UTF-8.  */
+        {
+          char input[] = "B\303\274\303\237er"; /* "Büßer" */
+          memset (&state, '\0', sizeof (mbstate_t));
+
+          ret = mbrlen (input, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[0] = '\0';
+
+          ret = mbrlen (input + 1, 1, &state);
+          ASSERT (ret == (size_t)(-2));
+          ASSERT (!mbsinit (&state));
+          input[1] = '\0';
+
+          ret = mbrlen (input + 2, 5, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[2] = '\0';
+
+          ret = mbrlen (input + 3, 4, &state);
+          ASSERT (ret == 2);
+          ASSERT (mbsinit (&state));
+          input[3] = '\0';
+          input[4] = '\0';
+
+          ret = mbrlen (input + 5, 2, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[5] = '\0';
+
+          ret = mbrlen (input + 6, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+        }
+        return 0;
+
+      case '3':
+        /* Locale encoding is EUC-JP.  */
+        {
+          char input[] = "<\306\374\313\334\270\354>"; /* "<日本語>" */
+          memset (&state, '\0', sizeof (mbstate_t));
+
+          ret = mbrlen (input, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[0] = '\0';
+
+          ret = mbrlen (input + 1, 2, &state);
+          ASSERT (ret == 2);
+          ASSERT (mbsinit (&state));
+          input[1] = '\0';
+          input[2] = '\0';
+
+          ret = mbrlen (input + 3, 1, &state);
+          ASSERT (ret == (size_t)(-2));
+          ASSERT (!mbsinit (&state));
+          input[3] = '\0';
+
+          ret = mbrlen (input + 4, 4, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[4] = '\0';
+
+          ret = mbrlen (input + 5, 3, &state);
+          ASSERT (ret == 2);
+          ASSERT (mbsinit (&state));
+          input[5] = '\0';
+          input[6] = '\0';
+
+          ret = mbrlen (input + 7, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+        }
+        return 0;
+
+      case '4':
+        /* Locale encoding is GB18030.  */
+        {
+          char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
+          memset (&state, '\0', sizeof (mbstate_t));
+
+          ret = mbrlen (input, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[0] = '\0';
+
+          ret = mbrlen (input + 1, 1, &state);
+          ASSERT (ret == (size_t)(-2));
+          ASSERT (!mbsinit (&state));
+          input[1] = '\0';
+
+          ret = mbrlen (input + 2, 7, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[2] = '\0';
+
+          ret = mbrlen (input + 3, 6, &state);
+          ASSERT (ret == 4);
+          ASSERT (mbsinit (&state));
+          input[3] = '\0';
+          input[4] = '\0';
+          input[5] = '\0';
+          input[6] = '\0';
+
+          ret = mbrlen (input + 7, 2, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+          input[7] = '\0';
+
+          ret = mbrlen (input + 8, 1, &state);
+          ASSERT (ret == 1);
+          ASSERT (mbsinit (&state));
+        }
+        return 0;
+
+      case '5':
+        /* C or POSIX locale.  */
+        {
+          int c;
+          char buf[1];
+
+          memset (&state, '\0', sizeof (mbstate_t));
+          for (c = 0; c < 0x100; c++)
+            if (c != 0)
+              {
+                /* We are testing all nonnull bytes.  */
+                buf[0] = c;
+
+                ret = mbrlen (buf, 1, &state);
+                /* POSIX:2018 says: "In the POSIX locale an [EILSEQ] error
+                   cannot occur since all byte values are valid characters."  */
+                ASSERT (ret == 1);
+                ASSERT (mbsinit (&state));
+              }
+        }
+        return 0;
+      }
+
+  return 1;
+}
diff --git a/tests/test-mbrlen1.sh b/tests/test-mbrlen1.sh
new file mode 100755 (executable)
index 0000000..f07bc37
--- /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-mbrlen${EXEEXT} 1
diff --git a/tests/test-mbrlen2.sh b/tests/test-mbrlen2.sh
new file mode 100755 (executable)
index 0000000..108394a
--- /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-mbrlen${EXEEXT} 2
diff --git a/tests/test-mbrlen3.sh b/tests/test-mbrlen3.sh
new file mode 100755 (executable)
index 0000000..57199a0
--- /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-mbrlen${EXEEXT} 3
diff --git a/tests/test-mbrlen4.sh b/tests/test-mbrlen4.sh
new file mode 100755 (executable)
index 0000000..8f83d4d
--- /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-mbrlen${EXEEXT} 4
diff --git a/tests/test-mbrlen5.sh b/tests/test-mbrlen5.sh
new file mode 100755 (executable)
index 0000000..9b14cb1
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+# Test whether the POSIX locale has encoding errors.
+LC_ALL=C \
+${CHECKER} ./test-mbrlen${EXEEXT} 5 || exit 1
+LC_ALL=POSIX \
+${CHECKER} ./test-mbrlen${EXEEXT} 5 || exit 1
+
+exit 0