aboutsummaryrefslogtreecommitdiff
path: root/src/unit/file.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-07-03 00:28:59 +0000
committerMike Buland <eichlan@xagasoft.com>2007-07-03 00:28:59 +0000
commitac517a2b7625e0aa0862679e961c6349f859ea3b (patch)
treee3e27a6b9bd5e2be6150088495c91fc91786ad9d /src/unit/file.cpp
parentf8d4301e9fa4f3709258505941e37fab2eadadc6 (diff)
parentbd865cee5f89116c1f054cd0e5c275e97c2d0a9b (diff)
downloadlibbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.gz
libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.bz2
libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.tar.xz
libbu++-ac517a2b7625e0aa0862679e961c6349f859ea3b.zip
The reorg is being put in trunk, I think it's ready. Now we just get to find
out how many applications won't work anymore :)
Diffstat (limited to 'src/unit/file.cpp')
-rw-r--r--src/unit/file.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/unit/file.cpp b/src/unit/file.cpp
new file mode 100644
index 0000000..1eaaf36
--- /dev/null
+++ b/src/unit/file.cpp
@@ -0,0 +1,111 @@
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
9class Unit : public Bu::UnitSuite
10{
11public:
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 if( sf.read( buf, 5 ) > 0 )
91 {
92 unitFailed("Non-zero read result");
93 }
94 else
95 {
96 sf.close();
97 }
98 }
99 catch( Bu::FileException &e )
100 {
101 sf.close();
102 return;
103 }
104 }
105};
106
107int main( int argc, char *argv[] )
108{
109 return Unit().run( argc, argv );
110}
111