diff options
author | Bruno Haible <bruno@clisp.org> | 2023-04-02 20:40:18 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2023-04-02 21:04:36 +0200 |
commit | 200a5192f650ab3063a3971790c15a65d5c31b2e (patch) | |
tree | 9c0b5d82af7f951cf81363056eecb5b78004941c /tests | |
parent | 1ca053ce4128c8a8fbd0ece8b4a26ec5ce791933 (diff) | |
download | gnulib-200a5192f650ab3063a3971790c15a65d5c31b2e.tar.gz |
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.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test-trim.c | 158 | ||||
-rw-r--r-- | tests/test-trim1.sh | 5 | ||||
-rw-r--r-- | tests/test-trim2.sh | 15 | ||||
-rw-r--r-- | tests/test-trim3.sh | 15 |
4 files changed, 193 insertions, 0 deletions
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 <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 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 |