]> git.ozlabs.org Git - ccan/blob - ccan/intmap/test/run-order.c
1b2fdffdfadcb68eeadebe547ae5c9510bebcc11
[ccan] / ccan / intmap / test / run-order.c
1 #include <ccan/intmap/intmap.c>
2 #include <ccan/tap/tap.h>
3 #include <stdio.h>
4
5 #define NUM 1000
6
7 typedef UINTMAP(unsigned int *) umap;
8 typedef SINTMAP(int *) smap;
9
10 static bool check_umap(const umap *map)
11 {
12         /* This is a larger type than unsigned, and allows negative */
13         int64_t prev;
14         uint64_t i;
15         unsigned int *v;
16
17         /* Must be in order, must contain value. */
18         prev = -1;
19         for (v = uintmap_first(map, &i); v; v = uintmap_after(map, &i)) {
20                 if ((int64_t)i <= prev)
21                         return false;
22                 if (*v != i)
23                         return false;
24                 prev = i;
25         }
26         return true;
27 }
28
29 static bool check_smap(const smap *map)
30 {
31         /* This is a larger type than int, and allows negative */
32         int64_t prev, i;
33         int *v;
34
35         /* Must be in order, must contain value. */
36         prev = -0x80000001ULL;
37         for (v = sintmap_first(map, &i); v; v = sintmap_after(map, &i)) {
38                 if (i <= prev)
39                         return false;
40                 if (*v != i)
41                         return false;
42                 prev = i;
43         }
44         return true;
45 }
46
47 int main(void)
48 {
49         umap umap;
50         smap smap;
51         int i;
52         unsigned int urandoms[NUM];
53         int srandoms[NUM];
54
55         plan_tests(6 * NUM + 2);
56         uintmap_init(&umap);
57         sintmap_init(&smap);
58
59         for (i = 0; i < NUM; i++) {
60                 urandoms[i] = random();
61                 srandoms[i] = random();
62         }
63         for (i = 0; i < NUM; i++) {
64                 /* In case we have duplicates. */
65                 while (!uintmap_add(&umap, urandoms[i], urandoms+i))
66                         urandoms[i] = random();
67                 ok1(check_umap(&umap));
68         }
69         for (i = 0; i < NUM; i++) {
70                 ok1(uintmap_del(&umap, urandoms[i]) == urandoms+i);
71                 ok1(check_umap(&umap));
72         }
73         ok1(uintmap_empty(&umap));
74
75         for (i = 0; i < NUM; i++) {
76                 /* In case we have duplicates. */
77                 while (!sintmap_add(&smap, srandoms[i], srandoms+i))
78                         srandoms[i] = random();
79                 ok1(check_smap(&smap));
80         }
81         for (i = 0; i < NUM; i++) {
82                 ok1(sintmap_del(&smap, srandoms[i]) == srandoms+i);
83                 ok1(check_smap(&smap));
84         }
85         ok1(sintmap_empty(&smap));
86
87         /* This exits depending on whether all tests passed */
88         return exit_status();
89 }