1 #include "stringmap/stringmap.h"
2 #include "stringmap/stringmap.c"
6 static void test_trivial(void) {
7 stringmap(int) map = stringmap_new(NULL);
9 ok1(stringmap_lookup(map, "") == NULL);
10 *stringmap_enter(map, "") = -1;
12 ok1(stringmap_lookup(map, "0") == NULL);
13 *stringmap_enter(map, "0") = 0;
15 ok1(stringmap_lookup(map, "one") == NULL);
16 *stringmap_enter(map, "one") = 1;
18 ok1(stringmap_lookup(map, "two") == NULL);
19 *stringmap_enter(map, "two") = 2;
21 ok1(stringmap_lookup(map, "three") == NULL);
22 *stringmap_enter(map, "three") = 3;
24 ok1(stringmap_lookup(map, "four") == NULL);
25 *stringmap_enter(map, "four") = 4;
27 ok1(*stringmap_lookup(map, "three") == 3);
28 ok1(*stringmap_lookup(map, "one") == 1);
29 ok1(*stringmap_lookup(map, "four") == 4);
30 ok1(*stringmap_lookup(map, "two") == 2);
31 ok1(*stringmap_lookup(map, "") == -1);
32 ok1(*stringmap_lookup(map, "0") == 0);
34 ok1(map.t.count == 6);
40 static void scramble(void *base, size_t nmemb, size_t size) {
44 for (;nmemb>1;nmemb--) {
45 o = i + size*(random()%nmemb);
54 //#define RANDOM_STRING_READABLE
56 static char *random_string(struct block_pool *bp, size_t *len_out) {
57 #ifndef RANDOM_STRING_READABLE
58 size_t len = random()%5 ? random()%10 : random()%1000;
60 size_t len = random() % 10;
62 char *str = block_pool_alloc(bp, len);
67 for (i=str; len--; i++) {
68 #ifndef RANDOM_STRING_READABLE
72 //only generate characters a-z
73 char c = random()%26 + 'a';
82 //note: struct layout needs to match *stringmap(char*).last
87 /* value is not a string, but a pointer to char marking that
88 this key has been entered already. */
91 static int tecmp(const struct test_entry *a, const struct test_entry *b) {
94 else if (a->len > b->len)
97 return memcmp(a->str, b->str, a->len);
100 static int by_str(const void *a, const void *b) {
104 static void cull_duplicates(struct test_entry *entries, size_t *count) {
105 struct test_entry *i, *o, *e = entries + *count;
107 qsort(entries, *count, sizeof(*entries), by_str);
109 for (i=entries, o=entries; i<e;) {
110 //skip repeated strings
112 struct test_entry *last = &o[-1];
113 if (!tecmp(last, i)) {
114 do i++; while(i<e && !tecmp(last, i));
119 //write all entries with the same value (should also have same string)
121 char *value = i->value;
122 do *o++ = *i++; while(i<e && i->value == value);
129 static int test_stringmap(size_t count, FILE *out) {
130 stringmap(char*) map = stringmap_new(NULL);
132 #define print(tag, fmt, ...) do { \
134 fprintf(out, tag fmt "\n", ##__VA_ARGS__); \
136 #define err(...) do { \
137 print("error: ", __VA_ARGS__); \
140 //#define debug(...) print("debug: ", __VA_ARGS__)
141 #define debug(...) do {} while(0)
142 #define msg(...) print("info: ", __VA_ARGS__)
144 struct block_pool *bp = block_pool_new(NULL);
145 struct test_entry *entries = block_pool_alloc(bp, sizeof(*entries) * count);
146 struct test_entry *i, *e = entries+count;
147 char *value_base = block_pool_alloc(bp, count), *value = value_base;
148 size_t unique_count = 0;
150 //we use value to track whether an entry has been added or not
151 memset(value, 0, count);
153 msg("Generating %zu test entries...", count);
155 for (i=entries; i<e; value++) {
157 char *str = random_string(bp, &len);
158 size_t same_count = (random()%5 ? random()%3 : random()%10) + 1;
160 for (;same_count-- && i<e; i++) {
167 cull_duplicates(entries, &count);
169 scramble(entries, count, sizeof(*entries));
171 msg("Inserting/looking up %zu entries...", count);
173 for (i=entries; i<e; i++) {
176 debug("Looking up %s", i->str);
178 node = stringmap_lookup_n(map, i->str, i->len);
182 err("Previously inserted entry not found");
184 debug("Not found; entering %s", i->str);
186 node = stringmap_enter_n(map, i->str, i->len);
187 if (!node || tecmp(i, (void*)map.last))
188 err("Node not properly entered");
189 if (map.last->str[map.last->len])
190 err("Entered string not zero-terminated");
192 *i->value = 1; //mark that the entry is entered
196 if (tecmp(i, (void*)map.last))
197 err("lookup returned incorrect string");
198 if (map.last->str[map.last->len])
199 err("Looked-up string not zero-terminated");
200 if (i->value != *node)
201 err("lookup returned incorrect value");
203 err("lookup returned bogus value");
207 if (map.t.count != unique_count)
208 err("Map has incorrect count");
210 printf("stringmap test passed after %zu inserts, %zu lookups (%zu total operations)\n",
211 unique_count, (i-entries)-unique_count, i-entries);
218 printf("stringmap test failed after %zu inserts, %zu lookups (%zu total operations)\n",
219 unique_count, (i-entries)-unique_count, i-entries);
236 ok1(test_stringmap(10000, stdout));
238 return exit_status();