File: TestsRandomStream.cxx

package info (click to toggle)
clam 1.4.0-5.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,780 kB
  • sloc: cpp: 92,499; python: 9,721; ansic: 1,602; xml: 444; sh: 239; makefile: 153; perl: 54; asm: 15
file content (263 lines) | stat: -rw-r--r-- 5,308 bytes parent folder | download | duplicates (2)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "WritingRegion.hxx" // CLAM
#include "ReadingRegion.hxx" // CLAM

#include "PhantomBuffer.hxx" // CLAM
#include "Assert.hxx" // CLAM
#include <cppunit/extensions/HelperMacros.h>
#include <sstream>
#include <fstream>
#include <list>
#include <cstdlib>
#include <ctime>
#include "cppUnitHelper.hxx"

namespace CLAMTest {

class TestsRandomStream ;
CPPUNIT_TEST_SUITE_REGISTRATION( TestsRandomStream );

class TestsRandomStream : public CppUnit::TestFixture
{
	typedef CLAM::WritingRegion<char, CLAM::PhantomBuffer > WRegion;
	typedef CLAM::WritingRegion<char, CLAM::PhantomBuffer>::ProperReadingRegion RRegion;

	WRegion mWriter;
	RRegion mOutputReader;
	std::list< RRegion * > mOtherReaders;
	std::ifstream mInputFile;
	std::string mInputString;
	std::ostringstream mOutput;
	std::ostringstream mBuff;

public:
	TestsRandomStream() :
		mInputFile( GetTestDataDirectory("networkTestsData/testFile.txt").c_str()),
		mInputString( (std::istreambuf_iterator<char>(mInputFile)), std::istreambuf_iterator<char>() )
	{

		mInputFile.seekg( std::ios::beg );
		std::srand(std::time(0));
	}
		
	~TestsRandomStream()
	{
		std::list< RRegion* >::iterator it;
		for(it=mOtherReaders.begin(); it!=mOtherReaders.end(); it++)
			delete *it;
	}

	CPPUNIT_TEST_SUITE( TestsRandomStream );
	CPPUNIT_TEST( test );
	CPPUNIT_TEST_SUITE_END();
private:
	bool FillWriterFromInputFile()
	{
		int inchar;
		for (int i=0; i<mWriter.Size(); i++)
		{
			if (i<mWriter.Hop())
			{
				inchar = mInputFile.get();
				if (inchar<0)
				{
					mWriter[i] ='\0';
					return false;
				}
				mWriter[i] = inchar;
			}
			else
				mWriter[i] = 'X';
		}
		return true;
	}
	void DumpOutputReader()
	{
		for (int i=0; i<mOutputReader.Size(); i++)
		{
			char actualToken = mOutputReader[i];
			if (actualToken =='\n')
			{
				mOutput << mBuff.str() << '\n';
				mBuff.str("");
			}
			else
				mBuff << actualToken;
		}
	}

	bool ChanceOf(double chance)
	{
		double rand = std::rand() / float(RAND_MAX);
		return rand < chance;
		return true;
	}

	int RandomIntFromTo(int from, int to)
	{
		int dist = to-from;
		int r = rand();
		int result = from + int( r / double(RAND_MAX) * dist);

		CLAM_DEBUG_ASSERT(result>=from && result <=to, "TestsRandomStream::randomIntFromTo() - "
								 " result must be inside interval marked by parameters" );
		return result;
	}


	void MaybeChangeSizeWriter()
	{
		if (ChanceOf(1-0.3))
			return;

		int newSize = RandomIntFromTo(1, 40);
		mWriter.Size(newSize);
		mWriter.Hop(newSize);
	}
	
	void MaybeChangeHopWriter()
	{
		if (ChanceOf(1-0.3))
			return;

		int newSize = RandomIntFromTo(1, mWriter.Size());
		mWriter.Hop(newSize);
	}

	void MaybeChangeHopAndSizeOutputReader()
	{	
		if (ChanceOf(1-0.3))
			return;

		int newSize = RandomIntFromTo(1, 40);
		mOutputReader.Size(newSize);
		mOutputReader.Hop(newSize);
	}

	void CreateOtherReader()
	{
		int newSize = RandomIntFromTo(1, 40);
		RRegion * newRegion = new RRegion; 
		newRegion->Size(newSize);
		newRegion->Hop(newSize);
		mWriter.LinkRegions( *newRegion );
		mOtherReaders.push_back( newRegion );

	}
	void CreateOtherReadersRandomly()
	{
		int nreaders = RandomIntFromTo(3, 25);
		for( int i=0; i<nreaders; i++)
		{
			CreateOtherReader();
		}
	}
	void MaybeCreateOtherReader()
	{
		if (ChanceOf(0.5))
			return;
		CreateOtherReader();
	}
	
	void MaybeRemoveOtherReader()
	{	
		std::list< RRegion * >::iterator it;
		for(it=mOtherReaders.begin(); it!=mOtherReaders.end(); it++)
		{
			if(ChanceOf(0.1))
			{
				RRegion * toDelete = *it;
				mWriter.RemoveRegion( *toDelete );
				mOtherReaders.remove( toDelete );
				delete toDelete;
				return;
			}
		}
	}
	
	void MaybeModifyHopAndSizeOfOtherReaders()
	{	
		std::list< RRegion * >::iterator it;
		for(it=mOtherReaders.begin(); it!=mOtherReaders.end(); it++)
		{
			if(ChanceOf(0.2))
			{
				RRegion * otherRegion = *it;
				int newSize = RandomIntFromTo(1, 40);
				otherRegion->Size( newSize );
				otherRegion->Hop( newSize );

			}		
			if(ChanceOf(0.2))
			{
				RRegion * otherRegion = *it;
				int newHop = RandomIntFromTo(1, otherRegion->Size() );
				otherRegion->Hop( newHop );
			}

		}
	}

	void MaybeChangeOtherReaders()
	{
		MaybeCreateOtherReader();
		MaybeRemoveOtherReader();
		MaybeModifyHopAndSizeOfOtherReaders();
	}

	void OtherReadersConsume()
	{
		std::list< RRegion * >::iterator it;
		for(it=mOtherReaders.begin(); it!=mOtherReaders.end(); it++)
			if( (*it)->CanConsume() )
			{
				for(int i=0;i<(*it)->Size();i++)
				{
					(*it)->operator[](i);
				}
				(*it)->Consume();
			}
	}

	void test()
	{
		mWriter.Size(2);
		mWriter.Hop(2);
		mWriter.LinkRegions(mOutputReader);
		bool endOfFile = false;
		CreateOtherReadersRandomly();

		while(true)
		{	
			if ( mWriter.CanProduce() && !endOfFile )
			{
				endOfFile =  !FillWriterFromInputFile();
				CLAM_ASSERT(mWriter.CanProduce(), "mWriter should be able to produce");
				mWriter.Produce();
			}
								
			MaybeChangeSizeWriter();
			MaybeChangeHopWriter();
		//	MaybeChangeHopAndSizeOutputReader();
			MaybeChangeOtherReaders();

			OtherReadersConsume();
			
			if( mOutputReader.CanConsume() )
			{
				DumpOutputReader();
				mOutputReader.Consume();
			}
			else
				if( endOfFile ) break;
		}

		CPPUNIT_ASSERT_EQUAL( mInputString, mOutput.str() );
	}
};





} // namespace CLAMTest