]> git.ozlabs.org Git - ccan/blob - ccan/idtree/test/run.c
read_write_all: avoid arithmetic on void pointers.
[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 int main(int argc, char *argv[])
8 {
9         unsigned int i;
10         const char allocated[ALLOC_MAX] = { 0 };
11         struct idtree *idtree;
12         void *ctx;
13
14         plan_tests(ALLOC_MAX * 5 + 2);
15         ctx = talloc_named_const(NULL, 1, "test root");
16         idtree = idtree_new(ctx);
17         ok1(talloc_find_parent_byname(idtree, "test root") == ctx);
18
19         for (i = 0; i < ALLOC_MAX; i++) {
20                 int id = idtree_add(idtree, &allocated[i], ALLOC_MAX-1);
21                 ok1(id == i);
22                 ok1(idtree_lookup(idtree, i) == &allocated[i]);
23         }
24         ok1(idtree_add(idtree, &allocated[i], ALLOC_MAX-1) == -1);
25
26         /* Remove every second one. */
27         for (i = 0; i < ALLOC_MAX; i += 2)
28                 ok1(idtree_remove(idtree, i));
29
30         for (i = 0; i < ALLOC_MAX; i++) {
31                 if (i % 2 == 0)
32                         ok1(!idtree_lookup(idtree, i));
33                 else
34                         ok1(idtree_lookup(idtree, i) == &allocated[i]);
35         }
36
37         /* Now, finally, reallocate. */
38         for (i = 0; i < ALLOC_MAX/2; i++) {
39                 ok1(idtree_add(idtree, &allocated[i*2], INT_MAX) == i * 2);
40         }
41         
42         for (i = 0; i < ALLOC_MAX; i++) {
43                 ok1(idtree_lookup(idtree, i) == &allocated[i]);
44         }
45         talloc_free(ctx);
46         exit(exit_status());
47 }