diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-04-10 20:16:19 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-04-10 20:16:19 +0000 |
commit | 070374dde0f53bff26078550997f7682e84412e5 (patch) | |
tree | d9088b02e9b5f08f25d7f07f3a26035eea00e8e9 /src/unit/file.cpp | |
parent | e7ab7cb1604c04763bbdcece5885e6ce5aa100b4 (diff) | |
download | libbu++-070374dde0f53bff26078550997f7682e84412e5.tar.gz libbu++-070374dde0f53bff26078550997f7682e84412e5.tar.bz2 libbu++-070374dde0f53bff26078550997f7682e84412e5.tar.xz libbu++-070374dde0f53bff26078550997f7682e84412e5.zip |
I did it, the streams don't start with an S now.
Diffstat (limited to 'src/unit/file.cpp')
-rw-r--r-- | src/unit/file.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
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 @@ | |||
1 | #include "unitsuite.h" | ||
2 | #include "file.h" | ||
3 | #include "exceptions.h" | ||
4 | |||
5 | #include <sys/types.h> | ||
6 | #include <sys/stat.h> | ||
7 | #include <unistd.h> | ||
8 | |||
9 | class Unit : public Bu::UnitSuite | ||
10 | { | ||
11 | public: | ||
12 | Unit() | ||
13 | { | ||
14 | setName("File"); | ||
15 | addTest( Unit::writeFull ); | ||
16 | addTest( Unit::readBlocks ); | ||
17 | addTest( Unit::readError1 ); | ||
18 | addTest( Unit::readError2 ); | ||
19 | } | ||
20 | |||
21 | virtual ~Unit() { } | ||
22 | |||
23 | // | ||
24 | // Tests go here | ||
25 | // | ||
26 | void writeFull() | ||
27 | { | ||
28 | Bu::File 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::File 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::File sf("doesn'texist", "rb"); | ||
72 | unitFailed("No exception thrown"); | ||
73 | } | ||
74 | catch( Bu::FileException &e ) | ||
75 | { | ||
76 | return; | ||
77 | } | ||
78 | } | ||
79 | |||
80 | void readError2() | ||
81 | { | ||
82 | Bu::File 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 | sf.read( buf, 5 ); | ||
91 | unitFailed("No exception thrown"); | ||
92 | } | ||
93 | catch( Bu::FileException &e ) | ||
94 | { | ||
95 | sf.close(); | ||
96 | return; | ||
97 | } | ||
98 | } | ||
99 | }; | ||
100 | |||
101 | int main( int argc, char *argv[] ) | ||
102 | { | ||
103 | return Unit().run( argc, argv ); | ||
104 | } | ||
105 | |||