1 #include <ccan/rbtree/rbtree.c>
2 #include <ccan/tap/tap.h>
3 #include <ccan/talloc/talloc.h>
6 static void *insert_callback(void *param, void *data)
9 return talloc_strdup(NULL, "insert_callback");
15 void *ctx = talloc_init("toplevel");
18 /* This is how many tests you plan to run */
21 rb = trbt_create(ctx, 0);
23 ok1(talloc_is_parent(rb, ctx));
26 ok1(trbt_lookup32(rb, 0) == NULL);
27 ok1(trbt_lookup32(rb, -1) == NULL);
29 /* Insert, should steal node onto data. */
30 data = talloc_strdup(NULL, "data");
31 ok1(trbt_insert32(rb, 0, data) == NULL);
32 ok1(trbt_lookup32(rb, 0) == data);
33 ok1(trbt_lookup32(rb, -1) == NULL);
35 /* Thus, freeing the data will delete the node. */
37 ok1(trbt_lookup32(rb, 0) == NULL);
40 data = talloc_strdup(NULL, "data");
41 ok1(trbt_insert32(rb, 0, data) == NULL);
43 /* Another insert should return old one. */
44 data2 = talloc_strdup(NULL, "data2");
45 ok1(trbt_insert32(rb, 0, data2) == data);
46 ok1(trbt_lookup32(rb, 0) == data2);
48 /* Freeing old data has no effect. */
50 ok1(trbt_lookup32(rb, 0) == data2);
52 /* Insert with callback on non-existing. */
53 trbt_insert32_callback(rb, 1, insert_callback, NULL);
54 ok1(strcmp(trbt_lookup32(rb, 1), "insert_callback") == 0);
55 /* Insert with callback on existing. */
56 trbt_insert32_callback(rb, 0, insert_callback, data2);
57 ok1(strcmp(trbt_lookup32(rb, 0), "insert_callback") == 0);
61 ok1(trbt_lookup32(rb, 1) == NULL);
62 ok1(trbt_lookup32(rb, 0));
64 /* This should free everything. */
65 talloc_free(trbt_lookup32(rb, 0));
68 /* This exits depending on whether all tests passed */