]> git.ipfire.org Git - thirdparty/gnulib.git/commitdiff
kwset: Add tests.
authorBruno Haible <bruno@clisp.org>
Wed, 25 Jun 2025 13:52:40 +0000 (15:52 +0200)
committerBruno Haible <bruno@clisp.org>
Wed, 25 Jun 2025 13:58:41 +0000 (15:58 +0200)
* lib/kwset.c (kwsexec): Correct documentation.
* tests/test-kwset.c: New file.
* modules/kwset-tests: New file.

ChangeLog
lib/kwset.c
modules/kwset-tests [new file with mode: 0644]
tests/test-kwset.c [new file with mode: 0644]

index b742e916015eea70d79f090086fbc7252467b67a..1a9a4a9ada7f243f1d19db70404afd8a36115ad1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2025-06-25  Bruno Haible  <bruno@clisp.org>
 
+       kwset: Add tests.
+       * lib/kwset.c (kwsexec): Correct documentation.
+       * tests/test-kwset.c: New file.
+       * modules/kwset-tests: New file.
+
        kwset: Usability tweaks.
        * lib/kwset.h: Make header idempotent and C++ safe. Check that config.h
        was already included.
index 5a3b9c9919855f9c3cc1d0f2c8b1c8327f5e69df..2784ba537145d04f4beabf65b96ea3673cc8f3fd 100644 (file)
@@ -916,10 +916,14 @@ acexec (kwset_t kwset, char const *text, idx_t size,
 
 /* Find the first instance of a KWSET member in TEXT, which has SIZE bytes.
    Return the offset (into TEXT) of the first byte of the matching substring,
-   or -1 if no match is found.  Upon a match, store details in
-   *KWSMATCH: index of matched keyword, start offset (same as the return
-   value), and length.  If LONGEST, find the longest match; otherwise
-   any match will do.  */
+   or -1 if no match is found.
+   Upon a match:
+     - Store details in *KWSMATCH: index of matched keyword, start offset
+       (same as the return value), and length.
+     - If LONGEST, find the longest match that starts at this offset;
+       otherwise any match that starts at this offset will do.
+   NOTE! LONGEST does *not* mean to search for the longest KWSET member
+   across the entire string.  */
 ptrdiff_t
 kwsexec (kwset_t kwset, char const *text, idx_t size,
          struct kwsmatch *kwsmatch, bool longest)
diff --git a/modules/kwset-tests b/modules/kwset-tests
new file mode 100644 (file)
index 0000000..25e4633
--- /dev/null
@@ -0,0 +1,11 @@
+Files:
+tests/test-kwset.c
+tests/macros.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-kwset
+check_PROGRAMS += test-kwset
diff --git a/tests/test-kwset.c b/tests/test-kwset.c
new file mode 100644 (file)
index 0000000..e25d7a2
--- /dev/null
@@ -0,0 +1,148 @@
+/* Test of searching for any of a given set of fixed strings in text.
+   Copyright (C) 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/>.  */
+
+/* Written by Bruno Haible <bruno@clisp.org>, 2025.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include "kwset.h"
+
+#include "macros.h"
+
+int
+main ()
+{
+  /* Trivial case.  */
+  {
+    kwset_t set = kwsalloc (NULL);
+    kwsprep (set);
+
+    ASSERT (kwswords (set) == 0);
+
+    static const char input[] = "foo";
+    struct kwsmatch match;
+    ptrdiff_t ret = kwsexec (set, input, 3, &match, false);
+    ASSERT (ret == -1);
+
+    kwsfree (set);
+  }
+
+  /* Simple case: one fixed string.  */
+  {
+    kwset_t set = kwsalloc (NULL);
+    kwsincr (set, "acab", 4);
+    kwsprep (set);
+
+    ASSERT (kwswords (set) == 1);
+
+    {
+      struct kwsmatch match;
+      ptrdiff_t ret = kwsexec (set, "abracadabracab", 14, &match, false);
+      ASSERT (ret == 10);
+      ASSERT (match.offset == ret);
+      ASSERT (match.index == 0);
+      ASSERT (match.size == 4);
+    }
+    {
+      struct kwsmatch match;
+      ptrdiff_t ret = kwsexec (set, "abracadabra", 11, &match, false);
+      ASSERT (ret == -1);
+    }
+
+    kwsfree (set);
+  }
+
+  /* Any match vs. longest left-most match.  */
+  {
+    kwset_t set = kwsalloc (NULL);
+    kwsincr (set, "acab", 4);
+    kwsincr (set, "acacia", 6);
+    kwsprep (set);
+
+    ASSERT (kwswords (set) == 2);
+
+    {
+      struct kwsmatch match;
+      ptrdiff_t ret = kwsexec (set, "abracadabracabatacaciaeinacabemia", 33, &match, false);
+      ASSERT (ret == 10);
+      ASSERT (match.offset == ret);
+      ASSERT (match.index == 0);
+      ASSERT (match.size == 4);
+    }
+    /* longest = true makes no difference, because it's only about the left-most match.  */
+    {
+      struct kwsmatch match;
+      ptrdiff_t ret = kwsexec (set, "abracadabracabatacaciaeinacabemia", 33, &match, true);
+      ASSERT (ret == 10);
+      ASSERT (match.offset == ret);
+      ASSERT (match.index == 0);
+      ASSERT (match.size == 4);
+    }
+
+    kwsfree (set);
+  }
+
+  /* Any match vs. longest left-most match.  */
+  {
+    kwset_t set = kwsalloc (NULL);
+    kwsincr (set, "acab", 4);
+    kwsincr (set, "acabe", 5);
+    kwsprep (set);
+
+    ASSERT (kwswords (set) == 2);
+
+    {
+      struct kwsmatch match;
+      ptrdiff_t ret = kwsexec (set, "abracadabracabatacaciaeinacabemia", 33, &match, false);
+      ASSERT (ret == 10);
+      ASSERT (match.offset == ret);
+      ASSERT (match.index == 0);
+      ASSERT (match.size == 4);
+    }
+    /* longest = true makes no difference, because it's only about the left-most match.  */
+    {
+      struct kwsmatch match;
+      ptrdiff_t ret = kwsexec (set, "abracadabracabatacaciaeinacabemia", 33, &match, true);
+      ASSERT (ret == 10);
+      ASSERT (match.offset == ret);
+      ASSERT (match.index == 0);
+      ASSERT (match.size == 4);
+    }
+
+    {
+      struct kwsmatch match;
+      ptrdiff_t ret = kwsexec (set, "abracadabracabematacaciaeinacabia", 33, &match, false);
+      ASSERT (ret == 10);
+      ASSERT (match.offset == ret);
+      ASSERT (match.index == 0);
+      ASSERT (match.size == 4);
+    }
+    /* Here, longest = true makes a difference.  */
+    {
+      struct kwsmatch match;
+      ptrdiff_t ret = kwsexec (set, "abracadabracabematacaciaeinacabia", 33, &match, true);
+      ASSERT (ret == 10);
+      ASSERT (match.offset == ret);
+      ASSERT (match.index == 1);
+      ASSERT (match.size == 5);
+    }
+
+    kwsfree (set);
+  }
+
+  return test_exit_status;
+}