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;
73 static int by_str(const void *ap, const void *bp) {
74 return strcmp(((struct test_entry*)ap)->str, ((struct test_entry*)bp)->str);
77 static void cull_duplicates(struct test_entry *entries, size_t *count) {
78 struct test_entry *i, *o, *e = entries + *count;
80 qsort(entries, *count, sizeof(*entries), by_str);
82 for (i=entries, o=entries; i<e;) {
83 //skip repeated strings
85 const char *last = i[-1].str;
86 if (!strcmp(last, i->str)) {
87 do i++; while(i<e && !strcmp(last, i->str));
92 //write all entries with the same value (should also have same string)
94 char *value = i->value;
95 do *o++ = *i++; while(i<e && i->value == value);
102 static int test_stringmap(size_t count, FILE *out) {
103 stringmap(char*) map = stringmap_new(NULL);
105 #define print(tag, fmt, ...) do { \
107 fprintf(out, tag fmt "\n", ##__VA_ARGS__); \
109 #define err(...) do { \
110 print("error: ", __VA_ARGS__); \
113 #define debug(...) print("debug: ", __VA_ARGS__)
114 #define msg(...) print("info: ", __VA_ARGS__)
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;
122 //we use value to track whether an entry has been added or not
123 memset(value, 0, count);
125 msg("Generating %zu test entries...", count);
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;
131 for (;same_count-- && i<e; i++) {
137 cull_duplicates(entries, &count);
139 scramble(entries, count, sizeof(*entries));
141 msg("Inserting/looking up %zu entries...", count);
143 for (i=entries; i<e; i++) {
146 debug("Looking up %s", i->str);
148 node = stringmap_lookup(map, i->str);
152 err("Previously inserted entry not found");
154 debug("Not found; entering");
156 node = stringmap_enter(map, i->str);
157 if (!node || strcmp(i->str, map.last->str))
158 err("Node not properly entered");
160 *i->value = 1; //mark that the entry is entered
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");
169 err("lookup returned bogus value");
173 if (map.t.count != unique_count)
174 err("Map has incorrect count");
176 printf("stringmap test passed after %zu inserts, %zu lookups (%zu total operations)\n", unique_count, (i-entries)-unique_count, i-entries);
183 printf("stringmap test failed after %zu inserts, %zu lookups (%zu total operations)\n", unique_count, (i-entries)-unique_count, i-entries);
200 ok1(test_stringmap(10000, NULL));
202 return exit_status();