1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // FixedVector_unittest:
7 // Tests of the FastVector class
8 //
9
10 #include <gtest/gtest.h>
11
12 #include "common/FastVector.h"
13
14 namespace angle
15 {
16 // Make sure the various constructors compile and do basic checks
TEST(FastVector,Constructors)17 TEST(FastVector, Constructors)
18 {
19 FastVector<int, 5> defaultContructor;
20 EXPECT_EQ(0u, defaultContructor.size());
21
22 // Try varying initial vector sizes to test purely stack-allocated and
23 // heap-allocated vectors, and ensure they copy correctly.
24 size_t vectorSizes[] = {5, 3, 16, 32};
25
26 for (size_t i = 0; i < sizeof(vectorSizes) / sizeof(vectorSizes[0]); i++)
27 {
28 FastVector<int, 5> count(vectorSizes[i]);
29 EXPECT_EQ(vectorSizes[i], count.size());
30
31 FastVector<int, 5> countAndValue(vectorSizes[i], 2);
32 EXPECT_EQ(vectorSizes[i], countAndValue.size());
33 EXPECT_EQ(2, countAndValue[1]);
34
35 FastVector<int, 5> copy(countAndValue);
36 EXPECT_EQ(copy, countAndValue);
37
38 FastVector<int, 5> copyRValue(std::move(count));
39 EXPECT_EQ(vectorSizes[i], copyRValue.size());
40
41 FastVector<int, 5> copyIter(countAndValue.begin(), countAndValue.end());
42 EXPECT_EQ(copyIter, countAndValue);
43
44 FastVector<int, 5> copyIterEmpty(countAndValue.begin(), countAndValue.begin());
45 EXPECT_TRUE(copyIterEmpty.empty());
46
47 FastVector<int, 5> assignCopy(copyRValue);
48 EXPECT_EQ(vectorSizes[i], assignCopy.size());
49
50 FastVector<int, 5> assignRValue(std::move(assignCopy));
51 EXPECT_EQ(vectorSizes[i], assignRValue.size());
52 }
53
54 FastVector<int, 5> initializerList{1, 2, 3, 4, 5};
55 EXPECT_EQ(5u, initializerList.size());
56 EXPECT_EQ(3, initializerList[2]);
57
58 // Larger than stack-allocated vector size
59 FastVector<int, 5> initializerListHeap{1, 2, 3, 4, 5, 6, 7, 8};
60 EXPECT_EQ(8u, initializerListHeap.size());
61 EXPECT_EQ(3, initializerListHeap[2]);
62
63 FastVector<int, 5> assignmentInitializerList = {1, 2, 3, 4, 5};
64 EXPECT_EQ(5u, assignmentInitializerList.size());
65 EXPECT_EQ(3, assignmentInitializerList[2]);
66
67 // Larger than stack-allocated vector size
68 FastVector<int, 5> assignmentInitializerListLarge = {1, 2, 3, 4, 5, 6, 7, 8};
69 EXPECT_EQ(8u, assignmentInitializerListLarge.size());
70 EXPECT_EQ(3, assignmentInitializerListLarge[2]);
71 }
72
73 // Test indexing operations (at, operator[])
TEST(FastVector,Indexing)74 TEST(FastVector, Indexing)
75 {
76 FastVector<int, 5> vec = {0, 1, 2, 3, 4};
77 for (int i = 0; i < 5; ++i)
78 {
79 EXPECT_EQ(i, vec.at(i));
80 EXPECT_EQ(vec[i], vec.at(i));
81 }
82 }
83
84 // Test the push_back functions
TEST(FastVector,PushBack)85 TEST(FastVector, PushBack)
86 {
87 FastVector<int, 5> vec;
88 vec.push_back(1);
89 EXPECT_EQ(1, vec[0]);
90 vec.push_back(1);
91 vec.push_back(1);
92 vec.push_back(1);
93 vec.push_back(1);
94 EXPECT_EQ(5u, vec.size());
95 }
96
97 // Tests growing the fast vector beyond the fixed storage.
TEST(FastVector,Growth)98 TEST(FastVector, Growth)
99 {
100 constexpr size_t kSize = 4;
101 FastVector<size_t, kSize> vec;
102
103 for (size_t i = 0; i < kSize * 2; ++i)
104 {
105 vec.push_back(i);
106 }
107
108 EXPECT_EQ(kSize * 2, vec.size());
109
110 for (size_t i = kSize * 2; i > 0; --i)
111 {
112 ASSERT_EQ(vec.back(), i - 1);
113 vec.pop_back();
114 }
115
116 EXPECT_EQ(0u, vec.size());
117 }
118
119 // Test the pop_back function
TEST(FastVector,PopBack)120 TEST(FastVector, PopBack)
121 {
122 FastVector<int, 5> vec;
123 vec.push_back(1);
124 EXPECT_EQ(1, (int)vec.size());
125 vec.pop_back();
126 EXPECT_EQ(0, (int)vec.size());
127 }
128
129 // Test the back function
TEST(FastVector,Back)130 TEST(FastVector, Back)
131 {
132 FastVector<int, 5> vec;
133 vec.push_back(1);
134 vec.push_back(2);
135 EXPECT_EQ(2, vec.back());
136 }
137
138 // Test the back function
TEST(FastVector,Front)139 TEST(FastVector, Front)
140 {
141 FastVector<int, 5> vec;
142 vec.push_back(1);
143 vec.push_back(2);
144 EXPECT_EQ(1, vec.front());
145 }
146
147 // Test the sizing operations
TEST(FastVector,Size)148 TEST(FastVector, Size)
149 {
150 FastVector<int, 5> vec;
151 EXPECT_TRUE(vec.empty());
152 EXPECT_EQ(0u, vec.size());
153
154 vec.push_back(1);
155 EXPECT_FALSE(vec.empty());
156 EXPECT_EQ(1u, vec.size());
157 }
158
159 // Test clearing the vector
TEST(FastVector,Clear)160 TEST(FastVector, Clear)
161 {
162 FastVector<int, 5> vec = {0, 1, 2, 3, 4};
163 vec.clear();
164 EXPECT_TRUE(vec.empty());
165 }
166
167 // Test clearing the vector larger than the fixed size.
TEST(FastVector,ClearWithLargerThanFixedSize)168 TEST(FastVector, ClearWithLargerThanFixedSize)
169 {
170 FastVector<int, 3> vec = {0, 1, 2, 3, 4};
171 vec.clear();
172 EXPECT_TRUE(vec.empty());
173 }
174
175 // Test resizing the vector
TEST(FastVector,Resize)176 TEST(FastVector, Resize)
177 {
178 FastVector<int, 5> vec;
179 vec.resize(5u, 1);
180 EXPECT_EQ(5u, vec.size());
181 for (int i : vec)
182 {
183 EXPECT_EQ(1, i);
184 }
185
186 vec.resize(2u);
187 EXPECT_EQ(2u, vec.size());
188 for (int i : vec)
189 {
190 EXPECT_EQ(1, i);
191 }
192
193 // Resize to larger than minimum
194 vec.resize(10u, 2);
195 EXPECT_EQ(10u, vec.size());
196
197 for (size_t index = 0; index < 2u; ++index)
198 {
199 EXPECT_EQ(1, vec[index]);
200 }
201 for (size_t index = 2u; index < 10u; ++index)
202 {
203 EXPECT_EQ(2, vec[index]);
204 }
205
206 // Resize back to smaller
207 vec.resize(2u, 2);
208 EXPECT_EQ(2u, vec.size());
209 }
210
211 // Test resetWithRawData on the vector
TEST(FastVector,resetWithRawData)212 TEST(FastVector, resetWithRawData)
213 {
214 FastVector<int, 5> vec;
215 int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
216
217 vec.resetWithRawData(9, reinterpret_cast<uint8_t *>(&data[0]));
218 EXPECT_EQ(9u, vec.size());
219 for (size_t i = 0; i < vec.size(); i++)
220 {
221 EXPECT_EQ(vec[i], data[i]);
222 }
223
224 vec.resetWithRawData(4, reinterpret_cast<uint8_t *>(&data[0]));
225 EXPECT_EQ(4u, vec.size());
226 for (size_t i = 0; i < vec.size(); i++)
227 {
228 EXPECT_EQ(vec[i], data[i]);
229 }
230 }
231
232 // Test iterating over the vector
TEST(FastVector,Iteration)233 TEST(FastVector, Iteration)
234 {
235 FastVector<int, 5> vec = {0, 1, 2, 3};
236
237 int vistedCount = 0;
238 for (int value : vec)
239 {
240 EXPECT_EQ(vistedCount, value);
241 vistedCount++;
242 }
243 EXPECT_EQ(4, vistedCount);
244 }
245
246 // Tests that equality comparisons work even if reserved size differs.
TEST(FastVector,EqualityWithDifferentReservedSizes)247 TEST(FastVector, EqualityWithDifferentReservedSizes)
248 {
249 FastVector<int, 3> vec1 = {1, 2, 3, 4, 5};
250 FastVector<int, 5> vec2 = {1, 2, 3, 4, 5};
251 EXPECT_EQ(vec1, vec2);
252 vec2.push_back(6);
253 EXPECT_NE(vec1, vec2);
254 }
255
256 // Tests vector operations with a non copyable type.
TEST(FastVector,NonCopyable)257 TEST(FastVector, NonCopyable)
258 {
259 struct s : angle::NonCopyable
260 {
261 s() : x(0) {}
262 s(int xin) : x(xin) {}
263 s(s &&other) : x(other.x) {}
264 s &operator=(s &&other)
265 {
266 x = other.x;
267 return *this;
268 }
269 int x;
270 };
271
272 FastVector<s, 3> vec;
273 vec.push_back(3);
274 EXPECT_EQ(3, vec[0].x);
275
276 FastVector<s, 3> copy = std::move(vec);
277 EXPECT_EQ(1u, copy.size());
278 EXPECT_EQ(3, copy[0].x);
279 }
280
281 // Basic functionality for FlatUnorderedMap
TEST(FlatUnorderedMap,BasicUsage)282 TEST(FlatUnorderedMap, BasicUsage)
283 {
284 FlatUnorderedMap<int, bool, 3> testMap;
285 EXPECT_TRUE(testMap.empty());
286 EXPECT_EQ(testMap.size(), 0u);
287
288 testMap.insert(5, true);
289 EXPECT_TRUE(testMap.contains(5));
290 EXPECT_EQ(testMap.size(), 1u);
291
292 bool value = false;
293 EXPECT_TRUE(testMap.get(5, &value));
294 EXPECT_TRUE(value);
295 EXPECT_FALSE(testMap.get(6, &value));
296
297 EXPECT_FALSE(testMap.empty());
298 testMap.clear();
299 EXPECT_TRUE(testMap.empty());
300 EXPECT_EQ(testMap.size(), 0u);
301
302 for (int i = 0; i < 10; ++i)
303 {
304 testMap.insert(i, false);
305 }
306
307 EXPECT_FALSE(testMap.empty());
308 EXPECT_EQ(testMap.size(), 10u);
309
310 for (int i = 0; i < 10; ++i)
311 {
312 EXPECT_TRUE(testMap.contains(i));
313 EXPECT_TRUE(testMap.get(i, &value));
314 EXPECT_FALSE(value);
315 }
316 }
317
318 // Basic functionality for FlatUnorderedSet
TEST(FlatUnorderedSet,BasicUsage)319 TEST(FlatUnorderedSet, BasicUsage)
320 {
321 FlatUnorderedSet<int, 3> testMap;
322 EXPECT_TRUE(testMap.empty());
323
324 testMap.insert(5);
325 EXPECT_TRUE(testMap.contains(5));
326 EXPECT_FALSE(testMap.contains(6));
327 EXPECT_FALSE(testMap.empty());
328
329 testMap.clear();
330 EXPECT_TRUE(testMap.empty());
331
332 for (int i = 0; i < 10; ++i)
333 {
334 testMap.insert(i);
335 }
336
337 for (int i = 0; i < 10; ++i)
338 {
339 EXPECT_TRUE(testMap.contains(i));
340 }
341 }
342
343 // Comparison of FlatUnorderedSet
TEST(FlatUnorderedSet,Comparison)344 TEST(FlatUnorderedSet, Comparison)
345 {
346 FlatUnorderedSet<int, 3> testSet0;
347 FlatUnorderedSet<int, 3> testSet1;
348 EXPECT_TRUE(testSet0.empty());
349 EXPECT_TRUE(testSet1.empty());
350
351 testSet0.insert(5);
352 EXPECT_FALSE(testSet0 == testSet1);
353
354 testSet0.insert(10);
355 EXPECT_FALSE(testSet0 == testSet1);
356
357 testSet1.insert(5);
358 EXPECT_FALSE(testSet0 == testSet1);
359
360 testSet1.insert(15);
361 EXPECT_FALSE(testSet0 == testSet1);
362
363 testSet1.clear();
364 testSet1.insert(5);
365 testSet1.insert(10);
366 EXPECT_TRUE(testSet0 == testSet1);
367 }
368
369 // Basic usage tests of fast map.
TEST(FastMap,Basic)370 TEST(FastMap, Basic)
371 {
372 FastMap<int, 5> testMap;
373 EXPECT_TRUE(testMap.empty());
374
375 testMap[5] = 5;
376 EXPECT_FALSE(testMap.empty());
377
378 testMap.clear();
379 EXPECT_TRUE(testMap.empty());
380
381 for (int i = 0; i < 10; ++i)
382 {
383 testMap[i] = i;
384 }
385
386 for (int i = 0; i < 10; ++i)
387 {
388 EXPECT_TRUE(testMap[i] == i);
389 }
390 }
391 } // namespace angle
392