Fri, 30 Nov 2012 13:10:58 +0100
fixed buffer
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]); /* 0 */
37 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
38 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
39 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
40 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
42 UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[0], "failed Key0");
43 UCX_TEST_ASSERT(map->map[0]->next != NULL, "no list at slot 0");
44 UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2], "failed Key2");
45 UCX_TEST_ASSERT(map->map[0]->next->next != NULL, "list corrupt at slot 0");
46 UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
47 "failed Key4")
48 UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL,
49 "slot 0 not terminated")
51 UCX_TEST_ASSERT(map->map[1] == NULL, "slot 1 not terminated");
53 UCX_TEST_ASSERT(*((int*)map->map[2]->data) == td[3], "failed KeY3");
54 UCX_TEST_ASSERT(map->map[2]->next == NULL, "slot 2 not terminated");
56 UCX_TEST_ASSERT(*((int*)map->map[3]->data) == td[1], "failed Key1");
58 ucx_map_cstr_put(map, "Key0", &td[3]); /* 0 */
60 UCX_TEST_ASSERT(*((int*)map->map[0]->data) == td[3], "overwrite failed");
61 UCX_TEST_ASSERT(*((int*)map->map[0]->next->data) == td[2],
62 "overwrite failed")
63 UCX_TEST_ASSERT(*((int*)map->map[0]->next->next->data) == td[4],
64 "overwrite failed")
65 UCX_TEST_ASSERT(map->map[0]->next->next->next == NULL, "overwrite failed");
67 UCX_TEST_END
68 ucx_map_free(map);
69 }
71 UCX_TEST_IMPLEMENT(test_ucx_map_get) {
72 UcxMap *map = ucx_map_new(4);
74 int td[5];
75 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
77 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
78 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
79 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
80 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
81 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
82 UCX_TEST_BEGIN
84 td[0] = *((int*)ucx_map_cstr_get(map, "Key0"));
85 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
86 td[2] = *((int*)ucx_map_cstr_get(map, "Key2"));
87 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
88 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
89 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
90 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
91 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
92 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
93 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
95 UCX_TEST_ASSERT(map->count == 5, "expected 5 remaining values");
96 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0") != NULL, "element removed");
98 UCX_TEST_END
99 ucx_map_free(map);
100 }
102 UCX_TEST_IMPLEMENT(test_ucx_map_remove) {
103 UcxMap *map = ucx_map_new(4);
105 int td[5];
106 td[0] = 10; td[1] = 42; td[2] = 70; td[3] = 11200; td[4] = 80000;
108 ucx_map_cstr_put(map, "Key2", &td[2]); /* 0 */
109 ucx_map_cstr_put(map, "Key0", &td[0]); /* 0 */
110 ucx_map_cstr_put(map, "Key1", &td[1]); /* 3 */
111 ucx_map_cstr_put(map, "KeY3", &td[3]); /* 2 */
112 ucx_map_cstr_put(map, "KEY4", &td[4]); /* 0 */
113 UCX_TEST_BEGIN
115 td[0] = *((int*)ucx_map_cstr_remove(map, "Key0"));
116 td[1] = *((int*)ucx_map_cstr_get(map, "Key1"));
117 td[2] = *((int*)ucx_map_cstr_remove(map, "Key2"));
118 td[3] = *((int*)ucx_map_cstr_get(map, "KeY3"));
119 td[4] = *((int*)ucx_map_cstr_get(map, "KEY4"));
120 UCX_TEST_ASSERT(td[0] == 10, "failed key 0");
121 UCX_TEST_ASSERT(td[1] == 42, "failed key 1");
122 UCX_TEST_ASSERT(td[2] == 70, "failed key 2");
123 UCX_TEST_ASSERT(td[3] == 11200, "failed key 3");
124 UCX_TEST_ASSERT(td[4] == 80000, "failed key 4");
126 UCX_TEST_ASSERT(map->count == 3, "expected 3 remaining values");
127 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key0")==NULL, "element not removed");
128 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key1")!=NULL, "element removed");
129 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "Key2")==NULL, "element not removed");
130 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KeY3")!=NULL, "element removed");
131 UCX_TEST_ASSERT(ucx_map_cstr_get(map, "KEY4")!=NULL, "element removed");
133 UCX_TEST_ASSERT(ucx_map_cstr_remove(map, "Key2") == NULL,
134 "subsequent remove call shall return NULL");
136 UCX_TEST_END
137 ucx_map_free(map);
138 }
140 UCX_TEST_SUBROUTINE(test_ucx_map_itersrt, mapptr) {
141 UcxMap *map = (UcxMap*) mapptr;
142 int v1 = 10;
143 int v2 = 15;
144 int v3 = 7;
145 int v4 = 9;
147 ucx_map_cstr_put(map, "v1", &v1);
148 ucx_map_cstr_put(map, "v2", &v2);
149 ucx_map_cstr_put(map, "v3", &v3);
150 ucx_map_cstr_put(map, "v4", &v4);
152 UcxMapIterator i = ucx_map_iterator(map);
153 int check = 0;
154 int hit = 0;
156 int* v;
157 UCX_MAP_FOREACH(v, i) {
158 check += *v;
159 hit++;
160 }
162 UCX_TEST_ASSERT(hit == 4, "test1: wrong number of hits");
163 UCX_TEST_ASSERT(check == v1+v2+v3+v4, "test1: wrong result");
164 }
166 UCX_TEST_IMPLEMENT(test_ucx_map_iterator) {
167 UcxMap *map = ucx_map_new(16);
168 UCX_TEST_BEGIN
169 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
170 UCX_TEST_END
171 ucx_map_free(map);
172 }
174 UCX_TEST_IMPLEMENT(test_ucx_map_iterator_chain) {
175 UcxMap *map = ucx_map_new(1);
176 UCX_TEST_BEGIN
177 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map)
178 UCX_TEST_END
179 ucx_map_free(map);
180 }
182 void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
183 const char *string = (const char*) value;
184 size_t n = strlen(string);
185 char *encoded = (char*) malloc(n+1);
186 for (int i = 0 ; i < n ; i++) {
187 encoded[i] = string[n-1-i];
188 }
189 encoded[n] = 0;
190 *size = n+1;
191 return encoded;
192 }
194 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) {
195 UcxMap *map = ucx_map_new(4);
197 ucx_map_cstr_put(map, "test", (void*)"test");
198 ucx_map_cstr_put(map, "key", (void*)"value");
199 ucx_map_cstr_put(map, "other.very.long.key", (void*)"value");
200 ucx_map_cstr_put(map, "testkey", (void*)"testvalue");
201 ucx_map_cstr_put(map, "simple", (void*)"not a key but an extremely long "
202 "value to test if the buffer extension works as designed");
204 UCX_TEST_BEGIN
205 FILE *f = tmpfile();
206 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted")
207 int r;
209 fwrite(" # comment test\n", 1, 16, f);
210 r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL);
211 fwrite("!discard this", 1, 13, f);
212 fflush(f);
214 ucx_map_free(map);
215 map = ucx_map_new(1);
216 fseek(f, 0, SEEK_SET);
217 UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
218 r += ucx_map_load_enc(map, f, allocator,
219 test_ucx_map_store_load_encdec, NULL);
220 fclose(f);
222 const char *value;
223 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
225 value = (const char *) ucx_map_cstr_get(map, "test");
226 UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
227 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
229 value = (const char *) ucx_map_cstr_get(map, "key");
230 UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
231 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
233 value = (const char *) ucx_map_cstr_get(map, "other.very.long.key");
234 UCX_TEST_ASSERT(value != NULL,
235 "value not found for key: other.very.long.key");
236 UCX_TEST_ASSERT(strcmp(value, "value") == 0,
237 "value error for key: other.very.long.key");
239 value = (const char *) ucx_map_cstr_get(map, "testkey");
240 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
241 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
242 "value error for key: testkey");
244 value = (const char *) ucx_map_cstr_get(map, "simple");
245 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
246 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
247 "to test if the buffer extension works as designed") == 0,
248 "value error for key: simple");
250 void *d;
251 UcxMapIterator iter = ucx_map_iterator(map);
252 UCX_MAP_FOREACH(d, iter) {
253 free(d);
254 }
255 ucx_map_free(map);
256 UCX_TEST_END
257 }
259 UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
260 UcxMap *map = ucx_map_new(4);
262 ucx_map_cstr_put(map, "test", (void*)"test");
263 ucx_map_cstr_put(map, "key", (void*)"value");
264 ucx_map_cstr_put(map, "testkey", (void*)"testvalue");
265 ucx_map_cstr_put(map, "simple", (void*)"a simple value");
267 UCX_TEST_BEGIN
268 FILE *f = tmpfile();
269 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted");
270 int r;
271 r = ucx_map_store_enc(map, f, NULL, NULL);
272 ucx_map_free(map);
273 fflush(f);
275 UcxMempool *pool = ucx_mempool_new(4);
276 map = ucx_map_new(4);
277 fseek(f, 0, SEEK_SET);
278 UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
279 r += ucx_map_load_enc(map, f, allocator,
280 test_ucx_map_store_load_encdec, NULL);
281 fclose(f);
283 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
284 UcxMapIterator iter = ucx_map_iterator(map);
285 const char *value; size_t n;
286 UCX_MAP_FOREACH(value, iter) {
287 n = strlen(value);
288 UCX_TEST_ASSERT(strncmp((const char*) pool->data[iter.index], value, n),
289 "values of map does not match pooled values");
290 }
292 ucx_mempool_free(pool);
293 ucx_map_free(map);
294 UCX_TEST_END
295 }
297 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
298 UcxMap *map = ucx_map_new(4);
300 ucx_map_cstr_put(map, "key1", (void*)"value1");
301 ucx_map_cstr_put(map, "key2", (void*)"value2");
302 ucx_map_cstr_put(map, "key3", (void*)"value3");
304 UcxMap *clone = ucx_map_clone(map, NULL, NULL);
306 const char *v1 = (const char *) ucx_map_cstr_get(map, "key1");
307 const char *v2 = (const char *) ucx_map_cstr_get(map, "key2");
308 const char *v3 = (const char *) ucx_map_cstr_get(map, "key3");
310 UCX_TEST_BEGIN
312 UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
313 UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
314 UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
316 const char *c1 = (const char *) ucx_map_cstr_get(clone, "key1");
317 const char *c2 = (const char *) ucx_map_cstr_get(clone, "key2");
318 const char *c3 = (const char *) ucx_map_cstr_get(clone, "key3");
320 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
321 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
322 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
324 UCX_TEST_ASSERT(strcmp(c1, v1) == 0, "value error for key1");
325 UCX_TEST_ASSERT(strcmp(c2, v2) == 0, "value error for key2");
326 UCX_TEST_ASSERT(strcmp(c3, v3) == 0, "value error for key3");
328 UCX_TEST_END
330 ucx_map_free(map);
331 ucx_map_free(clone);
332 }
334 UCX_TEST_IMPLEMENT(test_ucx_map_rehash) {
335 UcxMap *map = ucx_map_new(4);
337 char keys[10][5];
338 char values[10][7];
339 for (int i = 0 ; i < 10 ; i++) {
340 strcpy(keys[i], "key");
341 keys[i][3] = 48+i; keys[i][4] = 0;
342 strcpy(values[i], "value");
343 values[i][5] = 48+i; values[i][6] = 0;
345 ucx_map_cstr_put(map, keys[i], values[i]);
346 }
348 ucx_map_rehash(map);
350 UCX_TEST_BEGIN
351 UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count");
352 UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect");
353 for (int i = 0 ; i < 10 ; i++) {
354 const char *value = (const char *) ucx_map_cstr_get(map, keys[i]);
355 UCX_TEST_ASSERT(value != NULL, "new map is missing old keys");
356 UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0,
357 "new map contains incorrect values");
358 }
359 ucx_map_rehash(map);
360 UCX_TEST_ASSERT(map->size == 25,
361 "subsequent rehashing call shall not change size");
362 UCX_TEST_END
364 ucx_map_free(map);
365 }