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