1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2
3 #include <test/lib.h>
4 #include <test/ut.h>
5 #include <slre.h>
6
7 struct re_test {
8 const char *str;
9 const char *re;
10 int match;
11 };
12
13 static const struct re_test re_test[] = {
14 { "123", "^\\d+$", 1},
15 { "x23", "^\\d+$", 0},
16 { "banana", "^([bn]a)*$", 1},
17 { "panama", "^([bn]a)*$", 0},
18 { "xby", "^a|b", 1},
19 { "xby", "b|^a", 1},
20 { "xby", "b|c$", 1},
21 { "xby", "c$|b", 1},
22 { "", "x*$", 1},
23 { "", "^x*$", 1},
24 { "yy", "x*$", 1},
25 { "yy", "^x*$", 0},
26 { "Gadsby", "^[^eE]*$", 1},
27 { "Ernest", "^[^eE]*$", 0},
28 { "6d41f0a39d6", "^[0123456789abcdef]*$", 1 },
29 /* DIGIT is 17 */
30 { "##\x11%%\x11", "^[#%\\d]*$", 0 },
31 { "##23%%45", "^[#%\\d]*$", 1 },
32 { "U-Boot", "^[B-Uo-t]*$", 0 },
33 { "U-Boot", "^[A-Zm-v-]*$", 1 },
34 { "U-Boot", "^[-A-Za-z]*$", 1 },
35 /* The range --C covers both - and B. */
36 { "U-Boot", "^[--CUot]*$", 1 },
37 { "U-Boot", "^[^0-9]*$", 1 },
38 { "U-Boot", "^[^0-9<->]*$", 1 },
39 { "U-Boot", "^[^0-9<\\->]*$", 0 },
40 {}
41 };
42
lib_slre(struct unit_test_state * uts)43 static int lib_slre(struct unit_test_state *uts)
44 {
45 const struct re_test *t;
46
47 for (t = re_test; t->str; t++) {
48 struct slre slre;
49
50 ut_assert(slre_compile(&slre, t->re));
51 ut_assertf(!!slre_match(&slre, t->str, strlen(t->str), NULL) == t->match,
52 "'%s' unexpectedly %s '%s'\n", t->str,
53 t->match ? "didn't match" : "matched", t->re);
54 }
55
56 return 0;
57 }
58 LIB_TEST(lib_slre, 0);
59