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