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