]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
strcasecmp: Add tests.
authorBruno Haible <bruno@clisp.org>
Sun, 16 Feb 2025 17:02:00 +0000 (18:02 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 16 Feb 2025 17:02:00 +0000 (18:02 +0100)
* tests/test-strcasecmp-1.sh: New file.
* tests/test-strcasecmp-2.sh: New file.
* tests/test-strcasecmp.c: New file.
* modules/strcasecmp-tests: New file.

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

index bf5e91746b7146871576e0fdec275602fd00fddf..052ec74b9dcd946faf8ebf7406a6b3efb70d0653 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2025-02-16  Bruno Haible  <bruno@clisp.org>
 
+       strcasecmp: Add tests.
+       * tests/test-strcasecmp-1.sh: New file.
+       * tests/test-strcasecmp-2.sh: New file.
+       * tests/test-strcasecmp.c: New file.
+       * modules/strcasecmp-tests: New file.
+
        strcasecmp: Work around Solaris, Cygwin bug.
        * lib/strings.in.h (strcasecmp): Consider REPLACE_STRCASECMP. Use the
        usual idioms.
diff --git a/modules/strcasecmp-tests b/modules/strcasecmp-tests
new file mode 100644 (file)
index 0000000..aeff038
--- /dev/null
@@ -0,0 +1,21 @@
+Files:
+tests/test-strcasecmp-1.sh
+tests/test-strcasecmp-2.sh
+tests/test-strcasecmp.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-strcasecmp-1.sh test-strcasecmp-2.sh
+TESTS_ENVIRONMENT += \
+  LOCALE_FR='@LOCALE_FR@'
+check_PROGRAMS += test-strcasecmp
+test_strcasecmp_LDADD = $(LDADD) $(SETLOCALE_LIB)
diff --git a/tests/test-strcasecmp-1.sh b/tests/test-strcasecmp-1.sh
new file mode 100755 (executable)
index 0000000..1a8ad80
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/sh
+
+# Test in the POSIX locale.
+LC_ALL=C \
+${CHECKER} ./test-strcasecmp${EXEEXT} 1 || exit 1
+LC_ALL=POSIX \
+${CHECKER} ./test-strcasecmp${EXEEXT} 1 || exit 1
+
+exit 0
diff --git a/tests/test-strcasecmp-2.sh b/tests/test-strcasecmp-2.sh
new file mode 100755 (executable)
index 0000000..b590a7a
--- /dev/null
@@ -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-strcasecmp${EXEEXT} 2
diff --git a/tests/test-strcasecmp.c b/tests/test-strcasecmp.c
new file mode 100644 (file)
index 0000000..0be6ffa
--- /dev/null
@@ -0,0 +1,73 @@
+/* Test of strcasecmp() 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 <https://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+#include <strings.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (strcasecmp, int, (const char *, const char *));
+
+#include <locale.h>
+#include <stdlib.h>
+
+#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 (strcasecmp ("paragraph", "Paragraph") == 0);
+
+  ASSERT (strcasecmp ("paragrapH", "parAgRaph") == 0);
+
+  ASSERT (strcasecmp ("paragraph", "paraLyzed") < 0);
+  ASSERT (strcasecmp ("paraLyzed", "paragraph") > 0);
+
+  ASSERT (strcasecmp ("para", "paragraph") < 0);
+  ASSERT (strcasecmp ("paragraph", "para") > 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 (strcasecmp ("Fej\311r", "Fej\351r") == 0);
+        ASSERT (strcasecmp ("Fej\351r", "Fej\311r") == 0);
+        ASSERT (strcasecmp ("Fejer", "Fej\311r") < 0);
+        ASSERT (strcasecmp ("Fej\311r", "Fejer") > 0);
+
+        /* Compare with U+00D7 MULTIPLICATION SIGN */
+        ASSERT (strcasecmp ("Fej\311r", "Fej\327") > 0);
+        ASSERT (strcasecmp ("Fej\327", "Fej\311r") < 0);
+        ASSERT (strcasecmp ("Fej\351r", "Fej\327") > 0);
+        ASSERT (strcasecmp ("Fej\327", "Fej\351r") < 0);
+
+        return test_exit_status;
+      }
+
+  return 1;
+}