tests/test_hash_map.c

changeset 890
54565fd74e74
parent 856
6bbbf219251d
equal deleted inserted replaced
889:f549fd9fbd8f 890:54565fd74e74
240 cxMapPut(map, "key 5", (void *) "val 5"); 240 cxMapPut(map, "key 5", (void *) "val 5");
241 cxMapPut(map, "key 6", (void *) "val 6"); 241 cxMapPut(map, "key 6", (void *) "val 6");
242 242
243 CxIterator iter = cxMapMutIterator(map); 243 CxIterator iter = cxMapMutIterator(map);
244 cx_foreach(CxMapEntry*, entry, iter) { 244 cx_foreach(CxMapEntry*, entry, iter) {
245 if (((char const *)entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter); 245 if (((const char *)entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter);
246 } 246 }
247 CX_TEST_ASSERT(map->collection.size == 3); 247 CX_TEST_ASSERT(map->collection.size == 3);
248 CX_TEST_ASSERT(iter.index == map->collection.size); 248 CX_TEST_ASSERT(iter.index == map->collection.size);
249 249
250 CX_TEST_ASSERT(cxMapGet(map, "key 1") == NULL); 250 CX_TEST_ASSERT(cxMapGet(map, "key 1") == NULL);
436 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar")); 436 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar"));
437 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "hallo"), "welt")); 437 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "hallo"), "welt"));
438 438
439 // note: we don't have a destructor here, so remove and detach are the same 439 // note: we don't have a destructor here, so remove and detach are the same
440 cxMapRemove(map, cx_str("test")); 440 cxMapRemove(map, cx_str("test"));
441 char const *hallo = "hallo"; 441 const char *hallo = "hallo";
442 cxMapDetach(map, hallo); 442 cxMapDetach(map, hallo);
443 cxMapPut(map, cx_hash_key_str("key"), "value"); 443 cxMapPut(map, cx_hash_key_str("key"), "value");
444 444
445 CX_TEST_ASSERT(map->collection.size == 2); 445 CX_TEST_ASSERT(map->collection.size == 2);
446 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key"), "value")); 446 CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key"), "value"));
458 } 458 }
459 cx_testing_allocator_destroy(&talloc); 459 cx_testing_allocator_destroy(&talloc);
460 } 460 }
461 461
462 struct test_map_kv { 462 struct test_map_kv {
463 char const *key; 463 const char *key;
464 char const *value; 464 const char *value;
465 }; 465 };
466 466
467 static struct test_map_kv const test_map_operations[] = { 467 static struct test_map_kv const test_map_operations[] = {
468 {"key 1", "test"}, 468 {"key 1", "test"},
469 {"key 2", "blub"}, 469 {"key 2", "blub"},
497 {"key 9", NULL}, 497 {"key 9", NULL},
498 }; 498 };
499 static size_t const test_map_reference_len = 499 static size_t const test_map_reference_len =
500 sizeof(test_map_reference) / sizeof(struct test_map_kv); 500 sizeof(test_map_reference) / sizeof(struct test_map_kv);
501 501
502 static void test_map_reference_put(char const *key, char const* value) { 502 static void test_map_reference_put(const char *key, const char *value) {
503 for (size_t i = 0 ; i < test_map_reference_len ; i++) { 503 for (size_t i = 0 ; i < test_map_reference_len ; i++) {
504 if (0 == strcmp(key, test_map_reference[i].key)) { 504 if (0 == strcmp(key, test_map_reference[i].key)) {
505 test_map_reference[i].value = value; 505 test_map_reference[i].value = value;
506 return; 506 return;
507 } 507 }
508 } 508 }
509 } 509 }
510 510
511 static char const *test_map_reference_get(char const *key) { 511 static const char *test_map_reference_get(const char *key) {
512 for (size_t i = 0 ; i < test_map_reference_len ; i++) { 512 for (size_t i = 0 ; i < test_map_reference_len ; i++) {
513 if (0 == strcmp(key, test_map_reference[i].key)) { 513 if (0 == strcmp(key, test_map_reference[i].key)) {
514 return test_map_reference[i].value; 514 return test_map_reference[i].value;
515 } 515 }
516 } 516 }
517 return NULL; 517 return NULL;
518 } 518 }
519 519
520 static char const *test_map_reference_remove(char const *key) { 520 static const char *test_map_reference_remove(const char *key) {
521 for (size_t i = 0 ; i < test_map_reference_len ; i++) { 521 for (size_t i = 0 ; i < test_map_reference_len ; i++) {
522 if (0 == strcmp(key, test_map_reference[i].key)) { 522 if (0 == strcmp(key, test_map_reference[i].key)) {
523 char const *ret = test_map_reference[i].value; 523 const char *ret = test_map_reference[i].value;
524 test_map_reference[i].value = NULL; 524 test_map_reference[i].value = NULL;
525 return ret; 525 return ret;
526 } 526 }
527 } 527 }
528 return NULL; 528 return NULL;
567 // by using that the values in our test data are unique strings 567 // by using that the values in our test data are unique strings
568 // we can re-use a similar approach as above 568 // we can re-use a similar approach as above
569 CxIterator valiter = cxMapIteratorValues(map); 569 CxIterator valiter = cxMapIteratorValues(map);
570 CX_TEST_ASSERT(valiter.elem_size == map->collection.elem_size); 570 CX_TEST_ASSERT(valiter.elem_size == map->collection.elem_size);
571 CX_TEST_ASSERT(valiter.elem_count == map->collection.size); 571 CX_TEST_ASSERT(valiter.elem_count == map->collection.size);
572 char const** values = calloc(map->collection.size, sizeof(char const*)); 572 const char ** values = calloc(map->collection.size, sizeof(const char *));
573 cx_foreach(char const*, elem, valiter) { 573 cx_foreach(const char *, elem, valiter) {
574 values[valiter.index] = elem; 574 values[valiter.index] = elem;
575 } 575 }
576 CX_TEST_ASSERT(valiter.index == map->collection.size); 576 CX_TEST_ASSERT(valiter.index == map->collection.size);
577 // verify that all values are present in the reference map 577 // verify that all values are present in the reference map
578 for (size_t i = 0 ; i < map->collection.size ; i++) { 578 for (size_t i = 0 ; i < map->collection.size ; i++) {
593 CxIterator pairiter = cxMapIterator(map); 593 CxIterator pairiter = cxMapIterator(map);
594 CX_TEST_ASSERT(pairiter.elem_size == sizeof(CxMapEntry)); 594 CX_TEST_ASSERT(pairiter.elem_size == sizeof(CxMapEntry));
595 CX_TEST_ASSERT(pairiter.elem_count == map->collection.size); 595 CX_TEST_ASSERT(pairiter.elem_count == map->collection.size);
596 struct test_map_kv *pairs = calloc(map->collection.size, sizeof(struct test_map_kv)); 596 struct test_map_kv *pairs = calloc(map->collection.size, sizeof(struct test_map_kv));
597 cx_foreach(CxMapEntry*, entry, pairiter) { 597 cx_foreach(CxMapEntry*, entry, pairiter) {
598 CxHashKey const *key = entry->key; 598 const CxHashKey *key = entry->key;
599 pairs[pairiter.index].key = cx_strdup(cx_strn(key->data, key->len)).ptr; 599 pairs[pairiter.index].key = cx_strdup(cx_strn(key->data, key->len)).ptr;
600 pairs[pairiter.index].value = entry->value; 600 pairs[pairiter.index].value = entry->value;
601 } 601 }
602 CX_TEST_ASSERT(pairiter.index == map->collection.size); 602 CX_TEST_ASSERT(pairiter.index == map->collection.size);
603 // verify that all pairs are present in the reference map 603 // verify that all pairs are present in the reference map
638 CX_TEST_ASSERT(result == 0); 638 CX_TEST_ASSERT(result == 0);
639 void *added = cxMapGet(map, key); 639 void *added = cxMapGet(map, key);
640 CX_TEST_ASSERT(0 == memcmp(kv.value, added, strlen(kv.value))); 640 CX_TEST_ASSERT(0 == memcmp(kv.value, added, strlen(kv.value)));
641 } else { 641 } else {
642 // execute a remove and verify that the removed element was returned (or NULL) 642 // execute a remove and verify that the removed element was returned (or NULL)
643 char const *found = test_map_reference_remove(kv.key); 643 const char *found = test_map_reference_remove(kv.key);
644 void *removed = cxMapRemoveAndGet(map, key); 644 void *removed = cxMapRemoveAndGet(map, key);
645 CX_TEST_ASSERT(found == removed); 645 CX_TEST_ASSERT(found == removed);
646 } 646 }
647 // compare the current map state with the reference map 647 // compare the current map state with the reference map
648 CX_TEST_CALL_SUBROUTINE(verify_map_contents, map); 648 CX_TEST_CALL_SUBROUTINE(verify_map_contents, map);

mercurial