|
|
@ -11,16 +11,16 @@ |
|
|
|
static unsigned int checkqty = 0; |
|
|
|
static unsigned int checkqty = 0; |
|
|
|
#define check( x ) do { ++checkqty; if (!(x)) fail(); } while ( 0 ) |
|
|
|
#define check( x ) do { ++checkqty; if (!(x)) fail(); } while ( 0 ) |
|
|
|
|
|
|
|
|
|
|
|
static int empty( void ) {
|
|
|
|
static int empty( void ) { |
|
|
|
json_t pool[6]; |
|
|
|
json_t pool[6]; |
|
|
|
unsigned const qty = sizeof pool / sizeof *pool;
|
|
|
|
unsigned const qty = sizeof pool / sizeof *pool; |
|
|
|
{ |
|
|
|
{ |
|
|
|
char str[] = "{}"; |
|
|
|
char str[] = "{}"; |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
check( json ); |
|
|
|
check( json ); |
|
|
|
json_t const* child = json_getChild( json ); |
|
|
|
json_t const* child = json_getChild( json ); |
|
|
|
check( child == NULL ); |
|
|
|
check( child == NULL ); |
|
|
|
}
|
|
|
|
} |
|
|
|
{ |
|
|
|
{ |
|
|
|
char str[] = "{\"a\":[]}"; |
|
|
|
char str[] = "{\"a\":[]}"; |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
@ -29,10 +29,10 @@ static int empty( void ) { |
|
|
|
check( child ); |
|
|
|
check( child ); |
|
|
|
char const* childname = json_getName( child ); |
|
|
|
char const* childname = json_getName( child ); |
|
|
|
check( childname ); |
|
|
|
check( childname ); |
|
|
|
check( !strcmp( childname, "a" ) );
|
|
|
|
check( !strcmp( childname, "a" ) ); |
|
|
|
check( json_getType( child ) == JSON_ARRAY ); |
|
|
|
check( json_getType( child ) == JSON_ARRAY ); |
|
|
|
check( !json_getChild( child ) ); |
|
|
|
check( !json_getChild( child ) ); |
|
|
|
}
|
|
|
|
} |
|
|
|
{ |
|
|
|
{ |
|
|
|
char str[] = "{\"a\":[{},{}]}"; |
|
|
|
char str[] = "{\"a\":[{},{}]}"; |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
@ -41,7 +41,7 @@ static int empty( void ) { |
|
|
|
check( child ); |
|
|
|
check( child ); |
|
|
|
char const* childname = json_getName( child ); |
|
|
|
char const* childname = json_getName( child ); |
|
|
|
check( childname ); |
|
|
|
check( childname ); |
|
|
|
check( !strcmp( childname, "a" ) );
|
|
|
|
check( !strcmp( childname, "a" ) ); |
|
|
|
check( json_getType( child ) == JSON_ARRAY ); |
|
|
|
check( json_getType( child ) == JSON_ARRAY ); |
|
|
|
int num = 0; |
|
|
|
int num = 0; |
|
|
|
for( json_t const* i = json_getChild( child ); i; ++num, i = json_getSibling( i ) ) { |
|
|
|
for( json_t const* i = json_getChild( child ); i; ++num, i = json_getSibling( i ) ) { |
|
|
@ -50,13 +50,13 @@ static int empty( void ) { |
|
|
|
check( !json_getChild( i ) ); |
|
|
|
check( !json_getChild( i ) ); |
|
|
|
} |
|
|
|
} |
|
|
|
check( num == 2 ); |
|
|
|
check( num == 2 ); |
|
|
|
}
|
|
|
|
} |
|
|
|
done(); |
|
|
|
done(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static int primitive( void ) { |
|
|
|
static int primitive( void ) { |
|
|
|
json_t pool[8]; |
|
|
|
json_t pool[8]; |
|
|
|
unsigned const qty = sizeof pool / sizeof *pool;
|
|
|
|
unsigned const qty = sizeof pool / sizeof *pool; |
|
|
|
{ |
|
|
|
{ |
|
|
|
char str[] = "{" |
|
|
|
char str[] = "{" |
|
|
|
"\"max\": 9223372036854775807," |
|
|
|
"\"max\": 9223372036854775807," |
|
|
@ -65,102 +65,144 @@ static int primitive( void ) { |
|
|
|
"\"boolvar1\": true," |
|
|
|
"\"boolvar1\": true," |
|
|
|
"\"nullvar\": null," |
|
|
|
"\"nullvar\": null," |
|
|
|
"\"scientific\": 5368.32e-3," |
|
|
|
"\"scientific\": 5368.32e-3," |
|
|
|
"\"real\": -0.004," |
|
|
|
"\"real\": -0.25," |
|
|
|
"}"; |
|
|
|
"}"; |
|
|
|
|
|
|
|
|
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
check( json ); |
|
|
|
check( json ); |
|
|
|
|
|
|
|
|
|
|
|
json_t const* boolvar0 = json_getProperty( json, "boolvar0" ); |
|
|
|
json_t const* boolvar0 = json_getProperty( json, "boolvar0" ); |
|
|
|
check( boolvar0 ); |
|
|
|
check( boolvar0 ); |
|
|
|
check( JSON_BOOLEAN == json_getType( boolvar0 ) ); |
|
|
|
check( JSON_BOOLEAN == json_getType( boolvar0 ) ); |
|
|
|
check( !strcmp( "false", json_getValue( boolvar0 ) ) ); |
|
|
|
check( !strcmp( "false", json_getValue( boolvar0 ) ) ); |
|
|
|
check( false == json_getBoolean( boolvar0 ) ); |
|
|
|
check( false == json_getBoolean( boolvar0 ) ); |
|
|
|
|
|
|
|
|
|
|
|
json_t const* boolvar1 = json_getProperty( json, "boolvar1" ); |
|
|
|
json_t const* boolvar1 = json_getProperty( json, "boolvar1" ); |
|
|
|
check( boolvar1 ); |
|
|
|
check( boolvar1 ); |
|
|
|
check( JSON_BOOLEAN == json_getType( boolvar1 ) ); |
|
|
|
check( JSON_BOOLEAN == json_getType( boolvar1 ) ); |
|
|
|
check( !strcmp( "true", json_getValue( boolvar1 ) ) ); |
|
|
|
check( !strcmp( "true", json_getValue( boolvar1 ) ) ); |
|
|
|
check( true == json_getBoolean( boolvar1 ) );
|
|
|
|
check( true == json_getBoolean( boolvar1 ) ); |
|
|
|
|
|
|
|
|
|
|
|
json_t const* nullvar = json_getProperty( json, "nullvar" ); |
|
|
|
json_t const* nullvar = json_getProperty( json, "nullvar" ); |
|
|
|
check( nullvar ); |
|
|
|
check( nullvar ); |
|
|
|
check( JSON_NULL == json_getType( nullvar ) ); |
|
|
|
check( JSON_NULL == json_getType( nullvar ) ); |
|
|
|
check( !strcmp( "null", json_getValue( nullvar ) ) ); |
|
|
|
check( !strcmp( "null", json_getValue( nullvar ) ) ); |
|
|
|
|
|
|
|
|
|
|
|
json_t const* max = json_getProperty( json, "max" ); |
|
|
|
json_t const* max = json_getProperty( json, "max" ); |
|
|
|
check( max ); |
|
|
|
check( max ); |
|
|
|
check( JSON_INTEGER == json_getType( max ) ); |
|
|
|
check( JSON_INTEGER == json_getType( max ) ); |
|
|
|
check( !strcmp( "9223372036854775807", json_getValue( max ) ) ); |
|
|
|
check( !strcmp( "9223372036854775807", json_getValue( max ) ) ); |
|
|
|
check( INT64_MAX == json_getInteger( max ) );
|
|
|
|
check( INT64_MAX == json_getInteger( max ) ); |
|
|
|
|
|
|
|
|
|
|
|
json_t const* min = json_getProperty( json, "min" ); |
|
|
|
json_t const* min = json_getProperty( json, "min" ); |
|
|
|
check( max ); |
|
|
|
check( max ); |
|
|
|
check( JSON_INTEGER == json_getType( max ) ); |
|
|
|
check( JSON_INTEGER == json_getType( max ) ); |
|
|
|
check( !strcmp( "-9223372036854775808", json_getValue( min ) ) ); |
|
|
|
check( !strcmp( "-9223372036854775808", json_getValue( min ) ) ); |
|
|
|
check( INT64_MIN == json_getInteger( min ) );
|
|
|
|
check( INT64_MIN == json_getInteger( min ) ); |
|
|
|
|
|
|
|
|
|
|
|
json_t const* real = json_getProperty( json, "real" ); |
|
|
|
json_t const* real = json_getProperty( json, "real" ); |
|
|
|
check( real ); |
|
|
|
check( real ); |
|
|
|
check( JSON_REAL == json_getType( real ) ); |
|
|
|
check( JSON_REAL == json_getType( real ) ); |
|
|
|
check( !strcmp( "-0.004", json_getValue( real ) ) ); |
|
|
|
check( !strcmp( "-0.25", json_getValue( real ) ) ); |
|
|
|
check( -0.004 == json_getReal( real ) );
|
|
|
|
check( -0.25 == json_getReal( real ) ); |
|
|
|
|
|
|
|
|
|
|
|
json_t const* scientific = json_getProperty( json, "scientific" ); |
|
|
|
json_t const* scientific = json_getProperty( json, "scientific" ); |
|
|
|
check( scientific ); |
|
|
|
check( scientific ); |
|
|
|
check( JSON_REAL == json_getType( scientific ) ); |
|
|
|
check( JSON_REAL == json_getType( scientific ) ); |
|
|
|
check( !strcmp( "5368.32e-3", json_getValue( scientific ) ) ); |
|
|
|
check( !strcmp( "5368.32e-3", json_getValue( scientific ) ) ); |
|
|
|
check( 5368.32e-3 == json_getReal( scientific ) ); |
|
|
|
check( 5368.32e-3 == json_getReal( scientific ) ); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
done();
|
|
|
|
done(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static int text( void ) { |
|
|
|
static int text( void ) { |
|
|
|
json_t pool[2]; |
|
|
|
json_t pool[2]; |
|
|
|
unsigned const qty = sizeof pool / sizeof *pool; |
|
|
|
unsigned const qty = sizeof pool / sizeof *pool; |
|
|
|
|
|
|
|
|
|
|
|
char str[] = "{\"a\":\"\\tThis text: \\\"Hello\\\".\\n\"}"; |
|
|
|
char str[] = "{\"a\":\"\\tThis text: \\\"Hello\\\".\\n\"}"; |
|
|
|
|
|
|
|
|
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
check( json ); |
|
|
|
check( json ); |
|
|
|
|
|
|
|
|
|
|
|
json_t const* a = json_getProperty( json, "a" ); |
|
|
|
json_t const* a = json_getProperty( json, "a" ); |
|
|
|
check( a ); |
|
|
|
check( a ); |
|
|
|
check( JSON_TEXT == json_getType( a ) ); |
|
|
|
check( JSON_TEXT == json_getType( a ) ); |
|
|
|
check( !strcmp( "\tThis text: \"Hello\".\n", json_getValue( a ) ) );
|
|
|
|
check( !strcmp( "\tThis text: \"Hello\".\n", json_getValue( a ) ) ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int goodformats( void ) { |
|
|
|
|
|
|
|
json_t pool[4]; |
|
|
|
|
|
|
|
unsigned const qty = sizeof pool / sizeof *pool; |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
char str[] = "{\"qwerty\":false,}"; |
|
|
|
|
|
|
|
json_t const* root = json_create( str, pool, qty ); |
|
|
|
|
|
|
|
check( root ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
char str[] = "{\"a\":[0,]}"; |
|
|
|
|
|
|
|
json_t const* root = json_create( str, pool, qty ); |
|
|
|
|
|
|
|
check( root ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
char str[] = "{\"a\":[0],}"; |
|
|
|
|
|
|
|
json_t const* root = json_create( str, pool, qty ); |
|
|
|
|
|
|
|
check( root ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
char str[] = "{\"qwerty\":654,}"; |
|
|
|
|
|
|
|
json_t const* root = json_create( str, pool, qty ); |
|
|
|
|
|
|
|
check( root ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
char str[] = "{\"qwerty\":\"asdfgh\",}"; |
|
|
|
|
|
|
|
json_t const* root = json_create( str, pool, qty ); |
|
|
|
|
|
|
|
check( root ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
char str[] = "{,\"qwerty\":\"asdfgh\",}"; |
|
|
|
|
|
|
|
json_t const* root = json_create( str, pool, qty ); |
|
|
|
|
|
|
|
check( root ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
char str[] = "{,\"a\":1, , \"b\":2,,,,}"; |
|
|
|
|
|
|
|
json_t const* root = json_create( str, pool, qty ); |
|
|
|
|
|
|
|
check( root ); |
|
|
|
|
|
|
|
} |
|
|
|
done(); |
|
|
|
done(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static int array( void ) { |
|
|
|
static int array( void ) { |
|
|
|
json_t pool[7]; |
|
|
|
json_t pool[7]; |
|
|
|
unsigned const qty = sizeof pool / sizeof *pool; |
|
|
|
unsigned const qty = sizeof pool / sizeof *pool; |
|
|
|
|
|
|
|
|
|
|
|
char str[] = "{\"array\":[ 1, true, null, \"Text\", 0.3232 ]}"; |
|
|
|
char str[] = "{\"array\":[ 1, true, null, \"Text\", 0.3232 ]}"; |
|
|
|
|
|
|
|
|
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
check( json );
|
|
|
|
check( json ); |
|
|
|
|
|
|
|
|
|
|
|
json_t const* array = json_getProperty( json, "array" ); |
|
|
|
json_t const* array = json_getProperty( json, "array" ); |
|
|
|
check( array ); |
|
|
|
check( array ); |
|
|
|
check( JSON_ARRAY == json_getType( array ) ); |
|
|
|
check( JSON_ARRAY == json_getType( array ) ); |
|
|
|
|
|
|
|
|
|
|
|
static struct { jsonType_t type; char const* value; } const pairs[] = { |
|
|
|
static struct { jsonType_t type; char const* value; } const pairs[] = { |
|
|
|
{ JSON_INTEGER, "1" }, { JSON_BOOLEAN, "true" }, { JSON_NULL, "null" }, |
|
|
|
{ JSON_INTEGER, "1" }, { JSON_BOOLEAN, "true" }, { JSON_NULL, "null" }, |
|
|
|
{ JSON_TEXT, "Text" }, { JSON_REAL, "0.3232" }
|
|
|
|
{ JSON_TEXT, "Text" }, { JSON_REAL, "0.3232" } |
|
|
|
}; |
|
|
|
}; |
|
|
|
unsigned const len = sizeof pairs / sizeof *pairs; |
|
|
|
unsigned const len = sizeof pairs / sizeof *pairs; |
|
|
|
json_t const* element = json_getChild( array ); |
|
|
|
json_t const* element = json_getChild( array ); |
|
|
|
for( unsigned int i = 0; i < len; ++i, element = json_getSibling( element ) ) { |
|
|
|
for( unsigned int i = 0; i < len; ++i, element = json_getSibling( element ) ) { |
|
|
|
check( element ); |
|
|
|
check( element ); |
|
|
|
check( pairs[i].type == json_getType( element ) ); |
|
|
|
check( pairs[i].type == json_getType( element ) ); |
|
|
|
check( !strcmp( pairs[i].value, json_getValue( element ) ) );
|
|
|
|
check( !strcmp( pairs[i].value, json_getValue( element ) ) ); |
|
|
|
} |
|
|
|
} |
|
|
|
check( !element ); |
|
|
|
check( !element ); |
|
|
|
|
|
|
|
|
|
|
|
done(); |
|
|
|
done(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -181,7 +223,7 @@ int badformat( void ) { |
|
|
|
char str[] = "{\"var\":true"; |
|
|
|
char str[] = "{\"var\":true"; |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
check( !json ); |
|
|
|
check( !json ); |
|
|
|
}
|
|
|
|
} |
|
|
|
{ |
|
|
|
{ |
|
|
|
char str[] = "{\"var\":true} text outside json"; |
|
|
|
char str[] = "{\"var\":true} text outside json"; |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
@ -190,41 +232,45 @@ int badformat( void ) { |
|
|
|
check( var ); |
|
|
|
check( var ); |
|
|
|
check( JSON_BOOLEAN == json_getType( var ) ); |
|
|
|
check( JSON_BOOLEAN == json_getType( var ) ); |
|
|
|
check( !strcmp( "true", json_getValue( var ) ) ); |
|
|
|
check( !strcmp( "true", json_getValue( var ) ) ); |
|
|
|
check( true == json_getBoolean( var ) );
|
|
|
|
check( true == json_getBoolean( var ) ); |
|
|
|
} |
|
|
|
} |
|
|
|
{ |
|
|
|
{ |
|
|
|
char str[] = "{\"var\":truep}"; |
|
|
|
char str[] = "{\"var\":truep}"; |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
check( !json );
|
|
|
|
check( !json ); |
|
|
|
} |
|
|
|
} |
|
|
|
{ |
|
|
|
{ |
|
|
|
char str[] = "{\"var\":0s}"; |
|
|
|
char str[] = "{\"var\":0s}"; |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
check( !json );
|
|
|
|
check( !json ); |
|
|
|
}
|
|
|
|
} |
|
|
|
{ |
|
|
|
{ |
|
|
|
char str[] = "{\"var\":9223372036854775808}"; |
|
|
|
char str[] = "{\"var\":9223372036854775808}"; |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
check( !json );
|
|
|
|
check( !json ); |
|
|
|
}
|
|
|
|
} |
|
|
|
{ |
|
|
|
{ |
|
|
|
char str[] = "{\"var\":-9223372036854775809}"; |
|
|
|
char str[] = "{\"var\":-9223372036854775809}"; |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
check( !json );
|
|
|
|
check( !json ); |
|
|
|
}
|
|
|
|
} |
|
|
|
{ |
|
|
|
{ |
|
|
|
char str[] = "{\"var\":9}"; |
|
|
|
char str[] = "{\"var\":9}"; |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
check( json ); |
|
|
|
check( json ); |
|
|
|
}
|
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
char str[] = "{\"var\":,9}"; |
|
|
|
|
|
|
|
json_t const* json = json_create( str, pool, qty ); |
|
|
|
|
|
|
|
check( !json ); |
|
|
|
|
|
|
|
} |
|
|
|
done(); |
|
|
|
done(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct test { |
|
|
|
struct test { |
|
|
|
int(*func)(void); |
|
|
|
int(*func)(void); |
|
|
|
char const* name; |
|
|
|
char const* name; |
|
|
|
};
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static int test_exec( struct test const* test ) { |
|
|
|
static int test_exec( struct test const* test ) { |
|
|
|
int const err = test->func(); |
|
|
|
int const err = test->func(); |
|
|
@ -236,11 +282,12 @@ static int test_exec( struct test const* test ) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
static struct test const tests[] = { |
|
|
|
static struct test const tests[] = { |
|
|
|
{ empty, "Empty object and array" }, |
|
|
|
{ empty, "Empty object and array" }, |
|
|
|
{ primitive, "Primitive properties" }, |
|
|
|
{ primitive, "Primitive properties" }, |
|
|
|
{ text, "Text" }, |
|
|
|
{ text, "Text" }, |
|
|
|
{ array, "Array" }, |
|
|
|
{ array, "Array" }, |
|
|
|
{ badformat, "Bad format" }, |
|
|
|
{ badformat, "Bad format" }, |
|
|
|
|
|
|
|
{ goodformats, "Formats" }, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
int main( void ) { |
|
|
|
int main( void ) { |
|
|
|