]> git.ozlabs.org Git - ccan/blob - ccan/intmap/test/run-order.c
intmap: new module.
[ccan] / ccan / intmap / test / run-order.c
1 #include <ccan/intmap/intmap.c>
2 #include <ccan/tap/tap.h>
3 #include <stdio.h>
4
5 #define NUM 1000
6
7 typedef UINTMAP(unsigned int *) umap;
8 typedef SINTMAP(int *) smap;
9
10 static bool check_umap(const umap *map)
11 {
12         /* This is a larger type than unsigned, and allows negative */
13         int64_t i, prev;
14
15         /* Must be in order, must contain value. */
16         prev = -1;
17         for (i = uintmap_first(map); i != -1ULL; i = uintmap_after(map, i)) {
18                 if (i <= prev)
19                         return false;
20                 if (*(unsigned int *)uintmap_get(map, i) != i)
21                         return false;
22                 prev = i;
23         }
24         return true;
25 }
26
27 static bool check_smap(const smap *map)
28 {
29         /* This is a larger type than int, and allows negative */
30         int64_t i, prev;
31
32         /* Must be in order, must contain value. */
33         prev = -0x80000001ULL;
34         for (i = sintmap_first(map);
35              i != 0x7FFFFFFFFFFFFFFFLL;
36              i = sintmap_after(map, i)) {
37                 if (i <= prev)
38                         return false;
39                 if (*(int *)sintmap_get(map, i) != i)
40                         return false;
41                 prev = i;
42         }
43         return true;
44 }
45
46 int main(void)
47 {
48         umap umap;
49         smap smap;
50         int i;
51         unsigned int urandoms[NUM];
52         int srandoms[NUM];
53
54         plan_tests(6 * NUM + 2);
55         uintmap_init(&umap);
56         sintmap_init(&smap);
57
58         for (i = 0; i < NUM; i++) {
59                 urandoms[i] = random();
60                 srandoms[i] = random();
61         }
62         for (i = 0; i < NUM; i++) {
63                 /* In case we have duplicates. */
64                 while (!uintmap_add(&umap, urandoms[i], urandoms+i))
65                         urandoms[i] = random();
66                 ok1(check_umap(&umap));
67         }
68         for (i = 0; i < NUM; i++) {
69                 ok1(uintmap_del(&umap, urandoms[i]) == urandoms+i);
70                 ok1(check_umap(&umap));
71         }
72         ok1(uintmap_empty(&umap));
73
74         for (i = 0; i < NUM; i++) {
75                 /* In case we have duplicates. */
76                 while (!sintmap_add(&smap, srandoms[i], srandoms+i))
77                         srandoms[i] = random();
78                 ok1(check_smap(&smap));
79         }
80         for (i = 0; i < NUM; i++) {
81                 ok1(sintmap_del(&smap, srandoms[i]) == srandoms+i);
82                 ok1(check_smap(&smap));
83         }
84         ok1(sintmap_empty(&smap));
85
86         /* This exits depending on whether all tests passed */
87         return exit_status();
88 }