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 return uintmap_iterate(map, uint_iterate_check, &prev);
46 static bool sint_iterate_check(sintmap_index_t i, int *v, int64_t *prev)
56 static bool check_smap(const smap *map)
58 /* This is a larger type than int, and allows negative */
59 int64_t prev, i, last_idx;
63 /* Must be in order, must contain value. */
64 prev = -0x80000001ULL;
65 for (v = sintmap_first(map, &i); v; v = sintmap_after(map, &i)) {
70 last = (sintmap_last(map, &last_idx) == v);
78 return sintmap_iterate(map, sint_iterate_check, &prev);
86 unsigned int urandoms[NUM];
89 plan_tests(6 * NUM + 2);
93 for (i = 0; i < NUM; i++) {
94 urandoms[i] = random();
95 srandoms[i] = random();
97 for (i = 0; i < NUM; i++) {
98 /* In case we have duplicates. */
99 while (!uintmap_add(&umap, urandoms[i], urandoms+i))
100 urandoms[i] = random();
101 ok1(check_umap(&umap));
103 for (i = 0; i < NUM; i++) {
104 ok1(uintmap_del(&umap, urandoms[i]) == urandoms+i);
105 ok1(check_umap(&umap));
107 ok1(uintmap_empty(&umap));
109 for (i = 0; i < NUM; i++) {
110 /* In case we have duplicates. */
111 while (!sintmap_add(&smap, srandoms[i], srandoms+i))
112 srandoms[i] = random();
113 ok1(check_smap(&smap));
115 for (i = 0; i < NUM; i++) {
116 ok1(sintmap_del(&smap, srandoms[i]) == srandoms+i);
117 ok1(check_smap(&smap));
119 ok1(sintmap_empty(&smap));
121 /* This exits depending on whether all tests passed */
122 return exit_status();