Tue, 09 Oct 2012 10:21:18 +0200
fixed map tests + used tmpfiles in tests
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("This is a text.", 15);
19 UCX_TEST_BEGIN
20 UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed");
21 UCX_TEST_ASSERT(key.len == 15, "failed");
22 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed");
24 UCX_TEST_END
25 }
27 UCX_TEST_IMPLEMENT(test_ucx_map_put) {
29 UcxMap *map = ucx_map_new(4);
31 int td[5];
32 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
34 UCX_TEST_BEGIN
35 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
36 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
37 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
38 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
39 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
41 UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0");
42 UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0");
43 UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2");
44 UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0");
45 UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
46 "failed Key4")
47 UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL,
48 "slot 0 not terminated")
50 UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated");
52 UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3");
53 UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated");
55 UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1");
57 ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */
59 UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed");
60 UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2],
61 "overwrite failed")
62 UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
63 "overwrite failed")
64 UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed");
66 UCX_TEST_END
67 ucx_map_free(map);
68 }
70 UCX_TEST_IMPLEMENT(test_ucx_map_get) {
71 UcxMap *map = ucx_map_new(4);
73 int td[5];
74 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
76 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
77 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
78 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
79 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
80 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
81 UCX_TEST_BEGIN
83 td[0] = *((int*)ucx_map_cstr_get(map, "Key0"));
84 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
85 td[2] = *((int*)ucx_map_cstr_get(map, "Key2"));
86 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
87 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
88 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
89 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
90 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
91 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
92 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
94 UCX_TEST_ASSERT(map->count == 5, "expected 5 remaining values");
95 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0") != NULL, "element removed");
97 UCX_TEST_END
98 ucx_map_free(map);
99 }
101 UCX_TEST_IMPLEMENT(test_ucx_map_remove) {
102 UcxMap *map = ucx_map_new(4);
104 int td[5];
105 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
107 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
108 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
109 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
110 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
111 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
112 UCX_TEST_BEGIN
114 td[0] = *((int*)ucx_map_cstr_remove(map, "Key0"));
115 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
116 td[2] = *((int*)ucx_map_cstr_remove(map, "Key2"));
117 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
118 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
119 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
120 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
121 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
122 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
123 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
125 UCX_TEST_ASSERT(map->count == 3, "expected 3 remaining values");
126 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed");
127 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")!=NULL, "element removed");
128 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed");
129 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KeY3")!=NULL, "element removed");
130 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KEY4")!=NULL, "element removed");
132 UCX_TEST_ASSERT(ucx_map_cstr_remove(map, "Key2") == NULL,
133 "subsequent remove call shall return NULL");
135 UCX_TEST_END
136 ucx_map_free(map);
137 }
139 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
140 UcxMap *map = (UcxMap*) mapptr;
141 int v1 = 10;
142 int v2 = 15;
143 int v3 = 7;
144 int v4 = 9;
146 ucx_map_cstr_put(map, "v1", &v1);
147 ucx_map_cstr_put(map, "v2", &v2);
148 ucx_map_cstr_put(map, "v3", &v3);
149 ucx_map_cstr_put(map, "v4", &v4);
151 UcxMapIterator i = ucx_map_iterator(map);
152 int check = 0;
153 int hit = 0;
155 int* v;
156 UCX_MAP_FOREACH(v, i) {
157 check += *v;
158 hit++;
159 }
161 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
162 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
163 }
165 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
166 UcxMap *map = ucx_map_new(16);
167 UCX_TEST_BEGIN
168 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
169 UCX_TEST_END
170 ucx_map_free(map);
171 }
173 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
174 UcxMap *map = ucx_map_new(1);
175 UCX_TEST_BEGIN
176 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
177 UCX_TEST_END
178 ucx_map_free(map);
179 }
181 void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
182 char *string = (char*) value;
183 size_t n = strlen(string);
184 char *encoded = malloc(n+1);
185 for (int i = 0 ; i < n ; i++) {
186 encoded[i] = string[n-1-i];
187 }
188 encoded[n] = 0;
189 *size = n+1;
190 return encoded;
191 }
193 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
194 UcxMap *map = ucx_map_new(4);
196 ucx_map_cstr_put(map, "test", "test");
197 ucx_map_cstr_put(map, "key", "value");
198 ucx_map_cstr_put(map, "other.very.long.key", "value");
199 ucx_map_cstr_put(map, "testkey", "testvalue");
200 ucx_map_cstr_put(map, "simple", "not a key but an extremely long value "
201 "to test if the buffer extension works as designed");
203 UCX_TEST_BEGIN
204 FILE *f = tmpfile();
205 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted")
206 int r;
208 fwrite(" # comment test\n", 1, 16, f);
209 r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL);
210 fwrite("!discard this", 1, 13, f);
211 fflush(f);
213 ucx_map_free(map);
214 map = ucx_map_new(1);
215 fseek(f, 0, SEEK_SET);
216 UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
217 r += ucx_map_load_enc(map, f, allocator,
218 test_ucx_map_store_load_encdec, NULL);
219 fclose(f);
221 char *value;
222 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
224 value = ucx_map_cstr_get(map, "test");
225 UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
226 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
228 value = ucx_map_cstr_get(map, "key");
229 UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
230 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
232 value = ucx_map_cstr_get(map, "other.very.long.key");
233 UCX_TEST_ASSERT(value != NULL,
234 "value not found for key: other.very.long.key");
235 UCX_TEST_ASSERT(strcmp(value, "value") == 0,
236 "value error for key: other.very.long.key");
238 value = ucx_map_cstr_get(map, "testkey");
239 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
240 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
241 "value error for key: testkey");
243 value = ucx_map_cstr_get(map, "simple");
244 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
245 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
246 "to test if the buffer extension works as designed") == 0,
247 "value error for key: simple");
249 void *d;
250 UcxMapIterator iter = ucx_map_iterator(map);
251 UCX_MAP_FOREACH(d, iter) {
252 free(d);
253 }
254 ucx_map_free(map);
255 UCX_TEST_END
256 }
258 UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
259 UcxMap *map = ucx_map_new(4);
261 ucx_map_cstr_put(map, "test", "test");
262 ucx_map_cstr_put(map, "key", "value");
263 ucx_map_cstr_put(map, "testkey", "testvalue");
264 ucx_map_cstr_put(map, "simple", "a simple value");
266 UCX_TEST_BEGIN
267 FILE *f = tmpfile();
268 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted")
269 int r;
270 r = ucx_map_store_enc(map, f, NULL, NULL);
271 ucx_map_free(map);
272 fflush(f);
274 UcxMempool *pool = ucx_mempool_new(4);
275 map = ucx_map_new(4);
276 fseek(f, 0, SEEK_SET);
277 UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
278 r += ucx_map_load_enc(map, f, allocator,
279 test_ucx_map_store_load_encdec, NULL);
280 fclose(f);
282 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
283 UcxMapIterator iter = ucx_map_iterator(map);
284 char *value; size_t n;
285 UCX_MAP_FOREACH(value, iter) {
286 n = strlen(value);
287 UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n),
288 "values of map does not match pooled values");
289 }
291 ucx_mempool_free(pool);
292 ucx_map_free(map);
293 UCX_TEST_END
294 }
296 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
297 UcxMap *map = ucx_map_new(4);
299 ucx_map_cstr_put(map, "key1", "value1");
300 ucx_map_cstr_put(map, "key2", "value2");
301 ucx_map_cstr_put(map, "key3", "value3");
303 UcxMap *clone = ucx_map_clone(map, NULL, NULL);
305 char *v1 = ucx_map_cstr_get(map, "key1");
306 char *v2 = ucx_map_cstr_get(map, "key2");
307 char *v3 = ucx_map_cstr_get(map, "key3");
309 UCX_TEST_BEGIN
311 UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
312 UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
313 UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
315 char *c1 = ucx_map_cstr_get(clone, "key1");
316 char *c2 = ucx_map_cstr_get(clone, "key2");
317 char *c3 = ucx_map_cstr_get(clone, "key3");
319 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
320 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
321 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
323 UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
324 UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
325 UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
327 UCX_TEST_END
329 ucx_map_free(map);
330 ucx_map_free(clone);
331 }
333 UCX_TEST_IMPLEMENT(test_ucx_map_rehash) {
334 UcxMap *map = ucx_map_new(4);
336 char keys[10][5];
337 char values[10][7];
338 for (int i = 0 ; i < 10 ; i++) {
339 strcpy(keys[i], "key");
340 keys[i][3] = 48+i; keys[i][4] = 0;
341 strcpy(values[i], "value");
342 values[i][5] = 48+i; values[i][6] = 0;
344 ucx_map_cstr_put(map, keys[i], values[i]);
345 }
347 ucx_map_rehash(map);
349 UCX_TEST_BEGIN
350 UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count");
351 UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect");
352 for (int i = 0 ; i < 10 ; i++) {
353 char *value = ucx_map_cstr_get(map, keys[i]);
354 UCX_TEST_ASSERT(value != NULL, "new map is missing old keys");
355 UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0,
356 "new map contains incorrect values");
357 }
358 ucx_map_rehash(map);
359 UCX_TEST_ASSERT(map->size == 25,
360 "subsequent rehashing call shall not change size");
361 UCX_TEST_END
363 ucx_map_free(map);
364 }