From: Bruno Haible Date: Sun, 2 Apr 2023 18:40:18 +0000 (+0200) Subject: trim: Add tests. X-Git-Tag: v1.0~1530 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=200a5192f650ab3063a3971790c15a65d5c31b2e;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index 463274abee..a559f3dd71 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2023-04-02 Bruno Haible + + 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 unistr/u8-strstr: Simplify code. diff --git a/modules/trim-tests b/modules/trim-tests new file mode 100644 index 0000000000..326b800c3f --- /dev/null +++ b/modules/trim-tests @@ -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 index 0000000000..e2f5043ee7 --- /dev/null +++ b/tests/test-trim.c @@ -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 . */ + +/* Written by Bruno Haible , 2023. */ + +#include + +/* Specification. */ +#include "trim.h" + +#include +#include +#include +#include + +#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 index 0000000000..7d6ffe0ace --- /dev/null +++ b/tests/test-trim1.sh @@ -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 index 0000000000..1c14bd5d1a --- /dev/null +++ b/tests/test-trim2.sh @@ -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 index 0000000000..1da4307a79 --- /dev/null +++ b/tests/test-trim3.sh @@ -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