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 Array template backed by memory pool.
22 *//*--------------------------------------------------------------------*/
24 #include "dePoolArray.hpp"
32 static void intArrayTest (void)
35 PoolArray<int> arr (&pool);
36 PoolArray<deUint16> arr16 (&pool);
39 /* Test pushBack(). */
40 for (i = 0; i < 5000; i++)
42 /* Dummy alloc to try to break alignments. */
46 arr16.pushBack((deInt16)i);
49 DE_TEST_ASSERT(arr.size() == 5000);
50 DE_TEST_ASSERT(arr16.size() == 5000);
51 for (i = 0; i < 5000; i++)
53 DE_TEST_ASSERT(arr[i] == i);
54 DE_TEST_ASSERT(arr16[i] == i);
58 for (i = 0; i < 1000; i++)
60 DE_TEST_ASSERT(arr.popBack() == (4999 - i));
61 DE_TEST_ASSERT(arr16.popBack() == (4999 - i));
64 DE_TEST_ASSERT(arr.size() == 4000);
65 DE_TEST_ASSERT(arr16.size() == 4000);
66 for (i = 0; i < 4000; i++)
68 DE_TEST_ASSERT(arr[i] == i);
69 DE_TEST_ASSERT(arr16[i] == i);
75 for (i = 1000; i < 5000; i++)
78 arr16.pushBack((deInt16)i);
81 DE_TEST_ASSERT(arr.size() == 5000);
82 DE_TEST_ASSERT(arr16.size() == 5000);
83 for (i = 0; i < 5000; i++)
85 DE_TEST_ASSERT(arr[i] == i);
86 DE_TEST_ASSERT(arr16[i] == i);
89 /* Test set() and pushBack() with reserve(). */
90 PoolArray<int> arr2(&pool);
93 for (i = 0; i < 1500; i++)
98 DE_TEST_ASSERT(arr2.size() == 5000);
99 for (i = 0; i < 5000; i++)
102 DE_TEST_ASSERT(val == i);
106 static void alignedIntArrayTest (void)
109 PoolArray<int, 16> arr (&pool);
110 PoolArray<deUint16, 8> arr16 (&pool);
113 /* Test pushBack(). */
114 for (i = 0; i < 5000; i++)
116 /* Dummy alloc to try to break alignments. */
120 arr16.pushBack((deInt16)i);
123 DE_TEST_ASSERT(arr.size() == 5000);
124 DE_TEST_ASSERT(arr16.size() == 5000);
125 for (i = 0; i < 5000; i++)
127 DE_TEST_ASSERT(arr[i] == i);
128 DE_TEST_ASSERT(arr16[i] == i);
131 /* Test popBack(). */
132 for (i = 0; i < 1000; i++)
134 DE_TEST_ASSERT(arr.popBack() == (4999 - i));
135 DE_TEST_ASSERT(arr16.popBack() == (4999 - i));
138 DE_TEST_ASSERT(arr.size() == 4000);
139 DE_TEST_ASSERT(arr16.size() == 4000);
140 for (i = 0; i < 4000; i++)
142 DE_TEST_ASSERT(arr[i] == i);
143 DE_TEST_ASSERT(arr16[i] == i);
149 for (i = 1000; i < 5000; i++)
152 arr16.pushBack((deInt16)i);
155 DE_TEST_ASSERT(arr.size() == 5000);
156 DE_TEST_ASSERT(arr16.size() == 5000);
157 for (i = 0; i < 5000; i++)
159 DE_TEST_ASSERT(arr[i] == i);
160 DE_TEST_ASSERT(arr16[i] == i);
164 arr.resize(100, -123);
165 DE_TEST_ASSERT(arr.size() == 100);
166 for (i = 0; i < 100; i++)
167 DE_TEST_ASSERT(arr[i] == -123);
169 /* Test set() and pushBack() with reserve(). */
170 PoolArray<int, 32> arr2(&pool);
173 for (i = 0; i < 1500; i++)
175 for (; i < 5000; i++)
178 DE_TEST_ASSERT(arr2.size() == 5000);
179 for (i = 0; i < 5000; i++)
182 DE_TEST_ASSERT(val == i);
197 RefCount (int* count)
203 RefCount (const RefCount& other)
204 : m_count(other.m_count)
216 RefCount& operator= (const RefCount& other)
224 m_count = other.m_count;
238 static void sideEffectTest (void)
241 PoolArray<RefCount> arr (&pool);
243 RefCount counter (&count);
245 DE_TEST_ASSERT(count == 1);
247 for (int i = 0; i < 127; i++)
248 arr.pushBack(counter);
250 DE_TEST_ASSERT(count == 128);
252 for (int i = 0; i < 10; i++)
255 DE_TEST_ASSERT(count == 118);
258 DE_TEST_ASSERT(count == 118);
261 DE_TEST_ASSERT(count == 19);
264 DE_TEST_ASSERT(count == 19);
267 DE_TEST_ASSERT(count == 1);
270 static void iteratorTest (void)
273 PoolArray<int> arr (&pool);
275 for (int ndx = 0; ndx < 128; ndx++)
280 const PoolArray<int>& cRef = arr;
282 for (PoolArray<int>::ConstIterator iter = cRef.begin(); iter != cRef.end(); iter++, ndx++)
284 DE_TEST_ASSERT(*iter == ndx);
287 // Cast & interop with non-const array.
289 for (PoolArray<int>::ConstIterator iter = arr.begin(); iter != arr.end(); iter++, ndx++)
291 DE_TEST_ASSERT(*iter == ndx);
296 DE_TEST_ASSERT(arr.end()-arr.begin() == 128);
297 DE_TEST_ASSERT(*(arr.begin()+3) == 3);
298 DE_TEST_ASSERT(arr.begin()[4] == 4);
301 DE_TEST_ASSERT(arr.begin() != arr.begin()+1);
302 DE_TEST_ASSERT(arr.begin() == arr.begin());
303 DE_TEST_ASSERT(arr.begin() != arr.end());
304 DE_TEST_ASSERT(arr.begin() < arr.end());
305 DE_TEST_ASSERT(arr.begin() < arr.begin()+1);
306 DE_TEST_ASSERT(arr.begin() <= arr.begin());
307 DE_TEST_ASSERT(arr.end() > arr.begin());
308 DE_TEST_ASSERT(arr.begin() >= arr.begin());
310 // Compatibility with stl.
311 DE_TEST_ASSERT(std::distance(arr.begin(), arr.end()) == 128);
313 std::vector<int> vecCopy(arr.size());
314 std::copy(arr.begin(), arr.end(), vecCopy.begin());
315 for (int ndx = 0; ndx < (int)vecCopy.size(); ndx++)
316 DE_TEST_ASSERT(vecCopy[ndx] == ndx);
318 std::fill(arr.begin(), arr.end(), -1);
319 for (int ndx = 0; ndx < (int)arr.size(); ndx++)
320 DE_TEST_ASSERT(arr[ndx] == -1);
322 std::copy(vecCopy.begin(), vecCopy.end(), arr.begin());
323 for (int ndx = 0; ndx < (int)arr.size(); ndx++)
324 DE_TEST_ASSERT(arr[ndx] == ndx);
329 for (PoolArray<int>::Iterator iter = arr.begin(); iter != arr.end(); iter++, ndx++)
331 DE_TEST_ASSERT(*iter == ndx);
339 DE_TEST_ASSERT(arr[4] == 0);
340 DE_TEST_ASSERT(arr[6] == 1);
343 void PoolArray_selfTest (void)
346 alignedIntArrayTest();