From 070374dde0f53bff26078550997f7682e84412e5 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 10 Apr 2007 20:16:19 +0000 Subject: I did it, the streams don't start with an S now. --- src/file.cpp | 100 +++++++++++++++++++++++++++++++++++++++++++++++ src/file.h | 36 +++++++++++++++++ src/sfile.cpp | 100 ----------------------------------------------- src/sfile.h | 36 ----------------- src/socket.cpp | 10 +++++ src/socket.h | 24 ++++++++++++ src/ssocket.cpp | 9 ----- src/ssocket.h | 24 ------------ src/tests/archive.cpp | 4 +- src/unit/file.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/unit/sfile.cpp | 105 -------------------------------------------------- src/unitsuite.h | 35 +++++++++++++++++ 12 files changed, 312 insertions(+), 276 deletions(-) create mode 100644 src/file.cpp create mode 100644 src/file.h delete mode 100644 src/sfile.cpp delete mode 100644 src/sfile.h create mode 100644 src/socket.cpp create mode 100644 src/socket.h delete mode 100644 src/ssocket.cpp delete mode 100644 src/ssocket.h create mode 100644 src/unit/file.cpp delete mode 100644 src/unit/sfile.cpp (limited to 'src') diff --git a/src/file.cpp b/src/file.cpp new file mode 100644 index 0000000..5de5f6c --- /dev/null +++ b/src/file.cpp @@ -0,0 +1,100 @@ +#include "file.h" +#include "exceptions.h" +#include + +Bu::File::File( const char *sName, const char *sFlags ) +{ + fh = fopen( sName, sFlags ); + if( fh == NULL ) + { + throw Bu::FileException( errno, strerror(errno) ); + } +} + +Bu::File::~File() +{ + close(); +} + +void Bu::File::close() +{ + if( fh ) + { + fclose( fh ); + fh = NULL; + } +} + +size_t Bu::File::read( void *pBuf, size_t nBytes ) +{ + if( !fh ) + throw FileException("File not open."); + + int nAmnt = fread( pBuf, 1, nBytes, fh ); + + if( nAmnt == 0 ) + throw FileException("End of file."); + + return nAmnt; +} + +size_t Bu::File::write( const void *pBuf, size_t nBytes ) +{ + if( !fh ) + throw FileException("File not open."); + + return fwrite( pBuf, 1, nBytes, fh ); +} + +long Bu::File::tell() +{ + if( !fh ) + throw FileException("File not open."); + + return ftell( fh ); +} + +void Bu::File::seek( long offset ) +{ + if( !fh ) + throw FileException("File not open."); + + fseek( fh, offset, SEEK_CUR ); +} + +void Bu::File::setPos( long pos ) +{ + if( !fh ) + throw FileException("File not open."); + + fseek( fh, pos, SEEK_SET ); +} + +void Bu::File::setPosEnd( long pos ) +{ + if( !fh ) + throw FileException("File not open."); + + fseek( fh, pos, SEEK_END ); +} + +bool Bu::File::isEOS() +{ + return feof( fh ); +} + +bool Bu::File::canRead() +{ + return true; +} + +bool Bu::File::canWrite() +{ + return true; +} + +bool Bu::File::canSeek() +{ + return true; +} + diff --git a/src/file.h b/src/file.h new file mode 100644 index 0000000..bbc620c --- /dev/null +++ b/src/file.h @@ -0,0 +1,36 @@ +#ifndef FILE_H +#define FILE_H + +#include + +#include "stream.h" + +namespace Bu +{ + class File : public Bu::Stream + { + public: + File( const char *sName, const char *sFlags ); + virtual ~File(); + + virtual void close(); + 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 ); + virtual void setPos( long pos ); + virtual void setPosEnd( long pos ); + virtual bool isEOS(); + + virtual bool canRead(); + virtual bool canWrite(); + virtual bool canSeek(); + + private: + FILE *fh; + + }; +} + +#endif diff --git a/src/sfile.cpp b/src/sfile.cpp deleted file mode 100644 index 529d8cd..0000000 --- a/src/sfile.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#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() -{ - close(); -} - -void Bu::SFile::close() -{ - if( fh ) - { - fclose( fh ); - fh = NULL; - } -} - -size_t Bu::SFile::read( void *pBuf, size_t nBytes ) -{ - if( !fh ) - throw FileException("File not open."); - - int nAmnt = fread( pBuf, 1, nBytes, fh ); - - if( nAmnt == 0 ) - throw FileException("End of file."); - - return nAmnt; -} - -size_t Bu::SFile::write( const void *pBuf, size_t nBytes ) -{ - if( !fh ) - throw FileException("File not open."); - - return fwrite( pBuf, 1, nBytes, fh ); -} - -long Bu::SFile::tell() -{ - if( !fh ) - throw FileException("File not open."); - - return ftell( fh ); -} - -void Bu::SFile::seek( long offset ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, offset, SEEK_CUR ); -} - -void Bu::SFile::setPos( long pos ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, pos, SEEK_SET ); -} - -void Bu::SFile::setPosEnd( long pos ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, pos, SEEK_END ); -} - -bool Bu::SFile::isEOS() -{ - return feof( fh ); -} - -bool Bu::SFile::canRead() -{ - return true; -} - -bool Bu::SFile::canWrite() -{ - return true; -} - -bool Bu::SFile::canSeek() -{ - return true; -} - diff --git a/src/sfile.h b/src/sfile.h deleted file mode 100644 index f63b812..0000000 --- a/src/sfile.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef SFILE_H -#define SFILE_H - -#include - -#include "stream.h" - -namespace Bu -{ - class SFile : public Bu::Stream - { - public: - SFile( const char *sName, const char *sFlags ); - virtual ~SFile(); - - virtual void close(); - 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 ); - virtual void setPos( long pos ); - virtual void setPosEnd( long pos ); - virtual bool isEOS(); - - virtual bool canRead(); - virtual bool canWrite(); - virtual bool canSeek(); - - private: - FILE *fh; - - }; -} - -#endif diff --git a/src/socket.cpp b/src/socket.cpp new file mode 100644 index 0000000..c5c592b --- /dev/null +++ b/src/socket.cpp @@ -0,0 +1,10 @@ +#include "socket.h" + +Bu::Socket::Socket() +{ +} + +Bu::Socket::~Socket() +{ +} + diff --git a/src/socket.h b/src/socket.h new file mode 100644 index 0000000..8ccde71 --- /dev/null +++ b/src/socket.h @@ -0,0 +1,24 @@ +#ifndef SOCKET_H +#define SOCKET_H + +#include + +#include "stream.h" + +namespace Bu +{ + /** + * + */ + class Socket : public Stream + { + public: + Socket(); + virtual ~Socket(); + + private: + + }; +} + +#endif diff --git a/src/ssocket.cpp b/src/ssocket.cpp deleted file mode 100644 index 85a2da0..0000000 --- a/src/ssocket.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "ssocket.h" - -Bu::SSocket::SSocket() -{ -} - -Bu::SSocket::~SSocket() -{ -} diff --git a/src/ssocket.h b/src/ssocket.h deleted file mode 100644 index ce02091..0000000 --- a/src/ssocket.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef S_SOCKET_H -#define S_SOCKET_H - -#include - -#include "stream.h" - -namespace Bu -{ - /** - * - */ - class SSocket : public Stream - { - public: - SSocket(); - virtual ~SSocket(); - - private: - - }; -} - -#endif diff --git a/src/tests/archive.cpp b/src/tests/archive.cpp index 5b7e285..2035aa6 100644 --- a/src/tests/archive.cpp +++ b/src/tests/archive.cpp @@ -1,11 +1,11 @@ #include "archive.h" -#include "sfile.h" +#include "file.h" using namespace Bu; int main() { - SFile f("test.dat", "wb"); + File f("test.dat", "wb"); Archive ar( f, Archive::save ); std::string s("Hello there"); diff --git a/src/unit/file.cpp b/src/unit/file.cpp new file mode 100644 index 0000000..a45b8d7 --- /dev/null +++ b/src/unit/file.cpp @@ -0,0 +1,105 @@ +#include "unitsuite.h" +#include "file.h" +#include "exceptions.h" + +#include +#include +#include + +class Unit : public Bu::UnitSuite +{ +public: + Unit() + { + setName("File"); + addTest( Unit::writeFull ); + addTest( Unit::readBlocks ); + addTest( Unit::readError1 ); + addTest( Unit::readError2 ); + } + + virtual ~Unit() { } + + // + // Tests go here + // + void writeFull() + { + Bu::File 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::File 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::File sf("doesn'texist", "rb"); + unitFailed("No exception thrown"); + } + catch( Bu::FileException &e ) + { + return; + } + } + + void readError2() + { + Bu::File 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 + { + sf.read( buf, 5 ); + unitFailed("No exception thrown"); + } + catch( Bu::FileException &e ) + { + sf.close(); + return; + } + } +}; + +int main( int argc, char *argv[] ) +{ + return Unit().run( argc, argv ); +} + diff --git a/src/unit/sfile.cpp b/src/unit/sfile.cpp deleted file mode 100644 index 2aff312..0000000 --- a/src/unit/sfile.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include "unitsuite.h" -#include "sfile.h" -#include "exceptions.h" - -#include -#include -#include - -class Unit : public Bu::UnitSuite -{ -public: - Unit() - { - setName("SFile"); - addTest( Unit::writeFull ); - addTest( Unit::readBlocks ); - addTest( Unit::readError1 ); - addTest( Unit::readError2 ); - } - - virtual ~Unit() { } - - // - // Tests go here - // - 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() - { - 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 - { - sf.read( buf, 5 ); - unitFailed("No exception thrown"); - } - catch( Bu::FileException &e ) - { - sf.close(); - return; - } - } -}; - -int main( int argc, char *argv[] ) -{ - return Unit().run( argc, argv ); -} - diff --git a/src/unitsuite.h b/src/unitsuite.h index db249b2..6e9270a 100644 --- a/src/unitsuite.h +++ b/src/unitsuite.h @@ -8,7 +8,42 @@ namespace Bu { /** + * Provides a unit testing framework. This is pretty easy to use, probably + * the best way to get started is to use ch to generate a template, or use + * the code below with appropriate tweaks: + *@code + * #include "unitsuite.h" + * + * class Unit : public Bu::UnitSuite + * { + * public: + * Unit() + * { + * setName("Example"); + * addTest( Unit::test ); + * } + * + * virtual ~Unit() { } + * + * // + * // Tests go here + * // + * void test() + * { + * unitTest( 1 == 1 ); + * } + * }; + * + * int main( int argc, char *argv[] ) + * { + * return Unit().run( argc, argv ); + * } * + @endcode + * The main function can contain other things, but using this one exactly + * makes all of the test suites work exactly the same. Using the optional + * setName at the top of the constructor replaces the class name with the + * chosen name when printing out stats and info. */ class UnitSuite { -- cgit v1.2.3