6474007fb4f0f259b64231b0e026aa3df34f4f59
[ccan] / ccan / rbtree / test / run-many.c
1 #include <ccan/rbtree/rbtree.c>
2 #include <ccan/tap/tap.h>
3 #include <ccan/talloc/talloc.h>
4 #include <string.h>
5 #include <stdbool.h>
6
7 #define NUM_ELEMS 10000
8
9 static bool lookup_all(trbt_tree_t *rb, bool exist)
10 {
11         unsigned int i;
12
13         for (i = 0; i < NUM_ELEMS; i++) {
14                 int *p = trbt_lookup32(rb, i);
15                 if (p) {
16                         if (!exist)
17                                 return false;
18                         if (*p != i)
19                                 return false;
20                 } else
21                         if (exist)
22                                 return false;
23         }
24         return true;
25 }
26
27 static bool insert_all(trbt_tree_t *rb, bool exist)
28 {
29         unsigned int i;
30
31         for (i = 0; i < NUM_ELEMS; i++) {
32                 int *p = trbt_insert32(rb, i, talloc_memdup(rb, &i, sizeof(i)));
33                 if (p) {
34                         if (!exist)
35                                 return false;
36                         if (*p != i)
37                                 return false;
38                 } else
39                         if (exist)
40                                 return false;
41         }
42         return true;
43 }
44
45 static void delete_all(trbt_tree_t *rb)
46 {
47         unsigned int i;
48
49         for (i = 0; i < NUM_ELEMS; i++) {
50                 trbt_delete32(rb, i);
51         }
52 }
53
54 int main(void)
55 {
56         trbt_tree_t *rb;
57         void *ctx = talloc_init("toplevel");
58         unsigned int i;
59
60         plan_tests(7);
61
62         rb = trbt_create(ctx, 0);
63         ok1(rb);
64
65         /* None should be there. */
66         ok1(lookup_all(rb, false));
67
68         /* Insert, none should be there previously. */
69         ok1(insert_all(rb, false));
70
71         /* All there now. */
72         ok1(lookup_all(rb, true));
73
74         /* Replace all. */
75         ok1(insert_all(rb, true));
76
77         /* Delete all. */
78         delete_all(rb);
79
80         /* One more time... */
81         ok1(lookup_all(rb, false));
82         ok1(insert_all(rb, false));
83
84         /* All are children of rb, so this is clean. */
85         talloc_free(rb);
86
87         /* This exits depending on whether all tests passed */
88         return exit_status();
89 }