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

@ -13,7 +13,8 @@ int main(void)
int i = 0;
/* JSON Pointer tests: */
cJSON *root = NULL;
const char *json="{"
const char *json=
"{"
"\"foo\": [\"bar\", \"baz\"],"
"\"\": 0,"
"\"a/b\": 1,"
@ -29,7 +30,8 @@ int main(void)
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: */
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\" ] }"},
{"{\"baz\": \"qux\",\"foo\": \"bar\"}"," [{ \"op\": \"remove\", \"path\": \"/baz\" }]","{\"foo\": \"bar\" }"},
@ -44,10 +46,12 @@ int main(void)
{"{ \"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\"]] }"}};
{"{ \"foo\": [\"bar\"] }","[ { \"op\": \"add\", \"path\": \"/foo/-\", \"value\": [\"abc\", \"def\"] }]","{\"foo\": [\"bar\", [\"abc\", \"def\"]] }"}
};
/* 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\":null}", "{}"},
@ -62,15 +66,20 @@ int main(void)
{"{\"a\":\"foo\"}", "\"bar\"", "\"bar\""},
{"{\"e\":null}", "{\"a\":1}", "{\"e\":null,\"a\":1}"},
{"[1,2]", "{\"a\":\"b\",\"c\":null}", "{\"a\":\"b\"}"},
{"{}","{\"a\":{\"bb\":{\"ccc\":null}}}", "{\"a\":{\"bb\":{}}}"}};
{"{}","{\"a\":{\"bb\":{\"ccc\":null}}}", "{\"a\":{\"bb\":{}}}"}
};
/* Misc tests */
int numbers[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
const char *random = "QWERTYUIOPASDFGHJKLZXCVBNM";
char buf[2]={0,0}, *before = NULL,*after = NULL;
cJSON *object = NULL, *nums = NULL, *num6 = NULL, *sortme = NULL;
char buf[2] = {0,0};
char *before = NULL;
char *after = NULL;
cJSON *object = NULL;
cJSON *nums = NULL;
cJSON *num6 = NULL;
cJSON *sortme = NULL;
printf("JSON Pointer Tests\n");
@ -92,21 +101,34 @@ int main(void)
int err = cJSONUtils_ApplyPatches(object, patch);
char *output = cJSON_Print(object);
printf("Test %d (err %d):\n%s\n\n", i + 1, err, output);
free(output);cJSON_Delete(object);cJSON_Delete(patch);
free(output);
cJSON_Delete(object);
cJSON_Delete(patch);
}
/* JSON Generate Patch tests: */
printf("JSON Generate Patch Tests\n");
for (i = 0; i < 15; i++)
{
cJSON *from,*to,*patch;char *out;
if (!strlen(patches[i][2])) continue;
cJSON *from;
cJSON *to;
cJSON *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);
free(out);
cJSON_Delete(from);
cJSON_Delete(to);
cJSON_Delete(patch);
}
/* Misc tests: */
@ -130,13 +152,17 @@ int main(void)
sortme = cJSON_CreateObject();
for (i = 0; i < 26; i++)
{
buf[0]=random[i];cJSON_AddItemToObject(sortme,buf,cJSON_CreateNumber(1));
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);
free(before);
free(after);
cJSON_Delete(sortme);
/* Merge tests: */
printf("JSON Merge Patch tests\n");
@ -151,7 +177,11 @@ int main(void)
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);
free(before);
free(patchtext);
free(after);
cJSON_Delete(object);
cJSON_Delete(patch);
}
/* Generate Merge tests: */
@ -164,7 +194,12 @@ int main(void)
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);
cJSON_Delete(from);
cJSON_Delete(to);
cJSON_Delete(patch);
free(patchtext);
free(patchedtext);
}
return 0;