]> git.ozlabs.org Git - ccan/blob - ccan/rbtree/test/run.c
11d0ed78b015f03b252986ab4ea4cead5cde7ae9
[ccan] / ccan / rbtree / test / run.c
1 #include <ccan/rbtree/rbtree.c>
2 #include <ccan/tap/tap.h>
3 #include <ccan/talloc/talloc.h>
4 #include <string.h>
5
6 static void *insert_callback(void *param, void *data)
7 {
8         ok1(data == param);
9         return talloc_strdup(NULL, "insert_callback");
10 }
11
12 int main(void)
13 {
14         trbt_tree_t *rb;
15         void *ctx = talloc_init("toplevel");
16         char *data, *data2;
17
18         /* This is how many tests you plan to run */
19         plan_tests(18);
20
21         rb = trbt_create(ctx, 0);
22         ok1(rb);
23         ok1(talloc_is_parent(rb, ctx));
24
25         /* Failed lookup. */
26         ok1(trbt_lookup32(rb, 0) == NULL);
27         ok1(trbt_lookup32(rb, -1) == NULL);
28
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);
34
35         /* Thus, freeing the data will delete the node. */
36         talloc_free(data);
37         ok1(trbt_lookup32(rb, 0) == NULL);
38
39         /* Try again. */
40         data = talloc_strdup(NULL, "data");
41         ok1(trbt_insert32(rb, 0, data) == NULL);
42
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);
47
48         /* Freeing old data has no effect. */
49         talloc_free(data);
50         ok1(trbt_lookup32(rb, 0) == data2);
51
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);
58
59         /* Delete. */
60         trbt_delete32(rb, 1);
61         ok1(trbt_lookup32(rb, 1) == NULL);
62         ok1(trbt_lookup32(rb, 0));
63
64         /* This should free everything. */
65         talloc_free(trbt_lookup32(rb, 0));
66         talloc_free(rb);
67
68         /* This exits depending on whether all tests passed */
69         return exit_status();
70 }