tal: tal_dup()
[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(15);
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_dup(TAL_TAKE, char, c, 1, 0);
29         ok1(c[0] == 'h');
30         ok1(tal_parent(c) == parent);
31
32         c = tal_dup(TAL_TAKE, char, c, 1, 2);
33         ok1(c[0] == 'h');
34         strcpy(c, "hi");
35         ok1(tal_parent(c) == parent);
36
37         /* No leftover allocations. */
38         tal_free(c);
39         ok1(tal_first(parent) == NULL);
40
41         c = tal_strdup(parent, "hello %s");
42         c = tal_asprintf(TAL_TAKE, c, "there");
43         ok1(strcmp(c, "hello there") == 0);
44         ok1(tal_parent(c) == parent);
45         /* No leftover allocations. */
46         tal_free(c);
47         ok1(tal_first(parent) == NULL);
48
49         tal_free(parent);
50
51         return exit_status();
52 }