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