add uint64 data to json-c

This commit is contained in:
dota17
2020-02-20 15:17:05 +08:00
parent 518f337ce8
commit 3c3b5920f7
24 changed files with 431 additions and 37 deletions

View File

@@ -25,10 +25,13 @@ int main(int argc, char **argv)
\"decimal_number\": 99.55,\n\
\"boolean_true\": true,\n\
\"boolean_false\": false,\n\
\"big_number\": 2147483649,\n\
\"int64_number\": 2147483649,\n\
\"negative_number\": -321321321,\n\
\"a_null\": null,\n\
}";
/* Note: 2147483649 = INT_MAX + 2 */
/* Note: 9223372036854775809 = INT64_MAX + 2 */
/* Note: 18446744073709551617 = UINT64_MAX + 2 */
struct json_object *new_obj;
@@ -43,7 +46,8 @@ int main(int argc, char **argv)
getit(new_obj, "decimal_number");
getit(new_obj, "boolean_true");
getit(new_obj, "boolean_false");
getit(new_obj, "big_number");
getit(new_obj, "int64_number");
getit(new_obj, "negative_number");
getit(new_obj, "a_null");
// Now check the behaviour of the json_object_is_type() function.
@@ -55,7 +59,8 @@ int main(int argc, char **argv)
checktype(new_obj, "decimal_number");
checktype(new_obj, "boolean_true");
checktype(new_obj, "boolean_false");
checktype(new_obj, "big_number");
checktype(new_obj, "int64_number");
checktype(new_obj, "negative_number");
checktype(new_obj, "a_null");
json_object_put(new_obj);
@@ -76,6 +81,8 @@ static void getit(struct json_object *new_obj, const char *field)
json_object_get_int(o));
printf("new_obj.%s json_object_get_int64()=%" PRId64 "\n", field,
json_object_get_int64(o));
printf("new_obj.%s json_object_get_uint64()=%" PRIu64 "\n", field,
json_object_get_uint64(o));
printf("new_obj.%s json_object_get_boolean()=%d\n", field,
json_object_get_boolean(o));
printf("new_obj.%s json_object_get_double()=%f\n", field,
@@ -84,11 +91,12 @@ static void getit(struct json_object *new_obj, const char *field)
static void checktype_header()
{
printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s\n",
printf("json_object_is_type: %s,%s,%s,%s,%s,%s,%s,%s\n",
json_type_to_name(json_type_null),
json_type_to_name(json_type_boolean),
json_type_to_name(json_type_double),
json_type_to_name(json_type_int),
json_type_to_name(json_type_uint),
json_type_to_name(json_type_object),
json_type_to_name(json_type_array),
json_type_to_name(json_type_string));
@@ -99,12 +107,13 @@ static void checktype(struct json_object *new_obj, const char *field)
if (field && !json_object_object_get_ex(new_obj, field, &o))
printf("Field %s does not exist\n", field);
printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d\n",
printf("new_obj%s%-18s: %d,%d,%d,%d,%d,%d,%d,%d\n",
field ? "." : " ", field ? field : "",
json_object_is_type(o, json_type_null),
json_object_is_type(o, json_type_boolean),
json_object_is_type(o, json_type_double),
json_object_is_type(o, json_type_int),
json_object_is_type(o, json_type_uint),
json_object_is_type(o, json_type_object),
json_object_is_type(o, json_type_array),
json_object_is_type(o, json_type_string));

View File

@@ -4,53 +4,68 @@ Parsed input: {
"decimal_number": 99.55,
"boolean_true": true,
"boolean_false": false,
"big_number": 2147483649,
"int64_number": 2147483649,
"negative_number": -321321321,
"a_null": null,
}
Result is not NULL
new_obj.string_of_digits json_object_get_type()=string
new_obj.string_of_digits json_object_get_int()=123
new_obj.string_of_digits json_object_get_int64()=123
new_obj.string_of_digits json_object_get_uint64()=123
new_obj.string_of_digits json_object_get_boolean()=1
new_obj.string_of_digits json_object_get_double()=123.000000
new_obj.regular_number json_object_get_type()=int
new_obj.regular_number json_object_get_int()=222
new_obj.regular_number json_object_get_int64()=222
new_obj.regular_number json_object_get_uint64()=222
new_obj.regular_number json_object_get_boolean()=1
new_obj.regular_number json_object_get_double()=222.000000
new_obj.decimal_number json_object_get_type()=double
new_obj.decimal_number json_object_get_int()=99
new_obj.decimal_number json_object_get_int64()=99
new_obj.decimal_number json_object_get_uint64()=99
new_obj.decimal_number json_object_get_boolean()=1
new_obj.decimal_number json_object_get_double()=99.550000
new_obj.boolean_true json_object_get_type()=boolean
new_obj.boolean_true json_object_get_int()=1
new_obj.boolean_true json_object_get_int64()=1
new_obj.boolean_true json_object_get_uint64()=1
new_obj.boolean_true json_object_get_boolean()=1
new_obj.boolean_true json_object_get_double()=1.000000
new_obj.boolean_false json_object_get_type()=boolean
new_obj.boolean_false json_object_get_int()=0
new_obj.boolean_false json_object_get_int64()=0
new_obj.boolean_false json_object_get_uint64()=0
new_obj.boolean_false json_object_get_boolean()=0
new_obj.boolean_false json_object_get_double()=0.000000
new_obj.big_number json_object_get_type()=int
new_obj.big_number json_object_get_int()=2147483647
new_obj.big_number json_object_get_int64()=2147483649
new_obj.big_number json_object_get_boolean()=1
new_obj.big_number json_object_get_double()=2147483649.000000
new_obj.int64_number json_object_get_type()=int
new_obj.int64_number json_object_get_int()=2147483647
new_obj.int64_number json_object_get_int64()=2147483649
new_obj.int64_number json_object_get_uint64()=2147483649
new_obj.int64_number json_object_get_boolean()=1
new_obj.int64_number json_object_get_double()=2147483649.000000
new_obj.negative_number json_object_get_type()=int
new_obj.negative_number json_object_get_int()=-321321321
new_obj.negative_number json_object_get_int64()=-321321321
new_obj.negative_number json_object_get_uint64()=0
new_obj.negative_number json_object_get_boolean()=1
new_obj.negative_number json_object_get_double()=-321321321.000000
new_obj.a_null json_object_get_type()=null
new_obj.a_null json_object_get_int()=0
new_obj.a_null json_object_get_int64()=0
new_obj.a_null json_object_get_uint64()=0
new_obj.a_null json_object_get_boolean()=0
new_obj.a_null json_object_get_double()=0.000000
================================
json_object_is_type: null,boolean,double,int,object,array,string
new_obj : 0,0,0,0,1,0,0
new_obj.string_of_digits : 0,0,0,0,0,0,1
new_obj.regular_number : 0,0,0,1,0,0,0
new_obj.decimal_number : 0,0,1,0,0,0,0
new_obj.boolean_true : 0,1,0,0,0,0,0
new_obj.boolean_false : 0,1,0,0,0,0,0
new_obj.big_number : 0,0,0,1,0,0,0
new_obj.a_null : 1,0,0,0,0,0,0
json_object_is_type: null,boolean,double,int,uint,object,array,string
new_obj : 0,0,0,0,0,1,0,0
new_obj.string_of_digits : 0,0,0,0,0,0,0,1
new_obj.regular_number : 0,0,0,1,0,0,0,0
new_obj.decimal_number : 0,0,1,0,0,0,0,0
new_obj.boolean_true : 0,1,0,0,0,0,0,0
new_obj.boolean_false : 0,1,0,0,0,0,0,0
new_obj.int64_number : 0,0,0,1,0,0,0,0
new_obj.negative_number : 0,0,0,1,0,0,0,0
new_obj.a_null : 1,0,0,0,0,0,0,0

View File

@@ -15,6 +15,9 @@ int main()
struct json_object *int1 = json_object_new_int(0);
struct json_object *int2 = json_object_new_int(1);
struct json_object *int3 = json_object_new_int(1);
struct json_object *uint1 = json_object_new_uint64(0);
struct json_object *uint2 = json_object_new_uint64(1);
struct json_object *uint3 = json_object_new_uint64(1);
if (!json_object_equal(int1, int2))
printf("JSON integer comparison is correct\n");
@@ -31,9 +34,27 @@ int main()
else
printf("JSON same integer comparison failed\n");
if (!json_object_equal(uint1, uint2))
printf("JSON usigned integer comparison is correct\n");
else
printf("JSON usigned integer comparison failed\n");
if (json_object_equal(uint1, uint1))
printf("JSON same usigned object comparison is correct\n");
else
printf("JSON same usigned object comparison failed\n");
if (json_object_equal(uint2, uint3))
printf("JSON same usigned integer comparison is correct\n");
else
printf("JSON same usigned integer comparison failed\n");
json_object_put(int1);
json_object_put(int2);
json_object_put(int3);
json_object_put(uint1);
json_object_put(uint2);
json_object_put(uint3);
/* string tests */
struct json_object *str1 = json_object_new_string("TESTSTRING");

View File

@@ -1,6 +1,9 @@
JSON integer comparison is correct
JSON same object comparison is correct
JSON same integer comparison is correct
JSON usigned integer comparison is correct
JSON same usigned object comparison is correct
JSON same usigned integer comparison is correct
Comparing equal strings is correct
Comparing different strings is correct
Comparing equal doubles is correct

View File

@@ -41,6 +41,18 @@ int main(int argc, char **argv)
assert(json_object_get_int64(tmp) != INT64_MIN);
json_object_put(tmp);
printf("INT64 ADD UNDERFLOW PASSED\n");
tmp = json_object_new_uint64(321321321);
json_object_uint_inc(tmp, 321321321);
assert(json_object_get_uint64(tmp) == 642642642);
json_object_put(tmp);
printf("UINT64 ADD PASSED\n");
tmp = json_object_new_uint64(UINT64_MAX);
json_object_uint_inc(tmp, 100);
assert(json_object_get_uint64(tmp) == UINT64_MAX);
json_object_uint_inc(tmp, -100);
assert(json_object_get_uint64(tmp) != INT64_MAX);
json_object_put(tmp);
printf("UINT64 ADD OVERFLOW PASSED\n");
printf("PASSED\n");
return 0;

View File

@@ -4,4 +4,6 @@ INT ADD UNDERFLOW PASSED
INT64 ADD PASSED
INT64 ADD OVERFLOW PASSED
INT64 ADD UNDERFLOW PASSED
UINT64 ADD PASSED
UINT64 ADD OVERFLOW PASSED
PASSED

View File

@@ -128,8 +128,18 @@ static void test_basic_parse()
single_basic_parse("[0e]", 1);
single_basic_parse("[0e+]", 1);
single_basic_parse("[0e+-1]", 1);
single_basic_parse("[18446744073709551616]", 1);
single_basic_parse("\"hello world!\"", 1);
// uint64/int64 range test
single_basic_parse("[9223372036854775806]", 1);
single_basic_parse("[9223372036854775807]", 1);
single_basic_parse("[9223372036854775808]", 1);
single_basic_parse("[-9223372036854775807]", 1);
single_basic_parse("[-9223372036854775808]", 1);
single_basic_parse("[-9223372036854775809]", 1);
single_basic_parse("[18446744073709551614]", 1);
single_basic_parse("[18446744073709551615]", 1);
single_basic_parse("[18446744073709551616]", 1);
}
static void test_utf8_parse()

View File

@@ -66,8 +66,16 @@ new_obj.to_string(false)=false
new_obj.to_string([0e])=[ 0.0 ]
new_obj.to_string([0e+])=[ 0.0 ]
new_obj.to_string([0e+-1])=null
new_obj.to_string([18446744073709551616])=[ 9223372036854775807 ]
new_obj.to_string("hello world!")="hello world!"
new_obj.to_string([9223372036854775806])=[ 9223372036854775806 ]
new_obj.to_string([9223372036854775807])=[ 9223372036854775807 ]
new_obj.to_string([9223372036854775808])=[ 9223372036854775808 ]
new_obj.to_string([-9223372036854775807])=[ -9223372036854775807 ]
new_obj.to_string([-9223372036854775808])=[ -9223372036854775808 ]
new_obj.to_string([-9223372036854775809])=[ -9223372036854775808 ]
new_obj.to_string([18446744073709551614])=[ 18446744073709551614 ]
new_obj.to_string([18446744073709551615])=[ 18446744073709551615 ]
new_obj.to_string([18446744073709551616])=[ 18446744073709551615 ]
==================================
new_obj.to_string()=null
new_obj.to_string({})=null

View File

@@ -15,10 +15,18 @@ void checkit(const char *buf)
printf("buf=%s parseit=%d, value=%" PRId64 " \n", buf, retval, cint64);
}
void checkit_uint(const char *buf)
{
uint64_t cuint64 = 666;
int retval = json_parse_uint64(buf, &cuint64);
printf("buf=%s parseit=%d, value=%" PRIu64 " \n", buf, retval, cuint64);
}
/**
* This test calls json_parse_int64 with a variety of different strings.
* It's purpose is to ensure that the results are consistent across all
* different environments that it might be executed in.
* This test calls json_parse_int64 and json_parse_int64 with a variety
* of different strings. It's purpose is to ensure that the results are
* consistent across all different environments that it might be executed in.
*
* This always exits with a 0 exit value. The output should be compared
* against previously saved expected output.
@@ -27,6 +35,7 @@ int main()
{
char buf[100];
printf("==========json_parse_int64() test===========\n");
checkit("x");
checkit("0");
@@ -59,8 +68,10 @@ int main()
checkit(buf);
strcpy(buf, "4294967295"); // aka UINT32_MAX
checkit(buf);
sprintf(buf, "4294967296"); // aka UINT32_MAX + 1
strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
checkit(buf);
strcpy(buf, "21474836470"); // INT32_MAX * 10
checkit(buf);
@@ -111,5 +122,68 @@ int main()
strcpy(buf, "123");
checkit(buf);
printf("\n==========json_parse_uint64() test===========\n");
checkit_uint("x");
checkit_uint("0");
checkit_uint("-0");
checkit_uint("00000000");
checkit_uint("-00000000");
checkit_uint("1");
strcpy(buf, "2147483647"); // aka INT32_MAX
checkit_uint(buf);
strcpy(buf, "-1");
checkit_uint(buf);
strcpy(buf, "-9223372036854775808");
checkit_uint(buf);
strcpy(buf, " 1");
checkit_uint(buf);
strcpy(buf, "00001234");
checkit_uint(buf);
strcpy(buf, "0001234x");
checkit_uint(buf);
strcpy(buf, "4294967295"); // aka UINT32_MAX
checkit_uint(buf);
strcpy(buf, "4294967296"); // aka UINT32_MAX + 1
checkit_uint(buf);
strcpy(buf, "21474836470"); // INT32_MAX * 10
checkit_uint(buf);
strcpy(buf, "31474836470"); // INT32_MAX * 10 + a bunch
checkit_uint(buf);
strcpy(buf, "9223372036854775806"); // INT64_MAX - 1
checkit_uint(buf);
strcpy(buf, "9223372036854775807"); // INT64_MAX
checkit_uint(buf);
strcpy(buf, "9223372036854775808"); // INT64_MAX + 1
checkit_uint(buf);
strcpy(buf, "18446744073709551614"); // UINT64_MAX - 1
checkit_uint(buf);
strcpy(buf, "18446744073709551615"); // UINT64_MAX
checkit_uint(buf);
strcpy(buf, "18446744073709551616"); // UINT64_MAX + 1
checkit_uint(buf);
// Ensure we can still parse valid numbers after parsing out of range ones.
strcpy(buf, "123");
checkit_uint(buf);
return 0;
}

View File

@@ -1,3 +1,4 @@
==========json_parse_int64() test===========
buf=x parseit=1, value=-666
buf=0 parseit=0, value=0
buf=-0 parseit=0, value=0
@@ -11,6 +12,8 @@ buf=00001234 parseit=0, value=1234
buf=0001234x parseit=0, value=1234
buf=-00001234 parseit=0, value=-1234
buf=-00001234x parseit=0, value=-1234
buf=4294967295 parseit=0, value=4294967295
buf=4294967296 parseit=0, value=4294967296
buf=21474836470 parseit=0, value=21474836470
buf=31474836470 parseit=0, value=31474836470
buf=-2147483647 parseit=0, value=-2147483647
@@ -27,3 +30,28 @@ buf=18446744073709551615 parseit=0, value=9223372036854775807
buf=18446744073709551616 parseit=0, value=9223372036854775807
buf=-18446744073709551616 parseit=0, value=-9223372036854775808
buf=123 parseit=0, value=123
==========json_parse_uint64() test===========
buf=x parseit=1, value=666
buf=0 parseit=0, value=0
buf=-0 parseit=1, value=0
buf=00000000 parseit=0, value=0
buf=-00000000 parseit=1, value=0
buf=1 parseit=0, value=1
buf=2147483647 parseit=0, value=2147483647
buf=-1 parseit=1, value=18446744073709551615
buf=-9223372036854775808 parseit=1, value=9223372036854775808
buf= 1 parseit=0, value=1
buf=00001234 parseit=0, value=1234
buf=0001234x parseit=0, value=1234
buf=4294967295 parseit=0, value=4294967295
buf=4294967296 parseit=0, value=4294967296
buf=21474836470 parseit=0, value=21474836470
buf=31474836470 parseit=0, value=31474836470
buf=9223372036854775806 parseit=0, value=9223372036854775806
buf=9223372036854775807 parseit=0, value=9223372036854775807
buf=9223372036854775808 parseit=0, value=9223372036854775808
buf=18446744073709551614 parseit=0, value=18446744073709551614
buf=18446744073709551615 parseit=0, value=18446744073709551615
buf=18446744073709551616 parseit=0, value=18446744073709551615
buf=123 parseit=0, value=123

View File

@@ -15,6 +15,16 @@ int main(int argc, char **argv)
assert (json_object_get_int64(tmp)==321321321);
json_object_put(tmp);
printf("INT64 PASSED\n");
tmp=json_object_new_uint64(123);
assert (json_object_get_int(tmp)==123);
assert (json_object_get_int64(tmp)==123);
assert (json_object_get_uint64(tmp)==123);
json_object_set_uint64(tmp,(uint64_t)321321321);
assert (json_object_get_uint64(tmp)==321321321);
json_object_set_uint64(tmp,9223372036854775808U);
assert (json_object_get_uint64(tmp)==9223372036854775808U);
json_object_put(tmp);
printf("UINT64 PASSED\n");
tmp=json_object_new_boolean(1);
assert (json_object_get_boolean(tmp)==1);
json_object_set_boolean(tmp,0);
@@ -29,6 +39,12 @@ int main(int argc, char **argv)
assert (json_object_get_double(tmp)==34.56);
json_object_set_double(tmp,6435.34);
assert (json_object_get_double(tmp)==6435.34);
json_object_set_double(tmp,2e21);
assert (json_object_get_int64(tmp)==INT64_MAX);
assert (json_object_get_uint64(tmp)==UINT64_MAX);
json_object_set_double(tmp,-2e21);
assert (json_object_get_int64(tmp)==INT64_MIN);
assert (json_object_get_uint64(tmp)==0);
json_object_put(tmp);
printf("DOUBLE PASSED\n");
#define SHORT "SHORT"

View File

@@ -1,5 +1,6 @@
INT PASSED
INT64 PASSED
UINT64 PASSED
BOOL PASSED
DOUBLE PASSED
STRING PASSED