corelib/tests/arrays-objects.cc

875 lines
22 KiB
C++
Raw Normal View History

#include <ebcl/Arrays.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace ebcl;
class ArraysObjectsTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( ArraysObjectsTest );
CPPUNIT_TEST( testDefaults );
CPPUNIT_TEST( testAppendCopy );
CPPUNIT_TEST( testAppendMove );
CPPUNIT_TEST( testAppendNew );
CPPUNIT_TEST( testAppendTwo );
CPPUNIT_TEST( testExpand );
CPPUNIT_TEST( testClear );
CPPUNIT_TEST( testFree );
CPPUNIT_TEST( testEnsureFactor );
CPPUNIT_TEST( testEnsureOther );
CPPUNIT_TEST( testEnsureMoves );
CPPUNIT_TEST( testRemoveEnd );
CPPUNIT_TEST( testRemoveMiddle );
CPPUNIT_TEST( testRemoveSwapEnd );
CPPUNIT_TEST( testRemoveSwapMiddle );
CPPUNIT_TEST( testCopyCons );
CPPUNIT_TEST( testCopyAss );
CPPUNIT_TEST( testMoveCons );
CPPUNIT_TEST( testMoveAss );
CPPUNIT_TEST( testSwap );
CPPUNIT_TEST( testIndexOf );
CPPUNIT_TEST( testIndexOfMissing );
CPPUNIT_TEST( testContains );
CPPUNIT_TEST( testContainsMissing );
CPPUNIT_TEST( testInsertCopyEmpty );
CPPUNIT_TEST( testInsertCopyBefore );
CPPUNIT_TEST( testInsertCopyAfter );
CPPUNIT_TEST( testInsertMoveEmpty );
CPPUNIT_TEST( testInsertMoveBefore );
CPPUNIT_TEST( testInsertMoveAfter );
CPPUNIT_TEST( testInsertNewEmpty );
CPPUNIT_TEST( testInsertNewBefore );
CPPUNIT_TEST( testInsertNewAfter );
CPPUNIT_TEST( testSort );
CPPUNIT_TEST( testSortComparator );
CPPUNIT_TEST( testSortPartial );
CPPUNIT_TEST( testRangeOutside );
CPPUNIT_TEST( testRangeLarger );
CPPUNIT_TEST( testRangeSub );
CPPUNIT_TEST( testMoveRangeOutside );
CPPUNIT_TEST( testMoveRangeLarger );
CPPUNIT_TEST( testMoveRangeSub );
CPPUNIT_TEST_SUITE_END( );
public:
void tearDown( ) override;
void testDefaults( );
void testAppendCopy( );
void testAppendMove( );
void testAppendNew( );
void testAppendTwo( );
void testExpand( );
void testClear( );
void testFree( );
void testEnsureFactor( );
void testEnsureOther( );
void testEnsureMoves( );
void testRemoveEnd( );
void testRemoveMiddle( );
void testRemoveSwapEnd( );
void testRemoveSwapMiddle( );
void testCopyCons( );
void testCopyAss( );
void testMoveCons( );
void testMoveAss( );
void testSwap( );
void testIndexOf( );
void testIndexOfMissing( );
void testContains( );
void testContainsMissing( );
void testInsertCopyEmpty( );
void testInsertCopyBefore( );
void testInsertCopyAfter( );
void testInsertMoveEmpty( );
void testInsertMoveBefore( );
void testInsertMoveAfter( );
void testInsertNewEmpty( );
void testInsertNewBefore( );
void testInsertNewAfter( );
void testSort( );
void testSortComparator( );
void testSortPartial( );
void testRangeOutside( );
void testRangeLarger( );
void testRangeSub( );
void testMoveRangeOutside( );
void testMoveRangeLarger( );
void testMoveRangeSub( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( ArraysObjectsTest );
/*----------------------------------------------------------------------------*/
namespace {
struct T_Test
{
static uint32_t nConsDefault;
static uint32_t nConsCopy;
static uint32_t nConsMove;
static uint32_t nDestr;
static void reset( )
{
nConsDefault = 0;
nConsCopy = 0;
nConsMove = 0;
nDestr = 0;
}
bool ignoreMe;
uint32_t serial;
T_Test( ) : ignoreMe( false )
{ nConsDefault ++; }
T_Test( uint32_t serial )
: ignoreMe( true ) , serial( serial )
{ }
T_Test( T_Test const& o )
: ignoreMe( false ) , serial( o.serial )
{ nConsCopy ++; }
T_Test( T_Test && o )
: ignoreMe( false ) , serial( o.serial )
{ nConsMove ++; }
~T_Test( )
{ if ( !ignoreMe ) { nDestr ++; } }
T_Test& operator =( T_Test const& source )
{
serial = source.serial;
ignoreMe = false;
return *this;
}
T_Test& operator =( T_Test&& source )
{
std::swap( source.serial , serial );
ignoreMe = false;
return *this;
}
bool operator ==( T_Test const& other )
{
return other.serial == serial;
}
bool operator !=( T_Test const& other )
{
return other.serial != serial;
}
};
uint32_t T_Test::nConsDefault = 0;
uint32_t T_Test::nConsCopy = 0;
uint32_t T_Test::nConsMove = 0;
uint32_t T_Test::nDestr = 0;
void swap( T_Test& lhs , T_Test& rhs )
{
using std::swap;
swap( lhs.serial , rhs.serial );
swap( lhs.ignoreMe , rhs.ignoreMe );
}
} // namespace
namespace ebcl { M_DECLARE_COMPARATOR( T_Test ); }
M_DEFINE_COMPARATOR( T_Test )
{
return T_Comparator< uint32_t >::compare( a.serial , b.serial );
}
void ArraysObjectsTest::tearDown( )
{
T_Test::reset( );
}
#define M_CHECK_(ncd,ncc,ncm,nd) \
do { \
CPPUNIT_ASSERT_EQUAL( uint32_t( ncd ) , \
T_Test::nConsDefault ); \
CPPUNIT_ASSERT_EQUAL( uint32_t( ncc ) , T_Test::nConsCopy ); \
CPPUNIT_ASSERT_EQUAL( uint32_t( ncm ) , T_Test::nConsMove ); \
CPPUNIT_ASSERT_EQUAL( uint32_t( nd ) , T_Test::nDestr ); \
} while ( 0 )
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testDefaults( )
{
T_Array< T_Test > array;
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 4096 / sizeof( T_Test ) ) ,
array.growth( ) );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testAppendCopy( )
{
{
T_Array< T_Test > array;
T_Test obj( 0 );
array.add( obj );
M_CHECK_( 0 , 1 , 0 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 1 , 0 , 1 );
}
void ArraysObjectsTest::testAppendMove( )
{
{
T_Array< T_Test > array;
array.add( T_Test( 0 ) );
M_CHECK_( 0 , 0 , 1 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 0 , 1 , 1 );
}
void ArraysObjectsTest::testAppendNew( )
{
{
T_Array< T_Test > array;
array.addNew( 0 );
M_CHECK_( 0 , 0 , 0 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 0 , 0 , 0 );
}
void ArraysObjectsTest::testAppendTwo( )
{
{
T_Array< T_Test > array;
T_Test obj1( 0 ) , obj2( 1 );
array << obj1 << obj2;
M_CHECK_( 0 , 2 , 0 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array[ 1 ].serial );
}
M_CHECK_( 0 , 2 , 0 , 2 );
}
void ArraysObjectsTest::testExpand( )
{
uint32_t g , n;
{
T_Array< T_Test > array;
g = array.growth( );
n = g * 3 / 2;
for ( uint32_t i = 0 ; i < n ; i ++ ) {
T_Test obj( i );
array.add( obj );
}
M_CHECK_( 0 , n , g , g );
CPPUNIT_ASSERT_EQUAL( n , array.size( ) );
CPPUNIT_ASSERT_EQUAL( 2 * g , array.capacity( ) );
for ( uint32_t i = 0 ; i < n ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( i , array[ i ].serial );
}
}
M_CHECK_( 0 , n , g , g + n );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testClear( )
{
uint32_t n;
{
T_Array< T_Test > array;
n = array.growth( );
for ( uint32_t i = 0 ; i < n ; i ++ ) {
T_Test obj( i );
array.add( obj );
}
array.clear( );
M_CHECK_( 0 , n , 0 , n );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
}
M_CHECK_( 0 , n , 0 , n );
}
void ArraysObjectsTest::testFree( )
{
uint32_t n;
{
T_Array< T_Test > array;
n = array.growth( );
for ( uint32_t i = 0 ; i < n ; i ++ ) {
T_Test obj( i );
array.add( obj );
}
array.free( );
M_CHECK_( 0 , n , 0 , n );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.capacity( ) );
}
M_CHECK_( 0 , n , 0 , n );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testEnsureFactor( )
{
{
T_Array< T_Test > array;
array.ensureCapacity( array.growth( ) * 2 );
CPPUNIT_ASSERT_EQUAL( array.growth( ) * 2 , array.capacity( ) );
}
M_CHECK_( 0 , 0 , 0 , 0 );
}
void ArraysObjectsTest::testEnsureOther( )
{
{
T_Array< T_Test > array;
array.ensureCapacity( array.growth( ) * 3 / 2 );
CPPUNIT_ASSERT_EQUAL( array.growth( ) * 2 , array.capacity( ) );
}
M_CHECK_( 0 , 0 , 0 , 0 );
}
void ArraysObjectsTest::testEnsureMoves( )
{
{
T_Array< T_Test > array;
array.add( T_Test( 0 ) );
array.ensureCapacity( array.growth( ) * 2 );
M_CHECK_( 0 , 0 , 2 , 1 );
CPPUNIT_ASSERT_EQUAL( array.growth( ) * 2 , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 0 , 2 , 2 );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testRemoveEnd( )
{
{
T_Array< T_Test > array;
array << T_Test( 0 ) << T_Test( 1 ) << T_Test( 2 );
M_CHECK_( 0 , 0 , 3 , 0 );
array.remove( 2 );
M_CHECK_( 0 , 0 , 3 , 1 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array[ 1 ].serial );
}
M_CHECK_( 0 , 0 , 3 , 3 );
}
void ArraysObjectsTest::testRemoveMiddle( )
{
{
T_Array< T_Test > array;
array << T_Test( 0 ) << T_Test( 1 ) << T_Test( 2 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 3 ) , array.size( ) );
M_CHECK_( 0 , 0 , 3 , 0 );
array.remove( 1 );
M_CHECK_( 0 , 0 , 3 , 1 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array[ 1 ].serial );
}
M_CHECK_( 0 , 0 , 3 , 3 );
}
void ArraysObjectsTest::testRemoveSwapEnd( )
{
{
T_Array< T_Test > array;
array << T_Test( 0 ) << T_Test( 1 ) << T_Test( 2 );
M_CHECK_( 0 , 0 , 3 , 0 );
array.removeSwap( 2 );
M_CHECK_( 0 , 0 , 3 , 1 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array[ 1 ].serial );
}
M_CHECK_( 0 , 0 , 3 , 3 );
}
void ArraysObjectsTest::testRemoveSwapMiddle( )
{
{
T_Array< T_Test > array;
array << T_Test( 0 ) << T_Test( 1 ) << T_Test( 2 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 3 ) , array.size( ) );
M_CHECK_( 0 , 0 , 3 , 0 );
array.removeSwap( 0 );
M_CHECK_( 0 , 0 , 3 , 1 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array[ 0 ].serial );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array[ 1 ].serial );
}
M_CHECK_( 0 , 0 , 3 , 3 );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testCopyCons( )
{
{
T_Array< T_Test > array;
array.ensureCapacity( array.growth( ) * 2 );
array.add( T_Test( 0 ) );
T_Array< T_Test > copy( array );
M_CHECK_( 0 , 1 , 1 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , copy.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , copy.growth( ) );
CPPUNIT_ASSERT_EQUAL( array.capacity( ) , copy.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , copy[ 0 ].serial );
copy.add( T_Test( 1 ) );
M_CHECK_( 0 , 1 , 2 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
}
M_CHECK_( 0 , 1 , 2 , 3 );
}
void ArraysObjectsTest::testCopyAss( )
{
{
T_Array< T_Test > array;
array.ensureCapacity( array.growth( ) * 2 );
array.add( T_Test( 0 ) );
T_Array< T_Test > copy;
copy.add( T_Test( 1 ) );
M_CHECK_( 0 , 0 , 2 , 0 );
copy = array;
M_CHECK_( 0 , 1 , 2 , 1 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , copy.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , copy.growth( ) );
CPPUNIT_ASSERT_EQUAL( array.capacity( ) , copy.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , copy[ 0 ].serial );
copy.add( T_Test( 1 ) );
M_CHECK_( 0 , 1 , 3 , 1 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
}
M_CHECK_( 0 , 1 , 3 , 4 );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testMoveCons( )
{
{
T_Array< T_Test > array( std::move(
T_Array< T_Test >( 128 ).ensureCapacity( 256 )
<< T_Test( 0 ) ) );
M_CHECK_( 0 , 0 , 1 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 128 ) , array.growth( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 256 ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 0 , 1 , 1 );
}
void ArraysObjectsTest::testMoveAss( )
{
{
T_Array< T_Test > array;
array << T_Test( 1 );
array = std::move( T_Array< T_Test >( 128 ).ensureCapacity( 256 ) << T_Test( 0 ) );
M_CHECK_( 0 , 0 , 2 , 1 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 128 ) , array.growth( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 256 ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 0 , 2 , 2 );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testSwap( )
{
T_Array< T_Test > a1;
T_Array< T_Test > a2;
a1 << 1 << 2 << 3;
a2 << 2 << 4 << 6 << 8;
swap( a1 , a2 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 4 ) , a1.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 3 ) , a2.size( ) );
for ( uint32_t i = 0 ; i < 3 ; i ++ ) {
CPPUNIT_ASSERT( a1[ i ].serial == a2[ i ].serial * 2 );
}
CPPUNIT_ASSERT( a1[ 3 ].serial == 8 );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testIndexOf( )
{
T_Array< T_Test > a1;
a1 << 1 << 2 << 3;
CPPUNIT_ASSERT_EQUAL( int32_t( 1 ) , a1.indexOf( T_Test( 2 ) ) );
}
void ArraysObjectsTest::testIndexOfMissing( )
{
T_Array< T_Test > a1;
a1 << 1 << 2 << 3;
CPPUNIT_ASSERT_EQUAL( int32_t( -1 ) , a1.indexOf( T_Test( 8 ) ) );
}
void ArraysObjectsTest::testContains( )
{
T_Array< T_Test > a1;
a1 << 1 << 2 << 3;
CPPUNIT_ASSERT( a1.contains( T_Test( 2 ) ) );
}
void ArraysObjectsTest::testContainsMissing( )
{
T_Array< T_Test > a1;
a1 << 1 << 2 << 3;
CPPUNIT_ASSERT( !a1.contains( T_Test( 4 ) ) );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testInsertCopyEmpty( )
{
{
T_Array< T_Test > array;
T_Test obj( 0 );
array.insert( 0 , obj );
M_CHECK_( 0 , 1 , 0 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 1 , 0 , 1 );
}
void ArraysObjectsTest::testInsertCopyBefore( )
{
{
T_Array< T_Test > array;
for ( uint32_t i = 0 ; i < 10 ; i ++ ) {
array << i;
}
T_Test::reset( );
T_Test obj( 15 );
array.insert( 0 , obj );
M_CHECK_( 0 , 1 , 0 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 11 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 15 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 1 , 0 , 11 );
}
void ArraysObjectsTest::testInsertCopyAfter( )
{
{
T_Array< T_Test > array;
for ( uint32_t i = 0 ; i < 10 ; i ++ ) {
array << i;
}
T_Test::reset( );
T_Test obj( 15 );
array.insert( 10 , obj );
M_CHECK_( 0 , 1 , 0 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 11 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 15 ) , array[ 10 ].serial );
}
M_CHECK_( 0 , 1 , 0 , 11 );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testInsertMoveEmpty( )
{
{
T_Array< T_Test > array;
T_Test obj( 15 );
array.insert( 0 , std::move( obj ) );
M_CHECK_( 0 , 0 , 1 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 15 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 0 , 1 , 1 );
}
void ArraysObjectsTest::testInsertMoveBefore( )
{
{
T_Array< T_Test > array;
for ( uint32_t i = 0 ; i < 10 ; i ++ ) {
array << i;
}
T_Test::reset( );
T_Test obj( 15 );
array.insert( 0 , std::move( obj ) );
M_CHECK_( 0 , 0 , 1 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 11 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 15 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 0 , 1 , 11 );
}
void ArraysObjectsTest::testInsertMoveAfter( )
{
{
T_Array< T_Test > array;
for ( uint32_t i = 0 ; i < 10 ; i ++ ) {
array << i;
}
T_Test::reset( );
T_Test obj( 15 );
array.insert( 10 , std::move( obj ) );
M_CHECK_( 0 , 0 , 1 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 11 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 15 ) , array[ 10 ].serial );
}
M_CHECK_( 0 , 0 , 1 , 11 );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testInsertNewEmpty( )
{
{
T_Array< T_Test > array;
array.insertNew( 0 , 15 );
M_CHECK_( 0 , 0 , 0 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 15 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 0 , 0 , 0 );
}
void ArraysObjectsTest::testInsertNewBefore( )
{
{
T_Array< T_Test > array;
for ( uint32_t i = 0 ; i < 10 ; i ++ ) {
array << ( i + 2 );
}
array.insertNew( 0 , 15 );
M_CHECK_( 0 , 0 , 10 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 11 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 15 ) , array[ 0 ].serial );
}
M_CHECK_( 0 , 0 , 10 , 10 );
}
void ArraysObjectsTest::testInsertNewAfter( )
{
{
T_Array< T_Test > array;
for ( uint32_t i = 0 ; i < 10 ; i ++ ) {
array << ( i + 2 );
}
array.insertNew( 10 , 15 );
M_CHECK_( 0 , 0 , 10 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 11 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 15 ) , array[ 10 ].serial );
}
M_CHECK_( 0 , 0 , 10 , 10 );
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testSort( )
{
T_Array< T_Test > array;
for ( uint32_t i = 0 ; i < 5000 ; i ++ ) {
array << T_Test( std::rand( ) );
}
array.sort( );
for ( uint32_t i = 1 ; i < 5000 ; i ++ ) {
CPPUNIT_ASSERT( array[ i ].serial >= array[ i - 1 ].serial );
}
}
void ArraysObjectsTest::testSortComparator( )
{
T_Array< T_Test > array;
for ( uint32_t i = 0 ; i < 5000 ; i ++ ) {
array << T_Test( std::rand( ) );
}
array.sort( []( T_Test const& a , T_Test const& b ){
return T_Comparator< T_Test >::compare( b , a );
} );
for ( uint32_t i = 1 ; i < 5000 ; i ++ ) {
CPPUNIT_ASSERT( array[ i ].serial <= array[ i - 1 ].serial );
}
}
void ArraysObjectsTest::testSortPartial( )
{
T_Array< T_Test > array;
for ( uint32_t i = 0 ; i < 5000 ; i ++ ) {
array << std::rand( );
}
T_Array< T_Test > copy( array );
array.sort( 1000 , 1000 );
for ( uint32_t i = 0 ; i < 1000 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( copy[ i ].serial , array[ i ].serial );
}
for ( uint32_t i = 1001 ; i < 2000 ; i ++ ) {
CPPUNIT_ASSERT( array[ i ].serial >= array[ i - 1 ].serial );
}
for ( uint32_t i = 2000 ; i < 5000 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( copy[ i ].serial , array[ i ].serial );
}
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testRangeOutside( )
{
T_Array< T_Test > array( 5000 );
for ( uint32_t i = 0 ; i < 5000 ; i ++ ) {
array.addNew( std::rand( ) );
}
T_Array< T_Test > range( array.copyRange( 7000 , 1000 ) );
M_CHECK_( 0 , 0 , 0 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , range.size( ) );
}
void ArraysObjectsTest::testRangeLarger( )
{
T_Array< T_Test > array( 5000 );
for ( uint32_t i = 0 ; i < 5000 ; i ++ ) {
array.addNew( std::rand( ) );
}
T_Array< T_Test > range( array.copyRange( 1000 ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 4000 ) , range.size( ) );
M_CHECK_( 0 , 4000 , 0 , 0 );
for ( uint32_t i = 0 ; i < 4000 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( array[ i + 1000 ].serial , range[ i ].serial );
}
}
void ArraysObjectsTest::testRangeSub( )
{
T_Array< T_Test > array( 5000 );
for ( uint32_t i = 0 ; i < 5000 ; i ++ ) {
array.addNew( std::rand( ) );
}
T_Array< T_Test > range( array.copyRange( 1000 , 1999 ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1000 ) , range.size( ) );
M_CHECK_( 0 , 1000 , 0 , 0 );
for ( uint32_t i = 0 ; i < 1000 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( array[ i + 1000 ].serial , range[ i ].serial );
}
}
/*----------------------------------------------------------------------------*/
void ArraysObjectsTest::testMoveRangeOutside( )
{
T_Array< T_Test > array( 5000 );
for ( uint32_t i = 0 ; i < 5000 ; i ++ ) {
array.addNew( std::rand( ) );
}
T_Array< T_Test > range( array.moveRange( 7000 , 1000 ) );
M_CHECK_( 0 , 0 , 0 , 0 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , range.size( ) );
}
void ArraysObjectsTest::testMoveRangeLarger( )
{
T_Array< T_Test > array( 5000 );
for ( uint32_t i = 0 ; i < 5000 ; i ++ ) {
array.addNew( std::rand( ) );
}
T_Array< T_Test > range( array.moveRange( 1000 ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 4000 ) , range.size( ) );
M_CHECK_( 0 , 0 , 4000 , 0 );
for ( uint32_t i = 0 ; i < 4000 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( array[ i + 1000 ].serial , range[ i ].serial );
}
}
void ArraysObjectsTest::testMoveRangeSub( )
{
T_Array< T_Test > array( 5000 );
for ( uint32_t i = 0 ; i < 5000 ; i ++ ) {
array.addNew( std::rand( ) );
}
T_Array< T_Test > range( array.moveRange( 1000 , 1999 ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1000 ) , range.size( ) );
M_CHECK_( 0 , 0 , 1000 , 0 );
for ( uint32_t i = 0 ; i < 1000 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( array[ i + 1000 ].serial , range[ i ].serial );
}
}