Sat, 30 Dec 2023 18:48:25 +0100
migrate map tests - relates to #342
tests/Makefile | file | annotate | diff | comparison | revisions | |
tests/test_hash_map.c | file | annotate | diff | comparison | revisions | |
tests/test_map.cpp | file | annotate | diff | comparison | revisions | |
tests/test_map_generics.c | file | annotate | diff | comparison | revisions | |
tests/test_map_generics.h | file | annotate | diff | comparison | revisions | |
tests/test_mempool.c | file | annotate | diff | comparison | revisions | |
tests/ucxtest.c | file | annotate | diff | comparison | revisions |
--- a/tests/Makefile Sat Dec 30 15:21:16 2023 +0100 +++ b/tests/Makefile Sat Dec 30 18:48:25 2023 +0100 @@ -28,7 +28,7 @@ TEST_DIR=$(build_dir)/tests SRC = util_allocator.c test_utils.c test_hash_key.c test_allocator.c \ - test_string.c test_printf.c test_mempool.c ucxtest.c + test_string.c test_printf.c test_mempool.c test_hash_map.c ucxtest.c OBJ_EXT=.o OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT)) @@ -57,11 +57,11 @@ @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(TEST_DIR)/test_map_generics$(OBJ_EXT): test_map_generics.c \ - test_map_generics.h ../src/cx/map.h ../src/cx/common.h \ - ../src/cx/collection.h ../src/cx/allocator.h ../src/cx/iterator.h \ - ../src/cx/string.h ../src/cx/hash_key.h ../src/cx/hash_map.h \ - ../src/cx/map.h +$(TEST_DIR)/test_hash_map$(OBJ_EXT): test_hash_map.c ../src/cx/test.h \ + util_allocator.h ../src/cx/allocator.h ../src/cx/common.h \ + ../src/cx/hash_map.h ../src/cx/map.h ../src/cx/collection.h \ + ../src/cx/allocator.h ../src/cx/iterator.h ../src/cx/string.h \ + ../src/cx/hash_key.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $<
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_hash_map.c Sat Dec 30 18:48:25 2023 +0100 @@ -0,0 +1,669 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "cx/test.h" +#include "util_allocator.h" + +#include "cx/hash_map.h" + +CX_TEST(test_hash_map_create) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + CxMap *map = cxHashMapCreate(allocator, 1, 0); + struct cx_hash_map_s *hmap = (struct cx_hash_map_s *) map; + CX_TEST_ASSERT(hmap->bucket_count > 0); + 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); + cxMapStorePointers(map); + CX_TEST_ASSERT(map->store_pointer); + CX_TEST_ASSERT(map->item_size == sizeof(void *)); + cxMapStoreObjects(map); + CX_TEST_ASSERT(!map->store_pointer); + + cxMapDestroy(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_hash_map_create_store_pointers) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0); + struct cx_hash_map_s *hmap = (struct cx_hash_map_s *) map; + CX_TEST_ASSERT(hmap->bucket_count > 0); + 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 *)); + + cxMapDestroy(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_hash_map_rehash) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 7); + + cxMapPut(map, "key 1", (void *) "val 1"); + cxMapPut(map, "key 2", (void *) "val 2"); + cxMapPut(map, "key 3", (void *) "val 3"); + cxMapPut(map, "foo 4", (void *) "val 4"); + cxMapPut(map, "key 5", (void *) "val 5"); + cxMapPut(map, "key 6", (void *) "val 6"); + cxMapPut(map, "bar 7", (void *) "val 7"); + cxMapPut(map, "key 8", (void *) "val 8"); + cxMapPut(map, "key 9", (void *) "val 9"); + cxMapPut(map, "key 10", (void *) "val 10"); + + CX_TEST_ASSERT(((struct cx_hash_map_s *)map)->bucket_count == 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(0 == strcmp(cxMapGet(map, "key 1"), "val 1")); + CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 2"), "val 2")); + CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 3"), "val 3")); + CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "foo 4"), "val 4")); + CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 5"), "val 5")); + CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 6"), "val 6")); + CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "bar 7"), "val 7")); + CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 8"), "val 8")); + CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 9"), "val 9")); + CX_TEST_ASSERT(0 == strcmp(cxMapGet(map, "key 10"), "val 10")); + + cxMapDestroy(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_hash_map_rehash_not_required) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 8); + + cxMapPut(map, "key 1", (void *) "val 1"); + cxMapPut(map, "key 2", (void *) "val 2"); + cxMapPut(map, "key 3", (void *) "val 3"); + cxMapPut(map, "key 4", (void *) "val 4"); + cxMapPut(map, "key 5", (void *) "val 5"); + cxMapPut(map, "key 6", (void *) "val 6"); + + // 6/8 does not exceed 0.75, therefore the function should not rehash + int result = cxMapRehash(map); + CX_TEST_ASSERT(result == 0); + CX_TEST_ASSERT(((struct cx_hash_map_s *)map)->bucket_count == 8); + + cxMapDestroy(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_hash_map_clear) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0); + + cxMapPut(map, "key 1", (void *) "val 1"); + cxMapPut(map, "key 2", (void *) "val 2"); + cxMapPut(map, "key 3", (void *) "val 3"); + + CX_TEST_ASSERT(map->size == 3); + + cxMapClear(map); + + CX_TEST_ASSERT(map->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); + + cxMapDestroy(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_hash_map_store_ucx_strings) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + // create the map + CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 8); + + // define some strings + cxstring s1 = CX_STR("this"); + cxstring s2 = CX_STR("is"); + cxstring s3 = CX_STR("a"); + cxstring s4 = CX_STR("test"); + cxstring s5 = CX_STR("setup"); + + // put them into the map + cxMapPut(map, "s1", &s1); + cxMapPut(map, "s2", &s2); + cxMapPut(map, "s3", &s3); + cxMapPut(map, "s4", &s4); + + // overwrite a value + cxMapPut(map, "s1", &s5); + + // look up a string + cxstring * s3p = cxMapGet(map, "s3"); + CX_TEST_ASSERT(s3p->length == s3.length); + CX_TEST_ASSERT(s3p->ptr == s3.ptr); + CX_TEST_ASSERT(s3p != &s3); + + // remove a string + cxMapRemove(map, "s2"); + CX_TEST_ASSERT(map->size == 3); + + // iterate + bool s3found = false, s4found = false, s5found = false; + CxIterator iter = cxMapIteratorValues(map); + cx_foreach(cxstring*, s, iter) { + s3found |= s3.ptr == s->ptr; + s4found |= s4.ptr == s->ptr; + s5found |= s5.ptr == s->ptr; + } + CX_TEST_ASSERT(s3found && s4found && s5found); + + cxMapDestroy(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_hash_map_remove_via_iterator) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 4); + + cxMapPut(map, "key 1", (void *) "val 1"); + cxMapPut(map, "key 2", (void *) "val 2"); + cxMapPut(map, "key 3", (void *) "val 3"); + cxMapPut(map, "key 4", (void *) "val 4"); + cxMapPut(map, "key 5", (void *) "val 5"); + cxMapPut(map, "key 6", (void *) "val 6"); + + CxMutIterator iter = cxMapMutIterator(map); + 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(cxMapGet(map, "key 1") == NULL); + CX_TEST_ASSERT(cxMapGet(map, "key 2") != NULL); + CX_TEST_ASSERT(cxMapGet(map, "key 3") == NULL); + CX_TEST_ASSERT(cxMapGet(map, "key 4") != NULL); + CX_TEST_ASSERT(cxMapGet(map, "key 5") == NULL); + CX_TEST_ASSERT(cxMapGet(map, "key 6") != NULL); + + cxMapDestroy(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +static void test_simple_destructor(void *data) { + strcpy(data, "OK"); +} + +static void test_advanced_destructor( + __attribute__((__unused__)) void *unused, + void *data +) { + strcpy(data, "OK"); +} + +static CX_TEST_SUBROUTINE(verify_any_destructor, CxMap *map) { + CxHashKey k1 = cx_hash_key_str("key 1"); + CxHashKey k2 = cx_hash_key_str("key 2"); + CxHashKey k3 = cx_hash_key_str("key 3"); + CxHashKey k4 = cx_hash_key_str("key 4"); + CxHashKey k5 = cx_hash_key_str("key 5"); + + char v1[] = "val 1"; + char v2[] = "val 2"; + char v3[] = "val 3"; + char v4[] = "val 4"; + char v5[] = "val 5"; + + cxMapPut(map, k1, v1); + cxMapPut(map, k2, v2); + cxMapPut(map, k3, v3); + cxMapPut(map, k4, v4); + + cxMapRemove(map, k2); + char *r = cxMapRemoveAndGet(map, k3); + cxMapDetach(map, k1); + + CX_TEST_ASSERT(0 == strcmp(v1, "val 1")); + CX_TEST_ASSERT(0 == strcmp(v2, "OK")); + CX_TEST_ASSERT(0 == strcmp(v3, "val 3")); + CX_TEST_ASSERT(0 == strcmp(v4, "val 4")); + CX_TEST_ASSERT(0 == strcmp(v5, "val 5")); + CX_TEST_ASSERT(r == v3); + + cxMapClear(map); + + CX_TEST_ASSERT(0 == strcmp(v1, "val 1")); + CX_TEST_ASSERT(0 == strcmp(v2, "OK")); + CX_TEST_ASSERT(0 == strcmp(v3, "val 3")); + CX_TEST_ASSERT(0 == strcmp(v4, "OK")); + CX_TEST_ASSERT(0 == strcmp(v5, "val 5")); + + cxMapPut(map, k1, (void *) v1); + cxMapPut(map, k3, (void *) v3); + cxMapPut(map, k5, (void *) v5); + + { + CxMutIterator iter = cxMapMutIteratorKeys(map); + cx_foreach(CxHashKey*, key, iter) { + if (((char*)key->data)[4] == '1') cxIteratorFlagRemoval(iter); + } + } + { + CxMutIterator iter = cxMapMutIteratorValues(map); + cx_foreach(char*, v, iter) { + if (v[4] == '5') cxIteratorFlagRemoval(iter); + } + } + + CX_TEST_ASSERT(0 == strcmp(v1, "OK")); + CX_TEST_ASSERT(0 == strcmp(v2, "OK")); + CX_TEST_ASSERT(0 == strcmp(v3, "val 3")); + CX_TEST_ASSERT(0 == strcmp(v4, "OK")); + CX_TEST_ASSERT(0 == strcmp(v5, "OK")); + + v1[0] = v2[0] = v4[0] = v5[0] = 'c'; + + cxMapDestroy(map); + + CX_TEST_ASSERT(0 == strcmp(v1, "cK")); + CX_TEST_ASSERT(0 == strcmp(v2, "cK")); + CX_TEST_ASSERT(0 == strcmp(v3, "OK")); + CX_TEST_ASSERT(0 == strcmp(v4, "cK")); + CX_TEST_ASSERT(0 == strcmp(v5, "cK")); +} + +CX_TEST(test_hash_map_simple_destructor) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0); + map->simple_destructor = test_simple_destructor; + CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_hash_map_advanced_destructor) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 0); + map->advanced_destructor = test_advanced_destructor; + CX_TEST_CALL_SUBROUTINE(verify_any_destructor, map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_empty_map_size) { + CX_TEST_DO { + CX_TEST_ASSERT(cxEmptyMap->size == 0); + } +} + +CX_TEST(test_empty_map_iterator) { + CxMap *map = cxEmptyMap; + + CxIterator it1 = cxMapIterator(map); + CxIterator it2 = cxMapIteratorValues(map); + CxIterator it3 = cxMapIteratorKeys(map); + CxMutIterator it4 = cxMapMutIterator(map); + CxMutIterator it5 = cxMapMutIteratorValues(map); + CxMutIterator it6 = cxMapMutIteratorKeys(map); + + CX_TEST_DO { + CX_TEST_ASSERT(!cxIteratorValid(it1)); + CX_TEST_ASSERT(!cxIteratorValid(it2)); + CX_TEST_ASSERT(!cxIteratorValid(it3)); + CX_TEST_ASSERT(!cxIteratorValid(it4)); + CX_TEST_ASSERT(!cxIteratorValid(it5)); + CX_TEST_ASSERT(!cxIteratorValid(it6)); + + int c = 0; + cx_foreach(void*, data, it1) c++; + cx_foreach(void*, data, it2) c++; + cx_foreach(void*, data, it3) c++; + cx_foreach(void*, data, it4) c++; + cx_foreach(void*, data, it5) c++; + cx_foreach(void*, data, it6) c++; + CX_TEST_ASSERT(c == 0); + } +} + +CX_TEST(test_empty_map_no_ops) { + CX_TEST_DO { + // assertion not possible + // test that no segfault happens and valgrind is happy + cxMapClear(cxEmptyMap); + cxMapDestroy(cxEmptyMap); + CX_TEST_ASSERT(true); + } +} + +CX_TEST(test_empty_map_get) { + CX_TEST_DO { + CxHashKey key = cx_hash_key_str("test"); + CX_TEST_ASSERT(cxMapGet(cxEmptyMap, key) == NULL); + } +} + +CX_TEST(test_hash_map_generics) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 0); + cxMapPut(map, "test", "test"); + cxMapPut(map, cx_mutstr("foo"), "bar"); + cxMapPut(map, cx_str("hallo"), "welt"); + + CX_TEST_ASSERT(map->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")); + + // note: we don't have a destructor here, so remove and detach are the same + cxMapRemove(map, cx_str("test")); + char const *hallo = "hallo"; + cxMapDetach(map, hallo); + cxMapPut(map, cx_hash_key_str("key"), "value"); + + CX_TEST_ASSERT(map->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; + + CX_TEST_ASSERT(map->size == 0); + + cxMapDestroy(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +struct test_map_kv { + char const *key; + char const *value; +}; + +static struct test_map_kv const test_map_operations[] = { + {"key 1", "test"}, + {"key 2", "blub"}, + {"key 3", "hallo"}, + {"key 2", "foobar"}, + {"key 4", "value 4"}, + {"key 5", "value 5"}, + {"key 6", "value 6"}, + {"key 4", NULL}, + {"key 7", "value 7"}, + {"key 8", "value 8"}, + {"does not exist", NULL}, + {"key 9", "value 9"}, + {"key 6", "other value"}, + {"key 7", "something else"}, + {"key 8", NULL}, + {"key 2", NULL}, + {"key 8", "new value"}, +}; +static size_t const test_map_operations_len = + sizeof(test_map_operations) / sizeof(struct test_map_kv); +static struct test_map_kv test_map_reference[] = { + {"key 1", NULL}, + {"key 2", NULL}, + {"key 3", NULL}, + {"key 4", NULL}, + {"key 5", NULL}, + {"key 6", NULL}, + {"key 7", NULL}, + {"key 8", NULL}, + {"key 9", NULL}, +}; +static size_t const test_map_reference_len = + sizeof(test_map_reference) / sizeof(struct test_map_kv); + +static void test_map_reference_put(char const *key, char const* value) { + for (size_t i = 0 ; i < test_map_reference_len ; i++) { + if (0 == strcmp(key, test_map_reference[i].key)) { + test_map_reference[i].value = value; + return; + } + } +} + +static char const *test_map_reference_get(char const *key) { + for (size_t i = 0 ; i < test_map_reference_len ; i++) { + if (0 == strcmp(key, test_map_reference[i].key)) { + return test_map_reference[i].value; + } + } + return NULL; +} + +static char const *test_map_reference_remove(char const *key) { + for (size_t i = 0 ; i < test_map_reference_len ; i++) { + if (0 == strcmp(key, test_map_reference[i].key)) { + char const *ret = test_map_reference[i].value; + test_map_reference[i].value = NULL; + return ret; + } + } + return NULL; +} + +static size_t test_map_reference_size(void) { + size_t size = 0; + for (size_t i = 0; i < test_map_reference_len; i++) { + if (test_map_reference[i].value != NULL) { + size++; + } + } + return size; +} + +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()); + + // verify key iterator + { + // collect the keys from the map iterator + CxIterator keyiter = cxMapIteratorKeys(map); + CxHashKey *keys = calloc(map->size, sizeof(CxHashKey)); + cx_foreach(CxHashKey*, elem, keyiter) { + keys[keyiter.index] = *elem; + } + CX_TEST_ASSERT(keyiter.index == map->size); + // verify that all keys are mapped to values in reference map + for (size_t i = 0 ; i < map->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); + } + free(keys); + } + + // verify value iterator + { + // 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); + char const** values = calloc(map->size, sizeof(char const*)); + cx_foreach(char const*, elem, valiter) { + values[valiter.index] = elem; + } + CX_TEST_ASSERT(valiter.index == map->size); + // verify that all values are present in the reference map + for (size_t i = 0 ; i < map->size ; i++) { + bool found = false; + for (size_t j = 0; j < test_map_reference_len ; j++) { + if (test_map_reference[j].value == values[i]) { + found = true; + break; + } + } + CX_TEST_ASSERTM(found, "A value was not found in the reference map"); + } + free(values); + } + + // verify pair iterator + { + CxIterator pairiter = cxMapIterator(map); + struct test_map_kv *pairs = calloc(map->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); + // verify that all pairs are present in the reference map + for (size_t i = 0 ; i < map->size ; i++) { + CX_TEST_ASSERT(test_map_reference_get(pairs[i].key) == pairs[i].value); + // this was strdup'ed + free((void*)pairs[i].key); + } + free(pairs); + } +} + +CX_TEST(test_hash_map_basic_operations) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + CX_TEST_DO { + // create the map + CxMap *map = cxHashMapCreate(allocator, CX_STORE_POINTERS, 8); + + // clear the reference map + for (size_t i = 0 ; i < test_map_reference_len ; i++) { + test_map_reference[i].value = NULL; + } + + // verify iterators for empty map + CX_TEST_CALL_SUBROUTINE(verify_map_contents, map); + + // execute operations and verify results + for (size_t i = 0 ; i < test_map_operations_len ; i++) { + struct test_map_kv kv = test_map_operations[i]; + CxHashKey key = cx_hash_key_str(kv.key); + key.hash = 0; // force the hash map to compute the hash + if (kv.value != NULL) { + // execute a put operation and verify that the exact value can be read back + test_map_reference_put(kv.key, kv.value); + int result = cxMapPut(map, key, (void *) kv.value); + CX_TEST_ASSERT(result == 0); + void *added = cxMapGet(map, key); + CX_TEST_ASSERT(0 == memcmp(kv.value, added, strlen(kv.value))); + } else { + // execute a remove and verify that the removed element was returned (or NULL) + char const *found = test_map_reference_remove(kv.key); + void *removed = cxMapRemoveAndGet(map, key); + CX_TEST_ASSERT(found == removed); + } + // compare the current map state with the reference map + CX_TEST_CALL_SUBROUTINE(verify_map_contents, map); + } + + // destroy the map and verify the memory (de)allocations + cxMapDestroy(map); + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); +} + +CxTestSuite *cx_test_suite_hash_map(void) { + CxTestSuite *suite = cx_test_suite_new("map"); + + cx_test_register(suite, test_hash_map_create); + cx_test_register(suite, test_hash_map_create_store_pointers); + cx_test_register(suite, test_hash_map_basic_operations); + cx_test_register(suite, test_hash_map_rehash); + cx_test_register(suite, test_hash_map_rehash_not_required); + cx_test_register(suite, test_hash_map_clear); + cx_test_register(suite, test_hash_map_store_ucx_strings); + cx_test_register(suite, test_hash_map_remove_via_iterator); + cx_test_register(suite, test_empty_map_no_ops); + cx_test_register(suite, test_empty_map_size); + cx_test_register(suite, test_empty_map_get); + cx_test_register(suite, test_empty_map_iterator); + cx_test_register(suite, test_hash_map_generics); + + return suite; +}
--- a/tests/test_map.cpp Sat Dec 30 15:21:16 2023 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,521 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "cx/hash_map.h" -#include "cx/utils.h" -#include "cx/string.h" -#include "util_allocator.h" -#include "test_map_generics.h" - -#include <gtest/gtest.h> -#include <unordered_map> -#include <unordered_set> - -struct map_operation { - enum { - put, rm - } op; - char const *key; - char const *value; -}; - -auto generate_map_operations() -> std::vector<map_operation> { - return { - {map_operation::put, "key 1", "test"}, - {map_operation::put, "key 2", "blub"}, - {map_operation::put, "key 3", "hallo"}, - {map_operation::put, "key 2", "foobar"}, - {map_operation::put, "key 4", "value 4"}, - {map_operation::put, "key 5", "value 5"}, - {map_operation::put, "key 6", "value 6"}, - {map_operation::rm, "key 4", nullptr}, - {map_operation::put, "key 7", "value 7"}, - {map_operation::put, "key 8", "value 8"}, - {map_operation::rm, "does not exist", nullptr}, - {map_operation::put, "key 9", "value 9"}, - {map_operation::put, "key 6", "other value"}, - {map_operation::put, "key 7", "something else"}, - {map_operation::rm, "key 8", nullptr}, - {map_operation::rm, "key 2", nullptr}, - {map_operation::put, "key 8", "new value"}, - }; -} - -static void verify_map_contents( - CxMap *map, - std::unordered_map<std::string, std::string> const &refmap -) { - // verify key iterator - { - auto keyiter = cxMapIteratorKeys(map); - std::unordered_set<std::string> keys; - cx_foreach(CxHashKey*, elem, keyiter) { - keys.insert(std::string(reinterpret_cast<char const *>(elem->data), elem->len)); - } - EXPECT_EQ(keyiter.index, map->size); - ASSERT_EQ(keys.size(), map->size); - for (auto &&k: keys) { - EXPECT_NE(refmap.find(k), refmap.end()); - } - } - - // verify value iterator - { - auto valiter = cxMapIteratorValues(map); - std::unordered_set<std::string> values; // we use that the values in our test data are unique strings - cx_foreach(char const*, elem, valiter) { - values.insert(std::string(elem)); - } - EXPECT_EQ(valiter.index, map->size); - ASSERT_EQ(values.size(), map->size); - for (auto &&v: values) { - EXPECT_NE(std::find_if(refmap.begin(), refmap.end(), - [v](auto const &e) { return e.second == v; }), refmap.end()); - } - } - - // verify pair iterator - { - auto pairiter = cxMapIterator(map); - std::unordered_map<std::string, std::string> pairs; - cx_foreach(CxMapEntry*, entry, pairiter) { - pairs[std::string(reinterpret_cast<char const *>(entry->key->data), entry->key->len)] = std::string( - (char *) entry->value); - } - EXPECT_EQ(pairiter.index, map->size); - ASSERT_EQ(pairs.size(), refmap.size()); - for (auto &&p: pairs) { - ASSERT_EQ(p.second, refmap.at(p.first)); - } - } -} - -TEST(CxHashMap, Create) { - CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, 1, 0); - auto hmap = reinterpret_cast<struct cx_hash_map_s *>(map); - EXPECT_GT(hmap->bucket_count, 0); - cx_for_n(i, hmap->bucket_count) { - EXPECT_EQ(hmap->buckets[i], nullptr); - } - EXPECT_EQ(map->item_size, 1); - EXPECT_EQ(map->size, 0); - EXPECT_EQ(map->allocator, &allocator); - EXPECT_FALSE(map->store_pointer); - EXPECT_EQ(map->cmpfunc, nullptr); - EXPECT_EQ(map->simple_destructor, nullptr); - EXPECT_EQ(map->advanced_destructor, nullptr); - EXPECT_EQ(map->destructor_data, nullptr); - cxMapStorePointers(map); - EXPECT_TRUE(map->store_pointer); - EXPECT_EQ(map->item_size, sizeof(void *)); - cxMapStoreObjects(map); - EXPECT_FALSE(map->store_pointer); - - cxMapDestroy(map); - EXPECT_TRUE(allocator.verify()); -} - -TEST(CxHashMap, CreateForStoringPointers) { - CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); - auto hmap = reinterpret_cast<struct cx_hash_map_s *>(map); - EXPECT_GT(hmap->bucket_count, 0); - cx_for_n(i, hmap->bucket_count) { - EXPECT_EQ(hmap->buckets[i], nullptr); - } - EXPECT_EQ(map->size, 0); - EXPECT_EQ(map->allocator, &allocator); - EXPECT_TRUE(map->store_pointer); - EXPECT_EQ(map->item_size, sizeof(void *)); - - cxMapDestroy(map); - EXPECT_TRUE(allocator.verify()); -} - -TEST(CxHashMap, BasicOperations) { - // create the map - CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); - - // create a reference map - std::unordered_map<std::string, std::string> refmap; - - // generate operations - auto ops = generate_map_operations(); - - // verify iterators for empty map - verify_map_contents(map, refmap); - - // execute operations and verify results - for (auto &&op: ops) { - CxHashKey key = cx_hash_key_str(op.key); - key.hash = 0; // force the hash map to compute the hash - if (op.op == map_operation::put) { - // execute a put operation and verify that the exact value can be read back - refmap[std::string(op.key)] = std::string(op.value); - int result = cxMapPut(map, key, (void *) op.value); - EXPECT_EQ(result, 0); - auto added = cxMapGet(map, key); - EXPECT_EQ(memcmp(op.value, added, strlen(op.value)), 0); - } else { - // execute a remove and verify that the removed element was returned (or nullptr) - auto found = refmap.find(op.key); - auto removed = cxMapRemoveAndGet(map, key); - if (found == refmap.end()) { - EXPECT_EQ(removed, nullptr); - } else { - EXPECT_EQ(std::string((char *) removed), found->second); - refmap.erase(found); - } - } - // compare the current map state with the reference map - verify_map_contents(map, refmap); - } - - // destroy the map and verify the memory (de)allocations - cxMapDestroy(map); - EXPECT_TRUE(allocator.verify()); -} - -TEST(CxHashMap, RemoveViaIterator) { - CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 4); - - cxMapPut(map, "key 1", (void *) "val 1"); - cxMapPut(map, "key 2", (void *) "val 2"); - cxMapPut(map, "key 3", (void *) "val 3"); - cxMapPut(map, "key 4", (void *) "val 4"); - cxMapPut(map, "key 5", (void *) "val 5"); - cxMapPut(map, "key 6", (void *) "val 6"); - - auto iter = cxMapMutIterator(map); - cx_foreach(CxMapEntry*, entry, iter) { - if (reinterpret_cast<char const *>(entry->key->data)[4] % 2 == 1) cxIteratorFlagRemoval(iter); - } - EXPECT_EQ(map->size, 3); - EXPECT_EQ(iter.index, map->size); - - EXPECT_EQ(cxMapGet(map, "key 1"), nullptr); - EXPECT_NE(cxMapGet(map, "key 2"), nullptr); - EXPECT_EQ(cxMapGet(map, "key 3"), nullptr); - EXPECT_NE(cxMapGet(map, "key 4"), nullptr); - EXPECT_EQ(cxMapGet(map, "key 5"), nullptr); - EXPECT_NE(cxMapGet(map, "key 6"), nullptr); - - cxMapDestroy(map); - EXPECT_TRUE(allocator.verify()); -} - -TEST(CxHashMap, RehashNotRequired) { - CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 8); - - cxMapPut(map, "key 1", (void *) "val 1"); - cxMapPut(map, "key 2", (void *) "val 2"); - cxMapPut(map, "key 3", (void *) "val 3"); - cxMapPut(map, "key 4", (void *) "val 4"); - cxMapPut(map, "key 5", (void *) "val 5"); - cxMapPut(map, "key 6", (void *) "val 6"); - - // 6/8 does not exceed 0.75, therefore the function should not rehash - int result = cxMapRehash(map); - EXPECT_EQ(result, 0); - EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 8); - - cxMapDestroy(map); - EXPECT_TRUE(allocator.verify()); -} - -TEST(CxHashMap, Rehash) { - CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 7); - - cxMapPut(map, "key 1", (void *) "val 1"); - cxMapPut(map, "key 2", (void *) "val 2"); - cxMapPut(map, "key 3", (void *) "val 3"); - cxMapPut(map, "foo 4", (void *) "val 4"); - cxMapPut(map, "key 5", (void *) "val 5"); - cxMapPut(map, "key 6", (void *) "val 6"); - cxMapPut(map, "bar 7", (void *) "val 7"); - cxMapPut(map, "key 8", (void *) "val 8"); - cxMapPut(map, "key 9", (void *) "val 9"); - cxMapPut(map, "key 10", (void *) "val 10"); - - int result = cxMapRehash(map); - EXPECT_EQ(result, 0); - EXPECT_EQ(reinterpret_cast<struct cx_hash_map_s *>(map)->bucket_count, 25); - EXPECT_EQ(map->size, 10); - - EXPECT_STREQ((char *) cxMapGet(map, "key 1"), "val 1"); - EXPECT_STREQ((char *) cxMapGet(map, "key 2"), "val 2"); - EXPECT_STREQ((char *) cxMapGet(map, "key 3"), "val 3"); - EXPECT_STREQ((char *) cxMapGet(map, "foo 4"), "val 4"); - EXPECT_STREQ((char *) cxMapGet(map, "key 5"), "val 5"); - EXPECT_STREQ((char *) cxMapGet(map, "key 6"), "val 6"); - EXPECT_STREQ((char *) cxMapGet(map, "bar 7"), "val 7"); - EXPECT_STREQ((char *) cxMapGet(map, "key 8"), "val 8"); - EXPECT_STREQ((char *) cxMapGet(map, "key 9"), "val 9"); - EXPECT_STREQ((char *) cxMapGet(map, "key 10"), "val 10"); - - cxMapDestroy(map); - EXPECT_TRUE(allocator.verify()); -} - -TEST(CxHashMap, Clear) { - CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); - - cxMapPut(map, "key 1", (void *) "val 1"); - cxMapPut(map, "key 2", (void *) "val 2"); - cxMapPut(map, "key 3", (void *) "val 3"); - - EXPECT_EQ(map->size, 3); - - cxMapClear(map); - - EXPECT_EQ(map->size, 0); - EXPECT_EQ(cxMapGet(map, "key 1"), nullptr); - EXPECT_EQ(cxMapGet(map, "key 2"), nullptr); - EXPECT_EQ(cxMapGet(map, "key 3"), nullptr); - - cxMapDestroy(map); - EXPECT_TRUE(allocator.verify()); -} - -TEST(CxHashMap, StoreUcxStrings) { - // create the map - CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, sizeof(cxstring), 8); - - // define some strings - auto s1 = CX_STR("this"); - auto s2 = CX_STR("is"); - auto s3 = CX_STR("a"); - auto s4 = CX_STR("test"); - auto s5 = CX_STR("setup"); - - // put them into the map - cxMapPut(map, "s1", &s1); - cxMapPut(map, "s2", &s2); - cxMapPut(map, "s3", &s3); - cxMapPut(map, "s4", &s4); - - // overwrite a value - cxMapPut(map, "s1", &s5); - - // look up a string - auto s3p = reinterpret_cast<cxstring *>(cxMapGet(map, "s3")); - EXPECT_EQ(s3p->length, s3.length); - EXPECT_EQ(s3p->ptr, s3.ptr); - EXPECT_NE(s3p, &s3); - - // remove a string - cxMapRemove(map, "s2"); - - // iterate - auto ref = std::vector{s5.ptr, s3.ptr, s4.ptr}; - auto iter = cxMapIteratorValues(map); - cx_foreach(cxstring*, s, iter) { - auto found = std::find(ref.begin(), ref.end(), s->ptr); - ASSERT_NE(found, ref.end()); - ref.erase(found); - } - EXPECT_EQ(ref.size(), 0); - - cxMapDestroy(map); - EXPECT_TRUE(allocator.verify()); -} - -static void test_simple_destructor(void *data) { - strcpy((char *) data, "OK"); -} - -static void test_advanced_destructor( - [[maybe_unused]] void *unused, - void *data -) { - strcpy((char *) data, "OK"); -} - -static void verify_any_destructor(CxMap *map) { - auto k1 = cx_hash_key_str("key 1"); - auto k2 = cx_hash_key_str("key 2"); - auto k3 = cx_hash_key_str("key 3"); - auto k4 = cx_hash_key_str("key 4"); - auto k5 = cx_hash_key_str("key 5"); - - char v1[] = "val 1"; - char v2[] = "val 2"; - char v3[] = "val 3"; - char v4[] = "val 4"; - char v5[] = "val 5"; - - cxMapPut(map, k1, (void *) v1); - cxMapPut(map, k2, (void *) v2); - cxMapPut(map, k3, (void *) v3); - cxMapPut(map, k4, (void *) v4); - - cxMapRemove(map, k2); - auto r = cxMapRemoveAndGet(map, k3); - cxMapDetach(map, k1); - - EXPECT_STREQ(v1, "val 1"); - EXPECT_STREQ(v2, "OK"); - EXPECT_STREQ(v3, "val 3"); - EXPECT_STREQ(v4, "val 4"); - EXPECT_STREQ(v5, "val 5"); - EXPECT_EQ(r, v3); - - cxMapClear(map); - - EXPECT_STREQ(v1, "val 1"); - EXPECT_STREQ(v2, "OK"); - EXPECT_STREQ(v3, "val 3"); - EXPECT_STREQ(v4, "OK"); - EXPECT_STREQ(v5, "val 5"); - - cxMapPut(map, k1, (void *) v1); - cxMapPut(map, k3, (void *) v3); - cxMapPut(map, k5, (void *) v5); - - { - auto iter = cxMapMutIteratorKeys(map); - cx_foreach(CxHashKey*, key, iter) { - if (reinterpret_cast<char const *>(key->data)[4] == '1') cxIteratorFlagRemoval(iter); - } - } - { - auto iter = cxMapMutIteratorValues(map); - cx_foreach(char*, v, iter) { - if (v[4] == '5') cxIteratorFlagRemoval(iter); - } - } - - EXPECT_STREQ(v1, "OK"); - EXPECT_STREQ(v2, "OK"); - EXPECT_STREQ(v3, "val 3"); - EXPECT_STREQ(v4, "OK"); - EXPECT_STREQ(v5, "OK"); - - v1[0] = v2[0] = v4[0] = v5[0] = 'c'; - - cxMapDestroy(map); - - EXPECT_STREQ(v1, "cK"); - EXPECT_STREQ(v2, "cK"); - EXPECT_STREQ(v3, "OK"); - EXPECT_STREQ(v4, "cK"); - EXPECT_STREQ(v5, "cK"); -} - -TEST(CxHashMap, SimpleDestructor) { - CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); - map->simple_destructor = test_simple_destructor; - verify_any_destructor(map); - EXPECT_TRUE(allocator.verify()); -} - -TEST(CxHashMap, AdvancedDestructor) { - CxTestingAllocator allocator; - auto map = cxHashMapCreate(&allocator, CX_STORE_POINTERS, 0); - map->advanced_destructor = test_advanced_destructor; - verify_any_destructor(map); - EXPECT_TRUE(allocator.verify()); -} - -TEST(CxHashMap, Generics) { - CxTestingAllocator allocator; - auto map = test_map_generics_step_1(&allocator); - - EXPECT_EQ(map->size, 3); - EXPECT_STREQ((char *) cxMapGet(map, "test"), "test"); - EXPECT_STREQ((char *) cxMapGet(map, "foo"), "bar"); - EXPECT_STREQ((char *) cxMapGet(map, "hallo"), "welt"); - - test_map_generics_step_2(map); - - EXPECT_EQ(map->size, 2); - EXPECT_STREQ((char *) cxMapGet(map, "key"), "value"); - EXPECT_STREQ((char *) cxMapGet(map, "foo"), "bar"); - - test_map_generics_step_3(map); - - EXPECT_EQ(map->size, 0); - - cxMapDestroy(map); - EXPECT_TRUE(allocator.verify()); -} - -TEST(EmptyMap, Size) { - auto map = cxEmptyMap; - - EXPECT_EQ(map->size, 0); -} - -TEST(EmptyMap, Iterator) { - auto map = cxEmptyMap; - - auto it1 = cxMapIterator(map); - auto it2 = cxMapIteratorValues(map); - auto it3 = cxMapIteratorKeys(map); - auto it4 = cxMapMutIterator(map); - auto it5 = cxMapMutIteratorValues(map); - auto it6 = cxMapMutIteratorKeys(map); - - EXPECT_FALSE(cxIteratorValid(it1)); - EXPECT_FALSE(cxIteratorValid(it2)); - EXPECT_FALSE(cxIteratorValid(it3)); - EXPECT_FALSE(cxIteratorValid(it4)); - EXPECT_FALSE(cxIteratorValid(it5)); - EXPECT_FALSE(cxIteratorValid(it6)); - - int c = 0; - cx_foreach(void*, data, it1) c++; - cx_foreach(void*, data, it2) c++; - cx_foreach(void*, data, it3) c++; - cx_foreach(void*, data, it4) c++; - cx_foreach(void*, data, it5) c++; - cx_foreach(void*, data, it6) c++; - EXPECT_EQ(c, 0); -} - -TEST(EmptyMap, NoOps) { - auto map = cxEmptyMap; - - ASSERT_NO_FATAL_FAILURE(cxMapClear(map)); - ASSERT_NO_FATAL_FAILURE(cxMapDestroy(map)); -} - -TEST(EmptyMap, Get) { - auto map = cxEmptyMap; - - CxHashKey key = cx_hash_key_str("test"); - EXPECT_EQ(cxMapGet(map, key), nullptr); -}
--- a/tests/test_map_generics.c Sat Dec 30 15:21:16 2023 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include "test_map_generics.h" -#include "cx/hash_map.h" - -CxMap *test_map_generics_step_1(CxAllocator const * allocator) { - CxMap *map = cxHashMapCreate(allocator, sizeof(cxstring), 0); - - cxMapPut(map, "test", "test"); - cxMapPut(map, cx_mutstr("foo"), "bar"); - cxMapPut(map, cx_str("hallo"), "welt"); - - return map; -} - -void test_map_generics_step_2(CxMap *map) { - // note: we don't have a destructor here, so remove and detach are the same - cxMapRemove(map, cx_str("test")); - char const* hallo = "hallo"; - cxMapDetach(map, hallo); - cxMapPut(map, cx_hash_key_str("key"), "value"); -} - -void test_map_generics_step_3(CxMap *map) { - void *r; - r = cxMapRemoveAndGet(map, "key"); - r = cxMapRemoveAndGet(map, cx_str("foo")); - if (r != NULL) map->size = 47; -}
--- a/tests/test_map_generics.h Sat Dec 30 15:21:16 2023 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef UCX_TEST_MAP_GENERICS_H -#define UCX_TEST_MAP_GENERICS_H - -#include "cx/map.h" - -#ifdef __cplusplus -extern "C" { -#endif - -CxMap *test_map_generics_step_1(CxAllocator const *); - -void test_map_generics_step_2(CxMap *); - -void test_map_generics_step_3(CxMap *); - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif //UCX_TEST_MAP_GENERICS_H
--- a/tests/test_mempool.c Sat Dec 30 15:21:16 2023 +0100 +++ b/tests/test_mempool.c Sat Dec 30 18:48:25 2023 +0100 @@ -97,7 +97,7 @@ while (rdata == data) { n <<= 1; // eventually the memory should be moved elsewhere - CX_TEST_ASSERTM(n < 65536, "Reallocation attempt failed - test not executable."); + CX_TEST_ASSERTM(n < 65536, "Reallocation attempt failed - test not executable"); rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t)); }
--- a/tests/ucxtest.c Sat Dec 30 15:21:16 2023 +0100 +++ b/tests/ucxtest.c Sat Dec 30 18:48:25 2023 +0100 @@ -35,6 +35,7 @@ CxTestSuite *cx_test_suite_string(void); CxTestSuite *cx_test_suite_printf(void); CxTestSuite *cx_test_suite_mempool(void); +CxTestSuite *cx_test_suite_hash_map(void); #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure #define execute_test_suites(...) unsigned success = 0, failure = 0; CxTestSuite* test_suites[] = {__VA_ARGS__}; \ @@ -51,7 +52,8 @@ cx_test_suite_allocator(), cx_test_suite_string(), cx_test_suite_printf(), - cx_test_suite_mempool() + cx_test_suite_mempool(), + cx_test_suite_hash_map() ); printf("=== OVERALL RESULT ===\n"); printf(" Total: %u\n Success: %u\n Failure: %u\n",