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, "one") == NULL);
10 *stringmap_enter(map, "one") = 1;
12 ok1(stringmap_lookup(map, "two") == NULL);
13 *stringmap_enter(map, "two") = 2;
15 ok1(stringmap_lookup(map, "three") == NULL);
16 *stringmap_enter(map, "three") = 3;
18 ok1(stringmap_lookup(map, "four") == NULL);
19 *stringmap_enter(map, "four") = 4;
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);
26 ok1(map.t.count == 4);
32 static void scramble(void *base, size_t nmemb, size_t size) {
36 for (;nmemb>1;nmemb--) {
37 o = i + size*(random()%nmemb);
46 //#define RANDOM_STRING_READABLE
48 static char *random_string(struct block_pool *bp) {
49 size_t len = random() % 100;
50 char *str = block_pool_alloc(bp, len+1);
53 for (i=str; len--; i++) {
54 #ifndef RANDOM_STRING_READABLE
58 //only generate characters [32,126]
59 char c = random()%95 + 32;
71 /* value is not a string, but a pointer to char marking that
72 this key has been entered already. */
75 static int by_str(const void *ap, const void *bp) {
76 return strcmp(((struct test_entry*)ap)->str, ((struct test_entry*)bp)->str);
79 static void cull_duplicates(struct test_entry *entries, size_t *count) {
80 struct test_entry *i, *o, *e = entries + *count;
82 qsort(entries, *count, sizeof(*entries), by_str);
84 for (i=entries, o=entries; i<e;) {
85 //skip repeated strings
87 const char *last = i[-1].str;
88 if (!strcmp(last, i->str)) {
89 do i++; while(i<e && !strcmp(last, i->str));
94 //write all entries with the same value (should also have same string)
96 char *value = i->value;
97 do *o++ = *i++; while(i<e && i->value == value);
104 static int test_stringmap(size_t count, FILE *out) {
105 stringmap(char*) map = stringmap_new(NULL);
107 #define print(tag, fmt, ...) do { \
109 fprintf(out, tag fmt "\n", ##__VA_ARGS__); \
111 #define err(...) do { \
112 print("error: ", __VA_ARGS__); \
115 #define debug(...) print("debug: ", __VA_ARGS__)
116 #define msg(...) print("info: ", __VA_ARGS__)
118 struct block_pool *bp = block_pool_new(NULL);
119 struct test_entry *entries = block_pool_alloc(bp, sizeof(*entries) * count);
120 struct test_entry *i, *e = entries+count;
121 char *value_base = block_pool_alloc(bp, count), *value = value_base;
122 size_t unique_count = 0;
124 //we use value to track whether an entry has been added or not
125 memset(value, 0, count);
127 msg("Generating %zu test entries...", count);
129 for (i=entries; i<e; value++) {
130 char *str = random_string(bp);
131 size_t same_count = (random()%5 ? random()%3 : random()%10) + 1;
133 for (;same_count-- && i<e; i++) {
139 cull_duplicates(entries, &count);
141 scramble(entries, count, sizeof(*entries));
143 msg("Inserting/looking up %zu entries...", count);
145 for (i=entries; i<e; i++) {
148 debug("Looking up %s", i->str);
150 node = stringmap_lookup(map, i->str);
154 err("Previously inserted entry not found");
156 debug("Not found; entering");
158 node = stringmap_enter(map, i->str);
159 if (!node || strcmp(i->str, map.last->str))
160 err("Node not properly entered");
162 *i->value = 1; //mark that the entry is entered
166 if (strcmp(i->str, map.last->str))
167 err("lookup returned incorrect string");
168 if (i->value != *node)
169 err("lookup returned incorrect value");
171 err("lookup returned bogus value");
175 if (map.t.count != unique_count)
176 err("Map has incorrect count");
178 printf("stringmap test passed after %zu inserts, %zu lookups (%zu total operations)\n", unique_count, (i-entries)-unique_count, i-entries);
185 printf("stringmap test failed after %zu inserts, %zu lookups (%zu total operations)\n", unique_count, (i-entries)-unique_count, i-entries);
202 ok1(test_stringmap(10000, NULL));
204 return exit_status();