]> git.ozlabs.org Git - ccan/blob - ccan/tal/str/test/run-take.c
tal/path: fix unset vars in error paths.
[ccan] / ccan / tal / str / test / run-take.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 int main(void)
7 {
8         char *parent, *c;
9
10         plan_tests(14);
11
12         parent = tal(NULL, char);
13         ok1(parent);
14
15         c = tal_strdup(parent, "hello");
16
17         c = tal_strdup(parent, take(c));
18         ok1(strcmp(c, "hello") == 0);
19         ok1(tal_parent(c) == parent);
20
21         c = tal_strndup(parent, take(c), 5);
22         ok1(strcmp(c, "hello") == 0);
23         ok1(tal_parent(c) == parent);
24
25         c = tal_strndup(parent, take(c), 3);
26         ok1(strcmp(c, "hel") == 0);
27         ok1(tal_parent(c) == parent);
28         tal_free(c);
29
30         c = tal_strdup(parent, "hello %s");
31         c = tal_fmt(parent, take(c), "there");
32         ok1(strcmp(c, "hello there") == 0);
33         ok1(tal_parent(c) == parent);
34         /* No leftover allocations. */
35         tal_free(c);
36         ok1(no_children(parent));
37
38         tal_free(parent);
39         ok1(!taken_any());
40
41         /* NULL pass-through. */
42         c = NULL;
43         ok1(tal_strdup(NULL, take(c)) == NULL);
44         ok1(tal_strndup(NULL, take(c), 5) == NULL);
45         ok1(tal_fmt(NULL, take(c), 0) == NULL);
46
47         return exit_status();
48 }