1 /*-------------------------------------------------------------------------
2 * drawElements C++ Base Library
3 * -----------------------------
5 * Copyright 2015 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 Fast ordered append-only container
22 *//*--------------------------------------------------------------------*/
24 #include "deAppendList.hpp"
25 #include "deThread.hpp"
26 #include "deSpinBarrier.hpp"
27 #include "deSharedPtr.hpp"
45 TestElem (deUint32 threadNdx_, deUint32 elemNdx_)
46 : threadNdx (threadNdx_)
60 AppendList<TestElem> testList;
62 SharedState (deUint32 numThreads, deUint32 numElements_, deUint32 numElementsHint)
63 : numElements (numElements_)
64 , barrier (numThreads)
65 , testList (numElementsHint)
69 class TestThread : public Thread
72 TestThread (SharedState* shared, deUint32 threadNdx)
74 , m_threadNdx (threadNdx)
79 const deUint32 syncPerElems = 10000;
81 for (deUint32 elemNdx = 0; elemNdx < m_shared->numElements; elemNdx++)
83 if (elemNdx % syncPerElems == 0)
84 m_shared->barrier.sync(SpinBarrier::WAIT_MODE_AUTO);
86 m_shared->testList.append(TestElem(m_threadNdx, elemNdx));
91 SharedState* const m_shared;
92 const deUint32 m_threadNdx;
95 typedef SharedPtr<TestThread> TestThreadSp;
97 void runAppendListTest (deUint32 numThreads, deUint32 numElements, deUint32 numElementsHint)
99 SharedState sharedState (numThreads, numElements, numElementsHint);
100 vector<TestThreadSp> threads (numThreads);
102 for (deUint32 threadNdx = 0; threadNdx < numThreads; ++threadNdx)
104 threads[threadNdx] = TestThreadSp(new TestThread(&sharedState, threadNdx));
105 threads[threadNdx]->start();
108 for (deUint32 threadNdx = 0; threadNdx < numThreads; ++threadNdx)
109 threads[threadNdx]->join();
111 DE_TEST_ASSERT(sharedState.testList.size() == (size_t)numElements*(size_t)numThreads);
114 vector<deUint32> countByThread (numThreads);
116 std::fill(countByThread.begin(), countByThread.end(), 0);
118 for (AppendList<TestElem>::const_iterator elemIter = sharedState.testList.begin();
119 elemIter != sharedState.testList.end();
122 const TestElem& elem = *elemIter;
124 DE_TEST_ASSERT(de::inBounds(elem.threadNdx, 0u, numThreads));
125 DE_TEST_ASSERT(countByThread[elem.threadNdx] == elem.elemNdx);
127 countByThread[elem.threadNdx] += 1;
130 for (deUint32 threadNdx = 0; threadNdx < numThreads; ++threadNdx)
131 DE_TEST_ASSERT(countByThread[threadNdx] == numElements);
138 ObjCountElem (int* liveCount)
139 : m_liveCount(liveCount)
149 ObjCountElem (const ObjCountElem& other)
150 : m_liveCount(other.m_liveCount)
155 ObjCountElem& operator= (const ObjCountElem& other)
157 m_liveCount = other.m_liveCount;
166 void runClearTest (deUint32 numElements1, deUint32 numElements2, deUint32 numElementsHint)
171 de::AppendList<ObjCountElem> testList (numElementsHint);
173 for (deUint32 ndx = 0; ndx < numElements1; ++ndx)
174 testList.append(ObjCountElem(&liveCount));
176 DE_TEST_ASSERT(liveCount == (int)numElements1);
180 DE_TEST_ASSERT(liveCount == 0);
182 for (deUint32 ndx = 0; ndx < numElements2; ++ndx)
183 testList.append(ObjCountElem(&liveCount));
185 DE_TEST_ASSERT(liveCount == (int)numElements2);
188 DE_TEST_ASSERT(liveCount == 0);
193 void AppendList_selfTest (void)
196 runAppendListTest(1, 1000, 500);
197 runAppendListTest(1, 1000, 2000);
198 runAppendListTest(1, 35, 1);
201 runAppendListTest(2, 10000, 500);
202 runAppendListTest(2, 100, 10);
204 if (deGetNumAvailableLogicalCores() >= 4)
206 runAppendListTest(4, 10000, 500);
207 runAppendListTest(4, 100, 10);
211 runClearTest(1, 1, 1);
212 runClearTest(1, 2, 10);
213 runClearTest(50, 25, 10);
214 runClearTest(9, 50, 10);
215 runClearTest(10, 50, 10);
216 runClearTest(50, 9, 10);
217 runClearTest(50, 10, 10);