From da89e6d30e57bd6dbb10b4d36b093ce9bbf5c666 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Apr 2007 04:50:36 +0000 Subject: The first batch seem to have made it alright. Unfortunately the Archive class isn't done yet, I'm going to make it rely on streams, so those will be next, then we can make it work all sortsa' well. --- src/tests/archive.cpp | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/tests/archive.cpp (limited to 'src/tests/archive.cpp') diff --git a/src/tests/archive.cpp b/src/tests/archive.cpp new file mode 100644 index 0000000..fb0d97c --- /dev/null +++ b/src/tests/archive.cpp @@ -0,0 +1,7 @@ +#include "archive.h" + +int main() +{ + //Archive +} + -- cgit v1.2.3 From c884da672645231b5ec47706c886381dab1b391a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 3 Apr 2007 05:09:12 +0000 Subject: The file stream is imported and works, as does our first test, and the new tweaks to archive. The && operator is now a template function, and as such requires no special handling. It could be worth it to check this out for other types, yet dangerous, since it would let you archive anything, even a class, without writing the proper functions for it...we shall see what happens... --- src/archive.cpp | 27 ++++++++++++++-- src/archive.h | 12 ++++--- src/old/sfile.cpp | 74 ------------------------------------------ src/old/sfile.h | 29 ----------------- src/sfile.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/sfile.h | 36 +++++++++++++++++++++ src/tests/archive.cpp | 11 ++++++- 7 files changed, 169 insertions(+), 110 deletions(-) delete mode 100644 src/old/sfile.cpp delete mode 100644 src/old/sfile.h create mode 100644 src/sfile.cpp create mode 100644 src/sfile.h (limited to 'src/tests/archive.cpp') diff --git a/src/archive.cpp b/src/archive.cpp index 5f5145c..be06c0e 100644 --- a/src/archive.cpp +++ b/src/archive.cpp @@ -1,13 +1,36 @@ #include "archive.h" -Bu::Archive::Archive(bool bLoading): - bLoading(bLoading) +Bu::Archive::Archive( Stream &rStream, bool bLoading ) : + bLoading( bLoading ), + rStream( rStream ) { } + Bu::Archive::~Archive() { } +void Bu::Archive::write( const void *pData, int32_t nSize ) +{ + if( nSize == 0 || pData == NULL ) + return; + + rStream.write( (const char *)pData, nSize ); +} + +void Bu::Archive::read( void *pData, int32_t nSize ) +{ + if( nSize == 0 || pData == NULL ) + return; + + rStream.read( (char *)pData, nSize ); +} + +void Bu::Archive::close() +{ + rStream.close(); +} + bool Bu::Archive::isLoading() { return bLoading; diff --git a/src/archive.h b/src/archive.h index 7de9220..26e430b 100644 --- a/src/archive.h +++ b/src/archive.h @@ -4,6 +4,7 @@ #include #include #include "archable.h" +#include "stream.h" namespace Bu { @@ -20,12 +21,12 @@ namespace Bu save = false }; - Archive( bool bLoading ); + Archive( Stream &rStream, bool bLoading ); virtual ~Archive(); - virtual void close()=0; + virtual void close(); - virtual void write(const void *, int32_t)=0; - virtual void read(void *, int32_t)=0; + virtual void write(const void *, int32_t); + virtual void read(void *, int32_t); virtual Archive &operator<<(bool); virtual Archive &operator<<(int8_t); @@ -67,6 +68,9 @@ namespace Bu virtual Archive &operator&&(float &); virtual Archive &operator&&(double &); virtual Archive &operator&&(long double &); + + private: + Stream &rStream; }; Archive &operator<<(Archive &, class Bu::Archable &); diff --git a/src/old/sfile.cpp b/src/old/sfile.cpp deleted file mode 100644 index f1de03c..0000000 --- a/src/old/sfile.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include "sfile.h" -#include "exceptions.h" - -SFile::SFile( const char *sName, const char *sFlags ) -{ - fh = fopen( sName, sFlags ); -} - -SFile::~SFile() -{ -} - -void SFile::close() -{ - if( fh ) - { - fclose( fh ); - fh = NULL; - } -} - -size_t SFile::read( char *pBuf, size_t nBytes ) -{ - if( !fh ) - throw FileException("File not open."); - - return fread( pBuf, 1, nBytes, fh ); -} - -size_t SFile::write( const char *pBuf, size_t nBytes ) -{ - if( !fh ) - throw FileException("File not open."); - - return fwrite( pBuf, 1, nBytes, fh ); -} - -long SFile::tell() -{ - if( !fh ) - throw FileException("File not open."); - - return ftell( fh ); -} - -void SFile::seek( long offset ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, offset, SEEK_CUR ); -} - -void SFile::setPos( long pos ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, pos, SEEK_SET ); -} - -void SFile::setPosEnd( long pos ) -{ - if( !fh ) - throw FileException("File not open."); - - fseek( fh, pos, SEEK_END ); -} - -bool SFile::isEOS() -{ - return feof( fh ); -} - diff --git a/src/old/sfile.h b/src/old/sfile.h deleted file mode 100644 index b51e5bc..0000000 --- a/src/old/sfile.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef SFILE_H -#define SFILE_H - -#include - -#include "stream.h" - -class SFile : public Stream -{ -public: - SFile( const char *sName, const char *sFlags ); - 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 long tell(); - virtual void seek( long offset ); - virtual void setPos( long pos ); - virtual void setPosEnd( long pos ); - virtual bool isEOS(); - -private: - FILE *fh; - -}; - -#endif diff --git a/src/sfile.cpp b/src/sfile.cpp new file mode 100644 index 0000000..d7c5c83 --- /dev/null +++ b/src/sfile.cpp @@ -0,0 +1,90 @@ +#include "sfile.h" +#include "exceptions.h" + +Bu::SFile::SFile( const char *sName, const char *sFlags ) +{ + fh = fopen( sName, sFlags ); +} + +Bu::SFile::~SFile() +{ + close(); +} + +void Bu::SFile::close() +{ + if( fh ) + { + fclose( fh ); + fh = NULL; + } +} + +size_t Bu::SFile::read( char *pBuf, size_t nBytes ) +{ + if( !fh ) + throw FileException("File not open."); + + return fread( pBuf, 1, nBytes, fh ); +} + +size_t Bu::SFile::write( const char *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 new file mode 100644 index 0000000..304f6b7 --- /dev/null +++ b/src/sfile.h @@ -0,0 +1,36 @@ +#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( char *pBuf, size_t nBytes ); + virtual size_t write( const char *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/tests/archive.cpp b/src/tests/archive.cpp index fb0d97c..5b7e285 100644 --- a/src/tests/archive.cpp +++ b/src/tests/archive.cpp @@ -1,7 +1,16 @@ #include "archive.h" +#include "sfile.h" + +using namespace Bu; int main() { - //Archive + SFile f("test.dat", "wb"); + Archive ar( f, Archive::save ); + + std::string s("Hello there"); + ar << s; + + return 0; } -- cgit v1.2.3 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/tests/archive.cpp') 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