test_utils.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "cJSON_Utils.h"
  4. int main()
  5. {
  6. // JSON Pointer tests:
  7. const char *json="{"
  8. "\"foo\": [\"bar\", \"baz\"],"
  9. "\"\": 0,"
  10. "\"a/b\": 1,"
  11. "\"c%d\": 2,"
  12. "\"e^f\": 3,"
  13. "\"g|h\": 4,"
  14. "\"i\\\\j\": 5,"
  15. "\"k\\\"l\": 6,"
  16. "\" \": 7,"
  17. "\"m~n\": 8"
  18. "}";
  19. const char *tests[12]={"","/foo","/foo/0","/","/a~1b","/c%d","/e^f","/g|h","/i\\j","/k\"l","/ ","/m~0n"};
  20. printf("JSON Pointer Tests\n");
  21. cJSON *root=cJSON_Parse(json);
  22. for (int i=0;i<12;i++)
  23. {
  24. char *output=cJSON_Print(cJSONUtils_GetPointer(root,tests[i]));
  25. printf("Test %d:\n%s\n\n",i+1,output);
  26. free(output);
  27. }
  28. cJSON_Delete(root);
  29. // JSON Patch tests:
  30. const char *patches[15][2]={
  31. {"{ \"foo\": \"bar\"}", "[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\" }]"},
  32. {"{ \"foo\": [ \"bar\", \"baz\" ] }", "[{ \"op\": \"add\", \"path\": \"/foo/1\", \"value\": \"qux\" }]"},
  33. {"{\"baz\": \"qux\",\"foo\": \"bar\"}"," [{ \"op\": \"remove\", \"path\": \"/baz\" }]"},
  34. {"{ \"foo\": [ \"bar\", \"qux\", \"baz\" ] }","[{ \"op\": \"remove\", \"path\": \"/foo/1\" }]"},
  35. {"{ \"baz\": \"qux\",\"foo\": \"bar\"}","[{ \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" }]"},
  36. {"{\"foo\": {\"bar\": \"baz\",\"waldo\": \"fred\"},\"qux\": {\"corge\": \"grault\"}}","[{ \"op\": \"move\", \"from\": \"/foo/waldo\", \"path\": \"/qux/thud\" }]"},
  37. {"{ \"foo\": [ \"all\", \"grass\", \"cows\", \"eat\" ] }","[ { \"op\": \"move\", \"from\": \"/foo/1\", \"path\": \"/foo/3\" }]"},
  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\": { } } }]"},
  41. {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\", \"xyz\": 123 }]"},
  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\"] }]"}};
  46. printf("JSON Patch Tests\n");
  47. for (int i=0;i<15;i++)
  48. {
  49. cJSON *object=cJSON_Parse(patches[i][0]);
  50. cJSON *patch=cJSON_Parse(patches[i][1]);
  51. int err=cJSONUtils_ApplyPatches(object,patch);
  52. char *output=cJSON_Print(object);
  53. printf("Test %d (err %d):\n%s\n\n",i+1,err,output);
  54. free(output);
  55. }
  56. }