diff options
| author | William Casarin <jb55@jb55.com> | 2018-07-09 12:10:32 -0700 |
|---|---|---|
| committer | William Casarin <jb55@jb55.com> | 2018-07-09 12:10:32 -0700 |
| commit | 1b8fbbd843ddeb5fc81c9303db9c590a436d499b (patch) | |
| tree | a7227dfe8e4fbaee7b1e0b58b24994dce8078f3f /ccan/tal/test/run-destructor.c | |
| parent | 37a9cdd2e80386f2c94e14e4f511284ae14c745a (diff) | |
progress
Diffstat (limited to 'ccan/tal/test/run-destructor.c')
| -rw-r--r-- | ccan/tal/test/run-destructor.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/ccan/tal/test/run-destructor.c b/ccan/tal/test/run-destructor.c new file mode 100644 index 0000000..7183f7c --- /dev/null +++ b/ccan/tal/test/run-destructor.c @@ -0,0 +1,68 @@ +#include <ccan/tal/tal.h> +#include <ccan/tal/tal.c> +#include <ccan/tap/tap.h> + +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 UNNEEDED) +{ + destroy_count++; +} + +int main(void) +{ + char *child2; + + plan_tests(18); + + destroy_count = 0; + 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); + + destroy_count = 0; + parent = tal(NULL, char); + child = tal(parent, char); + ok1(tal_add_destructor(parent, destroy_parent)); + ok1(tal_add_destructor(child, destroy_child)); + ok1(tal_del_destructor(child, destroy_child)); + tal_free(parent); + ok1(destroy_count == 1); + + destroy_count = 0; + 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 == 4); + + tal_cleanup(); + return exit_status(); +} |
