corelib/tests/srd-bin-writer.cc

245 lines
5.2 KiB
C++

#include <ebcl/SRDBinary.hh>
#include <ebcl/MemoryStreams.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace ebcl;
class SRDBinWriterTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( SRDBinWriterTest );
CPPUNIT_TEST( testEmptyList );
CPPUNIT_TEST( testList );
CPPUNIT_TEST( testString );
CPPUNIT_TEST( testComment );
CPPUNIT_TEST( testCommentCoalesce );
CPPUNIT_TEST( testWordUnknown );
CPPUNIT_TEST( testWordKnown );
CPPUNIT_TEST( testVarUnknown );
CPPUNIT_TEST( testVarKnown );
CPPUNIT_TEST( testInt32 );
CPPUNIT_TEST( testInt64 );
CPPUNIT_TEST( testFloat );
CPPUNIT_TEST( testBinary );
CPPUNIT_TEST_SUITE_END( );
public:
void testEmptyList( );
void testList( );
void testString( );
void testComment( );
void testCommentCoalesce( );
void testWordUnknown( );
void testWordKnown( );
void testVarUnknown( );
void testVarKnown( );
void testInt32( );
void testInt64( );
void testFloat( );
void testBinary( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( SRDBinWriterTest );
/*----------------------------------------------------------------------------*/
#define V1PROLOGUE_ \
0xce , 0xa1 , 0x7c , 0xea , \
0x00 , 0xee , 0xff , 0xc0
#define M_RUN_( ) \
{ \
T_Buffer< uint8_t > buffer; \
T_MemoryOutputStream stream( buffer ); \
SRDBinaryWriteTo( stream , list ); \
CPPUNIT_ASSERT_EQUAL( size_t( sizeof( expected ) ) , \
buffer.size( ) ); \
for ( size_t i = 0 ; i < sizeof( expected ) ; i ++ ) { \
CPPUNIT_ASSERT_EQUAL( int( expected[ i ] ) , \
int( buffer[ i ] ) ); \
} \
}
/*----------------------------------------------------------------------------*/
void SRDBinWriterTest::testEmptyList( )
{
T_SRDList list;
const uint8_t expected[] = { V1PROLOGUE_ , 0x00 };
M_RUN_( );
}
void SRDBinWriterTest::testList( )
{
T_SRDList list;
list << T_SRDToken::List( T_SRDList( ) );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x01 , 0x00 ,
0x00
};
M_RUN_( );
}
void SRDBinWriterTest::testString( )
{
T_SRDList list;
list << T_SRDToken::String( T_String( "test" ) );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x05 ,
0x04 , 0x00 , 0x00 , 0x00 ,
't' , 'e' , 's' , 't' ,
0x00
};
M_RUN_( );
}
void SRDBinWriterTest::testComment( )
{
{
T_SRDList list;
list << T_SRDToken::Comment( T_String( "test" ) );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x09 ,
0x04 , 0x00 , 0x00 , 0x00 ,
't' , 'e' , 's' , 't' ,
0x00
};
M_RUN_( );
}
{
T_SRDList list;
list << T_SRDToken::List( T_SRDList( ) << T_SRDToken::Comment(
T_String( "test" ) ) );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x01 ,
0x09 ,
0x04 , 0x00 , 0x00 , 0x00 ,
't' , 'e' , 's' , 't' ,
0x00 , 0x00
};
M_RUN_( );
}
}
void SRDBinWriterTest::testCommentCoalesce( )
{
T_SRDList list;
list << T_SRDToken::Comment( T_String( "te" ) )
<< T_SRDToken::Comment( T_String( "st" ) );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x09 ,
0x04 , 0x00 , 0x00 , 0x00 ,
't' , 'e' , 's' , 't' ,
0x00
};
M_RUN_( );
}
void SRDBinWriterTest::testWordUnknown( )
{
T_SRDList list;
list << T_SRDToken::Word( T_String( "test" ) );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x02 , // WORD_NEW
0x04 , 0x00 , 0x00 , 0x00 ,
't' , 'e' , 's' , 't' ,
0x00
};
M_RUN_( );
}
void SRDBinWriterTest::testWordKnown( )
{
T_SRDToken word( T_SRDToken::Word( T_String( "test" ) ) );
T_SRDList list( T_SRDList() << word << word );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x02 , // WORD_NEW
0x04 , 0x00 , 0x00 , 0x00 ,
't' , 'e' , 's' , 't' ,
0x03 , // WORD_KNOWN
0x00 , 0x00 , 0x00 , 0x00 ,
0x00
};
M_RUN_( );
}
void SRDBinWriterTest::testVarUnknown( )
{
T_SRDList list;
list << T_SRDToken::Variable( T_String( "test" ) );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x04 , 0x02 , // VAR_WORD WORD_NEW
0x04 , 0x00 , 0x00 , 0x00 ,
't' , 'e' , 's' , 't' ,
0x00
};
M_RUN_( );
}
void SRDBinWriterTest::testVarKnown( )
{
T_SRDList list;
list << T_SRDToken::Word( T_String( "test" ) )
<< T_SRDToken::Variable( T_String( "test" ) );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x02 , // WORD_NEW
0x04 , 0x00 , 0x00 , 0x00 ,
't' , 'e' , 's' , 't' ,
0x04 , 0x03 , // VAR_WORD WORD_KNOWN
0x00 , 0x00 , 0x00 , 0x00 ,
0x00
};
M_RUN_( );
}
void SRDBinWriterTest::testInt32( )
{
T_SRDList list;
list << T_SRDToken::Integer( 0x01020304 );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x06 , // INT
0x04 , 0x03 , 0x02 , 0x01 ,
0x00
};
M_RUN_( );
}
void SRDBinWriterTest::testInt64( )
{
T_SRDList list;
list << T_SRDToken::Long( 0x01020304L );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x07 , // LONG
0x04 , 0x03 , 0x02 , 0x01 , 0x00 , 0x00 , 0x00 , 0x00 ,
0x00
};
M_RUN_( );
}
void SRDBinWriterTest::testFloat( )
{
T_SRDList list;
list << T_SRDToken::Float( 1.0 );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x08 , // FLOAT
0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0xf0 , 0x3f ,
0x00
};
M_RUN_( );
}
void SRDBinWriterTest::testBinary( )
{
T_Buffer< uint8_t > buf( 4 );
buf[ 0 ] = 0xde;
buf[ 1 ] = 0xad;
buf[ 2 ] = 0xbe;
buf[ 3 ] = 0xef;
T_SRDList list;
list << T_SRDToken::Binary( buf );
const uint8_t expected[] = { V1PROLOGUE_ ,
0x0a , // BINARY
0x04 , 0x00 , 0x00 , 0x00 ,
0xde , 0xad , 0xbe , 0xef ,
0x00
};
M_RUN_( );
}