tal/str: move tal string functions here from tal.
[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
5 int main(void)
6 {
7         char *parent, *c;
8
9         plan_tests(14);
10
11         parent = tal(NULL, char);
12         ok1(parent);
13
14         c = tal_strdup(parent, "hello");
15
16         c = tal_strdup(parent, take(c));
17         ok1(strcmp(c, "hello") == 0);
18         ok1(tal_parent(c) == parent);
19
20         c = tal_strndup(parent, take(c), 5);
21         ok1(strcmp(c, "hello") == 0);
22         ok1(tal_parent(c) == parent);
23
24         c = tal_strndup(parent, take(c), 3);
25         ok1(strcmp(c, "hel") == 0);
26         ok1(tal_parent(c) == parent);
27         tal_free(c);
28
29         c = tal_strdup(parent, "hello %s");
30         c = tal_asprintf(parent, take(c), "there");
31         ok1(strcmp(c, "hello there") == 0);
32         ok1(tal_parent(c) == parent);
33         /* No leftover allocations. */
34         tal_free(c);
35         ok1(tal_first(parent) == NULL);
36
37         tal_free(parent);
38         ok1(!taken_any());
39
40         /* NULL pass-through. */
41         c = NULL;
42         ok1(tal_strdup(NULL, take(c)) == NULL);
43         ok1(tal_strndup(NULL, take(c), 5) == NULL);
44         ok1(tal_asprintf(NULL, take(c), 0) == NULL);
45
46         return exit_status();
47 }