]> git.ozlabs.org Git - ccan/blob - ccan/intmap/test/run-order-smallsize.c
base64: fix for unsigned chars (e.g. ARM).
[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, last_idx;
18         uint8_t *v;
19         bool last = true;
20
21         /* Must be in order, must contain value. */
22         prev = 256;
23         for (v = uintmap_last(map, &i); v; v = uintmap_before(map, &i)) {
24                 if (i >= prev)
25                         return false;
26                 if (*v != i)
27                         return false;
28                 prev = i;
29         }
30
31         prev = -1;
32         for (v = uintmap_first(map, &i); v; v = uintmap_after(map, &i)) {
33                 if (i <= prev)
34                         return false;
35                 if (*v != i)
36                         return false;
37                 prev = i;
38                 last = (uintmap_last(map, &last_idx) == v);
39         }
40         return last;
41 }
42
43 static bool check_smap(const smap *map)
44 {
45         /* This is a larger type than int, and allows negative */
46         int64_t prev;
47         sintmap_index_t i, last_idx;
48         int8_t *v;
49         bool last = true;
50
51         /* Must be in order, must contain value. */
52         prev = 0x80000000ULL;
53         for (v = sintmap_last(map, &i); v; v = sintmap_before(map, &i)) {
54                 if (i >= prev)
55                         return false;
56                 if (*v != i)
57                         return false;
58                 prev = i;
59         }
60
61         prev = -0x80000001ULL;
62         for (v = sintmap_first(map, &i); v; v = sintmap_after(map, &i)) {
63                 if (i <= prev)
64                         return false;
65                 if (*v != i)
66                         return false;
67                 prev = i;
68                 last = (sintmap_last(map, &last_idx) == v);
69         }
70         return last;
71 }
72
73 int main(int argc, char *argv[])
74 {
75         umap umap;
76         smap smap;
77         int i;
78         uint8_t urandoms[NUM];
79         int8_t srandoms[NUM];
80
81         plan_tests(6 * NUM + 2);
82         uintmap_init(&umap);
83         sintmap_init(&smap);
84
85         if (argc > 1)
86                 srandom(atoi(argv[1]));
87
88         for (i = 0; i < NUM; i++) {
89                 urandoms[i] = random();
90                 srandoms[i] = random();
91         }
92         for (i = 0; i < NUM; i++) {
93                 /* In case we have duplicates. */
94                 while (!uintmap_add(&umap, urandoms[i], urandoms+i))
95                         urandoms[i] = random();
96                 ok1(check_umap(&umap));
97         }
98         for (i = 0; i < NUM; i++) {
99                 ok1(uintmap_del(&umap, urandoms[i]) == urandoms+i);
100                 ok1(check_umap(&umap));
101         }
102         ok1(uintmap_empty(&umap));
103
104         for (i = 0; i < NUM; i++) {
105                 /* In case we have duplicates. */
106                 while (!sintmap_add(&smap, srandoms[i], srandoms+i))
107                         srandoms[i] = random();
108                 ok1(check_smap(&smap));
109         }
110         for (i = 0; i < NUM; i++) {
111                 ok1(sintmap_del(&smap, srandoms[i]) == srandoms+i);
112                 ok1(check_smap(&smap));
113         }
114         ok1(sintmap_empty(&smap));
115
116         /* This exits depending on whether all tests passed */
117         return exit_status();
118 }