2017-04-03 00:43:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "tiny-json.h"
|
|
|
|
|
|
|
|
#define done() return 0
|
|
|
|
#define fail() return __LINE__
|
|
|
|
static unsigned int checkqty = 0;
|
|
|
|
#define check( x ) do { ++checkqty; if (!(x)) fail(); } while ( 0 )
|
|
|
|
|
2017-04-05 00:25:45 +00:00
|
|
|
static int empty( void ) {
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t pool[6];
|
2017-04-05 00:25:45 +00:00
|
|
|
unsigned const qty = sizeof pool / sizeof *pool;
|
2017-04-03 00:43:31 +00:00
|
|
|
{
|
|
|
|
char str[] = "{}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( json );
|
|
|
|
json_t const* child = json_getChild( json );
|
|
|
|
check( child == NULL );
|
2017-04-05 00:25:45 +00:00
|
|
|
}
|
2017-04-03 00:43:31 +00:00
|
|
|
{
|
|
|
|
char str[] = "{\"a\":[]}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( json );
|
|
|
|
json_t const* child = json_getChild( json );
|
|
|
|
check( child );
|
|
|
|
char const* childname = json_getName( child );
|
|
|
|
check( childname );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( !strcmp( childname, "a" ) );
|
2017-04-03 00:43:31 +00:00
|
|
|
check( json_getType( child ) == JSON_ARRAY );
|
|
|
|
check( !json_getChild( child ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
}
|
2017-04-03 00:43:31 +00:00
|
|
|
{
|
|
|
|
char str[] = "{\"a\":[{},{}]}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( json );
|
|
|
|
json_t const* child = json_getChild( json );
|
|
|
|
check( child );
|
|
|
|
char const* childname = json_getName( child );
|
|
|
|
check( childname );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( !strcmp( childname, "a" ) );
|
2017-04-03 00:43:31 +00:00
|
|
|
check( json_getType( child ) == JSON_ARRAY );
|
|
|
|
int num = 0;
|
|
|
|
for( json_t const* i = json_getChild( child ); i; ++num, i = json_getSibling( i ) ) {
|
|
|
|
check( i );
|
|
|
|
check( json_getType( i ) == JSON_OBJ );
|
|
|
|
check( !json_getChild( i ) );
|
|
|
|
}
|
|
|
|
check( num == 2 );
|
2017-04-05 00:25:45 +00:00
|
|
|
}
|
2017-04-03 00:43:31 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int primitive( void ) {
|
|
|
|
json_t pool[8];
|
2017-04-05 00:25:45 +00:00
|
|
|
unsigned const qty = sizeof pool / sizeof *pool;
|
2017-04-03 00:43:31 +00:00
|
|
|
{
|
|
|
|
char str[] = "{"
|
2017-04-03 23:38:06 +00:00
|
|
|
"\"max\": 9223372036854775807,"
|
|
|
|
"\"min\": -9223372036854775808,"
|
2017-04-03 00:43:31 +00:00
|
|
|
"\"boolvar0\": false,"
|
|
|
|
"\"boolvar1\": true,"
|
|
|
|
"\"nullvar\": null,"
|
|
|
|
"\"scientific\": 5368.32e-3,"
|
2017-04-05 00:25:45 +00:00
|
|
|
"\"real\": -0.25,"
|
2017-04-03 00:43:31 +00:00
|
|
|
"}";
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( json );
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t const* boolvar0 = json_getProperty( json, "boolvar0" );
|
|
|
|
check( boolvar0 );
|
|
|
|
check( JSON_BOOLEAN == json_getType( boolvar0 ) );
|
|
|
|
check( !strcmp( "false", json_getValue( boolvar0 ) ) );
|
|
|
|
check( false == json_getBoolean( boolvar0 ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t const* boolvar1 = json_getProperty( json, "boolvar1" );
|
|
|
|
check( boolvar1 );
|
|
|
|
check( JSON_BOOLEAN == json_getType( boolvar1 ) );
|
|
|
|
check( !strcmp( "true", json_getValue( boolvar1 ) ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( true == json_getBoolean( boolvar1 ) );
|
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t const* nullvar = json_getProperty( json, "nullvar" );
|
|
|
|
check( nullvar );
|
|
|
|
check( JSON_NULL == json_getType( nullvar ) );
|
|
|
|
check( !strcmp( "null", json_getValue( nullvar ) ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t const* max = json_getProperty( json, "max" );
|
|
|
|
check( max );
|
|
|
|
check( JSON_INTEGER == json_getType( max ) );
|
|
|
|
check( !strcmp( "9223372036854775807", json_getValue( max ) ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( INT64_MAX == json_getInteger( max ) );
|
2017-04-03 00:43:31 +00:00
|
|
|
|
|
|
|
json_t const* min = json_getProperty( json, "min" );
|
|
|
|
check( max );
|
|
|
|
check( JSON_INTEGER == json_getType( max ) );
|
|
|
|
check( !strcmp( "-9223372036854775808", json_getValue( min ) ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( INT64_MIN == json_getInteger( min ) );
|
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t const* real = json_getProperty( json, "real" );
|
|
|
|
check( real );
|
|
|
|
check( JSON_REAL == json_getType( real ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( !strcmp( "-0.25", json_getValue( real ) ) );
|
|
|
|
check( -0.25 == json_getReal( real ) );
|
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t const* scientific = json_getProperty( json, "scientific" );
|
|
|
|
check( scientific );
|
2017-04-03 21:22:15 +00:00
|
|
|
check( JSON_REAL == json_getType( scientific ) );
|
2017-04-03 00:43:31 +00:00
|
|
|
check( !strcmp( "5368.32e-3", json_getValue( scientific ) ) );
|
2017-04-03 23:38:06 +00:00
|
|
|
check( 5368.32e-3 == json_getReal( scientific ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-05 00:25:45 +00:00
|
|
|
done();
|
2017-04-03 00:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int text( void ) {
|
|
|
|
json_t pool[2];
|
|
|
|
unsigned const qty = sizeof pool / sizeof *pool;
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
char str[] = "{\"a\":\"\\tThis text: \\\"Hello\\\".\\n\"}";
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( json );
|
|
|
|
|
|
|
|
json_t const* a = json_getProperty( json, "a" );
|
|
|
|
check( a );
|
|
|
|
check( JSON_TEXT == json_getType( a ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
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 );
|
|
|
|
}
|
2017-04-03 00:43:31 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int array( void ) {
|
|
|
|
json_t pool[7];
|
|
|
|
unsigned const qty = sizeof pool / sizeof *pool;
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
char str[] = "{\"array\":[ 1, true, null, \"Text\", 0.3232 ]}";
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t const* json = json_create( str, pool, qty );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( json );
|
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
json_t const* array = json_getProperty( json, "array" );
|
|
|
|
check( array );
|
|
|
|
check( JSON_ARRAY == json_getType( array ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
static struct { jsonType_t type; char const* value; } const pairs[] = {
|
|
|
|
{ JSON_INTEGER, "1" }, { JSON_BOOLEAN, "true" }, { JSON_NULL, "null" },
|
2017-04-05 00:25:45 +00:00
|
|
|
{ JSON_TEXT, "Text" }, { JSON_REAL, "0.3232" }
|
2017-04-03 00:43:31 +00:00
|
|
|
};
|
|
|
|
unsigned const len = sizeof pairs / sizeof *pairs;
|
|
|
|
json_t const* element = json_getChild( array );
|
|
|
|
for( unsigned int i = 0; i < len; ++i, element = json_getSibling( element ) ) {
|
|
|
|
check( element );
|
|
|
|
check( pairs[i].type == json_getType( element ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( !strcmp( pairs[i].value, json_getValue( element ) ) );
|
2017-04-03 00:43:31 +00:00
|
|
|
}
|
|
|
|
check( !element );
|
2017-04-05 00:25:45 +00:00
|
|
|
|
2017-04-03 00:43:31 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
int badformat( void ) {
|
|
|
|
json_t pool[2];
|
|
|
|
unsigned const qty = sizeof pool / sizeof *pool;
|
|
|
|
{
|
|
|
|
char str[] = "{\"var:true}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( !json );
|
|
|
|
}
|
|
|
|
{
|
|
|
|
char str[] = "{\"var\":tr}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( !json );
|
|
|
|
}
|
|
|
|
{
|
|
|
|
char str[] = "{\"var\":true";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( !json );
|
2017-04-05 00:25:45 +00:00
|
|
|
}
|
2017-04-03 00:43:31 +00:00
|
|
|
{
|
|
|
|
char str[] = "{\"var\":true} text outside json";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( json );
|
|
|
|
json_t const* var = json_getProperty( json, "var" );
|
|
|
|
check( var );
|
|
|
|
check( JSON_BOOLEAN == json_getType( var ) );
|
|
|
|
check( !strcmp( "true", json_getValue( var ) ) );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( true == json_getBoolean( var ) );
|
2017-04-03 21:22:15 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
char str[] = "{\"var\":truep}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( !json );
|
2017-04-03 21:22:15 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
char str[] = "{\"var\":0s}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( !json );
|
|
|
|
}
|
2017-04-03 23:38:06 +00:00
|
|
|
{
|
|
|
|
char str[] = "{\"var\":9223372036854775808}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( !json );
|
|
|
|
}
|
2017-04-03 23:38:06 +00:00
|
|
|
{
|
|
|
|
char str[] = "{\"var\":-9223372036854775809}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
2017-04-05 00:25:45 +00:00
|
|
|
check( !json );
|
|
|
|
}
|
2017-04-03 23:38:06 +00:00
|
|
|
{
|
|
|
|
char str[] = "{\"var\":9}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( json );
|
2017-04-05 00:25:45 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
char str[] = "{\"var\":,9}";
|
|
|
|
json_t const* json = json_create( str, pool, qty );
|
|
|
|
check( !json );
|
|
|
|
}
|
2017-04-03 00:43:31 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct test {
|
|
|
|
int(*func)(void);
|
|
|
|
char const* name;
|
2017-04-05 00:25:45 +00:00
|
|
|
};
|
2017-04-03 00:43:31 +00:00
|
|
|
|
|
|
|
static int test_exec( struct test const* test ) {
|
|
|
|
int const err = test->func();
|
|
|
|
if ( err ) {
|
|
|
|
fprintf( stderr, "%s%s%s%d%s", "Failed test: '", test->name, "' Line: ", err, ".\n" );
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct test const tests[] = {
|
2017-04-05 00:25:45 +00:00
|
|
|
{ empty, "Empty object and array" },
|
|
|
|
{ primitive, "Primitive properties" },
|
|
|
|
{ text, "Text" },
|
|
|
|
{ array, "Array" },
|
|
|
|
{ badformat, "Bad format" },
|
|
|
|
{ goodformats, "Formats" },
|
2017-04-03 00:43:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int main( void ) {
|
|
|
|
int failed = 0;
|
|
|
|
unsigned int const qty = sizeof tests / sizeof *tests;
|
|
|
|
for( unsigned int i = 0; i < qty; ++i )
|
|
|
|
failed += test_exec( tests + i );
|
|
|
|
unsigned int const percent = 100.0 * ( qty - failed ) / qty;
|
|
|
|
printf( "%d%s%d%s", percent, "%. ", checkqty, " checks.\n" );
|
|
|
|
return failed;
|
|
|
|
}
|