1 #include <ccan/rbtree/rbtree.c>
2 #include <ccan/tap/tap.h>
3 #include <ccan/talloc/talloc.h>
6 #include <ccan/failtest/failtest_override.h>
7 #include <ccan/failtest/failtest.h>
10 /* We want to test talloc failure paths. */
11 static void *my_malloc(size_t size)
16 static void my_free(void *ptr)
21 static void *my_realloc(void *ptr, size_t size)
23 return realloc(ptr, size);
26 static bool lookup_all(trbt_tree_t *rb, bool exist)
30 for (i = 0; i < NUM_ELEMS; i++) {
31 int *p = trbt_lookup32(rb, i);
44 static bool add_one(trbt_tree_t *rb, bool exist, unsigned int i)
46 int *new = talloc_memdup(rb, &i, sizeof(i));
52 p = trbt_insert32(rb, i, new);
62 if (!trbt_lookup32(rb, i))
68 static bool insert_all(trbt_tree_t *rb, bool exist)
72 for (i = 0; i < NUM_ELEMS; i++) {
73 if (!add_one(rb, exist, i))
79 static void delete_all(trbt_tree_t *rb)
83 /* Don't delete them in the obvious order. */
84 for (i = 0; i < NUM_ELEMS / 2; i++) {
88 for (i = NUM_ELEMS-1; i >= NUM_ELEMS / 2; i--) {
94 static trbt_tree_t *rb;
96 static void exit_test(void)
99 ok1(talloc_total_blocks(ctx) == 1);
101 failtest_exit(exit_status());
104 int main(int argc, char *argv[])
106 failtest_init(argc, argv);
107 tap_fail_callback = exit_test;
110 ctx = talloc_strdup(NULL, "toplevel");
112 talloc_set_allocator(my_malloc, my_free, my_realloc);
114 rb = trbt_create(ctx, 0);
117 /* None should be there. */
118 ok1(lookup_all(rb, false));
120 /* Insert, none should be there previously. */
121 ok1(insert_all(rb, false));
124 ok1(lookup_all(rb, true));
127 ok1(insert_all(rb, true));
132 /* One more time... */
133 ok1(lookup_all(rb, false));
134 ok1(insert_all(rb, false));
136 /* All are children of rb, so this is clean. */
139 /* No memory leaks? */
140 ok1(talloc_total_blocks(ctx) == 1);
143 /* This exits depending on whether all tests passed */
144 failtest_exit(exit_status());