Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/angle
Path: blob/main_old/src/common/FastVector_unittest.cpp
1693 views
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
17
TEST(FastVector, Constructors)
18
{
19
FastVector<int, 5> defaultContructor;
20
EXPECT_EQ(0u, defaultContructor.size());
21
22
FastVector<int, 5> count(3);
23
EXPECT_EQ(3u, count.size());
24
25
FastVector<int, 5> countAndValue(3, 2);
26
EXPECT_EQ(3u, countAndValue.size());
27
EXPECT_EQ(2, countAndValue[1]);
28
29
FastVector<int, 5> copy(countAndValue);
30
EXPECT_EQ(copy, countAndValue);
31
32
FastVector<int, 5> copyRValue(std::move(count));
33
EXPECT_EQ(3u, copyRValue.size());
34
35
FastVector<int, 5> copyIter(countAndValue.begin(), countAndValue.end());
36
EXPECT_EQ(copyIter, countAndValue);
37
38
FastVector<int, 5> copyIterEmpty(countAndValue.begin(), countAndValue.begin());
39
EXPECT_TRUE(copyIterEmpty.empty());
40
41
FastVector<int, 5> initializerList{1, 2, 3, 4, 5};
42
EXPECT_EQ(5u, initializerList.size());
43
EXPECT_EQ(3, initializerList[2]);
44
45
FastVector<int, 5> assignCopy(copyRValue);
46
EXPECT_EQ(3u, assignCopy.size());
47
48
FastVector<int, 5> assignRValue(std::move(assignCopy));
49
EXPECT_EQ(3u, assignRValue.size());
50
51
FastVector<int, 5> assignmentInitializerList = {1, 2, 3, 4, 5};
52
EXPECT_EQ(5u, assignmentInitializerList.size());
53
EXPECT_EQ(3, assignmentInitializerList[2]);
54
}
55
56
// Test indexing operations (at, operator[])
57
TEST(FastVector, Indexing)
58
{
59
FastVector<int, 5> vec = {0, 1, 2, 3, 4};
60
for (int i = 0; i < 5; ++i)
61
{
62
EXPECT_EQ(i, vec.at(i));
63
EXPECT_EQ(vec[i], vec.at(i));
64
}
65
}
66
67
// Test the push_back functions
68
TEST(FastVector, PushBack)
69
{
70
FastVector<int, 5> vec;
71
vec.push_back(1);
72
EXPECT_EQ(1, vec[0]);
73
vec.push_back(1);
74
vec.push_back(1);
75
vec.push_back(1);
76
vec.push_back(1);
77
EXPECT_EQ(5u, vec.size());
78
}
79
80
// Tests growing the fast vector beyond the fixed storage.
81
TEST(FastVector, Growth)
82
{
83
constexpr size_t kSize = 4;
84
FastVector<size_t, kSize> vec;
85
86
for (size_t i = 0; i < kSize * 2; ++i)
87
{
88
vec.push_back(i);
89
}
90
91
EXPECT_EQ(kSize * 2, vec.size());
92
93
for (size_t i = kSize * 2; i > 0; --i)
94
{
95
ASSERT_EQ(vec.back(), i - 1);
96
vec.pop_back();
97
}
98
99
EXPECT_EQ(0u, vec.size());
100
}
101
102
// Test the pop_back function
103
TEST(FastVector, PopBack)
104
{
105
FastVector<int, 5> vec;
106
vec.push_back(1);
107
EXPECT_EQ(1, (int)vec.size());
108
vec.pop_back();
109
EXPECT_EQ(0, (int)vec.size());
110
}
111
112
// Test the back function
113
TEST(FastVector, Back)
114
{
115
FastVector<int, 5> vec;
116
vec.push_back(1);
117
vec.push_back(2);
118
EXPECT_EQ(2, vec.back());
119
}
120
121
// Test the back function
122
TEST(FastVector, Front)
123
{
124
FastVector<int, 5> vec;
125
vec.push_back(1);
126
vec.push_back(2);
127
EXPECT_EQ(1, vec.front());
128
}
129
130
// Test the sizing operations
131
TEST(FastVector, Size)
132
{
133
FastVector<int, 5> vec;
134
EXPECT_TRUE(vec.empty());
135
EXPECT_EQ(0u, vec.size());
136
137
vec.push_back(1);
138
EXPECT_FALSE(vec.empty());
139
EXPECT_EQ(1u, vec.size());
140
}
141
142
// Test clearing the vector
143
TEST(FastVector, Clear)
144
{
145
FastVector<int, 5> vec = {0, 1, 2, 3, 4};
146
vec.clear();
147
EXPECT_TRUE(vec.empty());
148
}
149
150
// Test clearing the vector larger than the fixed size.
151
TEST(FastVector, ClearWithLargerThanFixedSize)
152
{
153
FastVector<int, 3> vec = {0, 1, 2, 3, 4};
154
vec.clear();
155
EXPECT_TRUE(vec.empty());
156
}
157
158
// Test resizing the vector
159
TEST(FastVector, Resize)
160
{
161
FastVector<int, 5> vec;
162
vec.resize(5u, 1);
163
EXPECT_EQ(5u, vec.size());
164
for (int i : vec)
165
{
166
EXPECT_EQ(1, i);
167
}
168
169
vec.resize(2u);
170
EXPECT_EQ(2u, vec.size());
171
for (int i : vec)
172
{
173
EXPECT_EQ(1, i);
174
}
175
176
// Resize to larger than minimum
177
vec.resize(10u, 2);
178
EXPECT_EQ(10u, vec.size());
179
180
for (size_t index = 0; index < 2u; ++index)
181
{
182
EXPECT_EQ(1, vec[index]);
183
}
184
for (size_t index = 2u; index < 10u; ++index)
185
{
186
EXPECT_EQ(2, vec[index]);
187
}
188
189
// Resize back to smaller
190
vec.resize(2u, 2);
191
EXPECT_EQ(2u, vec.size());
192
}
193
194
// Test iterating over the vector
195
TEST(FastVector, Iteration)
196
{
197
FastVector<int, 5> vec = {0, 1, 2, 3};
198
199
int vistedCount = 0;
200
for (int value : vec)
201
{
202
EXPECT_EQ(vistedCount, value);
203
vistedCount++;
204
}
205
EXPECT_EQ(4, vistedCount);
206
}
207
208
// Tests that equality comparisons work even if reserved size differs.
209
TEST(FastVector, EqualityWithDifferentReservedSizes)
210
{
211
FastVector<int, 3> vec1 = {1, 2, 3, 4, 5};
212
FastVector<int, 5> vec2 = {1, 2, 3, 4, 5};
213
EXPECT_EQ(vec1, vec2);
214
vec2.push_back(6);
215
EXPECT_NE(vec1, vec2);
216
}
217
218
// Tests vector operations with a non copyable type.
219
TEST(FastVector, NonCopyable)
220
{
221
struct s : angle::NonCopyable
222
{
223
s() : x(0) {}
224
s(int xin) : x(xin) {}
225
s(s &&other) : x(other.x) {}
226
s &operator=(s &&other)
227
{
228
x = other.x;
229
return *this;
230
}
231
int x;
232
};
233
234
FastVector<s, 3> vec;
235
vec.push_back(3);
236
EXPECT_EQ(3, vec[0].x);
237
238
FastVector<s, 3> copy = std::move(vec);
239
EXPECT_EQ(1u, copy.size());
240
EXPECT_EQ(3, copy[0].x);
241
}
242
243
// Basic functionality for FastUnorderedMap
244
TEST(FastUnorderedMap, BasicUsage)
245
{
246
FastUnorderedMap<int, bool, 3> testMap;
247
EXPECT_TRUE(testMap.empty());
248
EXPECT_EQ(testMap.size(), 0u);
249
250
testMap.insert(5, true);
251
EXPECT_TRUE(testMap.contains(5));
252
EXPECT_EQ(testMap.size(), 1u);
253
254
bool value = false;
255
EXPECT_TRUE(testMap.get(5, &value));
256
EXPECT_TRUE(value);
257
EXPECT_FALSE(testMap.get(6, &value));
258
259
EXPECT_FALSE(testMap.empty());
260
testMap.clear();
261
EXPECT_TRUE(testMap.empty());
262
EXPECT_EQ(testMap.size(), 0u);
263
264
for (int i = 0; i < 10; ++i)
265
{
266
testMap.insert(i, false);
267
}
268
269
EXPECT_FALSE(testMap.empty());
270
EXPECT_EQ(testMap.size(), 10u);
271
272
for (int i = 0; i < 10; ++i)
273
{
274
EXPECT_TRUE(testMap.contains(i));
275
EXPECT_TRUE(testMap.get(i, &value));
276
EXPECT_FALSE(value);
277
}
278
}
279
280
// Basic functionality for FastUnorderedSet
281
TEST(FastUnorderedSet, BasicUsage)
282
{
283
FastUnorderedSet<int, 3> testMap;
284
EXPECT_TRUE(testMap.empty());
285
286
testMap.insert(5);
287
EXPECT_TRUE(testMap.contains(5));
288
EXPECT_FALSE(testMap.contains(6));
289
EXPECT_FALSE(testMap.empty());
290
291
testMap.clear();
292
EXPECT_TRUE(testMap.empty());
293
294
for (int i = 0; i < 10; ++i)
295
{
296
testMap.insert(i);
297
}
298
299
for (int i = 0; i < 10; ++i)
300
{
301
EXPECT_TRUE(testMap.contains(i));
302
}
303
}
304
305
// Basic functionality for FastIntegerSet
306
TEST(FastIntegerSet, BasicUsage)
307
{
308
FastIntegerSet testMap;
309
EXPECT_TRUE(testMap.empty());
310
311
testMap.insert(5);
312
EXPECT_TRUE(testMap.contains(5));
313
EXPECT_FALSE(testMap.contains(6));
314
EXPECT_FALSE(testMap.empty());
315
316
testMap.clear();
317
EXPECT_TRUE(testMap.empty());
318
319
for (int i = 0; i < 10; ++i)
320
{
321
testMap.insert(i);
322
}
323
324
for (int i = 0; i < 10; ++i)
325
{
326
EXPECT_TRUE(testMap.contains(i));
327
}
328
}
329
330
// Basic functionality for FastIntegerMap
331
TEST(FastIntegerMap, BasicUsage)
332
{
333
using KeyValuePair = std::pair<int, std::string>;
334
std::set<KeyValuePair> entries = {KeyValuePair(17, "testing"), KeyValuePair(63, "fast"),
335
KeyValuePair(97, "integer"), KeyValuePair(256, "map")};
336
337
FastIntegerMap<std::string> testMap;
338
EXPECT_TRUE(testMap.empty());
339
340
std::string str;
341
testMap.insert(entries.begin()->first, entries.begin()->second);
342
EXPECT_TRUE(testMap.contains(entries.begin()->first));
343
EXPECT_FALSE(testMap.contains(entries.rbegin()->first));
344
EXPECT_FALSE(testMap.empty());
345
EXPECT_EQ(testMap.size(), 1u);
346
EXPECT_TRUE(testMap.get(entries.begin()->first, &str));
347
EXPECT_EQ(entries.begin()->second, str);
348
EXPECT_FALSE(testMap.get(1, &str));
349
350
testMap.clear();
351
EXPECT_TRUE(testMap.empty());
352
EXPECT_EQ(testMap.size(), 0u);
353
354
for (KeyValuePair entry : entries)
355
{
356
testMap.insert(entry.first, entry.second);
357
}
358
EXPECT_EQ(testMap.size(), 4u);
359
360
for (KeyValuePair entry : entries)
361
{
362
std::string str;
363
EXPECT_TRUE(testMap.get(entry.first, &str));
364
EXPECT_EQ(entry.second, str);
365
}
366
367
testMap.clear();
368
EXPECT_TRUE(testMap.empty());
369
EXPECT_EQ(testMap.size(), 0u);
370
}
371
} // namespace angle
372
373