#include "query.hpp" #include "trace.hpp" void api() { DECLARE( path p ); DECLARE( path q ); ASSERT( p.empty() ); ASSERT( q.empty() ); TRACE( p.extend( 1 ) ); ASSERT( !p.empty() ); ASSERT( p.front_s().empty() ); ASSERT( p.back_s().empty() ); ASSERT( p.front_i() == 1 ); ASSERT( p.back_i() == 1 ); ASSERT( p.tail().empty() ); TRACE( q.extend( "x" ) ); ASSERT( q.front_s() == "x" ); ASSERT( q.back_s() == "x" ); ASSERT( q.front_i() == -1 ); ASSERT( q.back_i() == -1 ); ASSERT( q.tail().empty() ); TRACE( p.extend( 2 ) ); ASSERT( p.front_i() == 1 ); ASSERT( p.back_i() == 2 ); ASSERT( p.tail().back_i() == 2 ); ASSERT( p.tail().front_i() == 2 ); CLEAR(); } void iter() { int i = 0; DECLARE( auto f = []( path, const json_value &v ) { return v.type() == json_type::integer ? v.int_value() : 0; } ); DECLARE( auto odd = []( path p, const auto & ) { return !p.empty() && p.back_i() % 2; } ); DECLARE( auto q = query( "[ 1, 2, 3 ]" ) ); SAVE(); ASSERT_FOR( ( p, x ), q.map( f ).filter( odd ), ( ++i, x == 2 ) ); ASSERT( i == 1 ); CLEAR(); ASSERT_FOR( ( p, x ), q.filter( odd ).map( f ), ( ++i, x == 2 ) ); ASSERT( i == 2 ); CLEAR(); ASSERT_FOR( ( p, x ), q.filter( odd ).map( f ), ( ++i, p.back_i() % 2 == 1 ) ); ASSERT( i == 3 ); CLEAR(); ASSERT_FOR( ( p, x ), q.map( f ).filter( odd ), ( ++i, p.back_i() % 2 == 1 ) ); ASSERT( i == 4 ); CLEAR(); } int main() { api(); iter(); }