corelib/tests/arrays-static-basic.cc

468 lines
12 KiB
C++

#include <lw/lib/Arrays.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace lw;
class ArraysStaticBasicTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( ArraysStaticBasicTest );
CPPUNIT_TEST( testInitialValues );
CPPUNIT_TEST( testAppendOne );
CPPUNIT_TEST( testAppendTwo );
CPPUNIT_TEST( testClear );
CPPUNIT_TEST( testCopyCons );
CPPUNIT_TEST( testCopyAss );
CPPUNIT_TEST( testMoveCons );
CPPUNIT_TEST( testMoveAss );
CPPUNIT_TEST( testSwap );
CPPUNIT_TEST( testAddAllEmpty );
CPPUNIT_TEST( testAddAll );
CPPUNIT_TEST( testAddAllLimit );
CPPUNIT_TEST( testAddAllMove );
CPPUNIT_TEST( testInsertEmpty );
CPPUNIT_TEST( testInsertAfter );
CPPUNIT_TEST( testInsertBefore );
CPPUNIT_TEST( testRemoveEnd );
CPPUNIT_TEST( testRemoveMiddle );
CPPUNIT_TEST( testRemoveSwapEnd );
CPPUNIT_TEST( testRemoveSwapMiddle );
CPPUNIT_TEST( testIndexOf );
CPPUNIT_TEST( testIndexOfMissing );
CPPUNIT_TEST( testContains );
CPPUNIT_TEST( testContainsMissing );
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 testClear( );
void testCopyCons( );
void testCopyAss( );
void testMoveCons( );
void testMoveAss( );
void testSwap( );
void testRemoveEnd( );
void testRemoveMiddle( );
void testRemoveSwapEnd( );
void testRemoveSwapMiddle( );
void testAddAllEmpty( );
void testAddAll( );
void testAddAllLimit( );
void testAddAllMove( );
void testInsertEmpty( );
void testInsertAfter( );
void testInsertBefore( );
void testIndexOf( );
void testIndexOfMissing( );
void testContains( );
void testContainsMissing( );
void testSort( );
void testSortComparator( );
void testSortPartial( );
void testRangeOutside( );
void testRangeLarger( );
void testRangeSub( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( ArraysStaticBasicTest );
/*----------------------------------------------------------------------------*/
using T_Test_ = T_StaticArray< uint32_t , 8 >;
/*----------------------------------------------------------------------------*/
void ArraysStaticBasicTest::testInitialValues( )
{
T_Test_ array;
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 8 ) , array.capacity( ) );
}
/*----------------------------------------------------------------------------*/
void ArraysStaticBasicTest::testAppendOne( )
{
T_Test_ array;
array.add( 123 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] );
}
void ArraysStaticBasicTest::testAppendTwo( )
{
T_Test_ array;
array.add( 123 );
array.add( 456 );
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 ArraysStaticBasicTest::testClear( )
{
T_Test_ array;
const uint32_t n = 8;
for ( uint32_t i = 0 ; i < n ; i ++ ) {
array.add( i );
}
array.clear( );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , array.size( ) );
}
/*----------------------------------------------------------------------------*/
void ArraysStaticBasicTest::testCopyCons( )
{
T_Test_ array;
array.add( 1 );
T_Test_ copy( array );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , copy.size( ) );
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 ArraysStaticBasicTest::testCopyAss( )
{
T_Test_ array;
array.add( 1 );
T_Test_ copy;
copy = array;
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , copy.size( ) );
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 ArraysStaticBasicTest::testMoveCons( )
{
T_Test_ init;
init.add( 1 );
T_Test_ array( std::move( init ) );
CPPUNIT_ASSERT_EQUAL( 0u , init.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array[ 0 ] );
}
void ArraysStaticBasicTest::testMoveAss( )
{
T_Test_ init;
init.add( 1 );
T_Test_ array;
array = std::move( init );
CPPUNIT_ASSERT_EQUAL( 0u , init.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array[ 0 ] );
}
/*----------------------------------------------------------------------------*/
void ArraysStaticBasicTest::testSwap( )
{
T_Test_ a1 , 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 ArraysStaticBasicTest::testAddAllEmpty( )
{
T_Test_ empty , 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 ArraysStaticBasicTest::testAddAll( )
{
T_Test_ array , other;
array << 123 << 456 << 789;
other << 627 << 951 << 843;
array.addAll( other );
CPPUNIT_ASSERT_EQUAL( 6u , array.size( ) );
CPPUNIT_ASSERT_EQUAL( 3u , other.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 ArraysStaticBasicTest::testAddAllLimit( )
{
T_Test_ array , other;
for ( auto i = 0u ; i < 4 ; i ++ ) {
array.add( i );
other.add( i + 4 );
}
array.addAll( other );
CPPUNIT_ASSERT_EQUAL( 8u , array.size( ) );
for ( uint32_t i = 0 ; i < 8 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( i , array[ i ] );
}
}
void ArraysStaticBasicTest::testAddAllMove( )
{
T_Test_ array , other;
array << 123 << 456 << 789;
other << 627 << 951 << 843;
array.addAll( std::move( other ) );
CPPUNIT_ASSERT_EQUAL( 6u , array.size( ) );
CPPUNIT_ASSERT_EQUAL( 0u , other.size( ) );
CPPUNIT_ASSERT_EQUAL( 123u , array[ 0 ] );
CPPUNIT_ASSERT_EQUAL( 456u , array[ 1 ] );
CPPUNIT_ASSERT_EQUAL( 789u , array[ 2 ] );
CPPUNIT_ASSERT_EQUAL( 627u , array[ 3 ] );
CPPUNIT_ASSERT_EQUAL( 951u , array[ 4 ] );
CPPUNIT_ASSERT_EQUAL( 843u , array[ 5 ] );
}
/*----------------------------------------------------------------------------*/
void ArraysStaticBasicTest::testInsertEmpty( )
{
T_Test_ array;
array.insert( 0 , 123 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 1 ) , array.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 123 ) , array[ 0 ] );
}
void ArraysStaticBasicTest::testInsertAfter( )
{
T_Test_ 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 ArraysStaticBasicTest::testInsertBefore( )
{
T_Test_ 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 ArraysStaticBasicTest::testRemoveEnd( )
{
T_Test_ 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 ArraysStaticBasicTest::testRemoveMiddle( )
{
T_Test_ 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 ArraysStaticBasicTest::testRemoveSwapEnd( )
{
T_Test_ 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 ArraysStaticBasicTest::testRemoveSwapMiddle( )
{
T_Test_ 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 ArraysStaticBasicTest::testIndexOf( )
{
T_Test_ array;
array << 123 << 456 << 789;
CPPUNIT_ASSERT_EQUAL( int32_t( 1 ) , array.indexOf( 456 ) );
}
void ArraysStaticBasicTest::testIndexOfMissing( )
{
T_Test_ array;
array << 123 << 456 << 789;
CPPUNIT_ASSERT_EQUAL( int32_t( -1 ) , array.indexOf( 101112 ) );
}
void ArraysStaticBasicTest::testContains( )
{
T_Test_ array;
array << 123 << 456 << 789;
CPPUNIT_ASSERT( array.contains( 456 ) );
}
void ArraysStaticBasicTest::testContainsMissing( )
{
T_Test_ array;
array << 123 << 456 << 789;
CPPUNIT_ASSERT( !array.contains( 101112 ) );
}
/*----------------------------------------------------------------------------*/
void ArraysStaticBasicTest::testSort( )
{
T_StaticArray< uint32_t , 5000 > 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 ArraysStaticBasicTest::testSortComparator( )
{
T_StaticArray< uint32_t , 5000 > 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 ArraysStaticBasicTest::testSortPartial( )
{
T_StaticArray< uint32_t , 5000 > array;
for ( uint32_t i = 0 ; i < 5000 ; i ++ ) {
array << std::rand( );
}
decltype( array ) 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 ArraysStaticBasicTest::testRangeOutside( )
{
T_Test_ array;
for ( uint32_t i = 0 ; i < 8 ; i ++ ) {
array << std::rand( );
}
T_Test_ range( array.copyRange( 100 , 1000 ) );
CPPUNIT_ASSERT_EQUAL( 0u , range.size( ) );
}
void ArraysStaticBasicTest::testRangeLarger( )
{
T_Test_ array;
for ( uint32_t i = 0 ; i < 8 ; i ++ ) {
array << std::rand( );
}
T_Test_ range( array.copyRange( 4 ) );
CPPUNIT_ASSERT_EQUAL( 4u , range.size( ) );
for ( uint32_t i = 0 ; i < 4 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( array[ i + 4 ] , range[ i ] );
}
}
void ArraysStaticBasicTest::testRangeSub( )
{
T_Test_ array;
for ( uint32_t i = 0 ; i < 8 ; i ++ ) {
array << std::rand( );
}
T_Test_ range( array.copyRange( 2 , 5 ) );
CPPUNIT_ASSERT_EQUAL( 4u , range.size( ) );
for ( uint32_t i = 0 ; i < 4 ; i ++ ) {
CPPUNIT_ASSERT_EQUAL( array[ i + 2 ] , range[ i ] );
}
}