1 /*-------------------------------------------------------------------------
2 * drawElements C++ Base Library
3 * -----------------------------
5 * Copyright 2014 The Android Open Source Project
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Ring buffer template.
22 *//*--------------------------------------------------------------------*/
24 #include "deRingBuffer.hpp"
25 #include "deRandom.hpp"
34 void RingBuffer_selfTest (void)
36 const int numIterations = 16;
38 for (int iterNdx = 0; iterNdx < numIterations; 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);
46 for (int i = 0; i < dataSize; i++)
52 while (writePos < dataSize || readPos < dataSize)
54 bool canRead = buffer.getNumElements() > 0;
55 bool canWrite = writePos < dataSize && buffer.getNumFree() > 0;
56 bool doRead = canRead && (!canWrite || rnd.getBool());
58 // Free count must match.
59 DE_TEST_ASSERT(buffer.getNumFree() == bufSize - (writePos-readPos));
63 int numBytes = rnd.getInt(1, buffer.getNumElements());
64 vector<int> tmp (numBytes);
66 buffer.popBack(&tmp[0], numBytes);
68 for (int i = 0; i < numBytes; i++)
69 DE_TEST_ASSERT(tmp[i] == data[readPos+i]);
75 DE_TEST_ASSERT(canWrite);
77 int numBytes = rnd.getInt(1, de::min(dataSize-writePos, buffer.getNumFree()));
78 buffer.pushFront(&data[writePos], numBytes);