From 0a857ee6b06da5bd692dfb39a722f11f9458ccef Mon Sep 17 00:00:00 2001 From: "Mike D. Lowis" Date: Tue, 26 May 2015 15:34:10 -0400 Subject: [PATCH] map_has_key no longer depends on map_lookup and therefore will behave properly even when a user inserts a NULL value into the map --- source/map/map.c | 4 +++- tests/test_map.c | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/source/map/map.c b/source/map/map.c index f9b37c5..273660c 100644 --- a/source/map/map.c +++ b/source/map/map.c @@ -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) diff --git a/tests/test_map.c b/tests/test_map.c index 8f5544e..5b36ce7 100644 --- a/tests/test_map.c +++ b/tests/test_map.c @@ -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); + } } -- 2.49.0