summaryrefslogtreecommitdiff
path: root/src/unit/file.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/unit/file.cpp117
1 files changed, 0 insertions, 117 deletions
diff --git a/src/unit/file.cpp b/src/unit/file.cpp
deleted file mode 100644
index cc19fac..0000000
--- a/src/unit/file.cpp
+++ /dev/null
@@ -1,117 +0,0 @@
1/*
2 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/unitsuite.h"
9#include "bu/file.h"
10
11#include <sys/types.h>
12#include <sys/stat.h>
13#include <unistd.h>
14
15class Unit : public Bu::UnitSuite
16{
17public:
18 Unit()
19 {
20 setName("File");
21 addTest( Unit::writeFull );
22 addTest( Unit::readBlocks );
23 addTest( Unit::readError1 );
24 addTest( Unit::readError2 );
25 }
26
27 virtual ~Unit() { }
28
29 //
30 // Tests go here
31 //
32 void writeFull()
33 {
34 Bu::File sf("testfile1", Bu::File::Write );
35 for( int c = 0; c < 256; c++ )
36 {
37 unsigned char ch = (unsigned char)c;
38 sf.write( &ch, 1 );
39 unitTest( sf.tell() == c+1 );
40 }
41 //unitTest( sf.canRead() == false );
42 //unitTest( sf.canWrite() == true );
43 //unitTest( sf.canSeek() == true );
44 sf.close();
45 struct stat sdat;
46 stat("testfile1", &sdat );
47 unitTest( sdat.st_size == 256 );
48 }
49
50 void readBlocks()
51 {
52 Bu::File sf("testfile1", Bu::File::Read );
53 unsigned char buf[50];
54 size_t total = 0;
55 for(;;)
56 {
57 size_t s = sf.read( buf, 50 );
58 for( size_t c = 0; c < s; c++ )
59 {
60 unitTest( buf[c] == (unsigned char)(c+total) );
61 }
62 total += s;
63 if( s < 50 )
64 {
65 unitTest( total == 256 );
66 unitTest( sf.isEOS() == true );
67 break;
68 }
69 }
70 sf.close();
71 }
72
73 void readError1()
74 {
75 try
76 {
77 Bu::File sf("doesn'texist", Bu::File::Read );
78 unitFailed("No exception thrown");
79 }
80 catch( Bu::FileException &e )
81 {
82 return;
83 }
84 }
85
86 void readError2()
87 {
88 Bu::File sf("testfile1", Bu::File::Read );
89 char buf[256];
90 int r = sf.read( buf, 256 );
91 unitTest( r == 256 );
92 // You have to read past the end to set the EOS flag.
93 unitTest( sf.isEOS() == false );
94 try
95 {
96 if( sf.read( buf, 5 ) > 0 )
97 {
98 unitFailed("Non-zero read result");
99 }
100 else
101 {
102 sf.close();
103 }
104 }
105 catch( Bu::FileException &e )
106 {
107 sf.close();
108 return;
109 }
110 }
111};
112
113int main( int argc, char *argv[] )
114{
115 return Unit().run( argc, argv );
116}
117