aboutsummaryrefslogtreecommitdiff
path: root/src/unit/myriad.unit
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-11-05 22:41:51 +0000
committerMike Buland <eichlan@xagasoft.com>2012-11-05 22:41:51 +0000
commitec05778d5718a7912e506764d443a78d6a6179e3 (patch)
tree78a9a01532180030c095acefc45763f07c14edb8 /src/unit/myriad.unit
parentb20414ac1fe80a71a90601f4cd1767fa7014a9ba (diff)
downloadlibbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.gz
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.bz2
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.tar.xz
libbu++-ec05778d5718a7912e506764d443a78d6a6179e3.zip
Converted tabs to spaces with tabconv.
Diffstat (limited to 'src/unit/myriad.unit')
-rw-r--r--src/unit/myriad.unit678
1 files changed, 339 insertions, 339 deletions
diff --git a/src/unit/myriad.unit b/src/unit/myriad.unit
index 8c96f79..4960f38 100644
--- a/src/unit/myriad.unit
+++ b/src/unit/myriad.unit
@@ -26,360 +26,360 @@ class VerifyObject
26friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const VerifyObject &vo ); 26friend Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const VerifyObject &vo );
27friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, VerifyObject &vo ); 27friend Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, VerifyObject &vo );
28public: 28public:
29 VerifyObject( int iUnits ) : 29 VerifyObject( int iUnits ) :
30 iUnits( iUnits ), 30 iUnits( iUnits ),
31 iBytesWritten( 0 ) 31 iBytesWritten( 0 )
32 { 32 {
33 } 33 }
34 34
35 virtual ~VerifyObject() 35 virtual ~VerifyObject()
36 { 36 {
37 } 37 }
38 38
39 int getBytesWritten() 39 int getBytesWritten()
40 { 40 {
41 return iBytesWritten; 41 return iBytesWritten;
42 } 42 }
43 43
44private: 44private:
45 int iUnits; 45 int iUnits;
46 mutable int iBytesWritten; 46 mutable int iBytesWritten;
47}; 47};
48 48
49Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const VerifyObject &vo ) 49Bu::ArchiveBase &operator<<( Bu::ArchiveBase &ar, const VerifyObject &vo )
50{ 50{
51 Md5 sum; 51 Md5 sum;
52 ar << vo.iUnits; 52 ar << vo.iUnits;
53 vo.iBytesWritten = 4; 53 vo.iBytesWritten = 4;
54 sum.addData( &vo.iUnits, 4 ); 54 sum.addData( &vo.iUnits, 4 );
55 for( int j = 0; j < vo.iUnits; j++ ) 55 for( int j = 0; j < vo.iUnits; j++ )
56 { 56 {
57 int iRand = random()%128; 57 int iRand = random()%128;
58// ar << iRand; 58// ar << iRand;
59 Bu::String sDat( iRand ); 59 Bu::String sDat( iRand );
60 for( int j = 0; j < iRand; j++ ) 60 for( int j = 0; j < iRand; j++ )
61 sDat[j] = (char)((uint8_t)(random()%256)); 61 sDat[j] = (char)((uint8_t)(random()%256));
62 ar << sDat; 62 ar << sDat;
63 sum.addData( &iRand, 4 ); 63 sum.addData( &iRand, 4 );
64 sum.addData( sDat.getStr(), iRand ); 64 sum.addData( sDat.getStr(), iRand );
65 vo.iBytesWritten += 4 + iRand; 65 vo.iBytesWritten += 4 + iRand;
66 } 66 }
67 Bu::String sRes = sum.getResult(); 67 Bu::String sRes = sum.getResult();
68 ar << sRes; 68 ar << sRes;
69 vo.iBytesWritten += 4 + sRes.getSize(); 69 vo.iBytesWritten += 4 + sRes.getSize();
70 return ar; 70 return ar;
71} 71}
72 72
73Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, VerifyObject &vo ) 73Bu::ArchiveBase &operator>>( Bu::ArchiveBase &ar, VerifyObject &vo )
74{ 74{
75 Md5 sum; 75 Md5 sum;
76 ar >> vo.iUnits; 76 ar >> vo.iUnits;
77 sum.addData( &vo.iUnits, 4 ); 77 sum.addData( &vo.iUnits, 4 );
78 for( int j = 0; j < vo.iUnits; j++ ) 78 for( int j = 0; j < vo.iUnits; j++ )
79 { 79 {
80 int iRand; 80 int iRand;
81// ar >> iRand; 81// ar >> iRand;
82 Bu::String sStr; 82 Bu::String sStr;
83 ar >> sStr; 83 ar >> sStr;
84 iRand = sStr.getSize(); 84 iRand = sStr.getSize();
85 sum.addData( &iRand, 4 ); 85 sum.addData( &iRand, 4 );
86 sum.addData( sStr.getStr(), iRand ); 86 sum.addData( sStr.getStr(), iRand );
87 } 87 }
88 Bu::String sSum; 88 Bu::String sSum;
89 ar >> sSum; 89 ar >> sSum;
90 unitTest( sSum == sum.getResult() ); 90 unitTest( sSum == sum.getResult() );
91 int iTooMuch; 91 int iTooMuch;
92 try 92 try
93 { 93 {
94 ar >> iTooMuch; 94 ar >> iTooMuch;
95 unitFailed("should have thrown an exception."); 95 unitFailed("should have thrown an exception.");
96 } 96 }
97 catch( Bu::ExceptionBase &e ) 97 catch( Bu::ExceptionBase &e )
98 { 98 {
99 } 99 }
100 return ar; 100 return ar;
101} 101}
102 102
103suite Myriad 103suite Myriad
104{ 104{
105 test setSize 105 test setSize
106 { 106 {
107 String sFileName("myriad-XXXXXXX"); 107 String sFileName("myriad-XXXXXXX");
108 108
109 File fMyriad = tempFile( sFileName ); 109 File fMyriad = tempFile( sFileName );
110 Myriad m( fMyriad, 32 ); 110 Myriad m( fMyriad, 32 );
111 111
112 MyriadStream ms = m.openStream( m.createStream() ); 112 MyriadStream ms = m.openStream( m.createStream() );
113 ms.setSize( 150 ); 113 ms.setSize( 150 );
114 ms.setPos( 145 ); 114 ms.setPos( 145 );
115 char stuff[10]; 115 char stuff[10];
116 unitTest( ms.read( stuff, 10 ) == 5 ); 116 unitTest( ms.read( stuff, 10 ) == 5 );
117 117
118 ms.setSize( 12 ); 118 ms.setSize( 12 );
119 unitTest( ms.read( stuff, 10 ) == 0 ); 119 unitTest( ms.read( stuff, 10 ) == 0 );
120 unitTest( ms.write( "hello", 5 ) == 5 ); 120 unitTest( ms.write( "hello", 5 ) == 5 );
121 unitTest( ms.tell() == 17 ); 121 unitTest( ms.tell() == 17 );
122 122
123 ms.setSize( 500 ); 123 ms.setSize( 500 );
124 unitTest( ms.tell() == 17 ); 124 unitTest( ms.tell() == 17 );
125 } 125 }
126 126
127 void addBlock( Stream &s, bool bAppend=true ) 127 void addBlock( Stream &s, bool bAppend=true )
128 { 128 {
129 if( bAppend ) 129 if( bAppend )
130 s.setPosEnd( 0 ); 130 s.setPosEnd( 0 );
131 int iSize = (random()%1016)+8; 131 int iSize = (random()%1016)+8;
132 s.write( &iSize, 4 ); 132 s.write( &iSize, 4 );
133 char *buf = new char[iSize-8]; 133 char *buf = new char[iSize-8];
134 for( int j = 0; j < iSize-8; j++ ) 134 for( int j = 0; j < iSize-8; j++ )
135 { 135 {
136 buf[j] = (j+iSize)%256; 136 buf[j] = (j+iSize)%256;
137 } 137 }
138 if( random()%2 == 0 ) 138 if( random()%2 == 0 )
139 { 139 {
140 s.write( buf, iSize-8 ); 140 s.write( buf, iSize-8 );
141 } 141 }
142 else 142 else
143 { 143 {
144 for( int j = 0; j < iSize-8; ) 144 for( int j = 0; j < iSize-8; )
145 { 145 {
146 int iAmnt = (random()%8)+1; 146 int iAmnt = (random()%8)+1;
147 if( iAmnt+j > iSize-8 ) 147 if( iAmnt+j > iSize-8 )
148 iAmnt = iSize-8-j; 148 iAmnt = iSize-8-j;
149 iAmnt = s.write( buf+j, iAmnt ); 149 iAmnt = s.write( buf+j, iAmnt );
150 j += iAmnt; 150 j += iAmnt;
151 } 151 }
152 } 152 }
153 delete[] buf; 153 delete[] buf;
154 iSize = ~iSize; 154 iSize = ~iSize;
155 s.write( &iSize, 4 ); 155 s.write( &iSize, 4 );
156 } 156 }
157 157
158 void verifyBlock( Stream &s ) 158 void verifyBlock( Stream &s )
159 { 159 {
160 int iSize, iInv; 160 int iSize, iInv;
161 if( s.read( &iSize, 4 ) == 0 ) 161 if( s.read( &iSize, 4 ) == 0 )
162 return; 162 return;
163 if( iSize < 8 || iSize > 1024 ) 163 if( iSize < 8 || iSize > 1024 )
164 throw ExceptionBase("Read bad data, %d", iSize ); 164 throw ExceptionBase("Read bad data, %d", iSize );
165 char *buf = new char[iSize-8]; 165 char *buf = new char[iSize-8];
166 if( s.read( buf, iSize-8 ) < (size_t)iSize-8 ) 166 if( s.read( buf, iSize-8 ) < (size_t)iSize-8 )
167 { 167 {
168 delete[] buf; 168 delete[] buf;
169 throw ExceptionBase("Block failed verify (insuffient block data)."); 169 throw ExceptionBase("Block failed verify (insuffient block data).");
170 } 170 }
171 for( int j = 0; j < iSize-8; j++ ) 171 for( int j = 0; j < iSize-8; j++ )
172 { 172 {
173 if( buf[j] != (char)((j+iSize)%256) ) 173 if( buf[j] != (char)((j+iSize)%256) )
174 { 174 {
175 char b = buf[j]; 175 char b = buf[j];
176 delete[] buf; 176 delete[] buf;
177 throw ExceptionBase("Block failed computed data verify " 177 throw ExceptionBase("Block failed computed data verify "
178 "(%02X==%02X).", b, (char)((j+iSize)%256) ); 178 "(%02X==%02X).", b, (char)((j+iSize)%256) );
179 } 179 }
180 } 180 }
181 delete[] buf; 181 delete[] buf;
182 if( s.read( &iInv, 4 ) < 4 ) 182 if( s.read( &iInv, 4 ) < 4 )
183 throw ExceptionBase("Block failed verify (insufficient data)."); 183 throw ExceptionBase("Block failed verify (insufficient data).");
184 if( iInv != ~iSize ) 184 if( iInv != ~iSize )
185 throw ExceptionBase("Block failed inversion verify."); 185 throw ExceptionBase("Block failed inversion verify.");
186 } 186 }
187 187
188 void verifyStream( Stream &s ) 188 void verifyStream( Stream &s )
189 { 189 {
190 s.setPos( 0 ); 190 s.setPos( 0 );
191 while( !s.isEos() ) 191 while( !s.isEos() )
192 verifyBlock( s ); 192 verifyBlock( s );
193 } 193 }
194 194
195 test stressGrow 195 test stressGrow
196 { 196 {
197 String sFileName("myriad-XXXXXXX"); 197 String sFileName("myriad-XXXXXXX");
198 198
199 File fMyriad = tempFile( sFileName ); 199 File fMyriad = tempFile( sFileName );
200 Myriad m( fMyriad ); 200 Myriad m( fMyriad );
201 m.initialize( 64 ); 201 m.initialize( 64 );
202 202
203 Array<int> aStreams; 203 Array<int> aStreams;
204 for( int j = 0; j < 5; j++ ) 204 for( int j = 0; j < 5; j++ )
205 { 205 {
206 aStreams.append( m.createStream() ); 206 aStreams.append( m.createStream() );
207 } 207 }
208 208
209 srandom( 512 ); 209 srandom( 512 );
210 210
211 for( int j = 0; j < 2500; j++ ) 211 for( int j = 0; j < 2500; j++ )
212 { 212 {
213 switch( random()%5 ) 213 switch( random()%5 )
214 { 214 {
215 case 0: 215 case 0:
216 aStreams.append( m.createStream() ); 216 aStreams.append( m.createStream() );
217 break; 217 break;
218 218
219 case 1: 219 case 1:
220 if( aStreams.getSize() > 0 ) 220 if( aStreams.getSize() > 0 )
221 { 221 {
222 int iStream = random()%aStreams.getSize(); 222 int iStream = random()%aStreams.getSize();
223 { 223 {
224 MyriadStream ms = m.openStream( aStreams[iStream] ); 224 MyriadStream ms = m.openStream( aStreams[iStream] );
225 verifyStream( ms ); 225 verifyStream( ms );
226 } 226 }
227 m.deleteStream( aStreams[iStream] ); 227 m.deleteStream( aStreams[iStream] );
228 Array<int>::iterator i = aStreams.begin(); 228 Array<int>::iterator i = aStreams.begin();
229 for( int k = 0; k < iStream; k++ ) 229 for( int k = 0; k < iStream; k++ )
230 i++; 230 i++;
231 aStreams.erase( i ); 231 aStreams.erase( i );
232 } 232 }
233 break; 233 break;
234 234
235 default: 235 default:
236 if( aStreams.getSize() == 0 ) 236 if( aStreams.getSize() == 0 )
237 { 237 {
238 aStreams.append( m.createStream() ); 238 aStreams.append( m.createStream() );
239 } 239 }
240 { 240 {
241 int iStream = random()%aStreams.getSize(); 241 int iStream = random()%aStreams.getSize();
242 MyriadStream ms = m.openStream( aStreams[iStream] ); 242 MyriadStream ms = m.openStream( aStreams[iStream] );
243 addBlock( ms ); 243 addBlock( ms );
244 verifyStream( ms ); 244 verifyStream( ms );
245 } 245 }
246 break; 246 break;
247 } 247 }
248 } 248 }
249 249
250 for( Array<int>::iterator i = aStreams.begin(); i; i++ ) 250 for( Array<int>::iterator i = aStreams.begin(); i; i++ )
251 { 251 {
252 MyriadStream ms = m.openStream( *i ); 252 MyriadStream ms = m.openStream( *i );
253 verifyStream( ms ); 253 verifyStream( ms );
254 } 254 }
255 } 255 }
256 256
257 test stressTruncate 257 test stressTruncate
258 { 258 {
259 String sFileName("myriad-XXXXXXX"); 259 String sFileName("myriad-XXXXXXX");
260 260
261 File fMyriad = tempFile( sFileName ); 261 File fMyriad = tempFile( sFileName );
262 Myriad m( fMyriad ); 262 Myriad m( fMyriad );
263 m.initialize( 128 ); 263 m.initialize( 128 );
264 264
265 Array<int> aStream; 265 Array<int> aStream;
266 266
267 for( int j = 0; j < 5; j++ ) 267 for( int j = 0; j < 5; j++ )
268 { 268 {
269 aStream.append( m.createStream() ); 269 aStream.append( m.createStream() );
270 } 270 }
271 271
272 srandom( 1024 ); 272 srandom( 1024 );
273 273
274 char b; 274 char b;
275 for( int iter = 0; iter < 2500; iter++ ) 275 for( int iter = 0; iter < 2500; iter++ )
276 { 276 {
277 for( Array<int>::iterator i = aStream.begin(); i; i++ ) 277 for( Array<int>::iterator i = aStream.begin(); i; i++ )
278 { 278 {
279 MyriadStream ms = m.openStream( *i ); 279 MyriadStream ms = m.openStream( *i );
280 addBlock( ms, false ); 280 addBlock( ms, false );
281 ms.setSize( ms.tell() ); 281 ms.setSize( ms.tell() );
282 unitTest( ms.read( &b, 1 ) == 0 ); 282 unitTest( ms.read( &b, 1 ) == 0 );
283 ms.setPos( 0 ); 283 ms.setPos( 0 );
284 verifyBlock( ms ); 284 verifyBlock( ms );
285 unitTest( ms.read( &b, 1 ) == 0 ); 285 unitTest( ms.read( &b, 1 ) == 0 );
286 } 286 }
287 } 287 }
288 } 288 }
289 289
290 test stressTruncate2 290 test stressTruncate2
291 { 291 {
292 String sFileName("myriad-XXXXXXX"); 292 String sFileName("myriad-XXXXXXX");
293 293
294 Array<int> aStream; 294 Array<int> aStream;
295 295
296 setStepCount( 5*2500 + 5 ); 296 setStepCount( 5*2500 + 5 );
297 297
298 { 298 {
299 File fMyriad = tempFile( sFileName ); 299 File fMyriad = tempFile( sFileName );
300 Myriad m( fMyriad, 128 ); 300 Myriad m( fMyriad, 128 );
301 301
302 for( int j = 0; j < 5; j++ ) 302 for( int j = 0; j < 5; j++ )
303 { 303 {
304 aStream.append( m.createStream() ); 304 aStream.append( m.createStream() );
305 incProgress(); 305 incProgress();
306 } 306 }
307 } 307 }
308 308
309 srandom( 1024 ); 309 srandom( 1024 );
310 310
311 char b; 311 char b;
312 for( int iter = 0; iter < 2500; iter++ ) 312 for( int iter = 0; iter < 2500; iter++ )
313 { 313 {
314 File fMyriad( sFileName, File::ReadWrite ); 314 File fMyriad( sFileName, File::ReadWrite );
315 Myriad m( fMyriad ); 315 Myriad m( fMyriad );
316 for( Array<int>::iterator i = aStream.begin(); i; i++ ) 316 for( Array<int>::iterator i = aStream.begin(); i; i++ )
317 { 317 {
318 MyriadStream ms = m.openStream( *i ); 318 MyriadStream ms = m.openStream( *i );
319 addBlock( ms, false ); 319 addBlock( ms, false );
320 ms.setSize( ms.tell() ); 320 ms.setSize( ms.tell() );
321 unitTest( ms.read( &b, 1 ) == 0 ); 321 unitTest( ms.read( &b, 1 ) == 0 );
322 ms.setPos( 0 ); 322 ms.setPos( 0 );
323 verifyBlock( ms ); 323 verifyBlock( ms );
324 unitTest( ms.read( &b, 1 ) == 0 ); 324 unitTest( ms.read( &b, 1 ) == 0 );
325 incProgress(); 325 incProgress();
326 } 326 }
327 } 327 }
328 } 328 }
329 329
330 test stressArchive 330 test stressArchive
331 { 331 {
332 String sFileName("myriad-XXXXXX"); 332 String sFileName("myriad-XXXXXX");
333 Array<int> aStream; 333 Array<int> aStream;
334 334
335 srandom( 2096 ); 335 srandom( 2096 );
336 336
337 setStepCount( 15*250 + 15 ); 337 setStepCount( 15*250 + 15 );
338 338
339 { 339 {
340 File fMyriad = tempFile( sFileName ); 340 File fMyriad = tempFile( sFileName );
341 Myriad m( fMyriad, 1024 ); 341 Myriad m( fMyriad, 1024 );
342 342
343 for( int j = 0; j < 15; j++ ) 343 for( int j = 0; j < 15; j++ )
344 { 344 {
345 int iStream = m.createStream(); 345 int iStream = m.createStream();
346 aStream.append( iStream ); 346 aStream.append( iStream );
347 VerifyObject vo( random()%1024 ); 347 VerifyObject vo( random()%1024 );
348 { 348 {
349 MyriadStream ms = m.openStream( iStream ); 349 MyriadStream ms = m.openStream( iStream );
350 Archive ar( ms, Archive::save ); 350 Archive ar( ms, Archive::save );
351 ar << vo; 351 ar << vo;
352 unitTest( ms.tell() == vo.getBytesWritten() ); 352 unitTest( ms.tell() == vo.getBytesWritten() );
353 ms.setSize( ms.tell() ); 353 ms.setSize( ms.tell() );
354 } 354 }
355 unitTest( m.getStreamSize( iStream ) == vo.getBytesWritten() ); 355 unitTest( m.getStreamSize( iStream ) == vo.getBytesWritten() );
356 incProgress(); 356 incProgress();
357 } 357 }
358 } 358 }
359 359
360 for( int iter = 0; iter < 250; iter++ ) 360 for( int iter = 0; iter < 250; iter++ )
361 { 361 {
362 File fMyriad( sFileName, File::ReadWrite ); 362 File fMyriad( sFileName, File::ReadWrite );
363 Myriad m( fMyriad ); 363 Myriad m( fMyriad );
364 for( Array<int>::iterator i = aStream.begin(); i; i++ ) 364 for( Array<int>::iterator i = aStream.begin(); i; i++ )
365 { 365 {
366 VerifyObject vo( random()%1024 ); 366 VerifyObject vo( random()%1024 );
367 { 367 {
368 MyriadStream ms = m.openStream( *i ); 368 MyriadStream ms = m.openStream( *i );
369 Archive ar( ms, Archive::load ); 369 Archive ar( ms, Archive::load );
370 ar >> vo; 370 ar >> vo;
371 } 371 }
372 { 372 {
373 MyriadStream ms = m.openStream( *i ); 373 MyriadStream ms = m.openStream( *i );
374 Archive ar( ms, Archive::save ); 374 Archive ar( ms, Archive::save );
375 ar << vo; 375 ar << vo;
376 unitTest( ms.tell() == vo.getBytesWritten() ); 376 unitTest( ms.tell() == vo.getBytesWritten() );
377 ms.setSize( ms.tell() ); 377 ms.setSize( ms.tell() );
378 } 378 }
379 unitTest( m.getStreamSize( *i ) == vo.getBytesWritten() ); 379 unitTest( m.getStreamSize( *i ) == vo.getBytesWritten() );
380 incProgress(); 380 incProgress();
381 } 381 }
382 } 382 }
383 } 383 }
384} 384}
385 385