1 #include <ccan/ciniparser/dictionary.h>
2 #include <ccan/ciniparser/dictionary.c>
4 #include <ccan/tap/tap.h>
6 static void test_trivial(void) {
7 dictionary *map = dictionary_new(0);
8 char *notfound = (char*)0xDEADBEEF;
10 ok1(dictionary_get(map, "notfound", notfound) == notfound);
12 ok1(dictionary_get(map, "one", NULL) == NULL);
13 ok1(dictionary_set(map, "one", "1") == 0);
15 ok1(dictionary_get(map, "two", NULL) == NULL);
16 ok1(dictionary_set(map, "two", "2") == 0);
18 ok1(dictionary_get(map, "three", NULL) == NULL);
19 ok1(dictionary_set(map, "three", "3") == 0);
21 ok1(dictionary_get(map, "four", NULL) == NULL);
22 ok1(dictionary_set(map, "four", "4") == 0);
24 ok1(!strcmp(dictionary_get(map, "three", NULL), "3"));
25 ok1(!strcmp(dictionary_get(map, "one", NULL), "1"));
26 ok1(!strcmp(dictionary_get(map, "four", NULL), "4"));
27 ok1(!strcmp(dictionary_get(map, "two", NULL), "2"));
34 static void scramble(void *base, size_t nmemb, size_t size) {
38 for (;nmemb>1;nmemb--) {
39 o = i + size*(random()%nmemb);
48 //#define RANDOM_STRING_READABLE
50 static char *random_string(void) {
51 size_t len = random() % 100;
52 char *str = malloc(len+1);
55 for (i=str; len--; i++) {
56 #ifndef RANDOM_STRING_READABLE
60 //only generate characters [32,126]
61 char c = random()%95 + 32;
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 = o[-1].str;
88 if (!strcmp(last, i->str)) {
92 } while(i<e && !strcmp(last, i->str));
97 //write all entries with the same value (should also have same string)
99 char *value = i->value;
100 do *o++ = *i++; while(i<e && i->value == value);
107 static void free_test_entries(struct test_entry *entries, size_t count) {
108 struct test_entry *i = entries;
114 static int test_dictionary(size_t count, FILE *out) {
115 dictionary *map = dictionary_new(0);
117 #define print(tag, fmt, ...) do { \
119 fprintf(out, tag fmt "\n", ##__VA_ARGS__); \
121 #define debug(...) print("debug: ", __VA_ARGS__)
122 #define msg(...) print("info: ", __VA_ARGS__)
124 struct test_entry *entries = malloc(sizeof(*entries) * count);
125 struct test_entry *i, *e = entries+count;
126 char *value_base = malloc(count), *value = value_base;
127 size_t unique_count = 0;
129 //we use value to track whether an entry has been added or not
130 memset(value, 0, count);
132 msg("Generating %zu test entries...", count);
134 for (i=entries; i<e; value++) {
135 char *str = random_string();
136 size_t same_count = random()%5 ? random()%3 : random()%10;
142 for (;same_count-- && i<e; i++) {
143 i->str = strdup(str);
148 cull_duplicates(entries, &count);
150 scramble(entries, count, sizeof(*entries));
152 #define err(...) do { \
153 print("error: ", __VA_ARGS__); \
157 msg("Inserting/looking up %zu entries...", count);
159 for (i=entries; i<e; i++) {
162 debug("Looking up %s", i->str);
164 value = dictionary_get(map, i->str, NULL);
170 err("Previously inserted entry not found");
172 debug("Not found; entering");
174 /* Because dictionary only accepts strings, and we want
175 to store pointer indices, we'll write the indices as
176 decimal numbers :) */
177 sprintf(value_str, "%lu", (unsigned long)(i->value - value_base));
179 if (dictionary_set(map, i->str, value_str) != 0)
180 err("dictionary_set had an error");
182 *i->value = 1; //mark that the entry is entered
186 if ((unsigned long)(i->value - value_base) !=
187 strtoul(value, NULL, 10))
188 err("lookup returned incorrect value");
190 err("lookup returned bogus value");
194 if (map->n != unique_count)
195 err("Dictionary has incorrect count");
197 printf("dictionary test passed after %zu inserts, %zu lookups (%zu total operations)\n", unique_count, (i-entries)-unique_count, i-entries);
199 free_test_entries(entries, e-entries);
205 printf("dictionary test failed after %zu inserts, %zu lookups (%zu total operations)\n", unique_count, (i-entries)-unique_count, i-entries);
207 free_test_entries(entries, e-entries);
223 ok1(test_dictionary(10000, NULL));
225 return exit_status();