]> git.mdlowis.com Git - projs/libcds.git/commitdiff
map_has_key no longer depends on map_lookup and therefore will behave properly even...
authorMike D. Lowis <mike.lowis@gentex.com>
Tue, 26 May 2015 19:34:10 +0000 (15:34 -0400)
committerMike D. Lowis <mike.lowis@gentex.com>
Tue, 26 May 2015 19:34:10 +0000 (15:34 -0400)
source/map/map.c
tests/test_map.c

index f9b37c51d010c65dd2a87f0bd34cd0dea2807fd5..273660c6a61dce1c2e3ec87b341eebc189415f52 100644 (file)
@@ -62,7 +62,9 @@ map_t* map_new(cmp_t* cmp, hashfn_t hash_fn)
 
 bool map_has_key(map_t* map, void* key)
 {
-    return (NULL != map_lookup(map, key));
+    map_pair_t  pair = { map->hash_func(key), key, NULL };
+    rbt_node_t* curr = rbt_lookup(map->tree, &pair);
+    return (NULL != curr);
 }
 
 size_t map_size(map_t* map)
index 8f5544ec97f2617d625ca45c040a17c51b6bfae8..5b36ce77723a4904a5e2b261af9eeb21bc32cf1d 100644 (file)
@@ -116,4 +116,27 @@ TEST_SUITE(Map) {
         mem_release(map);
         mem_release(del_val);
     }
+
+    //-------------------------------------------------------------------------
+    // Test map_lookup function
+    //-------------------------------------------------------------------------
+    TEST(Verify_map_lookup_should_return_the_associated_value_for_the_given_key)
+    {
+        map_t* map = map_new(cmp_new(NULL, cmp_int), hash_int);
+        void* lup_val = mem_box(42);
+        map_insert(map, mem_retain(lup_val), mem_retain(lup_val));
+        CHECK(map_size(map) == 1);
+        CHECK(map_lookup(map, lup_val) == lup_val);
+        mem_release(map);
+        mem_release(lup_val);
+    }
+
+    TEST(Verify_map_lookup_should_return_NULL_if_no_association_exists)
+    {
+        map_t* map = map_new(cmp_new(NULL, cmp_int), hash_int);
+        void* lup_val = mem_box(42);
+        CHECK(map_lookup(map, lup_val) == NULL);
+        mem_release(map);
+        mem_release(lup_val);
+    }
 }