]> git.ozlabs.org Git - ccan/blob - ccan/stringmap/test/run.c
Added module stringmap
[ccan] / ccan / stringmap / test / run.c
1 #include "stringmap/stringmap.h"
2 #include "stringmap/stringmap.c"
3
4 #include "tap/tap.h"
5
6 static void test_trivial(void) {
7         stringmap(int) map = stringmap_new(NULL);
8         
9         ok1(stringmap_lookup(map, "one") == NULL);
10         *stringmap_enter(map, "one") = 1;
11         
12         ok1(stringmap_lookup(map, "two") == NULL);
13         *stringmap_enter(map, "two") = 2;
14         
15         ok1(stringmap_lookup(map, "three") == NULL);
16         *stringmap_enter(map, "three") = 3;
17         
18         ok1(stringmap_lookup(map, "four") == NULL);
19         *stringmap_enter(map, "four") = 4;
20         
21         ok1(*stringmap_lookup(map, "three") == 3);
22         ok1(*stringmap_lookup(map, "one") == 1);
23         ok1(*stringmap_lookup(map, "four") == 4);
24         ok1(*stringmap_lookup(map, "two") == 2);
25         
26         ok1(map.t.count == 4);
27         
28         stringmap_free(map);
29 }
30
31
32 static void scramble(void *base, size_t nmemb, size_t size) {
33    char *i = base;
34    char *o;
35    size_t sd;
36    for (;nmemb>1;nmemb--) {
37       o = i + size*(random()%nmemb);
38       for (sd=size;sd--;) {
39          char tmp = *o;
40          *o++ = *i;
41          *i++ = tmp;
42       }
43    }
44 }
45
46 //#define RANDOM_STRING_READABLE
47
48 static char *random_string(struct block_pool *bp) {
49         size_t len = random() % 100;
50         char *str = block_pool_alloc(bp, len+1);
51         char *i;
52         
53         for (i=str; len--; i++) {
54                 #ifndef RANDOM_STRING_READABLE
55                 char c = random();
56                 *i = c ? c : ' ';
57                 #else
58                 //only generate characters [32,126]
59                 char c = random()%95 + 32;
60                 *i = c;
61                 #endif
62         }
63         *i = 0;
64         
65         return str;
66 }
67
68 struct test_entry {
69         const char *str;
70         char *value;
71 };
72
73 static int by_str(const void *ap, const void *bp) {
74         return strcmp(((struct test_entry*)ap)->str, ((struct test_entry*)bp)->str);
75 }
76
77 static void cull_duplicates(struct test_entry *entries, size_t *count) {
78         struct test_entry *i, *o, *e = entries + *count;
79         
80         qsort(entries, *count, sizeof(*entries), by_str);
81         
82         for (i=entries, o=entries; i<e;) {
83                 //skip repeated strings
84                 if (i>entries) {
85                         const char *last = i[-1].str;
86                         if (!strcmp(last, i->str)) {
87                                 do i++; while(i<e && !strcmp(last, i->str));
88                                 continue;
89                         }
90                 }
91                 
92                 //write all entries with the same value (should also have same string)
93                 {
94                         char *value = i->value;
95                         do *o++ = *i++; while(i<e && i->value == value);
96                 }
97         }
98         
99         *count = o-entries;
100 }
101
102 static int test_stringmap(size_t count, FILE *out) {
103         stringmap(char*) map = stringmap_new(NULL);
104         
105         #define print(tag, fmt, ...) do { \
106                         if (out) \
107                                 fprintf(out, tag fmt "\n", ##__VA_ARGS__); \
108                 } while(0)
109         #define err(...) do { \
110                         print("error: ", __VA_ARGS__); \
111                         goto fail; \
112                 } while(0)
113         #define debug(...) print("debug: ", __VA_ARGS__)
114         #define msg(...) print("info: ", __VA_ARGS__)
115         
116         struct block_pool *bp = block_pool_new(NULL);
117         struct test_entry *entries = block_pool_alloc(bp, sizeof(*entries) * count);
118         struct test_entry *i, *e = entries+count;
119         char *value_base = block_pool_alloc(bp, count), *value = value_base;
120         size_t unique_count = 0;
121         
122         //we use value to track whether an entry has been added or not
123         memset(value, 0, count);
124         
125         msg("Generating %zu test entries...", count);
126         
127         for (i=entries; i<e; value++) {
128                 char *str = random_string(bp);
129                 size_t same_count = (random()%5 ? random()%3 : random()%10) + 1;
130                 
131                 for (;same_count-- && i<e; i++) {
132                         i->str = str;
133                         i->value = value;
134                 }
135         }
136         
137         cull_duplicates(entries, &count);
138         e = entries+count;
139         scramble(entries, count, sizeof(*entries));
140         
141         msg("Inserting/looking up %zu entries...", count);
142         
143         for (i=entries; i<e; i++) {
144                 char **node;
145                 
146                 debug("Looking up %s", i->str);
147                 
148                 node = stringmap_lookup(map, i->str);
149                 
150                 if (!node) {
151                         if (*i->value)
152                                 err("Previously inserted entry not found");
153                         
154                         debug("Not found; entering");
155                         
156                         node = stringmap_enter(map, i->str);
157                         if (!node || strcmp(i->str, map.last->str))
158                                 err("Node not properly entered");
159                         *node = i->value;
160                         *i->value = 1; //mark that the entry is entered
161                         
162                         unique_count++;
163                 } else {
164                         if (strcmp(i->value, map.last->value))
165                                 err("lookup returned incorrect string");
166                         if (i->value != *node)
167                                 err("lookup returned incorrect value");
168                         if (!**node)
169                                 err("lookup returned bogus value");
170                 }
171         }
172         
173         if (map.t.count != unique_count)
174                 err("Map has incorrect count");
175         
176         printf("stringmap test passed after %zu inserts, %zu lookups (%zu total operations)\n", unique_count, (i-entries)-unique_count, i-entries);
177         
178         block_pool_free(bp);
179         stringmap_free(map);
180         return 1;
181
182 fail:
183         printf("stringmap test failed after %zu inserts, %zu lookups (%zu total operations)\n", unique_count, (i-entries)-unique_count, i-entries);
184         
185         block_pool_free(bp);
186         stringmap_free(map);
187         return 0;
188         
189         #undef print
190         #undef err
191         #undef debug
192         #undef msg
193 }
194
195 int main(void)
196 {
197         plan_tests(10);
198         
199         test_trivial();
200         ok1(test_stringmap(10000, NULL));
201         
202         return exit_status();
203 }