fix hash map not being able to deal with 64-bit hashes

Fri, 24 Oct 2025 20:52:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 24 Oct 2025 20:52:10 +0200
changeset 1442
244fb8bc3584
parent 1441
78ec3e2243e4
child 1443
6986e9ed70f3

fix hash map not being able to deal with 64-bit hashes

src/hash_map.c file | annotate | diff | comparison | revisions
tests/test_hash_map.c file | annotate | diff | comparison | revisions
--- a/src/hash_map.c	Thu Oct 23 17:54:17 2025 +0200
+++ b/src/hash_map.c	Fri Oct 24 20:52:10 2025 +0200
@@ -86,7 +86,7 @@
     struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
     const CxAllocator *allocator = map->collection.allocator;
 
-    unsigned hash = key.hash;
+    uint64_t hash = key.hash;
     if (hash == 0) {
         cx_hash_murmur(&key);
         hash = key.hash;
@@ -203,7 +203,7 @@
 ) {
     struct cx_hash_map_s *hash_map = (struct cx_hash_map_s *) map;
 
-    unsigned hash = key.hash;
+    uint64_t hash = key.hash;
     if (hash == 0) {
         cx_hash_murmur(&key);
         hash = key.hash;
--- a/tests/test_hash_map.c	Thu Oct 23 17:54:17 2025 +0200
+++ b/tests/test_hash_map.c	Fri Oct 24 20:52:10 2025 +0200
@@ -287,6 +287,21 @@
     cx_testing_allocator_destroy(&talloc);
 }
 
+CX_TEST(test_hash_map_integer_keys) {
+    CxMap *map = cxHashMapCreateSimple(sizeof(cxstring));
+    CX_TEST_DO {
+        cxstring s1 = CX_STR("hello");
+        cxstring s2 = CX_STR("world");
+
+        cxMapPut(map, UINT32_C(70875), &s1);
+        cxMapPut(map, UINT64_C(5785133750), &s2);
+
+        CX_TEST_ASSERT(cx_strcmp_p(&s1, cxMapGet(map, UINT32_C(70875))) == 0);
+        CX_TEST_ASSERT(cx_strcmp_p(&s2, cxMapGet(map, UINT64_C(5785133750))) == 0);
+    }
+    cxMapFree(map);
+}
+
 CX_TEST(test_hash_map_remove_via_iterator) {
     CxTestingAllocator talloc;
     cx_testing_allocator_init(&talloc);
@@ -916,6 +931,7 @@
     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_integer_keys);
     cx_test_register(suite, test_hash_map_remove_via_iterator);
     cx_test_register(suite, test_hash_map_simple_destructor_objects);
     cx_test_register(suite, test_hash_map_advanced_destructor_objects);

mercurial