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_first(map, &i); v; v = uintmap_after(map, &i)) {
29 last = (uintmap_last(map, &last_idx) == v);
34 static bool check_smap(const smap *map)
36 /* This is a larger type than int, and allows negative */
38 sintmap_index_t i, last_idx;
42 /* Must be in order, must contain value. */
43 prev = -0x80000001ULL;
44 for (v = sintmap_first(map, &i); v; v = sintmap_after(map, &i)) {
50 last = (sintmap_last(map, &last_idx) == v);
60 uint8_t urandoms[NUM];
63 plan_tests(6 * NUM + 2);
67 for (i = 0; i < NUM; i++) {
68 urandoms[i] = random();
69 srandoms[i] = random();
71 for (i = 0; i < NUM; i++) {
72 /* In case we have duplicates. */
73 while (!uintmap_add(&umap, urandoms[i], urandoms+i))
74 urandoms[i] = random();
75 ok1(check_umap(&umap));
77 for (i = 0; i < NUM; i++) {
78 ok1(uintmap_del(&umap, urandoms[i]) == urandoms+i);
79 ok1(check_umap(&umap));
81 ok1(uintmap_empty(&umap));
83 for (i = 0; i < NUM; i++) {
84 /* In case we have duplicates. */
85 while (!sintmap_add(&smap, srandoms[i], srandoms+i))
86 srandoms[i] = random();
87 ok1(check_smap(&smap));
89 for (i = 0; i < NUM; i++) {
90 ok1(sintmap_del(&smap, srandoms[i]) == srandoms+i);
91 ok1(check_smap(&smap));
93 ok1(sintmap_empty(&smap));
95 /* This exits depending on whether all tests passed */