corelib/tests/srd-mem-target.cc

196 lines
5.6 KiB
C++
Raw Normal View History

#include <lw/lib/SRDIO.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace lw;
/* - SRDMemTargetTest ---------------------------------------------------{{{-*/
class SRDMemTargetTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( SRDMemTargetTest );
CPPUNIT_TEST( testStartEnd );
CPPUNIT_TEST( testPushUnstructured );
CPPUNIT_TEST( testPushUnstructuredExtraEnd );
CPPUNIT_TEST( testPushUnstructuredUnterminated );
CPPUNIT_TEST( testPushStructured );
CPPUNIT_TEST( testPushStructuredExtraEnd );
CPPUNIT_TEST( testPushStructuredUnterminated );
CPPUNIT_TEST_SUITE_END( );
size_t counter;
T_String location = T_String( "test" );
T_SRDToken && mkToken( T_SRDToken && t )
{
t.location( location , counter ++ );
return std::move( t );
}
public:
void setUp( ) override
{ counter = 0; }
void testStartEnd( );
void testPushUnstructured( );
void testPushUnstructuredExtraEnd( );
void testPushUnstructuredUnterminated( );
void testPushStructured( );
void testPushStructuredExtraEnd( );
void testPushStructuredUnterminated( );
};
#define M_CKTOK_(ITEM,TYPE,POS) \
do { \
CPPUNIT_ASSERT( E_SRDTokenType::TYPE == (ITEM).type( ) ); \
CPPUNIT_ASSERT( (ITEM).hasLocation( ) ); \
CPPUNIT_ASSERT( location == (ITEM).location( ).source( ) ); \
CPPUNIT_ASSERT_EQUAL( size_t( POS ) , \
(ITEM).location( ).byte( ) ); \
} while ( 0 )
#define M_CKERR_(IDX,STR,POS) \
do { \
auto const& _e( errors[ IDX ] ); \
CPPUNIT_ASSERT( T_String( STR ) == _e.error( ) ); \
CPPUNIT_ASSERT( location == _e.location( ).source( ) ); \
CPPUNIT_ASSERT_EQUAL( size_t( POS ) , \
_e.location( ).byte( ) ); \
} while ( 0 )
CPPUNIT_TEST_SUITE_REGISTRATION( SRDMemTargetTest );
void SRDMemTargetTest::testStartEnd( ) // {{{
{
T_SRDErrors errors;
{
T_SRDMemoryTarget mt( false );
mt.start( errors );
mt.end( errors );
}
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , errors.size( ) );
}
// }}}
void SRDMemTargetTest::testPushUnstructured( ) // {{{
{
T_SRDErrors errors;
T_SRDMemoryTarget mt( false );
{
T_SRDReaderTargetHelper rth( mt , errors );
mt.push( errors , mkToken( T_SRDToken::ListStart( ) ) );
mt.push( errors , mkToken( T_SRDToken::Integer( 123 ) ) );
mt.push( errors , mkToken( T_SRDToken::ListEnd( ) ) );
}
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , errors.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 3 ) , mt.list( ).size( ) );
M_CKTOK_( mt.list( )[ 0 ] , START , 0 );
M_CKTOK_( mt.list( )[ 1 ] , INT , 1 );
M_CKTOK_( mt.list( )[ 2 ] , END , 2 );
}
// }}}
void SRDMemTargetTest::testPushUnstructuredExtraEnd( ) // {{{
{
T_SRDErrors errors;
T_SRDMemoryTarget mt( false );
{
T_SRDReaderTargetHelper rth( mt , errors );
mt.push( errors , mkToken( T_SRDToken::ListStart( ) ) );
mt.push( errors , mkToken( T_SRDToken::Integer( 123 ) ) );
mt.push( errors , mkToken( T_SRDToken::ListEnd( ) ) );
mt.push( errors , mkToken( T_SRDToken::ListEnd( ) ) );
}
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , errors.size( ) );
M_CKERR_( 0 , "unexpected ')'" , 3 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 3 ) , mt.list( ).size( ) );
M_CKTOK_( mt.list( )[ 0 ] , START , 0 );
M_CKTOK_( mt.list( )[ 1 ] , INT , 1 );
M_CKTOK_( mt.list( )[ 2 ] , END , 2 );
}
// }}}
void SRDMemTargetTest::testPushUnstructuredUnterminated( ) // {{{
{
T_SRDErrors errors;
T_SRDMemoryTarget mt( false );
{
T_SRDReaderTargetHelper rth( mt , errors );
mt.push( errors , mkToken( T_SRDToken::ListStart( ) ) );
mt.push( errors , mkToken( T_SRDToken::Integer( 123 ) ) );
mt.push( errors , mkToken( T_SRDToken::ListStart( ) ) );
}
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , errors.size( ) );
M_CKERR_( 0 , "unterminated list" , 0 );
M_CKERR_( 1 , "unterminated list" , 2 );
}
// }}}
void SRDMemTargetTest::testPushStructured( ) // {{{
{
T_SRDErrors errors;
T_SRDMemoryTarget mt( true );
{
T_SRDReaderTargetHelper rth( mt , errors );
mt.push( errors , mkToken( T_SRDToken::ListStart( ) ) );
mt.push( errors , mkToken( T_SRDToken::Integer( 123 ) ) );
mt.push( errors , mkToken( T_SRDToken::ListEnd( ) ) );
}
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , errors.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , mt.list( ).size( ) );
M_CKTOK_( mt.list( )[ 0 ] , LIST , 0 );
auto const& list( mt.list( )[ 0 ].list( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , list.size( ) );
M_CKTOK_( list[ 0 ] , INT , 1 );
}
// }}}
void SRDMemTargetTest::testPushStructuredExtraEnd( ) // {{{
{
T_SRDErrors errors;
T_SRDMemoryTarget mt( true );
{
T_SRDReaderTargetHelper rth( mt , errors );
mt.push( errors , mkToken( T_SRDToken::ListStart( ) ) );
mt.push( errors , mkToken( T_SRDToken::Integer( 123 ) ) );
mt.push( errors , mkToken( T_SRDToken::ListEnd( ) ) );
mt.push( errors , mkToken( T_SRDToken::ListEnd( ) ) );
}
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , errors.size( ) );
M_CKERR_( 0 , "unexpected ')'" , 3 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , mt.list( ).size( ) );
M_CKTOK_( mt.list( )[ 0 ] , LIST , 0 );
auto const& list( mt.list( )[ 0 ].list( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , list.size( ) );
M_CKTOK_( list[ 0 ] , INT , 1 );
}
// }}}
void SRDMemTargetTest::testPushStructuredUnterminated( ) // {{{
{
T_SRDErrors errors;
T_SRDMemoryTarget mt( true );
{
T_SRDReaderTargetHelper rth( mt , errors );
mt.push( errors , mkToken( T_SRDToken::ListStart( ) ) );
mt.push( errors , mkToken( T_SRDToken::Integer( 123 ) ) );
mt.push( errors , mkToken( T_SRDToken::ListStart( ) ) );
}
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , errors.size( ) );
M_CKERR_( 0 , "unterminated list" , 0 );
M_CKERR_( 1 , "unterminated list" , 2 );
}
// }}}
/*------------------------------------------------------------------------}}}-*/