Browse Source

Merge pull request #542 from dota17/adduint64_final

add uint64 data to json-c
tags/json-c-0.14-20200419
Eric Haszlakiewicz GitHub 5 years ago
parent
commit
737aee40c4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 526 additions and 50 deletions
  1. +11
    -0
      CMakeLists.txt
  2. +2
    -0
      cmake/config.h.in
  3. +2
    -0
      config.h.win32
  4. +1
    -0
      configure.ac
  5. +1
    -0
      json_inttypes.h
  6. +153
    -21
      json_object.c
  7. +33
    -1
      json_object.h
  8. +13
    -1
      json_object_private.h
  9. +21
    -9
      json_tokener.c
  10. +19
    -1
      json_util.c
  11. +1
    -0
      json_util.h
  12. +10
    -3
      tests/test_cast.c
  13. +22
    -7
      tests/test_cast.expected
  14. +65
    -0
      tests/test_compare.c
  15. +11
    -0
      tests/test_compare.expected
  16. +16
    -1
      tests/test_int_add.c
  17. +2
    -0
      tests/test_int_add.expected
  18. +11
    -1
      tests/test_parse.c
  19. +9
    -1
      tests/test_parse.expected
  20. +78
    -4
      tests/test_parse_int64.c
  21. +28
    -0
      tests/test_parse_int64.expected
  22. +16
    -0
      tests/test_set_value.c
  23. +1
    -0
      tests/test_set_value.expected

+ 11
- 0
CMakeLists.txt View File

@@ -161,6 +161,7 @@ endif()

if (MSVC)
check_symbol_exists(strtoll "stdlib.h" HAVE_STRTOLL)
check_symbol_exists(strtoull "stdlib.h" HAVE_STRTOULL)

set(json_c_strtoll "strtoll")
if (NOT HAVE_STRTOLL)
@@ -172,6 +173,16 @@ if (MSVC)
# could do the same for strtoull, if needed
endif()
endif()

set(json_c_strtoull "strtoull")
if (NOT HAVE_STRTOULL)
# Use _strtoui64 if strtoull is not available.
check_symbol_exists(_strtoui64 "stdlib.h" __have_strtoui64)
if (__have_strtoui64)
set(HAVE_STRTOULL 1)
set(json_c_strtoull "_strtoui64")
endif()
endif()
endif()




+ 2
- 0
cmake/config.h.in View File

@@ -137,6 +137,8 @@

#cmakedefine HAVE_STRTOLL
#cmakedefine strtoll @json_c_strtoll@
#cmakedefine HAVE_STRTOULL
#cmakedefine strtoull @json_c_strtoull@

/* Have __thread */
#cmakedefine HAVE___THREAD


+ 2
- 0
config.h.win32 View File

@@ -115,6 +115,8 @@
#cmakedefine HAVE_STRTOLL
#cmakedefine strtoll @cmake_strtoll@
#cmakedefine HAVE_STRTOULL
#cmakedefine strtoull @cmake_strtoull@
/* Define to 1 if you have the <syslog.h> header file. */
#undef HAVE_SYSLOG_H


+ 1
- 0
configure.ac View File

@@ -200,6 +200,7 @@ AX_COMPILE_CHECK_SIZEOF(long)
AX_COMPILE_CHECK_SIZEOF(long long)
AX_COMPILE_CHECK_SIZEOF(size_t, [#include <stdint.h>])
AX_COMPILE_CHECK_SIZEOF(int64_t, [#include <stdint.h>])
AX_COMPILE_CHECK_SIZEOF(uint64_t, [#include <stdint.h>])

AC_CONFIG_FILES([
Makefile


+ 1
- 0
json_inttypes.h View File

@@ -17,6 +17,7 @@

#define PRId64 "I64d"
#define SCNd64 "I64d"
#define PRIu64 "I64u"

#endif



+ 153
- 21
json_object.c View File

@@ -591,7 +591,14 @@ json_bool json_object_get_boolean(const struct json_object *jso)
case json_type_boolean:
return jso->o.c_boolean;
case json_type_int:
return (jso->o.c_int64 != 0);
switch(jso->o.c_int.cint_type) {
case json_object_int_type_int64:
return (jso->o.c_int.cint.c_int64 != 0);
case json_object_int_type_uint64:
return (jso->o.c_int.cint.c_uint64 != 0);
default:
assert(0);
}
case json_type_double:
return (jso->o.c_double != 0);
case json_type_string:
@@ -618,7 +625,10 @@ static int json_object_int_to_json_string(struct json_object* jso,
{
/* room for 19 digits, the sign char, and a null term */
char sbuf[21];
snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int64);
if (jso->o.c_int.cint_type == json_object_int_type_int64)
snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int.cint.c_int64);
else
snprintf(sbuf, sizeof(sbuf), "%" PRIu64, jso->o.c_int.cint.c_uint64);
return printbuf_memappend (pb, sbuf, strlen(sbuf));
}

@@ -628,7 +638,8 @@ struct json_object* json_object_new_int(int32_t i)
if (!jso)
return NULL;
jso->_to_json_string = &json_object_int_to_json_string;
jso->o.c_int64 = i;
jso->o.c_int.cint.c_int64 = i;
jso->o.c_int.cint_type = json_object_int_type_int64;
return jso;
}

@@ -640,7 +651,13 @@ int32_t json_object_get_int(const struct json_object *jso)
if(!jso) return 0;

o_type = jso->o_type;
cint64 = jso->o.c_int64;
if (jso->o.c_int.cint_type == json_object_int_type_int64) {
cint64 = jso->o.c_int.cint.c_int64;
} else {
if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
cint64 = INT64_MAX;
cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
}

if (o_type == json_type_string)
{
@@ -675,10 +692,7 @@ int32_t json_object_get_int(const struct json_object *jso)
}

int json_object_set_int(struct json_object *jso,int new_value){
if (!jso || jso->o_type!=json_type_int)
return 0;
jso->o.c_int64=new_value;
return 1;
return json_object_set_int64(jso,(int64_t)new_value);
}

struct json_object* json_object_new_int64(int64_t i)
@@ -687,7 +701,19 @@ struct json_object* json_object_new_int64(int64_t i)
if (!jso)
return NULL;
jso->_to_json_string = &json_object_int_to_json_string;
jso->o.c_int64 = i;
jso->o.c_int.cint.c_int64 = i;
jso->o.c_int.cint_type = json_object_int_type_int64;
return jso;
}

struct json_object* json_object_new_uint64(uint64_t i)
{
struct json_object *jso = json_object_new(json_type_int);
if (!jso)
return NULL;
jso->_to_json_string = &json_object_int_to_json_string;
jso->o.c_int.cint.c_uint64 = i;
jso->o.c_int.cint_type = json_object_int_type_uint64;
return jso;
}

@@ -700,7 +726,16 @@ int64_t json_object_get_int64(const struct json_object *jso)
switch(jso->o_type)
{
case json_type_int:
return jso->o.c_int64;
switch(jso->o.c_int.cint_type) {
case json_object_int_type_int64:
return jso->o.c_int.cint.c_int64;
case json_object_int_type_uint64:
if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
return INT64_MAX;
return (int64_t)jso->o.c_int.cint.c_uint64;
default:
assert(0);
}
case json_type_double:
// INT64_MAX can't be exactly represented as a double
// so cast to tell the compiler it's ok to round up.
@@ -720,24 +755,89 @@ int64_t json_object_get_int64(const struct json_object *jso)
}
}

uint64_t json_object_get_uint64(const struct json_object *jso)
{
uint64_t cuint;

if (!jso)
return 0;
switch(jso->o_type)
{
case json_type_int:
switch(jso->o.c_int.cint_type) {
case json_object_int_type_int64:
if (jso->o.c_int.cint.c_int64 < 0)
return 0;
return (uint64_t)jso->o.c_int.cint.c_int64;
case json_object_int_type_uint64:
return jso->o.c_int.cint.c_uint64;
default:
assert(0);
}
case json_type_double:
// UINT64_MAX can't be exactly represented as a double
// so cast to tell the compiler it's ok to round up.
if (jso->o.c_double >= (double)UINT64_MAX)
return UINT64_MAX;
if (jso->o.c_double < 0)
return 0;
return (uint64_t)jso->o.c_double;
case json_type_boolean:
return jso->o.c_boolean;
case json_type_string:
if (json_parse_uint64(get_string_component(jso), &cuint) == 0)
return cuint;
/* FALLTHRU */
default:
return 0;
}
}

int json_object_set_int64(struct json_object *jso,int64_t new_value){
if (!jso || jso->o_type!=json_type_int)
return 0;
jso->o.c_int64=new_value;
jso->o.c_int.cint.c_int64=new_value;
jso->o.c_int.cint_type = json_object_int_type_int64;
return 1;
}

int json_object_set_uint64(struct json_object *jso,uint64_t new_value){
if (!jso || jso->o_type!=json_type_int)
return 0;
jso->o.c_int.cint.c_uint64=new_value;
jso->o.c_int.cint_type = json_object_int_type_uint64;
return 1;
}

int json_object_int_inc(struct json_object *jso, int64_t val) {
if (!jso || jso->o_type != json_type_int)
return 0;
if (val > 0 && jso->o.c_int64 > INT64_MAX - val) {
jso->o.c_int64 = INT64_MAX;
} else if (val < 0 && jso->o.c_int64 < INT64_MIN - val) {
jso->o.c_int64 = INT64_MIN;
} else {
jso->o.c_int64 += val;
switch(jso->o.c_int.cint_type) {
case json_object_int_type_int64:
if (val > 0 && jso->o.c_int.cint.c_int64 > INT64_MAX - val) {
jso->o.c_int.cint.c_uint64 = (uint64_t)jso->o.c_int.cint.c_int64 + (uint64_t)val;
jso->o.c_int.cint_type = json_object_int_type_uint64;
} else if (val < 0 && jso->o.c_int.cint.c_int64 < INT64_MIN - val) {
jso->o.c_int.cint.c_int64 = INT64_MIN;
} else {
jso->o.c_int.cint.c_int64 += val;
}
return 1;
case json_object_int_type_uint64:
if (val > 0 && jso->o.c_int.cint.c_uint64 > UINT64_MAX - (uint64_t)val) {
jso->o.c_int.cint.c_uint64 = UINT64_MAX;
} else if (val < 0 && jso->o.c_int.cint.c_uint64 < (uint64_t)(-val)) {
jso->o.c_int.cint.c_int64 = (int64_t)jso->o.c_int.cint.c_uint64 + val;
jso->o.c_int.cint_type = json_object_int_type_int64;
} else if (val < 0 && jso->o.c_int.cint.c_uint64 >= (uint64_t)(-val)) {
jso->o.c_int.cint.c_uint64 -= (uint64_t)(-val);
} else {
jso->o.c_int.cint.c_uint64 += val;
}
return 1;
default:
assert(0);
}
return 1;
}

/* json_object_double */
@@ -961,7 +1061,14 @@ double json_object_get_double(const struct json_object *jso)
case json_type_double:
return jso->o.c_double;
case json_type_int:
return jso->o.c_int64;
switch(jso->o.c_int.cint_type) {
case json_object_int_type_int64:
return jso->o.c_int.cint.c_int64;
case json_object_int_type_uint64:
return jso->o.c_int.cint.c_uint64;
default:
assert(0);
}
case json_type_boolean:
return jso->o.c_boolean;
case json_type_string:
@@ -1340,7 +1447,23 @@ int json_object_equal(struct json_object* jso1, struct json_object* jso2)
return (jso1->o.c_double == jso2->o.c_double);

case json_type_int:
return (jso1->o.c_int64 == jso2->o.c_int64);
if (jso1->o.c_int.cint_type == json_object_int_type_int64) {
if (jso2->o.c_int.cint_type == json_object_int_type_int64) {
return (jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_int64);
} else {
if (jso1->o.c_int.cint.c_int64 < 0)
return 0;
return ((uint64_t)jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_uint64);
}
} else {
if (jso2->o.c_int.cint_type == json_object_int_type_uint64) {
return (jso1->o.c_int.cint.c_uint64 == jso2->o.c_int.cint.c_uint64);
} else {
if (jso2->o.c_int.cint.c_int64 < 0)
return 0;
return (jso1->o.c_int.cint.c_uint64 == (uint64_t)jso2->o.c_int.cint.c_int64);
}
}

case json_type_string:
return (jso1->o.c_string.len == jso2->o.c_string.len &&
@@ -1402,7 +1525,16 @@ int json_c_shallow_copy_default(json_object *src, json_object *parent, const cha
break;

case json_type_int:
*dst = json_object_new_int64(src->o.c_int64);
switch(src->o.c_int.cint_type) {
case json_object_int_type_int64:
*dst = json_object_new_int64(src->o.c_int.cint.c_int64);
break;
case json_object_int_type_uint64:
*dst = json_object_new_uint64(src->o.c_int.cint.c_uint64);
break;
default:
assert(0);
}
break;

case json_type_string:


+ 33
- 1
json_object.h View File

@@ -701,6 +701,13 @@ JSON_EXPORT struct json_object* json_object_new_int(int32_t i);
JSON_EXPORT struct json_object* json_object_new_int64(int64_t i);


/** Create a new empty json_object of type json_type_uint
* @param i the integer
* @returns a json_object of type json_type_uint
*/
JSON_EXPORT struct json_object* json_object_new_uint64(uint64_t i);


/** Get the int value of a json_object
*
* The type is coerced to a int if the passed object is not a int.
@@ -745,7 +752,6 @@ JSON_EXPORT int json_object_set_int(struct json_object *obj,int new_value);
*/
JSON_EXPORT int json_object_int_inc(struct json_object *obj, int64_t val);


/** Get the int value of a json_object
*
* The type is coerced to a int64 if the passed object is not a int64.
@@ -761,6 +767,20 @@ JSON_EXPORT int json_object_int_inc(struct json_object *obj, int64_t val);
*/
JSON_EXPORT int64_t json_object_get_int64(const struct json_object *obj);

/** Get the uint value of a json_object
*
* The type is coerced to a uint64 if the passed object is not a uint64.
* double objects will return their uint64 conversion. Strings will be
* parsed as an uint64. If no conversion exists then 0 is returned.
*
* NOTE: Set errno to 0 directly before a call to this function to determine
* whether or not conversion was successful (it does not clear the value for
* you).
*
* @param obj the json_object instance
* @returns an uint64
*/
JSON_EXPORT uint64_t json_object_get_uint64(const struct json_object *obj);

/** Set the int64_t value of a json_object
*
@@ -774,6 +794,18 @@ JSON_EXPORT int64_t json_object_get_int64(const struct json_object *obj);
*/
JSON_EXPORT int json_object_set_int64(struct json_object *obj,int64_t new_value);

/** Set the uint64_t value of a json_object
*
* The type of obj is checked to be a json_type_uint and 0 is returned
* if it is not without any further actions. If type of obj is json_type_uint
* the object value is changed to new_value
*
* @param obj the json_object instance
* @param new_value the value to be set
* @returns 1 if value is set correctly, 0 otherwise
*/
JSON_EXPORT int json_object_set_uint64(struct json_object *obj,uint64_t new_value);

/* double type methods */

/** Create a new empty json_object of type json_type_double


+ 13
- 1
json_object_private.h View File

@@ -24,6 +24,12 @@ extern "C" {

typedef void (json_object_private_delete_fn)(struct json_object *o);

/* json object int type, support extension*/
typedef enum json_object_int_type {
json_object_int_type_int64,
json_object_int_type_uint64
}json_object_int_type;

struct json_object
{
enum json_type o_type;
@@ -34,7 +40,13 @@ struct json_object
union data {
json_bool c_boolean;
double c_double;
int64_t c_int64;
struct {
union {
int64_t c_int64;
uint64_t c_uint64;
} cint;
enum json_object_int_type cint_type;
} c_int;
struct lh_table *c_object;
struct array_list *c_array;
struct {


+ 21
- 9
json_tokener.c View File

@@ -792,21 +792,33 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok,
}
{
int64_t num64;
uint64_t numuint64;
double numd;
if (!tok->is_double && json_parse_int64(tok->pb->buf, &num64) == 0) {
if (num64 && tok->pb->buf[0]=='0' &&
if (!tok->is_double && tok->pb->buf[0] == '-'
&& json_parse_int64(tok->pb->buf, &num64) == 0) {
current = json_object_new_int64(num64);
if(current == NULL)
goto out;
} else if ( !tok->is_double && tok->pb->buf[0] != '-'
&& json_parse_uint64(tok->pb->buf, &numuint64) == 0) {
if (numuint64 && tok->pb->buf[0]=='0' &&
(tok->flags & JSON_TOKENER_STRICT)) {
/* in strict mode, number must not start with 0 */
tok->err = json_tokener_error_parse_number;
goto out;
}
current = json_object_new_int64(num64);
if(current == NULL)
goto out;
}
else if(tok->is_double && json_parse_double(tok->pb->buf, &numd) == 0)
if (numuint64 <= INT64_MAX){
num64 = (uint64_t) numuint64;
current = json_object_new_int64(num64);
if(current == NULL)
goto out;
} else {
current = json_object_new_uint64(numuint64);
if(current == NULL)
goto out;
}
} else if(tok->is_double && json_parse_double(tok->pb->buf, &numd) == 0)
{
current = json_object_new_double_s(numd, tok->pb->buf);
current = json_object_new_double_s(numd, tok->pb->buf);
if(current == NULL)
goto out;
} else {


+ 19
- 1
json_util.c View File

@@ -40,8 +40,9 @@

#ifdef WIN32
# if MSC_VER < 1800
/* strtoll is available only since Visual Studio 2013 */
/* strtoll/strtoull is available only since Visual Studio 2013 */
# define strtoll _strtoi64
# define strtoull _strtoui64
# endif
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
@@ -230,6 +231,23 @@ int json_parse_int64(const char *buf, int64_t *retval)
return ((val == 0 && errno != 0) || (end == buf)) ? 1 : 0;
}

int json_parse_uint64(const char *buf, uint64_t *retval)
{
char *end = NULL;
uint64_t val;
errno = 1;

while (*buf == ' ') {
buf++;
}
if (*buf == '-') errno = 0;

val = strtoull(buf, &end, 10);
if (end != buf)
*retval = val;
return ((errno == 0) || (end == buf)) ? 1 : 0;
}

#ifndef HAVE_REALLOC
void* rpl_realloc(void* p, size_t n)
{


+ 1
- 0
json_util.h View File

@@ -103,6 +103,7 @@ JSON_EXPORT const char *json_util_get_last_err(void);


JSON_EXPORT int json_parse_int64(const char *buf, int64_t *retval);
JSON_EXPORT int json_parse_uint64(const char *buf, uint64_t *retval);
JSON_EXPORT int json_parse_double(const char *buf, double *retval);

/**


+ 10
- 3
tests/test_cast.c 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,


+ 22
- 7
tests/test_cast.expected View File

@@ -4,43 +4,57 @@ 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

@@ -52,5 +66,6 @@ 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.int64_number : 0,0,0,1,0,0,0
new_obj.negative_number : 0,0,0,1,0,0,0
new_obj.a_null : 1,0,0,0,0,0,0

+ 65
- 0
tests/test_compare.c View File

@@ -15,6 +15,11 @@ 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 *int4 = 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);
struct json_object *uint4 = json_object_new_uint64((uint64_t)INT64_MAX + 100);

if (!json_object_equal(int1, int2))
printf("JSON integer comparison is correct\n");
@@ -31,9 +36,69 @@ 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");

if (json_object_equal(int2, uint2))
printf("JSON integer & usigned integer comparison is correct\n");
else
printf("JSON integer & usigned integer comparison failed\n");

if (!json_object_equal(int2, uint4))
printf("JSON integer & usigned integer comparison is correct\n");
else
printf("JSON integer & usigned integer comparison failed\n");

if (!json_object_equal(int4, uint2))
printf("JSON integer & usigned integer comparison is correct\n");
else
printf("JSON integer & usigned integer comparison failed\n");

if (!json_object_equal(int4, uint4))
printf("JSON integer & usigned integer comparison is correct\n");
else
printf("JSON integer & usigned integer comparison failed\n");

if (json_object_equal(uint2, int2))
printf("JSON usigned integer & integer comparison is correct\n");
else
printf("JSON usigned integer & integer comparison failed\n");

if (!json_object_equal(uint2, int4))
printf("JSON usigned integer & integer comparison is correct\n");
else
printf("JSON usigned integer & integer comparison failed\n");

if (!json_object_equal(uint4, int2))
printf("JSON usigned integer & integer comparison is correct\n");
else
printf("JSON usigned integer & integer comparison failed\n");

if (!json_object_equal(uint4, int4))
printf("JSON usigned integer & integer comparison is correct\n");
else
printf("JSON usigned integer & integer comparison failed\n");

json_object_put(int1);
json_object_put(int2);
json_object_put(int3);
json_object_put(int4);
json_object_put(uint1);
json_object_put(uint2);
json_object_put(uint3);
json_object_put(uint4);

/* string tests */
struct json_object *str1 = json_object_new_string("TESTSTRING");


+ 11
- 0
tests/test_compare.expected View File

@@ -1,6 +1,17 @@
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
JSON integer & usigned integer comparison is correct
JSON integer & usigned integer comparison is correct
JSON integer & usigned integer comparison is correct
JSON integer & usigned integer comparison is correct
JSON usigned integer & integer comparison is correct
JSON usigned integer & integer comparison is correct
JSON usigned integer & integer comparison is correct
JSON usigned integer & integer comparison is correct
Comparing equal strings is correct
Comparing different strings is correct
Comparing equal doubles is correct


+ 16
- 1
tests/test_int_add.c View File

@@ -30,8 +30,10 @@ int main(int argc, char **argv)
tmp = json_object_new_int64(INT64_MAX);
json_object_int_inc(tmp, 100);
assert(json_object_get_int64(tmp) == INT64_MAX);
assert(json_object_get_uint64(tmp) == (uint64_t)INT64_MAX + 100U);
json_object_int_inc(tmp, -100);
assert(json_object_get_int64(tmp) != INT64_MAX);
assert(json_object_get_int64(tmp) == INT64_MAX);
assert(json_object_get_uint64(tmp) == (uint64_t)INT64_MAX);
json_object_put(tmp);
printf("INT64 ADD OVERFLOW PASSED\n");
tmp = json_object_new_int64(INT64_MIN);
@@ -41,6 +43,19 @@ int main(int argc, char **argv)
assert(json_object_get_int64(tmp) != INT64_MIN);
json_object_put(tmp);
printf("INT64 ADD UNDERFLOW PASSED\n");
// uint64 + negative int64--> negative int64
tmp = json_object_new_uint64(400);
json_object_int_inc(tmp, -200);
assert(json_object_get_int64(tmp) == 200);
assert(json_object_get_uint64(tmp) == 200);
json_object_put(tmp);
printf("UINT64 ADD PASSED\n");
tmp = json_object_new_uint64(100);
json_object_int_inc(tmp, -200);
assert(json_object_get_int64(tmp) == -100);
assert(json_object_get_uint64(tmp) == 0);
json_object_put(tmp);
printf("UINT64 ADD UNDERFLOW PASSED\n");

printf("PASSED\n");
return 0;


+ 2
- 0
tests/test_int_add.expected 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 UNDERFLOW PASSED
PASSED

+ 11
- 1
tests/test_parse.c 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()


+ 9
- 1
tests/test_parse.expected 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


+ 78
- 4
tests/test_parse_int64.c 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;
}

+ 28
- 0
tests/test_parse_int64.expected 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

+ 16
- 0
tests/test_set_value.c 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"


+ 1
- 0
tests/test_set_value.expected View File

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


Loading…
Cancel
Save