diff options
author | Rasmus Villemoes <ravi@prevas.dk> | 2025-05-13 10:40:25 +0200 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2025-05-29 08:25:18 -0600 |
commit | ced883d92c0568cdb15b5b67106c29a4623b19d8 (patch) | |
tree | 384629a21a644139951170c565838ed5580dcb99 /test/lib/slre.c | |
parent | a5af8f9ad2779e2a18bc9d723c687ee2213086f0 (diff) |
test: slre: add tests for regex library
Inspecting the slre.c code reveals a few bugs; those are easy to
demonstrate with the new '=~' test operator. Before fixing them, let's
add a place to add test cases.
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Rasmus Villemoes <ravi@prevas.dk>
Diffstat (limited to 'test/lib/slre.c')
-rw-r--r-- | test/lib/slre.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/test/lib/slre.c b/test/lib/slre.c new file mode 100644 index 00000000000..51a50b269aa --- /dev/null +++ b/test/lib/slre.c @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 OR MIT + +#include <test/lib.h> +#include <test/ut.h> +#include <slre.h> + +struct re_test { + const char *str; + const char *re; + int match; +}; + +static const struct re_test re_test[] = { + { "123", "^\\d+$", 1}, + { "x23", "^\\d+$", 0}, + { "banana", "^([bn]a)*$", 1}, + { "panama", "^([bn]a)*$", 0}, + {} +}; + +static int lib_slre(struct unit_test_state *uts) +{ + const struct re_test *t; + + for (t = re_test; t->str; t++) { + struct slre slre; + + ut_assert(slre_compile(&slre, t->re)); + ut_assertf(!!slre_match(&slre, t->str, strlen(t->str), NULL) == t->match, + "'%s' unexpectedly %s '%s'\n", t->str, + t->match ? "didn't match" : "matched", t->re); + } + + return 0; +} +LIB_TEST(lib_slre, 0); |