compiler: RETURNS_NONNULL macro
[ccan] / ccan / tal / test / run-destructor2.c
1 #include <ccan/tal/tal.h>
2 #include <ccan/tal/tal.c>
3 #include <ccan/tap/tap.h>
4
5 static void destroy_inc(char *p UNNEEDED, int *destroy_count)
6 {
7         (*destroy_count)++;
8 }
9
10 static void destroy_dec(char *p UNNEEDED, int *destroy_count)
11 {
12         (*destroy_count)--;
13 }
14
15 int main(void)
16 {
17         char *p;
18         int destroy_count1 = 0, destroy_count2 = 0;
19
20         plan_tests(10);
21
22         p = tal(NULL, char);
23         /* Del must match both fn and arg. */
24         ok1(tal_add_destructor2(p, destroy_inc, &destroy_count1));
25         ok1(!tal_del_destructor2(p, destroy_inc, &destroy_count2));
26         ok1(!tal_del_destructor2(p, destroy_dec, &destroy_count1));
27         ok1(tal_del_destructor2(p, destroy_inc, &destroy_count1));
28         ok1(!tal_del_destructor2(p, destroy_inc, &destroy_count1));
29
30         ok1(tal_add_destructor2(p, destroy_inc, &destroy_count1));
31         ok1(tal_add_destructor2(p, destroy_dec, &destroy_count2));
32         ok1(tal_free(p) == NULL);
33         ok1(destroy_count1 == 1);
34         ok1(destroy_count2 == -1);
35
36         tal_cleanup();
37         return exit_status();
38 }