]> git.ozlabs.org Git - ccan/blob - ccan/tal/str/test/run-strreg.c
a2a7dd95ae7410a752d717442c45d50d9d2da5f8
[ccan] / ccan / tal / str / test / run-strreg.c
1 #include <ccan/tal/str/str.h>
2 #include <ccan/tal/str/str.c>
3 #include <ccan/tap/tap.h>
4
5 static unsigned int tal_total_blocks(tal_t *ctx)
6 {
7         unsigned int num = 1;
8         tal_t *i;
9
10         for (i = tal_first(ctx); i; i = tal_next(ctx, i))
11                 num++;
12         return num;
13 }
14
15 static bool find_parent(tal_t *child, tal_t *parent)
16 {
17         tal_t *i;
18
19         for (i = child; i; i = tal_parent(i))
20                 if (i == parent)
21                         return true;
22
23         return false;
24 }
25
26 int main(int argc, char *argv[])
27 {
28         void *ctx = tal_strdup(NULL, "toplevel");
29         unsigned int top_blocks = tal_total_blocks(ctx);
30         char *a, *b;
31         /* If it accesses this, it will crash. */
32         char **invalid = (char **)1L;
33
34         plan_tests(25);
35         /* Simple matching. */
36         ok1(strreg(ctx, "hello world!", "hello") == true);
37         ok1(strreg(ctx, "hello world!", "hi") == false);
38
39         /* No parentheses means we don't use any extra args. */
40         ok1(strreg(ctx, "hello world!", "hello", invalid) == true);
41         ok1(strreg(ctx, "hello world!", "hi", invalid) == false);
42
43         ok1(strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
44         ok1(strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
45         /* Found string */
46         ok1(streq(a, "hello"));
47         /* Allocated off ctx */
48         ok1(find_parent(a, ctx));
49         tal_free(a);
50
51         ok1(strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
52                    &a, &b, invalid) == true);
53         ok1(streq(a, "hello"));
54         ok1(streq(b, "world"));
55         ok1(find_parent(a, ctx));
56         ok1(find_parent(b, ctx));
57         tal_free(a);
58         tal_free(b);
59
60         /* * after parentheses returns last match. */
61         ok1(strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
62                    &a, &b, invalid) == true);
63         ok1(streq(a, "o"));
64         ok1(streq(b, "world"));
65         tal_free(a);
66         tal_free(b);
67
68         /* Nested parentheses are ordered by open brace. */
69         ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
70                    &a, &b, invalid) == true);
71         ok1(streq(a, "hello world"));
72         ok1(streq(b, "hello"));
73         tal_free(a);
74         tal_free(b);
75
76         /* Nested parentheses are ordered by open brace. */
77         ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
78                    &a, &b, invalid) == true);
79         ok1(streq(a, "hello world"));
80         ok1(streq(b, "hello"));
81         tal_free(a);
82         tal_free(b);
83
84         /* NULL means we're not interested. */
85         ok1(strreg(ctx, "hello world!", "((hello|goodbye) world)",
86                    &a, NULL, invalid) == true);
87         ok1(streq(a, "hello world"));
88         tal_free(a);
89
90         /* No leaks! */
91         ok1(tal_total_blocks(ctx) == top_blocks);
92         tal_free(ctx);
93
94         return exit_status();
95 }