Wed, 06 Feb 2013 14:35:15 +0100
Fixed map tests + added some formatting options to logger
1 /*
2 *
3 */
5 #include "map_tests.h"
7 UCX_TEST_IMPLEMENT(test_ucx_map_new) {
8 UcxMap *map = ucx_map_new(16);
9 UCX_TEST_BEGIN
10 UCX_TEST_ASSERT(map->size == 16, "wrong size");
11 UCX_TEST_ASSERT(map->map != NULL, "failed");
13 UCX_TEST_END
14 ucx_map_free(map);
15 }
17 UCX_TEST_IMPLEMENT(test_ucx_key) {
18 UcxKey key = ucx_key((void*)"This is a text.", 15);
19 UCX_TEST_BEGIN
20 UCX_TEST_ASSERT(strncmp((const char*)key.data, "This is a text.", 15) == 0,
21 "failed");
22 UCX_TEST_ASSERT(key.len == 15, "failed");
23 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed");
25 UCX_TEST_END
26 }
28 UCX_TEST_IMPLEMENT(test_ucx_map_put) {
30 UcxMap *map = ucx_map_new(4);
32 int td[5];
33 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
35 UCX_TEST_BEGIN
36 ucx_map_cstr_put(map, "Key2", &td[2]); /* 3.2 */
37 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0.0 */
38 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3.0 */
39 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 3.1 */
40 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 1.0 */
42 UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0");
43 UCX_TEST_ASSERT(*((int*)map->map[1]->data) == td[4], "failed KEY4");
44 UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1");
46 UCX_TEST_ASSERT(map->map[3]->next != NULL, "no list at slot 3");
47 UCX_TEST_ASSERT(map->map[3]->next->next != NULL, "list corrupt at slot 3");
48 UCX_TEST_ASSERT(*((int*)map->map[3]->next->data) == td[3],
49 "failed KeY3")
50 UCX_TEST_ASSERT(*((int*)map->map[3]->next->next->data) == td[2],
51 "failed KeY2");
53 UCX_TEST_ASSERT(map->map[0]->next == NULL, "slot 0 not terminated");
54 UCX_TEST_ASSERT(map->map[1]->next == NULL, "slot 1 not terminated");
55 UCX_TEST_ASSERT(map->map[2] == NULL, "slot 2 not empty");
56 UCX_TEST_ASSERT(map->map[3]->next->next->next == NULL,
57 "slot 3 not terminated")
59 ucx_map_cstr_put(map, "KeY3", &td[4]); /* replace 3.1 */
61 UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1],
62 "overwrite failed")
63 UCX_TEST_ASSERT(*((int*)map->map[3]->next->data) == td[4],
64 "overwrite failed");
65 UCX_TEST_ASSERT(*((int*)map->map[3]->next->next->data) == td[2],
66 "overwrite failed")
67 UCX_TEST_ASSERT(map->map[3]->next->next->next == NULL, "overwrite failed");
69 UCX_TEST_END
70 ucx_map_free(map);
71 }
73 UCX_TEST_IMPLEMENT(test_ucx_map_get) {
74 UcxMap *map = ucx_map_new(4);
76 int td[5];
77 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
79 ucx_map_cstr_put(map, "Key2", &td[2]);
80 ucx_map_cstr_put(map, "Key0", &td[0]);
81 ucx_map_cstr_put(map, "Key1", &td[1]);
82 ucx_map_cstr_put(map, "KeY3", &td[3]);
83 ucx_map_cstr_put(map, "KEY4", &td[4]);
84 UCX_TEST_BEGIN
86 td[0] = *((int*)ucx_map_cstr_get(map, "Key0"));
87 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
88 td[2] = *((int*)ucx_map_cstr_get(map, "Key2"));
89 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
90 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
91 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
92 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
93 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
94 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
95 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
97 UCX_TEST_ASSERT(map->count == 5, "expected 5 remaining values");
98 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0") != NULL, "element removed");
100 UCX_TEST_END
101 ucx_map_free(map);
102 }
104 UCX_TEST_IMPLEMENT(test_ucx_map_remove) {
105 UcxMap *map = ucx_map_new(4);
107 int td[5];
108 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
110 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
111 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
112 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
113 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
114 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
115 UCX_TEST_BEGIN
117 td[0] = *((int*)ucx_map_cstr_remove(map, "Key0"));
118 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
119 td[2] = *((int*)ucx_map_cstr_remove(map, "Key2"));
120 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
121 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
122 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
123 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
124 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
125 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
126 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
128 UCX_TEST_ASSERT(map->count == 3, "expected 3 remaining values");
129 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed");
130 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")!=NULL, "element removed");
131 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed");
132 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KeY3")!=NULL, "element removed");
133 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KEY4")!=NULL, "element removed");
135 UCX_TEST_ASSERT(ucx_map_cstr_remove(map, "Key2") == NULL,
136 "subsequent remove call shall return NULL");
138 UCX_TEST_END
139 ucx_map_free(map);
140 }
142 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
143 UcxMap *map = (UcxMap*) mapptr;
144 int v1 = 10;
145 int v2 = 15;
146 int v3 = 7;
147 int v4 = 9;
149 ucx_map_cstr_put(map, "v1", &v1);
150 ucx_map_cstr_put(map, "v2", &v2);
151 ucx_map_cstr_put(map, "v3", &v3);
152 ucx_map_cstr_put(map, "v4", &v4);
154 UcxMapIterator i = ucx_map_iterator(map);
155 int check = 0;
156 int hit = 0;
158 int* v;
159 UCX_MAP_FOREACH(v, i) {
160 check += *v;
161 hit++;
162 }
164 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
165 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
166 }
168 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
169 UcxMap *map = ucx_map_new(16);
170 UCX_TEST_BEGIN
171 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
172 UCX_TEST_END
173 ucx_map_free(map);
174 }
176 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
177 UcxMap *map = ucx_map_new(1);
178 UCX_TEST_BEGIN
179 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
180 UCX_TEST_END
181 ucx_map_free(map);
182 }
184 void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
185 const char *string = (const char*) value;
186 size_t n = strlen(string);
187 char *encoded = (char*) malloc(n+1);
188 for (int i = 0 ; i < n ; i++) {
189 encoded[i] = string[n-1-i];
190 }
191 encoded[n] = 0;
192 *size = n+1;
193 return encoded;
194 }
196 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
197 UcxMap *map = ucx_map_new(4);
199 ucx_map_cstr_put(map, "test", (void*)"test");
200 ucx_map_cstr_put(map, "key", (void*)"value");
201 ucx_map_cstr_put(map, "other.very.long.key", (void*)"value");
202 ucx_map_cstr_put(map, "testkey", (void*)"testvalue");
203 ucx_map_cstr_put(map, "simple", (void*)"not a key but an extremely long "
204 "value to test if the buffer extension works as designed");
206 UCX_TEST_BEGIN
207 FILE *f = tmpfile();
208 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted")
209 int r;
211 fwrite(" # comment test\n", 1, 16, f);
212 r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL);
213 fwrite("!discard this", 1, 13, f);
214 fflush(f);
216 ucx_map_free(map);
217 map = ucx_map_new(1);
218 fseek(f, 0, SEEK_SET);
219 UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
220 r += ucx_map_load_enc(map, f, allocator,
221 test_ucx_map_store_load_encdec, NULL);
222 fclose(f);
224 const char *value;
225 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
227 value = (const char *) ucx_map_cstr_get(map, "test");
228 UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
229 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
231 value = (const char *) ucx_map_cstr_get(map, "key");
232 UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
233 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
235 value = (const char *) ucx_map_cstr_get(map, "other.very.long.key");
236 UCX_TEST_ASSERT(value != NULL,
237 "value not found for key: other.very.long.key");
238 UCX_TEST_ASSERT(strcmp(value, "value") == 0,
239 "value error for key: other.very.long.key");
241 value = (const char *) ucx_map_cstr_get(map, "testkey");
242 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
243 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
244 "value error for key: testkey");
246 value = (const char *) ucx_map_cstr_get(map, "simple");
247 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
248 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
249 "to test if the buffer extension works as designed") == 0,
250 "value error for key: simple");
252 void *d;
253 UcxMapIterator iter = ucx_map_iterator(map);
254 UCX_MAP_FOREACH(d, iter) {
255 free(d);
256 }
257 ucx_map_free(map);
258 UCX_TEST_END
259 }
261 UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
262 UcxMap *map = ucx_map_new(4);
264 ucx_map_cstr_put(map, "test", (void*)"test");
265 ucx_map_cstr_put(map, "key", (void*)"value");
266 ucx_map_cstr_put(map, "testkey", (void*)"testvalue");
267 ucx_map_cstr_put(map, "simple", (void*)"a simple value");
269 UCX_TEST_BEGIN
270 FILE *f = tmpfile();
271 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted");
272 int r;
273 r = ucx_map_store_enc(map, f, NULL, NULL);
274 ucx_map_free(map);
275 fflush(f);
277 UcxMempool *pool = ucx_mempool_new(4);
278 map = ucx_map_new(4);
279 fseek(f, 0, SEEK_SET);
280 UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
281 r += ucx_map_load_enc(map, f, allocator,
282 test_ucx_map_store_load_encdec, NULL);
283 fclose(f);
285 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
286 UcxMapIterator iter = ucx_map_iterator(map);
287 const char *value; size_t n;
288 UCX_MAP_FOREACH(value, iter) {
289 n = strlen(value);
290 UCX_TEST_ASSERT(strncmp((const char*) pool->data[iter.index], value, n),
291 "values of map does not match pooled values");
292 }
294 ucx_mempool_free(pool);
295 ucx_map_free(map);
296 UCX_TEST_END
297 }
299 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
300 UcxMap *map = ucx_map_new(4);
302 ucx_map_cstr_put(map, "key1", (void*)"value1");
303 ucx_map_cstr_put(map, "key2", (void*)"value2");
304 ucx_map_cstr_put(map, "key3", (void*)"value3");
306 UcxMap *clone = ucx_map_clone(map, NULL, NULL);
308 const char *v1 = (const char *) ucx_map_cstr_get(map, "key1");
309 const char *v2 = (const char *) ucx_map_cstr_get(map, "key2");
310 const char *v3 = (const char *) ucx_map_cstr_get(map, "key3");
312 UCX_TEST_BEGIN
314 UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
315 UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
316 UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
318 const char *c1 = (const char *) ucx_map_cstr_get(clone, "key1");
319 const char *c2 = (const char *) ucx_map_cstr_get(clone, "key2");
320 const char *c3 = (const char *) ucx_map_cstr_get(clone, "key3");
322 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
323 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
324 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
326 UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
327 UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
328 UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
330 UCX_TEST_END
332 ucx_map_free(map);
333 ucx_map_free(clone);
334 }
336 UCX_TEST_IMPLEMENT(test_ucx_map_rehash) {
337 UcxMap *map = ucx_map_new(4);
339 char keys[10][5];
340 char values[10][7];
341 for (int i = 0 ; i < 10 ; i++) {
342 strcpy(keys[i], "key");
343 keys[i][3] = 48+i; keys[i][4] = 0;
344 strcpy(values[i], "value");
345 values[i][5] = 48+i; values[i][6] = 0;
347 ucx_map_cstr_put(map, keys[i], values[i]);
348 }
350 ucx_map_rehash(map);
352 UCX_TEST_BEGIN
353 UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count");
354 UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect");
355 for (int i = 0 ; i < 10 ; i++) {
356 const char *value = (const char *) ucx_map_cstr_get(map, keys[i]);
357 UCX_TEST_ASSERT(value != NULL, "new map is missing old keys");
358 UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0,
359 "new map contains incorrect values");
360 }
361 ucx_map_rehash(map);
362 UCX_TEST_ASSERT(map->size == 25,
363 "subsequent rehashing call shall not change size");
364 UCX_TEST_END
366 ucx_map_free(map);
367 }