tal: add tal_expand().
[ccan] / ccan / tal / test / run-destructor.c
1 #include <ccan/tal/tal.h>
2 #include <ccan/tal/tal.c>
3 #include <ccan/tap/tap.h>
4
5 static char *parent, *child;
6 static int destroy_count;
7
8 /* Parent gets destroyed first. */
9 static void destroy_parent(char *p)
10 {
11         ok1(p == parent);
12         ok1(destroy_count == 0);
13         /* Can still access child. */
14         *child = '1';
15         destroy_count++;
16 }
17
18 static void destroy_child(char *p)
19 {
20         ok1(p == child);
21         ok1(destroy_count == 1);
22         /* Can still access parent (though destructor has been called). */
23         *parent = '1';
24         destroy_count++;
25 }
26
27 static void destroy_inc(char *p)
28 {
29         destroy_count++;
30 }
31
32 int main(void)
33 {
34         char *child2;
35
36         plan_tests(18);
37
38         destroy_count = 0;
39         parent = tal(NULL, char);
40         child = tal(parent, char);
41         ok1(tal_add_destructor(parent, destroy_parent));
42         ok1(tal_add_destructor(child, destroy_child));
43         tal_free(parent);
44         ok1(destroy_count == 2);
45
46         destroy_count = 0;
47         parent = tal(NULL, char);
48         child = tal(parent, char);
49         ok1(tal_add_destructor(parent, destroy_parent));
50         ok1(tal_add_destructor(child, destroy_child));
51         ok1(tal_del_destructor(child, destroy_child));
52         tal_free(parent);
53         ok1(destroy_count == 1);
54
55         destroy_count = 0;
56         parent = tal(NULL, char);
57         child = tal(parent, char);
58         child2 = tal(parent, char);
59         ok1(tal_add_destructor(parent, destroy_inc));
60         ok1(tal_add_destructor(parent, destroy_inc));
61         ok1(tal_add_destructor(child, destroy_inc));
62         ok1(tal_add_destructor(child2, destroy_inc));
63         tal_free(parent);
64         ok1(destroy_count == 4);
65
66         return exit_status();
67 }