]> git.ozlabs.org Git - ccan/blob - ccan/strmap/test/run-prefix.c
Remove travis workarounds for previous build system
[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         typedef STRMAP(char *) map_t;
28         map_t map;
29         const map_t *sub;
30         unsigned int i;
31         char *str[NUM], *empty;
32
33         plan_tests(8 + 2 * (1 + 10 + 100) + 1);
34         strmap_init(&map);
35
36         for (i = 0; i < NUM; i++) {
37                 char template[10];
38                 sprintf(template, "%08u", i);
39                 str[i] = strdup(template);
40         }
41
42         /* All prefixes of an empty map are empty. */
43         sub = strmap_prefix(&map, "a");
44         ok1(strmap_empty(sub));
45         sub = strmap_prefix(&map, "");
46         ok1(strmap_empty(sub));
47
48         for (i = 0; i < NUM; i++)
49                 strmap_add(&map, str[i], str[i]+1);
50
51         /* Nothing */
52         sub = strmap_prefix(&map, "a");
53         ok1(strmap_empty(sub));
54
55         /* Everything */
56         sub = strmap_prefix(&map, "0");
57         ok1(tcon_unwrap(sub)->u.n == tcon_unwrap(&map)->u.n);
58         sub = strmap_prefix(&map, "");
59         ok1(tcon_unwrap(sub)->u.n == tcon_unwrap(&map)->u.n);
60
61         /* Single. */
62         sub = strmap_prefix(&map, "00000000");
63         i = 0;
64         strmap_iterate(sub, in_order, &i);
65         ok1(i == 1);
66
67         /* First 10. */
68         sub = strmap_prefix(&map, "0000000");
69         i = 0;
70         strmap_iterate(sub, in_order, &i);
71         ok1(i == 10);
72
73         /* First 100. */
74         sub = strmap_prefix(&map, "000000");
75         i = 0;
76         strmap_iterate(sub, in_order, &i);
77         ok1(i == 100);
78
79         /* Everything, *plus* empty string. */
80         empty = strdup("");
81         strmap_add(&map, empty, empty);
82
83         sub = strmap_prefix(&map, "");
84         /* Check we get *our* empty string back! */
85         strmap_iterate(sub, find_empty, empty);
86
87         strmap_clear(&map);
88
89         for (i = 0; i < NUM; i++)
90                 free(str[i]);
91         free(empty);
92
93         /* This exits depending on whether all tests passed */
94         return exit_status();
95 }