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",
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",
json_object_put(my_object);
json_object_put(my_array);
- return 0;
+ return EXIT_SUCCESS;
}
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;
}
#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;
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";
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);
/*
-* Tests if binary strings are supported.
-*/
+ * Tests if binary strings are supported.
+ */
#include <stdio.h>
#include <string.h>
#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);
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);
{
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;
}
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()
/* 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 {
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();
{
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;
}
}
{
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",
json_tokener_free(tok);
printf("End Incremental Tests OK=%d ERROR=%d\n", num_ok, num_error);
-
- return;
}
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);
}
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)
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];
test_read_nonexistant();
test_read_closed();
test_write_to_file();
+ return EXIT_SUCCESS;
}
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);
}
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
{
// 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());
}