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 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> initializerList{1, 2, 3, 4, 5};
36 EXPECT_EQ(5u, initializerList.size());
37 EXPECT_EQ(3, initializerList[2]);
38
39 FastVector<int, 5> assignCopy(copyRValue);
40 EXPECT_EQ(3u, assignCopy.size());
41
42 FastVector<int, 5> assignRValue(std::move(assignCopy));
43 EXPECT_EQ(3u, assignRValue.size());
44
45 FastVector<int, 5> assignmentInitializerList = {1, 2, 3, 4, 5};
46 EXPECT_EQ(5u, assignmentInitializerList.size());
47 EXPECT_EQ(3, assignmentInitializerList[2]);
48 }
49
50 // Test indexing operations (at, operator[])
TEST(FastVector,Indexing)51 TEST(FastVector, Indexing)
52 {
53 FastVector<int, 5> vec = {0, 1, 2, 3, 4};
54 for (int i = 0; i < 5; ++i)
55 {
56 EXPECT_EQ(i, vec.at(i));
57 EXPECT_EQ(vec[i], vec.at(i));
58 }
59 }
60
61 // Test the push_back functions
TEST(FastVector,PushBack)62 TEST(FastVector, PushBack)
63 {
64 FastVector<int, 5> vec;
65 vec.push_back(1);
66 EXPECT_EQ(1, vec[0]);
67 vec.push_back(1);
68 vec.push_back(1);
69 vec.push_back(1);
70 vec.push_back(1);
71 EXPECT_EQ(5u, vec.size());
72 }
73
74 // Tests growing the fast vector beyond the fixed storage.
TEST(FastVector,Growth)75 TEST(FastVector, Growth)
76 {
77 constexpr size_t kSize = 4;
78 FastVector<size_t, kSize> vec;
79
80 for (size_t i = 0; i < kSize * 2; ++i)
81 {
82 vec.push_back(i);
83 }
84
85 EXPECT_EQ(kSize * 2, vec.size());
86
87 for (size_t i = kSize * 2; i > 0; --i)
88 {
89 ASSERT_EQ(vec.back(), i - 1);
90 vec.pop_back();
91 }
92
93 EXPECT_EQ(0u, vec.size());
94 }
95
96 // Test the pop_back function
TEST(FastVector,PopBack)97 TEST(FastVector, PopBack)
98 {
99 FastVector<int, 5> vec;
100 vec.push_back(1);
101 EXPECT_EQ(1, (int)vec.size());
102 vec.pop_back();
103 EXPECT_EQ(0, (int)vec.size());
104 }
105
106 // Test the back function
TEST(FastVector,Back)107 TEST(FastVector, Back)
108 {
109 FastVector<int, 5> vec;
110 vec.push_back(1);
111 vec.push_back(2);
112 EXPECT_EQ(2, vec.back());
113 }
114
115 // Test the back function
TEST(FastVector,Front)116 TEST(FastVector, Front)
117 {
118 FastVector<int, 5> vec;
119 vec.push_back(1);
120 vec.push_back(2);
121 EXPECT_EQ(1, vec.front());
122 }
123
124 // Test the sizing operations
TEST(FastVector,Size)125 TEST(FastVector, Size)
126 {
127 FastVector<int, 5> vec;
128 EXPECT_TRUE(vec.empty());
129 EXPECT_EQ(0u, vec.size());
130
131 vec.push_back(1);
132 EXPECT_FALSE(vec.empty());
133 EXPECT_EQ(1u, vec.size());
134 }
135
136 // Test clearing the vector
TEST(FastVector,Clear)137 TEST(FastVector, Clear)
138 {
139 FastVector<int, 5> vec = {0, 1, 2, 3, 4};
140 vec.clear();
141 EXPECT_TRUE(vec.empty());
142 }
143
144 // Test clearing the vector larger than the fixed size.
TEST(FastVector,ClearWithLargerThanFixedSize)145 TEST(FastVector, ClearWithLargerThanFixedSize)
146 {
147 FastVector<int, 3> vec = {0, 1, 2, 3, 4};
148 vec.clear();
149 EXPECT_TRUE(vec.empty());
150 }
151
152 // Test resizing the vector
TEST(FastVector,Resize)153 TEST(FastVector, Resize)
154 {
155 FastVector<int, 5> vec;
156 vec.resize(5u, 1);
157 EXPECT_EQ(5u, vec.size());
158 for (int i : vec)
159 {
160 EXPECT_EQ(1, i);
161 }
162
163 vec.resize(2u);
164 EXPECT_EQ(2u, vec.size());
165 for (int i : vec)
166 {
167 EXPECT_EQ(1, i);
168 }
169
170 // Resize to larger than minimum
171 vec.resize(10u, 2);
172 EXPECT_EQ(10u, vec.size());
173
174 for (size_t index = 0; index < 2u; ++index)
175 {
176 EXPECT_EQ(1, vec[index]);
177 }
178 for (size_t index = 2u; index < 10u; ++index)
179 {
180 EXPECT_EQ(2, vec[index]);
181 }
182
183 // Resize back to smaller
184 vec.resize(2u, 2);
185 EXPECT_EQ(2u, vec.size());
186 }
187
188 // Test iterating over the vector
TEST(FastVector,Iteration)189 TEST(FastVector, Iteration)
190 {
191 FastVector<int, 5> vec = {0, 1, 2, 3};
192
193 int vistedCount = 0;
194 for (int value : vec)
195 {
196 EXPECT_EQ(vistedCount, value);
197 vistedCount++;
198 }
199 EXPECT_EQ(4, vistedCount);
200 }
201
202 // Tests that equality comparisons work even if reserved size differs.
TEST(FastVector,EqualityWithDifferentReservedSizes)203 TEST(FastVector, EqualityWithDifferentReservedSizes)
204 {
205 FastVector<int, 3> vec1 = {1, 2, 3, 4, 5};
206 FastVector<int, 5> vec2 = {1, 2, 3, 4, 5};
207 EXPECT_EQ(vec1, vec2);
208 vec2.push_back(6);
209 EXPECT_NE(vec1, vec2);
210 }
211
212 // Tests vector operations with a non copyable type.
TEST(FastVector,NonCopyable)213 TEST(FastVector, NonCopyable)
214 {
215 struct s : angle::NonCopyable
216 {
217 s() : x(0) {}
218 s(int xin) : x(xin) {}
219 s(s &&other) : x(other.x) {}
220 s &operator=(s &&other)
221 {
222 x = other.x;
223 return *this;
224 }
225 int x;
226 };
227
228 FastVector<s, 3> vec;
229 vec.push_back(3);
230 EXPECT_EQ(3, vec[0].x);
231
232 FastVector<s, 3> copy = std::move(vec);
233 EXPECT_EQ(1u, copy.size());
234 EXPECT_EQ(3, copy[0].x);
235 }
236 } // namespace angle
237