test/map_tests.c

changeset 55
180bc6b18fec
parent 53
e533c170bfb8
child 69
fb59270b1de3
equal deleted inserted replaced
54:f634f790661a 55:180bc6b18fec
1 /* 1 /*
2 * 2 *
3 */ 3 */
4 4
5 #include "map_tests.h" 5 #include "map_tests.h"
6
7 #ifndef _WIN32
8 #include <unistd.h>
9 #endif /* not _WIN32 */
10 6
11 UCX_TEST_IMPLEMENT(test_ucx_map_new) { 7 UCX_TEST_IMPLEMENT(test_ucx_map_new) {
12 UcxMap *map = ucx_map_new(16); 8 UcxMap *map = ucx_map_new(16);
13 UCX_TEST_BEGIN 9 UCX_TEST_BEGIN
14 UCX_TEST_ASSERT(map->size == 16, "wrong size"); 10 UCX_TEST_ASSERT(map->size == 16, "wrong size");
202 ucx_map_cstr_put(map, "other.very.long.key", "value"); 198 ucx_map_cstr_put(map, "other.very.long.key", "value");
203 ucx_map_cstr_put(map, "testkey", "testvalue"); 199 ucx_map_cstr_put(map, "testkey", "testvalue");
204 ucx_map_cstr_put(map, "simple", "not a key but an extremely long value " 200 ucx_map_cstr_put(map, "simple", "not a key but an extremely long value "
205 "to test if the buffer extension works as designed"); 201 "to test if the buffer extension works as designed");
206 202
207 FILE *f = fopen("test_ucx_map_store", "w"); 203 UCX_TEST_BEGIN
204 FILE *f = tmpfile();
205 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted")
208 int r; 206 int r;
209 207
210 fwrite(" # comment test\n", 1, 16, f); 208 fwrite(" # comment test\n", 1, 16, f);
211 r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL); 209 r = ucx_map_store_enc(map, f, test_ucx_map_store_load_encdec, NULL);
212 fwrite("!discard this", 1, 13, f); 210 fwrite("!discard this", 1, 13, f);
213 211 fflush(f);
214 fclose(f); 212
215 ucx_map_free(map); 213 ucx_map_free(map);
216 map = ucx_map_new(1); 214 map = ucx_map_new(1);
217 f = fopen("test_ucx_map_store", "r"); 215 fseek(f, 0, SEEK_SET);
218 UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT; 216 UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
219 r += ucx_map_load_enc(map, f, allocator, 217 r += ucx_map_load_enc(map, f, allocator,
220 test_ucx_map_store_load_encdec, NULL); 218 test_ucx_map_store_load_encdec, NULL);
221 fclose(f); 219 fclose(f);
222 unlink("test_ucx_map_store"); 220
223
224 UCX_TEST_BEGIN
225 char *value; 221 char *value;
226 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); 222 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
227 223
228 value = ucx_map_cstr_get(map, "test"); 224 value = ucx_map_cstr_get(map, "test");
229 UCX_TEST_ASSERT(value != NULL, "value not found for key: test"); 225 UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
248 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple"); 244 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple");
249 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value " 245 UCX_TEST_ASSERT(strcmp(value, "not a key but an extremely long value "
250 "to test if the buffer extension works as designed") == 0, 246 "to test if the buffer extension works as designed") == 0,
251 "value error for key: simple"); 247 "value error for key: simple");
252 248
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);
253 UCX_TEST_END 255 UCX_TEST_END
254 } 256 }
255 257
256 UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) { 258 UCX_TEST_IMPLEMENT(test_ucx_map_store_load_with_mempool) {
257 UcxMap *map = ucx_map_new(4); 259 UcxMap *map = ucx_map_new(4);
259 ucx_map_cstr_put(map, "test", "test"); 261 ucx_map_cstr_put(map, "test", "test");
260 ucx_map_cstr_put(map, "key", "value"); 262 ucx_map_cstr_put(map, "key", "value");
261 ucx_map_cstr_put(map, "testkey", "testvalue"); 263 ucx_map_cstr_put(map, "testkey", "testvalue");
262 ucx_map_cstr_put(map, "simple", "a simple value"); 264 ucx_map_cstr_put(map, "simple", "a simple value");
263 265
264 FILE *f = fopen("test_ucx_map_store", "w"); 266 UCX_TEST_BEGIN
267 FILE *f = tmpfile();
268 UCX_TEST_ASSERT(f, "test file cannot be opened, test aborted")
265 int r; 269 int r;
266 r = ucx_map_store_enc(map, f, NULL, NULL); 270 r = ucx_map_store_enc(map, f, NULL, NULL);
267 fclose(f); 271 ucx_map_free(map);
268 ucx_map_free(map); 272 fflush(f);
269 273
270 UcxMempool *pool = ucx_mempool_new(4); 274 UcxMempool *pool = ucx_mempool_new(4);
271 map = ucx_map_new(4); 275 map = ucx_map_new(4);
272 f = fopen("test_ucx_map_store", "r"); 276 fseek(f, 0, SEEK_SET);
273 UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool); 277 UcxAllocator allocator = UCX_ALLOCATOR_MEMPOOL(pool);
274 r += ucx_map_load_enc(map, f, allocator, 278 r += ucx_map_load_enc(map, f, allocator,
275 test_ucx_map_store_load_encdec, NULL); 279 test_ucx_map_store_load_encdec, NULL);
276 fclose(f); 280 fclose(f);
277 unlink("test_ucx_map_store"); 281
278
279 UCX_TEST_BEGIN
280 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); 282 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
281 UcxMapIterator iter = ucx_map_iterator(map); 283 UcxMapIterator iter = ucx_map_iterator(map);
282 char *value; size_t n; 284 char *value; size_t n;
283 UCX_MAP_FOREACH(value, iter) { 285 UCX_MAP_FOREACH(value, iter) {
284 n = strlen(value); 286 n = strlen(value);
285 UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n), 287 UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n),
286 "values of map does not match pooled values"); 288 "values of map does not match pooled values");
287 } 289 }
288 UCX_TEST_END
289 290
290 ucx_mempool_free(pool); 291 ucx_mempool_free(pool);
292 ucx_map_free(map);
293 UCX_TEST_END
291 } 294 }
292 295
293 UCX_TEST_IMPLEMENT(test_ucx_map_clone) { 296 UCX_TEST_IMPLEMENT(test_ucx_map_clone) {
294 UcxMap *map = ucx_map_new(4); 297 UcxMap *map = ucx_map_new(4);
295 298

mercurial