]> git.mdlowis.com Git - projs/libcds.git/commitdiff
NULL is now a valid value for an inserted association
authorMike D. Lowis <mike.lowis@gentex.com>
Tue, 26 May 2015 19:38:55 +0000 (15:38 -0400)
committerMike D. Lowis <mike.lowis@gentex.com>
Tue, 26 May 2015 19:38:55 +0000 (15:38 -0400)
source/map/map.c
tests/test_map.c

index 273660c6a61dce1c2e3ec87b341eebc189415f52..9a8771da3313ac72bdb30fd44b6a258674c827a4 100644 (file)
@@ -17,8 +17,10 @@ struct map_t {
 
 static void map_pair_free(void* obj)
 {
-    mem_release(((map_pair_t*)obj)->key);
-    mem_release(((map_pair_t*)obj)->value);
+    if(((map_pair_t*)obj)->key)
+        mem_release(((map_pair_t*)obj)->key);
+    if(((map_pair_t*)obj)->value)
+        mem_release(((map_pair_t*)obj)->value);
 }
 
 static map_pair_t* map_pair_new(uint32_t hash, void* key, void* value)
index 5b36ce77723a4904a5e2b261af9eeb21bc32cf1d..3d44c08941a6754ac133ce46cb3e85ace428f5e5 100644 (file)
@@ -131,6 +131,17 @@ TEST_SUITE(Map) {
         mem_release(lup_val);
     }
 
+    TEST(Verify_map_lookup_should_return_NULL_if_the_associated_value_is_null)
+    {
+        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), NULL);
+        CHECK(map_size(map) == 1);
+        CHECK(map_lookup(map, lup_val) == NULL);
+        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);