Browse Source

tests: cJSON_ArrayForEach

Max Bruckner 8 years ago
parent
commit
cbbcc91498
2 changed files with 76 additions and 0 deletions
  1. 1 0
      tests/CMakeLists.txt
  2. 75 0
      tests/misc_tests.c

+ 1 - 0
tests/CMakeLists.txt

@@ -19,6 +19,7 @@ if(ENABLE_CJSON_TEST)
         print_array
         print_object
         print_value
+        misc_tests
     )
 
     add_library(test-common common.c)

+ 75 - 0
tests/misc_tests.c

@@ -0,0 +1,75 @@
+/*
+  Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
+
+  Permission is hereby granted, free of charge, to any person obtaining a copy
+  of this software and associated documentation files (the "Software"), to deal
+  in the Software without restriction, including without limitation the rights
+  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+  copies of the Software, and to permit persons to whom the Software is
+  furnished to do so, subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be included in
+  all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+  THE SOFTWARE.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "unity/examples/unity_config.h"
+#include "unity/src/unity.h"
+#include "common.h"
+
+
+static void cjson_array_foreach_should_loop_over_arrays(void)
+{
+    cJSON array[1];
+    cJSON elements[10];
+    cJSON *element_pointer = NULL;
+    size_t i = 0;
+
+    memset(array, 0, sizeof(array));
+    memset(elements, 0, sizeof(elements));
+
+    /* create array */
+    array[0].child = &elements[0];
+    elements[0].prev = NULL;
+    elements[9].next = NULL;
+    for (i = 0; i < 9; i++)
+    {
+        elements[i].next = &elements[i + 1];
+        elements[i + 1].prev = &elements[i];
+    }
+
+    i = 0;
+    cJSON_ArrayForEach(element_pointer, array)
+    {
+        TEST_ASSERT_TRUE_MESSAGE(element_pointer == &elements[i], "Not iterating over array properly");
+        i++;
+    }
+}
+
+static void cjson_array_foreach_should_not_dereference_null_pointer(void)
+{
+    cJSON *array = NULL;
+    cJSON *element = NULL;
+    cJSON_ArrayForEach(element, array);
+}
+
+int main(void)
+{
+    UNITY_BEGIN();
+
+    RUN_TEST(cjson_array_foreach_should_loop_over_arrays);
+    RUN_TEST(cjson_array_foreach_should_not_dereference_null_pointer);
+
+    return UNITY_END();
+}