#include #include using namespace ebcl; class ArraysBasicTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE( ArraysBasicTest ); CPPUNIT_TEST( testInitialValues ); CPPUNIT_TEST( testAppendOne ); CPPUNIT_TEST( testAppendTwo ); CPPUNIT_TEST( testAppendExpand ); CPPUNIT_TEST( testClear ); CPPUNIT_TEST( testFree ); CPPUNIT_TEST( testCopyCons ); CPPUNIT_TEST( testCopyAss ); CPPUNIT_TEST( testMoveCons ); CPPUNIT_TEST( testMoveAss ); CPPUNIT_TEST( testSwap ); CPPUNIT_TEST( testEnsureFactor ); CPPUNIT_TEST( testEnsureOther ); CPPUNIT_TEST( testEnsureKeepData ); CPPUNIT_TEST( testRemoveEnd ); CPPUNIT_TEST( testRemoveMiddle ); CPPUNIT_TEST( testRemoveSwapEnd ); CPPUNIT_TEST( testRemoveSwapMiddle ); CPPUNIT_TEST( testAddAllEmpty ); CPPUNIT_TEST( testAddAll ); CPPUNIT_TEST( testInsertEmpty ); CPPUNIT_TEST( testInsertAfter ); CPPUNIT_TEST( testInsertBefore ); CPPUNIT_TEST( testIndexOf ); CPPUNIT_TEST( testIndexOfMissing ); CPPUNIT_TEST( testContains ); CPPUNIT_TEST( testContainsMissing ); CPPUNIT_TEST( testIndexOfPred ); CPPUNIT_TEST( testIndexOfPredMissing ); CPPUNIT_TEST( testContainsPred ); CPPUNIT_TEST( testContainsPredMissing ); CPPUNIT_TEST( testSort ); CPPUNIT_TEST( testSortComparator ); CPPUNIT_TEST( testSortPartial ); CPPUNIT_TEST( testRangeOutside ); CPPUNIT_TEST( testRangeLarger ); CPPUNIT_TEST( testRangeSub ); CPPUNIT_TEST_SUITE_END( ); public: void testInitialValues( ); void testAppendOne( ); void testAppendTwo( ); void testAppendExpand( ); void testClear( ); void testFree( ); void testCopyCons( ); void testCopyAss( ); void testMoveCons( ); void testMoveAss( ); void testSwap( ); void testEnsureFactor( ); void testEnsureOther( ); void testEnsureKeepData( ); void testRemoveEnd( ); void testRemoveMiddle( ); void testRemoveSwapEnd( ); void testRemoveSwapMiddle( ); void testAddAllEmpty( ); void testAddAll( ); void testInsertEmpty( ); void testInsertAfter( ); void testInsertBefore( ); void testIndexOf( ); void testIndexOfMissing( ); void testContains( ); void testContainsMissing( ); void testIndexOfPred( ); void testIndexOfPredMissing( ); void testContainsPred( ); void testContainsPredMissing( ); void testSort( ); void testSortComparator( ); void testSortPartial( ); void testRangeOutside( ); void testRangeLarger( ); void testRangeSub( ); }; CPPUNIT_TEST_SUITE_REGISTRATION( ArraysBasicTest ); /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testInitialValues( ) { T_Array< uint32_t > array; CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.capacity( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 4096 / sizeof( uint32_t ) ) , array.growth( ) ); } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testAppendOne( ) { T_Array< uint32_t > array; array.add( 123 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] ); } void ArraysBasicTest::testAppendTwo( ) { T_Array< uint32_t > array; array << 123 << 456; CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( array.growth( ) , array.capacity( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 456 ) , array[ 1 ] ); } void ArraysBasicTest::testAppendExpand( ) { T_Array< uint32_t > array; const uint32_t n = array.growth( ) * 3 / 2; for ( uint32_t i = 0 ; i < n ; i ++ ) { array.add( i ); } CPPUNIT_ASSERT_EQUAL( n , array.size( ) ); CPPUNIT_ASSERT_EQUAL( 2 * array.growth( ) , array.capacity( ) ); for ( uint32_t i = 0 ; i < n ; i ++ ) { CPPUNIT_ASSERT_EQUAL( uint32_t( i ) , array[ i ] ); } } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testClear( ) { T_Array< uint32_t > array; const uint32_t n = array.growth( ) * 3 / 2; for ( uint32_t i = 0 ; i < n ; i ++ ) { array.add( i ); } array.clear( ); CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( 2 * array.growth( ) , array.capacity( ) ); } void ArraysBasicTest::testFree( ) { T_Array< uint32_t > array; const uint32_t n = array.growth( ) * 3 / 2; for ( uint32_t i = 0 ; i < n ; i ++ ) { array.add( i ); } array.free( ); CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.capacity( ) ); } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testCopyCons( ) { T_Array< uint32_t > array; array.ensureCapacity( array.growth( ) * 2 ); array.add( 1 ); T_Array< uint32_t > copy( array ); 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( 1 ) , copy[ 0 ] ); copy.add( 2 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) ); } void ArraysBasicTest::testCopyAss( ) { T_Array< uint32_t > array; array.ensureCapacity( array.growth( ) * 2 ); array.add( 1 ); T_Array< uint32_t > copy; copy = array; 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( 1 ) , copy[ 0 ] ); copy.add( 2 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) ); } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testMoveCons( ) { T_Array< uint32_t > array( std::move( T_Array< uint32_t >( 128 ).ensureCapacity( 256 ) << 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( 1 ) , array[ 0 ] ); } void ArraysBasicTest::testMoveAss( ) { T_Array< uint32_t > array; array = std::move( T_Array< uint32_t > ( 128 ).ensureCapacity( 256 ) << 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( 1 ) , array[ 0 ] ); } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testSwap( ) { T_Array< uint32_t > a1; T_Array< uint32_t > 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 ] == a2[ i ] * 2 ); } CPPUNIT_ASSERT( a1[ 3 ] == 8 ); } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testEnsureFactor( ) { T_Array< uint32_t > array; array.ensureCapacity( array.growth( ) * 2 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( 2 * array.growth( ) , array.capacity( ) ); } void ArraysBasicTest::testEnsureOther( ) { T_Array< uint32_t > array; array.ensureCapacity( array.growth( ) * 3 / 2 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( 2 * array.growth( ) , array.capacity( ) ); } void ArraysBasicTest::testEnsureKeepData( ) { T_Array< uint32_t > array; array.add( 123 ); array.ensureCapacity( array.growth( ) * 2 ); CPPUNIT_ASSERT_EQUAL( 2 * array.growth( ) , array.capacity( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] ); } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testRemoveEnd( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; array.remove( 2 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 456 ) , array[ 1 ] ); } void ArraysBasicTest::testRemoveMiddle( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; array.remove( 0 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 456 ) , array[ 0 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 789 ) , array[ 1 ] ); } void ArraysBasicTest::testRemoveSwapEnd( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; array.removeSwap( 2 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 456 ) , array[ 1 ] ); } void ArraysBasicTest::testRemoveSwapMiddle( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; array.removeSwap( 0 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 2 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 789 ) , array[ 0 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 456 ) , array[ 1 ] ); } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testAddAllEmpty( ) { T_Array< uint32_t > empty; T_Array< uint32_t > array; array << 123 << 456 << 789; array.addAll( empty ); CPPUNIT_ASSERT_EQUAL( uint32_t( 3 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 456 ) , array[ 1 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 789 ) , array[ 2 ] ); } void ArraysBasicTest::testAddAll( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; T_Array< uint32_t > other; other << 627 << 951 << 843; array.addAll( other ); CPPUNIT_ASSERT_EQUAL( uint32_t( 6 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 456 ) , array[ 1 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 789 ) , array[ 2 ] ); for ( uint32_t i = 0 ; i < 3 ; i ++ ) { CPPUNIT_ASSERT_EQUAL( other[ i ] , array[ i + 3 ] ); } } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testInsertEmpty( ) { T_Array< uint32_t > array; array.insert( 0 , 123 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] ); } void ArraysBasicTest::testInsertAfter( ) { T_Array< uint32_t > array; array << 123 << 456; array.insert( 2 , 789 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 3 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 456 ) , array[ 1 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 789 ) , array[ 2 ] ); } void ArraysBasicTest::testInsertBefore( ) { T_Array< uint32_t > array; array << 456 << 789; array.insert( 0 , 123 ); CPPUNIT_ASSERT_EQUAL( uint32_t( 3 ) , array.size( ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 456 ) , array[ 1 ] ); CPPUNIT_ASSERT_EQUAL( uint32_t( 789 ) , array[ 2 ] ); } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testIndexOf( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; CPPUNIT_ASSERT_EQUAL( int32_t( 1 ) , array.indexOf( 456 ) ); } void ArraysBasicTest::testIndexOfMissing( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; CPPUNIT_ASSERT_EQUAL( int32_t( -1 ) , array.indexOf( 101112 ) ); } void ArraysBasicTest::testContains( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; CPPUNIT_ASSERT( array.contains( 456 ) ); } void ArraysBasicTest::testContainsMissing( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; CPPUNIT_ASSERT( !array.contains( 101112 ) ); } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testIndexOfPred( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; CPPUNIT_ASSERT_EQUAL( 0 , array.indexOf( [](auto i ) { return i > 100; } ) ); } void ArraysBasicTest::testIndexOfPredMissing( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; CPPUNIT_ASSERT_EQUAL( -1 , array.indexOf( [](auto i ) { return i > 1000; } ) ); } void ArraysBasicTest::testContainsPred( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; CPPUNIT_ASSERT( array.contains( [](auto i ) { return i > 100; } ) ); } void ArraysBasicTest::testContainsPredMissing( ) { T_Array< uint32_t > array; array << 123 << 456 << 789; CPPUNIT_ASSERT( !array.contains( [](auto i ) { return i > 1000; } ) ); } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testSort( ) { T_Array< uint32_t > array; for ( uint32_t i = 0 ; i < 5000 ; i ++ ) { array << std::rand( ); } array.sort( ); for ( uint32_t i = 1 ; i < 5000 ; i ++ ) { CPPUNIT_ASSERT( array[ i ] >= array[ i - 1 ] ); } } void ArraysBasicTest::testSortComparator( ) { T_Array< uint32_t > array; for ( uint32_t i = 0 ; i < 5000 ; i ++ ) { array << std::rand( ); } array.sort( []( uint32_t const& a , uint32_t const& b ) { return T_Comparator< uint32_t >::compare( b , a ); } ); for ( uint32_t i = 1 ; i < 5000 ; i ++ ) { CPPUNIT_ASSERT( array[ i ] <= array[ i - 1 ] ); } } void ArraysBasicTest::testSortPartial( ) { T_Array< uint32_t > array; for ( uint32_t i = 0 ; i < 5000 ; i ++ ) { array << std::rand( ); } T_Array< uint32_t > copy( array ); array.sort( 1000 , 1000 ); for ( uint32_t i = 0 ; i < 1000 ; i ++ ) { CPPUNIT_ASSERT_EQUAL( copy[ i ] , array[ i ] ); } for ( uint32_t i = 1001 ; i < 2000 ; i ++ ) { CPPUNIT_ASSERT( array[ i ] >= array[ i - 1 ] ); } for ( uint32_t i = 2000 ; i < 5000 ; i ++ ) { CPPUNIT_ASSERT_EQUAL( copy[ i ] , array[ i ] ); } } /*----------------------------------------------------------------------------*/ void ArraysBasicTest::testRangeOutside( ) { T_Array< uint32_t > array; for ( uint32_t i = 0 ; i < 5000 ; i ++ ) { array << std::rand( ); } T_Array< uint32_t > range( array.copyRange( 7000 , 1000 ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , range.size( ) ); } void ArraysBasicTest::testRangeLarger( ) { T_Array< uint32_t > array; for ( uint32_t i = 0 ; i < 5000 ; i ++ ) { array << std::rand( ); } T_Array< uint32_t > range( array.copyRange( 1000 ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 4000 ) , range.size( ) ); for ( uint32_t i = 0 ; i < 4000 ; i ++ ) { CPPUNIT_ASSERT_EQUAL( array[ i + 1000 ] , range[ i ] ); } } void ArraysBasicTest::testRangeSub( ) { T_Array< uint32_t > array; for ( uint32_t i = 0 ; i < 5000 ; i ++ ) { array << std::rand( ); } T_Array< uint32_t > range( array.copyRange( 1000 , 1999 ) ); CPPUNIT_ASSERT_EQUAL( uint32_t( 1000 ) , range.size( ) ); for ( uint32_t i = 0 ; i < 1000 ; i ++ ) { CPPUNIT_ASSERT_EQUAL( array[ i + 1000 ] , range[ i ] ); } }