]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
test: slre: add tests for regex library
authorRasmus Villemoes <ravi@prevas.dk>
Tue, 13 May 2025 08:40:25 +0000 (10:40 +0200)
committerTom Rini <trini@konsulko.com>
Thu, 29 May 2025 14:25:18 +0000 (08:25 -0600)
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>
MAINTAINERS
test/lib/Makefile
test/lib/slre.c [new file with mode: 0644]

index 94c62daf83484fb177bbc13d9976141a2f19f51e..111e2767917359ca6b43859d4096bc4221a66b9c 100644 (file)
@@ -1633,6 +1633,7 @@ M:        Rasmus Villemoes <ravi@prevas.dk>
 S:     Maintained
 F:     include/slre.h
 F:     lib/slre.c
+F:     test/lib/slre.c
 
 SMCCC TRNG
 M:     Etienne Carriere <etienne.carriere@linaro.org>
index d620510f9981e0f53f8f62537eba3dd883df8f04..ff4ff63270df5a7470a50884c16e7373f76bc2ce 100644 (file)
@@ -29,6 +29,7 @@ obj-$(CONFIG_SHA256) += test_sha256_hmac.o
 obj-$(CONFIG_HKDF_MBEDTLS) += test_sha256_hkdf.o
 obj-$(CONFIG_GETOPT) += getopt.o
 obj-$(CONFIG_CRC8) += test_crc8.o
+obj-$(CONFIG_REGEX) += slre.o
 obj-$(CONFIG_UT_LIB_CRYPT) += test_crypt.o
 obj-$(CONFIG_UT_TIME) += time.o
 obj-$(CONFIG_$(PHASE_)UT_UNICODE) += unicode.o
diff --git a/test/lib/slre.c b/test/lib/slre.c
new file mode 100644 (file)
index 0000000..51a50b2
--- /dev/null
@@ -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);