Browse Source

add test cases

tags/json-c-0.14-20200419
dota17 5 years ago
parent
commit
3822177473
9 changed files with 89 additions and 1 deletions
  1. +2
    -1
      json_object.c
  2. +42
    -0
      tests/test_compare.c
  3. +6
    -0
      tests/test_compare.expected
  4. +1
    -0
      tests/test_deep_copy.c
  5. +1
    -0
      tests/test_deep_copy.expected
  6. +9
    -0
      tests/test_int_add.c
  7. +1
    -0
      tests/test_int_add.expected
  8. +26
    -0
      tests/test_set_value.c
  9. +1
    -0
      tests/test_set_value.expected

+ 2
- 1
json_object.c View File

@@ -656,7 +656,8 @@ int32_t json_object_get_int(const struct json_object *jso)
} else {
if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
cint64 = INT64_MAX;
cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
else
cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
}

if (o_type == json_type_string)


+ 42
- 0
tests/test_compare.c View File

@@ -208,8 +208,50 @@ int main()
else
printf("Comparing different objects is incorrect\n");

/* iterate over jso2 keys to see if any exist that are not in jso1 */
json_object_object_add(obj2, "test3", json_object_new_int(320));
json_object_object_add(obj2, "test6", json_object_new_int(321));
if (!json_object_equal(obj1, obj2))
printf("Comparing different objects is correct\n");
else
printf("Comparing different objects is incorrect\n");

/* iterate over jso1 keys and see if they exist in jso1 */
json_object_object_add(obj1, "test6", json_object_new_int(321));
if (json_object_equal(obj1, obj2))
printf("Comparing different objects is correct\n");
else
printf("Comparing different objects is incorrect\n");
json_object_object_add(obj1, "test7", json_object_new_int(322));
if (!json_object_equal(obj1, obj2))
printf("Comparing different objects is correct\n");
else
printf("Comparing different objects is incorrect\n");

json_object_put(obj1);
json_object_put(obj2);

/* different types tests */
struct json_object *int5 = json_object_new_int(0);
struct json_object *dbl5 = json_object_new_double(3.14159);

if (!json_object_equal(int5, NULL))
printf("JSON integer and NULL comparison is correct\n");
else
printf("JSON integer and NULL comparison failed\n");

if (!json_object_equal(NULL, dbl5))
printf("JSON NULL and double comparison is correct\n");
else
printf("JSON NULL and double comparison failed\n");

if (!json_object_equal(int5, dbl5))
printf("JSON integer and double comparison is correct\n");
else
printf("JSON integer and double comparison failed\n");

json_object_put(int5);
json_object_put(dbl5);

return 0;
}

+ 6
- 0
tests/test_compare.expected View File

@@ -22,3 +22,9 @@ Comparing different arrays is correct
Comparing different arrays (one empty) is correct
Comparing JSON object with different key order is correct
Comparing different objects is correct
Comparing different objects is correct
Comparing different objects is correct
Comparing different objects is correct
JSON integer and NULL comparison is correct
JSON NULL and double comparison is correct
JSON integer and double comparison is correct

+ 1
- 0
tests/test_deep_copy.c View File

@@ -19,6 +19,7 @@ static const char *json_str1 =
" \"glossary\": {"
" \"title\": \"example glossary\","
" \"GlossDiv\": {"
" \"number\": 16446744073709551615,"
" \"title\": \"S\","
" \"null_obj\": null, "
" \"exixt\": false,"


+ 1
- 0
tests/test_deep_copy.expected View File

@@ -11,6 +11,7 @@ Printing JSON objects for visual inspection
"glossary":{
"title":"example glossary",
"GlossDiv":{
"number":16446744073709551615,
"title":"S",
"null_obj":null,
"exixt":false,


+ 9
- 0
tests/test_int_add.c View File

@@ -48,8 +48,17 @@ int main(int argc, char **argv)
json_object_int_inc(tmp, -200);
assert(json_object_get_int64(tmp) == 200);
assert(json_object_get_uint64(tmp) == 200);
json_object_int_inc(tmp, 200);
assert(json_object_get_int64(tmp) == 400);
assert(json_object_get_uint64(tmp) == 400);
json_object_put(tmp);
printf("UINT64 ADD PASSED\n");
tmp = json_object_new_uint64(UINT64_MAX-50);
json_object_int_inc(tmp, 100);
assert(json_object_get_int64(tmp) == INT64_MAX);
assert(json_object_get_uint64(tmp) == UINT64_MAX);
json_object_put(tmp);
printf("UINT64 ADD OVERFLOW PASSED\n");
tmp = json_object_new_uint64(100);
json_object_int_inc(tmp, -200);
assert(json_object_get_int64(tmp) == -100);


+ 1
- 0
tests/test_int_add.expected View File

@@ -5,5 +5,6 @@ INT64 ADD PASSED
INT64 ADD OVERFLOW PASSED
INT64 ADD UNDERFLOW PASSED
UINT64 ADD PASSED
UINT64 ADD OVERFLOW PASSED
UINT64 ADD UNDERFLOW PASSED
PASSED

+ 26
- 0
tests/test_set_value.c View File

@@ -16,12 +16,15 @@ int main(int argc, char **argv)
json_object_put(tmp);
printf("INT64 PASSED\n");
tmp=json_object_new_uint64(123);
assert (json_object_get_boolean(tmp)==1);
assert (json_object_get_int(tmp)==123);
assert (json_object_get_int64(tmp)==123);
assert (json_object_get_uint64(tmp)==123);
assert (json_object_get_double(tmp)==123.000000);
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_int(tmp)==INT32_MAX);
assert (json_object_get_uint64(tmp)==9223372036854775808U);
json_object_put(tmp);
printf("UINT64 PASSED\n");
@@ -40,9 +43,11 @@ int main(int argc, char **argv)
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_int(tmp)==INT32_MAX);
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_int(tmp)==INT32_MIN);
assert (json_object_get_int64(tmp)==INT64_MIN);
assert (json_object_get_uint64(tmp)==0);
json_object_put(tmp);
@@ -62,6 +67,27 @@ int main(int argc, char **argv)
json_object_put(tmp);
printf("STRING PASSED\n");

#define STR "STR"
#define DOUBLE "123.123"
#define DOUBLE_E "12E+3"
#define DOUBLE_STR "123.123STR"
#define DOUBLE_OVER "1.8E+308"
#define DOUBLE_OVER_NEGATIVE "-1.8E+308"
tmp=json_object_new_string(STR);
assert (json_object_get_double(tmp)==0.0);
json_object_set_string(tmp,DOUBLE);
assert (json_object_get_double(tmp)==123.123000);
json_object_set_string(tmp,DOUBLE_E);
assert (json_object_get_double(tmp)==12000.000000);
json_object_set_string(tmp,DOUBLE_STR);
assert (json_object_get_double(tmp)==0.0);
json_object_set_string(tmp,DOUBLE_OVER);
assert (json_object_get_double(tmp)==0.0);
json_object_set_string(tmp,DOUBLE_OVER_NEGATIVE);
assert (json_object_get_double(tmp)==0.0);
json_object_put(tmp);
printf("STRINGTODOUBLE PASSED\n");

tmp = json_tokener_parse("1.234");
json_object_set_double(tmp, 12.3);
const char *serialized = json_object_to_json_string(tmp);


+ 1
- 0
tests/test_set_value.expected View File

@@ -4,5 +4,6 @@ UINT64 PASSED
BOOL PASSED
DOUBLE PASSED
STRING PASSED
STRINGTODOUBLE PASSED
PARSE AND SET PASSED
PASSED

Loading…
Cancel
Save