X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;ds=sidebyside;f=ccan%2Frbtree%2Ftest%2Frun-many.c;h=9ab96c820ba8da0677f93649825ec01a25289ca0;hb=291237b4fed863be74051274ac5ad9920cb33cc3;hp=6474007fb4f0f259b64231b0e026aa3df34f4f59;hpb=8f2bfcd370a0e00605a7d84600847b150255c533;p=ccan diff --git a/ccan/rbtree/test/run-many.c b/ccan/rbtree/test/run-many.c index 6474007f..9ab96c82 100644 --- a/ccan/rbtree/test/run-many.c +++ b/ccan/rbtree/test/run-many.c @@ -3,8 +3,25 @@ #include #include #include +#include +#include +#define NUM_ELEMS 100 -#define NUM_ELEMS 10000 +/* We want to test talloc failure paths. */ +static void *my_malloc(size_t size) +{ + return malloc(size); +} + +static void my_free(void *ptr) +{ + free(ptr); +} + +static void *my_realloc(void *ptr, size_t size) +{ + return realloc(ptr, size); +} static bool lookup_all(trbt_tree_t *rb, bool exist) { @@ -24,20 +41,37 @@ static bool lookup_all(trbt_tree_t *rb, bool exist) return true; } +static bool add_one(trbt_tree_t *rb, bool exist, unsigned int i) +{ + int *new = talloc_memdup(rb, &i, sizeof(i)); + int *p; + + if (!new) + return false; + + p = trbt_insert32(rb, i, new); + if (p) { + if (!exist) + return false; + if (*p != i) + return false; + } else { + if (exist) + return false; + else + if (!trbt_lookup32(rb, i)) + return false; + } + return true; +} + static bool insert_all(trbt_tree_t *rb, bool exist) { unsigned int i; for (i = 0; i < NUM_ELEMS; i++) { - int *p = trbt_insert32(rb, i, talloc_memdup(rb, &i, sizeof(i))); - if (p) { - if (!exist) - return false; - if (*p != i) - return false; - } else - if (exist) - return false; + if (!add_one(rb, exist, i)) + return false; } return true; } @@ -46,18 +80,36 @@ static void delete_all(trbt_tree_t *rb) { unsigned int i; - for (i = 0; i < NUM_ELEMS; i++) { + /* Don't delete them in the obvious order. */ + for (i = 0; i < NUM_ELEMS / 2; i++) { + trbt_delete32(rb, i); + } + + for (i = NUM_ELEMS-1; i >= NUM_ELEMS / 2; i--) { trbt_delete32(rb, i); } } -int main(void) +static void *ctx; +static trbt_tree_t *rb; + +static void exit_test(void) { - trbt_tree_t *rb; - void *ctx = talloc_init("toplevel"); - unsigned int i; + talloc_free(rb); + ok1(talloc_total_blocks(ctx) == 1); + talloc_free(ctx); + failtest_exit(exit_status()); +} - plan_tests(7); +int main(int argc, char *argv[]) +{ + failtest_init(argc, argv); + tap_fail_callback = exit_test; + plan_tests(8); + + ctx = talloc_strdup(NULL, "toplevel"); + + talloc_set_allocator(my_malloc, my_free, my_realloc); rb = trbt_create(ctx, 0); ok1(rb); @@ -84,6 +136,10 @@ int main(void) /* All are children of rb, so this is clean. */ talloc_free(rb); + /* No memory leaks? */ + ok1(talloc_total_blocks(ctx) == 1); + talloc_free(ctx); + /* This exits depending on whether all tests passed */ - return exit_status(); + failtest_exit(exit_status()); }