summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-04-10 17:23:35 +0000
committerMike Buland <eichlan@xagasoft.com>2007-04-10 17:23:35 +0000
commit903e7a1e3d4fe99e9de7f4adc1e401ba871caec9 (patch)
treeb907c597cf228d4fd58a771f1b65d559c2c01c89
parentb6e100b94b12f3f92ec025dc2363eaf7c0ee6662 (diff)
downloadlibbu++-903e7a1e3d4fe99e9de7f4adc1e401ba871caec9.tar.gz
libbu++-903e7a1e3d4fe99e9de7f4adc1e401ba871caec9.tar.bz2
libbu++-903e7a1e3d4fe99e9de7f4adc1e401ba871caec9.tar.xz
libbu++-903e7a1e3d4fe99e9de7f4adc1e401ba871caec9.zip
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 *.
Diffstat (limited to '')
-rw-r--r--src/sfile.cpp16
-rw-r--r--src/sfile.h4
-rw-r--r--src/stream.h4
-rw-r--r--src/unit/sfile.cpp84
-rw-r--r--src/unitsuite.h1
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 @@
1#include "sfile.h" 1#include "sfile.h"
2#include "exceptions.h" 2#include "exceptions.h"
3#include <errno.h>
3 4
4Bu::SFile::SFile( const char *sName, const char *sFlags ) 5Bu::SFile::SFile( const char *sName, const char *sFlags )
5{ 6{
6 fh = fopen( sName, sFlags ); 7 fh = fopen( sName, sFlags );
8 if( fh == NULL )
9 {
10 throw Bu::FileException( errno, strerror(errno) );
11 }
7} 12}
8 13
9Bu::SFile::~SFile() 14Bu::SFile::~SFile()
@@ -20,15 +25,20 @@ void Bu::SFile::close()
20 } 25 }
21} 26}
22 27
23size_t Bu::SFile::read( char *pBuf, size_t nBytes ) 28size_t Bu::SFile::read( void *pBuf, size_t nBytes )
24{ 29{
25 if( !fh ) 30 if( !fh )
26 throw FileException("File not open."); 31 throw FileException("File not open.");
27 32
28 return fread( pBuf, 1, nBytes, fh ); 33 int nAmnt = fread( pBuf, 1, nBytes, fh );
34
35 if( nAmnt == 0 )
36 throw FileException("End of file.");
37
38 return nAmnt;
29} 39}
30 40
31size_t Bu::SFile::write( const char *pBuf, size_t nBytes ) 41size_t Bu::SFile::write( const void *pBuf, size_t nBytes )
32{ 42{
33 if( !fh ) 43 if( !fh )
34 throw FileException("File not open."); 44 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
14 virtual ~SFile(); 14 virtual ~SFile();
15 15
16 virtual void close(); 16 virtual void close();
17 virtual size_t read( char *pBuf, size_t nBytes ); 17 virtual size_t read( void *pBuf, size_t nBytes );
18 virtual size_t write( const char *pBuf, size_t nBytes ); 18 virtual size_t write( const void *pBuf, size_t nBytes );
19 19
20 virtual long tell(); 20 virtual long tell();
21 virtual void seek( long offset ); 21 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
13 virtual ~Stream(); 13 virtual ~Stream();
14 14
15 virtual void close() = 0; 15 virtual void close() = 0;
16 virtual size_t read( char *pBuf, size_t nBytes ) = 0; 16 virtual size_t read( void *pBuf, size_t nBytes ) = 0;
17 virtual size_t write( const char *pBuf, size_t nBytes ) = 0; 17 virtual size_t write( const void *pBuf, size_t nBytes ) = 0;
18 18
19 virtual long tell() = 0; 19 virtual long tell() = 0;
20 virtual void seek( long offset ) = 0; 20 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 @@
1#include "unitsuite.h" 1#include "unitsuite.h"
2#include "sfile.h"
3#include "exceptions.h"
4
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <unistd.h>
2 8
3class Unit : public Bu::UnitSuite 9class Unit : public Bu::UnitSuite
4{ 10{
@@ -6,7 +12,10 @@ public:
6 Unit() 12 Unit()
7 { 13 {
8 setName("SFile"); 14 setName("SFile");
9 addTest( Unit::test ); 15 addTest( Unit::writeFull );
16 addTest( Unit::readBlocks );
17 addTest( Unit::readError1 );
18 addTest( Unit::readError2 );
10 } 19 }
11 20
12 virtual ~Unit() { } 21 virtual ~Unit() { }
@@ -14,9 +23,78 @@ public:
14 // 23 //
15 // Tests go here 24 // Tests go here
16 // 25 //
17 void test() 26 void writeFull()
27 {
28 Bu::SFile sf("testfile1", "wb");
29 for( int c = 0; c < 256; c++ )
30 {
31 unsigned char ch = (unsigned char)c;
32 sf.write( &ch, 1 );
33 unitTest( sf.tell() == c+1 );
34 }
35 //unitTest( sf.canRead() == false );
36 //unitTest( sf.canWrite() == true );
37 //unitTest( sf.canSeek() == true );
38 sf.close();
39 struct stat sdat;
40 stat("testfile1", &sdat );
41 unitTest( sdat.st_size == 256 );
42 }
43
44 void readBlocks()
45 {
46 Bu::SFile sf("testfile1", "rb");
47 unsigned char buf[50];
48 size_t total = 0;
49 for(;;)
50 {
51 size_t s = sf.read( buf, 50 );
52 for( size_t c = 0; c < s; c++ )
53 {
54 unitTest( buf[c] == (unsigned char)(c+total) );
55 }
56 total += s;
57 if( s < 50 )
58 {
59 unitTest( total == 256 );
60 unitTest( sf.isEOS() == true );
61 break;
62 }
63 }
64 sf.close();
65 }
66
67 void readError1()
68 {
69 try
70 {
71 Bu::SFile sf("doesn'texist", "rb");
72 unitFailed("No exception thrown");
73 }
74 catch( Bu::FileException &e )
75 {
76 return;
77 }
78 }
79
80 void readError2()
18 { 81 {
19 unitTest( 1 == 1 ); 82 Bu::SFile sf("testfile1", "rb");
83 char buf[256];
84 int r = sf.read( buf, 256 );
85 unitTest( r == 256 );
86 // You have to read past the end to set the EOS flag.
87 unitTest( sf.isEOS() == false );
88 try
89 {
90 int r = sf.read( buf, 5 );
91 unitFailed("No exception thrown");
92 }
93 catch( Bu::FileException &e )
94 {
95 sf.close();
96 return;
97 }
20 } 98 }
21}; 99};
22 100
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
56{ \ 56{ \
57 throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ ); \ 57 throw Bu::UnitSuite::Failed( #tst, __FILE__, __LINE__ ); \
58} 58}
59#define unitFailed( msg ) throw Bu::UnitSuite::Failed(msg, __FILE__, __LINE__);
59 60
60#endif 61#endif