test_utils.c 6.7 KB

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