]> git.ozlabs.org Git - ccan/blob - ccan/rbtree/test/run-many.c
ff32f4f18675dc62b56018f64c1f1daf18776507
[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
59         plan_tests(7);
60
61         rb = trbt_create(ctx, 0);
62         ok1(rb);
63
64         /* None should be there. */
65         ok1(lookup_all(rb, false));
66
67         /* Insert, none should be there previously. */
68         ok1(insert_all(rb, false));
69
70         /* All there now. */
71         ok1(lookup_all(rb, true));
72
73         /* Replace all. */
74         ok1(insert_all(rb, true));
75
76         /* Delete all. */
77         delete_all(rb);
78
79         /* One more time... */
80         ok1(lookup_all(rb, false));
81         ok1(insert_all(rb, false));
82
83         /* All are children of rb, so this is clean. */
84         talloc_free(rb);
85
86         /* This exits depending on whether all tests passed */
87         return exit_status();
88 }