corelib/tests/binary-writer.cc

229 lines
6.4 KiB
C++
Raw Normal View History

#include <lw/lib/BinaryStreams.hh>
#include <lw/lib/MemoryStreams.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace lw;
/* - BinaryWriterTest ---------------------------------------------------{{{-*/
class BinaryWriterTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( BinaryWriterTest );
CPPUNIT_TEST( testNumericBigEndian8 );
CPPUNIT_TEST( testNumericLittleEndian8 );
CPPUNIT_TEST( testNumericBigEndian16 );
CPPUNIT_TEST( testNumericLittleEndian16 );
CPPUNIT_TEST( testNumericBigEndian32 );
CPPUNIT_TEST( testNumericLittleEndian32 );
CPPUNIT_TEST( testNumericBigEndian32f );
CPPUNIT_TEST( testNumericLittleEndian32f );
CPPUNIT_TEST( testNumericBigEndian64 );
CPPUNIT_TEST( testNumericLittleEndian64 );
CPPUNIT_TEST( testNumericBigEndian64f );
CPPUNIT_TEST( testNumericLittleEndian64f );
CPPUNIT_TEST_SUITE_END( );
public:
void testNumericBigEndian8( );
void testNumericLittleEndian8( );
void testNumericBigEndian16( );
void testNumericLittleEndian16( );
void testNumericBigEndian32( );
void testNumericLittleEndian32( );
void testNumericBigEndian32f( );
void testNumericLittleEndian32f( );
void testNumericBigEndian64( );
void testNumericLittleEndian64( );
void testNumericBigEndian64f( );
void testNumericLittleEndian64f( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( BinaryWriterTest );
void BinaryWriterTest::testNumericBigEndian8( ) // {{{
{
const uint8_t data[] = { 0x12 };
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericBigEndian( uint8_t( 0x12 ) );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericLittleEndian8( ) // {{{
{
const uint8_t data[] = { 0x12 };
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericLittleEndian( uint8_t( 0x12 ) );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericBigEndian16( ) // {{{
{
const uint8_t data[] = { 0xbe , 0xef };
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericBigEndian( uint16_t( 0xbeef ) );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericLittleEndian16( ) // {{{
{
const uint8_t data[] = { 0xbe , 0xef };
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericLittleEndian( uint16_t( 0xefbe ) );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericBigEndian32( ) // {{{
{
const uint8_t data[] = { 0xde , 0xad , 0xbe , 0xef };
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericBigEndian( uint32_t( 0xdeadbeef ) );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericLittleEndian32( ) // {{{
{
const uint8_t data[] = { 0xde , 0xad , 0xbe , 0xef };
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericLittleEndian( uint32_t( 0xefbeadde ) );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericBigEndian32f( ) // {{{
{
const uint8_t data[] = { 0x3f , 0x80 , 0x00 , 0x00 };
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericBigEndian( 1.0f );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericLittleEndian32f( ) // {{{
{
const uint8_t data[] = { 0x00 , 0x00 , 0x80 , 0x3f };
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericLittleEndian( 1.0f );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericBigEndian64( ) // {{{
{
const uint8_t data[] = {
0xba , 0xdb , 0xa7 , 0x5e , 0xa7 , 0xba , 0xb1 , 0x35
};
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericBigEndian( uint64_t( 0xbadba75ea7bab135L ) );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericLittleEndian64( ) // {{{
{
const uint8_t data[] = {
0x35 , 0xb1 , 0xba , 0xa7 , 0x5e , 0xa7 , 0xdb , 0xba ,
};
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericLittleEndian( uint64_t( 0xbadba75ea7bab135L ) );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericBigEndian64f( ) // {{{
{
const uint8_t data[] = { 0x3f , 0xf0 , 0 , 0 , 0 , 0 , 0 , 0 };
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericBigEndian( 1.0 );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
void BinaryWriterTest::testNumericLittleEndian64f( ) // {{{
{
const uint8_t data[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0xf0 , 0x3f };
T_Buffer< uint8_t > buffer;
T_MemoryOutputStream ms( buffer );
T_BinaryWriter wr( ms );
wr.writeNumericLittleEndian( 1.0 );
CPPUNIT_ASSERT_EQUAL( sizeof( data ) , buffer.size( ) );
for ( size_t i = 0 ; i < sizeof( data ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , buffer[ i ] );
}
}
// }}}
/*------------------------------------------------------------------------}}}-*/