1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "gtest/gtest.h"
18 
19 #include "chre/util/fixed_size_vector.h"
20 
21 using chre::FixedSizeVector;
22 
23 namespace {
24 constexpr int kMaxTestCapacity = 10;
25 int destructor_count[kMaxTestCapacity];
26 
27 class Foo {
28  public:
~Foo()29   ~Foo() {
30     if (mValue >= 0) {
31       destructor_count[mValue]++;
32     }
33   };
setValue(int value)34   void setValue(int value) {
35     mValue = value;
36   }
37 
38  private:
39   int mValue = -1;
40 };
41 }
42 
TEST(FixedSizeVector,EmptyWithCapacityWithDefault)43 TEST(FixedSizeVector, EmptyWithCapacityWithDefault) {
44   FixedSizeVector<int, 8> testVector;
45   ASSERT_NE(testVector.data(), nullptr);
46   ASSERT_EQ(testVector.size(), 0);
47   ASSERT_EQ(testVector.capacity(), 8);
48   ASSERT_TRUE(testVector.empty());
49   ASSERT_FALSE(testVector.full());
50 }
51 
TEST(FixedSizeVector,PushBackOneAndRead)52 TEST(FixedSizeVector, PushBackOneAndRead) {
53   FixedSizeVector<int, 8> testVector;
54   testVector.push_back(0x1337);
55   ASSERT_NE(testVector.data(), nullptr);
56   ASSERT_EQ(testVector.size(), 1);
57   ASSERT_EQ(testVector.capacity(), 8);
58   ASSERT_FALSE(testVector.empty());
59   ASSERT_FALSE(testVector.full());
60   ASSERT_EQ(testVector[0], 0x1337);
61 }
62 
TEST(FixedSizeVector,PushBackUntilFullAndRead)63 TEST(FixedSizeVector, PushBackUntilFullAndRead) {
64   FixedSizeVector<int, 4> testVector;
65   testVector.push_back(1000);
66   testVector.push_back(2000);
67   testVector.push_back(3000);
68   testVector.push_back(4000);
69 
70   ASSERT_NE(testVector.data(), nullptr);
71   ASSERT_TRUE(testVector.full());
72   ASSERT_FALSE(testVector.empty());
73   ASSERT_EQ(testVector.size(), 4);
74   ASSERT_EQ(testVector[0], 1000);
75   ASSERT_EQ(testVector[1], 2000);
76   ASSERT_EQ(testVector[2], 3000);
77   ASSERT_EQ(testVector[3], 4000);
78 
79   ASSERT_EQ(testVector.data()[0], 1000);
80   ASSERT_EQ(testVector.data()[1], 2000);
81   ASSERT_EQ(testVector.data()[2], 3000);
82   ASSERT_EQ(testVector.data()[3], 4000);
83 }
84 
TEST(FixedSizeVector,PushBackAndErase)85 TEST(FixedSizeVector, PushBackAndErase) {
86   FixedSizeVector<int, 8> vector;
87   vector.push_back(0x1337);
88   vector.push_back(0xcafe);
89   vector.push_back(0xbeef);
90   vector.push_back(0xface);
91 
92   vector.erase(1);
93   ASSERT_EQ(vector[0], 0x1337);
94   ASSERT_EQ(vector.data()[0], 0x1337);
95   ASSERT_EQ(vector[1], 0xbeef);
96   ASSERT_EQ(vector.data()[1], 0xbeef);
97   ASSERT_EQ(vector[2], 0xface);
98   ASSERT_EQ(vector.data()[2], 0xface);
99   ASSERT_EQ(vector.size(), 3);
100 }
101 
TEST(FixedSizeVector,EraseDestructorCalled)102 TEST(FixedSizeVector, EraseDestructorCalled) {
103   FixedSizeVector<Foo, 4> vector;
104   for (size_t i = 0; i < 4; ++i) {
105     vector.push_back(Foo());
106     vector[i].setValue(i);
107   }
108 
109   // last item before erase is '3'.
110   vector.erase(1);
111   EXPECT_EQ(0, destructor_count[0]);
112   EXPECT_EQ(0, destructor_count[1]);
113   EXPECT_EQ(0, destructor_count[2]);
114   EXPECT_EQ(1, destructor_count[3]);
115 
116   // last item before erase is still '3'.
117   vector.erase(2);
118   EXPECT_EQ(0, destructor_count[0]);
119   EXPECT_EQ(0, destructor_count[1]);
120   EXPECT_EQ(0, destructor_count[2]);
121   EXPECT_EQ(2, destructor_count[3]);
122 
123   // last item before erase is now '2'.
124   vector.erase(0);
125   EXPECT_EQ(0, destructor_count[0]);
126   EXPECT_EQ(0, destructor_count[1]);
127   EXPECT_EQ(1, destructor_count[2]);
128   EXPECT_EQ(2, destructor_count[3]);
129 }
130 
TEST(FixedSizeVectorDeathTest,SwapWithInvalidIndex)131 TEST(FixedSizeVectorDeathTest, SwapWithInvalidIndex) {
132   FixedSizeVector<int, 4> vector;
133   vector.push_back(0x1337);
134   vector.push_back(0xcafe);
135   EXPECT_DEATH(vector.swap(0, 2), "");
136 }
137 
TEST(FixedSizeVectorDeathTest,SwapWithInvalidIndices)138 TEST(FixedSizeVectorDeathTest, SwapWithInvalidIndices) {
139   FixedSizeVector<int, 4> vector;
140   vector.push_back(0x1337);
141   vector.push_back(0xcafe);
142   EXPECT_DEATH(vector.swap(2, 3), "");
143 }
144 
TEST(FixedSizeVector,Swap)145 TEST(FixedSizeVector, Swap) {
146   FixedSizeVector<int, 4> vector;
147   vector.push_back(0x1337);
148   vector.push_back(0xcafe);
149 
150   vector.swap(0, 1);
151   EXPECT_EQ(vector[0], 0xcafe);
152   EXPECT_EQ(vector[1], 0x1337);
153 }
154 
TEST(FixedSizeVector,ResizeLarger)155 TEST(FixedSizeVector, ResizeLarger) {
156   FixedSizeVector<int, 4> vector;
157   vector.resize(4);
158   EXPECT_EQ(vector.size(), 4);
159 }
160 
TEST(FixedSizeVector,ResizeSmaller)161 TEST(FixedSizeVector, ResizeSmaller) {
162   destructor_count[0] = 0;
163 
164   FixedSizeVector<Foo, 4> vector;
165   for (size_t i = 0; i < 3; i++) {
166     vector.push_back(Foo());
167     vector[i].setValue(0);
168   }
169 
170   EXPECT_EQ(vector.size(), 3);
171   EXPECT_EQ(destructor_count[0], 0);
172   vector.resize(2);
173   EXPECT_EQ(vector.size(), 2);
174   EXPECT_EQ(destructor_count[0], 1);
175 }
176 
TEST(FixedSizeVector,Iterator)177 TEST(FixedSizeVector, Iterator) {
178   FixedSizeVector<int, 8> vector;
179   vector.push_back(0);
180   vector.push_back(1);
181   vector.push_back(2);
182 
183   size_t index = 0;
184   for (FixedSizeVector<int, 8>::iterator it = vector.begin();
185        it != vector.end(); ++it) {
186     EXPECT_EQ(vector[index++], *it);
187   }
188 
189   FixedSizeVector<int, 8>::iterator it = vector.begin() + vector.size() - 1;
190   EXPECT_EQ(vector[vector.size() - 1], *it);
191 
192   it = vector.begin() + vector.size();
193   EXPECT_TRUE(it == vector.end());
194 }
195 
TEST(FixedSizeVector,ConstIterator)196 TEST(FixedSizeVector, ConstIterator) {
197   FixedSizeVector<int, 8> vector;
198   vector.push_back(0);
199   vector.push_back(1);
200   vector.push_back(2);
201 
202   size_t index = 0;
203   for (FixedSizeVector<int, 8>::const_iterator cit = vector.cbegin();
204        cit != vector.cend(); ++cit) {
205     EXPECT_EQ(vector[index++], *cit);
206   }
207 
208   FixedSizeVector<int, 8>::const_iterator cit =
209       vector.cbegin() + vector.size() - 1;
210   EXPECT_EQ(vector[vector.size() - 1], *cit);
211 
212   cit = vector.cbegin() + vector.size();
213   EXPECT_TRUE(cit == vector.cend());
214 }
215 
TEST(FixedSizeVector,IteratorAndPushBack)216 TEST(FixedSizeVector, IteratorAndPushBack) {
217   FixedSizeVector<int, 8> vector;
218   vector.push_back(0);
219   vector.push_back(1);
220   vector.push_back(2);
221 
222   FixedSizeVector<int, 8>::iterator it_b = vector.begin();
223   FixedSizeVector<int, 8>::iterator it_e = vector.end();
224 
225   vector.push_back(3);
226 
227   size_t index = 0;
228   while (it_b != it_e) {
229     EXPECT_EQ(vector[index++], *it_b++);
230   }
231 }
232 
TEST(FixedSizeVector,IteratorAndEmplaceBack)233 TEST(FixedSizeVector, IteratorAndEmplaceBack) {
234   FixedSizeVector<int, 8> vector;
235   vector.push_back(0);
236   vector.push_back(1);
237   vector.push_back(2);
238 
239   FixedSizeVector<int, 8>::iterator it_b = vector.begin();
240   FixedSizeVector<int, 8>::iterator it_e = vector.end();
241 
242   vector.emplace_back(3);
243 
244   size_t index = 0;
245   while (it_b != it_e) {
246     EXPECT_EQ(vector[index++], *it_b++);
247   }
248 }
249 
TEST(FixedSizeVector,IteratorAndErase)250 TEST(FixedSizeVector, IteratorAndErase) {
251   FixedSizeVector<int, 8> vector;
252   vector.push_back(0);
253   vector.push_back(1);
254   vector.push_back(2);
255 
256   FixedSizeVector<int, 8>::iterator it_b = vector.begin();
257 
258   vector.erase(2);
259 
260   size_t index = 0;
261   while (index < 2) {
262     EXPECT_EQ(vector[index++], *it_b++);
263   }
264 }
265 
TEST(FixedSizeVector,IteratorAndSwap)266 TEST(FixedSizeVector, IteratorAndSwap) {
267   FixedSizeVector<int, 8> vector;
268   vector.push_back(0);
269   vector.push_back(1);
270   vector.push_back(2);
271   vector.push_back(3);
272 
273   FixedSizeVector<int, 8>::iterator it_b = vector.begin();
274 
275   vector.swap(1, 3);
276 
277   size_t index = 0;
278   while (index < 4) {
279     if (index != 1 && index != 3) {
280       EXPECT_EQ(vector[index], *it_b);
281     }
282     index++;
283     it_b++;
284   }
285 }
286 
TEST(FixedSizeVector,IteratorAndResize)287 TEST(FixedSizeVector, IteratorAndResize) {
288   FixedSizeVector<int, 8> vector;
289   vector.push_back(0);
290   vector.push_back(1);
291   vector.push_back(2);
292   vector.push_back(3);
293 
294   FixedSizeVector<int, 8>::iterator it_b = vector.begin();
295 
296   vector.resize(2);
297 
298   size_t index = 0;
299   while (index < 2) {
300     EXPECT_EQ(vector[index++], *it_b++);
301   }
302 }
303