• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * drawElements C++ Base Library
3  * -----------------------------
4  *
5  * Copyright 2014 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Ring buffer template.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "deRingBuffer.hpp"
25 #include "deRandom.hpp"
26 
27 #include <vector>
28 
29 using std::vector;
30 
31 namespace de
32 {
33 
RingBuffer_selfTest(void)34 void RingBuffer_selfTest (void)
35 {
36 	const int numIterations = 16;
37 
38 	for (int iterNdx = 0; iterNdx < numIterations; iterNdx++)
39 	{
40 		Random			rnd			(iterNdx);
41 		int				bufSize		= rnd.getInt(1, 2048);
42 		int				dataSize	= rnd.getInt(100, 10000);
43 		RingBuffer<int>	buffer		(bufSize);
44 		vector<int>		data		(dataSize);
45 
46 		for (int i = 0; i < dataSize; i++)
47 			data[i] = i;
48 
49 		int				writePos	= 0;
50 		int				readPos		= 0;
51 
52 		while (writePos < dataSize || readPos < dataSize)
53 		{
54 			bool	canRead		= buffer.getNumElements() > 0;
55 			bool	canWrite	= writePos < dataSize && buffer.getNumFree() > 0;
56 			bool	doRead		= canRead && (!canWrite || rnd.getBool());
57 
58 			// Free count must match.
59 			DE_TEST_ASSERT(buffer.getNumFree() == bufSize - (writePos-readPos));
60 
61 			if (doRead)
62 			{
63 				int			numBytes	= rnd.getInt(1, buffer.getNumElements());
64 				vector<int>	tmp			(numBytes);
65 
66 				buffer.popBack(&tmp[0], numBytes);
67 
68 				for (int i = 0; i < numBytes; i++)
69 					DE_TEST_ASSERT(tmp[i] == data[readPos+i]);
70 
71 				readPos += numBytes;
72 			}
73 			else
74 			{
75 				DE_TEST_ASSERT(canWrite);
76 
77 				int numBytes = rnd.getInt(1, de::min(dataSize-writePos, buffer.getNumFree()));
78 				buffer.pushFront(&data[writePos], numBytes);
79 				writePos += numBytes;
80 			}
81 		}
82 	}
83 }
84 
85 } // de
86