From: Bruno Haible Date: Wed, 25 Jun 2025 13:52:40 +0000 (+0200) Subject: kwset: Add tests. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=84f19f053c483ae5268c580c9629abc2d08d41f5;p=thirdparty%2Fgnulib.git kwset: Add tests. * lib/kwset.c (kwsexec): Correct documentation. * tests/test-kwset.c: New file. * modules/kwset-tests: New file. --- diff --git a/ChangeLog b/ChangeLog index b742e91601..1a9a4a9ada 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2025-06-25 Bruno Haible + 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. diff --git a/lib/kwset.c b/lib/kwset.c index 5a3b9c9919..2784ba5371 100644 --- a/lib/kwset.c +++ b/lib/kwset.c @@ -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 index 0000000000..25e46338d0 --- /dev/null +++ b/modules/kwset-tests @@ -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 index 0000000000..e25d7a23b2 --- /dev/null +++ b/tests/test-kwset.c @@ -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 . */ + +/* Written by Bruno Haible , 2025. */ + +#include + +/* 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; +}