corelib/tests/strings-iterator.cc

156 lines
3.4 KiB
C++

#include <lw/lib/Strings.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace lw;
class StringsIteratorTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( StringsIteratorTest );
CPPUNIT_TEST( testGet );
CPPUNIT_TEST( testMove );
CPPUNIT_TEST( testEmpty );
CPPUNIT_TEST( testCharacters );
CPPUNIT_TEST( testIndex );
CPPUNIT_TEST( testCopyCons );
CPPUNIT_TEST( testCopyAss );
CPPUNIT_TEST( testMoveCons );
CPPUNIT_TEST( testMoveAss );
CPPUNIT_TEST( testSwap );
CPPUNIT_TEST_SUITE_END( );
public:
void testGet( );
void testMove( );
void testEmpty( );
void testCharacters( );
void testIndex( );
void testCopyCons( );
void testCopyAss( );
void testMoveCons( );
void testMoveAss( );
void testSwap( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( StringsIteratorTest );
/*----------------------------------------------------------------------------*/
void StringsIteratorTest::testGet( )
{
T_String str( "wargl" );
T_StringIterator it( str );
CPPUNIT_ASSERT( !it.atEnd( ) );
}
void StringsIteratorTest::testMove( )
{
T_String str( "wargl" );
T_StringIterator it( str );
int counter( 0 );
while ( !it.atEnd( ) ) {
counter ++;
it.next( );
}
CPPUNIT_ASSERT_EQUAL( 5 , counter );
}
void StringsIteratorTest::testEmpty( )
{
T_String str;
T_StringIterator it( str );
CPPUNIT_ASSERT( it.atEnd( ) );
}
void StringsIteratorTest::testCharacters( )
{
char const* const s( "wa\xe2\x82\xacrgl" );
T_String str( s );
T_StringIterator it( str );
int counter( 0 );
while ( !it.atEnd( ) ) {
T_Character c( it );
if ( counter < 2 ) {
CPPUNIT_ASSERT( c == s[ counter ] );
} else if ( counter == 2 ) {
CPPUNIT_ASSERT( c == 0x20ac );
} else {
CPPUNIT_ASSERT( c == s[ counter + 2 ] );
}
counter ++;
it.next( );
}
}
void StringsIteratorTest::testIndex( )
{
char const* const s( "wa\xe2\x82\xacrgl" );
T_String str( s );
T_StringIterator it( str );
uint32_t counter( 0 );
while ( !it.atEnd( ) ) {
CPPUNIT_ASSERT_EQUAL( counter , it.index( ) );
counter ++;
it.next( );
}
}
/*----------------------------------------------------------------------------*/
void StringsIteratorTest::testCopyCons( )
{
T_String str( "wargl" );
T_StringIterator it( str );
it.next( );
T_StringIterator copy( it );
copy.next( );
CPPUNIT_ASSERT( T_Character( it ) == 'a' );
CPPUNIT_ASSERT( T_Character( copy ) == 'r' );
}
void StringsIteratorTest::testCopyAss( )
{
T_String str( "wargl" );
T_StringIterator it( str );
T_StringIterator copy( str );
it.next( );
copy = it;
copy.next( );
CPPUNIT_ASSERT( T_Character( it ) == 'a' );
CPPUNIT_ASSERT( T_Character( copy ) == 'r' );
}
void StringsIteratorTest::testMoveCons( )
{
T_String str( "wargl" );
T_StringIterator it( str );
it.next( );
T_StringIterator copy( std::move( it ) );
copy.next( );
CPPUNIT_ASSERT( it.atEnd( ) );
CPPUNIT_ASSERT( T_Character( copy ) == 'r' );
}
void StringsIteratorTest::testMoveAss( )
{
T_String str( "wargl" );
T_StringIterator it( str );
T_StringIterator copy( str );
it.next( );
copy = std::move( it );
copy.next( );
CPPUNIT_ASSERT( T_Character( it ) == 'w' );
CPPUNIT_ASSERT( T_Character( copy ) == 'r' );
}
void StringsIteratorTest::testSwap( )
{
T_String str( "wargl" );
T_StringIterator it( str );
T_StringIterator copy( str );
it.next( );
swap( it , copy );
CPPUNIT_ASSERT( T_Character( it ) == 'w' );
CPPUNIT_ASSERT( T_Character( copy ) == 'a' );
}