test_utils.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "cJSON_Utils.h"
  5. int main(void)
  6. {
  7. /* Some variables */
  8. char *temp = NULL;
  9. char *patchtext = NULL;
  10. char *patchedtext = NULL;
  11. int i = 0;
  12. /* JSON Pointer tests: */
  13. cJSON *root = NULL;
  14. const char *json=
  15. "{"
  16. "\"foo\": [\"bar\", \"baz\"],"
  17. "\"\": 0,"
  18. "\"a/b\": 1,"
  19. "\"c%d\": 2,"
  20. "\"e^f\": 3,"
  21. "\"g|h\": 4,"
  22. "\"i\\\\j\": 5,"
  23. "\"k\\\"l\": 6,"
  24. "\" \": 7,"
  25. "\"m~n\": 8"
  26. "}";
  27. const char *tests[12] = {"","/foo","/foo/0","/","/a~1b","/c%d","/e^f","/g|h","/i\\j","/k\"l","/ ","/m~0n"};
  28. /* JSON Apply Patch tests: */
  29. const char *patches[15][3] =
  30. {
  31. {"{ \"foo\": \"bar\"}", "[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\" }]","{\"baz\": \"qux\",\"foo\": \"bar\"}"},
  32. {"{ \"foo\": [ \"bar\", \"baz\" ] }", "[{ \"op\": \"add\", \"path\": \"/foo/1\", \"value\": \"qux\" }]","{\"foo\": [ \"bar\", \"qux\", \"baz\" ] }"},
  33. {"{\"baz\": \"qux\",\"foo\": \"bar\"}"," [{ \"op\": \"remove\", \"path\": \"/baz\" }]","{\"foo\": \"bar\" }"},
  34. {"{ \"foo\": [ \"bar\", \"qux\", \"baz\" ] }","[{ \"op\": \"remove\", \"path\": \"/foo/1\" }]","{\"foo\": [ \"bar\", \"baz\" ] }"},
  35. {"{ \"baz\": \"qux\",\"foo\": \"bar\"}","[{ \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" }]","{\"baz\": \"boo\",\"foo\": \"bar\"}"},
  36. {"{\"foo\": {\"bar\": \"baz\",\"waldo\": \"fred\"},\"qux\": {\"corge\": \"grault\"}}","[{ \"op\": \"move\", \"from\": \"/foo/waldo\", \"path\": \"/qux/thud\" }]","{\"foo\": {\"bar\": \"baz\"},\"qux\": {\"corge\": \"grault\",\"thud\": \"fred\"}}"},
  37. {"{ \"foo\": [ \"all\", \"grass\", \"cows\", \"eat\" ] }","[ { \"op\": \"move\", \"from\": \"/foo/1\", \"path\": \"/foo/3\" }]","{ \"foo\": [ \"all\", \"cows\", \"eat\", \"grass\" ] }"},
  38. {"{\"baz\": \"qux\",\"foo\": [ \"a\", 2, \"c\" ]}","[{ \"op\": \"test\", \"path\": \"/baz\", \"value\": \"qux\" },{ \"op\": \"test\", \"path\": \"/foo/1\", \"value\": 2 }]",""},
  39. {"{ \"baz\": \"qux\" }","[ { \"op\": \"test\", \"path\": \"/baz\", \"value\": \"bar\" }]",""},
  40. {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/child\", \"value\": { \"grandchild\": { } } }]","{\"foo\": \"bar\",\"child\": {\"grandchild\": {}}}"},
  41. {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\", \"xyz\": 123 }]","{\"foo\": \"bar\",\"baz\": \"qux\"}"},
  42. {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz/bat\", \"value\": \"qux\" }]",""},
  43. {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": 10}]",""},
  44. {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": \"10\"}]",""},
  45. {"{ \"foo\": [\"bar\"] }","[ { \"op\": \"add\", \"path\": \"/foo/-\", \"value\": [\"abc\", \"def\"] }]","{\"foo\": [\"bar\", [\"abc\", \"def\"]] }"}
  46. };
  47. /* JSON Apply Merge tests: */
  48. const char *merges[15][3] =
  49. {
  50. {"{\"a\":\"b\"}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"},
  51. {"{\"a\":\"b\"}", "{\"b\":\"c\"}", "{\"a\":\"b\",\"b\":\"c\"}"},
  52. {"{\"a\":\"b\"}", "{\"a\":null}", "{}"},
  53. {"{\"a\":\"b\",\"b\":\"c\"}", "{\"a\":null}", "{\"b\":\"c\"}"},
  54. {"{\"a\":[\"b\"]}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"},
  55. {"{\"a\":\"c\"}", "{\"a\":[\"b\"]}", "{\"a\":[\"b\"]}"},
  56. {"{\"a\":{\"b\":\"c\"}}", "{\"a\":{\"b\":\"d\",\"c\":null}}", "{\"a\":{\"b\":\"d\"}}"},
  57. {"{\"a\":[{\"b\":\"c\"}]}", "{\"a\":[1]}", "{\"a\":[1]}"},
  58. {"[\"a\",\"b\"]", "[\"c\",\"d\"]", "[\"c\",\"d\"]"},
  59. {"{\"a\":\"b\"}", "[\"c\"]", "[\"c\"]"},
  60. {"{\"a\":\"foo\"}", "null", "null"},
  61. {"{\"a\":\"foo\"}", "\"bar\"", "\"bar\""},
  62. {"{\"e\":null}", "{\"a\":1}", "{\"e\":null,\"a\":1}"},
  63. {"[1,2]", "{\"a\":\"b\",\"c\":null}", "{\"a\":\"b\"}"},
  64. {"{}","{\"a\":{\"bb\":{\"ccc\":null}}}", "{\"a\":{\"bb\":{}}}"}
  65. };
  66. /* Misc tests */
  67. int numbers[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  68. const char *random = "QWERTYUIOPASDFGHJKLZXCVBNM";
  69. char buf[2] = {0,0};
  70. char *before = NULL;
  71. char *after = NULL;
  72. cJSON *object = NULL;
  73. cJSON *nums = NULL;
  74. cJSON *num6 = NULL;
  75. cJSON *sortme = NULL;
  76. printf("JSON Pointer Tests\n");
  77. root = cJSON_Parse(json);
  78. for (i = 0; i < 12; i++)
  79. {
  80. char *output = cJSON_Print(cJSONUtils_GetPointer(root, tests[i]));
  81. printf("Test %d:\n%s\n\n", i + 1, output);
  82. free(output);
  83. }
  84. cJSON_Delete(root);
  85. printf("JSON Apply Patch Tests\n");
  86. for (i = 0; i < 15; i++)
  87. {
  88. cJSON *object = cJSON_Parse(patches[i][0]);
  89. cJSON *patch = cJSON_Parse(patches[i][1]);
  90. int err = cJSONUtils_ApplyPatches(object, patch);
  91. char *output = cJSON_Print(object);
  92. printf("Test %d (err %d):\n%s\n\n", i + 1, err, output);
  93. free(output);
  94. cJSON_Delete(object);
  95. cJSON_Delete(patch);
  96. }
  97. /* JSON Generate Patch tests: */
  98. printf("JSON Generate Patch Tests\n");
  99. for (i = 0; i < 15; i++)
  100. {
  101. cJSON *from;
  102. cJSON *to;
  103. cJSON *patch;
  104. char *out;
  105. if (!strlen(patches[i][2]))
  106. {
  107. continue;
  108. }
  109. from = cJSON_Parse(patches[i][0]);
  110. to = cJSON_Parse(patches[i][2]);
  111. patch = cJSONUtils_GeneratePatches(from, to);
  112. out = cJSON_Print(patch);
  113. printf("Test %d: (patch: %s):\n%s\n\n", i + 1, patches[i][1], out);
  114. free(out);
  115. cJSON_Delete(from);
  116. cJSON_Delete(to);
  117. cJSON_Delete(patch);
  118. }
  119. /* Misc tests: */
  120. printf("JSON Pointer construct\n");
  121. object = cJSON_CreateObject();
  122. nums = cJSON_CreateIntArray(numbers, 10);
  123. num6 = cJSON_GetArrayItem(nums, 6);
  124. cJSON_AddItemToObject(object, "numbers", nums);
  125. temp = cJSONUtils_FindPointerFromObjectTo(object, num6);
  126. printf("Pointer: [%s]\n", temp);
  127. free(temp);
  128. temp = cJSONUtils_FindPointerFromObjectTo(object, nums);
  129. printf("Pointer: [%s]\n", temp);
  130. free(temp);
  131. temp = cJSONUtils_FindPointerFromObjectTo(object, object);
  132. printf("Pointer: [%s]\n", temp);
  133. free(temp);
  134. cJSON_Delete(object);
  135. /* JSON Sort test: */
  136. sortme = cJSON_CreateObject();
  137. for (i = 0; i < 26; i++)
  138. {
  139. buf[0] = random[i];
  140. cJSON_AddItemToObject(sortme, buf, cJSON_CreateNumber(1));
  141. }
  142. before = cJSON_PrintUnformatted(sortme);
  143. cJSONUtils_SortObject(sortme);
  144. after = cJSON_PrintUnformatted(sortme);
  145. printf("Before: [%s]\nAfter: [%s]\n\n", before, after);
  146. free(before);
  147. free(after);
  148. cJSON_Delete(sortme);
  149. /* Merge tests: */
  150. printf("JSON Merge Patch tests\n");
  151. for (i = 0; i < 15; i++)
  152. {
  153. cJSON *object = cJSON_Parse(merges[i][0]);
  154. cJSON *patch = cJSON_Parse(merges[i][1]);
  155. char *before = cJSON_PrintUnformatted(object);
  156. patchtext = cJSON_PrintUnformatted(patch);
  157. printf("Before: [%s] -> [%s] = ", before, patchtext);
  158. object = cJSONUtils_MergePatch(object, patch);
  159. after = cJSON_PrintUnformatted(object);
  160. printf("[%s] vs [%s] (%s)\n", after, merges[i][2], strcmp(after, merges[i][2]) ? "FAIL" : "OK");
  161. free(before);
  162. free(patchtext);
  163. free(after);
  164. cJSON_Delete(object);
  165. cJSON_Delete(patch);
  166. }
  167. /* Generate Merge tests: */
  168. for (i = 0; i < 15; i++)
  169. {
  170. cJSON *from = cJSON_Parse(merges[i][0]);
  171. cJSON *to = cJSON_Parse(merges[i][2]);
  172. cJSON *patch = cJSONUtils_GenerateMergePatch(from,to);
  173. from = cJSONUtils_MergePatch(from,patch);
  174. patchtext = cJSON_PrintUnformatted(patch);
  175. patchedtext = cJSON_PrintUnformatted(from);
  176. printf("Patch [%s] vs [%s] = [%s] vs [%s] (%s)\n", patchtext, merges[i][1], patchedtext, merges[i][2], strcmp(patchedtext, merges[i][2]) ? "FAIL" : "OK");
  177. cJSON_Delete(from);
  178. cJSON_Delete(to);
  179. cJSON_Delete(patch);
  180. free(patchtext);
  181. free(patchedtext);
  182. }
  183. return 0;
  184. }