#include "trace.hpp" #include "parser.hpp" int main() { for ( auto s : { "{ x: 0, x: 0 }", "{ x: [], x: 0 }" } ) EXCEPT( json_error, json_parse( s ), "s =", s ); for ( auto s : { "{ x: 0, y: 0 }", "{ x: 0, y: [] }", "{ y: [], x: 0 }", "{ y: {}, x: 0 }", "{ y: 0, x: -1 }", "{ x: -1, y: 0 }" } ) { CLEAR(); DECLARE( auto top = json_parse( s ), "s =", s ); ASSERT( top->type() == json_type::object ); ASSERT( top->length() == 2 ); ASSERT( top->item_at( 0 ).type() == json_type::integer ); ASSERT( top->item_at( "x" ).type() == json_type::integer ); ASSERT( top->item_at( "y" ).length() == 0 ); EXCEPT( std::out_of_range, top->item_at( 2 ) ); EXCEPT( std::out_of_range, top->item_at( "z" ) ); EXCEPT( std::out_of_range, top->item_at( "" ) ); } for ( auto s : { "{ x: { x: 0 }, y: { x: 0 } }", "{ x: { y: 0 }, y: { y: 0 } }", "{ x: [ { x: 0 }, { x: 0 } ] }" } ) ASSERT( json_parse( s )->type() == json_type::object, "s =", s ); for ( int l = 0; l < 10; ++l ) { CLEAR(); std::string s = "{ "; for ( int i = 0; i < l; ++i ) { s += ('a' + i); s += ": " + std::to_string( i ) + ", "; } s += "x: {} }"; DECLARE( auto top = json_parse( s ), "s =", s ); for ( int i = 0; i < l; ++i ) ASSERT( top->item_at( i ).int_value() == i, "i =", i ); for ( int i = 0; i < l; ++i ) ASSERT( top->item_at( std::string( 1, 'a' + i ) ).int_value() == i, "i =", i ); ASSERT( top->item_at( "x" ).type() == json_type::object ); ASSERT( top->item_at( l ).type() == json_type::object ); } }