]> git.ozlabs.org Git - ccan/blob - ccan/tal/str/test/run-strreg.c
tal/str: accept take() for arguments.
[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 bool find_parent(tal_t *child, tal_t *parent)
6 {
7         tal_t *i;
8
9         for (i = child; i; i = tal_parent(i))
10                 if (i == parent)
11                         return true;
12
13         return false;
14 }
15
16 int main(int argc, char *argv[])
17 {
18         void *ctx = tal_strdup(NULL, "toplevel");
19         char *a, *b;
20         /* If it accesses this, it will crash. */
21         char **invalid = (char **)1L;
22
23         plan_tests(40);
24         /* Simple matching. */
25         ok1(strreg(ctx, "hello world!", "hello") == true);
26         ok1(strreg(ctx, "hello world!", "hi") == false);
27
28         /* No parentheses means we don't use any extra args. */
29         ok1(strreg(ctx, "hello world!", "hello", invalid) == true);
30         ok1(strreg(ctx, "hello world!", "hi", invalid) == false);
31
32         ok1(strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
33         ok1(strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
34         /* Found string */
35         ok1(streq(a, "hello"));
36         /* Allocated off ctx */
37         ok1(find_parent(a, ctx));
38         tal_free(a);
39
40         ok1(strreg(ctx, "hello world!", "([a-z]*) ([a-z]+)",
41                    &a, &b, invalid) == true);
42         ok1(streq(a, "hello"));
43         ok1(streq(b, "world"));
44         ok1(find_parent(a, ctx));
45         ok1(find_parent(b, ctx));
46         tal_free(a);
47         tal_free(b);
48
49         /* * after parentheses returns last match. */
50         ok1(strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
51                    &a, &b, invalid) == true);
52         ok1(streq(a, "o"));
53         ok1(streq(b, "world"));
54         tal_free(a);
55         tal_free(b);
56
57         /* Nested parentheses are ordered by open brace. */
58         ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
59                    &a, &b, invalid) == true);
60         ok1(streq(a, "hello world"));
61         ok1(streq(b, "hello"));
62         tal_free(a);
63         tal_free(b);
64
65         /* Nested parentheses are ordered by open brace. */
66         ok1(strreg(ctx, "hello world!", "(([a-z]*) world)",
67                    &a, &b, invalid) == true);
68         ok1(streq(a, "hello world"));
69         ok1(streq(b, "hello"));
70         tal_free(a);
71         tal_free(b);
72
73         /* NULL means we're not interested. */
74         ok1(strreg(ctx, "hello world!", "((hello|goodbye) world)",
75                    &a, NULL, invalid) == true);
76         ok1(streq(a, "hello world"));
77         tal_free(a);
78
79         /* No leaks! */
80         ok1(!tal_first(ctx));
81
82         /* NULL arg with take means always fail. */
83         ok1(strreg(ctx, take(NULL), "((hello|goodbye) world)",
84                    &b, NULL, invalid) == false);
85
86         /* Take string. */
87         a = tal_strdup(ctx, "hello world!");
88         ok1(strreg(ctx, take(a), "([a-z]+)", &b, invalid) == true);
89         ok1(streq(b, "hello"));
90         ok1(tal_parent(b) == ctx);
91         tal_free(b);
92         ok1(tal_first(ctx) == NULL);
93
94         /* Take regex. */
95         a = tal_strdup(ctx, "([a-z]+)");
96         ok1(strreg(ctx, "hello world!", take(a), &b, invalid) == true);
97         ok1(streq(b, "hello"));
98         ok1(tal_parent(b) == ctx);
99         tal_free(b);
100         ok1(tal_first(ctx) == NULL);
101
102         /* Take both. */
103         a = tal_strdup(ctx, "([a-z]+)");
104         ok1(strreg(ctx, take(tal_strdup(ctx, "hello world!")),
105                    take(a), &b, invalid) == true);
106         ok1(streq(b, "hello"));
107         ok1(tal_parent(b) == ctx);
108         tal_free(b);
109         ok1(tal_first(ctx) == NULL);
110
111         /* ... even if we fail to match. */
112         a = tal_strdup(ctx, "([a-z]+)");
113         ok1(strreg(ctx, take(tal_strdup(ctx, "HELLO WORLD!")),
114                    take(a), &b, invalid) == false);
115         ok1(tal_first(ctx) == NULL);
116         tal_free(ctx);
117
118         return exit_status();
119 }