diff -r d4baf4dd55c3 -r fe0d69d72bcd tests/test_hash_map.c --- a/tests/test_hash_map.c Thu May 23 19:29:14 2024 +0200 +++ b/tests/test_hash_map.c Thu May 23 20:29:28 2024 +0200 @@ -42,19 +42,19 @@ for(size_t i = 0 ; i < hmap->bucket_count ; i++) { CX_TEST_ASSERT(hmap->buckets[i] == NULL); } - CX_TEST_ASSERT(map->item_size == 1); - CX_TEST_ASSERT(map->size == 0); - CX_TEST_ASSERT(map->allocator == allocator); - CX_TEST_ASSERT(!map->store_pointer); - CX_TEST_ASSERT(map->cmpfunc == NULL); - CX_TEST_ASSERT(map->simple_destructor == NULL); - CX_TEST_ASSERT(map->advanced_destructor == NULL); - CX_TEST_ASSERT(map->destructor_data == NULL); + CX_TEST_ASSERT(map->base.item_size == 1); + CX_TEST_ASSERT(map->base.size == 0); + CX_TEST_ASSERT(map->base.allocator == allocator); + CX_TEST_ASSERT(!map->base.store_pointer); + CX_TEST_ASSERT(map->base.cmpfunc == NULL); + CX_TEST_ASSERT(map->base.simple_destructor == NULL); + CX_TEST_ASSERT(map->base.advanced_destructor == NULL); + CX_TEST_ASSERT(map->base.destructor_data == NULL); cxMapStorePointers(map); - CX_TEST_ASSERT(map->store_pointer); - CX_TEST_ASSERT(map->item_size == sizeof(void *)); + CX_TEST_ASSERT(map->base.store_pointer); + CX_TEST_ASSERT(map->base.item_size == sizeof(void *)); cxMapStoreObjects(map); - CX_TEST_ASSERT(!map->store_pointer); + CX_TEST_ASSERT(!map->base.store_pointer); cxMapDestroy(map); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); @@ -73,10 +73,10 @@ for (size_t i = 0; i < hmap->bucket_count; i++) { CX_TEST_ASSERT(hmap->buckets[i] == NULL); } - CX_TEST_ASSERT(map->size == 0); - CX_TEST_ASSERT(map->allocator == allocator); - CX_TEST_ASSERT(map->store_pointer); - CX_TEST_ASSERT(map->item_size == sizeof(void *)); + CX_TEST_ASSERT(map->base.size == 0); + CX_TEST_ASSERT(map->base.allocator == allocator); + CX_TEST_ASSERT(map->base.store_pointer); + CX_TEST_ASSERT(map->base.item_size == sizeof(void *)); cxMapDestroy(map); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); @@ -106,7 +106,7 @@ int result = cxMapRehash(map); CX_TEST_ASSERT(result == 0); CX_TEST_ASSERT(((struct cx_hash_map_s *)map)->bucket_count == 25); - CX_TEST_ASSERT(map->size == 10); + CX_TEST_ASSERT(map->base.size == 10); CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 1"), "val 1")); CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 2"), "val 2")); @@ -161,11 +161,11 @@ cxMapPut(map, "key 2", (void *) "val 2"); cxMapPut(map, "key 3", (void *) "val 3"); - CX_TEST_ASSERT(map->size == 3); + CX_TEST_ASSERT(map->base.size == 3); cxMapClear(map); - CX_TEST_ASSERT(map->size == 0); + CX_TEST_ASSERT(map->base.size == 0); CX_TEST_ASSERT(cxMapGet(map, "key 1") == NULL); CX_TEST_ASSERT(cxMapGet(map, "key 2") == NULL); CX_TEST_ASSERT(cxMapGet(map, "key 3") == NULL); @@ -208,7 +208,7 @@ // remove a string cxMapRemove(map, "s2"); - CX_TEST_ASSERT(map->size == 3); + CX_TEST_ASSERT(map->base.size == 3); // iterate bool s3found = false, s4found = false, s5found = false; @@ -244,8 +244,8 @@ cx_foreach(CxMapEntry*, entry, iter) { if (((char const *)entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter); } - CX_TEST_ASSERT(map->size == 3); - CX_TEST_ASSERT(iter.index == map->size); + CX_TEST_ASSERT(map->base.size == 3); + CX_TEST_ASSERT(iter.index == map->base.size); CX_TEST_ASSERT(cxMapGet(map, "key 1") == NULL); CX_TEST_ASSERT(cxMapGet(map, "key 2") != NULL); @@ -348,7 +348,7 @@ CxAllocator *allocator = &talloc.base; CX_TEST_DO { CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0); - map->simple_destructor = test_simple_destructor; + map->base.simple_destructor = test_simple_destructor; CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } @@ -361,7 +361,7 @@ CxAllocator *allocator = &talloc.base; CX_TEST_DO { CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0); - map->advanced_destructor = test_advanced_destructor; + map->base.advanced_destructor = test_advanced_destructor; CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } @@ -370,7 +370,7 @@ CX_TEST(test_empty_map_size) { CX_TEST_DO { - CX_TEST_ASSERT(cxEmptyMap->size == 0); + CX_TEST_ASSERT(cxEmptyMap->base.size == 0); } } @@ -430,7 +430,7 @@ cxMapPut(map, cx_mutstr("foo"), "bar"); cxMapPut(map, cx_str("hallo"), "welt"); - CX_TEST_ASSERT(map->size == 3); + CX_TEST_ASSERT(map->base.size == 3); CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "test"), "test")); CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar")); CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "hallo"), "welt")); @@ -441,16 +441,16 @@ cxMapDetach(map, hallo); cxMapPut(map, cx_hash_key_str("key"), "value"); - CX_TEST_ASSERT(map->size == 2); + CX_TEST_ASSERT(map->base.size == 2); CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key"), "value")); CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo"), "bar")); void *r; r = cxMapRemoveAndGet(map, "key"); r = cxMapRemoveAndGet(map, cx_str("foo")); - if (r != NULL) map->size = 47; + if (r != NULL) map->base.size = 47; - CX_TEST_ASSERT(map->size == 0); + CX_TEST_ASSERT(map->base.size == 0); cxMapDestroy(map); CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); @@ -539,21 +539,21 @@ static CX_TEST_SUBROUTINE(verify_map_contents, CxMap *map) { // verify that the reference map has same size (i.e. no other keys are mapped) - CX_TEST_ASSERT(map->size == test_map_reference_size()); + CX_TEST_ASSERT(map->base.size == test_map_reference_size()); // verify key iterator { // collect the keys from the map iterator CxIterator keyiter = cxMapIteratorKeys(map); CX_TEST_ASSERT(keyiter.elem_size == sizeof(CxHashKey)); - CX_TEST_ASSERT(keyiter.elem_count == map->size); - CxHashKey *keys = calloc(map->size, sizeof(CxHashKey)); + CX_TEST_ASSERT(keyiter.elem_count == map->base.size); + CxHashKey *keys = calloc(map->base.size, sizeof(CxHashKey)); cx_foreach(CxHashKey*, elem, keyiter) { keys[keyiter.index] = *elem; } - CX_TEST_ASSERT(keyiter.index == map->size); + CX_TEST_ASSERT(keyiter.index == map->base.size); // verify that all keys are mapped to values in reference map - for (size_t i = 0 ; i < map->size ; i++) { + for (size_t i = 0 ; i < map->base.size ; i++) { cxmutstr ksz = cx_strdup(cx_strn(keys[i].data, keys[i].len)); CX_TEST_ASSERT(test_map_reference_get(ksz.ptr) != NULL); cx_strfree(&ksz); @@ -566,15 +566,15 @@ // by using that the values in our test data are unique strings // we can re-use a similar approach as above CxIterator valiter = cxMapIteratorValues(map); - CX_TEST_ASSERT(valiter.elem_size == map->item_size); - CX_TEST_ASSERT(valiter.elem_count == map->size); - char const** values = calloc(map->size, sizeof(char const*)); + CX_TEST_ASSERT(valiter.elem_size == map->base.item_size); + CX_TEST_ASSERT(valiter.elem_count == map->base.size); + char const** values = calloc(map->base.size, sizeof(char const*)); cx_foreach(char const*, elem, valiter) { values[valiter.index] = elem; } - CX_TEST_ASSERT(valiter.index == map->size); + CX_TEST_ASSERT(valiter.index == map->base.size); // verify that all values are present in the reference map - for (size_t i = 0 ; i < map->size ; i++) { + for (size_t i = 0 ; i < map->base.size ; i++) { bool found = false; for (size_t j = 0; j < test_map_reference_len ; j++) { if (test_map_reference[j].value == values[i]) { @@ -591,16 +591,16 @@ { CxIterator pairiter = cxMapIterator(map); CX_TEST_ASSERT(pairiter.elem_size == sizeof(CxMapEntry)); - CX_TEST_ASSERT(pairiter.elem_count == map->size); - struct test_map_kv *pairs = calloc(map->size, sizeof(struct test_map_kv)); + CX_TEST_ASSERT(pairiter.elem_count == map->base.size); + struct test_map_kv *pairs = calloc(map->base.size, sizeof(struct test_map_kv)); cx_foreach(CxMapEntry*, entry, pairiter) { CxHashKey const *key = entry->key; pairs[pairiter.index].key = cx_strdup(cx_strn(key->data, key->len)).ptr; pairs[pairiter.index].value = entry->value; } - CX_TEST_ASSERT(pairiter.index == map->size); + CX_TEST_ASSERT(pairiter.index == map->base.size); // verify that all pairs are present in the reference map - for (size_t i = 0 ; i < map->size ; i++) { + for (size_t i = 0 ; i < map->base.size ; i++) { CX_TEST_ASSERT(test_map_reference_get(pairs[i].key) == pairs[i].value); // this was strdup'ed free((void*)pairs[i].key);