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.unit139
1 files changed, 139 insertions, 0 deletions
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