corelib/tests/stream-file-input.cc

268 lines
7 KiB
C++

#include <lw/lib/Files.hh>
#include <cppunit/extensions/HelperMacros.h>
using namespace lw;
#if 0
/*= FileInputStreamTest =====================================================*/
class FileInputStreamTest : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( FileInputStreamTest );
CPPUNIT_TEST( testInitRealFile );
CPPUNIT_TEST( testInitBadFile );
CPPUNIT_TEST( testOpenRealFile );
CPPUNIT_TEST( testOpenBadFile );
CPPUNIT_TEST( testClose );
CPPUNIT_TEST( testReadOpensFile );
CPPUNIT_TEST( testReadAll );
CPPUNIT_TEST( testReadPartial );
CPPUNIT_TEST( testSetPosOpensFile );
CPPUNIT_TEST( testSetPosStart );
CPPUNIT_TEST( testSetPosEnd );
CPPUNIT_TEST( testSetPosEndBad );
CPPUNIT_TEST( testMoveOpensFile );
CPPUNIT_TEST( testMoveForward );
CPPUNIT_TEST( testMoveBack );
CPPUNIT_TEST( testMoveBad );
CPPUNIT_TEST_SUITE_END( );
T_String OK_FILE( )
{
return T_String::Pooled( "tests/data/inputstream.txt" );
}
T_String BAD_FILE( )
{
return T_String::Pooled( "tests/data/no_such_file.txt" );
}
char const* CONTENTS( )
{
return "THIS IS A TEST\n";
}
public:
void testInitRealFile( );
void testInitBadFile( );
void testOpenRealFile( );
void testOpenBadFile( );
void testClose( );
void testReadOpensFile( );
void testReadAll( );
void testReadPartial( );
void testSetPosOpensFile( );
void testSetPosStart( );
void testSetPosEnd( );
void testSetPosEndBad( );
void testMoveOpensFile( );
void testMoveForward( );
void testMoveBack( );
void testMoveBad( );
};
CPPUNIT_TEST_SUITE_REGISTRATION( FileInputStreamTest );
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testInitRealFile( )
{
T_FileInputStream fis( OK_FILE( ) );
CPPUNIT_ASSERT( fis.path( ) == OK_FILE( ) );
CPPUNIT_ASSERT( fis.canUseSize( ) );
CPPUNIT_ASSERT( fis.canRead( ) );
CPPUNIT_ASSERT( fis.size( ) == 0 );
CPPUNIT_ASSERT( fis.position( ) == 0 );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testInitBadFile( )
{
T_FileInputStream fis( BAD_FILE( ) );
CPPUNIT_ASSERT( fis.path( ) == BAD_FILE( ) );
CPPUNIT_ASSERT( fis.canUseSize( ) );
CPPUNIT_ASSERT( fis.canRead( ) );
CPPUNIT_ASSERT( fis.size( ) == 0 );
CPPUNIT_ASSERT( fis.position( ) == 0 );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testOpenRealFile( )
{
T_FileInputStream fis( OK_FILE( ) );
fis.open( );
CPPUNIT_ASSERT( fis.size( ) == 15 );
CPPUNIT_ASSERT( fis.position( ) == 0 );
CPPUNIT_ASSERT( fis.isOpen( ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testOpenBadFile( )
{
T_FileInputStream fis( BAD_FILE( ) );
try {
fis.open( );
CPPUNIT_FAIL( "No exception thrown" );
} catch ( X_StreamError const& e ) {
CPPUNIT_ASSERT( e.code( ) == E_StreamError::SYSTEM_ERROR );
CPPUNIT_ASSERT( e.systemError( ) == ENOENT );
}
CPPUNIT_ASSERT( fis.size( ) == 0 );
CPPUNIT_ASSERT( fis.position( ) == 0 );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testClose( )
{
T_FileInputStream fis( OK_FILE( ) );
fis.open( );
fis.close( );
CPPUNIT_ASSERT( !fis.isOpen( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 0 ) , fis.size( ) );
CPPUNIT_ASSERT_EQUAL( size_t( 0 ) , fis.position( ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testReadOpensFile( )
{
T_FileInputStream fis( OK_FILE( ) );
uint8_t x;
fis.read( &x , 0 );
CPPUNIT_ASSERT( fis.isOpen( ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testReadAll( )
{
T_FileInputStream fis( OK_FILE( ) );
uint8_t buffer[ 1024 ];
auto rv( fis.read( buffer , sizeof( buffer ) ) );
CPPUNIT_ASSERT_EQUAL( fis.size( ) , rv );
CPPUNIT_ASSERT_EQUAL( fis.size( ) , fis.position( ) );
CPPUNIT_ASSERT( !memcmp( buffer , CONTENTS( ) , rv ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testReadPartial( )
{
T_FileInputStream fis( OK_FILE( ) );
uint8_t buffer[ 1024 ];
auto rv( fis.read( buffer , 4 ) );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , rv );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , fis.position( ) );
CPPUNIT_ASSERT( !memcmp( buffer , CONTENTS( ) , 4 ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testSetPosOpensFile( )
{
T_FileInputStream fis( OK_FILE( ) );
fis.position( 4 , false );
CPPUNIT_ASSERT( fis.isOpen( ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testSetPosStart( )
{
T_FileInputStream fis( OK_FILE( ) );
fis.open( );
fis.position( 4 , false );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , fis.position( ) );
uint8_t buffer[ 1024 ];
auto rv( fis.read( buffer , 3 ) );
CPPUNIT_ASSERT_EQUAL( size_t( 3 ) , rv );
CPPUNIT_ASSERT( !memcmp( buffer , CONTENTS( ) + 4 , 3 ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testSetPosEnd( )
{
T_FileInputStream fis( OK_FILE( ) );
fis.open( );
fis.position( 4 , true );
CPPUNIT_ASSERT_EQUAL( size_t( 11 ) , fis.position( ) );
uint8_t buffer[ 1024 ];
auto rv( fis.read( buffer , 1024 ) );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , rv );
CPPUNIT_ASSERT( !memcmp( buffer , CONTENTS( ) + 11 , 4 ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testSetPosEndBad( )
{
T_FileInputStream fis( OK_FILE( ) );
fis.open( );
CPPUNIT_ASSERT_THROW( fis.position( fis.size( ) + 1 , true ) ,
X_StreamError );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testMoveOpensFile( )
{
T_FileInputStream fis( OK_FILE( ) );
fis.move( 0 );
CPPUNIT_ASSERT( fis.isOpen( ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testMoveForward( )
{
T_FileInputStream fis( OK_FILE( ) );
fis.open( );
fis.move( 4 );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , fis.position( ) );
uint8_t buffer[ 1024 ];
auto rv( fis.read( buffer , 3 ) );
CPPUNIT_ASSERT_EQUAL( size_t( 3 ) , rv );
CPPUNIT_ASSERT( !memcmp( buffer , CONTENTS( ) + 4 , 3 ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testMoveBack( )
{
T_FileInputStream fis( OK_FILE( ) );
fis.open( );
fis.position( 8 );
fis.move( -4 );
CPPUNIT_ASSERT_EQUAL( size_t( 4 ) , fis.position( ) );
uint8_t buffer[ 1024 ];
auto rv( fis.read( buffer , 3 ) );
CPPUNIT_ASSERT_EQUAL( size_t( 3 ) , rv );
CPPUNIT_ASSERT( !memcmp( buffer , CONTENTS( ) + 4 , 3 ) );
}
/*---------------------------------------------------------------------------*/
void FileInputStreamTest::testMoveBad( )
{
T_FileInputStream fis( OK_FILE( ) );
fis.open( );
CPPUNIT_ASSERT_THROW( fis.move( -1 ) , X_StreamError );
}
#endif