diff options
author | Petri Lehtinen <petri@digip.org> | 2019-10-17 09:08:51 +0300 |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2019-10-21 08:55:01 +0300 |
commit | 79fe8c343572ea6a75c754a15bea8a94c7429265 (patch) | |
tree | 7caa8d66fdee16bb744847a010e902df0eeda122 /test/suites/api/test_dump.c | |
parent | 7dc463ee4ee4d9b42ad4d914ac4caf734ecc56ae (diff) | |
download | jansson-79fe8c343572ea6a75c754a15bea8a94c7429265.zip jansson-79fe8c343572ea6a75c754a15bea8a94c7429265.tar.gz jansson-79fe8c343572ea6a75c754a15bea8a94c7429265.tar.bz2 |
Run clang-format on *.c, *.h
Diffstat (limited to 'test/suites/api/test_dump.c')
-rw-r--r-- | test/suites/api/test_dump.c | 129 |
1 files changed, 57 insertions, 72 deletions
diff --git a/test/suites/api/test_dump.c b/test/suites/api/test_dump.c index c97a105..de90dc6 100644 --- a/test/suites/api/test_dump.c +++ b/test/suites/api/test_dump.c @@ -18,39 +18,36 @@ #define pipe(fds) _pipe(fds, 1024, _O_BINARY) #endif -static int encode_null_callback(const char *buffer, size_t size, void *data) -{ +static int encode_null_callback(const char *buffer, size_t size, void *data) { (void)buffer; (void)size; (void)data; return 0; } -static void encode_null() -{ - if(json_dumps(NULL, JSON_ENCODE_ANY) != NULL) +static void encode_null() { + if (json_dumps(NULL, JSON_ENCODE_ANY) != NULL) fail("json_dumps didn't fail for NULL"); - if(json_dumpb(NULL, NULL, 0, JSON_ENCODE_ANY) != 0) + if (json_dumpb(NULL, NULL, 0, JSON_ENCODE_ANY) != 0) fail("json_dumpb didn't fail for NULL"); - if(json_dumpf(NULL, stderr, JSON_ENCODE_ANY) != -1) + if (json_dumpf(NULL, stderr, JSON_ENCODE_ANY) != -1) fail("json_dumpf didn't fail for NULL"); #ifdef HAVE_UNISTD_H - if(json_dumpfd(NULL, STDERR_FILENO, JSON_ENCODE_ANY) != -1) + if (json_dumpfd(NULL, STDERR_FILENO, JSON_ENCODE_ANY) != -1) fail("json_dumpfd didn't fail for NULL"); #endif /* Don't test json_dump_file to avoid creating a file */ - if(json_dump_callback(NULL, encode_null_callback, NULL, JSON_ENCODE_ANY) != -1) + if (json_dump_callback(NULL, encode_null_callback, NULL, JSON_ENCODE_ANY) != + -1) fail("json_dump_callback didn't fail for NULL"); } - -static void encode_twice() -{ +static void encode_twice() { /* Encode an empty object/array, add an item, encode again */ json_t *json; @@ -58,35 +55,34 @@ static void encode_twice() json = json_object(); result = json_dumps(json, 0); - if(!result || strcmp(result, "{}")) - fail("json_dumps failed"); + if (!result || strcmp(result, "{}")) + fail("json_dumps failed"); free(result); json_object_set_new(json, "foo", json_integer(5)); result = json_dumps(json, 0); - if(!result || strcmp(result, "{\"foo\": 5}")) - fail("json_dumps failed"); + if (!result || strcmp(result, "{\"foo\": 5}")) + fail("json_dumps failed"); free(result); json_decref(json); json = json_array(); result = json_dumps(json, 0); - if(!result || strcmp(result, "[]")) - fail("json_dumps failed"); + if (!result || strcmp(result, "[]")) + fail("json_dumps failed"); free(result); json_array_append_new(json, json_integer(5)); result = json_dumps(json, 0); - if(!result || strcmp(result, "[5]")) - fail("json_dumps failed"); + if (!result || strcmp(result, "[5]")) + fail("json_dumps failed"); free(result); json_decref(json); } -static void circular_references() -{ +static void circular_references() { /* Construct a JSON object/array with a circular reference: object: {"a": {"b": {"c": <circular reference to $.a>}}} @@ -104,13 +100,13 @@ static void circular_references() json_object_set(json_object_get(json_object_get(json, "a"), "b"), "c", json_object_get(json, "a")); - if(json_dumps(json, 0)) + if (json_dumps(json, 0)) fail("json_dumps encoded a circular reference!"); json_object_del(json_object_get(json_object_get(json, "a"), "b"), "c"); result = json_dumps(json, 0); - if(!result || strcmp(result, "{\"a\": {\"b\": {}}}")) + if (!result || strcmp(result, "{\"a\": {\"b\": {}}}")) fail("json_dumps failed!"); free(result); @@ -122,21 +118,20 @@ static void circular_references() json_array_append(json_array_get(json_array_get(json, 0), 0), json_array_get(json, 0)); - if(json_dumps(json, 0)) + if (json_dumps(json, 0)) fail("json_dumps encoded a circular reference!"); json_array_remove(json_array_get(json_array_get(json, 0), 0), 0); result = json_dumps(json, 0); - if(!result || strcmp(result, "[[[]]]")) + if (!result || strcmp(result, "[[[]]]")) fail("json_dumps failed!"); free(result); json_decref(json); } -static void encode_other_than_array_or_object() -{ +static void encode_other_than_array_or_object() { /* Encoding anything other than array or object should only * succeed if the JSON_ENCODE_ANY flag is used */ @@ -144,78 +139,77 @@ static void encode_other_than_array_or_object() char *result; json = json_string("foo"); - if(json_dumps(json, 0) != NULL) + if (json_dumps(json, 0) != NULL) fail("json_dumps encoded a string!"); - if(json_dumpf(json, NULL, 0) == 0) + if (json_dumpf(json, NULL, 0) == 0) fail("json_dumpf encoded a string!"); - if(json_dumpfd(json, -1, 0) == 0) + if (json_dumpfd(json, -1, 0) == 0) fail("json_dumpfd encoded a string!"); result = json_dumps(json, JSON_ENCODE_ANY); - if(!result || strcmp(result, "\"foo\"") != 0) + if (!result || strcmp(result, "\"foo\"") != 0) fail("json_dumps failed to encode a string with JSON_ENCODE_ANY"); free(result); json_decref(json); json = json_integer(42); - if(json_dumps(json, 0) != NULL) + if (json_dumps(json, 0) != NULL) fail("json_dumps encoded an integer!"); - if(json_dumpf(json, NULL, 0) == 0) + if (json_dumpf(json, NULL, 0) == 0) fail("json_dumpf encoded an integer!"); - if(json_dumpfd(json, -1, 0) == 0) + if (json_dumpfd(json, -1, 0) == 0) fail("json_dumpfd encoded an integer!"); result = json_dumps(json, JSON_ENCODE_ANY); - if(!result || strcmp(result, "42") != 0) + if (!result || strcmp(result, "42") != 0) fail("json_dumps failed to encode an integer with JSON_ENCODE_ANY"); free(result); json_decref(json); - - } -static void escape_slashes() -{ +static void escape_slashes() { /* Test dump escaping slashes */ json_t *json; char *result; json = json_object(); - json_object_set_new(json, "url", json_string("https://github.com/akheron/jansson")); + json_object_set_new(json, "url", + json_string("https://github.com/akheron/jansson")); result = json_dumps(json, 0); - if(!result || strcmp(result, "{\"url\": \"https://github.com/akheron/jansson\"}")) + if (!result || + strcmp(result, "{\"url\": \"https://github.com/akheron/jansson\"}")) fail("json_dumps failed to not escape slashes"); free(result); result = json_dumps(json, JSON_ESCAPE_SLASH); - if(!result || strcmp(result, "{\"url\": \"https:\\/\\/github.com\\/akheron\\/jansson\"}")) + if (!result || + strcmp(result, + "{\"url\": \"https:\\/\\/github.com\\/akheron\\/jansson\"}")) fail("json_dumps failed to escape slashes"); free(result); json_decref(json); } -static void encode_nul_byte() -{ +static void encode_nul_byte() { json_t *json; char *result; json = json_stringn("nul byte \0 in string", 20); result = json_dumps(json, JSON_ENCODE_ANY); - if(!result || memcmp(result, "\"nul byte \\u0000 in string\"", 27)) + if (!result || memcmp(result, "\"nul byte \\u0000 in string\"", 27)) fail("json_dumps failed to dump an embedded NUL byte"); free(result); json_decref(json); } -static void dump_file() -{ +static void dump_file() { json_t *json; int result; @@ -232,8 +226,7 @@ static void dump_file() remove("json_dump_file.json"); } -static void dumpb() -{ +static void dumpb() { char buf[2]; json_t *obj; size_t size; @@ -241,31 +234,30 @@ static void dumpb() obj = json_object(); size = json_dumpb(obj, buf, sizeof(buf), 0); - if(size != 2 || strncmp(buf, "{}", 2)) - fail("json_dumpb failed"); + if (size != 2 || strncmp(buf, "{}", 2)) + fail("json_dumpb failed"); json_decref(obj); obj = json_pack("{s:s}", "foo", "bar"); size = json_dumpb(obj, buf, sizeof(buf), JSON_COMPACT); - if(size != 13) - fail("json_dumpb size check failed"); + if (size != 13) + fail("json_dumpb size check failed"); json_decref(obj); } -static void dumpfd() -{ +static void dumpfd() { #ifdef HAVE_UNISTD_H int fds[2] = {-1, -1}; json_t *a, *b; - if(pipe(fds)) + if (pipe(fds)) fail("pipe() failed"); a = json_pack("{s:s}", "foo", "bar"); - if(json_dumpfd(a, fds[1], 0)) + if (json_dumpfd(a, fds[1], 0)) fail("json_dumpfd() failed"); close(fds[1]); @@ -282,19 +274,13 @@ static void dumpfd() #endif } -static void embed() -{ - static const char *plains[] = { - "{\"bar\":[],\"foo\":{}}", - "[[],{}]", - "{}", - "[]", - NULL - }; +static void embed() { + static const char *plains[] = {"{\"bar\":[],\"foo\":{}}", "[[],{}]", "{}", + "[]", NULL}; size_t i; - for(i = 0; plains[i]; i++) { + for (i = 0; plains[i]; i++) { const char *plain = plains[i]; json_t *parse = NULL; char *embed = NULL; @@ -307,16 +293,15 @@ static void embed() esize = json_dumpb(parse, embed, psize, JSON_COMPACT | JSON_SORT_KEYS | JSON_EMBED); json_decref(parse); - if(esize != psize) + if (esize != psize) fail("json_dumpb(JSON_EMBED) returned an invalid size"); - if(strncmp(plain + 1, embed, esize) != 0) + if (strncmp(plain + 1, embed, esize) != 0) fail("json_dumps(JSON_EMBED) returned an invalid value"); free(embed); } } -static void run_tests() -{ +static void run_tests() { encode_null(); encode_twice(); circular_references(); |