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