Skip to content

Commit

Permalink
add modifications to FileReader class (and unit tests)
Browse files Browse the repository at this point in the history
issue #29
  • Loading branch information
Valentin Noel committed Jul 24, 2013
1 parent bdcc5fc commit 6e6cac2
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 48 deletions.
6 changes: 3 additions & 3 deletions libraries/fileReader/src/FileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ size_t FileReader::getPosition()

bool FileReader::readData( char* data, const size_t size )
{
_fileBuffer.read( data, size );
_fileBuffer.readsome( data, size );
return ( _fileBuffer.gcount() == (int)size );
}

void FileReader::goToBegin()
{
_fileBuffer.seekg( std::ios_base::beg );
_fileBuffer.seekg( 0, _fileBuffer.beg );
}

void FileReader::goBack( const std::size_t size )
Expand All @@ -45,7 +45,7 @@ void FileReader::goForward( const std::size_t size )

bool FileReader::isEndOfFile( )
{
return _fileBuffer.eof();
return getPosition() == getLength();
}

}
199 changes: 154 additions & 45 deletions libraries/fileReader/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,175 @@
using namespace boost::unit_test;
using namespace std;

boost::shared_ptr<common::formatters::Formatter> formatter( common::formatters::Formatter::get() );
boost::shared_ptr<common::Color> color( common::Color::get() );

BOOST_AUTO_TEST_SUITE( fileReader_tests_suite01 )

BOOST_AUTO_TEST_CASE( fileReader_init )
{
formatter->init_logging();
color->enable();
}

BOOST_AUTO_TEST_CASE( fileReader_test_streambuffer )
{
bool ret;
std::stringbuf buffer;
filereader::FileReader fr( &buffer );
{
LOG_INFO( " >>> fileReader_test_streambuffer <<< " );
bool ret;
std::stringbuf buffer;
filereader::FileReader fr( &buffer );

BOOST_CHECK_EQUAL( fr.getLength(), 0 );
buffer.sputc( 'W' );
buffer.sputc( 'A' );
buffer.sputc( 'V' );
buffer.sputc( 'E' );
BOOST_CHECK_EQUAL( fr.getLength(), 4 );

BOOST_CHECK_EQUAL( fr.getPosition(), 0 );

char out[4] = { 0, 0, 0, 0 };

ret= fr.readData( out, 2 );
BOOST_CHECK_EQUAL( ret, true );

BOOST_CHECK_EQUAL( out[0], 'W' );
BOOST_CHECK_EQUAL( out[1], 'A' );
BOOST_CHECK_EQUAL( out[2], 0 );
BOOST_CHECK_EQUAL( out[3], 0 );

BOOST_CHECK_EQUAL( fr.getPosition(), 2 );
BOOST_CHECK_EQUAL( fr.getLength(), 4 );
BOOST_CHECK_EQUAL( fr.getPosition(), 2 );

ret = fr.readData( out, 2 );
BOOST_CHECK_EQUAL( ret, true );

BOOST_CHECK_EQUAL( out[0], 'V' );
BOOST_CHECK_EQUAL( out[1], 'E' );
BOOST_CHECK_EQUAL( out[2], 0 );
BOOST_CHECK_EQUAL( out[3], 0 );

BOOST_CHECK_EQUAL( fr.getPosition(), 4 );
BOOST_CHECK_EQUAL( fr.getLength(), 4 );
BOOST_CHECK_EQUAL( fr.getPosition(), 4 );
BOOST_CHECK_EQUAL( fr.isEndOfFile(), true );

ret = fr.readData( out, 2 );
BOOST_CHECK_EQUAL( ret, 0 );

BOOST_CHECK_EQUAL( fr.isEndOfFile(), true );
fr.goToBegin();
BOOST_CHECK_EQUAL( fr.isEndOfFile(), false );
}
{
bool ret;
std::stringbuf buffer;
filereader::FileReader fr( &buffer );

BOOST_CHECK_EQUAL( fr.getLength(), 0 );
buffer.sputc( 'W' );
buffer.sputc( 'A' );
buffer.sputc( 'V' );
buffer.sputc( 'E' );
BOOST_CHECK_EQUAL( fr.getLength(), 4 );

BOOST_CHECK_EQUAL( fr.getPosition(), 0 );

char out1[1] = { 0 };

ret= fr.readData( out1, 1 );
BOOST_CHECK_EQUAL( ret, true );
BOOST_CHECK_EQUAL( out1[0], 'W' );
BOOST_CHECK_EQUAL( fr.getPosition(), 1 );

fr.goForward( 2 );
ret= fr.readData( out1, 1 );
BOOST_CHECK_EQUAL( ret, true );
BOOST_CHECK_EQUAL( out1[0], 'E' );
BOOST_CHECK_EQUAL( fr.getPosition(), 4 );
BOOST_CHECK_EQUAL( fr.isEndOfFile(), true );

fr.goBack( 1 );
ret= fr.readData( out1, 1 );
BOOST_CHECK_EQUAL( ret, true );
BOOST_CHECK_EQUAL( out1[0], 'E' );
BOOST_CHECK_EQUAL( fr.getPosition(), 4 );
BOOST_CHECK_EQUAL( fr.isEndOfFile(), true );

fr.goBack( 3 );
ret= fr.readData( out1, 1 );
BOOST_CHECK_EQUAL( ret, true );
BOOST_CHECK_EQUAL( out1[0], 'A' );
BOOST_CHECK_EQUAL( fr.getPosition(), 2 );

fr.goBack( 2 );
ret= fr.readData( out1, 1 );
BOOST_CHECK_EQUAL( ret, true );
BOOST_CHECK_EQUAL( out1[0], 'W' );
BOOST_CHECK_EQUAL( fr.getPosition(), 1 );

fr.goForward( 1 );
ret= fr.readData( out1, 1 );
BOOST_CHECK_EQUAL( ret, true );
BOOST_CHECK_EQUAL( out1[0], 'V' );
BOOST_CHECK_EQUAL( fr.getPosition(), 3 );

fr.goBack( 3 );
BOOST_CHECK_EQUAL( fr.getPosition(), 0 );

char out2[4] = { 0, 0, 0, 0 };
ret= fr.readData( out2, 4 );
BOOST_CHECK_EQUAL( ret, true );
BOOST_CHECK_EQUAL( strcmp( out2, "WAVE" ), 0 );
BOOST_CHECK_EQUAL( fr.getPosition(), 4 );
BOOST_CHECK_EQUAL( fr.isEndOfFile(), true );
}
}

BOOST_AUTO_TEST_CASE( fileReader_test_filebuffer )
{
LOG_INFO( " >>> fileReader_test_filebuffer <<< " );

std::string content = "FILE reader";

ofstream myfile;
myfile.open ("test.txt");
myfile << content;
myfile.close();

std::ifstream is;
std::filebuf* fb = is.rdbuf();

BOOST_CHECK_EQUAL( fr.getLength(), 0 );
buffer.sputc( 'W' );
buffer.sputc( 'A' );
buffer.sputc( 'V' );
buffer.sputc( 'E' );
BOOST_CHECK_EQUAL( fr.getLength(), 4 );
fb->open( "test.txt", std::ios::in );
BOOST_CHECK_EQUAL( fb->is_open(), true );
BOOST_CHECK_EQUAL( fb->in_avail(), content.size() );

filereader::FileReader fr( fb );

BOOST_CHECK_EQUAL( fr.getLength(), content.size() );
BOOST_CHECK_EQUAL( fr.getPosition(), 0 );

char out[4] = { 0, 0, 0, 0};

ret= fr.readData( out, 2 );

BOOST_CHECK_EQUAL( ret, 1 );
BOOST_CHECK_EQUAL( out[0], 'W' );
BOOST_CHECK_EQUAL( out[1], 'A' );
BOOST_CHECK_EQUAL( out[2], 0 );
BOOST_CHECK_EQUAL( out[3], 0 );
BOOST_CHECK_EQUAL( fr.getPosition(), 2 );
BOOST_CHECK_EQUAL( fr.getLength(), 4 );
BOOST_CHECK_EQUAL( fr.getPosition(), 2 );

ret = fr.readData( out, 2 );

BOOST_CHECK_EQUAL( ret, 1 );
BOOST_CHECK_EQUAL( out[0], 'V' );
BOOST_CHECK_EQUAL( out[1], 'E' );
BOOST_CHECK_EQUAL( out[2], 0 );
BOOST_CHECK_EQUAL( out[3], 0 );
BOOST_CHECK_EQUAL( fr.getPosition(), 4 );
BOOST_CHECK_EQUAL( fr.getLength(), 4 );
BOOST_CHECK_EQUAL( fr.getPosition(), 4 );
char buffer[ content.size() ];
bool ret;

ret= fr.readData( buffer, content.size() );
BOOST_CHECK_EQUAL( ret, true );

BOOST_CHECK_EQUAL( strcmp( content.c_str(), buffer ), 0 );

BOOST_CHECK_EQUAL( fr.getPosition(), content.size() );
BOOST_CHECK_EQUAL( fr.getLength(), content.size() );
BOOST_CHECK_EQUAL( fr.getPosition(), content.size() );
BOOST_CHECK_EQUAL( fr.isEndOfFile(), true );

ret = fr.readData( out, 2 );
ret = fr.readData( buffer, 1 );
BOOST_CHECK_EQUAL( ret, 0 );

BOOST_CHECK_EQUAL( fr.isEndOfFile(), true );
fr.goToBegin();
BOOST_CHECK_EQUAL( fr.isEndOfFile(), false );
}


BOOST_AUTO_TEST_CASE( fileReader_test_filebuffer )
{
std::ifstream is;
std::filebuf* fb = is.rdbuf();

fb->open( "test.txt", std::ios::out | std::ios::app );


filereader::FileReader fr( fb );

fb->close();
}
Expand Down

0 comments on commit 6e6cac2

Please sign in to comment.