1 #include <ccan/str_talloc/str_talloc.h>
2 #include <ccan/str_talloc/str_talloc.c>
3 #include <ccan/tap/tap.h>
5 int main(int argc, char *argv[])
7 void *ctx = talloc_init("toplevel");
8 unsigned int top_blocks = talloc_total_blocks(ctx);
10 /* If it accesses this, it will crash. */
11 char **invalid = (char **)1L;
14 /* Simple matching. */
15 ok1(strreg(ctx, "hello world!", "hello") == true);
16 ok1(strreg(ctx, "hello world!", "hi") == false);
18 /* No parentheses means we don't use any extra args. */
19 ok1(strreg(ctx, "hello world!", "hello", invalid) == true);
20 ok1(strreg(ctx, "hello world!", "hi", invalid) == false);
22 ok1(strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
23 ok1(strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
25 ok1(streq(a, "hello"));
26 /* Allocated off ctx */
27 ok1(talloc_find_parent_byname(a, "toplevel") == ctx);
30 ok1(strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
31 &a, &b, invalid) == true);
32 ok1(streq(a, "hello"));
33 ok1(streq(b, "world"));
34 ok1(talloc_find_parent_byname(a, "toplevel") == ctx);
35 ok1(talloc_find_parent_byname(b, "toplevel") == ctx);
39 /* * after parentheses returns last match. */
40 ok1(strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
41 &a, &b, invalid) == true);
43 ok1(streq(b, "world"));
47 /* Nested parentheses are ordered by open brace. */
48 ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
49 &a, &b, invalid) == true);
50 ok1(streq(a, "hello world"));
51 ok1(streq(b, "hello"));
55 /* Nested parentheses are ordered by open brace. */
56 ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
57 &a, &b, invalid) == true);
58 ok1(streq(a, "hello world"));
59 ok1(streq(b, "hello"));
63 /* NULL means we're not interested. */
64 ok1(strreg(ctx, "hello world!", "((hello|goodbye) world)",
65 &a, NULL, invalid) == true);
66 ok1(streq(a, "hello world"));
70 ok1(talloc_total_blocks(ctx) == top_blocks);
72 talloc_disable_null_tracking();