test_utils.c 6.8 KB

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