150 lines
4.5 KiB
C++
150 lines
4.5 KiB
C++
#include <ebcl/BinaryStreams.hh>
|
|
#include <ebcl/MemoryStreams.hh>
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
using namespace ebcl;
|
|
|
|
|
|
/* - StreamReaderTest -------------------------------------------------------*/
|
|
|
|
class StreamReaderTest : public CppUnit::TestFixture
|
|
{
|
|
CPPUNIT_TEST_SUITE( StreamReaderTest );
|
|
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( StreamReaderTest );
|
|
|
|
void StreamReaderTest::testNumericBigEndian8( )
|
|
{
|
|
const uint8_t data[] = { 0x12 };
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( uint8_t( 0x12 ) ,
|
|
rd.readNumericBigEndian< uint8_t >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericLittleEndian8( )
|
|
{
|
|
const uint8_t data[] = { 0x12 };
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( uint8_t( 0x12 ) ,
|
|
rd.readNumericLittleEndian< uint8_t >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericBigEndian16( )
|
|
{
|
|
const uint8_t data[] = { 0xbe , 0xef };
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( uint16_t( 0xbeef ) ,
|
|
rd.readNumericBigEndian< uint16_t >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericLittleEndian16( )
|
|
{
|
|
const uint8_t data[] = { 0xbe , 0xef };
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( uint16_t( 0xefbe ) ,
|
|
rd.readNumericLittleEndian< uint16_t >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericBigEndian32( )
|
|
{
|
|
const uint8_t data[] = { 0xde , 0xad , 0xbe , 0xef };
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( uint32_t( 0xdeadbeef ) ,
|
|
rd.readNumericBigEndian< uint32_t >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericLittleEndian32( )
|
|
{
|
|
const uint8_t data[] = { 0xde , 0xad , 0xbe , 0xef };
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( uint32_t( 0xefbeadde ) ,
|
|
rd.readNumericLittleEndian< uint32_t >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericBigEndian32f( )
|
|
{
|
|
const uint8_t data[] = { 0x3f , 0x80 , 0x00 , 0x00 };
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( 1.0f , rd.readNumericBigEndian< float >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericLittleEndian32f( )
|
|
{
|
|
const uint8_t data[] = { 0x00 , 0x00 , 0x80 , 0x3f };
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( 1.0f , rd.readNumericLittleEndian< float >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericBigEndian64( )
|
|
{
|
|
const uint8_t data[] = {
|
|
0xba , 0xdb , 0xa7 , 0x5e , 0xa7 , 0xba , 0xb1 , 0x35
|
|
};
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( uint64_t( 0xbadba75ea7bab135L ) ,
|
|
rd.readNumericBigEndian< uint64_t >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericLittleEndian64( )
|
|
{
|
|
const uint8_t data[] = {
|
|
0x35 , 0xb1 , 0xba , 0xa7 , 0x5e , 0xa7 , 0xdb , 0xba ,
|
|
};
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( uint64_t( 0xbadba75ea7bab135L ) ,
|
|
rd.readNumericLittleEndian< uint64_t >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericBigEndian64f( )
|
|
{
|
|
const uint8_t data[] = { 0x3f , 0xf0 , 0 , 0 , 0 , 0 , 0 , 0 };
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( 1.0 , rd.readNumericBigEndian< double >( ) );
|
|
}
|
|
|
|
void StreamReaderTest::testNumericLittleEndian64f( )
|
|
{
|
|
const uint8_t data[] = { 0 , 0 , 0 , 0 , 0 , 0 , 0xf0 , 0x3f };
|
|
T_MemoryInputStream ms( data , sizeof( data ) );
|
|
T_BinaryReader rd( ms );
|
|
CPPUNIT_ASSERT_EQUAL( 1.0 , rd.readNumericLittleEndian< double >( ) );
|
|
}
|