• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef GTEST
20 #include "chre/platform/linux/expect_assert.h"
21 #endif
22 
23 #include "chre/platform/assert.h"
24 #include "chre/util/dynamic_vector.h"
25 #include "chre/util/macros.h"
26 
27 #include <stdint.h>
28 
29 using chre::DynamicVector;
30 
31 namespace {
32 constexpr int kMaxTestCapacity = 10;
33 int gDestructorCount[kMaxTestCapacity];
34 
35 class DestructorCounter {
36  public:
~DestructorCounter()37   ~DestructorCounter() {
38     if (mValue >= 0) {
39       gDestructorCount[mValue]++;
40     }
41   };
setValue(int value)42   void setValue(int value) {
43     mValue = value;
44   }
getValue()45   int getValue() {
46     return mValue;
47   }
48 
49  private:
50   int mValue = -1;
51 };
52 
resetDestructorCounts()53 void resetDestructorCounts() {
54   for (size_t i = 0; i < ARRAY_SIZE(gDestructorCount); i++) {
55     gDestructorCount[i] = 0;
56   }
57 }
58 }  // namespace
59 
TEST(DynamicVector,EmptyByDefault)60 TEST(DynamicVector, EmptyByDefault) {
61   DynamicVector<int> vector;
62   EXPECT_EQ(vector.data(), nullptr);
63   EXPECT_TRUE(vector.empty());
64   EXPECT_EQ(vector.size(), 0);
65   EXPECT_EQ(vector.capacity(), 0);
66   vector.clear();
67 }
68 
TEST(DynamicVector,PushBackAndRead)69 TEST(DynamicVector, PushBackAndRead) {
70   DynamicVector<int> vector;
71   ASSERT_TRUE(vector.push_back(0x1337));
72   EXPECT_EQ(vector.size(), 1);
73   EXPECT_EQ(vector.capacity(), 1);
74   EXPECT_EQ(vector.data(), &vector[0]);
75   EXPECT_FALSE(vector.empty());
76   EXPECT_EQ(vector[0], 0x1337);
77 }
78 
TEST(DynamicVector,PushBackReserveAndReadTrivialType)79 TEST(DynamicVector, PushBackReserveAndReadTrivialType) {
80   DynamicVector<int> vector;
81   ASSERT_TRUE(vector.emplace_back(0x1337));
82   ASSERT_TRUE(vector.push_back(0xface));
83   int x = 0xcafe;
84   ASSERT_TRUE(vector.push_back(std::move(x)));
85   ASSERT_TRUE(vector.insert(vector.size(), 0xd00d));
86   EXPECT_EQ(vector.size(), 4);
87   EXPECT_EQ(vector.capacity(), 4);
88   EXPECT_EQ(vector[0], 0x1337);
89   EXPECT_EQ(vector[1], 0xface);
90   EXPECT_EQ(vector[2], 0xcafe);
91   EXPECT_EQ(vector[3], 0xd00d);
92 
93   ASSERT_TRUE(vector.reserve(8));
94   EXPECT_EQ(vector.size(), 4);
95   EXPECT_EQ(vector.capacity(), 8);
96   EXPECT_EQ(vector[0], 0x1337);
97   EXPECT_EQ(vector[1], 0xface);
98   EXPECT_EQ(vector[2], 0xcafe);
99   EXPECT_EQ(vector[3], 0xd00d);
100 }
101 
TEST(DynamicVector,CompareEqual)102 TEST(DynamicVector, CompareEqual) {
103   DynamicVector<int> lhs;
104   ASSERT_TRUE(lhs.push_back(0x1337));
105   ASSERT_TRUE(lhs.push_back(0xface));
106   DynamicVector<int> rhs;
107   ASSERT_TRUE(rhs.push_back(0x1337));
108   ASSERT_TRUE(rhs.push_back(0xface));
109 
110   ASSERT_EQ(lhs, rhs);  // equal vectors
111 
112   ASSERT_TRUE(lhs.push_back(0xb00c));
113   ASSERT_FALSE(lhs == rhs);  // different size
114 
115   ASSERT_TRUE(rhs.push_back(0xc00b));
116   ASSERT_FALSE(lhs == rhs);  // equal size different elements
117 }
118 
119 constexpr int kConstructedMagic = 0xdeadbeef;
120 
121 class MovableButNonCopyable : public chre::NonCopyable {
122  public:
MovableButNonCopyable(int value)123   MovableButNonCopyable(int value) : mValue(value) {}
124 
MovableButNonCopyable(MovableButNonCopyable && other)125   MovableButNonCopyable(MovableButNonCopyable &&other) {
126     mValue = other.mValue;
127     other.mValue = -1;
128   }
129 
operator =(MovableButNonCopyable && other)130   MovableButNonCopyable &operator=(MovableButNonCopyable &&other) {
131     CHRE_ASSERT(mMagic == kConstructedMagic);
132     mValue = other.mValue;
133     other.mValue = -1;
134     return *this;
135   }
136 
getValue() const137   int getValue() const {
138     return mValue;
139   }
140 
141  private:
142   int mMagic = kConstructedMagic;
143   int mValue;
144 };
145 
TEST(DynamicVector,PushBackReserveAndReadMovableButNonCopyable)146 TEST(DynamicVector, PushBackReserveAndReadMovableButNonCopyable) {
147   DynamicVector<MovableButNonCopyable> vector;
148   ASSERT_TRUE(vector.emplace_back(0x1337));
149   ASSERT_TRUE(vector.emplace_back(0xface));
150   MovableButNonCopyable mbnc(0xcafe);
151   ASSERT_TRUE(vector.push_back(std::move(mbnc)));
152   EXPECT_EQ(mbnc.getValue(), -1);
153   MovableButNonCopyable mbnc2(0xd00d);
154   ASSERT_TRUE(vector.insert(vector.size(), std::move(mbnc2)));
155   EXPECT_EQ(mbnc2.getValue(), -1);
156 
157   ASSERT_TRUE(vector.reserve(8));
158   EXPECT_EQ(vector[0].getValue(), 0x1337);
159   EXPECT_EQ(vector[1].getValue(), 0xface);
160   EXPECT_EQ(vector[2].getValue(), 0xcafe);
161   EXPECT_EQ(vector[3].getValue(), 0xd00d);
162   EXPECT_EQ(vector.size(), 4);
163   EXPECT_EQ(vector.capacity(), 8);
164 }
165 
166 class CopyableButNonMovable {
167  public:
CopyableButNonMovable(int value)168   CopyableButNonMovable(int value) : mValue(value) {}
169 
CopyableButNonMovable(const CopyableButNonMovable & other)170   CopyableButNonMovable(const CopyableButNonMovable &other) {
171     mValue = other.mValue;
172   }
173 
operator =(const CopyableButNonMovable & other)174   CopyableButNonMovable &operator=(const CopyableButNonMovable &other) {
175     CHRE_ASSERT(mMagic == kConstructedMagic);
176     mValue = other.mValue;
177     return *this;
178   }
179 
180   CopyableButNonMovable(CopyableButNonMovable &&other) = delete;
181   CopyableButNonMovable &operator=(CopyableButNonMovable &&other) = delete;
182 
getValue() const183   int getValue() const {
184     return mValue;
185   }
186 
187  private:
188   int mMagic = kConstructedMagic;
189   int mValue;
190 };
191 
TEST(DynamicVector,PushBackReserveAndReadCopyableButNonMovable)192 TEST(DynamicVector, PushBackReserveAndReadCopyableButNonMovable) {
193   DynamicVector<CopyableButNonMovable> vector;
194   ASSERT_TRUE(vector.emplace_back(0x1337));
195   ASSERT_TRUE(vector.emplace_back(0xface));
196   CopyableButNonMovable cbnm(0xcafe);
197   ASSERT_TRUE(vector.push_back(cbnm));
198   CopyableButNonMovable cbnm2(0xd00d);
199   ASSERT_TRUE(vector.insert(vector.size(), cbnm2));
200 
201   ASSERT_TRUE(vector.reserve(8));
202   EXPECT_EQ(vector[0].getValue(), 0x1337);
203   EXPECT_EQ(vector[1].getValue(), 0xface);
204   EXPECT_EQ(vector[2].getValue(), 0xcafe);
205   EXPECT_EQ(vector[3].getValue(), 0xd00d);
206   EXPECT_EQ(vector.size(), 4);
207   EXPECT_EQ(vector.capacity(), 8);
208 }
209 
210 class MovableAndCopyable {
211  public:
MovableAndCopyable(int value)212   MovableAndCopyable(int value) : mValue(value) {}
213 
MovableAndCopyable(const MovableAndCopyable & other)214   MovableAndCopyable(const MovableAndCopyable &other) {
215     mValue = other.mValue;
216   }
217 
MovableAndCopyable(MovableAndCopyable && other)218   MovableAndCopyable(MovableAndCopyable &&other) {
219     // The move constructor multiplies the value by 2 so that we can see that it
220     // was used
221     mValue = other.mValue * 2;
222   }
223 
operator =(const MovableAndCopyable & other)224   MovableAndCopyable &operator=(const MovableAndCopyable &other) {
225     CHRE_ASSERT(mMagic == kConstructedMagic);
226     mValue = other.mValue;
227     return *this;
228   }
229 
operator =(MovableAndCopyable && other)230   MovableAndCopyable &operator=(MovableAndCopyable &&other) {
231     assert(mMagic == kConstructedMagic);
232     mValue = other.mValue * 2;
233     other.mValue = -1;
234     return *this;
235   }
236 
getValue() const237   int getValue() const {
238     return mValue;
239   }
240 
241  private:
242   int mMagic = kConstructedMagic;
243   int mValue;
244 };
245 
TEST(DynamicVector,ReservePrefersMove)246 TEST(DynamicVector, ReservePrefersMove) {
247   // Ensure that preference is given to std::move in reserve()
248   DynamicVector<MovableAndCopyable> vector;
249 
250   // Reserve enough space for the first two elements.
251   ASSERT_TRUE(vector.reserve(2));
252   ASSERT_TRUE(vector.emplace_back(1000));
253   ASSERT_TRUE(vector.emplace_back(2000));
254 
255   // Reserve more than enough space causing a move to be required.
256   ASSERT_TRUE(vector.reserve(4));
257 
258   // Move on this type results in a multiplication by 2. Verify that all
259   // elements have been multiplied by 2.
260   EXPECT_EQ(vector[0].getValue(), 2000);
261   EXPECT_EQ(vector[1].getValue(), 4000);
262 }
263 
264 /**
265  * A simple test helper object to count number of construction and destructions.
266  */
267 class Foo {
268  public:
269   /**
270    * Construct an object storing a simple integer. Increment the number of
271    * objects that have been constructed of this type.
272    */
Foo(int value)273   Foo(int value) : value(value) {
274     sConstructedCounter++;
275   }
276 
Foo(const Foo & other)277   Foo(const Foo &other) {
278     value = other.value;
279     sConstructedCounter++;
280   }
281 
282   Foo(Foo &&other) = delete;
283 
284   /**
285    * Tear down the object, decrementing the number of objects that have been
286    * constructed of this type.
287    */
~Foo()288   ~Foo() {
289     sConstructedCounter--;
290   }
291 
292   //! The number of objects of this type that have been constructed.
293   static ssize_t sConstructedCounter;
294 
295   //! The value stored in the object to verify the contents of this object after
296   //! construction.
297   int value;
298 };
299 
300 //! Storage for the Foo reference counter.
301 ssize_t Foo::sConstructedCounter = 0;
302 
TEST(DynamicVector,EmplaceBackAndDestruct)303 TEST(DynamicVector, EmplaceBackAndDestruct) {
304   Foo::sConstructedCounter = 0;
305   {
306     DynamicVector<Foo> vector;
307     ASSERT_TRUE(vector.emplace_back(1000));
308     ASSERT_TRUE(vector.emplace_back(2000));
309     ASSERT_TRUE(vector.emplace_back(3000));
310     ASSERT_TRUE(vector.emplace_back(4000));
311 
312     ASSERT_EQ(vector[0].value, 1000);
313     ASSERT_EQ(vector[1].value, 2000);
314     ASSERT_EQ(vector[2].value, 3000);
315     ASSERT_EQ(vector[3].value, 4000);
316 
317     EXPECT_EQ(Foo::sConstructedCounter, 4);
318   }
319 
320   EXPECT_EQ(Foo::sConstructedCounter, 0);
321 }
322 
TEST(DynamicVector,InsertEmpty)323 TEST(DynamicVector, InsertEmpty) {
324   DynamicVector<int> vector;
325   EXPECT_CHRE_ASSERT(EXPECT_FALSE(vector.insert(1, 0x1337)));
326 
327   // Insert to empty vector
328   ASSERT_TRUE(vector.insert(0, 0x1337));
329   EXPECT_EQ(vector[0], 0x1337);
330 
331   // Insert at end triggering grow
332   ASSERT_EQ(vector.capacity(), 1);
333   EXPECT_TRUE(vector.insert(1, 0xface));
334   EXPECT_EQ(vector[0], 0x1337);
335   EXPECT_EQ(vector[1], 0xface);
336 
337   // Insert at beginning triggering grow
338   ASSERT_EQ(vector.capacity(), 2);
339   EXPECT_TRUE(vector.insert(0, 0xcafe));
340   EXPECT_EQ(vector[0], 0xcafe);
341   EXPECT_EQ(vector[1], 0x1337);
342   EXPECT_EQ(vector[2], 0xface);
343 
344   // Insert at middle with spare capacity
345   ASSERT_EQ(vector.capacity(), 4);
346   EXPECT_TRUE(vector.insert(1, 0xdead));
347   EXPECT_EQ(vector[0], 0xcafe);
348   EXPECT_EQ(vector[1], 0xdead);
349   EXPECT_EQ(vector[2], 0x1337);
350   EXPECT_EQ(vector[3], 0xface);
351 
352   // Insert at middle triggering grow
353   ASSERT_EQ(vector.capacity(), 4);
354   EXPECT_TRUE(vector.insert(2, 0xbeef));
355   EXPECT_EQ(vector[0], 0xcafe);
356   EXPECT_EQ(vector[1], 0xdead);
357   EXPECT_EQ(vector[2], 0xbeef);
358   EXPECT_EQ(vector[3], 0x1337);
359   EXPECT_EQ(vector[4], 0xface);
360 
361   // Insert at beginning with spare capacity
362   ASSERT_EQ(vector.capacity(), 8);
363   ASSERT_EQ(vector.size(), 5);
364   EXPECT_TRUE(vector.insert(0, 0xabad));
365   EXPECT_EQ(vector[0], 0xabad);
366   EXPECT_EQ(vector[1], 0xcafe);
367   EXPECT_EQ(vector[2], 0xdead);
368   EXPECT_EQ(vector[3], 0xbeef);
369   EXPECT_EQ(vector[4], 0x1337);
370   EXPECT_EQ(vector[5], 0xface);
371 
372   // Insert at end with spare capacity
373   ASSERT_EQ(vector.size(), 6);
374   EXPECT_TRUE(vector.insert(vector.size(), 0xc0de));
375   EXPECT_EQ(vector[0], 0xabad);
376   EXPECT_EQ(vector[1], 0xcafe);
377   EXPECT_EQ(vector[2], 0xdead);
378   EXPECT_EQ(vector[3], 0xbeef);
379   EXPECT_EQ(vector[4], 0x1337);
380   EXPECT_EQ(vector[5], 0xface);
381   EXPECT_EQ(vector[6], 0xc0de);
382 }
383 
TEST(DynamicVector,PushBackInsertInMiddleAndRead)384 TEST(DynamicVector, PushBackInsertInMiddleAndRead) {
385   DynamicVector<int> vector;
386   ASSERT_TRUE(vector.push_back(0x1337));
387   ASSERT_TRUE(vector.push_back(0xface));
388   ASSERT_TRUE(vector.push_back(0xcafe));
389   ASSERT_TRUE(vector.insert(1, 0xbeef));
390 
391   ASSERT_EQ(vector[0], 0x1337);
392   ASSERT_EQ(vector[1], 0xbeef);
393   ASSERT_EQ(vector[2], 0xface);
394   ASSERT_EQ(vector[3], 0xcafe);
395 }
396 
TEST(DynamicVector,PushBackAndErase)397 TEST(DynamicVector, PushBackAndErase) {
398   DynamicVector<int> vector;
399   ASSERT_TRUE(vector.push_back(0x1337));
400   ASSERT_TRUE(vector.push_back(0xcafe));
401   ASSERT_TRUE(vector.push_back(0xbeef));
402   ASSERT_TRUE(vector.push_back(0xface));
403 
404   vector.erase(1);
405 
406   ASSERT_EQ(vector[0], 0x1337);
407   ASSERT_EQ(vector[1], 0xbeef);
408   ASSERT_EQ(vector[2], 0xface);
409   ASSERT_EQ(vector.size(), 3);
410 }
411 
TEST(DynamicVector,FindEmpty)412 TEST(DynamicVector, FindEmpty) {
413   DynamicVector<int> vector;
414   ASSERT_EQ(vector.find(0), 0);
415 }
416 
TEST(DynamicVector,FindWithElements)417 TEST(DynamicVector, FindWithElements) {
418   DynamicVector<int> vector;
419   ASSERT_TRUE(vector.push_back(0x1337));
420   ASSERT_TRUE(vector.push_back(0xcafe));
421   ASSERT_TRUE(vector.push_back(0xbeef));
422 
423   ASSERT_EQ(vector.find(0x1337), 0);
424   ASSERT_EQ(vector.find(0xcafe), 1);
425   ASSERT_EQ(vector.find(0xbeef), 2);
426   ASSERT_EQ(vector.find(1000), 3);
427 }
428 
TEST(DynamicVector,EraseDestructorCalled)429 TEST(DynamicVector, EraseDestructorCalled) {
430   resetDestructorCounts();
431 
432   DynamicVector<DestructorCounter> vector;
433   vector.reserve(4);
434   for (size_t i = 0; i < 4; ++i) {
435     vector.emplace_back();
436     vector[i].setValue(i);
437   }
438 
439   // last item before erase is '3'.
440   vector.erase(1);
441   EXPECT_EQ(0, gDestructorCount[0]);
442   EXPECT_EQ(0, gDestructorCount[1]);
443   EXPECT_EQ(0, gDestructorCount[2]);
444   EXPECT_EQ(1, gDestructorCount[3]);
445 
446   // last item before erase is still '3'.
447   vector.erase(2);
448   EXPECT_EQ(0, gDestructorCount[0]);
449   EXPECT_EQ(0, gDestructorCount[1]);
450   EXPECT_EQ(0, gDestructorCount[2]);
451   EXPECT_EQ(2, gDestructorCount[3]);
452 
453   // last item before erase is now '2'.
454   vector.erase(0);
455   EXPECT_EQ(0, gDestructorCount[0]);
456   EXPECT_EQ(0, gDestructorCount[1]);
457   EXPECT_EQ(1, gDestructorCount[2]);
458   EXPECT_EQ(2, gDestructorCount[3]);
459 }
460 
TEST(DynamicVector,Clear)461 TEST(DynamicVector, Clear) {
462   resetDestructorCounts();
463 
464   DynamicVector<DestructorCounter> vector;
465   vector.reserve(4);
466   for (size_t i = 0; i < 4; ++i) {
467     vector.emplace_back();
468     vector[i].setValue(i);
469   }
470 
471   vector.clear();
472   EXPECT_EQ(vector.size(), 0);
473   EXPECT_EQ(vector.capacity(), 4);
474 
475   for (size_t i = 0; i < 4; ++i) {
476     EXPECT_EQ(gDestructorCount[i], 1);
477   }
478 }
479 
TEST(DynamicVectorDeathTest,SwapWithInvalidIndex)480 TEST(DynamicVectorDeathTest, SwapWithInvalidIndex) {
481   DynamicVector<int> vector;
482   vector.push_back(0x1337);
483   vector.push_back(0xcafe);
484   EXPECT_DEATH(vector.swap(0, 2), "");
485 }
486 
TEST(DynamicVectorDeathTest,SwapWithInvalidIndices)487 TEST(DynamicVectorDeathTest, SwapWithInvalidIndices) {
488   DynamicVector<int> vector;
489   vector.push_back(0x1337);
490   vector.push_back(0xcafe);
491   EXPECT_DEATH(vector.swap(2, 3), "");
492 }
493 
TEST(DynamicVector,Swap)494 TEST(DynamicVector, Swap) {
495   DynamicVector<int> vector;
496   vector.push_back(0x1337);
497   vector.push_back(0xcafe);
498 
499   vector.swap(0, 1);
500   EXPECT_EQ(vector[0], 0xcafe);
501   EXPECT_EQ(vector[1], 0x1337);
502 }
503 
TEST(DynamicVector,BackFront)504 TEST(DynamicVector, BackFront) {
505   DynamicVector<int> vector;
506   vector.push_back(0x1337);
507   EXPECT_EQ(vector.front(), 0x1337);
508   EXPECT_EQ(vector.back(), 0x1337);
509   vector.push_back(0xcafe);
510   EXPECT_EQ(vector.front(), 0x1337);
511   EXPECT_EQ(vector.back(), 0xcafe);
512   vector.erase(0);
513   EXPECT_EQ(vector.front(), 0xcafe);
514   EXPECT_EQ(vector.back(), 0xcafe);
515 }
516 
TEST(DynamicVector,Iterator)517 TEST(DynamicVector, Iterator) {
518   DynamicVector<int> vector;
519   vector.push_back(0);
520   vector.push_back(1);
521   vector.push_back(2);
522 
523   size_t index = 0;
524   for (DynamicVector<int>::iterator it = vector.begin(); it != vector.end();
525        ++it) {
526     EXPECT_EQ(vector[index++], *it);
527   }
528 
529   DynamicVector<int>::iterator it = vector.begin() + vector.size() - 1;
530   EXPECT_EQ(vector[vector.size() - 1], *it);
531 
532   it = vector.begin() + vector.size();
533   EXPECT_TRUE(it == vector.end());
534 }
535 
TEST(DynamicVector,ConstIterator)536 TEST(DynamicVector, ConstIterator) {
537   DynamicVector<int> vector;
538   vector.push_back(0);
539   vector.push_back(1);
540   vector.push_back(2);
541 
542   size_t index = 0;
543   for (DynamicVector<int>::const_iterator cit = vector.cbegin();
544        cit != vector.cend(); ++cit) {
545     EXPECT_EQ(vector[index++], *cit);
546   }
547 
548   DynamicVector<int>::const_iterator cit = vector.cbegin() + vector.size() - 1;
549   EXPECT_EQ(vector[vector.size() - 1], *cit);
550 
551   cit = vector.cbegin() + vector.size();
552   EXPECT_TRUE(cit == vector.cend());
553 }
554 
TEST(DynamicVector,IteratorAndPushBack)555 TEST(DynamicVector, IteratorAndPushBack) {
556   DynamicVector<int> vector;
557   vector.push_back(0);
558   vector.push_back(1);
559   vector.push_back(2);
560   size_t oldCapacity = vector.capacity();
561 
562   DynamicVector<int>::iterator it_b = vector.begin();
563   DynamicVector<int>::iterator it_e = vector.end();
564 
565   vector.push_back(3);
566   ASSERT_TRUE(oldCapacity == vector.capacity());
567 
568   size_t index = 0;
569   for (; it_b != it_e; ++it_b) {
570     EXPECT_EQ(vector[index++], *it_b);
571   }
572 }
573 
TEST(DynamicVector,IteratorAndEmplaceBack)574 TEST(DynamicVector, IteratorAndEmplaceBack) {
575   DynamicVector<int> vector;
576   vector.push_back(0);
577   vector.push_back(1);
578   vector.push_back(2);
579   size_t oldCapacity = vector.capacity();
580 
581   DynamicVector<int>::iterator it_b = vector.begin();
582   DynamicVector<int>::iterator it_e = vector.end();
583 
584   vector.emplace_back(3);
585   ASSERT_TRUE(oldCapacity == vector.capacity());
586 
587   size_t index = 0;
588   for (; it_b != it_e; ++it_b) {
589     EXPECT_EQ(vector[index++], *it_b);
590   }
591 }
592 
TEST(DynamicVector,IteratorAndReserve)593 TEST(DynamicVector, IteratorAndReserve) {
594   DynamicVector<int> vector;
595   vector.push_back(0);
596   vector.push_back(1);
597   vector.push_back(2);
598   size_t oldCapacity = vector.capacity();
599 
600   DynamicVector<int>::iterator it_b = vector.begin();
601   DynamicVector<int>::iterator it_e = vector.end();
602 
603   vector.reserve(oldCapacity);
604   ASSERT_TRUE(oldCapacity == vector.capacity());
605 
606   size_t index = 0;
607   for (; it_b != it_e; ++it_b) {
608     EXPECT_EQ(vector[index++], *it_b);
609   }
610 }
611 
TEST(DynamicVector,IteratorAndInsert)612 TEST(DynamicVector, IteratorAndInsert) {
613   DynamicVector<int> vector;
614   vector.push_back(0);
615   vector.push_back(1);
616   vector.push_back(2);
617   size_t oldCapacity = vector.capacity();
618 
619   DynamicVector<int>::iterator it_b = vector.begin();
620 
621   vector.insert(2, 3);
622   ASSERT_TRUE(oldCapacity == vector.capacity());
623 
624   size_t index = 0;
625   while (index < 2) {
626     EXPECT_EQ(vector[index++], *it_b++);
627   }
628 }
629 
TEST(DynamicVector,IteratorAndErase)630 TEST(DynamicVector, IteratorAndErase) {
631   DynamicVector<int> vector;
632   vector.push_back(0);
633   vector.push_back(1);
634   vector.push_back(2);
635 
636   DynamicVector<int>::iterator it_b = vector.begin();
637 
638   vector.erase(2);
639 
640   size_t index = 0;
641   while (index < 2) {
642     EXPECT_EQ(vector[index++], *it_b++);
643   }
644 }
645 
TEST(DynamicVector,IteratorAndSwap)646 TEST(DynamicVector, IteratorAndSwap) {
647   DynamicVector<int> vector;
648   vector.push_back(0);
649   vector.push_back(1);
650   vector.push_back(2);
651   vector.push_back(3);
652 
653   DynamicVector<int>::iterator it_b = vector.begin();
654 
655   vector.swap(1, 3);
656 
657   size_t index = 0;
658   while (index < 4) {
659     if (index != 1 && index != 3) {
660       EXPECT_EQ(vector[index], *it_b);
661     }
662     index++;
663     it_b++;
664   }
665 }
666 
TEST(DynamicVector,MoveConstruct)667 TEST(DynamicVector, MoveConstruct) {
668   DynamicVector<int> vector;
669   ASSERT_TRUE(vector.push_back(0));
670   ASSERT_TRUE(vector.push_back(1));
671   ASSERT_TRUE(vector.push_back(2));
672 
673   DynamicVector<int> movedVector(std::move(vector));
674   EXPECT_EQ(vector.data(), nullptr);
675   EXPECT_NE(movedVector.data(), nullptr);
676   EXPECT_EQ(vector.size(), 0);
677   EXPECT_EQ(movedVector.size(), 3);
678   EXPECT_EQ(vector.capacity(), 0);
679   EXPECT_EQ(movedVector.capacity(), 4);
680 }
681 
TEST(DynamicVector,MoveAssignmentConstruct)682 TEST(DynamicVector, MoveAssignmentConstruct) {
683   DynamicVector<int> vector;
684   ASSERT_TRUE(vector.push_back(0));
685   ASSERT_TRUE(vector.push_back(1));
686   ASSERT_TRUE(vector.push_back(2));
687 
688   DynamicVector<int> movedVector;
689   movedVector = std::move(vector);
690   EXPECT_EQ(vector.data(), nullptr);
691   EXPECT_NE(movedVector.data(), nullptr);
692   EXPECT_EQ(vector.size(), 0);
693   EXPECT_EQ(movedVector.size(), 3);
694   EXPECT_EQ(vector.capacity(), 0);
695   EXPECT_EQ(movedVector.capacity(), 4);
696 }
697 
TEST(DynamicVector,PrepareForPush)698 TEST(DynamicVector, PrepareForPush) {
699   DynamicVector<int> vector;
700   EXPECT_EQ(vector.size(), 0);
701   EXPECT_EQ(vector.capacity(), 0);
702 
703   // Perform an initial prepareForPush operation which causes a size of one.
704   ASSERT_TRUE(vector.prepareForPush());
705   EXPECT_EQ(vector.size(), 0);
706   EXPECT_EQ(vector.capacity(), 1);
707   ASSERT_TRUE(vector.push_back(0xcafe));
708   EXPECT_EQ(vector.size(), 1);
709   EXPECT_EQ(vector.capacity(), 1);
710 
711   // Verify that it becomes larger
712   ASSERT_TRUE(vector.prepareForPush());
713   EXPECT_EQ(vector[0], 0xcafe);
714   EXPECT_EQ(vector.size(), 1);
715   EXPECT_EQ(vector.capacity(), 2);
716 
717   // The vector should not become any larger than necessary.
718   ASSERT_TRUE(vector.prepareForPush());
719   EXPECT_EQ(vector[0], 0xcafe);
720   EXPECT_EQ(vector.size(), 1);
721   EXPECT_EQ(vector.capacity(), 2);
722 }
723 
724 // TODO: Add a test for when memory allocation returns nullptr.
725 
TEST(DynamicVector,PopBack)726 TEST(DynamicVector, PopBack) {
727   DynamicVector<int> vector;
728   constexpr size_t kSize = 4;
729   for (int i = 0; i < kSize; i++) {
730     vector.push_back(i);
731   }
732 
733   for (int i = kSize - 1; i >= 0; i--) {
734     EXPECT_EQ(vector.back(), i);
735     vector.pop_back();
736   }
737   EXPECT_TRUE(vector.empty());
738 }
739 
740 /**
741  * A test class to default construct an integer with an incrementing value.
742  */
743 struct FancyInt {
744   static int index;
745   int value;
746 
FancyIntFancyInt747   FancyInt() : value(index++) {}
748 };
749 
750 int FancyInt::index = 0;
751 
TEST(DynamicVector,Resize)752 TEST(DynamicVector, Resize) {
753   DynamicVector<FancyInt> vector;
754   ASSERT_TRUE(vector.resize(4));
755   ASSERT_EQ(vector.size(), 4);
756 
757   EXPECT_EQ(vector[0].value, 0);
758   EXPECT_EQ(vector[1].value, 1);
759   EXPECT_EQ(vector[2].value, 2);
760   EXPECT_EQ(vector[3].value, 3);
761 
762   ASSERT_TRUE(vector.resize(2));
763   ASSERT_EQ(vector.size(), 2);
764 
765   EXPECT_EQ(vector[0].value, 0);
766   EXPECT_EQ(vector[1].value, 1);
767 
768   ASSERT_TRUE(vector.resize(4));
769   ASSERT_EQ(vector.size(), 4);
770 
771   EXPECT_EQ(vector[0].value, 0);
772   EXPECT_EQ(vector[1].value, 1);
773   EXPECT_EQ(vector[2].value, 4);
774   EXPECT_EQ(vector[3].value, 5);
775 
776   // Reset index for future tests
777   FancyInt::index = 0;
778 }
779