aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-06-27 15:42:50 +0000
committerMike Buland <eichlan@xagasoft.com>2007-06-27 15:42:50 +0000
commit5ec9a131e12d021c42b46b601f5e79502485bebb (patch)
tree31d63d293a6c5bc6ea1d677474380ea48976add4
parent01ecf54b07e75c17ca5f7039084daeefaea9a1c7 (diff)
downloadlibbu++-5ec9a131e12d021c42b46b601f5e79502485bebb.tar.gz
libbu++-5ec9a131e12d021c42b46b601f5e79502485bebb.tar.bz2
libbu++-5ec9a131e12d021c42b46b601f5e79502485bebb.tar.xz
libbu++-5ec9a131e12d021c42b46b601f5e79502485bebb.zip
The MemBuf works just fine, although it still can't over-write data in the
buffer.
-rw-r--r--build.conf9
-rw-r--r--src/membuf.cpp30
-rw-r--r--src/membuf.h7
-rw-r--r--src/unit/entities/unit30
-rw-r--r--src/unit/file.cpp10
-rw-r--r--src/unit/hash.cpp4
-rw-r--r--src/unit/membuf.cpp37
7 files changed, 113 insertions, 14 deletions
diff --git a/build.conf b/build.conf
index f493d67..d994977 100644
--- a/build.conf
+++ b/build.conf
@@ -41,15 +41,6 @@ filesIn("src/tests") filter regexp("^src/tests/(.*)\\.cpp$", "tests/{re:1}"):
41 41
42["tests/itoqueue1", "tests/itoqueue2"]: set "LDFLAGS" += "-lpthread" 42["tests/itoqueue1", "tests/itoqueue2"]: set "LDFLAGS" += "-lpthread"
43 43
44directoriesIn("src/unit","unit/"):
45 rule "exe",
46 target file,
47 group "tests",
48 requires "libbu++.a",
49 set "CXXFLAGS" += "-Isrc",
50 set "LDFLAGS" += "-L. -lbu++",
51 input filesIn("{fulldir}") filter regexp("^.*\\.cpp$")
52
53filesIn("src/unit") filter regexp("^src/unit/(.*)\\.cpp$", "unit/{re:1}"): 44filesIn("src/unit") filter regexp("^src/unit/(.*)\\.cpp$", "unit/{re:1}"):
54 rule "exe", 45 rule "exe",
55 target file, 46 target file,
diff --git a/src/membuf.cpp b/src/membuf.cpp
index fdb4366..3c394b0 100644
--- a/src/membuf.cpp
+++ b/src/membuf.cpp
@@ -2,7 +2,8 @@
2 2
3using namespace Bu; 3using namespace Bu;
4 4
5Bu::MemBuf::MemBuf() 5Bu::MemBuf::MemBuf() :
6 nPos( 0 )
6{ 7{
7} 8}
8 9
@@ -16,35 +17,56 @@ void Bu::MemBuf::close()
16 17
17size_t Bu::MemBuf::read( void *pBuf, size_t nBytes ) 18size_t Bu::MemBuf::read( void *pBuf, size_t nBytes )
18{ 19{
20 if( (size_t)sBuf.getSize()-(size_t)nPos < nBytes )
21 nBytes = sBuf.getSize()-nPos;
19 22
23 memcpy( pBuf, sBuf.getStr()+nPos, nBytes );
24 nPos += nBytes;
25
26 return nBytes;
20} 27}
21 28
22size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes ) 29size_t Bu::MemBuf::write( const void *pBuf, size_t nBytes )
23{ 30{
31 sBuf.append( (const char *)pBuf, nBytes );
32 nPos += nBytes;
33 return nBytes;
24} 34}
25 35
26long Bu::MemBuf::tell() 36long Bu::MemBuf::tell()
27{ 37{
38 return nPos;
28} 39}
29 40
30void Bu::MemBuf::seek( long offset ) 41void Bu::MemBuf::seek( long offset )
31{ 42{
43 nPos += offset;
44 if( nPos < 0 ) nPos = 0;
45 else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize();
32} 46}
33 47
34void Bu::MemBuf::setPos( long pos ) 48void Bu::MemBuf::setPos( long pos )
35{ 49{
50 nPos = pos;
51 if( nPos < 0 ) nPos = 0;
52 else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize();
36} 53}
37 54
38void Bu::MemBuf::setPosEnd( long pos ) 55void Bu::MemBuf::setPosEnd( long pos )
39{ 56{
57 nPos = sBuf.getSize()-pos;
58 if( nPos < 0 ) nPos = 0;
59 else if( nPos > sBuf.getSize() ) nPos = sBuf.getSize();
40} 60}
41 61
42bool Bu::MemBuf::isEOS() 62bool Bu::MemBuf::isEOS()
43{ 63{
64 return (nPos == sBuf.getSize());
44} 65}
45 66
46bool Bu::MemBuf::isOpen() 67bool Bu::MemBuf::isOpen()
47{ 68{
69 return true;
48} 70}
49 71
50void Bu::MemBuf::flush() 72void Bu::MemBuf::flush()
@@ -53,26 +75,32 @@ void Bu::MemBuf::flush()
53 75
54bool Bu::MemBuf::canRead() 76bool Bu::MemBuf::canRead()
55{ 77{
78 return !isEOS();
56} 79}
57 80
58bool Bu::MemBuf::canWrite() 81bool Bu::MemBuf::canWrite()
59{ 82{
83 return isEOS();
60} 84}
61 85
62bool Bu::MemBuf::isReadable() 86bool Bu::MemBuf::isReadable()
63{ 87{
88 return true;
64} 89}
65 90
66bool Bu::MemBuf::isWritable() 91bool Bu::MemBuf::isWritable()
67{ 92{
93 return true;
68} 94}
69 95
70bool Bu::MemBuf::isSeekable() 96bool Bu::MemBuf::isSeekable()
71{ 97{
98 return true;
72} 99}
73 100
74bool Bu::MemBuf::isBlocking() 101bool Bu::MemBuf::isBlocking()
75{ 102{
103 return true;
76} 104}
77 105
78void Bu::MemBuf::setBlocking( bool bBlocking ) 106void Bu::MemBuf::setBlocking( bool bBlocking )
diff --git a/src/membuf.h b/src/membuf.h
index 2cbbbdc..877b35e 100644
--- a/src/membuf.h
+++ b/src/membuf.h
@@ -4,6 +4,7 @@
4#include <stdint.h> 4#include <stdint.h>
5 5
6#include "bu/stream.h" 6#include "bu/stream.h"
7#include "bu/fstring.h"
7 8
8namespace Bu 9namespace Bu
9{ 10{
@@ -18,6 +19,12 @@ namespace Bu
18 19
19 virtual void close(); 20 virtual void close();
20 virtual size_t read( void *pBuf, size_t nBytes ); 21 virtual size_t read( void *pBuf, size_t nBytes );
22
23 /**
24 *@todo Allow writes at the current position, not just appending to
25 * the current buffer. This is a silly way to do it, but it covers all
26 * of our bases for now.
27 */
21 virtual size_t write( const void *pBuf, size_t nBytes ); 28 virtual size_t write( const void *pBuf, size_t nBytes );
22 virtual long tell(); 29 virtual long tell();
23 virtual void seek( long offset ); 30 virtual void seek( long offset );
diff --git a/src/unit/entities/unit b/src/unit/entities/unit
new file mode 100644
index 0000000..28db45f
--- /dev/null
+++ b/src/unit/entities/unit
@@ -0,0 +1,30 @@
1<?xml version="1.1" ?>
2<entity desc="Unit test framework">
3 <param name="name" required="yes" desc="Name of the class"/>
4 <file
5 name="source"
6 filename="{=name:%tolower}.cpp"
7>#include "bu/unitsuite.h"
8
9class Unit : public Bu::UnitSuite
10{
11public:
12 Unit()
13 {
14 setName("{=name}");
15 addTest( Unit::test01 );
16 }
17
18 virtual ~Unit()
19 {
20 }
21
22 void test01()
23 {
24 unitTest( 0 == 5 );
25 }
26};
27
28int main( int argc, char *argv[] ){ return Unit().run( argc, argv ); }
29</file>
30</entity>
diff --git a/src/unit/file.cpp b/src/unit/file.cpp
index a45b8d7..1eaaf36 100644
--- a/src/unit/file.cpp
+++ b/src/unit/file.cpp
@@ -87,8 +87,14 @@ public:
87 unitTest( sf.isEOS() == false ); 87 unitTest( sf.isEOS() == false );
88 try 88 try
89 { 89 {
90 sf.read( buf, 5 ); 90 if( sf.read( buf, 5 ) > 0 )
91 unitFailed("No exception thrown"); 91 {
92 unitFailed("Non-zero read result");
93 }
94 else
95 {
96 sf.close();
97 }
92 } 98 }
93 catch( Bu::FileException &e ) 99 catch( Bu::FileException &e )
94 { 100 {
diff --git a/src/unit/hash.cpp b/src/unit/hash.cpp
index 588e687..9ea933f 100644
--- a/src/unit/hash.cpp
+++ b/src/unit/hash.cpp
@@ -1,6 +1,6 @@
1#include "bu/fstring.h" 1#include "bu/fstring.h"
2#include "bu/hash.h" 2#include "bu/hash.h"
3#include "unitsuite.h" 3#include "bu/unitsuite.h"
4 4
5#include <stdio.h> 5#include <stdio.h>
6 6
@@ -23,7 +23,7 @@ public:
23 { 23 {
24 StrIntHash h; 24 StrIntHash h;
25 char buf[20]; 25 char buf[20];
26 for(int i=0;i<10000;i++) 26 for(int i=1;i<10000;i++)
27 { 27 {
28 sprintf(buf,"%d",i); 28 sprintf(buf,"%d",i);
29 Bu::FString sTmp(buf); 29 Bu::FString sTmp(buf);
diff --git a/src/unit/membuf.cpp b/src/unit/membuf.cpp
new file mode 100644
index 0000000..65ba82a
--- /dev/null
+++ b/src/unit/membuf.cpp
@@ -0,0 +1,37 @@
1#include "bu/unitsuite.h"
2#include "bu/membuf.h"
3
4class Unit : public Bu::UnitSuite
5{
6public:
7 Unit()
8 {
9 setName("MemBuf");
10 addTest( Unit::testWriteRead01 );
11 }
12
13 virtual ~Unit()
14 {
15 }
16
17 void testWriteRead01()
18 {
19 Bu::MemBuf mb;
20 unitTest( mb.write("ab", 2 ) == 2 );
21 unitTest( mb.write("cde", 3 ) == 3 );
22 unitTest( mb.write("FG", 2 ) == 2 );
23
24 mb.setPos( 0 );
25
26 char buf[8];
27 buf[7] = '\0';
28 unitTest( mb.read( buf, 7 ) == 7 );
29 unitTest( !strncmp( buf, "abcdeFG", 7 ) );
30 unitTest( mb.read( buf, 7 ) == 0 );
31 mb.seek( -3 );
32 unitTest( mb.read( buf, 7 ) == 3 );
33 unitTest( !strncmp( buf, "eFG", 3 ) );
34 }
35};
36
37int main( int argc, char *argv[] ){ return Unit().run( argc, argv ); }