old_utils_tests.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "unity/examples/unity_config.h"
  23. #include "unity/src/unity.h"
  24. #include "common.h"
  25. #include "../cJSON_Utils.h"
  26. /* JSON Apply Merge tests: */
  27. static const char *merges[15][3] =
  28. {
  29. {"{\"a\":\"b\"}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"},
  30. {"{\"a\":\"b\"}", "{\"b\":\"c\"}", "{\"a\":\"b\",\"b\":\"c\"}"},
  31. {"{\"a\":\"b\"}", "{\"a\":null}", "{}"},
  32. {"{\"a\":\"b\",\"b\":\"c\"}", "{\"a\":null}", "{\"b\":\"c\"}"},
  33. {"{\"a\":[\"b\"]}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"},
  34. {"{\"a\":\"c\"}", "{\"a\":[\"b\"]}", "{\"a\":[\"b\"]}"},
  35. {"{\"a\":{\"b\":\"c\"}}", "{\"a\":{\"b\":\"d\",\"c\":null}}", "{\"a\":{\"b\":\"d\"}}"},
  36. {"{\"a\":[{\"b\":\"c\"}]}", "{\"a\":[1]}", "{\"a\":[1]}"},
  37. {"[\"a\",\"b\"]", "[\"c\",\"d\"]", "[\"c\",\"d\"]"},
  38. {"{\"a\":\"b\"}", "[\"c\"]", "[\"c\"]"},
  39. {"{\"a\":\"foo\"}", "null", "null"},
  40. {"{\"a\":\"foo\"}", "\"bar\"", "\"bar\""},
  41. {"{\"e\":null}", "{\"a\":1}", "{\"e\":null,\"a\":1}"},
  42. {"[1,2]", "{\"a\":\"b\",\"c\":null}", "{\"a\":\"b\"}"},
  43. {"{}","{\"a\":{\"bb\":{\"ccc\":null}}}", "{\"a\":{\"bb\":{}}}"}
  44. };
  45. static void json_pointer_tests(void)
  46. {
  47. cJSON *root = NULL;
  48. const char *json=
  49. "{"
  50. "\"foo\": [\"bar\", \"baz\"],"
  51. "\"\": 0,"
  52. "\"a/b\": 1,"
  53. "\"c%d\": 2,"
  54. "\"e^f\": 3,"
  55. "\"g|h\": 4,"
  56. "\"i\\\\j\": 5,"
  57. "\"k\\\"l\": 6,"
  58. "\" \": 7,"
  59. "\"m~n\": 8"
  60. "}";
  61. root = cJSON_Parse(json);
  62. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, ""), root);
  63. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/foo"), cJSON_GetObjectItem(root, "foo"));
  64. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/foo/0"), cJSON_GetObjectItem(root, "foo")->child);
  65. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/foo/0"), cJSON_GetObjectItem(root, "foo")->child);
  66. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/"), cJSON_GetObjectItem(root, ""));
  67. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/a~1b"), cJSON_GetObjectItem(root, "a/b"));
  68. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/c%d"), cJSON_GetObjectItem(root, "c%d"));
  69. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/c^f"), cJSON_GetObjectItem(root, "c^f"));
  70. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/c|f"), cJSON_GetObjectItem(root, "c|f"));
  71. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/i\\j"), cJSON_GetObjectItem(root, "i\\j"));
  72. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/k\"l"), cJSON_GetObjectItem(root, "k\"l"));
  73. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/ "), cJSON_GetObjectItem(root, " "));
  74. TEST_ASSERT_EQUAL_PTR(cJSONUtils_GetPointer(root, "/m~0n"), cJSON_GetObjectItem(root, "m~n"));
  75. cJSON_Delete(root);
  76. }
  77. static void misc_tests(void)
  78. {
  79. /* Misc tests */
  80. int numbers[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  81. cJSON *object = NULL;
  82. cJSON *object1 = NULL;
  83. cJSON *object2 = NULL;
  84. cJSON *object3 = NULL;
  85. cJSON *object4 = NULL;
  86. cJSON *nums = NULL;
  87. cJSON *num6 = NULL;
  88. char *pointer = NULL;
  89. printf("JSON Pointer construct\n");
  90. object = cJSON_CreateObject();
  91. nums = cJSON_CreateIntArray(numbers, 10);
  92. num6 = cJSON_GetArrayItem(nums, 6);
  93. cJSON_AddItemToObject(object, "numbers", nums);
  94. pointer = cJSONUtils_FindPointerFromObjectTo(object, num6);
  95. TEST_ASSERT_EQUAL_STRING("/numbers/6", pointer);
  96. free(pointer);
  97. pointer = cJSONUtils_FindPointerFromObjectTo(object, nums);
  98. TEST_ASSERT_EQUAL_STRING("/numbers", pointer);
  99. free(pointer);
  100. pointer = cJSONUtils_FindPointerFromObjectTo(object, object);
  101. TEST_ASSERT_EQUAL_STRING("", pointer);
  102. free(pointer);
  103. object1 = cJSON_CreateObject();
  104. object2 = cJSON_CreateString("m~n");
  105. cJSON_AddItemToObject(object1, "m~n", object2);
  106. pointer = cJSONUtils_FindPointerFromObjectTo(object1, object2);
  107. TEST_ASSERT_EQUAL_STRING("/m~0n",pointer);
  108. free(pointer);
  109. object3 = cJSON_CreateObject();
  110. object4 = cJSON_CreateString("m/n");
  111. cJSON_AddItemToObject(object3, "m/n", object4);
  112. pointer = cJSONUtils_FindPointerFromObjectTo(object3, object4);
  113. TEST_ASSERT_EQUAL_STRING("/m~1n",pointer);
  114. free(pointer);
  115. cJSON_Delete(object);
  116. cJSON_Delete(object1);
  117. cJSON_Delete(object3);
  118. }
  119. static void sort_tests(void)
  120. {
  121. /* Misc tests */
  122. const char *random = "QWERTYUIOPASDFGHJKLZXCVBNM";
  123. char buf[2] = {'\0', '\0'};
  124. cJSON *sortme = NULL;
  125. size_t i = 0;
  126. cJSON *current_element = NULL;
  127. /* JSON Sort test: */
  128. sortme = cJSON_CreateObject();
  129. for (i = 0; i < 26; i++)
  130. {
  131. buf[0] = random[i];
  132. cJSON_AddItemToObject(sortme, buf, cJSON_CreateNumber(1));
  133. }
  134. cJSONUtils_SortObject(sortme);
  135. /* check sorting */
  136. current_element = sortme->child->next;
  137. for (i = 1; (i < 26) && (current_element != NULL) && (current_element->prev != NULL); i++)
  138. {
  139. TEST_ASSERT_TRUE(current_element->string[0] >= current_element->prev->string[0]);
  140. current_element = current_element->next;
  141. }
  142. cJSON_Delete(sortme);
  143. }
  144. static void merge_tests(void)
  145. {
  146. size_t i = 0;
  147. char *patchtext = NULL;
  148. char *after = NULL;
  149. /* Merge tests: */
  150. printf("JSON Merge Patch tests\n");
  151. for (i = 0; i < 15; i++)
  152. {
  153. cJSON *object_to_be_merged = cJSON_Parse(merges[i][0]);
  154. cJSON *patch = cJSON_Parse(merges[i][1]);
  155. patchtext = cJSON_PrintUnformatted(patch);
  156. object_to_be_merged = cJSONUtils_MergePatch(object_to_be_merged, patch);
  157. after = cJSON_PrintUnformatted(object_to_be_merged);
  158. TEST_ASSERT_EQUAL_STRING(merges[i][2], after);
  159. free(patchtext);
  160. free(after);
  161. cJSON_Delete(object_to_be_merged);
  162. cJSON_Delete(patch);
  163. }
  164. }
  165. static void generate_merge_tests(void)
  166. {
  167. size_t i = 0;
  168. char *patchedtext = NULL;
  169. /* Generate Merge tests: */
  170. for (i = 0; i < 15; i++)
  171. {
  172. cJSON *from = cJSON_Parse(merges[i][0]);
  173. cJSON *to = cJSON_Parse(merges[i][2]);
  174. cJSON *patch = cJSONUtils_GenerateMergePatch(from,to);
  175. from = cJSONUtils_MergePatch(from,patch);
  176. patchedtext = cJSON_PrintUnformatted(from);
  177. TEST_ASSERT_EQUAL_STRING(merges[i][2], patchedtext);
  178. cJSON_Delete(from);
  179. cJSON_Delete(to);
  180. cJSON_Delete(patch);
  181. free(patchedtext);
  182. }
  183. }
  184. int main(void)
  185. {
  186. UNITY_BEGIN();
  187. RUN_TEST(json_pointer_tests);
  188. RUN_TEST(misc_tests);
  189. RUN_TEST(sort_tests);
  190. RUN_TEST(merge_tests);
  191. RUN_TEST(generate_merge_tests);
  192. return UNITY_END();
  193. }