tests/test_hash_map.c

changeset 1579
0393c67556ec
parent 1510
89cf6b4a5792
child 1582
32b82c424252
--- a/tests/test_hash_map.c	Sat Dec 13 12:09:58 2025 +0100
+++ b/tests/test_hash_map.c	Sat Dec 13 12:24:35 2025 +0100
@@ -1263,6 +1263,64 @@
     cxMapFree(d2);
 }
 
+CX_TEST(test_hash_map_compare) {
+    CxMap *map1 = cxHashMapCreateSimple(sizeof(int));
+    CxMap *map2 = cxHashMapCreateSimple(CX_STORE_POINTERS);
+    // TODO: fix specification of compare function once #622 is realized
+    map1->collection.cmpfunc = cx_cmp_int;
+    map2->collection.cmpfunc = cx_cmp_int;
+
+    // some ints we can point to in the pointer map
+    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);
+
+        // 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);
+
+        // both are equal
+        cxMapPut(map1, "third key", &z42);
+        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);
+
+        // key sets differ
+        cxMapPut(map2, "wrong key", &z1337);
+        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);
+
+        // 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);
+    }
+    cxMapFree(map1);
+    cxMapFree(map2);
+}
+
 CX_TEST(test_empty_map_size) {
     CX_TEST_DO {
         CX_TEST_ASSERT(cxEmptyMap->collection.size == 0);
@@ -1629,6 +1687,7 @@
     cx_test_register(suite, test_hash_map_union_ptr);
     cx_test_register(suite, test_hash_map_union_alloc_fail);
     cx_test_register(suite, test_hash_map_simple_clones);
+    cx_test_register(suite, test_hash_map_compare);
     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);

mercurial