1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
// 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 <stdlib.h>
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 fMyriad = tempFile( sFileName );
Myriad m( fMyriad );
m.initialize( 64 );
Array<int> 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<int>::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<int>::iterator i = aStreams.begin(); i; i++ )
{
MyriadStream ms = m.openStream( *i );
verifyStream( ms );
}
}
}
|