diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2007-04-10 17:23:35 +0000 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2007-04-10 17:23:35 +0000 |
| commit | 903e7a1e3d4fe99e9de7f4adc1e401ba871caec9 (patch) | |
| tree | b907c597cf228d4fd58a771f1b65d559c2c01c89 /src/unit | |
| parent | b6e100b94b12f3f92ec025dc2363eaf7c0ee6662 (diff) | |
| download | libbu++-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/unit/sfile.cpp | 84 | ||||
| -rw-r--r-- | src/unitsuite.h | 1 |
2 files changed, 82 insertions, 3 deletions
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 | ||
| 3 | class Unit : public Bu::UnitSuite | 9 | class 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 |
