]> Savannah Git Hosting - gnulib.git/commitdiff
trim: Add tests.
authorBruno Haible <bruno@clisp.org>
Sun, 2 Apr 2023 18:40:18 +0000 (20:40 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 2 Apr 2023 19:04:36 +0000 (21:04 +0200)
* tests/test-trim.c: New file.
* tests/test-trim1.sh: New file.
* tests/test-trim2.sh: New file.
* tests/test-trim3.sh: New file.
* modules/trim-tests: New file.

ChangeLog
modules/trim-tests [new file with mode: 0644]
tests/test-trim.c [new file with mode: 0644]
tests/test-trim1.sh [new file with mode: 0644]
tests/test-trim2.sh [new file with mode: 0644]
tests/test-trim3.sh [new file with mode: 0644]

index 463274abee7ebe1b962ce153bf74dd61c9e9d412..a559f3dd713de5dfdd16c4223e6b1d059e0210dd 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2023-04-02  Bruno Haible  <bruno@clisp.org>
+
+       trim: Add tests.
+       * tests/test-trim.c: New file.
+       * tests/test-trim1.sh: New file.
+       * tests/test-trim2.sh: New file.
+       * tests/test-trim3.sh: New file.
+       * modules/trim-tests: New file.
+
 2023-04-02  Bruno Haible  <bruno@clisp.org>
 
        unistr/u8-strstr: Simplify code.
diff --git a/modules/trim-tests b/modules/trim-tests
new file mode 100644 (file)
index 0000000..326b800
--- /dev/null
@@ -0,0 +1,22 @@
+Files:
+tests/test-trim1.sh
+tests/test-trim2.sh
+tests/test-trim3.sh
+tests/test-trim.c
+tests/macros.h
+m4/locale-fr.m4
+m4/locale-zh.m4
+m4/codeset.m4
+
+Depends-on:
+setlocale
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-trim1.sh test-trim2.sh test-trim3.sh
+TESTS_ENVIRONMENT += \
+  LOCALE_FR_UTF8='@LOCALE_FR_UTF8@' \
+  LOCALE_ZH_CN='@LOCALE_ZH_CN@'
+check_PROGRAMS += test-trim
+test_trim_LDADD = $(LDADD) $(LIBUNISTRING) $(MBRTOWC_LIB)
diff --git a/tests/test-trim.c b/tests/test-trim.c
new file mode 100644 (file)
index 0000000..e2f5043
--- /dev/null
@@ -0,0 +1,158 @@
+/* Tests of removing leading and/or trailing whitespaces.
+   Copyright (C) 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>
+
+/* Specification.  */
+#include "trim.h"
+
+#include <locale.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "macros.h"
+
+static void
+test_ascii (void)
+{
+  {
+    char *result = trim ("");
+    ASSERT (strcmp (result, "") == 0);
+    free (result);
+    result = trim_leading ("");
+    ASSERT (strcmp (result, "") == 0);
+    free (result);
+    result = trim_trailing ("");
+    ASSERT (strcmp (result, "") == 0);
+    free (result);
+  }
+
+  {
+    char *result = trim ("  ");
+    ASSERT (strcmp (result, "") == 0);
+    free (result);
+    result = trim_leading ("  ");
+    ASSERT (strcmp (result, "") == 0);
+    free (result);
+    result = trim_trailing ("  ");
+    ASSERT (strcmp (result, "") == 0);
+    free (result);
+  }
+
+  {
+    char *result = trim ("Hello world");
+    ASSERT (strcmp (result, "Hello world") == 0);
+    free (result);
+    result = trim_leading ("Hello world");
+    ASSERT (strcmp (result, "Hello world") == 0);
+    free (result);
+    result = trim_trailing ("Hello world");
+    ASSERT (strcmp (result, "Hello world") == 0);
+    free (result);
+  }
+
+  {
+    char *result = trim ("   Hello world");
+    ASSERT (strcmp (result, "Hello world") == 0);
+    free (result);
+    result = trim_leading ("   Hello world");
+    ASSERT (strcmp (result, "Hello world") == 0);
+    free (result);
+    result = trim_trailing ("   Hello world");
+    ASSERT (strcmp (result, "   Hello world") == 0);
+    free (result);
+  }
+
+  {
+    char *result = trim ("Hello world  ");
+    ASSERT (strcmp (result, "Hello world") == 0);
+    free (result);
+    result = trim_leading ("Hello world  ");
+    ASSERT (strcmp (result, "Hello world  ") == 0);
+    free (result);
+    result = trim_trailing ("Hello world  ");
+    ASSERT (strcmp (result, "Hello world") == 0);
+    free (result);
+  }
+
+  {
+    char *result = trim ("   Hello world  ");
+    ASSERT (strcmp (result, "Hello world") == 0);
+    free (result);
+    result = trim_leading ("   Hello world  ");
+    ASSERT (strcmp (result, "Hello world  ") == 0);
+    free (result);
+    result = trim_trailing ("   Hello world  ");
+    ASSERT (strcmp (result, "   Hello world") == 0);
+    free (result);
+  }
+}
+
+int
+main (int argc, char *argv[])
+{
+  /* configure should already have checked that the locale is supported.  */
+  if (setlocale (LC_ALL, "") == NULL)
+    return 1;
+
+  /* Test ASCII arguments.  */
+  test_ascii ();
+
+  if (argc > 1)
+    switch (argv[1][0])
+      {
+      case '1':
+        /* C or POSIX locale.  */
+        return 0;
+
+      case '2':
+        /* Locale encoding is UTF-8.  */
+        { /* U+2002 EN SPACE */
+          char *result = trim ("\342\200\202\302\267foo\342\200\202");
+          ASSERT (strcmp (result, "\302\267foo") == 0);
+          free (result);
+        }
+        { /* U+3000 IDEOGRAPHIC SPACE */
+          char *result = trim ("\343\200\200\302\267foo\343\200\200");
+          ASSERT (strcmp (result, "\302\267foo") == 0);
+          free (result);
+        }
+        return 0;
+
+      case '3':
+        /* Locale encoding is GB18030.  */
+        #if !(defined __FreeBSD__ || defined __DragonFly__ || defined __sun)
+        { /* U+2002 EN SPACE */
+          char *result = trim ("\201\066\243\070\241\244foo\201\066\243\070");
+          ASSERT (strcmp (result, "\241\244foo") == 0);
+          free (result);
+        }
+        #endif
+        #if !(defined __FreeBSD__ || defined __DragonFly__)
+        { /* U+3000 IDEOGRAPHIC SPACE */
+          char *result = trim ("\241\241\241\244foo\241\241");
+          ASSERT (strcmp (result, "\241\244foo") == 0);
+          free (result);
+        }
+        #endif
+        return 0;
+      }
+
+  return 1;
+}
diff --git a/tests/test-trim1.sh b/tests/test-trim1.sh
new file mode 100644 (file)
index 0000000..7d6ffe0
--- /dev/null
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+# Test in the "C" or "POSIX" locale.
+LC_ALL=C \
+${CHECKER} ./test-trim${EXEEXT} 1
diff --git a/tests/test-trim2.sh b/tests/test-trim2.sh
new file mode 100644 (file)
index 0000000..1c14bd5
--- /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-trim${EXEEXT} 2
diff --git a/tests/test-trim3.sh b/tests/test-trim3.sh
new file mode 100644 (file)
index 0000000..1da4307
--- /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-trim${EXEEXT} 3