From 0a9ad49feb5dcdaf2a252caf95eb9eb19ae1a70d Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 16 Feb 2025 18:24:16 +0100 Subject: [PATCH] strncasecmp: Add tests. * tests/test-strncasecmp-1.sh: New file. * tests/test-strncasecmp-2.sh: New file. * tests/test-strncasecmp.c: New file. * modules/strncasecmp-tests: New file. --- ChangeLog | 6 +++ modules/strncasecmp-tests | 21 +++++++++++ tests/test-strncasecmp-1.sh | 9 +++++ tests/test-strncasecmp-2.sh | 15 ++++++++ tests/test-strncasecmp.c | 73 +++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 modules/strncasecmp-tests create mode 100755 tests/test-strncasecmp-1.sh create mode 100755 tests/test-strncasecmp-2.sh create mode 100644 tests/test-strncasecmp.c diff --git a/ChangeLog b/ChangeLog index e73bdd7804..1fa41775dc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2025-02-16 Bruno Haible + strncasecmp: Add tests. + * tests/test-strncasecmp-1.sh: New file. + * tests/test-strncasecmp-2.sh: New file. + * tests/test-strncasecmp.c: New file. + * modules/strncasecmp-tests: New file. + strncasecmp: Work around Solaris, Cygwin bug. * lib/strings.in.h (strncasecmp): Consider REPLACE_STRNCASECMP. Use the usual idioms. diff --git a/modules/strncasecmp-tests b/modules/strncasecmp-tests new file mode 100644 index 0000000000..54f2a347b3 --- /dev/null +++ b/modules/strncasecmp-tests @@ -0,0 +1,21 @@ +Files: +tests/test-strncasecmp-1.sh +tests/test-strncasecmp-2.sh +tests/test-strncasecmp.c +tests/signature.h +tests/macros.h +m4/locale-fr.m4 +m4/codeset.m4 + +Depends-on: +setlocale + +configure.ac: +gt_LOCALE_FR + +Makefile.am: +TESTS += test-strncasecmp-1.sh test-strncasecmp-2.sh +TESTS_ENVIRONMENT += \ + LOCALE_FR='@LOCALE_FR@' +check_PROGRAMS += test-strncasecmp +test_strncasecmp_LDADD = $(LDADD) $(SETLOCALE_LIB) diff --git a/tests/test-strncasecmp-1.sh b/tests/test-strncasecmp-1.sh new file mode 100755 index 0000000000..b64a876a44 --- /dev/null +++ b/tests/test-strncasecmp-1.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +# Test in the POSIX locale. +LC_ALL=C \ +${CHECKER} ./test-strncasecmp${EXEEXT} 1 || exit 1 +LC_ALL=POSIX \ +${CHECKER} ./test-strncasecmp${EXEEXT} 1 || exit 1 + +exit 0 diff --git a/tests/test-strncasecmp-2.sh b/tests/test-strncasecmp-2.sh new file mode 100755 index 0000000000..83a422d4bf --- /dev/null +++ b/tests/test-strncasecmp-2.sh @@ -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-strncasecmp${EXEEXT} 2 diff --git a/tests/test-strncasecmp.c b/tests/test-strncasecmp.c new file mode 100644 index 0000000000..fe479e408a --- /dev/null +++ b/tests/test-strncasecmp.c @@ -0,0 +1,73 @@ +/* Test of strncasecmp() function. + Copyright (C) 2008-2025 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 . */ + +#include + +#include + +#include "signature.h" +SIGNATURE_CHECK (strncasecmp, int, (const char *, const char *, size_t)); + +#include +#include + +#include "macros.h" + +int +main (int argc, char *argv[]) +{ + /* configure should already have checked that the locale is supported. */ + if (setlocale (LC_ALL, "") == NULL) + return 1; + + ASSERT (strncasecmp ("paragraph", "Paragraph", 9) == 0); + + ASSERT (strncasecmp ("paragrapH", "parAgRaph", 9) == 0); + + ASSERT (strncasecmp ("paragraph", "paraLyzed", 9) < 0); + ASSERT (strncasecmp ("paraLyzed", "paragraph", 9) > 0); + + ASSERT (strncasecmp ("para", "paragraph", 9) < 0); + ASSERT (strncasecmp ("paragraph", "para", 9) > 0); + + if (argc > 1) + switch (argv[1][0]) + { + case '1': + /* C or POSIX locale. */ + return test_exit_status; + + case '2': + /* Locale encoding is ISO-8859-1 or ISO-8859-15. */ + + /* U+00C9 LATIN CAPITAL LETTER E WITH ACUTE */ + /* U+00E9 LATIN SMALL LETTER E WITH ACUTE */ + ASSERT (strncasecmp ("Fej\311r", "Fej\351r", 5) == 0); + ASSERT (strncasecmp ("Fej\351r", "Fej\311r", 5) == 0); + ASSERT (strncasecmp ("Fejer", "Fej\311r", 5) < 0); + ASSERT (strncasecmp ("Fej\311r", "Fejer", 5) > 0); + + /* Compare with U+00D7 MULTIPLICATION SIGN */ + ASSERT (strncasecmp ("Fej\311r", "Fej\327", 5) > 0); + ASSERT (strncasecmp ("Fej\327", "Fej\311r", 5) < 0); + ASSERT (strncasecmp ("Fej\351r", "Fej\327", 5) > 0); + ASSERT (strncasecmp ("Fej\327", "Fej\351r", 5) < 0); + + return test_exit_status; + } + + return 1; +} -- 2.39.5