aboutsummaryrefslogtreecommitdiff
path: root/src/unit/myriad.unit
diff options
context:
space:
mode:
Diffstat (limited to 'src/unit/myriad.unit')
-rw-r--r--src/unit/myriad.unit143
1 files changed, 142 insertions, 1 deletions
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
19using namespace Bu; 22using namespace Bu;
20 23
24class VerifyObject
25{
26friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const VerifyObject &vo );
27friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, VerifyObject &vo );
28public:
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
44private:
45 int iUnits;
46 mutable int iBytesWritten;
47};
48
49Bu::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
73Bu::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
21suite Myriad 103suite 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 }