202 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) |
202 UCX_TEST_CALL_SUBROUTINE(test_ucx_map_itersrt, map) |
203 UCX_TEST_END |
203 UCX_TEST_END |
204 ucx_map_free(map); |
204 ucx_map_free(map); |
205 } |
205 } |
206 |
206 |
207 void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) { |
|
208 const char *string = (const char*) value; |
|
209 size_t n = strlen(string); |
|
210 char *encoded = (char*) malloc(n+1); |
|
211 for (size_t i = 0 ; i < n ; i++) { |
|
212 encoded[i] = string[n-1-i]; |
|
213 } |
|
214 encoded[n] = 0; |
|
215 *size = n+1; |
|
216 return encoded; |
|
217 } |
|
218 |
|
219 UCX_TEST_IMPLEMENT(test_ucx_map_store_load) { |
|
220 UcxMap *map = ucx_map_new(4); |
|
221 |
|
222 ucx_map_cstr_put(map, "test", (void*)"test"); |
|
223 ucx_map_cstr_put(map, "key", (void*)"value"); |
|
224 ucx_map_cstr_put(map, "other.very.long.key", (void*)"value"); |
|
225 ucx_map_cstr_put(map, "testkey", (void*)"testvalue"); |
|
226 ucx_map_cstr_put(map, "simple", (void*)"not a key but an extremely long " |
|
227 "value to test if the buffer extension works as designed"); |
|
228 |
|
229 UCX_TEST_BEGIN |
|
230 FILE *f = tmpfile(); |
|
231 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted") |
|
232 int r; |
|
233 |
|
234 fwrite(" # comment test\n", 1, 16, f); |
|
235 r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL); |
|
236 fwrite("!discard this", 1, 13, f); |
|
237 fflush(f); |
|
238 |
|
239 ucx_map_free(map); |
|
240 map = ucx_map_new(1); |
|
241 fseek(f, 0, SEEK_SET); |
|
242 UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT; |
|
243 r += ucx_map_load_enc(map, f, allocator, |
|
244 test_ucx_map_store_load_encdec, NULL); |
|
245 fclose(f); |
|
246 |
|
247 const char *value; |
|
248 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); |
|
249 |
|
250 value = (const char *) ucx_map_cstr_get(map, "test"); |
|
251 UCX_TEST_ASSERT(value != NULL, "value not found for key: test"); |
|
252 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test"); |
|
253 |
|
254 value = (const char *) ucx_map_cstr_get(map, "key"); |
|
255 UCX_TEST_ASSERT(value != NULL, "value not found for key: key"); |
|
256 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key"); |
|
257 |
|
258 value = (const char *) ucx_map_cstr_get(map, "other.very.long.key"); |
|
259 UCX_TEST_ASSERT(value != NULL, |
|
260 "value not found for key: other.very.long.key"); |
|
261 UCX_TEST_ASSERT(strcmp(value, "value") == 0, |
|
262 "value error for key: other.very.long.key"); |
|
263 |
|
264 value = (const char *) ucx_map_cstr_get(map, "testkey"); |
|
265 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey"); |
|
266 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0, |
|
267 "value error for key: testkey"); |
|
268 |
|
269 value = (const char *) ucx_map_cstr_get(map, "simple"); |
|
270 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple"); |
|
271 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value " |
|
272 "to test if the buffer extension works as designed") == 0, |
|
273 "value error for key: simple"); |
|
274 |
|
275 void *d; |
|
276 UcxMapIterator iter = ucx_map_iterator(map); |
|
277 UCX_MAP_FOREACH(key, d, iter) { |
|
278 free(d); |
|
279 } |
|
280 ucx_map_free(map); |
|
281 UCX_TEST_END |
|
282 } |
|
283 |
|
284 UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) { |
|
285 UcxMap *map = ucx_map_new(4); |
|
286 |
|
287 ucx_map_cstr_put(map, "test", (void*)"test"); |
|
288 ucx_map_cstr_put(map, "key", (void*)"value"); |
|
289 ucx_map_cstr_put(map, "testkey", (void*)"testvalue"); |
|
290 ucx_map_cstr_put(map, "simple", (void*)"a simple value"); |
|
291 |
|
292 UCX_TEST_BEGIN |
|
293 FILE *f = tmpfile(); |
|
294 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted"); |
|
295 int r; |
|
296 r = ucx_map_store_enc(map, f, NULL, NULL); |
|
297 ucx_map_free(map); |
|
298 fflush(f); |
|
299 |
|
300 UcxMempool *pool = ucx_mempool_new(4); |
|
301 map = ucx_map_new(4); |
|
302 fseek(f, 0, SEEK_SET); |
|
303 UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool); |
|
304 r += ucx_map_load_enc(map, f, allocator, |
|
305 test_ucx_map_store_load_encdec, NULL); |
|
306 fclose(f); |
|
307 |
|
308 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); |
|
309 UcxMapIterator iter = ucx_map_iterator(map); |
|
310 const char *value; size_t n; |
|
311 UCX_MAP_FOREACH(key, value, iter) { |
|
312 n = strlen(value); |
|
313 UCX_TEST_ASSERT(strncmp((const char*) pool->data[iter.index], value, n), |
|
314 "values of map does not match pooled values"); |
|
315 } |
|
316 |
|
317 ucx_mempool_free(pool); |
|
318 ucx_map_free(map); |
|
319 UCX_TEST_END |
|
320 } |
|
321 |
|
322 UCX_TEST_IMPLEMENT(test_ucx_map_clone) { |
207 UCX_TEST_IMPLEMENT(test_ucx_map_clone) { |
323 UcxMap *map = ucx_map_new(4); |
208 UcxMap *map = ucx_map_new(4); |
324 |
209 |
325 ucx_map_cstr_put(map, "key1", (void*)"value1"); |
210 ucx_map_cstr_put(map, "key1", (void*)"value1"); |
326 ucx_map_cstr_put(map, "key2", (void*)"value2"); |
211 ucx_map_cstr_put(map, "key2", (void*)"value2"); |