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