diff -r 244fb8bc3584 -r 6986e9ed70f3 tests/test_hash_map.c --- a/tests/test_hash_map.c Fri Oct 24 20:52:10 2025 +0200 +++ b/tests/test_hash_map.c Fri Oct 24 21:15:14 2025 +0200 @@ -513,8 +513,11 @@ cx_testing_allocator_destroy(&talloc); } +static unsigned test_hash_map_clone_func_max_clones; static void *test_hash_map_clone_func(void *dst, const void *src, const CxAllocator *al, void *data) { + if (test_hash_map_clone_func_max_clones == 0) return NULL; + test_hash_map_clone_func_max_clones--; if (dst == NULL) { dst = cxMalloc(al, sizeof(int)); } @@ -535,6 +538,7 @@ } CX_TEST_DO { int c = 4; + test_hash_map_clone_func_max_clones = 100; // no limit size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c); CX_TEST_ASSERT(cloned == 3); CX_TEST_ASSERT(cxMapSize(dst) == 5); @@ -552,6 +556,38 @@ cxMapFree(src); } +CX_TEST(test_hash_map_clone_alloc_fail) { + CxMap *dst = cxHashMapCreateSimple(sizeof(int)); + CxMap *src = cxHashMapCreateSimple(sizeof(int)); + const char *exist_keys[] = {"k1", "k2", "k3"}; + int exists[] = {1, 3, 4}; + const char *source_keys[] = {"k4", "k2", "k5"}; + int source[] = {7, 9, 15}; + for (unsigned int i = 0 ; i < 3 ; i++) { + cxMapPut(dst, exist_keys[i], &exists[i]); + cxMapPut(src, source_keys[i], &source[i]); + } + CX_TEST_DO { + int c = 4; + test_hash_map_clone_func_max_clones = 2; + size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, NULL, &c); + CX_TEST_ASSERT(cloned == 2); + CX_TEST_ASSERT(cxMapSize(dst) == 4); + CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k1")) == 1); + CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k2")) == 13); + CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k3")) == 4); + // the concrete element which is affected might change when the hash function changes + CX_TEST_ASSERT(cxMapGet(dst, "k4") == NULL); + CX_TEST_ASSERT(*((int*)cxMapGet(dst, "k5")) == 19); + CX_TEST_ASSERT(cxMapSize(src) == 3); + CX_TEST_ASSERT(*((int*)cxMapGet(src, "k4")) == 7); + CX_TEST_ASSERT(*((int*)cxMapGet(src, "k2")) == 9); + CX_TEST_ASSERT(*((int*)cxMapGet(src, "k5")) == 15); + } + cxMapFree(dst); + cxMapFree(src); +} + CX_TEST(test_hash_map_clone_ptr) { CxTestingAllocator talloc; cx_testing_allocator_init(&talloc); @@ -571,6 +607,7 @@ } CX_TEST_DO { int c = 4; + test_hash_map_clone_func_max_clones = 100; // no limit size_t cloned = cxMapClone(dst, src, test_hash_map_clone_func, allocator, &c); CX_TEST_ASSERT(cloned == 3); CX_TEST_ASSERT(cxMapSize(dst) == 5); @@ -938,6 +975,7 @@ cx_test_register(suite, test_hash_map_simple_destructor_pointers); cx_test_register(suite, test_hash_map_advanced_destructor_pointers); cx_test_register(suite, test_hash_map_clone); + cx_test_register(suite, test_hash_map_clone_alloc_fail); cx_test_register(suite, test_hash_map_clone_ptr); cx_test_register(suite, test_empty_map_no_ops); cx_test_register(suite, test_empty_map_size);