test_utils.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "cJSON_Utils.h"
  5. int main()
  6. {
  7. // JSON Pointer tests:
  8. const char *json="{"
  9. "\"foo\": [\"bar\", \"baz\"],"
  10. "\"\": 0,"
  11. "\"a/b\": 1,"
  12. "\"c%d\": 2,"
  13. "\"e^f\": 3,"
  14. "\"g|h\": 4,"
  15. "\"i\\\\j\": 5,"
  16. "\"k\\\"l\": 6,"
  17. "\" \": 7,"
  18. "\"m~n\": 8"
  19. "}";
  20. const char *tests[12]={"","/foo","/foo/0","/","/a~1b","/c%d","/e^f","/g|h","/i\\j","/k\"l","/ ","/m~0n"};
  21. printf("JSON Pointer Tests\n");
  22. cJSON *root=cJSON_Parse(json);
  23. for (int i=0;i<12;i++)
  24. {
  25. char *output=cJSON_Print(cJSONUtils_GetPointer(root,tests[i]));
  26. printf("Test %d:\n%s\n\n",i+1,output);
  27. free(output);
  28. }
  29. cJSON_Delete(root);
  30. // JSON Apply Patch tests:
  31. const char *patches[15][3]={
  32. {"{ \"foo\": \"bar\"}", "[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\" }]","{\"baz\": \"qux\",\"foo\": \"bar\"}"},
  33. {"{ \"foo\": [ \"bar\", \"baz\" ] }", "[{ \"op\": \"add\", \"path\": \"/foo/1\", \"value\": \"qux\" }]","{\"foo\": [ \"bar\", \"qux\", \"baz\" ] }"},
  34. {"{\"baz\": \"qux\",\"foo\": \"bar\"}"," [{ \"op\": \"remove\", \"path\": \"/baz\" }]","{\"foo\": \"bar\" }"},
  35. {"{ \"foo\": [ \"bar\", \"qux\", \"baz\" ] }","[{ \"op\": \"remove\", \"path\": \"/foo/1\" }]","{\"foo\": [ \"bar\", \"baz\" ] }"},
  36. {"{ \"baz\": \"qux\",\"foo\": \"bar\"}","[{ \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" }]","{\"baz\": \"boo\",\"foo\": \"bar\"}"},
  37. {"{\"foo\": {\"bar\": \"baz\",\"waldo\": \"fred\"},\"qux\": {\"corge\": \"grault\"}}","[{ \"op\": \"move\", \"from\": \"/foo/waldo\", \"path\": \"/qux/thud\" }]","{\"foo\": {\"bar\": \"baz\"},\"qux\": {\"corge\": \"grault\",\"thud\": \"fred\"}}"},
  38. {"{ \"foo\": [ \"all\", \"grass\", \"cows\", \"eat\" ] }","[ { \"op\": \"move\", \"from\": \"/foo/1\", \"path\": \"/foo/3\" }]","{ \"foo\": [ \"all\", \"cows\", \"eat\", \"grass\" ] }"},
  39. {"{\"baz\": \"qux\",\"foo\": [ \"a\", 2, \"c\" ]}","[{ \"op\": \"test\", \"path\": \"/baz\", \"value\": \"qux\" },{ \"op\": \"test\", \"path\": \"/foo/1\", \"value\": 2 }]",""},
  40. {"{ \"baz\": \"qux\" }","[ { \"op\": \"test\", \"path\": \"/baz\", \"value\": \"bar\" }]",""},
  41. {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/child\", \"value\": { \"grandchild\": { } } }]","{\"foo\": \"bar\",\"child\": {\"grandchild\": {}}}"},
  42. {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\", \"xyz\": 123 }]","{\"foo\": \"bar\",\"baz\": \"qux\"}"},
  43. {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz/bat\", \"value\": \"qux\" }]",""},
  44. {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": 10}]",""},
  45. {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": \"10\"}]",""},
  46. {"{ \"foo\": [\"bar\"] }","[ { \"op\": \"add\", \"path\": \"/foo/-\", \"value\": [\"abc\", \"def\"] }]","{\"foo\": [\"bar\", [\"abc\", \"def\"]] }"}};
  47. printf("JSON Apply Patch Tests\n");
  48. for (int i=0;i<15;i++)
  49. {
  50. cJSON *object=cJSON_Parse(patches[i][0]);
  51. cJSON *patch=cJSON_Parse(patches[i][1]);
  52. int err=cJSONUtils_ApplyPatches(object,patch);
  53. char *output=cJSON_Print(object);
  54. printf("Test %d (err %d):\n%s\n\n",i+1,err,output);
  55. free(output);cJSON_Delete(object);cJSON_Delete(patch);
  56. }
  57. // JSON Generate Patch tests:
  58. printf("JSON Generate Patch Tests\n");
  59. for (int i=0;i<15;i++)
  60. {
  61. if (!strlen(patches[i][2])) continue;
  62. cJSON *from=cJSON_Parse(patches[i][0]);
  63. cJSON *to=cJSON_Parse(patches[i][2]);
  64. cJSON *patch=cJSONUtils_GeneratePatches(from,to);
  65. char *out=cJSON_Print(patch);
  66. printf("Test %d: (patch: %s):\n%s\n\n",i+1,patches[i][1],out);
  67. free(out);cJSON_Delete(from);cJSON_Delete(to);cJSON_Delete(patch);
  68. }
  69. // Misc tests:
  70. printf("JSON Pointer construct\n");
  71. int numbers[10]={0,1,2,3,4,5,6,7,8,9};
  72. cJSON *object=cJSON_CreateObject();
  73. cJSON *nums=cJSON_CreateIntArray(numbers,10);
  74. cJSON *num6=cJSON_GetArrayItem(nums,6);
  75. cJSON_AddItemToObject(object,"numbers",nums);
  76. printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,num6));
  77. printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,nums));
  78. printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,object));
  79. }