]> git.ozlabs.org Git - ccan/blob - ccan/str_talloc/test/run-strreg.c
tdb2: rename internal hashfn and logfn to hash_fn and log_fn.
[ccan] / ccan / str_talloc / test / run-strreg.c
1 #include <ccan/str_talloc/str_talloc.h>
2 #include <ccan/str_talloc/str_talloc.c>
3 #include <ccan/tap/tap.h>
4
5 int main(int argc, char *argv[])
6 {
7         void *ctx = talloc_init("toplevel");
8         unsigned int top_blocks = talloc_total_blocks(ctx);
9         char *a, *b;
10         /* If it accesses this, it will crash. */
11         char **invalid = (char **)1L;
12
13         plan_tests(25);
14         /* Simple matching. */
15         ok1(strreg(ctx, "hello world!", "hello") == true);
16         ok1(strreg(ctx, "hello world!", "hi") == false);
17
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);
21
22         ok1(strreg(ctx, "hello world!", "[a-z]+", invalid) == true);
23         ok1(strreg(ctx, "hello world!", "([a-z]+)", &a, invalid) == true);
24         /* Found string */
25         ok1(streq(a, "hello"));
26         /* Allocated off ctx */
27         ok1(talloc_find_parent_byname(a, "toplevel") == ctx);
28         talloc_free(a);
29
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);
36         talloc_free(a);
37         talloc_free(b);
38
39         /* * after parentheses returns last match. */
40         ok1(strreg(ctx, "hello world!", "([a-z])* ([a-z]+)",
41                    &a, &b, invalid) == true);
42         ok1(streq(a, "o"));
43         ok1(streq(b, "world"));
44         talloc_free(a);
45         talloc_free(b);
46
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"));
52         talloc_free(a);
53         talloc_free(b);
54
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"));
60         talloc_free(a);
61         talloc_free(b);
62
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"));
67         talloc_free(a);
68
69         /* No leaks! */
70         ok1(talloc_total_blocks(ctx) == top_blocks);
71         talloc_free(ctx);
72         talloc_disable_null_tracking();
73
74         return exit_status();
75 }