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