cJSON_Utils tests: fix formatting

This commit is contained in:
Max Bruckner 2016-11-19 17:50:01 +07:00
parent 679004914f
commit 65ac0a1a2b

View File

@ -5,167 +5,202 @@
int main(void) int main(void)
{ {
/* Some variables */ /* Some variables */
char *temp = NULL; char *temp = NULL;
char *patchtext = NULL; char *patchtext = NULL;
char *patchedtext = NULL; char *patchedtext = NULL;
int i = 0; int i = 0;
/* JSON Pointer tests: */ /* JSON Pointer tests: */
cJSON *root = NULL; cJSON *root = NULL;
const char *json="{" const char *json=
"\"foo\": [\"bar\", \"baz\"]," "{"
"\"\": 0," "\"foo\": [\"bar\", \"baz\"],"
"\"a/b\": 1," "\"\": 0,"
"\"c%d\": 2," "\"a/b\": 1,"
"\"e^f\": 3," "\"c%d\": 2,"
"\"g|h\": 4," "\"e^f\": 3,"
"\"i\\\\j\": 5," "\"g|h\": 4,"
"\"k\\\"l\": 6," "\"i\\\\j\": 5,"
"\" \": 7," "\"k\\\"l\": 6,"
"\"m~n\": 8" "\" \": 7,"
"}"; "\"m~n\": 8"
"}";
const char *tests[12]={"","/foo","/foo/0","/","/a~1b","/c%d","/e^f","/g|h","/i\\j","/k\"l","/ ","/m~0n"}; const char *tests[12] = {"","/foo","/foo/0","/","/a~1b","/c%d","/e^f","/g|h","/i\\j","/k\"l","/ ","/m~0n"};
/* JSON Apply Patch tests: */ /* JSON Apply Patch tests: */
const char *patches[15][3]={ const char *patches[15][3] =
{"{ \"foo\": \"bar\"}", "[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\" }]","{\"baz\": \"qux\",\"foo\": \"bar\"}"}, {
{"{ \"foo\": [ \"bar\", \"baz\" ] }", "[{ \"op\": \"add\", \"path\": \"/foo/1\", \"value\": \"qux\" }]","{\"foo\": [ \"bar\", \"qux\", \"baz\" ] }"}, {"{ \"foo\": \"bar\"}", "[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\" }]","{\"baz\": \"qux\",\"foo\": \"bar\"}"},
{"{\"baz\": \"qux\",\"foo\": \"bar\"}"," [{ \"op\": \"remove\", \"path\": \"/baz\" }]","{\"foo\": \"bar\" }"}, {"{ \"foo\": [ \"bar\", \"baz\" ] }", "[{ \"op\": \"add\", \"path\": \"/foo/1\", \"value\": \"qux\" }]","{\"foo\": [ \"bar\", \"qux\", \"baz\" ] }"},
{"{ \"foo\": [ \"bar\", \"qux\", \"baz\" ] }","[{ \"op\": \"remove\", \"path\": \"/foo/1\" }]","{\"foo\": [ \"bar\", \"baz\" ] }"}, {"{\"baz\": \"qux\",\"foo\": \"bar\"}"," [{ \"op\": \"remove\", \"path\": \"/baz\" }]","{\"foo\": \"bar\" }"},
{"{ \"baz\": \"qux\",\"foo\": \"bar\"}","[{ \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" }]","{\"baz\": \"boo\",\"foo\": \"bar\"}"}, {"{ \"foo\": [ \"bar\", \"qux\", \"baz\" ] }","[{ \"op\": \"remove\", \"path\": \"/foo/1\" }]","{\"foo\": [ \"bar\", \"baz\" ] }"},
{"{\"foo\": {\"bar\": \"baz\",\"waldo\": \"fred\"},\"qux\": {\"corge\": \"grault\"}}","[{ \"op\": \"move\", \"from\": \"/foo/waldo\", \"path\": \"/qux/thud\" }]","{\"foo\": {\"bar\": \"baz\"},\"qux\": {\"corge\": \"grault\",\"thud\": \"fred\"}}"}, {"{ \"baz\": \"qux\",\"foo\": \"bar\"}","[{ \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" }]","{\"baz\": \"boo\",\"foo\": \"bar\"}"},
{"{ \"foo\": [ \"all\", \"grass\", \"cows\", \"eat\" ] }","[ { \"op\": \"move\", \"from\": \"/foo/1\", \"path\": \"/foo/3\" }]","{ \"foo\": [ \"all\", \"cows\", \"eat\", \"grass\" ] }"}, {"{\"foo\": {\"bar\": \"baz\",\"waldo\": \"fred\"},\"qux\": {\"corge\": \"grault\"}}","[{ \"op\": \"move\", \"from\": \"/foo/waldo\", \"path\": \"/qux/thud\" }]","{\"foo\": {\"bar\": \"baz\"},\"qux\": {\"corge\": \"grault\",\"thud\": \"fred\"}}"},
{"{\"baz\": \"qux\",\"foo\": [ \"a\", 2, \"c\" ]}","[{ \"op\": \"test\", \"path\": \"/baz\", \"value\": \"qux\" },{ \"op\": \"test\", \"path\": \"/foo/1\", \"value\": 2 }]",""}, {"{ \"foo\": [ \"all\", \"grass\", \"cows\", \"eat\" ] }","[ { \"op\": \"move\", \"from\": \"/foo/1\", \"path\": \"/foo/3\" }]","{ \"foo\": [ \"all\", \"cows\", \"eat\", \"grass\" ] }"},
{"{ \"baz\": \"qux\" }","[ { \"op\": \"test\", \"path\": \"/baz\", \"value\": \"bar\" }]",""}, {"{\"baz\": \"qux\",\"foo\": [ \"a\", 2, \"c\" ]}","[{ \"op\": \"test\", \"path\": \"/baz\", \"value\": \"qux\" },{ \"op\": \"test\", \"path\": \"/foo/1\", \"value\": 2 }]",""},
{"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/child\", \"value\": { \"grandchild\": { } } }]","{\"foo\": \"bar\",\"child\": {\"grandchild\": {}}}"}, {"{ \"baz\": \"qux\" }","[ { \"op\": \"test\", \"path\": \"/baz\", \"value\": \"bar\" }]",""},
{"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\", \"xyz\": 123 }]","{\"foo\": \"bar\",\"baz\": \"qux\"}"}, {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/child\", \"value\": { \"grandchild\": { } } }]","{\"foo\": \"bar\",\"child\": {\"grandchild\": {}}}"},
{"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz/bat\", \"value\": \"qux\" }]",""}, {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\", \"xyz\": 123 }]","{\"foo\": \"bar\",\"baz\": \"qux\"}"},
{"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": 10}]",""}, {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz/bat\", \"value\": \"qux\" }]",""},
{"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": \"10\"}]",""}, {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": 10}]",""},
{"{ \"foo\": [\"bar\"] }","[ { \"op\": \"add\", \"path\": \"/foo/-\", \"value\": [\"abc\", \"def\"] }]","{\"foo\": [\"bar\", [\"abc\", \"def\"]] }"}}; {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": \"10\"}]",""},
{"{ \"foo\": [\"bar\"] }","[ { \"op\": \"add\", \"path\": \"/foo/-\", \"value\": [\"abc\", \"def\"] }]","{\"foo\": [\"bar\", [\"abc\", \"def\"]] }"}
};
/* JSON Apply Merge tests: */ /* JSON Apply Merge tests: */
const char *merges[15][3]={ const char *merges[15][3] =
{"{\"a\":\"b\"}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"}, {
{"{\"a\":\"b\"}", "{\"b\":\"c\"}", "{\"a\":\"b\",\"b\":\"c\"}"}, {"{\"a\":\"b\"}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"},
{"{\"a\":\"b\"}", "{\"a\":null}", "{}"}, {"{\"a\":\"b\"}", "{\"b\":\"c\"}", "{\"a\":\"b\",\"b\":\"c\"}"},
{"{\"a\":\"b\",\"b\":\"c\"}", "{\"a\":null}", "{\"b\":\"c\"}"}, {"{\"a\":\"b\"}", "{\"a\":null}", "{}"},
{"{\"a\":[\"b\"]}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"}, {"{\"a\":\"b\",\"b\":\"c\"}", "{\"a\":null}", "{\"b\":\"c\"}"},
{"{\"a\":\"c\"}", "{\"a\":[\"b\"]}", "{\"a\":[\"b\"]}"}, {"{\"a\":[\"b\"]}", "{\"a\":\"c\"}", "{\"a\":\"c\"}"},
{"{\"a\":{\"b\":\"c\"}}", "{\"a\":{\"b\":\"d\",\"c\":null}}", "{\"a\":{\"b\":\"d\"}}"}, {"{\"a\":\"c\"}", "{\"a\":[\"b\"]}", "{\"a\":[\"b\"]}"},
{"{\"a\":[{\"b\":\"c\"}]}", "{\"a\":[1]}", "{\"a\":[1]}"}, {"{\"a\":{\"b\":\"c\"}}", "{\"a\":{\"b\":\"d\",\"c\":null}}", "{\"a\":{\"b\":\"d\"}}"},
{"[\"a\",\"b\"]", "[\"c\",\"d\"]", "[\"c\",\"d\"]"}, {"{\"a\":[{\"b\":\"c\"}]}", "{\"a\":[1]}", "{\"a\":[1]}"},
{"{\"a\":\"b\"}", "[\"c\"]", "[\"c\"]"}, {"[\"a\",\"b\"]", "[\"c\",\"d\"]", "[\"c\",\"d\"]"},
{"{\"a\":\"foo\"}", "null", "null"}, {"{\"a\":\"b\"}", "[\"c\"]", "[\"c\"]"},
{"{\"a\":\"foo\"}", "\"bar\"", "\"bar\""}, {"{\"a\":\"foo\"}", "null", "null"},
{"{\"e\":null}", "{\"a\":1}", "{\"e\":null,\"a\":1}"}, {"{\"a\":\"foo\"}", "\"bar\"", "\"bar\""},
{"[1,2]", "{\"a\":\"b\",\"c\":null}", "{\"a\":\"b\"}"}, {"{\"e\":null}", "{\"a\":1}", "{\"e\":null,\"a\":1}"},
{"{}","{\"a\":{\"bb\":{\"ccc\":null}}}", "{\"a\":{\"bb\":{}}}"}}; {"[1,2]", "{\"a\":\"b\",\"c\":null}", "{\"a\":\"b\"}"},
{"{}","{\"a\":{\"bb\":{\"ccc\":null}}}", "{\"a\":{\"bb\":{}}}"}
};
/* Misc tests */ /* Misc tests */
int numbers[10]={0,1,2,3,4,5,6,7,8,9}; int numbers[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
const char *random="QWERTYUIOPASDFGHJKLZXCVBNM"; const char *random = "QWERTYUIOPASDFGHJKLZXCVBNM";
char buf[2]={0,0}, *before = NULL,*after = NULL; char buf[2] = {0,0};
cJSON *object = NULL, *nums = NULL, *num6 = NULL, *sortme = NULL; char *before = NULL;
char *after = NULL;
cJSON *object = NULL;
cJSON *nums = NULL;
cJSON *num6 = NULL;
cJSON *sortme = NULL;
printf("JSON Pointer Tests\n");
printf("JSON Pointer Tests\n"); root = cJSON_Parse(json);
root=cJSON_Parse(json); for (i = 0; i < 12; i++)
for (i=0;i<12;i++) {
{ char *output = cJSON_Print(cJSONUtils_GetPointer(root, tests[i]));
char *output=cJSON_Print(cJSONUtils_GetPointer(root,tests[i])); printf("Test %d:\n%s\n\n", i + 1, output);
printf("Test %d:\n%s\n\n",i+1,output); free(output);
free(output); }
} cJSON_Delete(root);
cJSON_Delete(root);
printf("JSON Apply Patch Tests\n"); printf("JSON Apply Patch Tests\n");
for (i=0;i<15;i++) for (i = 0; i < 15; i++)
{ {
cJSON *object=cJSON_Parse(patches[i][0]); cJSON *object = cJSON_Parse(patches[i][0]);
cJSON *patch=cJSON_Parse(patches[i][1]); cJSON *patch = cJSON_Parse(patches[i][1]);
int err=cJSONUtils_ApplyPatches(object,patch); int err = cJSONUtils_ApplyPatches(object, patch);
char *output=cJSON_Print(object); char *output = cJSON_Print(object);
printf("Test %d (err %d):\n%s\n\n",i+1,err,output); printf("Test %d (err %d):\n%s\n\n", i + 1, err, output);
free(output);cJSON_Delete(object);cJSON_Delete(patch);
}
/* JSON Generate Patch tests: */ free(output);
printf("JSON Generate Patch Tests\n"); cJSON_Delete(object);
for (i=0;i<15;i++) cJSON_Delete(patch);
{ }
cJSON *from,*to,*patch;char *out;
if (!strlen(patches[i][2])) continue;
from=cJSON_Parse(patches[i][0]);
to=cJSON_Parse(patches[i][2]);
patch=cJSONUtils_GeneratePatches(from,to);
out=cJSON_Print(patch);
printf("Test %d: (patch: %s):\n%s\n\n",i+1,patches[i][1],out);
free(out);cJSON_Delete(from);cJSON_Delete(to);cJSON_Delete(patch);
}
/* Misc tests: */ /* JSON Generate Patch tests: */
printf("JSON Pointer construct\n"); printf("JSON Generate Patch Tests\n");
object=cJSON_CreateObject(); for (i = 0; i < 15; i++)
nums=cJSON_CreateIntArray(numbers,10); {
num6=cJSON_GetArrayItem(nums,6); cJSON *from;
cJSON_AddItemToObject(object,"numbers",nums); cJSON *to;
temp=cJSONUtils_FindPointerFromObjectTo(object,num6); cJSON *patch;
printf("Pointer: [%s]\n",temp); char *out;
free(temp); if (!strlen(patches[i][2]))
temp=cJSONUtils_FindPointerFromObjectTo(object,nums); {
printf("Pointer: [%s]\n",temp); continue;
free(temp); }
temp=cJSONUtils_FindPointerFromObjectTo(object,object); from = cJSON_Parse(patches[i][0]);
printf("Pointer: [%s]\n",temp); to = cJSON_Parse(patches[i][2]);
free(temp); patch = cJSONUtils_GeneratePatches(from, to);
cJSON_Delete(object); out = cJSON_Print(patch);
printf("Test %d: (patch: %s):\n%s\n\n", i + 1, patches[i][1], out);
/* JSON Sort test: */ free(out);
sortme=cJSON_CreateObject(); cJSON_Delete(from);
for (i=0;i<26;i++) cJSON_Delete(to);
{ cJSON_Delete(patch);
buf[0]=random[i];cJSON_AddItemToObject(sortme,buf,cJSON_CreateNumber(1)); }
}
before=cJSON_PrintUnformatted(sortme);
cJSONUtils_SortObject(sortme);
after=cJSON_PrintUnformatted(sortme);
printf("Before: [%s]\nAfter: [%s]\n\n",before,after);
free(before);free(after);cJSON_Delete(sortme);
/* Merge tests: */ /* Misc tests: */
printf("JSON Merge Patch tests\n"); printf("JSON Pointer construct\n");
for (i=0;i<15;i++) object = cJSON_CreateObject();
{ nums = cJSON_CreateIntArray(numbers, 10);
cJSON *object=cJSON_Parse(merges[i][0]); num6 = cJSON_GetArrayItem(nums, 6);
cJSON *patch=cJSON_Parse(merges[i][1]); cJSON_AddItemToObject(object, "numbers", nums);
char *before=cJSON_PrintUnformatted(object); temp = cJSONUtils_FindPointerFromObjectTo(object, num6);
patchtext=cJSON_PrintUnformatted(patch); printf("Pointer: [%s]\n", temp);
printf("Before: [%s] -> [%s] = ",before,patchtext); free(temp);
object=cJSONUtils_MergePatch(object,patch); temp = cJSONUtils_FindPointerFromObjectTo(object, nums);
after=cJSON_PrintUnformatted(object); printf("Pointer: [%s]\n", temp);
printf("[%s] vs [%s] (%s)\n",after,merges[i][2],strcmp(after,merges[i][2])?"FAIL":"OK"); free(temp);
temp = cJSONUtils_FindPointerFromObjectTo(object, object);
printf("Pointer: [%s]\n", temp);
free(temp);
cJSON_Delete(object);
free(before);free(patchtext);free(after);cJSON_Delete(object);cJSON_Delete(patch); /* JSON Sort test: */
} sortme = cJSON_CreateObject();
for (i = 0; i < 26; i++)
{
buf[0] = random[i];
cJSON_AddItemToObject(sortme, buf, cJSON_CreateNumber(1));
}
before = cJSON_PrintUnformatted(sortme);
cJSONUtils_SortObject(sortme);
after = cJSON_PrintUnformatted(sortme);
printf("Before: [%s]\nAfter: [%s]\n\n", before, after);
/* Generate Merge tests: */ free(before);
for (i=0;i<15;i++) free(after);
{ cJSON_Delete(sortme);
cJSON *from=cJSON_Parse(merges[i][0]);
cJSON *to=cJSON_Parse(merges[i][2]);
cJSON *patch=cJSONUtils_GenerateMergePatch(from,to);
from=cJSONUtils_MergePatch(from,patch);
patchtext=cJSON_PrintUnformatted(patch);
patchedtext=cJSON_PrintUnformatted(from);
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");
cJSON_Delete(from);cJSON_Delete(to);cJSON_Delete(patch);free(patchtext);free(patchedtext);
}
return 0; /* Merge tests: */
printf("JSON Merge Patch tests\n");
for (i = 0; i < 15; i++)
{
cJSON *object = cJSON_Parse(merges[i][0]);
cJSON *patch = cJSON_Parse(merges[i][1]);
char *before = cJSON_PrintUnformatted(object);
patchtext = cJSON_PrintUnformatted(patch);
printf("Before: [%s] -> [%s] = ", before, patchtext);
object = cJSONUtils_MergePatch(object, patch);
after = cJSON_PrintUnformatted(object);
printf("[%s] vs [%s] (%s)\n", after, merges[i][2], strcmp(after, merges[i][2]) ? "FAIL" : "OK");
free(before);
free(patchtext);
free(after);
cJSON_Delete(object);
cJSON_Delete(patch);
}
/* Generate Merge tests: */
for (i = 0; i < 15; i++)
{
cJSON *from = cJSON_Parse(merges[i][0]);
cJSON *to = cJSON_Parse(merges[i][2]);
cJSON *patch = cJSONUtils_GenerateMergePatch(from,to);
from = cJSONUtils_MergePatch(from,patch);
patchtext = cJSON_PrintUnformatted(patch);
patchedtext = cJSON_PrintUnformatted(from);
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");
cJSON_Delete(from);
cJSON_Delete(to);
cJSON_Delete(patch);
free(patchtext);
free(patchedtext);
}
return 0;
} }