diff options
author | Mike Buland <eichlan@xagasoft.com> | 2010-05-28 19:23:45 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2010-05-28 19:23:45 +0000 |
commit | 674274ed06dfdf9488e7f3bc9e3b45b9f7f4fb18 (patch) | |
tree | df88d2ee7f25614e16ce2df7c07ca191f1745ac8 | |
parent | 5b68b0c6abd98fc99acd57a2c5c691468e7dcb97 (diff) | |
download | libbu++-674274ed06dfdf9488e7f3bc9e3b45b9f7f4fb18.tar.gz libbu++-674274ed06dfdf9488e7f3bc9e3b45b9f7f4fb18.tar.bz2 libbu++-674274ed06dfdf9488e7f3bc9e3b45b9f7f4fb18.tar.xz libbu++-674274ed06dfdf9488e7f3bc9e3b45b9f7f4fb18.zip |
More myriad testing and unit test features, not unit tests that may take a
while are welcome to provide progress info with some builtin functions.
The Bu::Archive class now throws an exception if reading is interrupted by EOS
-rw-r--r-- | src/archive.cpp | 3 | ||||
-rw-r--r-- | src/unit/myriad.unit | 143 | ||||
-rw-r--r-- | src/unitsuite.cpp | 41 | ||||
-rw-r--r-- | src/unitsuite.h | 8 |
4 files changed, 193 insertions, 2 deletions
diff --git a/src/archive.cpp b/src/archive.cpp index d5f77c5..f29895c 100644 --- a/src/archive.cpp +++ b/src/archive.cpp | |||
@@ -33,7 +33,8 @@ void Bu::Archive::read( void *pData, int32_t nSize ) | |||
33 | if( nSize == 0 || pData == NULL ) | 33 | if( nSize == 0 || pData == NULL ) |
34 | return; | 34 | return; |
35 | 35 | ||
36 | rStream.read( (char *)pData, nSize ); | 36 | if( rStream.read( (char *)pData, nSize ) < nSize ) |
37 | throw Bu::ExceptionBase("Insufficient data to unarchive object."); | ||
37 | } | 38 | } |
38 | 39 | ||
39 | void Bu::Archive::close() | 40 | void Bu::Archive::close() |
diff --git a/src/unit/myriad.unit b/src/unit/myriad.unit index a88650f..ad8fb5d 100644 --- a/src/unit/myriad.unit +++ b/src/unit/myriad.unit | |||
@@ -13,11 +13,93 @@ | |||
13 | #include "bu/array.h" | 13 | #include "bu/array.h" |
14 | 14 | ||
15 | #include "bu/sio.h" | 15 | #include "bu/sio.h" |
16 | #include "bu/archive.h" | ||
17 | #include "bu/md5.h" | ||
18 | #include "bu/unitsuite.h" | ||
16 | 19 | ||
17 | #include <stdlib.h> | 20 | #include <stdlib.h> |
18 | 21 | ||
19 | using namespace Bu; | 22 | using namespace Bu; |
20 | 23 | ||
24 | class VerifyObject | ||
25 | { | ||
26 | friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const VerifyObject &vo ); | ||
27 | friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, VerifyObject &vo ); | ||
28 | public: | ||
29 | VerifyObject( int iUnits ) : | ||
30 | iUnits( iUnits ), | ||
31 | iBytesWritten( 0 ) | ||
32 | { | ||
33 | } | ||
34 | |||
35 | virtual ~VerifyObject() | ||
36 | { | ||
37 | } | ||
38 | |||
39 | int getBytesWritten() | ||
40 | { | ||
41 | return iBytesWritten; | ||
42 | } | ||
43 | |||
44 | private: | ||
45 | int iUnits; | ||
46 | mutable int iBytesWritten; | ||
47 | }; | ||
48 | |||
49 | Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const VerifyObject &vo ) | ||
50 | { | ||
51 | Md5 sum; | ||
52 | ar << vo.iUnits; | ||
53 | vo.iBytesWritten = 4; | ||
54 | sum.addData( &vo.iUnits, 4 ); | ||
55 | for( int j = 0; j < vo.iUnits; j++ ) | ||
56 | { | ||
57 | int iRand = random()%128; | ||
58 | // ar << iRand; | ||
59 | Bu::FString sDat( iRand ); | ||
60 | for( int j = 0; j < iRand; j++ ) | ||
61 | sDat[j] = (char)((uint8_t)(random()%256)); | ||
62 | ar << sDat; | ||
63 | sum.addData( &iRand, 4 ); | ||
64 | sum.addData( sDat.getStr(), iRand ); | ||
65 | vo.iBytesWritten += 4 + iRand; | ||
66 | } | ||
67 | Bu::FString sRes = sum.getResult(); | ||
68 | ar << sRes; | ||
69 | vo.iBytesWritten += 4 + sRes.getSize(); | ||
70 | return ar; | ||
71 | } | ||
72 | |||
73 | Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, VerifyObject &vo ) | ||
74 | { | ||
75 | Md5 sum; | ||
76 | ar >> vo.iUnits; | ||
77 | sum.addData( &vo.iUnits, 4 ); | ||
78 | for( int j = 0; j < vo.iUnits; j++ ) | ||
79 | { | ||
80 | int iRand; | ||
81 | // ar >> iRand; | ||
82 | Bu::FString sStr; | ||
83 | ar >> sStr; | ||
84 | iRand = sStr.getSize(); | ||
85 | sum.addData( &iRand, 4 ); | ||
86 | sum.addData( sStr.getStr(), iRand ); | ||
87 | } | ||
88 | Bu::FString sSum; | ||
89 | ar >> sSum; | ||
90 | unitTest( sSum == sum.getResult() ); | ||
91 | int iTooMuch; | ||
92 | try | ||
93 | { | ||
94 | ar >> iTooMuch; | ||
95 | unitFailed("should have thrown an exception."); | ||
96 | } | ||
97 | catch( Bu::ExceptionBase &e ) | ||
98 | { | ||
99 | } | ||
100 | return ar; | ||
101 | } | ||
102 | |||
21 | suite Myriad | 103 | suite Myriad |
22 | { | 104 | { |
23 | test setSize | 105 | test setSize |
@@ -211,13 +293,16 @@ suite Myriad | |||
211 | 293 | ||
212 | Array<int> aStream; | 294 | Array<int> aStream; |
213 | 295 | ||
296 | setStepCount( 5*2500 + 5 ); | ||
297 | |||
214 | { | 298 | { |
215 | File fMyriad = tempFile( sFileName ); | 299 | File fMyriad = tempFile( sFileName ); |
216 | Myriad m( fMyriad, 64 ); | 300 | Myriad m( fMyriad, 128 ); |
217 | 301 | ||
218 | for( int j = 0; j < 5; j++ ) | 302 | for( int j = 0; j < 5; j++ ) |
219 | { | 303 | { |
220 | aStream.append( m.createStream() ); | 304 | aStream.append( m.createStream() ); |
305 | incProgress(); | ||
221 | } | 306 | } |
222 | } | 307 | } |
223 | 308 | ||
@@ -237,6 +322,62 @@ suite Myriad | |||
237 | ms.setPos( 0 ); | 322 | ms.setPos( 0 ); |
238 | verifyBlock( ms ); | 323 | verifyBlock( ms ); |
239 | unitTest( ms.read( &b, 1 ) == 0 ); | 324 | unitTest( ms.read( &b, 1 ) == 0 ); |
325 | incProgress(); | ||
326 | } | ||
327 | } | ||
328 | } | ||
329 | |||
330 | test stressArchive | ||
331 | { | ||
332 | FString sFileName("myriad-XXXXXX"); | ||
333 | Array<int> aStream; | ||
334 | |||
335 | srandom( 2096 ); | ||
336 | |||
337 | setStepCount( 15*250 + 15 ); | ||
338 | |||
339 | { | ||
340 | File fMyriad = tempFile( sFileName ); | ||
341 | Myriad m( fMyriad, 1024 ); | ||
342 | |||
343 | for( int j = 0; j < 15; j++ ) | ||
344 | { | ||
345 | int iStream = m.createStream(); | ||
346 | aStream.append( iStream ); | ||
347 | VerifyObject vo( random()%1024 ); | ||
348 | { | ||
349 | MyriadStream ms = m.openStream( iStream ); | ||
350 | Archive ar( ms, Archive::save ); | ||
351 | ar << vo; | ||
352 | unitTest( ms.tell() == vo.getBytesWritten() ); | ||
353 | ms.setSize( ms.tell() ); | ||
354 | } | ||
355 | unitTest( m.getStreamSize( iStream ) == vo.getBytesWritten() ); | ||
356 | incProgress(); | ||
357 | } | ||
358 | } | ||
359 | |||
360 | for( int iter = 0; iter < 250; iter++ ) | ||
361 | { | ||
362 | File fMyriad( sFileName, File::ReadWrite ); | ||
363 | Myriad m( fMyriad ); | ||
364 | for( Array<int>::iterator i = aStream.begin(); i; i++ ) | ||
365 | { | ||
366 | VerifyObject vo( random()%1024 ); | ||
367 | { | ||
368 | MyriadStream ms = m.openStream( *i ); | ||
369 | Archive ar( ms, Archive::load ); | ||
370 | ar >> vo; | ||
371 | } | ||
372 | { | ||
373 | MyriadStream ms = m.openStream( *i ); | ||
374 | Archive ar( ms, Archive::save ); | ||
375 | ar << vo; | ||
376 | unitTest( ms.tell() == vo.getBytesWritten() ); | ||
377 | ms.setSize( ms.tell() ); | ||
378 | } | ||
379 | unitTest( m.getStreamSize( *i ) == vo.getBytesWritten() ); | ||
380 | incProgress(); | ||
240 | } | 381 | } |
241 | } | 382 | } |
242 | } | 383 | } |
diff --git a/src/unitsuite.cpp b/src/unitsuite.cpp index 0a531c0..7a20128 100644 --- a/src/unitsuite.cpp +++ b/src/unitsuite.cpp | |||
@@ -10,6 +10,7 @@ | |||
10 | #include "bu/sio.h" | 10 | #include "bu/sio.h" |
11 | #include "bu/optparser.h" | 11 | #include "bu/optparser.h" |
12 | #include <stdlib.h> | 12 | #include <stdlib.h> |
13 | #include <time.h> | ||
13 | 14 | ||
14 | using namespace Bu; | 15 | using namespace Bu; |
15 | 16 | ||
@@ -53,6 +54,8 @@ int Bu::UnitSuite::run( int argc, char *argv[] ) | |||
53 | << sio.flush; | 54 | << sio.flush; |
54 | try | 55 | try |
55 | { | 56 | { |
57 | iStepCount = -1; | ||
58 | iProgress = 0; | ||
56 | (this->*(i->fTest))(); | 59 | (this->*(i->fTest))(); |
57 | switch( i->eExpect ) | 60 | switch( i->eExpect ) |
58 | { | 61 | { |
@@ -185,6 +188,44 @@ void Bu::UnitSuite::setName( const FString &sName ) | |||
185 | sSuiteName = sName; | 188 | sSuiteName = sName; |
186 | } | 189 | } |
187 | 190 | ||
191 | void Bu::UnitSuite::dispProgress() | ||
192 | { | ||
193 | if( tLastUpdate == time( NULL ) ) | ||
194 | return; | ||
195 | sio << Fmt(3) << (iProgress*100/iStepCount) << "%" << "\b\b\b\b" | ||
196 | << sio.flush; | ||
197 | tLastUpdate = time( NULL ); | ||
198 | } | ||
199 | |||
200 | void Bu::UnitSuite::setStepCount( int iSteps ) | ||
201 | { | ||
202 | iStepCount = iSteps; | ||
203 | if( iStepCount < 0 ) | ||
204 | return; | ||
205 | tLastUpdate = 0; | ||
206 | dispProgress(); | ||
207 | } | ||
208 | |||
209 | void Bu::UnitSuite::incProgress( int iAmnt ) | ||
210 | { | ||
211 | iProgress += iAmnt; | ||
212 | if( iProgress < 0 ) | ||
213 | iProgress = 0; | ||
214 | if( iProgress > iStepCount ) | ||
215 | iProgress = iStepCount; | ||
216 | dispProgress(); | ||
217 | } | ||
218 | |||
219 | void Bu::UnitSuite::setProgress( int iAmnt ) | ||
220 | { | ||
221 | iProgress = iAmnt; | ||
222 | if( iProgress < 0 ) | ||
223 | iProgress = 0; | ||
224 | if( iProgress > iStepCount ) | ||
225 | iProgress = iStepCount; | ||
226 | dispProgress(); | ||
227 | } | ||
228 | |||
188 | int Bu::UnitSuite::onListCases( StrArray ) | 229 | int Bu::UnitSuite::onListCases( StrArray ) |
189 | { | 230 | { |
190 | sio << "Test cases:" << sio.nl; | 231 | sio << "Test cases:" << sio.nl; |
diff --git a/src/unitsuite.h b/src/unitsuite.h index ddd3835..475ee48 100644 --- a/src/unitsuite.h +++ b/src/unitsuite.h | |||
@@ -96,6 +96,11 @@ namespace Bu | |||
96 | void add( Test fTest, const Bu::FString &sName, Expect e=expectPass ); | 96 | void add( Test fTest, const Bu::FString &sName, Expect e=expectPass ); |
97 | void setName( const FString &sName ); | 97 | void setName( const FString &sName ); |
98 | 98 | ||
99 | void dispProgress(); | ||
100 | void setStepCount( int iSteps ); | ||
101 | void incProgress( int iAmnt = 1 ); | ||
102 | void setProgress( int iAmnt ); | ||
103 | |||
99 | private: | 104 | private: |
100 | int onListCases( Bu::Array<Bu::FString> aParam ); | 105 | int onListCases( Bu::Array<Bu::FString> aParam ); |
101 | 106 | ||
@@ -116,6 +121,9 @@ namespace Bu | |||
116 | typedef Bu::List<Bu::FString> StrList; | 121 | typedef Bu::List<Bu::FString> StrList; |
117 | StrList lFileCleanup; | 122 | StrList lFileCleanup; |
118 | int iNameWidth; | 123 | int iNameWidth; |
124 | int iStepCount; | ||
125 | int iProgress; | ||
126 | time_t tLastUpdate; | ||
119 | }; | 127 | }; |
120 | 128 | ||
121 | Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::UnitSuite::Expect &e ); | 129 | Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::UnitSuite::Expect &e ); |