X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Frbtree%2Ftest%2Frun.c;h=11d0ed78b015f03b252986ab4ea4cead5cde7ae9;hp=67ffd72d3df2cb0a481ab3a8dba17a869ec9a261;hb=8f2bfcd370a0e00605a7d84600847b150255c533;hpb=c5074939f21be43eb408dbab5f6708dd74dba531;ds=sidebyside diff --git a/ccan/rbtree/test/run.c b/ccan/rbtree/test/run.c index 67ffd72d..11d0ed78 100644 --- a/ccan/rbtree/test/run.c +++ b/ccan/rbtree/test/run.c @@ -1,23 +1,69 @@ #include #include +#include +#include + +static void *insert_callback(void *param, void *data) +{ + ok1(data == param); + return talloc_strdup(NULL, "insert_callback"); +} int main(void) { + trbt_tree_t *rb; + void *ctx = talloc_init("toplevel"); + char *data, *data2; + /* This is how many tests you plan to run */ - plan_tests(3); - - /* Simple thing we expect to succeed */ - ok1(some_test()) - /* Same, with an explicit description of the test. */ - ok(some_test(), "%s with no args should return 1", "some_test") - /* How to print out messages for debugging. */ - diag("Address of some_test is %p", &some_test) - /* Conditional tests must be explicitly skipped. */ -#if HAVE_SOME_FEATURE - ok1(test_some_feature()) -#else - skip(1, "Don't have SOME_FEATURE") -#endif + plan_tests(18); + + rb = trbt_create(ctx, 0); + ok1(rb); + ok1(talloc_is_parent(rb, ctx)); + + /* Failed lookup. */ + ok1(trbt_lookup32(rb, 0) == NULL); + ok1(trbt_lookup32(rb, -1) == NULL); + + /* Insert, should steal node onto data. */ + data = talloc_strdup(NULL, "data"); + ok1(trbt_insert32(rb, 0, data) == NULL); + ok1(trbt_lookup32(rb, 0) == data); + ok1(trbt_lookup32(rb, -1) == NULL); + + /* Thus, freeing the data will delete the node. */ + talloc_free(data); + ok1(trbt_lookup32(rb, 0) == NULL); + + /* Try again. */ + data = talloc_strdup(NULL, "data"); + ok1(trbt_insert32(rb, 0, data) == NULL); + + /* Another insert should return old one. */ + data2 = talloc_strdup(NULL, "data2"); + ok1(trbt_insert32(rb, 0, data2) == data); + ok1(trbt_lookup32(rb, 0) == data2); + + /* Freeing old data has no effect. */ + talloc_free(data); + ok1(trbt_lookup32(rb, 0) == data2); + + /* Insert with callback on non-existing. */ + trbt_insert32_callback(rb, 1, insert_callback, NULL); + ok1(strcmp(trbt_lookup32(rb, 1), "insert_callback") == 0); + /* Insert with callback on existing. */ + trbt_insert32_callback(rb, 0, insert_callback, data2); + ok1(strcmp(trbt_lookup32(rb, 0), "insert_callback") == 0); + + /* Delete. */ + trbt_delete32(rb, 1); + ok1(trbt_lookup32(rb, 1) == NULL); + ok1(trbt_lookup32(rb, 0)); + + /* This should free everything. */ + talloc_free(trbt_lookup32(rb, 0)); + talloc_free(rb); /* This exits depending on whether all tests passed */ return exit_status();