corelib/tests/strings-storage.cc

328 lines
8.3 KiB
C++

#include <lw/lib/Strings.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace lw;
/*= StringsDynamicTest ========================================================*/
class StringsDynamicTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( StringsDynamicTest );
CPPUNIT_TEST( testNoPool );
CPPUNIT_TEST( testPoolReg );
CPPUNIT_TEST( testPoolPersist );
CPPUNIT_TEST( testTStringPooled );
CPPUNIT_TEST( testDynamicCons );
CPPUNIT_TEST( testUsePool );
CPPUNIT_TEST_SUITE_END( );
private:
static const char TEST_STRING_1[];
static const char TEST_STRING_2[];
static const char TEST_STRING_3[];
static const char TEST_STRING_4[];
static const char TEST_STRING_5[];
static const char TEST_STRING_6[];
public:
void testNoPool( );
void testPoolReg( );
void testPoolPersist( );
void testTStringPooled( );
void testDynamicCons( );
void testUsePool( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( StringsDynamicTest );
/*----------------------------------------------------------------------------*/
const char StringsDynamicTest::TEST_STRING_1[] = "Dynamic strings test 1";
const char StringsDynamicTest::TEST_STRING_2[] = "Dynamic strings test 2";
const char StringsDynamicTest::TEST_STRING_3[] = "Dynamic strings test 3";
const char StringsDynamicTest::TEST_STRING_4[] = "Dynamic strings test 4";
const char StringsDynamicTest::TEST_STRING_5[] = "Dynamic strings test 5";
const char StringsDynamicTest::TEST_STRING_6[] = "Dynamic strings test 6";
/*----------------------------------------------------------------------------*/
void StringsDynamicTest::testNoPool( )
{
T_String test1( TEST_STRING_1 ) , test2( TEST_STRING_1 );
CPPUNIT_ASSERT( test1.data( ) != test2.data( ) );
}
void StringsDynamicTest::testPoolReg( )
{
const T_String pooled( std::move(
T_String( TEST_STRING_2 ).addToPool( ) ) );
const T_String test1( TEST_STRING_2 ) , test2( TEST_STRING_2 );
CPPUNIT_ASSERT( test1.data( ) == pooled.data( ) );
CPPUNIT_ASSERT( test2.data( ) == pooled.data( ) );
}
void StringsDynamicTest::testPoolPersist( )
{
{
T_String( TEST_STRING_3 ).addToPool( );
}
const T_String test1( TEST_STRING_3 ) , test2( TEST_STRING_3 );
CPPUNIT_ASSERT( test1.data( ) == test2.data( ) );
}
void StringsDynamicTest::testTStringPooled( )
{
const T_String pooled( T_String::Pooled( TEST_STRING_4 ) );
const T_String test( TEST_STRING_4 );
CPPUNIT_ASSERT( test.data( ) == pooled.data( ) );
}
void StringsDynamicTest::testDynamicCons( )
{
const T_String pooled( T_String::Pooled( TEST_STRING_5 ) );
const T_String test( TEST_STRING_5 , strlen( TEST_STRING_5 ) );
CPPUNIT_ASSERT( test.data( ) != pooled.data( ) );
}
void StringsDynamicTest::testUsePool( )
{
T_String test( TEST_STRING_6 );
const T_String pooled( T_String::Pooled( TEST_STRING_6 ) );
CPPUNIT_ASSERT( test.data( ) != pooled.data( ) );
test.usePool( );
CPPUNIT_ASSERT( test.data( ) == pooled.data( ) );
}
/*= StringsStaticTest ========================================================*/
class StringsStaticTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( StringsStaticTest );
CPPUNIT_TEST( testEmpty );
CPPUNIT_TEST( testUnpooled );
CPPUNIT_TEST( testPooled );
CPPUNIT_TEST_SUITE_END( );
private:
static const char TEST_STRING_1[];
static const char TEST_STRING_2[];
public:
void testEmpty( );
void testUnpooled( );
void testPooled( );
};
const char StringsStaticTest::TEST_STRING_1[] = "Static strings test 1";
const char StringsStaticTest::TEST_STRING_2[] = "Static strings test 2";
CPPUNIT_TEST_SUITE_REGISTRATION( StringsStaticTest );
/*----------------------------------------------------------------------------*/
void StringsStaticTest::testEmpty( )
{
T_String test1 , test2( "" ) , test3( T_String::Pooled( "" ) );
CPPUNIT_ASSERT( test1.data( ) == nullptr );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , test1.length( ) );
CPPUNIT_ASSERT( test2.data( ) == nullptr );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , test2.length( ) );
CPPUNIT_ASSERT( test3.data( ) == nullptr );
CPPUNIT_ASSERT_EQUAL( uint32_t( 0 ) , test3.length( ) );
}
/*----------------------------------------------------------------------------*/
void StringsStaticTest::testUnpooled( )
{
T_String test1( TEST_STRING_1 ) , test2( TEST_STRING_1 );
CPPUNIT_ASSERT( test1.data( ) != test2.data( ) );
}
/*----------------------------------------------------------------------------*/
void StringsStaticTest::testPooled( )
{
char const* ptr;
{
const T_String reg( T_String::Pooled( TEST_STRING_2 ) );
const T_String test1( TEST_STRING_2 ) , test2( TEST_STRING_2 );
CPPUNIT_ASSERT( test1.data( ) == reg.data( ) );
CPPUNIT_ASSERT( test2.data( ) == reg.data( ) );
ptr = reg.data( );
}
{
const T_String test( TEST_STRING_2 );
CPPUNIT_ASSERT( test.data( ) == ptr );
}
{
const T_String test( T_String::Pooled( TEST_STRING_2 ) );
CPPUNIT_ASSERT( test.data( ) == ptr );
}
{
const T_String test( TEST_STRING_2 , strlen( TEST_STRING_2 ) );
CPPUNIT_ASSERT( test.data( ) != ptr );
}
{
T_String test( TEST_STRING_2 , strlen( TEST_STRING_2 ) );
test.usePool( );
CPPUNIT_ASSERT( test.data( ) == ptr );
}
{
T_String test( TEST_STRING_2 , strlen( TEST_STRING_2 ) );
test.addToPool( );
CPPUNIT_ASSERT( test.data( ) == ptr );
}
}
/*= SubstringsTest ===========================================================*/
class SubstringsTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( SubstringsTest );
CPPUNIT_TEST( testLeft );
CPPUNIT_TEST( testRight );
CPPUNIT_TEST( testSubstr );
CPPUNIT_TEST( testRange );
CPPUNIT_TEST( testPooling );
CPPUNIT_TEST_SUITE_END( );
private:
static const char TEST_STRING[];
public:
void testLeft( );
void testRight( );
void testSubstr( );
void testRange( );
void testPooling( );
};
const char SubstringsTest::TEST_STRING[] = "abcdefghijkl";
CPPUNIT_TEST_SUITE_REGISTRATION( SubstringsTest );
/*----------------------------------------------------------------------------*/
void SubstringsTest::testLeft( )
{
const T_String s( TEST_STRING );
{
const T_String sub( s.left( 4 ) );
CPPUNIT_ASSERT( sub == "abcd" );
}
{
const T_String sub( s.left( 0 ) );
CPPUNIT_ASSERT( sub == "" );
}
{
const T_String sub( s.left( s.length( ) * 2 ) );
CPPUNIT_ASSERT( sub == s );
}
}
/*----------------------------------------------------------------------------*/
void SubstringsTest::testRight( )
{
const T_String s( TEST_STRING );
{
const T_String sub( s.right( 4 ) );
CPPUNIT_ASSERT( sub == "ijkl" );
}
{
const T_String sub( s.right( 0 ) );
CPPUNIT_ASSERT( sub == "" );
}
{
const T_String sub( s.right( s.length( ) * 2 ) );
CPPUNIT_ASSERT( sub == s );
}
}
/*----------------------------------------------------------------------------*/
void SubstringsTest::testSubstr( )
{
const T_String s( TEST_STRING );
{
const T_String sub( s.substr( 2 ) );
CPPUNIT_ASSERT( sub == "cdefghijkl" );
}
{
const T_String sub( s.substr( 2 , 2 ) );
CPPUNIT_ASSERT( sub == "cd" );
}
{
const T_String sub( s.substr( 0 ) );
CPPUNIT_ASSERT( sub == s );
}
{
const T_String sub( s.substr( 0 , 2 ) );
CPPUNIT_ASSERT( sub == "ab" );
}
{
const T_String sub( s.substr( s.length( ) ) );
CPPUNIT_ASSERT( sub == "" );
}
{
const T_String sub( s.substr( 0 , 0 ) );
CPPUNIT_ASSERT( sub == "" );
}
}
/*----------------------------------------------------------------------------*/
void SubstringsTest::testRange( )
{
const T_String s( TEST_STRING );
{
const T_String sub( s.range( 2 , UINT32_MAX ) );
CPPUNIT_ASSERT( sub == "cdefghijkl" );
}
{
const T_String sub( s.range( 2 , 3 ) );
CPPUNIT_ASSERT( sub == "cd" );
}
{
const T_String sub( s.range( 0 , UINT32_MAX ) );
CPPUNIT_ASSERT( sub == s );
}
{
const T_String sub( s.range( 0 , 1 ) );
CPPUNIT_ASSERT( sub == "ab" );
}
{
const T_String sub( s.range( s.length( ) , UINT32_MAX ) );
CPPUNIT_ASSERT( sub == "" );
}
{
const T_String sub( s.range( 0 , 0 ) );
CPPUNIT_ASSERT( sub == "a" );
}
{
const T_String sub( s.range( 1 , 0 ) );
CPPUNIT_ASSERT( sub == "" );
}
}
/*----------------------------------------------------------------------------*/
void SubstringsTest::testPooling( )
{
const T_String s( TEST_STRING );
const T_String pooled( T_String::Pooled( "abcd" ) );
T_String sub( s.left( 4 ) );
CPPUNIT_ASSERT( sub == pooled );
CPPUNIT_ASSERT( sub.data( ) != pooled.data( ) );
sub.addToPool( );
CPPUNIT_ASSERT( sub == pooled );
CPPUNIT_ASSERT( sub.data( ) == pooled.data( ) );
}