aboutsummaryrefslogtreecommitdiff
path: root/src/unit/file.unit
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-01-07 15:59:57 +0000
committerMike Buland <eichlan@xagasoft.com>2009-01-07 15:59:57 +0000
commit45e065bc4fc93731ea9a0543462bc7cf9e6084d7 (patch)
treea9e8279fe00b9b01dc2393f59dc7f41b5e416b2a /src/unit/file.unit
parentd96fe229e79f9b1947cbd24ff52d6bf7bb9bf80d (diff)
downloadlibbu++-45e065bc4fc93731ea9a0543462bc7cf9e6084d7.tar.gz
libbu++-45e065bc4fc93731ea9a0543462bc7cf9e6084d7.tar.bz2
libbu++-45e065bc4fc93731ea9a0543462bc7cf9e6084d7.tar.xz
libbu++-45e065bc4fc93731ea9a0543462bc7cf9e6084d7.zip
Only two real changes. First, Bu::FString and Bu::FBasicString are in different
files. This won't affect any programs at all anywhere. This will just make it easier to maintain and extend later. You still want to include "bu/fstring.h" and use Bu::FString in code. The other is kinda fun. I created a special format for unit tests, they use the extension .unit now and use the mkunit.sh script to convert them to c++ code. There are some nice features here too, maintaining unit tests is much, much easier, and we can have more features without making the code any harder to use. Also, it will be easier to have the unit tests generate reports and be run from a master program and the like.
Diffstat (limited to 'src/unit/file.unit')
-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}