1 #include <ccan/tal/link/link.c>
2 #include <ccan/tap/tap.h>
3 #include <stdlib.h>
4 #include <err.h>
5
6 static unsigned int destroy_count = 0;
destroy_obj(void * obj UNNEEDED)7 static void destroy_obj(void *obj UNNEEDED)
8 {
9 destroy_count++;
10 }
11
main(void)12 int main(void)
13 {
14 char *linkable, *p1, *p2, *p3;
15 void **voidpp;
16
17 plan_tests(23);
18
19 linkable = tal(NULL, char);
20 ok1(tal_linkable(linkable) == linkable);
21 ok1(tal_add_destructor(linkable, destroy_obj));
22 /* First, free it immediately. */
23 tal_free(linkable);
24 ok1(destroy_count == 1);
25
26 /* Now create and remove a single link. */
27 linkable = tal_linkable(tal(NULL, char));
28 ok1(tal_add_destructor(linkable, destroy_obj));
29 ok1(p1 = tal_link(NULL, linkable));
30 ok1(p1 == linkable);
31 tal_delink(NULL, linkable);
32 ok1(destroy_count == 2);
33
34 /* Two links.*/
35 linkable = tal_linkable(tal(NULL, char));
36 ok1(tal_add_destructor(linkable, destroy_obj));
37 ok1(p1 = tal_link(NULL, linkable));
38 ok1(p1 == linkable);
39 ok1(p2 = tal_link(NULL, linkable));
40 ok1(p2 == linkable);
41 tal_delink(NULL, linkable);
42 tal_delink(NULL, linkable);
43 ok1(destroy_count == 3);
44
45 /* Three links.*/
46 linkable = tal_linkable(tal(NULL, char));
47 ok1(tal_add_destructor(linkable, destroy_obj));
48 ok1(p1 = tal_link(NULL, linkable));
49 ok1(p1 == linkable);
50 ok1(p2 = tal_link(NULL, linkable));
51 ok1(p2 == linkable);
52 ok1(p3 = tal_link(NULL, linkable));
53 ok1(p3 == linkable);
54 tal_delink(NULL, linkable);
55 tal_delink(NULL, linkable);
56 tal_delink(NULL, linkable);
57 ok1(destroy_count == 4);
58
59 /* Now, indirectly. */
60 voidpp = tal(NULL, void *);
61 linkable = tal_linkable(tal(NULL, char));
62 ok1(tal_add_destructor(linkable, destroy_obj));
63 /* Suppress gratuitous warning with tests_compile_without_features */
64 #if HAVE_STATEMENT_EXPR
65 tal_link(voidpp, linkable);
66 #else
67 (void)tal_link(voidpp, linkable);
68 #endif
69 tal_free(voidpp);
70 ok1(destroy_count == 5);
71
72 return exit_status();
73 }
74