From f56c5c1a603f625d200055ee646df2d99a8a9b5e Mon Sep 17 00:00:00 2001 From: chenguoping Date: Fri, 3 Jan 2020 10:05:09 +0800 Subject: [PATCH 1/4] Increased the test coverage of json_object_iterator.c from 0% to 100% --- tests/CMakeLists.txt | 3 ++- tests/test_object_iterator.c | 40 +++++++++++++++++++++++++++++ tests/test_object_iterator.expected | 14 ++++++++++ tests/test_object_iterator.test | 1 + 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/test_object_iterator.c create mode 100644 tests/test_object_iterator.expected create mode 120000 tests/test_object_iterator.test diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index f18d9f3..a871573 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -33,7 +33,8 @@ foreach(TESTNAME test_set_serializer test_set_value test_util_file - test_visit) + test_visit + test_object_iterator) add_executable(${TESTNAME} ${TESTNAME}.c) add_test(NAME ${TESTNAME} COMMAND ${PROJECT_SOURCE_DIR}/tests/${TESTNAME}.test) diff --git a/tests/test_object_iterator.c b/tests/test_object_iterator.c new file mode 100644 index 0000000..5bc5101 --- /dev/null +++ b/tests/test_object_iterator.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include "config.h" + +#include "json_object.h" +#include "json_tokener.h" +#include "json_object_iterator.h" + +int main(int atgc, char **argv) +{ + const char *input = "{\n\ + \"string_of_digits\": \"123\",\n\ + \"regular_number\": 222,\n\ + \"decimal_number\": 99.55,\n\ + \"boolean_true\": true,\n\ + \"boolean_false\": false,\n\ + \"big_number\": 2147483649,\n\ + \"a_null\": null,\n\ + }"; + + struct json_object *new_obj; + struct json_object_iterator it; + struct json_object_iterator itEnd; + + it = json_object_iter_init_default(); + new_obj = json_tokener_parse(input); + it = json_object_iter_begin(new_obj); + itEnd = json_object_iter_end(new_obj); + + while (!json_object_iter_equal(&it,&itEnd)) { + printf("%s\n",json_object_iter_peek_name(&it)); + printf("%s\n",json_object_to_json_string(json_object_iter_peek_value(&it))); + json_object_iter_next(&it); + } + + json_object_put(new_obj); + + return 0; +} diff --git a/tests/test_object_iterator.expected b/tests/test_object_iterator.expected new file mode 100644 index 0000000..e56e288 --- /dev/null +++ b/tests/test_object_iterator.expected @@ -0,0 +1,14 @@ +string_of_digits +"123" +regular_number +222 +decimal_number +99.55 +boolean_true +true +boolean_false +false +big_number +2147483649 +a_null +null diff --git a/tests/test_object_iterator.test b/tests/test_object_iterator.test new file mode 120000 index 0000000..58a13f4 --- /dev/null +++ b/tests/test_object_iterator.test @@ -0,0 +1 @@ +test_basic.test \ No newline at end of file From 80961256182a465b75d790005fa30db9a043b885 Mon Sep 17 00:00:00 2001 From: chenguoping Date: Fri, 3 Jan 2020 10:17:05 +0800 Subject: [PATCH 2/4] Increased the test coverage of json_util.c from 76% to 90.3%. --- tests/test_util_file.c | 45 +++++++++++++++++++++++++++++++++++ tests/test_util_file.expected | 2 ++ 2 files changed, 47 insertions(+) diff --git a/tests/test_util_file.c b/tests/test_util_file.c index ebc5edb..7906157 100644 --- a/tests/test_util_file.c +++ b/tests/test_util_file.c @@ -16,6 +16,7 @@ #endif /* HAVE_UNISTD_H */ #include #include +#include #include "json.h" #include "json_util.h" @@ -28,6 +29,7 @@ static void test_read_closed(void); static void test_write_to_file(); static void stat_and_cat(const char *file); +static void test_read_fd_equal(const char *testdir); #ifndef PATH_MAX #define PATH_MAX 256 @@ -82,7 +84,21 @@ static void test_write_to_file() if (rv == 0) stat_and_cat(outfile3); + const char *outfile4 = "./test_cast.test"; + json_object_to_file_ext(outfile4, jso, JSON_C_TO_STRING_PRETTY); + json_object *new_jso = NULL; + assert(-1 == json_object_to_file(outfile4, new_jso)); + d = open(outfile4, O_WRONLY|O_CREAT, 0600); + if (d < 0) + { + printf("FAIL: unable to open %s %s\n", outfile4, strerror(errno)); + return; + } + assert(-1 == json_object_to_fd(d, new_jso, JSON_C_TO_STRING_PRETTY)); + close(d); + json_object_put(jso); + json_object_put(new_jso); } static void stat_and_cat(const char *file) @@ -125,6 +141,8 @@ int main(int argc, char **argv) // json_object_to_file(file, obj); // json_object_to_file_ext(file, obj, flags); + _json_c_strerror(0); + json_util_get_last_err(); _json_c_strerror_enable = 1; const char *testdir; @@ -157,6 +175,7 @@ int main(int argc, char **argv) test_read_nonexistant(); test_read_closed(); test_write_to_file(); + test_read_fd_equal(testdir); return EXIT_SUCCESS; } @@ -196,6 +215,7 @@ static void test_read_valid_nested_with_fd(const char *testdir) fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno)); exit(EXIT_FAILURE); } + assert(NULL == json_object_from_fd_ex(d, -2)); json_object *jso = json_object_from_fd_ex(d, 20); if (jso != NULL) { @@ -275,3 +295,28 @@ static void test_read_closed() "expecting NULL, EBADF, got:NULL, %s\n", json_util_get_last_err()); } + +static void test_read_fd_equal(const char *testdir) +{ + char filename[PATH_MAX]; + (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir); + + json_object *jso = json_object_from_file(filename); + + assert(NULL == json_type_to_name(20)); + int d = open(filename, O_RDONLY, 0); + if (d < 0) + { + fprintf(stderr, + "FAIL: unable to open %s: %s\n", + filename, strerror(errno)); + exit(EXIT_FAILURE); + } + json_object *new_jso = json_object_from_fd(d); + close(d); + + printf("OK: json_object_from_file(valid.json)=%s\n", json_object_to_json_string(jso)); + printf("OK: json_object_from_fd(valid.json)=%s\n", json_object_to_json_string(new_jso)); + json_object_put(jso); + json_object_put(new_jso); +} diff --git a/tests/test_util_file.expected b/tests/test_util_file.expected index 8d87535..f149f85 100644 --- a/tests/test_util_file.expected +++ b/tests/test_util_file.expected @@ -36,3 +36,5 @@ file[json3.out], size=703, contents={ "foo8":"abcdefghijklmnopqrstuvwxyz", "foo9":"abcdefghijklmnopqrstuvwxyz" }{"foo":1234,"foo1":"abcdefghijklmnopqrstuvwxyz","foo2":"abcdefghijklmnopqrstuvwxyz","foo3":"abcdefghijklmnopqrstuvwxyz","foo4":"abcdefghijklmnopqrstuvwxyz","foo5":"abcdefghijklmnopqrstuvwxyz","foo6":"abcdefghijklmnopqrstuvwxyz","foo7":"abcdefghijklmnopqrstuvwxyz","foo8":"abcdefghijklmnopqrstuvwxyz","foo9":"abcdefghijklmnopqrstuvwxyz"} +OK: json_object_from_file(valid.json)={ "foo": 123, "obj2": { "obj3": { "obj4": { "foo": 999 } } } } +OK: json_object_from_fd(valid.json)={ "foo": 123, "obj2": { "obj3": { "obj4": { "foo": 999 } } } } From 2876fcc1370ae87fa6f748ff0326b2bc0a7c2897 Mon Sep 17 00:00:00 2001 From: dota17 Date: Tue, 14 Apr 2020 10:15:27 +0800 Subject: [PATCH 3/4] clang-format two test_util_file.c and test_object_iterator.c --- tests/test_object_iterator.c | 15 ++++++++------- tests/test_util_file.c | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/tests/test_object_iterator.c b/tests/test_object_iterator.c index 5bc5101..da5192a 100644 --- a/tests/test_object_iterator.c +++ b/tests/test_object_iterator.c @@ -1,11 +1,11 @@ -#include -#include -#include #include "config.h" +#include +#include +#include #include "json_object.h" -#include "json_tokener.h" #include "json_object_iterator.h" +#include "json_tokener.h" int main(int atgc, char **argv) { @@ -28,9 +28,10 @@ int main(int atgc, char **argv) it = json_object_iter_begin(new_obj); itEnd = json_object_iter_end(new_obj); - while (!json_object_iter_equal(&it,&itEnd)) { - printf("%s\n",json_object_iter_peek_name(&it)); - printf("%s\n",json_object_to_json_string(json_object_iter_peek_value(&it))); + while (!json_object_iter_equal(&it, &itEnd)) + { + printf("%s\n", json_object_iter_peek_name(&it)); + printf("%s\n", json_object_to_json_string(json_object_iter_peek_value(&it))); json_object_iter_next(&it); } diff --git a/tests/test_util_file.c b/tests/test_util_file.c index 7906157..ecd1713 100644 --- a/tests/test_util_file.c +++ b/tests/test_util_file.c @@ -14,9 +14,9 @@ #if HAVE_UNISTD_H #include #endif /* HAVE_UNISTD_H */ +#include #include #include -#include #include "json.h" #include "json_util.h" @@ -88,14 +88,14 @@ static void test_write_to_file() json_object_to_file_ext(outfile4, jso, JSON_C_TO_STRING_PRETTY); json_object *new_jso = NULL; assert(-1 == json_object_to_file(outfile4, new_jso)); - d = open(outfile4, O_WRONLY|O_CREAT, 0600); + d = open(outfile4, O_WRONLY | O_CREAT, 0600); if (d < 0) { printf("FAIL: unable to open %s %s\n", outfile4, strerror(errno)); return; } assert(-1 == json_object_to_fd(d, new_jso, JSON_C_TO_STRING_PRETTY)); - close(d); + close(d); json_object_put(jso); json_object_put(new_jso); @@ -160,13 +160,15 @@ int main(int argc, char **argv) if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION))) { printf("FAIL: Output from json_c_version(): %s " - "does not match %s", json_c_version(), JSON_C_VERSION); + "does not match %s", + json_c_version(), JSON_C_VERSION); return EXIT_FAILURE; } if (json_c_version_num() != JSON_C_VERSION_NUM) { printf("FAIL: Output from json_c_version_num(): %d " - "does not match %d", json_c_version_num(), JSON_C_VERSION_NUM); + "does not match %d", + json_c_version_num(), JSON_C_VERSION_NUM); return EXIT_FAILURE; } @@ -300,16 +302,14 @@ static void test_read_fd_equal(const char *testdir) { char filename[PATH_MAX]; (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir); - + json_object *jso = json_object_from_file(filename); assert(NULL == json_type_to_name(20)); int d = open(filename, O_RDONLY, 0); if (d < 0) - { - fprintf(stderr, - "FAIL: unable to open %s: %s\n", - filename, strerror(errno)); + { + fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno)); exit(EXIT_FAILURE); } json_object *new_jso = json_object_from_fd(d); From b14363ae323b189ed2d764672c6607912ff35398 Mon Sep 17 00:00:00 2001 From: dota17 Date: Wed, 15 Apr 2020 20:06:12 +0800 Subject: [PATCH 4/4] remove unsuitable case --- tests/test_util_file.c | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/tests/test_util_file.c b/tests/test_util_file.c index ecd1713..2f8f8b5 100644 --- a/tests/test_util_file.c +++ b/tests/test_util_file.c @@ -84,21 +84,7 @@ static void test_write_to_file() if (rv == 0) stat_and_cat(outfile3); - const char *outfile4 = "./test_cast.test"; - json_object_to_file_ext(outfile4, jso, JSON_C_TO_STRING_PRETTY); - json_object *new_jso = NULL; - assert(-1 == json_object_to_file(outfile4, new_jso)); - d = open(outfile4, O_WRONLY | O_CREAT, 0600); - if (d < 0) - { - printf("FAIL: unable to open %s %s\n", outfile4, strerror(errno)); - return; - } - assert(-1 == json_object_to_fd(d, new_jso, JSON_C_TO_STRING_PRETTY)); - close(d); - json_object_put(jso); - json_object_put(new_jso); } static void stat_and_cat(const char *file) @@ -141,8 +127,6 @@ int main(int argc, char **argv) // json_object_to_file(file, obj); // json_object_to_file_ext(file, obj, flags); - _json_c_strerror(0); - json_util_get_last_err(); _json_c_strerror_enable = 1; const char *testdir; @@ -305,7 +289,6 @@ static void test_read_fd_equal(const char *testdir) json_object *jso = json_object_from_file(filename); - assert(NULL == json_type_to_name(20)); int d = open(filename, O_RDONLY, 0); if (d < 0) {