]> git.ozlabs.org Git - ccan/blob - ccan/strmap/test/run-prefix.c
endian: add constant versions.
[ccan] / ccan / strmap / test / run-prefix.c
1 #include <ccan/strmap/strmap.h>
2 #include <ccan/strmap/strmap.c>
3 #include <ccan/tap/tap.h>
4 #include <stdio.h>
5
6 /* Must be > 100, see below. */
7 #define NUM 200
8
9 static bool in_order(const char *index, char *value, unsigned int *count)
10 {
11         int i = atoi(index);
12         ok1(i == atoi(value));
13         ok1(*count == i);
14         (*count)++;
15         return true;
16 }
17
18 static bool find_empty(const char *index, char *value, char *empty)
19 {
20         if (index == empty)
21                 pass("Found empty entry!");
22         return true;
23 }
24
25 int main(void)
26 {
27         struct map {
28                 STRMAP_MEMBERS(char *);
29         };
30         struct map map;
31         const struct map *sub;
32         unsigned int i;
33         char *str[NUM], *empty;
34
35         plan_tests(8 + 2 * (1 + 10 + 100) + 1);
36         strmap_init(&map);
37
38         for (i = 0; i < NUM; i++) {
39                 char template[10];
40                 sprintf(template, "%08u", i);
41                 str[i] = strdup(template);
42         }
43
44         /* All prefixes of an empty map are empty. */
45         sub = strmap_prefix(&map, "a");
46         ok1(strmap_empty(sub));
47         sub = strmap_prefix(&map, "");
48         ok1(strmap_empty(sub));
49
50         for (i = 0; i < NUM; i++)
51                 strmap_add(&map, str[i], str[i]+1);
52
53         /* Nothing */
54         sub = strmap_prefix(&map, "a");
55         ok1(strmap_empty(sub));
56
57         /* Everything */
58         sub = strmap_prefix(&map, "0");
59         ok1(sub->raw.u.n == map.raw.u.n);
60         sub = strmap_prefix(&map, "");
61         ok1(sub->raw.u.n == map.raw.u.n);
62
63         /* Single. */
64         sub = strmap_prefix(&map, "00000000");
65         i = 0;
66         strmap_iterate(sub, in_order, &i);
67         ok1(i == 1);
68
69         /* First 10. */
70         sub = strmap_prefix(&map, "0000000");
71         i = 0;
72         strmap_iterate(sub, in_order, &i);
73         ok1(i == 10);
74
75         /* First 100. */
76         sub = strmap_prefix(&map, "000000");
77         i = 0;
78         strmap_iterate(sub, in_order, &i);
79         ok1(i == 100);
80
81         /* Everything, *plus* empty string. */
82         empty = strdup("");
83         strmap_add(&map, empty, empty);
84
85         sub = strmap_prefix(&map, "");
86         /* Check we get *our* empty string back! */
87         strmap_iterate(sub, find_empty, empty);
88
89         strmap_clear(&map);
90
91         for (i = 0; i < NUM; i++)
92                 free(str[i]);
93         free(empty);
94
95         /* This exits depending on whether all tests passed */
96         return exit_status();
97 }