]> Savannah Git Hosting - gnulib.git/commitdiff
mbsnlen: Add tests.
authorBruno Haible <bruno@clisp.org>
Wed, 19 Jun 2024 00:32:00 +0000 (02:32 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 19 Jun 2024 00:32:00 +0000 (02:32 +0200)
* tests/test-mbsnlen.c: New file.
* tests/test-mbsnlen.sh: New file, based on tests/test-mbsspn.sh.
* modules/mbsnlen-tests: New file.

ChangeLog
modules/mbsnlen-tests [new file with mode: 0644]
tests/test-mbsnlen.c [new file with mode: 0644]
tests/test-mbsnlen.sh [new file with mode: 0755]

index fbde74072f2c1c7c4d1d7b62a6b62e6132203bd9..330bf49b325abf33025ac272033a76ba6b034263 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2024-06-18  Bruno Haible  <bruno@clisp.org>
 
+       mbsnlen: Add tests.
+       * tests/test-mbsnlen.c: New file.
+       * tests/test-mbsnlen.sh: New file, based on tests/test-mbsspn.sh.
+       * modules/mbsnlen-tests: New file.
+
        mbsnlen: Fix bug (regression 2023-09-26).
        * lib/mbsnlen.c (mbsnlen): Fix bug in GNULIB_MCEL_PREFER implementation.
 
diff --git a/modules/mbsnlen-tests b/modules/mbsnlen-tests
new file mode 100644 (file)
index 0000000..d6fad3c
--- /dev/null
@@ -0,0 +1,18 @@
+Files:
+tests/test-mbsnlen.sh
+tests/test-mbsnlen.c
+tests/macros.h
+m4/locale-fr.m4
+m4/codeset.m4
+
+Depends-on:
+setlocale
+
+configure.ac:
+gt_LOCALE_FR_UTF8
+
+Makefile.am:
+TESTS += test-mbsnlen.sh
+TESTS_ENVIRONMENT += LOCALE_FR_UTF8='@LOCALE_FR_UTF8@'
+check_PROGRAMS += test-mbsnlen
+test_mbsnlen_LDADD = $(LDADD) $(LIBUNISTRING) $(SETLOCALE_LIB) $(MBRTOWC_LIB) $(LIBC32CONV)
diff --git a/tests/test-mbsnlen.c b/tests/test-mbsnlen.c
new file mode 100644 (file)
index 0000000..a424ccd
--- /dev/null
@@ -0,0 +1,90 @@
+/* Test of searching a string for a character outside a given set of characters.
+   Copyright (C) 2007-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/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
+
+#include <config.h>
+
+#include <string.h>
+
+#include <locale.h>
+
+#include "macros.h"
+
+/* The mcel-based implementation of mbsnlen behaves differently than the
+   original one.  Namely, for invalid/incomplete byte sequences:
+   Where we ideally should have multi-byte-per-encoding-error (MEE) behaviour
+   everywhere, mcel implements single-byte-per-encoding-error (SEE) behaviour.
+   See <https://lists.gnu.org/archive/html/bug-gnulib/2023-07/msg00131.html>,
+       <https://lists.gnu.org/archive/html/bug-gnulib/2023-07/msg00145.html>.
+   Therefore, here we have different expected results, depending on the
+   implementation.  */
+#if GNULIB_MCEL_PREFER
+# define OR(a,b) b
+#else
+# define OR(a,b) a
+#endif
+
+int
+main ()
+{
+  /* configure should already have checked that the locale is supported.  */
+  if (setlocale (LC_ALL, "") == NULL)
+    return 1;
+
+  ASSERT (mbsnlen ("", 0) == 0);
+  ASSERT (mbsnlen ("", 1) == 1);
+  ASSERT (mbsnlen ("\0", 2) == 2);
+
+  ASSERT (mbsnlen ("H", 0) == 0);
+  ASSERT (mbsnlen ("H", 1) == 1);
+  ASSERT (mbsnlen ("H", 2) == 2);
+
+  ASSERT (mbsnlen ("Hello", 0) == 0);
+  ASSERT (mbsnlen ("Hello", 1) == 1);
+  ASSERT (mbsnlen ("Hello", 2) == 2);
+  ASSERT (mbsnlen ("Hello", 5) == 5);
+  ASSERT (mbsnlen ("Hello", 6) == 6);
+
+  /* The following tests shows how mbsnlen() is different from strnlen().  */
+  /* "äö" */
+  ASSERT (mbsnlen ("\303\244\303\266", 0) == 0);
+  ASSERT (mbsnlen ("\303\244\303\266", 1) == 1); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\303\244\303\266", 2) == 1);
+  ASSERT (mbsnlen ("\303\244\303\266", 3) == 2); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\303\244\303\266", 4) == 2);
+  ASSERT (mbsnlen ("\303\244\303\266", 5) == 3);
+  /* "7€" */
+  ASSERT (mbsnlen ("7\342\202\254", 0) == 0);
+  ASSERT (mbsnlen ("7\342\202\254", 1) == 1);
+  ASSERT (mbsnlen ("7\342\202\254", 2) == 2); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("7\342\202\254", 3) == OR(2,3)); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("7\342\202\254", 4) == 2);
+  ASSERT (mbsnlen ("7\342\202\254", 5) == 3);
+  /* "🐃" */
+  ASSERT (mbsnlen ("\360\237\220\203", 0) == 0);
+  ASSERT (mbsnlen ("\360\237\220\203", 1) == 1); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\360\237\220\203", 2) == OR(1,2)); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\360\237\220\203", 3) == OR(1,3)); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\360\237\220\203", 4) == 1);
+  ASSERT (mbsnlen ("\360\237\220\203", 5) == 2);
+
+  ASSERT (mbsnlen ("\303", 1) == 1); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\342\202", 2) == OR(1,2)); /* invalid multibyte sequence */
+  ASSERT (mbsnlen ("\360\237\220", 3) == OR(1,3)); /* invalid multibyte sequence */
+
+  return test_exit_status;
+}
diff --git a/tests/test-mbsnlen.sh b/tests/test-mbsnlen.sh
new file mode 100755 (executable)
index 0000000..48fa3f2
--- /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-mbsnlen${EXEEXT}