test/map_tests.c

changeset 69
fb59270b1de3
parent 55
180bc6b18fec
child 71
303dabadff1c
equal deleted inserted replaced
68:88dbea299440 69:fb59270b1de3
15 } 15 }
16 16
17 UCX_TEST_IMPLEMENT(test_ucx_key) { 17 UCX_TEST_IMPLEMENT(test_ucx_key) {
18 UcxKey key = ucx_key("This is a text.", 15); 18 UcxKey key = ucx_key("This is a text.", 15);
19 UCX_TEST_BEGIN 19 UCX_TEST_BEGIN
20 UCX_TEST_ASSERT(strncmp(key.data, "This is a text.", 15) == 0, "failed"); 20 UCX_TEST_ASSERT(strncmp((const char*)key.data, "This is a text.", 15) == 0,
21 "failed");
21 UCX_TEST_ASSERT(key.len == 15, "failed"); 22 UCX_TEST_ASSERT(key.len == 15, "failed");
22 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed"); 23 UCX_TEST_ASSERT(key.hash == 1261186027, "hash failed");
23 24
24 UCX_TEST_END 25 UCX_TEST_END
25 } 26 }
177 UCX_TEST_END 178 UCX_TEST_END
178 ucx_map_free(map); 179 ucx_map_free(map);
179 } 180 }
180 181
181 void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) { 182 void* test_ucx_map_store_load_encdec(void *value, void *data, size_t *size) {
182 char *string = (char*) value; 183 const char *string = (const char*) value;
183 size_t n = strlen(string); 184 size_t n = strlen(string);
184 char *encoded = malloc(n+1); 185 char *encoded = (char*) malloc(n+1);
185 for (int i = 0 ; i < n ; i++) { 186 for (int i = 0 ; i < n ; i++) {
186 encoded[i] = string[n-1-i]; 187 encoded[i] = string[n-1-i];
187 } 188 }
188 encoded[n] = 0; 189 encoded[n] = 0;
189 *size = n+1; 190 *size = n+1;
216 UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT; 217 UcxAllocator allocator = UCX_ALLOCATOR_DEFAULT;
217 r += ucx_map_load_enc(map, f, allocator, 218 r += ucx_map_load_enc(map, f, allocator,
218 test_ucx_map_store_load_encdec, NULL); 219 test_ucx_map_store_load_encdec, NULL);
219 fclose(f); 220 fclose(f);
220 221
221 char *value; 222 const char *value;
222 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); 223 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
223 224
224 value = ucx_map_cstr_get(map, "test"); 225 value = (const char *) ucx_map_cstr_get(map, "test");
225 UCX_TEST_ASSERT(value != NULL, "value not found for key: test"); 226 UCX_TEST_ASSERT(value != NULL, "value not found for key: test");
226 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test"); 227 UCX_TEST_ASSERT(strcmp(value, "test") == 0, "value error for key: test");
227 228
228 value = ucx_map_cstr_get(map, "key"); 229 value = (const char *) ucx_map_cstr_get(map, "key");
229 UCX_TEST_ASSERT(value != NULL, "value not found for key: key"); 230 UCX_TEST_ASSERT(value != NULL, "value not found for key: key");
230 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key"); 231 UCX_TEST_ASSERT(strcmp(value, "value") == 0, "value error for key: key");
231 232
232 value = ucx_map_cstr_get(map, "other.very.long.key"); 233 value = (const char *) ucx_map_cstr_get(map, "other.very.long.key");
233 UCX_TEST_ASSERT(value != NULL, 234 UCX_TEST_ASSERT(value != NULL,
234 "value not found for key: other.very.long.key"); 235 "value not found for key: other.very.long.key");
235 UCX_TEST_ASSERT(strcmp(value, "value") == 0, 236 UCX_TEST_ASSERT(strcmp(value, "value") == 0,
236 "value error for key: other.very.long.key"); 237 "value error for key: other.very.long.key");
237 238
238 value = ucx_map_cstr_get(map, "testkey"); 239 value = (const char *) ucx_map_cstr_get(map, "testkey");
239 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey"); 240 UCX_TEST_ASSERT(value != NULL, "value not found for key: testkey");
240 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0, 241 UCX_TEST_ASSERT(strcmp(value, "testvalue") == 0,
241 "value error for key: testkey"); 242 "value error for key: testkey");
242 243
243 value = ucx_map_cstr_get(map, "simple"); 244 value = (const char *) ucx_map_cstr_get(map, "simple");
244 UCX_TEST_ASSERT(value != NULL, "value not found for key: simple"); 245 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 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 "to test if the buffer extension works as designed") == 0,
247 "value error for key: simple"); 248 "value error for key: simple");
248 249
279 test_ucx_map_store_load_encdec, NULL); 280 test_ucx_map_store_load_encdec, NULL);
280 fclose(f); 281 fclose(f);
281 282
282 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed"); 283 UCX_TEST_ASSERT(r == 0, "IO errors, test cannot be performed");
283 UcxMapIterator iter = ucx_map_iterator(map); 284 UcxMapIterator iter = ucx_map_iterator(map);
284 char *value; size_t n; 285 const char *value; size_t n;
285 UCX_MAP_FOREACH(value, iter) { 286 UCX_MAP_FOREACH(value, iter) {
286 n = strlen(value); 287 n = strlen(value);
287 UCX_TEST_ASSERT(strncmp(pool->data[iter.index], value, n), 288 UCX_TEST_ASSERT(strncmp((const char*) pool->data[iter.index], value, n),
288 "values of map does not match pooled values"); 289 "values of map does not match pooled values");
289 } 290 }
290 291
291 ucx_mempool_free(pool); 292 ucx_mempool_free(pool);
292 ucx_map_free(map); 293 ucx_map_free(map);
300 ucx_map_cstr_put(map, "key2", "value2"); 301 ucx_map_cstr_put(map, "key2", "value2");
301 ucx_map_cstr_put(map, "key3", "value3"); 302 ucx_map_cstr_put(map, "key3", "value3");
302 303
303 UcxMap *clone = ucx_map_clone(map, NULL, NULL); 304 UcxMap *clone = ucx_map_clone(map, NULL, NULL);
304 305
305 char *v1 = ucx_map_cstr_get(map, "key1"); 306 const char *v1 = (const char *) ucx_map_cstr_get(map, "key1");
306 char *v2 = ucx_map_cstr_get(map, "key2"); 307 const char *v2 = (const char *) ucx_map_cstr_get(map, "key2");
307 char *v3 = ucx_map_cstr_get(map, "key3"); 308 const char *v3 = (const char *) ucx_map_cstr_get(map, "key3");
308 309
309 UCX_TEST_BEGIN 310 UCX_TEST_BEGIN
310 311
311 UCX_TEST_ASSERT(v1 != NULL, "failed key 1"); 312 UCX_TEST_ASSERT(v1 != NULL, "failed key 1");
312 UCX_TEST_ASSERT(v2 != NULL, "failed key 2"); 313 UCX_TEST_ASSERT(v2 != NULL, "failed key 2");
313 UCX_TEST_ASSERT(v3 != NULL, "failed key 3"); 314 UCX_TEST_ASSERT(v3 != NULL, "failed key 3");
314 315
315 char *c1 = ucx_map_cstr_get(clone, "key1"); 316 const char *c1 = (const char *) ucx_map_cstr_get(clone, "key1");
316 char *c2 = ucx_map_cstr_get(clone, "key2"); 317 const char *c2 = (const char *) ucx_map_cstr_get(clone, "key2");
317 char *c3 = ucx_map_cstr_get(clone, "key3"); 318 const char *c3 = (const char *) ucx_map_cstr_get(clone, "key3");
318 319
319 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)"); 320 UCX_TEST_ASSERT(c1 != NULL, "failed key 1 (clone)");
320 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)"); 321 UCX_TEST_ASSERT(c2 != NULL, "failed key 2 (clone)");
321 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)"); 322 UCX_TEST_ASSERT(c3 != NULL, "failed key 3 (clone)");
322 323
348 349
349 UCX_TEST_BEGIN 350 UCX_TEST_BEGIN
350 UCX_TEST_ASSERT(map->size == 25, "new capacity shall be 2.5 * count"); 351 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 UCX_TEST_ASSERT(map->count == 10, "new map element count incorrect");
352 for (int i = 0 ; i < 10 ; i++) { 353 for (int i = 0 ; i < 10 ; i++) {
353 char *value = ucx_map_cstr_get(map, keys[i]); 354 const char *value = (const char *) ucx_map_cstr_get(map, keys[i]);
354 UCX_TEST_ASSERT(value != NULL, "new map is missing old keys"); 355 UCX_TEST_ASSERT(value != NULL, "new map is missing old keys");
355 UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0, 356 UCX_TEST_ASSERT(strncmp(value, values[i], 6) == 0,
356 "new map contains incorrect values"); 357 "new map contains incorrect values");
357 } 358 }
358 ucx_map_rehash(map); 359 ucx_map_rehash(map);

mercurial