tal: add TAL_TAKE.
[ccan] / ccan / tal / test / run-take.c
1 #include <ccan/tal/tal.h>
2 #include <ccan/tal/tal.c>
3 #include <ccan/tap/tap.h>
4
5 int main(void)
6 {
7         char *parent, *c;
8
9         plan_tests(13);
10
11         parent = tal(NULL, char);
12         ok1(parent);
13
14         c = tal_strdup(parent, "hello");
15
16         c = tal_strdup(TAL_TAKE, c);
17         ok1(strcmp(c, "hello") == 0);
18         ok1(tal_parent(c) == parent);
19
20         c = tal_strndup(TAL_TAKE, c, 5);
21         ok1(strcmp(c, "hello") == 0);
22         ok1(tal_parent(c) == parent);
23
24         c = tal_strndup(TAL_TAKE, c, 3);
25         ok1(strcmp(c, "hel") == 0);
26         ok1(tal_parent(c) == parent);
27
28         c = tal_memdup(TAL_TAKE, c, 1);
29         ok1(c[0] == 'h');
30         ok1(tal_parent(c) == parent);
31
32         /* No leftover allocations. */
33         tal_free(c);
34         ok1(tal_first(parent) == NULL);
35
36         c = tal_strdup(parent, "hello %s");
37         c = tal_asprintf(TAL_TAKE, c, "there");
38         ok1(strcmp(c, "hello there") == 0);
39         ok1(tal_parent(c) == parent);
40         /* No leftover allocations. */
41         tal_free(c);
42         ok1(tal_first(parent) == NULL);
43
44         tal_free(parent);
45
46         return exit_status();
47 }