]> git.ozlabs.org Git - ccan/blob - ccan/intmap/test/run-order-smallsize.c
intmap: implement uintmap_last/sintmap_last.
[ccan] / ccan / intmap / test / run-order-smallsize.c
1 #define intmap_index_t uint8_t
2 #define sintmap_index_t int8_t
3
4 #include <ccan/intmap/intmap.c>
5 #include <ccan/tap/tap.h>
6 #include <stdio.h>
7
8 #define NUM 100
9
10 typedef UINTMAP(uint8_t *) umap;
11 typedef SINTMAP(int8_t *) smap;
12
13 static bool check_umap(const umap *map)
14 {
15         /* This is a larger type than unsigned, and allows negative */
16         int64_t prev;
17         intmap_index_t i, last_idx;
18         uint8_t *v;
19         bool last = true;
20
21         /* Must be in order, must contain value. */
22         prev = -1;
23         for (v = uintmap_first(map, &i); v; v = uintmap_after(map, &i)) {
24                 if (i <= prev)
25                         return false;
26                 if (*v != i)
27                         return false;
28                 prev = i;
29                 last = (uintmap_last(map, &last_idx) == v);
30         }
31         return last;
32 }
33
34 static bool check_smap(const smap *map)
35 {
36         /* This is a larger type than int, and allows negative */
37         int64_t prev;
38         sintmap_index_t i, last_idx;
39         int8_t *v;
40         bool last = true;
41
42         /* Must be in order, must contain value. */
43         prev = -0x80000001ULL;
44         for (v = sintmap_first(map, &i); v; v = sintmap_after(map, &i)) {
45                 if (i <= prev)
46                         return false;
47                 if (*v != i)
48                         return false;
49                 prev = i;
50                 last = (sintmap_last(map, &last_idx) == v);
51         }
52         return last;
53 }
54
55 int main(void)
56 {
57         umap umap;
58         smap smap;
59         int i;
60         uint8_t urandoms[NUM];
61         int8_t srandoms[NUM];
62
63         plan_tests(6 * NUM + 2);
64         uintmap_init(&umap);
65         sintmap_init(&smap);
66
67         for (i = 0; i < NUM; i++) {
68                 urandoms[i] = random();
69                 srandoms[i] = random();
70         }
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));
76         }
77         for (i = 0; i < NUM; i++) {
78                 ok1(uintmap_del(&umap, urandoms[i]) == urandoms+i);
79                 ok1(check_umap(&umap));
80         }
81         ok1(uintmap_empty(&umap));
82
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));
88         }
89         for (i = 0; i < NUM; i++) {
90                 ok1(sintmap_del(&smap, srandoms[i]) == srandoms+i);
91                 ok1(check_smap(&smap));
92         }
93         ok1(sintmap_empty(&smap));
94
95         /* This exits depending on whether all tests passed */
96         return exit_status();
97 }