1 #define intmap_index_t uint8_t
2 #define sintmap_index_t int8_t
4 #include <ccan/intmap/intmap.c>
5 #include <ccan/tap/tap.h>
10 typedef UINTMAP(uint8_t *) umap;
11 typedef SINTMAP(int8_t *) smap;
13 static bool check_umap(const umap *map)
15 /* This is a larger type than unsigned, and allows negative */
17 intmap_index_t i, last_idx;
21 /* Must be in order, must contain value. */
23 for (v = uintmap_last(map, &i); v; v = uintmap_before(map, &i)) {
32 for (v = uintmap_first(map, &i); v; v = uintmap_after(map, &i)) {
38 last = (uintmap_last(map, &last_idx) == v);
43 static bool check_smap(const smap *map)
45 /* This is a larger type than int, and allows negative */
47 sintmap_index_t i, last_idx;
51 /* Must be in order, must contain value. */
53 for (v = sintmap_last(map, &i); v; v = sintmap_before(map, &i)) {
61 prev = -0x80000001ULL;
62 for (v = sintmap_first(map, &i); v; v = sintmap_after(map, &i)) {
68 last = (sintmap_last(map, &last_idx) == v);
73 int main(int argc, char *argv[])
78 uint8_t urandoms[NUM];
81 plan_tests(6 * NUM + 2);
86 srandom(atoi(argv[1]));
88 for (i = 0; i < NUM; i++) {
89 urandoms[i] = random();
90 srandoms[i] = random();
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));
98 for (i = 0; i < NUM; i++) {
99 ok1(uintmap_del(&umap, urandoms[i]) == urandoms+i);
100 ok1(check_umap(&umap));
102 ok1(uintmap_empty(&umap));
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));
110 for (i = 0; i < NUM; i++) {
111 ok1(sintmap_del(&smap, srandoms[i]) == srandoms+i);
112 ok1(check_smap(&smap));
114 ok1(sintmap_empty(&smap));
116 /* This exits depending on whether all tests passed */
117 return exit_status();