test_utils.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /* Misc tests */
  41. int numbers[10]={0,1,2,3,4,5,6,7,8,9};
  42. const char *random="QWERTYUIOPASDFGHJKLZXCVBNM";
  43. char buf[2]={0,0},*before,*after;
  44. cJSON *object,*nums,*num6,*sortme;
  45. printf("JSON Pointer Tests\n");
  46. root=cJSON_Parse(json);
  47. for (i=0;i<12;i++)
  48. {
  49. char *output=cJSON_Print(cJSONUtils_GetPointer(root,tests[i]));
  50. printf("Test %d:\n%s\n\n",i+1,output);
  51. free(output);
  52. }
  53. cJSON_Delete(root);
  54. printf("JSON Apply Patch Tests\n");
  55. for (i=0;i<15;i++)
  56. {
  57. cJSON *object=cJSON_Parse(patches[i][0]);
  58. cJSON *patch=cJSON_Parse(patches[i][1]);
  59. int err=cJSONUtils_ApplyPatches(object,patch);
  60. char *output=cJSON_Print(object);
  61. printf("Test %d (err %d):\n%s\n\n",i+1,err,output);
  62. free(output);cJSON_Delete(object);cJSON_Delete(patch);
  63. }
  64. /* JSON Generate Patch tests: */
  65. printf("JSON Generate Patch Tests\n");
  66. for (i=0;i<15;i++)
  67. {
  68. cJSON *from,*to,*patch;char *out;
  69. if (!strlen(patches[i][2])) continue;
  70. from=cJSON_Parse(patches[i][0]);
  71. to=cJSON_Parse(patches[i][2]);
  72. patch=cJSONUtils_GeneratePatches(from,to);
  73. out=cJSON_Print(patch);
  74. printf("Test %d: (patch: %s):\n%s\n\n",i+1,patches[i][1],out);
  75. free(out);cJSON_Delete(from);cJSON_Delete(to);cJSON_Delete(patch);
  76. }
  77. /* Misc tests: */
  78. printf("JSON Pointer construct\n");
  79. object=cJSON_CreateObject();
  80. nums=cJSON_CreateIntArray(numbers,10);
  81. num6=cJSON_GetArrayItem(nums,6);
  82. cJSON_AddItemToObject(object,"numbers",nums);
  83. printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,num6));
  84. printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,nums));
  85. printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,object));
  86. /* JSON Sort test: */
  87. sortme=cJSON_CreateObject();
  88. for (i=0;i<26;i++)
  89. {
  90. buf[0]=random[i];cJSON_AddItemToObject(sortme,buf,cJSON_CreateNumber(1));
  91. }
  92. before=cJSON_PrintUnformatted(sortme);
  93. cJSONUtils_SortObject(sortme);
  94. after=cJSON_PrintUnformatted(sortme);
  95. printf("Before: [%s]\nAfter: [%s]\n\n",before,after);
  96. free(before);free(after);cJSON_Delete(sortme);
  97. }