diff options
Diffstat (limited to 'src/unit')
-rw-r--r-- | src/unit/sfile.cpp | 84 |
1 files changed, 81 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 | ||