From 71191e311d949b1b7bdd74fc36a14306f492c181 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 24 May 2010 06:46:39 +0000 Subject: Myriad unit test. Seems like it's workin' well. --- src/myriad.cpp | 2 +- src/myriadstream.cpp | 24 ++++----- src/tools/mkunit.cpp | 6 ++- src/unit/myriad.unit | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 src/unit/myriad.unit 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() char *pBlock = new char[iBlockSize]; memset( pBlock, 0, iBlockSize ); sStore.write( pBlock, iBlockSize ); - delete pBlock; + delete[] pBlock; return iBlocks++; } 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, iPos( 0 ) { #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: Created, iId=" << pStream->iId << ", iSize=" + sio << "MyriadStream: " << __LINE__ << ": Created, iId=" << pStream->iId << ", iSize=" << pStream->iSize << sio.nl; #endif //pCurBlock = rMyriad.newBlock(); @@ -46,7 +46,7 @@ void Bu::MyriadStream::close() size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes ) { #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: Started, asked to read " << nBytes << "b." + sio << "MyriadStream: read: " << __LINE__ << ": Started, asked to read " << nBytes << "b." << sio.nl; #endif if( nBytes > pStream->iSize-iPos ) @@ -55,13 +55,13 @@ size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes ) return 0; int iLeft = nBytes; #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: Started, going to read " << nBytes << "b." + sio << "MyriadStream: read: " << __LINE__ << ": Started, going to read " << nBytes << "b." << sio.nl; #endif if( pCurBlock == NULL ) { #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: No block loaded, loading initial block." + sio << "MyriadStream: read: " << __LINE__ << ": No block loaded, loading initial block." << sio.nl; #endif pCurBlock = rMyriad.getBlock( @@ -74,7 +74,7 @@ size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes ) if( pCurBlock->iBlockIndex != iCurBlock ) { #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: Loading new block " << iCurBlock << "." + sio << "MyriadStream: read: " << __LINE__ << ": Loading new block " << iCurBlock << "." << sio.nl; #endif rMyriad.releaseBlock( pCurBlock ); @@ -89,7 +89,7 @@ size_t Bu::MyriadStream::read( void *pBuf, size_t nBytes ) pStream->iSize-iPos ); #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: read: Copying out bytes: " + sio << "MyriadStream: read: " << __LINE__ << ": Copying out bytes: " << iPos << "(" << (iPos%rMyriad.iBlockSize) << ")+" << iAmnt << ", " << iLeft << "b left." << sio.nl; @@ -112,7 +112,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes ) return 0; #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: Started, asked to write " << nBytes << "b." + sio << "MyriadStream: write: " << __LINE__ << ": Started, asked to write " << nBytes << "b." << sio.nl; #endif if( nBytes <= 0 ) @@ -143,7 +143,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes ) pStream->aBlocks.append( iCurBlock ); rMyriad.bsBlockUsed.setBit( iCurBlock ); #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: New block allocated and appended: " + sio << "MyriadStream: write: " << __LINE__ << ": New block allocated and appended: " << iCurBlock << "." << sio.nl; #endif @@ -151,7 +151,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes ) if( !pCurBlock || pCurBlock->iBlockIndex != iCurBlock ) { #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write: Loading new block " << iCurBlock << "." + sio << "MyriadStream: write: " << __LINE__ << ": Loading new block " << iCurBlock << "." << sio.nl; #endif rMyriad.releaseBlock( pCurBlock ); @@ -172,7 +172,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes ) pStream->iSize-iPos ); #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write (ovr): Copying in bytes: " + sio << "MyriadStream: write (ovr): " << __LINE__ << ": Copying in bytes: " << (iPos%rMyriad.iBlockSize) << "+" << iAmnt << ", " << iLeft << "b left." << sio.nl; @@ -193,7 +193,7 @@ size_t Bu::MyriadStream::write( const void *pBuf, size_t nBytes ) iLeft ); #ifdef MYRIAD_STREAM_DEBUG - sio << "MyriadStream: write (app): Copying in bytes: " + sio << "MyriadStream: write (app): " << __LINE__ << ": Copying in bytes: " << (iPos%rMyriad.iBlockSize) << "+" << iAmnt << ", " << iLeft << "b left." << sio.nl; @@ -230,7 +230,7 @@ void Bu::MyriadStream::setPos( long pos ) void Bu::MyriadStream::setPosEnd( long pos ) { - iPos = pStream->iSize-pos-1; + iPos = pStream->iSize-pos; } bool 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 { public: Parser( const Bu::FString &sFile ) : + sIn( sFile ), fIn( sFile, File::Read ), bIn( fIn ), cBuf( 0 ), @@ -475,7 +476,7 @@ public: f << "\tvoid " << t.sName << "()" << f.nl << "#line " << iL - << " \"" << sOut << "\"" << f.nl + << " \"" << sIn << "\"" << f.nl << v << f.nl; } break; @@ -496,7 +497,7 @@ public: case tokBlock: fOut.write( sWs ); - f << f.nl << "#line " << iL << " \"" << sOut + f << f.nl << "#line " << iL << " \"" << sIn << "\"" << f.nl; fOut.write( v.get() ); @@ -520,6 +521,7 @@ public: } private: + Bu::FString sIn; File fIn; Buffer bIn; 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 @@ +// vim: syntax=cpp +/* + * Copyright (C) 2007-2010 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#include "bu/fstring.h" +#include "bu/file.h" +#include "bu/myriad.h" +#include "bu/myriadstream.h" +#include "bu/array.h" + +#include "bu/sio.h" + +#include + +using namespace Bu; + +suite Myriad +{ + void addBlock( Stream &s ) + { + s.setPosEnd( 0 ); + int iSize = (random()%1016)+8; + s.write( &iSize, 4 ); + char *buf = new char[iSize-8]; + for( int j = 0; j < iSize-8; j++ ) + { + buf[j] = (j+iSize)%256; + } + s.write( buf, iSize-8 ); + delete[] buf; + iSize = ~iSize; + s.write( &iSize, 4 ); + } + + void verifyBlock( Stream &s ) + { + int iSize, iInv; + if( s.read( &iSize, 4 ) == 0 ) + return; + if( iSize < 8 || iSize > 1024 ) + throw ExceptionBase("Read bad data, %d", iSize ); + char *buf = new char[iSize-8]; + if( s.read( buf, iSize-8 ) < (size_t)iSize-8 ) + { + delete[] buf; + throw ExceptionBase("Block failed verify (insuffient block data)."); + } + for( int j = 0; j < iSize-8; j++ ) + { + if( buf[j] != (char)((j+iSize)%256) ) + { + char b = buf[j]; + delete[] buf; + throw ExceptionBase("Block failed computed data verify (%02X==%02X).", b, (char)((j+iSize)%256) ); + } + } + delete[] buf; + if( s.read( &iInv, 4 ) < 4 ) + throw ExceptionBase("Block failed verify (insufficient data)."); + if( iInv != ~iSize ) + throw ExceptionBase("Block failed inversion verify."); + } + + void verifyStream( Stream &s ) + { + s.setPos( 0 ); + while( !s.isEos() ) + verifyBlock( s ); + } + + test grow1 + { + FString sFileName("myriad-XXXXXXX"); + + File::tempFile( sFileName ); + File fMyriad( sFileName, File::WriteNew|File::Read ); + Myriad m( fMyriad ); + m.initialize( 64 ); + + Array aStreams; + for( int j = 0; j < 5; j++ ) + { + aStreams.append( m.createStream() ); + } + + srandom( 512 ); + + for( int j = 0; j < 2500; j++ ) + { + + switch( random()%5 ) + { + case 0: + aStreams.append( m.createStream() ); + break; + + case 1: + if( aStreams.getSize() > 0 ) + { + int iStream = random()%aStreams.getSize(); + { + MyriadStream ms = m.openStream( aStreams[iStream] ); + verifyStream( ms ); + } + m.deleteStream( aStreams[iStream] ); + Array::iterator i = aStreams.begin(); + for( int k = 0; k < iStream; k++ ) + i++; + aStreams.erase( i ); + } + break; + + default: + if( aStreams.getSize() == 0 ) + { + aStreams.append( m.createStream() ); + } + { + int iStream = random()%aStreams.getSize(); + MyriadStream ms = m.openStream( aStreams[iStream] ); + addBlock( ms ); + verifyStream( ms ); + } + break; + } + } + + for( Array::iterator i = aStreams.begin(); i; i++ ) + { + MyriadStream ms = m.openStream( *i ); + verifyStream( ms ); + } + } +} + -- cgit v1.2.3