]> git.ozlabs.org Git - ccan/blob - ccan/idtree/test/run.c
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / idtree / test / run.c
1 #include <ccan/idtree/idtree.c>
2 #include <ccan/tap/tap.h>
3 #include <limits.h>
4
5 #define ALLOC_MAX (2 * IDTREE_SIZE)
6
7 static bool check_tal_parent(const tal_t *parent, const tal_t *ctx)
8 {
9         while (ctx) {
10                 if (ctx == parent)
11                         return true;
12                 ctx = tal_parent(ctx);
13         }
14         return false;
15 }
16
17 int main(int argc, char *argv[])
18 {
19         unsigned int i;
20         const char allocated[ALLOC_MAX] = { 0 };
21         struct idtree *idtree;
22         void *ctx;
23
24         plan_tests(ALLOC_MAX * 5 + 2);
25         ctx = tal(NULL, char);
26         idtree = idtree_new(ctx);
27         ok1(check_tal_parent(ctx, idtree));
28
29         for (i = 0; i < ALLOC_MAX; i++) {
30                 int id = idtree_add(idtree, &allocated[i], ALLOC_MAX-1);
31                 ok1(id == i);
32                 ok1(idtree_lookup(idtree, i) == &allocated[i]);
33         }
34         ok1(idtree_add(idtree, &allocated[i], ALLOC_MAX-1) == -1);
35
36         /* Remove every second one. */
37         for (i = 0; i < ALLOC_MAX; i += 2)
38                 ok1(idtree_remove(idtree, i));
39
40         for (i = 0; i < ALLOC_MAX; i++) {
41                 if (i % 2 == 0)
42                         ok1(!idtree_lookup(idtree, i));
43                 else
44                         ok1(idtree_lookup(idtree, i) == &allocated[i]);
45         }
46
47         /* Now, finally, reallocate. */
48         for (i = 0; i < ALLOC_MAX/2; i++) {
49                 ok1(idtree_add(idtree, &allocated[i*2], INT_MAX) == i * 2);
50         }
51         
52         for (i = 0; i < ALLOC_MAX; i++) {
53                 ok1(idtree_lookup(idtree, i) == &allocated[i]);
54         }
55         tal_free(ctx);
56         exit(exit_status());
57 }