1 #include <ccan/failtest/failtest.h>
2 #include <ccan/rbtree/rbtree.c>
3 #include <ccan/tap/tap.h>
4 #include <ccan/talloc/talloc.h>
7 /* We want to test talloc failure paths. */
8 static void *my_malloc(size_t size)
13 static void my_free(void *ptr)
18 static void *my_realloc(void *ptr, size_t size)
20 return realloc(ptr, size);
23 static void *insert_callback(void *param, void *data)
26 return talloc_strdup(NULL, "insert_callback");
32 void *ctx = talloc_strdup(NULL, "toplevel");
35 /* This is how many tests you plan to run */
38 talloc_set_allocator(my_malloc, my_free, my_realloc);
40 rb = trbt_create(ctx, 0);
42 ok1(talloc_is_parent(rb, ctx));
45 ok1(trbt_lookup32(rb, 0) == NULL);
46 ok1(trbt_lookup32(rb, -1) == NULL);
48 /* Insert, should steal node onto data. */
49 data = talloc_strdup(NULL, "data");
50 ok1(trbt_insert32(rb, 0, data) == NULL);
51 ok1(trbt_lookup32(rb, 0) == data);
52 ok1(trbt_lookup32(rb, -1) == NULL);
54 /* Thus, freeing the data will delete the node. */
56 ok1(trbt_lookup32(rb, 0) == NULL);
59 data = talloc_strdup(NULL, "data");
60 ok1(trbt_insert32(rb, 0, data) == NULL);
62 /* Another insert should return old one. */
63 data2 = talloc_strdup(NULL, "data2");
64 ok1(trbt_insert32(rb, 0, data2) == data);
65 ok1(trbt_lookup32(rb, 0) == data2);
67 /* Freeing old data has no effect. */
69 ok1(trbt_lookup32(rb, 0) == data2);
71 /* Insert with callback on non-existing. */
72 trbt_insert32_callback(rb, 1, insert_callback, NULL);
73 ok1(strcmp(trbt_lookup32(rb, 1), "insert_callback") == 0);
74 /* Insert with callback on existing. */
75 trbt_insert32_callback(rb, 0, insert_callback, data2);
76 ok1(strcmp(trbt_lookup32(rb, 0), "insert_callback") == 0);
80 data2 = trbt_lookup32(rb, 1);
82 ok1(trbt_lookup32(rb, 1) == NULL);
83 ok1(trbt_lookup32(rb, 0));
86 /* This should free everything. */
87 talloc_free(trbt_lookup32(rb, 0));
90 /* No memory leaks? */
91 ok1(talloc_total_blocks(ctx) == 1);
94 /* This exits depending on whether all tests passed */
95 failtest_exit(exit_status());