From 903e7a1e3d4fe99e9de7f4adc1e401ba871caec9 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 10 Apr 2007 17:23:35 +0000 Subject: Wrote some cute file unit tests, and added some more error reporting to SFile. Also fixed the stream system to use void * pointers instead of char *. --- src/sfile.cpp | 16 +++++++++-- src/sfile.h | 4 +-- src/stream.h | 4 +-- src/unit/sfile.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/unitsuite.h | 1 + 5 files changed, 99 insertions(+), 10 deletions(-) diff --git a/src/sfile.cpp b/src/sfile.cpp index d7c5c83..529d8cd 100644 --- a/src/sfile.cpp +++ b/src/sfile.cpp @@ -1,9 +1,14 @@ #include "sfile.h" #include "exceptions.h" +#include Bu::SFile::SFile( const char *sName, const char *sFlags ) { fh = fopen( sName, sFlags ); + if( fh == NULL ) + { + throw Bu::FileException( errno, strerror(errno) ); + } } Bu::SFile::~SFile() @@ -20,15 +25,20 @@ void Bu::SFile::close() } } -size_t Bu::SFile::read( char *pBuf, size_t nBytes ) +size_t Bu::SFile::read( void *pBuf, size_t nBytes ) { if( !fh ) throw FileException("File not open."); - return fread( pBuf, 1, nBytes, fh ); + int nAmnt = fread( pBuf, 1, nBytes, fh ); + + if( nAmnt == 0 ) + throw FileException("End of file."); + + return nAmnt; } -size_t Bu::SFile::write( const char *pBuf, size_t nBytes ) +size_t Bu::SFile::write( const void *pBuf, size_t nBytes ) { if( !fh ) throw FileException("File not open."); diff --git a/src/sfile.h b/src/sfile.h index 304f6b7..f63b812 100644 --- a/src/sfile.h +++ b/src/sfile.h @@ -14,8 +14,8 @@ namespace Bu virtual ~SFile(); virtual void close(); - virtual size_t read( char *pBuf, size_t nBytes ); - virtual size_t write( const char *pBuf, size_t nBytes ); + virtual size_t read( void *pBuf, size_t nBytes ); + virtual size_t write( const void *pBuf, size_t nBytes ); virtual long tell(); virtual void seek( long offset ); diff --git a/src/stream.h b/src/stream.h index 274f4fd..ae94234 100644 --- a/src/stream.h +++ b/src/stream.h @@ -13,8 +13,8 @@ namespace Bu virtual ~Stream(); virtual void close() = 0; - virtual size_t read( char *pBuf, size_t nBytes ) = 0; - virtual size_t write( const char *pBuf, size_t nBytes ) = 0; + virtual size_t read( void *pBuf, size_t nBytes ) = 0; + virtual size_t write( const void *pBuf, size_t nBytes ) = 0; virtual long tell() = 0; virtual void seek( long offset ) = 0; diff --git a/src/unit/sfile.cpp b/src/unit/sfile.cpp index 7b19942..3f52272 100644 --- a/src/unit/sfile.cpp +++ b/src/unit/sfile.cpp @@ -1,4 +1,10 @@ #include "unitsuite.h" +#include "sfile.h" +#include "exceptions.h" + +#include +#include +#include class Unit : public Bu::UnitSuite { @@ -6,7 +12,10 @@ public: Unit() { setName("SFile"); - addTest( Unit::test ); + addTest( Unit::writeFull ); + addTest( Unit::readBlocks ); + addTest( Unit::readError1 ); + addTest( Unit::readError2 ); } virtual ~Unit() { } @@ -14,9 +23,78 @@ public: // // Tests go here // - void test() + void writeFull() + { + Bu::SFile sf("testfile1", "wb"); + for( int c = 0; c < 256; c++ ) + { + unsigned char ch = (unsigned char)c; + sf.write( &ch, 1 ); + unitTest( sf.tell() == c+1 ); + } + //unitTest( sf.canRead() == false ); + //unitTest( sf.canWrite() == true ); + //unitTest( sf.canSeek() == true ); + sf.close(); + struct stat sdat; + stat("testfile1", &sdat ); + unitTest( sdat.st_size == 256 ); + } + + void readBlocks() + { + Bu::SFile sf("testfile1", "rb"); + unsigned char buf[50]; + size_t total = 0; + for(;;) + { + size_t s = sf.read( buf, 50 ); + for( size_t c = 0; c < s; c++ ) + { + unitTest( buf[c] == (unsigned char)(c+total) ); + } + total += s; + if( s < 50 ) + { + unitTest( total == 256 ); + unitTest( sf.isEOS() == true ); + break; + } + } + sf.close(); + } + + void readError1() + { + try + { + Bu::SFile sf("doesn'texist", "rb"); + unitFailed("No exception thrown"); + } + catch( Bu::FileException &e ) + { + return; + } + } + + void readError2() { - unitTest( 1 == 1 ); + Bu::SFile sf("testfile1", "rb"); + char buf[256]; + int r = sf.read( buf, 256 ); + unitTest( r == 256 ); + // You have to read past the end to set the EOS flag. + unitTest( sf.isEOS() == false ); + try + { + int r = sf.read( buf, 5 ); + unitFailed("No exception thrown"); + } + catch( Bu::FileException &e ) + { + sf.close(); + return; + } } }; diff --git a/src/unitsuite.h b/src/unitsuite.h index 3502a1b..db249b2 100644 --- a/src/unitsuite.h +++ b/src/unitsuite.h @@ -56,5 +56,6 @@ namespace Bu { \ throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ ); \ } +#define unitFailed( msg ) throw Bu::UnitSuite::Failed(msg, __FILE__, __LINE__); #endif -- cgit v1.2.3