summaryrefslogtreecommitdiff
path: root/test/lib/slre.c
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2025-05-29 08:27:13 -0600
committerTom Rini <trini@konsulko.com>2025-05-29 08:27:13 -0600
commit23be77e18d19ddb9c2ecdf71638bacfbc369fd13 (patch)
tree73d82f75a865be88af0e376baa0fc2980f1b4b48 /test/lib/slre.c
parent2f3766949bbea7aa5a472157561d387fd94205d2 (diff)
parent6990cc5257283a631a286a4321a3515c7537f636 (diff)
Merge patch series "regex patches"
Rasmus Villemoes <ravi@prevas.dk> says: This started as a rather simple patch, 1/12, adding the ability to more conveniently do regex matching in shell. But with that, it became very easy to see what the slre library can and especially what it cannot do, and that way I found both outright bugs and a "wow, doesn't it support that syntax" gotcha. I couldn't find any tests ('git grep slre -- test/' was empty), so I added a small test suite and tweaked slre.c. Link: https://lore.kernel.org/r/20250513084034.654865-1-ravi@prevas.dk
Diffstat (limited to 'test/lib/slre.c')
-rw-r--r--test/lib/slre.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/test/lib/slre.c b/test/lib/slre.c
new file mode 100644
index 00000000000..ff2386d614a
--- /dev/null
+++ b/test/lib/slre.c
@@ -0,0 +1,58 @@
+// 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},
+ { "xby", "^a|b", 1},
+ { "xby", "b|^a", 1},
+ { "xby", "b|c$", 1},
+ { "xby", "c$|b", 1},
+ { "", "x*$", 1},
+ { "", "^x*$", 1},
+ { "yy", "x*$", 1},
+ { "yy", "^x*$", 0},
+ { "Gadsby", "^[^eE]*$", 1},
+ { "Ernest", "^[^eE]*$", 0},
+ { "6d41f0a39d6", "^[0123456789abcdef]*$", 1 },
+ /* DIGIT is 17 */
+ { "##\x11%%\x11", "^[#%\\d]*$", 0 },
+ { "##23%%45", "^[#%\\d]*$", 1 },
+ { "U-Boot", "^[B-Uo-t]*$", 0 },
+ { "U-Boot", "^[A-Zm-v-]*$", 1 },
+ { "U-Boot", "^[-A-Za-z]*$", 1 },
+ /* The range --C covers both - and B. */
+ { "U-Boot", "^[--CUot]*$", 1 },
+ { "U-Boot", "^[^0-9]*$", 1 },
+ { "U-Boot", "^[^0-9<->]*$", 1 },
+ { "U-Boot", "^[^0-9<\\->]*$", 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);