# HG changeset patch # User Mike Becker # Date 1766165486 -3600 # Node ID 14ae6a039af723bb7c600801eda6e517a330da21 # Parent 34c2e143694592bd09365e713d06c380c347e683 add tests for cxMapCompare() - relates to #622 diff -r 34c2e1436945 -r 14ae6a039af7 tests/test_hash_map.c --- a/tests/test_hash_map.c Fri Dec 19 18:12:20 2025 +0100 +++ b/tests/test_hash_map.c Fri Dec 19 18:31:26 2025 +0100 @@ -1273,63 +1273,125 @@ cxMapFree(d2); } -CX_TEST(test_hash_map_compare) { - CxMap *map1 = cxHashMapCreate(NULL, sizeof(int), 0); - CxMap *map2 = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0); - cxSetCompareFunc(map1, cx_cmp_int); - cxSetCompareFunc(map2, cx_cmp_int); - +static CX_TEST_SUBROUTINE(test_hash_map_compare_verify, CxMap *map1, CxMap *map2) { // some ints we can point to in the pointer map + int x13 = 13; + int x15 = 15; + int x42 = 42; + int x1337 = 1337; + int x4711 = 4711; int z13 = 13; int z15 = 15; int z42 = 42; int z1337 = 1337; int z4711 = 4711; - - CX_TEST_DO { - // empty maps are equal - CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0); - CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0); + int *y13; + int *y15; + int *y42; + int *y1337; + int *y4711; + if (map1->collection.simple_cmp == cx_cmp_ptr) { + // for the defaulted compare func, the pointers must be exactly equal + y13 = &x13; + y15 = &x15; + y42 = &x42; + y1337 = &x1337; + y4711 = &x4711; + } else { + y13 = &z13; + y15 = &z15; + y42 = &z42; + y1337 = &z1337; + y4711 = &z4711; + } - // left has fewer keys than right - cxMapPut(map1, "first key", &z13); - cxMapPut(map1, "second key", &z15); - cxMapPut(map2, "first key", &z13); - cxMapPut(map2, "second key", &z15); - cxMapPut(map2, "third key", &z42); - CX_TEST_ASSERT(cxMapCompare(map1, map2) < 0); - CX_TEST_ASSERT(cxMapCompare(map2, map1) > 0); + // empty maps are equal + CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0); + CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0); - // both are equal - cxMapPut(map1, "third key", &z42); - CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0); - CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0); + // left has fewer keys than right + cxMapPut(map1, "first key", &x13); + cxMapPut(map1, "second key", &x15); + cxMapPut(map2, "first key", y13); + cxMapPut(map2, "second key", y15); + cxMapPut(map2, "third key", y42); + CX_TEST_ASSERT(cxMapCompare(map1, map2) < 0); + CX_TEST_ASSERT(cxMapCompare(map2, map1) > 0); - // left has more keys than right - cxMapPut(map1, "fourth key", &z1337); - CX_TEST_ASSERT(cxMapCompare(map1, map2) > 0); - CX_TEST_ASSERT(cxMapCompare(map2, map1) < 0); + // both are equal + cxMapPut(map1, "third key", &x42); + CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0); + CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0); - // key sets differ - cxMapPut(map2, "wrong key", &z1337); - CX_TEST_ASSERT(cxMapCompare(map1, map2) != 0); - CX_TEST_ASSERT(cxMapCompare(map2, map1) != 0); + // left has more keys than right + cxMapPut(map1, "fourth key", &x1337); + CX_TEST_ASSERT(cxMapCompare(map1, map2) > 0); + CX_TEST_ASSERT(cxMapCompare(map2, map1) < 0); + + // key sets differ + cxMapPut(map2, "wrong key", y1337); + CX_TEST_ASSERT(cxMapCompare(map1, map2) != 0); + CX_TEST_ASSERT(cxMapCompare(map2, map1) != 0); - // values differ - cxMapRemove(map2, "wrong key"); - cxMapPut(map2, "fourth key", &z4711); - CX_TEST_ASSERT(cxMapCompare(map1, map2) != 0); - CX_TEST_ASSERT(cxMapCompare(map2, map1) != 0); + // values differ + cxMapRemove(map2, "wrong key"); + cxMapPut(map2, "fourth key", y4711); + CX_TEST_ASSERT(cxMapCompare(map1, map2) != 0); + CX_TEST_ASSERT(cxMapCompare(map2, map1) != 0); - // equal again (by overwriting value in map 1) - cxMapPut(map1, "fourth key", &z4711); - CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0); - CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0); + // equal again (by overwriting value in map 1) + cxMapPut(map1, "fourth key", &x4711); + CX_TEST_ASSERT(cxMapCompare(map1, map2) == 0); + CX_TEST_ASSERT(cxMapCompare(map2, map1) == 0); +} + +CX_TEST(test_hash_map_compare) { + CxMap *map1 = cxHashMapCreate(NULL, sizeof(int), 0); + CxMap *map2 = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0); + cxSetCompareFunc(map1, cx_cmp_int); + cxSetCompareFunc(map2, cx_cmp_int); + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(test_hash_map_compare_verify, map1, map2); } cxMapFree(map1); cxMapFree(map2); } +static int test_ccmp_int(const void *l, const void *r, void *c) { + int *z = c; + // return bullshit to make the test fail when c was not passed correctly + if (z == NULL || *z != 1337) return -1; + return cx_cmp_int(l, r); +} + +CX_TEST(test_hash_map_compare2) { + CxMap *map1 = cxHashMapCreate(NULL, sizeof(int), 0); + CxMap *map2 = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0); + int z = 1337; + cxSetAdvancedCompareFunc(map1, test_ccmp_int, &z); + cxSetAdvancedCompareFunc(map2, test_ccmp_int, &z); + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(test_hash_map_compare_verify, map1, map2); + } + cxMapFree(map1); + cxMapFree(map2); +} + +CX_TEST(test_hash_map_compare_default_cmp_funcs) { + CxMap *map1 = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0); + CxMap *map2 = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0); + CxMap *map3 = cxHashMapCreate(NULL, sizeof(int), 0); + CxMap *map4 = cxHashMapCreate(NULL, sizeof(int), 0); + CX_TEST_DO { + CX_TEST_CALL_SUBROUTINE(test_hash_map_compare_verify, map1, map2); + CX_TEST_CALL_SUBROUTINE(test_hash_map_compare_verify, map3, map4); + } + cxMapFree(map1); + cxMapFree(map2); + cxMapFree(map3); + cxMapFree(map4); +} + CX_TEST(test_empty_map_size) { CX_TEST_DO { CX_TEST_ASSERT(cxEmptyMap->collection.size == 0); @@ -1697,6 +1759,8 @@ cx_test_register(suite, test_hash_map_union_alloc_fail); cx_test_register(suite, test_hash_map_shallow_clones); cx_test_register(suite, test_hash_map_compare); + cx_test_register(suite, test_hash_map_compare2); + cx_test_register(suite, test_hash_map_compare_default_cmp_funcs); 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);