]> granicus.if.org Git - json-c/commitdiff
Very minor changes to some tests
authorNicola Spanti (RyDroid) <dev@nicola-spanti.info>
Mon, 8 Aug 2016 13:11:19 +0000 (15:11 +0200)
committerNicola Spanti (RyDroid) <dev@nicola-spanti.info>
Fri, 12 Aug 2016 22:53:33 +0000 (00:53 +0200)
tests/test-defs.sh
tests/test1.c
tests/test2.c
tests/test4.c
tests/test_null.c
tests/test_parse.c
tests/test_util_file.c

index 028d99b1bc18a8d9bcd2a4266a372bc69ad4b8d2..5a32d392fb75eded8866a18a6efb361cba168c8b 100755 (executable)
@@ -124,5 +124,3 @@ run_output_test()
 
        return $err
 }
-
-
index 3cbd1860d66cff3a4db932796135052cee4975b2..ba44742a5640efac85b928c7596a1856f8f19881 100644 (file)
@@ -78,14 +78,14 @@ void test_array_del_idx()
                       rc, json_object_to_json_string(my_array));
        }
 
-       // One more time, with the empty array:
+       /* One more time, with the empty array: */
        rc = json_object_array_del_idx(my_array, 0, 1);
        printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n",
               rc, json_object_to_json_string(my_array));
 
        json_object_put(my_array);
 
-       // Delete all array indexes at once
+       /* Delete all array indexes at once */
        my_array = make_array();
        rc = json_object_array_del_idx(my_array, 0, orig_array_len);
        printf("after del_idx(0,%llu)=%d, my_array.to_string()=%s\n",
@@ -93,7 +93,7 @@ void test_array_del_idx()
 
        json_object_put(my_array);
 
-       // Delete *more* than all array indexes at once
+       /* Delete *more* than all array indexes at once */
        my_array = make_array();
        rc = json_object_array_del_idx(my_array, 0, orig_array_len + 1);
        printf("after del_idx(0,%llu)=%d, my_array.to_string()=%s\n",
@@ -213,5 +213,5 @@ int main(int argc, char **argv)
        json_object_put(my_object);
        json_object_put(my_array);
 
-       return 0;
+       return EXIT_SUCCESS;
 }
index ce44e46ffab2001e97076873665ec5c127c4497f..06a453e504954b3ca4fc3e719e99b6aebc7caf99 100644 (file)
@@ -26,9 +26,10 @@ int main(int argc, char **argv)
        sflags = parse_flags(argc, argv);
 #endif
 
-       new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
+       new_obj = json_tokener_parse("/* more difficult test case */"
+                                    "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }");
        printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
        json_object_put(new_obj);
 
-       return 0;
+       return EXIT_SUCCESS;
 }
index 628fef5668ddcb945498c6640a9cda3f17d205ed..fc8b79dbf457cfff083714538e86a348d824d993 100644 (file)
@@ -10,7 +10,7 @@
 #include "json_object.h"
 #include "json_tokener.h"
 
-void print_hex( const char* s)
+void print_hex(const char* s)
 {
        const char *iter = s;
        unsigned char ch;
@@ -21,10 +21,10 @@ void print_hex( const char* s)
                else
                        printf( ",");
        }
-       printf("\n");
+       putchar('\n');
 }
 
-int main()
+int main(void)
 {
        const char *input = "\"\\ud840\\udd26,\\ud840\\udd27,\\ud800\\udd26,\\ud800\\udd27\"";
        const char *expected = "\xF0\xA0\x84\xA6,\xF0\xA0\x84\xA7,\xF0\x90\x84\xA6,\xF0\x90\x84\xA7";
@@ -38,14 +38,14 @@ int main()
        if (strings_match)
        {
                printf("JSON parse result is correct: %s\n", unjson);
-               printf("PASS\n");
+               puts("PASS");
        } else {
                printf("JSON parse result doesn't match expected string\n");
                printf("expected string bytes: ");
-               print_hex( expected);
+               print_hex(expected);
                printf("parsed string bytes:   ");
-               print_hex( unjson);
-               printf("FAIL\n");
+               print_hex(unjson);
+               puts("FAIL");
                retval = 1;
        }
        json_object_put(parse_result);
index 1f07910ee1756907b04292e909c9b800cc36772c..6ef0a967eee5ac2da4fb435c47800b578a2b9216 100644 (file)
@@ -1,6 +1,6 @@
 /*
-* Tests if binary strings are supported.
-*/
+ * Tests if binary strings are supported.
+ */
 
 #include <stdio.h>
 #include <string.h>
@@ -10,9 +10,9 @@
 #include "json_object.h"
 #include "json_tokener.h"
 
-int main()
+int main(void)
 {
-       // this test has a space after the null character. check that it's still included
+       /* this test has a space after the null character. check that it's still included */
        const char *input = " \0 ";
        const char *expected = "\" \\u0000 \"";
        struct json_object *string = json_object_new_string_len(input, 3);
@@ -23,14 +23,14 @@ int main()
        if (strings_match)
        {
                printf("JSON write result is correct: %s\n", json);
-               printf("PASS\n");
+               puts("PASS");
        } else {
-               printf("JSON write result doesn't match expected string\n");
+               puts("JSON write result doesn't match expected string");
                printf("expected string: ");
-               printf("%s\n", expected);
+               puts(expected);
                printf("parsed string:   ");
-               printf("%s\n", json);
-               printf("FAIL\n");
+               puts(json);
+               puts("FAIL");
                retval=1;
        }
        json_object_put(string);
@@ -46,12 +46,12 @@ int main()
                {
                        printf("%s%d", (ii ? ", " : ""), (int)parsed_cstr[ii]);
                }
-               printf("]\n");
+               puts("]");
                json_object_put(parsed_str);
        }
        else
        {
-               printf("ERROR: failed to parse\n");
+               puts("ERROR: failed to parse");
        }
        return retval;
 }
index 129fc06c3c00e841436f81dc10973f839c41d873..1c3504f5528da8027f55edd9346168819ec91f3c 100644 (file)
@@ -11,16 +11,17 @@ static void test_basic_parse(void);
 static void test_verbose_parse(void);
 static void test_incremental_parse(void);
 
-int main(int argc, char **argv)
+int main(void)
 {
        MC_SET_DEBUG(1);
 
+       static const char separator[] = "==================================";
        test_basic_parse();
-       printf("==================================\n");
+       puts(separator);
        test_verbose_parse();
-       printf("==================================\n");
+       puts(separator);
        test_incremental_parse();
-       printf("==================================\n");
+       puts(separator);
 }
 
 static void test_basic_parse()
@@ -173,7 +174,7 @@ static void test_verbose_parse()
        /* b/c the string starts with 'f' parsing return a boolean error */
        assert (error == json_tokener_error_parse_boolean);
 
-       printf("json_tokener_parse_versbose() OK\n");
+       puts("json_tokener_parse_versbose() OK");
 }
 
 struct incremental_step {
@@ -263,7 +264,7 @@ static void test_incremental_parse()
        string_to_parse = "{ \"foo"; /* } */
        printf("json_tokener_parse(%s) ... ", string_to_parse);
        new_obj = json_tokener_parse(string_to_parse);
-       if (new_obj == NULL) printf("got error as expected\n");
+       if (new_obj == NULL) puts("got error as expected");
 
        /* test incremental parsing in various forms */
        tok = json_tokener_new();
@@ -293,17 +294,18 @@ static void test_incremental_parse()
                {
                        if (new_obj != NULL)
                                printf("ERROR: invalid object returned: %s\n",
-                                       json_object_to_json_string(new_obj));
+                                      json_object_to_json_string(new_obj));
                        else if (jerr != step->expected_error)
                                printf("ERROR: got wrong error: %s\n",
-                                       json_tokener_error_desc(jerr));
+                                      json_tokener_error_desc(jerr));
                        else if (tok->char_offset != expected_char_offset)
                                printf("ERROR: wrong char_offset %d != expected %d\n",
-                                       tok->char_offset,
-                                       expected_char_offset);
+                                      tok->char_offset,
+                                      expected_char_offset);
                        else
                        {
-                               printf("OK: got correct error: %s\n", json_tokener_error_desc(jerr));
+                               printf("OK: got correct error: %s\n",
+                                      json_tokener_error_desc(jerr));
                                this_step_ok = 1;
                        }
                }
@@ -311,11 +313,11 @@ static void test_incremental_parse()
                {
                        if (new_obj == NULL)
                                printf("ERROR: expected valid object, instead: %s\n",
-                                       json_tokener_error_desc(jerr));
+                                      json_tokener_error_desc(jerr));
                        else if (tok->char_offset != expected_char_offset)
                                printf("ERROR: wrong char_offset %d != expected %d\n",
-                                       tok->char_offset,
-                                       expected_char_offset);
+                                      tok->char_offset,
+                                      expected_char_offset);
                        else
                        {
                                printf("OK: got object of type [%s]: %s\n",
@@ -340,6 +342,4 @@ static void test_incremental_parse()
        json_tokener_free(tok);
 
        printf("End Incremental Tests OK=%d ERROR=%d\n", num_ok, num_error);
-
-       return;
 }
index 776393371d444b1a2878370eedda49104f9a8f9c..467c841ec060b38015910398c1a8ea60694d0459 100644 (file)
@@ -41,12 +41,12 @@ static void test_write_to_file()
        if (rv == 0)
                stat_and_cat(outfile);
 
-       printf("\n");
+       putchar('\n');
 
        const char *outfile2 = "json2.out";
        rv = json_object_to_file_ext(outfile2, jso, JSON_C_TO_STRING_PRETTY);
        printf("%s: json_object_to_file_ext(%s, jso, JSON_C_TO_STRING_PRETTY)=%d\n",
-            (rv == 0) ? "OK" : "FAIL", outfile2, rv);
+              (rv == 0) ? "OK" : "FAIL", outfile2, rv);
        if (rv == 0)
                stat_and_cat(outfile2);
 }
@@ -57,24 +57,35 @@ static void stat_and_cat(const char *file)
        int d = open(file, O_RDONLY, 0600);
        if (d < 0)
        {
-               printf("FAIL: unable to open %s: %s\n", file, strerror(errno));
+               printf("FAIL: unable to open %s: %s\n",
+                      file, strerror(errno));
                return;
        }
        if (fstat(d, &sb) < 0)
        {
-               printf("FAIL: unable to stat %s: %s\n", file, strerror(errno));
+               printf("FAIL: unable to stat %s: %s\n",
+                      file, strerror(errno));
                close(d);
                return;
        }
        char *buf = malloc(sb.st_size + 1);
+       if(!buf)
+       {
+               printf("FAIL: unable to allocate memory\n");
+               close(d);
+               return;
+       }
        if (read(d, buf, sb.st_size) < sb.st_size)
        {
-               printf("FAIL: unable to read all of %s: %s\n", file, strerror(errno));
+               printf("FAIL: unable to read all of %s: %s\n",
+                      file, strerror(errno));
+               free(buf);
                close(d);
                return;
        }
        buf[sb.st_size] = '\0';
        printf("file[%s], size=%d, contents=%s\n", file, (int)sb.st_size, buf);
+       free(buf);
 }
 
 int main(int argc, char **argv)
@@ -85,8 +96,11 @@ int main(int argc, char **argv)
        const char *testdir;
        if (argc < 2)
        {
-               fprintf(stderr, "Usage: %s <testdir>\n  <testdir> is the location of input files\n", argv[0]);
-               exit(1);
+               fprintf(stderr,
+                       "Usage: %s <testdir>\n"
+                       "  <testdir> is the location of input files\n",
+                       argv[0]);
+               return EXIT_FAILURE;
        }
        testdir = argv[1];
 
@@ -94,6 +108,7 @@ int main(int argc, char **argv)
        test_read_nonexistant();
        test_read_closed();
        test_write_to_file();
+       return EXIT_SUCCESS;
 }
 
 static void test_read_valid_with_fd(const char *testdir)
@@ -103,18 +118,23 @@ static void test_read_valid_with_fd(const char *testdir)
        int d = open(filename, O_RDONLY, 0);
        if (d < 0)
        {
-               fprintf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
-               exit(1);
+               fprintf(stderr,
+                       "FAIL: unable to open %s: %s\n",
+                       filename, strerror(errno));
+               exit(EXIT_FAILURE);
        }
        json_object *jso = json_object_from_fd(d);
        if (jso != NULL)
        {
-               printf("OK: json_object_from_fd(%s)=%s\n", filename, json_object_to_json_string(jso));
+               printf("OK: json_object_from_fd(%s)=%s\n",
+                      filename, json_object_to_json_string(jso));
                json_object_put(jso);
        }
        else
        {
-               fprintf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename, json_util_get_last_err());
+               fprintf(stderr,
+                       "FAIL: unable to parse contents of %s: %s\n",
+                       filename, json_util_get_last_err());
        }
        close(d);
 }
@@ -126,7 +146,8 @@ static void test_read_nonexistant()
        json_object *jso = json_object_from_file(filename);
        if (jso != NULL)
        {
-               printf("FAIL: json_object_from_file(%s) returned 0x%lx when NULL expected\n", filename, (unsigned long)jso);
+               printf("FAIL: json_object_from_file(%s) returned 0x%lx when NULL expected\n",
+                      filename, (unsigned long)jso);
                json_object_put(jso);
        }
        else
@@ -140,15 +161,23 @@ static void test_read_closed()
 {
        // Test reading from a closed fd
        int d = open("/dev/null", O_RDONLY, 0);
+       if(d < 0)
+       {
+         puts("FAIL: unable to open");
+       }
        close(d);
+
        json_object *jso = json_object_from_fd(d);
        if (jso != NULL)
        {
-               printf("FAIL: read from closed fd returning non-NULL: 0x%lx\n", (unsigned long)jso);
+               printf("FAIL: read from closed fd returning non-NULL: 0x%lx\n",
+                      (unsigned long) jso);
                fflush(stdout);
                printf("  jso=%s\n", json_object_to_json_string(jso));
                json_object_put(jso);
                return;
        }
-       printf("OK: json_object_from_fd(closed_fd), expecting NULL, EBADF, got:0x%lx, %s\n", (unsigned long)jso, json_util_get_last_err());
+       printf("OK: json_object_from_fd(closed_fd), "
+              "expecting NULL, EBADF, got:0x%lx, %s\n",
+              (unsigned long)jso, json_util_get_last_err());
 }