Fixed bug in u_arraylist_contains caused by poor member naming.
authorJon A. Cruz <jonc@osg.samsung.com>
Tue, 1 Sep 2015 22:42:38 +0000 (15:42 -0700)
committerJon A. Cruz <jonc@osg.samsung.com>
Thu, 3 Sep 2015 04:18:29 +0000 (04:18 +0000)
Corrected a bug in u_arraylist_contains caused by the member names
'size' and 'length' being too similar and hiding a logic problem.
Fixed by changing 'size' to the more common 'capacity'.

The base fix (without any of the name change) was pulled from
Vimala's revision 10 submission of change 2301.
https://gerrit.iotivity.org/gerrit/#/c/2301/10/

Change-Id: I22ac6fc159669ad58212b145f70cb559187fa999
Signed-off-by: Jon A. Cruz <jonc@osg.samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2339
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Vimala Vishwanathan <vimala.v@samsung.com>
Reviewed-by: jihwan seo <jihwan.seo@samsung.com>
resource/csdk/connectivity/common/inc/uarraylist.h
resource/csdk/connectivity/common/src/uarraylist.c
resource/csdk/connectivity/test/uarraylist_test.cpp

index 1457e31..31a70f6 100644 (file)
@@ -40,7 +40,7 @@ typedef struct u_arraylist_t
 {
     void **data;
     uint32_t length;
-    uint32_t size;
+    uint32_t capacity;
 } u_arraylist_t;
 
 /**
index d205aa5..17fce9a 100644 (file)
@@ -27,9 +27,9 @@
 #define TAG "UARRAYLIST"
 
 /**
- * Use this default size when initialized
+ * Use this default capacity when initialized
  */
-#define U_ARRAYLIST_DEFAULT_SIZE 1
+#define U_ARRAYLIST_DEFAULT_CAPACITY 1
 
 u_arraylist_t *u_arraylist_create()
 {
@@ -40,10 +40,10 @@ u_arraylist_t *u_arraylist_create()
         return NULL;
     }
 
-    list->size = U_ARRAYLIST_DEFAULT_SIZE;
+    list->capacity = U_ARRAYLIST_DEFAULT_CAPACITY;
     list->length = 0;
 
-    list->data = (void **) OICMalloc(list->size * sizeof(list->data[0]));
+    list->data = (void **) OICMalloc(list->capacity * sizeof(list->data[0]));
     if (!list->data)
     {
         OIC_LOG(DEBUG, TAG, "Out of memory");
@@ -68,7 +68,7 @@ void u_arraylist_free(u_arraylist_t **list)
 
 void u_arraylist_reserve(u_arraylist_t *list, size_t count)
 {
-    if (list && (count > list->size))
+    if (list && (count > list->capacity))
     {
         void *tmp = OICRealloc(list->data, count * sizeof(list->data[0]));
         if (!tmp)
@@ -79,7 +79,7 @@ void u_arraylist_reserve(u_arraylist_t *list, size_t count)
         else
         {
             list->data = (void **) tmp;
-            list->size = count;
+            list->capacity = count;
         }
     }
 }
@@ -91,8 +91,8 @@ void u_arraylist_shrink_to_fit(u_arraylist_t *list)
         return;
     }
 
-    if ((list->size > list->length)
-        && (list->length >= U_ARRAYLIST_DEFAULT_SIZE))
+    if ((list->capacity > list->length)
+        && (list->length >= U_ARRAYLIST_DEFAULT_CAPACITY))
     {
         void *tmp = OICRealloc(list->data,
                                list->length * sizeof(list->data[0]));
@@ -104,7 +104,7 @@ void u_arraylist_shrink_to_fit(u_arraylist_t *list)
         else
         {
             list->data = (void **) tmp;
-            list->size = list->length;
+            list->capacity = list->length;
         }
     }
 }
@@ -132,21 +132,22 @@ bool u_arraylist_add(u_arraylist_t *list, void *data)
         return false;
     }
 
-    if (list->size <= list->length)
+    if (list->capacity <= list->length)
     {
-        uint32_t new_size = (list->size * GROWTH_FACTOR) + 0.5;
+        uint32_t new_capacity = (list->capacity * GROWTH_FACTOR) + 0.5;
         // In case the re-alloc returns null, use a local variable to avoid
         // losing the current block of memory.
-        void *tmp = OICRealloc(list->data, new_size * sizeof(list->data[0]));
+        void *tmp = OICRealloc(list->data,
+                               new_capacity * sizeof(list->data[0]));
         if (!tmp)
         {
             OIC_LOG(DEBUG, TAG, "Memory reallocation failed.");
             return false;
         }
         list->data = (void **) tmp;
-        memset(list->data + list->size, 0,
-               (new_size - list->size) * sizeof(list->data[0]));
-        list->size = new_size;
+        memset(list->data + list->capacity, 0,
+               (new_capacity - list->capacity) * sizeof(list->data[0]));
+        list->capacity = new_capacity;
     }
 
     list->data[list->length] = data;
@@ -195,7 +196,7 @@ bool u_arraylist_contains(const u_arraylist_t *list, const void *data)
         return false;
     }
 
-    for (uint32_t i = 0; i < list->size; i++)
+    for (uint32_t i = 0; i < list->length; i++)
     {
         if (data == list->data[i])
         {
index 167835c..614d500 100644 (file)
@@ -157,9 +157,9 @@ TEST_F(UArrayListF, ShrinkToFit)
         u_arraylist_remove(list, i);
     }
 
-    EXPECT_GT(list->size, list->length);
+    EXPECT_GT(list->capacity, list->length);
     u_arraylist_shrink_to_fit(list);
-    EXPECT_EQ(list->size, list->length);
+    EXPECT_EQ(list->capacity, list->length);
 }
 
 TEST_F(UArrayListF, Get)