corelib/tests/buffers.cc

319 lines
8.4 KiB
C++

#include <ebcl/Buffers.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace ebcl;
class FixedBufferTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( FixedBufferTest );
CPPUNIT_TEST( testInit );
CPPUNIT_TEST( testSetAll );
CPPUNIT_TEST( testWrite );
CPPUNIT_TEST( testMoveCons );
CPPUNIT_TEST( testMoveAss );
CPPUNIT_TEST( testCopyCons );
CPPUNIT_TEST( testCopyAss );
CPPUNIT_TEST( testSwap );
CPPUNIT_TEST_SUITE_END( );
public:
void testInit( );
void testSetAll( );
void testWrite( );
void testMoveCons( );
void testMoveAss( );
void testCopyCons( );
void testCopyAss( );
void testSwap( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( FixedBufferTest );
/*----------------------------------------------------------------------------*/
class BufferTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( BufferTest );
CPPUNIT_TEST( testInitEmpty );
CPPUNIT_TEST( testInitSize );
CPPUNIT_TEST( testInitData );
CPPUNIT_TEST( testWrite );
CPPUNIT_TEST( testSetAll );
CPPUNIT_TEST( testCopyAll );
CPPUNIT_TEST( testResizeFromZero );
CPPUNIT_TEST( testResize );
CPPUNIT_TEST( testResizeToZero );
CPPUNIT_TEST( testCopyCons );
CPPUNIT_TEST( testCopyAss );
CPPUNIT_TEST( testMoveCons );
CPPUNIT_TEST( testMoveAss );
CPPUNIT_TEST( testSwap );
CPPUNIT_TEST_SUITE_END( );
public:
void testInitEmpty( );
void testInitSize( );
void testInitData( );
void testWrite( );
void testSetAll( );
void testCopyAll( );
void testResizeFromZero( );
void testResize( );
void testResizeToZero( );
void testCopyCons( );
void testCopyAss( );
void testMoveCons( );
void testMoveAss( );
void testSwap( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( BufferTest );
/*----------------------------------------------------------------------------*/
void FixedBufferTest::testInit( )
{
T_FixedBuffer< 4096 , char > buffer;
CPPUNIT_ASSERT_EQUAL( size_t( 4096 ) , buffer.bytes( ) );
T_FixedBuffer< 4096 , int16_t > b2;
CPPUNIT_ASSERT_EQUAL( size_t( 8192 ) , b2.bytes( ) );
}
void FixedBufferTest::testSetAll( )
{
T_FixedBuffer< 4096 , uint32_t > b;
b.setAll( 0xdeadbeef );
for ( uint32_t i = 0 ; i < 4096 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( 0xdeadbeefu , b[ i ] );
}
}
void FixedBufferTest::testWrite( )
{
T_FixedBuffer< 1 , uint32_t > b;
b[ 0 ] = 0x1a7ef00du;
CPPUNIT_ASSERT_EQUAL( 0x1a7ef00du , b[ 0 ] );
}
void FixedBufferTest::testMoveCons( )
{
T_FixedBuffer< 1 , uint32_t > b;
b[ 0 ] = 0x1a7ef00du;
T_FixedBuffer< 1 , uint32_t > b2( std::move( b ) );
CPPUNIT_ASSERT_EQUAL( 0x1a7ef00du , b2[ 0 ] );
}
void FixedBufferTest::testMoveAss( )
{
T_FixedBuffer< 1 , uint32_t > b;
b[ 0 ] = 0x1a7ef00du;
T_FixedBuffer< 1 , uint32_t > b2;
b2[ 0 ] = 0xdeadbeefu;
b2 = std::move( b );
CPPUNIT_ASSERT_EQUAL( 0xdeadbeefu , b[ 0 ] );
CPPUNIT_ASSERT_EQUAL( 0x1a7ef00du , b2[ 0 ] );
}
void FixedBufferTest::testCopyCons( )
{
T_FixedBuffer< 1 , uint32_t > b;
b[ 0 ] = 0x1a7ef00du;
T_FixedBuffer< 1 , uint32_t > b2( b );
CPPUNIT_ASSERT_EQUAL( 0x1a7ef00du , b2[ 0 ] );
}
void FixedBufferTest::testCopyAss( )
{
T_FixedBuffer< 1 , uint32_t > b;
b[ 0 ] = 0x1a7ef00du;
T_FixedBuffer< 1 , uint32_t > b2;
b2[ 0 ] = 0xdeadbeefu;
b2 = b;
CPPUNIT_ASSERT_EQUAL( 0x1a7ef00du , b[ 0 ] );
CPPUNIT_ASSERT_EQUAL( 0x1a7ef00du , b2[ 0 ] );
}
void FixedBufferTest::testSwap( )
{
T_FixedBuffer< 1 , uint32_t > b;
b[ 0 ] = 0x1a7ef00du;
T_FixedBuffer< 1 , uint32_t > b2;
b2[ 0 ] = 0xdeadbeefu;
swap( b , b2 );
CPPUNIT_ASSERT_EQUAL( 0xdeadbeefu , b[ 0 ] );
CPPUNIT_ASSERT_EQUAL( 0x1a7ef00du , b2[ 0 ] );
}
/*----------------------------------------------------------------------------*/
void BufferTest::testInitEmpty( )
{
T_Buffer< uint32_t > test;
CPPUNIT_ASSERT_EQUAL( size_t( 0 ) , test.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 0 ) , test.bytes( ) );
}
void BufferTest::testInitSize( )
{
T_Buffer< uint32_t > test( 256 );
CPPUNIT_ASSERT_EQUAL( size_t( 256 ) , test.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 1024 ) , test.bytes( ) );
}
void BufferTest::testInitData( )
{
static const uint32_t data[] = { 12 , 34 , 56 , 78 };
T_Buffer< uint32_t > test( data , 4 );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , test.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , test.bytes( ) );
for ( uint32_t i = 0 ; i < 4 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , test[ i ] );
}
}
/*----------------------------------------------------------------------------*/
void BufferTest::testWrite( )
{
T_Buffer< uint32_t > test( 4 );
for ( uint32_t i = 0 ; i < test.size( ) ; i ++ ) {
test[ i ] = 10 + i * 2;
}
for ( uint32_t i = 0 ; i < test.size( ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( uint32_t( 10 + i * 2 ) , test[ i ] );
}
}
void BufferTest::testSetAll( )
{
T_Buffer< uint32_t > test( 256 );
test.setAll( 0xdeadbeefu );
for ( uint32_t i = 0 ; i < test.size( ) ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( 0xdeadbeefu , test[ i ] );
}
}
void BufferTest::testCopyAll( )
{
static const uint32_t data[] = { 12 , 34 , 56 , 78 };
T_Buffer< uint32_t > test( 4 );
test.copyAll( data );
for ( uint32_t i = 0 ; i < 4 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , test[ i ] );
}
}
/*----------------------------------------------------------------------------*/
void BufferTest::testResizeFromZero( )
{
T_Buffer< uint32_t > test;
test.resize( 4 );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , test.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , test.bytes( ) );
test[ 3 ] = 0xdeadbeefu;
CPPUNIT_ASSERT_EQUAL( 0xdeadbeefu , test[ 3 ] );
}
void BufferTest::testResize( )
{
T_Buffer< uint32_t > test( 256 );
test.resize( 4 );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , test.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , test.bytes( ) );
test[ 3 ] = 0xdeadbeefu;
CPPUNIT_ASSERT_EQUAL( 0xdeadbeefu , test[ 3 ] );
}
void BufferTest::testResizeToZero( )
{
T_Buffer< uint32_t > test( 256 );
test.resize( 0 );
CPPUNIT_ASSERT_EQUAL( size_t( 0 ) , test.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 0 ) , test.bytes( ) );
}
/*----------------------------------------------------------------------------*/
void BufferTest::testCopyCons( )
{
static const uint32_t data[] = { 12 , 34 , 56 , 78 };
T_Buffer< uint32_t > test( data , 4 );
T_Buffer< uint32_t > copy( test );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , test.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , test.bytes( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , copy.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , copy.bytes( ) );
for ( uint32_t i = 0 ; i < 4 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , copy[ i ] );
}
}
void BufferTest::testCopyAss( )
{
static const uint32_t data[] = { 12 , 34 , 56 , 78 };
T_Buffer< uint32_t > test( data , 4 );
T_Buffer< uint32_t > copy;
copy = test;
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , test.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , test.bytes( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , copy.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , copy.bytes( ) );
for ( uint32_t i = 0 ; i < 4 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , copy[ i ] );
}
}
void BufferTest::testMoveCons( )
{
static const uint32_t data[] = { 12 , 34 , 56 , 78 };
T_Buffer< uint32_t > test( data , 4 );
T_Buffer< uint32_t > copy( std::move( test ) );
CPPUNIT_ASSERT_EQUAL( size_t( 0 ) , test.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 0 ) , test.bytes( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , copy.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , copy.bytes( ) );
for ( uint32_t i = 0 ; i < 4 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , copy[ i ] );
}
}
void BufferTest::testMoveAss( )
{
static const uint32_t data[] = { 12 , 34 , 56 , 78 };
T_Buffer< uint32_t > test( data , 4 );
T_Buffer< uint32_t > copy;
copy = std::move( test );
CPPUNIT_ASSERT_EQUAL( size_t( 0 ) , test.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 0 ) , test.bytes( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , copy.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , copy.bytes( ) );
for ( uint32_t i = 0 ; i < 4 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data[ i ] , copy[ i ] );
}
}
void BufferTest::testSwap( )
{
static const uint32_t data1[] = { 12 , 34 , 56 , 78 };
static const uint32_t data2[] = { 910 , 1112 , 1314 , 1516 };
T_Buffer< uint32_t > test1( data1 , 4 );
T_Buffer< uint32_t > test2( data2 , 4 );
swap( test1 , test2 );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , test1.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , test1.bytes( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , test2.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 16 ) , test2.bytes( ) );
for ( uint32_t i = 0 ; i < 4 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( data1[ i ] , test2[ i ] );
CPPUNIT_ASSERT_EQUAL( data2[ i ] , test1[ i ] );
}
}