X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftalloc_link%2Ftest%2Frun.c;fp=ccan%2Ftalloc_link%2Ftest%2Frun.c;h=e095bd9eb04549eec1e586df1291539a13bdb852;hp=0000000000000000000000000000000000000000;hb=4e5111fb40e1882b8108d61277d3e506cb958124;hpb=f8f434981cbc1959bcae207a6043324b32777b98 diff --git a/ccan/talloc_link/test/run.c b/ccan/talloc_link/test/run.c new file mode 100644 index 00000000..e095bd9e --- /dev/null +++ b/ccan/talloc_link/test/run.c @@ -0,0 +1,116 @@ +#include "talloc_link/talloc_link.h" +#include "tap/tap.h" +#include "talloc_link/talloc_link.c" +#include +#include + +static unsigned int destroy_count = 0; +static int destroy_obj(void *obj) +{ + destroy_count++; + return 0; +} + +int main(int argc, char *argv[]) +{ + void *obj, *p1, *p2, *p3; + + plan_tests(16); + + talloc_enable_leak_report(); + + /* Single parent case. */ + p1 = talloc(NULL, char); + obj = talloc_linked(p1, talloc(NULL, char)); + talloc_set_destructor(obj, destroy_obj); + + ok(destroy_count == 0, "destroy_count = %u", destroy_count); + talloc_free(p1); + ok(destroy_count == 1, "destroy_count = %u", destroy_count); + + /* Dual parent case. */ + p1 = talloc(NULL, char); + obj = talloc_linked(p1, talloc(NULL, char)); + talloc_set_destructor(obj, destroy_obj); + p2 = talloc(NULL, char); + talloc_link(p2, obj); + + talloc_free(p1); + ok(destroy_count == 1, "destroy_count = %u", destroy_count); + talloc_free(p2); + ok(destroy_count == 2, "destroy_count = %u", destroy_count); + + /* Triple parent case. */ + p1 = talloc(NULL, char); + obj = talloc_linked(p1, talloc(NULL, char)); + talloc_set_destructor(obj, destroy_obj); + + p2 = talloc(NULL, char); + p3 = talloc(NULL, char); + + talloc_link(p2, obj); + talloc_link(p3, obj); + + talloc_free(p1); + ok(destroy_count == 2, "destroy_count = %u", destroy_count); + talloc_free(p2); + ok(destroy_count == 2, "destroy_count = %u", destroy_count); + talloc_free(p3); + ok(destroy_count == 3, "destroy_count = %u", destroy_count); + + /* Single delink case. */ + p1 = talloc(NULL, char); + obj = talloc_linked(p1, talloc(NULL, char)); + talloc_set_destructor(obj, destroy_obj); + + ok(destroy_count == 3, "destroy_count = %u", destroy_count); + talloc_delink(p1, obj); + ok(destroy_count == 4, "destroy_count = %u", destroy_count); + talloc_free(p1); + + /* Double delink case. */ + p1 = talloc(NULL, char); + obj = talloc_linked(p1, talloc(NULL, char)); + talloc_set_destructor(obj, destroy_obj); + + p2 = talloc(NULL, char); + talloc_link(p2, obj); + + talloc_delink(p1, obj); + ok(destroy_count == 4, "destroy_count = %u", destroy_count); + talloc_delink(p2, obj); + ok(destroy_count == 5, "destroy_count = %u", destroy_count); + talloc_free(p1); + talloc_free(p2); + + /* Delink and free. */ + p1 = talloc(NULL, char); + obj = talloc_linked(p1, talloc(NULL, char)); + talloc_set_destructor(obj, destroy_obj); + p2 = talloc(NULL, char); + talloc_link(p2, obj); + + talloc_delink(p1, obj); + ok(destroy_count == 5, "destroy_count = %u", destroy_count); + talloc_free(p2); + ok(destroy_count == 6, "destroy_count = %u", destroy_count); + talloc_free(p1); + + /* Free and delink. */ + p1 = talloc(NULL, char); + obj = talloc_linked(p1, talloc(NULL, char)); + talloc_set_destructor(obj, destroy_obj); + p2 = talloc(NULL, char); + talloc_link(p2, obj); + + talloc_free(p1); + ok(destroy_count == 6, "destroy_count = %u", destroy_count); + talloc_delink(p2, obj); + ok(destroy_count == 7, "destroy_count = %u", destroy_count); + talloc_free(p2); + + /* No leaks? */ + ok1(talloc_total_size(NULL) == 0); + + return exit_status(); +}