aboutsummaryrefslogtreecommitdiff
path: root/src/unit/myriad.unit
blob: a88650f0a03cf3114367ce6ad125e96bd3908163 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// 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
{
	test setSize
	{
		FString sFileName("myriad-XXXXXXX");

		File fMyriad = tempFile( sFileName );
		Myriad m( fMyriad, 32 );

		MyriadStream ms = m.openStream( m.createStream() );
		ms.setSize( 150 );
		ms.setPos( 145 );
		char stuff[10];
		unitTest( ms.read( stuff, 10 ) == 5 );

		ms.setSize( 12 );
		unitTest( ms.read( stuff, 10 ) == 0 );
		unitTest( ms.write( "hello", 5 ) == 5 );
		unitTest( ms.tell() == 17 );

		ms.setSize( 500 );
		unitTest( ms.tell() == 17 );
	}

	void addBlock( Stream &s, bool bAppend=true )
	{
		if( bAppend )
			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;
		}
		if( random()%2 == 0 )
		{
			s.write( buf, iSize-8 );
		}
		else
		{
			for( int j = 0; j < iSize-8; )
			{
				int iAmnt = (random()%8)+1;
				if( iAmnt+j > iSize-8 )
					iAmnt = iSize-8-j;
				iAmnt = s.write( buf+j, iAmnt );
				j += iAmnt;
			}
		}
		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 stressGrow
	{
		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 );
		}
	}
	
	test stressTruncate
	{
		FString sFileName("myriad-XXXXXXX");

		File fMyriad = tempFile( sFileName );
		Myriad m( fMyriad );
		m.initialize( 128 );

		Array<int> aStream;

		for( int j = 0; j < 5; j++ )
		{
			aStream.append( m.createStream() );
		}
		
		srandom( 1024 );

		char b;
		for( int iter = 0; iter < 2500; iter++ )
		{
			for( Array<int>::iterator i = aStream.begin(); i; i++ )
			{
				MyriadStream ms = m.openStream( *i );
				addBlock( ms, false );
				ms.setSize( ms.tell() );
				unitTest( ms.read( &b, 1 ) == 0 );
				ms.setPos( 0 );
				verifyBlock( ms );
				unitTest( ms.read( &b, 1 ) == 0 );
			}
		}
	}
	
	test stressTruncate2
	{
		FString sFileName("myriad-XXXXXXX");

		Array<int> aStream;

		{
			File fMyriad = tempFile( sFileName );
			Myriad m( fMyriad, 64 );

			for( int j = 0; j < 5; j++ )
			{
				aStream.append( m.createStream() );
			}
		}
		
		srandom( 1024 );

		char b;
		for( int iter = 0; iter < 2500; iter++ )
		{
			File fMyriad( sFileName, File::ReadWrite );
			Myriad m( fMyriad );
			for( Array<int>::iterator i = aStream.begin(); i; i++ )
			{
				MyriadStream ms = m.openStream( *i );
				addBlock( ms, false );
				ms.setSize( ms.tell() );
				unitTest( ms.read( &b, 1 ) == 0 );
				ms.setPos( 0 );
				verifyBlock( ms );
				unitTest( ms.read( &b, 1 ) == 0 );
			}
		}
	}
}