1 #include <ccan/intmap/intmap.c>
2 #include <ccan/tap/tap.h>
7 typedef UINTMAP(unsigned int *) umap;
8 typedef SINTMAP(int *) smap;
10 static bool uint_iterate_check(intmap_index_t i, unsigned int *v, int64_t *prev)
12 if ((int64_t)i <= *prev)
20 static bool check_umap(const umap *map)
22 /* This is a larger type than unsigned, and allows negative */
28 /* Must be in order, must contain value. */
30 for (v = uintmap_first(map, &i); v; v = uintmap_after(map, &i)) {
31 if ((int64_t)i <= prev)
36 last = (uintmap_last(map, &last_idx) == v);
43 for (v = uintmap_last(map, &i); v; v = uintmap_before(map, &i)) {
44 if ((int64_t)i >= prev)
49 last = (uintmap_first(map, &last_idx) == v);
56 return uintmap_iterate(map, uint_iterate_check, &prev);
59 static bool sint_iterate_check(sintmap_index_t i, int *v, int64_t *prev)
69 static bool check_smap(const smap *map)
71 /* This is a larger type than int, and allows negative */
72 int64_t prev, i, last_idx;
76 /* Must be in order, must contain value. */
77 prev = -0x80000001ULL;
78 for (v = sintmap_first(map, &i); v; v = sintmap_after(map, &i)) {
83 last = (sintmap_last(map, &last_idx) == v);
91 for (v = sintmap_last(map, &i); v; v = sintmap_before(map, &i)) {
97 last = (sintmap_first(map, &last_idx) == v);
103 return sintmap_iterate(map, sint_iterate_check, &prev);
111 unsigned int urandoms[NUM];
114 plan_tests(6 * NUM + 2);
118 for (i = 0; i < NUM; i++) {
119 urandoms[i] = random();
120 srandoms[i] = random();
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));
128 for (i = 0; i < NUM; i++) {
129 ok1(uintmap_del(&umap, urandoms[i]) == urandoms+i);
130 ok1(check_umap(&umap));
132 ok1(uintmap_empty(&umap));
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));
140 for (i = 0; i < NUM; i++) {
141 ok1(sintmap_del(&smap, srandoms[i]) == srandoms+i);
142 ok1(check_smap(&smap));
144 ok1(sintmap_empty(&smap));
146 /* This exits depending on whether all tests passed */
147 return exit_status();