aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/myriad.cpp2
-rw-r--r--src/myriadstream.cpp24
-rw-r--r--src/tools/mkunit.cpp6
-rw-r--r--src/unit/myriad.unit139
4 files changed, 156 insertions, 15 deletions
diff --git a/src/myriad.cpp b/src/myriad.cpp
index a95e885..4476d53 100644
--- a/src/myriad.cpp
+++ b/src/myriad.cpp
@@ -347,7 +347,7 @@ int Bu::Myriad::findEmptyBlock()
347 char *pBlock = new char[iBlockSize]; 347 char *pBlock = new char[iBlockSize];
348 memset( pBlock, 0, iBlockSize ); 348 memset( pBlock, 0, iBlockSize );
349 sStore.write( pBlock, iBlockSize ); 349 sStore.write( pBlock, iBlockSize );
350 delete pBlock; 350 delete[] pBlock;
351 351
352 return iBlocks++; 352 return iBlocks++;
353} 353}
diff --git a/src/myriadstream.cpp b/src/myriadstream.cpp
index d714c23..2218ae4 100644
--- a/src/myriadstream.cpp
+++ b/src/myriadstream.cpp
@@ -23,7 +23,7 @@ Bu::MyriadStream::MyriadStream( Bu::Myriad &rMyriad,
23 iPos( 0 ) 23 iPos( 0 )
24{ 24{
25#ifdef MYRIAD_STREAM_DEBUG 25#ifdef MYRIAD_STREAM_DEBUG
26 sio << "MyriadStream: Created, iId=" << pStream->iId << ", iSize=" 26 sio << "MyriadStream: " << __LINE__ << ": Created, iId=" << pStream->iId << ", iSize="
27 << pStream->iSize << sio.nl; 27 << pStream->iSize << sio.nl;
28#endif 28#endif
29 //pCurBlock = rMyriad.newBlock(); 29 //pCurBlock = rMyriad.newBlock();
@@ -46,7 +46,7 @@ void Bu::MyriadStream::close()
46size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes ) 46size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes )
47{ 47{
48#ifdef MYRIAD_STREAM_DEBUG 48#ifdef MYRIAD_STREAM_DEBUG
49 sio << "MyriadStream: read: Started, asked to read " << nBytes << "b." 49 sio << "MyriadStream: read: " << __LINE__ << ": Started, asked to read " << nBytes << "b."
50 << sio.nl; 50 << sio.nl;
51#endif 51#endif
52 if( nBytes > pStream->iSize-iPos ) 52 if( nBytes > pStream->iSize-iPos )
@@ -55,13 +55,13 @@ size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes )
55 return 0; 55 return 0;
56 int iLeft = nBytes; 56 int iLeft = nBytes;
57#ifdef MYRIAD_STREAM_DEBUG 57#ifdef MYRIAD_STREAM_DEBUG
58 sio << "MyriadStream: read: Started, going to read " << nBytes << "b." 58 sio << "MyriadStream: read: " << __LINE__ << ": Started, going to read " << nBytes << "b."
59 << sio.nl; 59 << sio.nl;
60#endif 60#endif
61 if( pCurBlock == NULL ) 61 if( pCurBlock == NULL )
62 { 62 {
63#ifdef MYRIAD_STREAM_DEBUG 63#ifdef MYRIAD_STREAM_DEBUG
64 sio << "MyriadStream: read: No block loaded, loading initial block." 64 sio << "MyriadStream: read: " << __LINE__ << ": No block loaded, loading initial block."
65 << sio.nl; 65 << sio.nl;
66#endif 66#endif
67 pCurBlock = rMyriad.getBlock( 67 pCurBlock = rMyriad.getBlock(
@@ -74,7 +74,7 @@ size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes )
74 if( pCurBlock->iBlockIndex != iCurBlock ) 74 if( pCurBlock->iBlockIndex != iCurBlock )
75 { 75 {
76#ifdef MYRIAD_STREAM_DEBUG 76#ifdef MYRIAD_STREAM_DEBUG
77 sio << "MyriadStream: read: Loading new block " << iCurBlock << "." 77 sio << "MyriadStream: read: " << __LINE__ << ": Loading new block " << iCurBlock << "."
78 << sio.nl; 78 << sio.nl;
79#endif 79#endif
80 rMyriad.releaseBlock( pCurBlock ); 80 rMyriad.releaseBlock( pCurBlock );
@@ -89,7 +89,7 @@ size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes )
89 pStream->iSize-iPos 89 pStream->iSize-iPos
90 ); 90 );
91#ifdef MYRIAD_STREAM_DEBUG 91#ifdef MYRIAD_STREAM_DEBUG
92 sio << "MyriadStream: read: Copying out bytes: " 92 sio << "MyriadStream: read: " << __LINE__ << ": Copying out bytes: "
93 << iPos << "(" << (iPos%rMyriad.iBlockSize) << ")+" 93 << iPos << "(" << (iPos%rMyriad.iBlockSize) << ")+"
94 << iAmnt 94 << iAmnt
95 << ", " << iLeft << "b left." << sio.nl; 95 << ", " << iLeft << "b left." << sio.nl;
@@ -112,7 +112,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes )
112 return 0; 112 return 0;
113 113
114#ifdef MYRIAD_STREAM_DEBUG 114#ifdef MYRIAD_STREAM_DEBUG
115 sio << "MyriadStream: write: Started, asked to write " << nBytes << "b." 115 sio << "MyriadStream: write: " << __LINE__ << ": Started, asked to write " << nBytes << "b."
116 << sio.nl; 116 << sio.nl;
117#endif 117#endif
118 if( nBytes <= 0 ) 118 if( nBytes <= 0 )
@@ -143,7 +143,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes )
143 pStream->aBlocks.append( iCurBlock ); 143 pStream->aBlocks.append( iCurBlock );
144 rMyriad.bsBlockUsed.setBit( iCurBlock ); 144 rMyriad.bsBlockUsed.setBit( iCurBlock );
145#ifdef MYRIAD_STREAM_DEBUG 145#ifdef MYRIAD_STREAM_DEBUG
146 sio << "MyriadStream: write: New block allocated and appended: " 146 sio << "MyriadStream: write: " << __LINE__ << ": New block allocated and appended: "
147 << iCurBlock << "." << sio.nl; 147 << iCurBlock << "." << sio.nl;
148 148
149#endif 149#endif
@@ -151,7 +151,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes )
151 if( !pCurBlock || pCurBlock->iBlockIndex != iCurBlock ) 151 if( !pCurBlock || pCurBlock->iBlockIndex != iCurBlock )
152 { 152 {
153#ifdef MYRIAD_STREAM_DEBUG 153#ifdef MYRIAD_STREAM_DEBUG
154 sio << "MyriadStream: write: Loading new block " << iCurBlock << "." 154 sio << "MyriadStream: write: " << __LINE__ << ": Loading new block " << iCurBlock << "."
155 << sio.nl; 155 << sio.nl;
156#endif 156#endif
157 rMyriad.releaseBlock( pCurBlock ); 157 rMyriad.releaseBlock( pCurBlock );
@@ -172,7 +172,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes )
172 pStream->iSize-iPos 172 pStream->iSize-iPos
173 ); 173 );
174#ifdef MYRIAD_STREAM_DEBUG 174#ifdef MYRIAD_STREAM_DEBUG
175 sio << "MyriadStream: write (ovr): Copying in bytes: " 175 sio << "MyriadStream: write (ovr): " << __LINE__ << ": Copying in bytes: "
176 << (iPos%rMyriad.iBlockSize) << "+" 176 << (iPos%rMyriad.iBlockSize) << "+"
177 << iAmnt 177 << iAmnt
178 << ", " << iLeft << "b left." << sio.nl; 178 << ", " << iLeft << "b left." << sio.nl;
@@ -193,7 +193,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes )
193 iLeft 193 iLeft
194 ); 194 );
195#ifdef MYRIAD_STREAM_DEBUG 195#ifdef MYRIAD_STREAM_DEBUG
196 sio << "MyriadStream: write (app): Copying in bytes: " 196 sio << "MyriadStream: write (app): " << __LINE__ << ": Copying in bytes: "
197 << (iPos%rMyriad.iBlockSize) << "+" 197 << (iPos%rMyriad.iBlockSize) << "+"
198 << iAmnt 198 << iAmnt
199 << ", " << iLeft << "b left." << sio.nl; 199 << ", " << iLeft << "b left." << sio.nl;
@@ -230,7 +230,7 @@ void Bu::MyriadStream::setPos( long pos )
230 230
231void Bu::MyriadStream::setPosEnd( long pos ) 231void Bu::MyriadStream::setPosEnd( long pos )
232{ 232{
233 iPos = pStream->iSize-pos-1; 233 iPos = pStream->iSize-pos;
234} 234}
235 235
236bool Bu::MyriadStream::isEos() 236bool Bu::MyriadStream::isEos()
diff --git a/src/tools/mkunit.cpp b/src/tools/mkunit.cpp
index 43fab38..0454013 100644
--- a/src/tools/mkunit.cpp
+++ b/src/tools/mkunit.cpp
@@ -67,6 +67,7 @@ class Parser
67{ 67{
68public: 68public:
69 Parser( const Bu::FString &sFile ) : 69 Parser( const Bu::FString &sFile ) :
70 sIn( sFile ),
70 fIn( sFile, File::Read ), 71 fIn( sFile, File::Read ),
71 bIn( fIn ), 72 bIn( fIn ),
72 cBuf( 0 ), 73 cBuf( 0 ),
@@ -475,7 +476,7 @@ public:
475 476
476 f << "\tvoid " << t.sName << "()" 477 f << "\tvoid " << t.sName << "()"
477 << f.nl << "#line " << iL 478 << f.nl << "#line " << iL
478 << " \"" << sOut << "\"" << f.nl 479 << " \"" << sIn << "\"" << f.nl
479 << v << f.nl; 480 << v << f.nl;
480 } 481 }
481 break; 482 break;
@@ -496,7 +497,7 @@ public:
496 497
497 case tokBlock: 498 case tokBlock:
498 fOut.write( sWs ); 499 fOut.write( sWs );
499 f << f.nl << "#line " << iL << " \"" << sOut 500 f << f.nl << "#line " << iL << " \"" << sIn
500 << "\"" << f.nl; 501 << "\"" << f.nl;
501 fOut.write( v.get<Bu::FString>() ); 502 fOut.write( v.get<Bu::FString>() );
502 503
@@ -520,6 +521,7 @@ public:
520 } 521 }
521 522
522private: 523private:
524 Bu::FString sIn;
523 File fIn; 525 File fIn;
524 Buffer bIn; 526 Buffer bIn;
525 char cBuf; 527 char cBuf;
diff --git a/src/unit/myriad.unit b/src/unit/myriad.unit
new file mode 100644
index 0000000..bc05bf7
--- /dev/null
+++ b/src/unit/myriad.unit
@@ -0,0 +1,139 @@
1// vim: syntax=cpp
2/*
3 * Copyright (C) 2007-2010 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/fstring.h"
10#include "bu/file.h"
11#include "bu/myriad.h"
12#include "bu/myriadstream.h"
13#include "bu/array.h"
14
15#include "bu/sio.h"
16
17#include <stdlib.h>
18
19using namespace Bu;
20
21suite Myriad
22{
23 void addBlock( Stream &s )
24 {
25 s.setPosEnd( 0 );
26 int iSize = (random()%1016)+8;
27 s.write( &iSize, 4 );
28 char *buf = new char[iSize-8];
29 for( int j = 0; j < iSize-8; j++ )
30 {
31 buf[j] = (j+iSize)%256;
32 }
33 s.write( buf, iSize-8 );
34 delete[] buf;
35 iSize = ~iSize;
36 s.write( &iSize, 4 );
37 }
38
39 void verifyBlock( Stream &s )
40 {
41 int iSize, iInv;
42 if( s.read( &iSize, 4 ) == 0 )
43 return;
44 if( iSize < 8 || iSize > 1024 )
45 throw ExceptionBase("Read bad data, %d", iSize );
46 char *buf = new char[iSize-8];
47 if( s.read( buf, iSize-8 ) < (size_t)iSize-8 )
48 {
49 delete[] buf;
50 throw ExceptionBase("Block failed verify (insuffient block data).");
51 }
52 for( int j = 0; j < iSize-8; j++ )
53 {
54 if( buf[j] != (char)((j+iSize)%256) )
55 {
56 char b = buf[j];
57 delete[] buf;
58 throw ExceptionBase("Block failed computed data verify (%02X==%02X).", b, (char)((j+iSize)%256) );
59 }
60 }
61 delete[] buf;
62 if( s.read( &iInv, 4 ) < 4 )
63 throw ExceptionBase("Block failed verify (insufficient data).");
64 if( iInv != ~iSize )
65 throw ExceptionBase("Block failed inversion verify.");
66 }
67
68 void verifyStream( Stream &s )
69 {
70 s.setPos( 0 );
71 while( !s.isEos() )
72 verifyBlock( s );
73 }
74
75 test grow1
76 {
77 FString sFileName("myriad-XXXXXXX");
78
79 File::tempFile( sFileName );
80 File fMyriad( sFileName, File::WriteNew|File::Read );
81 Myriad m( fMyriad );
82 m.initialize( 64 );
83
84 Array<int> aStreams;
85 for( int j = 0; j < 5; j++ )
86 {
87 aStreams.append( m.createStream() );
88 }
89
90 srandom( 512 );
91
92 for( int j = 0; j < 2500; j++ )
93 {
94
95 switch( random()%5 )
96 {
97 case 0:
98 aStreams.append( m.createStream() );
99 break;
100
101 case 1:
102 if( aStreams.getSize() > 0 )
103 {
104 int iStream = random()%aStreams.getSize();
105 {
106 MyriadStream ms = m.openStream( aStreams[iStream] );
107 verifyStream( ms );
108 }
109 m.deleteStream( aStreams[iStream] );
110 Array<int>::iterator i = aStreams.begin();
111 for( int k = 0; k < iStream; k++ )
112 i++;
113 aStreams.erase( i );
114 }
115 break;
116
117 default:
118 if( aStreams.getSize() == 0 )
119 {
120 aStreams.append( m.createStream() );
121 }
122 {
123 int iStream = random()%aStreams.getSize();
124 MyriadStream ms = m.openStream( aStreams[iStream] );
125 addBlock( ms );
126 verifyStream( ms );
127 }
128 break;
129 }
130 }
131
132 for( Array<int>::iterator i = aStreams.begin(); i; i++ )
133 {
134 MyriadStream ms = m.openStream( *i );
135 verifyStream( ms );
136 }
137 }
138}
139