X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftal%2Ftest%2Frun-destructor.c;fp=ccan%2Ftal%2Ftest%2Frun-destructor.c;h=9d3a94e60b323a188b3f3f3bcb5d859ad942591f;hb=0e34459a02e2615f50bac2767c7dce6632470946;hp=0000000000000000000000000000000000000000;hpb=33527c60ba3c5c72ed31fbb064c38db2b3d2c733;p=ccan diff --git a/ccan/tal/test/run-destructor.c b/ccan/tal/test/run-destructor.c new file mode 100644 index 00000000..9d3a94e6 --- /dev/null +++ b/ccan/tal/test/run-destructor.c @@ -0,0 +1,57 @@ +#include +#include +#include + +static char *parent, *child; +static int destroy_count; + +/* Parent gets destroyed first. */ +static void destroy_parent(char *p) +{ + ok1(p == parent); + ok1(destroy_count == 0); + /* Can still access child. */ + *child = '1'; + destroy_count++; +} + +static void destroy_child(char *p) +{ + ok1(p == child); + ok1(destroy_count == 1); + /* Can still access parent (though destructor has been called). */ + *parent = '1'; + destroy_count++; +} + +static void destroy_inc(char *p) +{ + destroy_count++; +} + +int main(void) +{ + char *child2; + + plan_tests(12); + + parent = tal(NULL, char); + child = tal(parent, char); + ok1(tal_add_destructor(parent, destroy_parent)); + ok1(tal_add_destructor(child, destroy_child)); + + tal_free(parent); + ok1(destroy_count == 2); + + parent = tal(NULL, char); + child = tal(parent, char); + child2 = tal(parent, char); + ok1(tal_add_destructor(parent, destroy_inc)); + ok1(tal_add_destructor(parent, destroy_inc)); + ok1(tal_add_destructor(child, destroy_inc)); + ok1(tal_add_destructor(child2, destroy_inc)); + tal_free(parent); + ok1(destroy_count == 6); + + return exit_status(); +}