blob: 7fd71f5eb067af7fd10739e2f8817392286c86c2 [file] [log] [blame]
Bill Wendling3d9fbee2009-01-11 01:25:51 +00001//===- llvm/unittest/ADT/SmallVectorTest.cpp ------------------------------===//
Bill Wendlingf2850d92009-01-10 12:56:31 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// SmallVector unit tests.
11//
12//===----------------------------------------------------------------------===//
13
14#include "gtest/gtest.h"
15#include "llvm/ADT/SmallVector.h"
Bill Wendlingd4008502010-08-19 18:52:02 +000016#include "llvm/Support/Compiler.h"
Bill Wendlingf2850d92009-01-10 12:56:31 +000017#include <stdarg.h>
Dan Gohmana7a33fd2010-03-26 18:53:37 +000018#include <list>
Bill Wendlingf2850d92009-01-10 12:56:31 +000019
20using namespace llvm;
21
22namespace {
23
24/// A helper class that counts the total number of constructor and
25/// destructor calls.
26class Constructable {
27private:
28 static int numConstructorCalls;
29 static int numDestructorCalls;
30 static int numAssignmentCalls;
31
32 int value;
33
34public:
35 Constructable() : value(0) {
36 ++numConstructorCalls;
37 }
Owen Anderson9cbd7af2011-07-06 22:36:59 +000038
Bill Wendlingf2850d92009-01-10 12:56:31 +000039 Constructable(int val) : value(val) {
40 ++numConstructorCalls;
41 }
Owen Anderson9cbd7af2011-07-06 22:36:59 +000042
Bill Wendlingf2850d92009-01-10 12:56:31 +000043 Constructable(const Constructable & src) {
44 value = src.value;
45 ++numConstructorCalls;
46 }
Owen Anderson9cbd7af2011-07-06 22:36:59 +000047
Bill Wendlingf2850d92009-01-10 12:56:31 +000048 ~Constructable() {
49 ++numDestructorCalls;
50 }
Owen Anderson9cbd7af2011-07-06 22:36:59 +000051
Bill Wendlingf2850d92009-01-10 12:56:31 +000052 Constructable & operator=(const Constructable & src) {
53 value = src.value;
54 ++numAssignmentCalls;
55 return *this;
56 }
Owen Anderson9cbd7af2011-07-06 22:36:59 +000057
Bill Wendlingf2850d92009-01-10 12:56:31 +000058 int getValue() const {
59 return abs(value);
60 }
61
62 static void reset() {
63 numConstructorCalls = 0;
64 numDestructorCalls = 0;
65 numAssignmentCalls = 0;
66 }
Owen Anderson9cbd7af2011-07-06 22:36:59 +000067
Bill Wendlingf2850d92009-01-10 12:56:31 +000068 static int getNumConstructorCalls() {
69 return numConstructorCalls;
70 }
71
72 static int getNumDestructorCalls() {
73 return numDestructorCalls;
74 }
75
76 friend bool operator==(const Constructable & c0, const Constructable & c1) {
77 return c0.getValue() == c1.getValue();
78 }
79
Chandler Carruth100c2672010-10-23 08:10:43 +000080 friend bool LLVM_ATTRIBUTE_UNUSED
Bill Wendlingd4008502010-08-19 18:52:02 +000081 operator!=(const Constructable & c0, const Constructable & c1) {
Bill Wendlingf2850d92009-01-10 12:56:31 +000082 return c0.getValue() != c1.getValue();
83 }
84};
85
86int Constructable::numConstructorCalls;
87int Constructable::numDestructorCalls;
88int Constructable::numAssignmentCalls;
89
90// Test fixture class
Chandler Carruthba1f5802012-07-30 22:17:52 +000091template <typename VectorT>
Bill Wendlingf2850d92009-01-10 12:56:31 +000092class SmallVectorTest : public testing::Test {
93protected:
Chandler Carruthba1f5802012-07-30 22:17:52 +000094 VectorT theVector;
95 VectorT otherVector;
Owen Anderson9cbd7af2011-07-06 22:36:59 +000096
Bill Wendlingf2850d92009-01-10 12:56:31 +000097 void SetUp() {
98 Constructable::reset();
99 }
100
Chandler Carruthba1f5802012-07-30 22:17:52 +0000101 void assertEmpty(VectorT & v) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000102 // Size tests
103 EXPECT_EQ(0u, v.size());
104 EXPECT_TRUE(v.empty());
105
106 // Iterator tests
107 EXPECT_TRUE(v.begin() == v.end());
108 }
109
110 // Assert that theVector contains the specified values, in order.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000111 void assertValuesInOrder(VectorT & v, size_t size, ...) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000112 EXPECT_EQ(size, v.size());
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000113
Bill Wendlingf2850d92009-01-10 12:56:31 +0000114 va_list ap;
115 va_start(ap, size);
116 for (size_t i = 0; i < size; ++i) {
117 int value = va_arg(ap, int);
118 EXPECT_EQ(value, v[i].getValue());
119 }
120
121 va_end(ap);
122 }
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000123
Bill Wendlingf2850d92009-01-10 12:56:31 +0000124 // Generate a sequence of values to initialize the vector.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000125 void makeSequence(VectorT & v, int start, int end) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000126 for (int i = start; i <= end; ++i) {
127 v.push_back(Constructable(i));
128 }
129 }
130};
131
Chandler Carruthba1f5802012-07-30 22:17:52 +0000132typedef ::testing::Types<SmallVector<Constructable, 0>,
133 SmallVector<Constructable, 1>,
134 SmallVector<Constructable, 2>,
135 SmallVector<Constructable, 4>
136 > SmallVectorTestTypes;
137TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes);
138
Bill Wendlingf2850d92009-01-10 12:56:31 +0000139// New vector test.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000140TYPED_TEST(SmallVectorTest, EmptyVectorTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000141 SCOPED_TRACE("EmptyVectorTest");
Chandler Carruthba1f5802012-07-30 22:17:52 +0000142 this->assertEmpty(this->theVector);
143 EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend());
Bill Wendlingf2850d92009-01-10 12:56:31 +0000144 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
145 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
146}
147
148// Simple insertions and deletions.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000149TYPED_TEST(SmallVectorTest, PushPopTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000150 SCOPED_TRACE("PushPopTest");
151
Chandler Carruthba1f5802012-07-30 22:17:52 +0000152 // Track whether the vector will potentially have to grow.
153 bool RequiresGrowth = this->theVector.capacity() < 3;
154
Bill Wendlingf2850d92009-01-10 12:56:31 +0000155 // Push an element
Chandler Carruthba1f5802012-07-30 22:17:52 +0000156 this->theVector.push_back(Constructable(1));
Bill Wendlingf2850d92009-01-10 12:56:31 +0000157
158 // Size tests
Chandler Carruthba1f5802012-07-30 22:17:52 +0000159 this->assertValuesInOrder(this->theVector, 1u, 1);
160 EXPECT_FALSE(this->theVector.begin() == this->theVector.end());
161 EXPECT_FALSE(this->theVector.empty());
Bill Wendlingf2850d92009-01-10 12:56:31 +0000162
163 // Push another element
Chandler Carruthba1f5802012-07-30 22:17:52 +0000164 this->theVector.push_back(Constructable(2));
165 this->assertValuesInOrder(this->theVector, 2u, 1, 2);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000166
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000167 // Insert at beginning
Chandler Carruthba1f5802012-07-30 22:17:52 +0000168 this->theVector.insert(this->theVector.begin(), this->theVector[1]);
169 this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2);
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000170
Bill Wendlingf2850d92009-01-10 12:56:31 +0000171 // Pop one element
Chandler Carruthba1f5802012-07-30 22:17:52 +0000172 this->theVector.pop_back();
173 this->assertValuesInOrder(this->theVector, 2u, 2, 1);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000174
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000175 // Pop remaining elements
Chandler Carruthba1f5802012-07-30 22:17:52 +0000176 this->theVector.pop_back();
177 this->theVector.pop_back();
178 this->assertEmpty(this->theVector);
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000179
Bill Wendlingf2850d92009-01-10 12:56:31 +0000180 // Check number of constructor calls. Should be 2 for each list element,
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000181 // one for the argument to push_back, one for the argument to insert,
182 // and one for the list element itself.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000183 if (!RequiresGrowth) {
184 EXPECT_EQ(5, Constructable::getNumConstructorCalls());
185 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
186 } else {
187 // If we had to grow the vector, these only have a lower bound, but should
188 // always be equal.
189 EXPECT_LE(5, Constructable::getNumConstructorCalls());
190 EXPECT_EQ(Constructable::getNumConstructorCalls(),
191 Constructable::getNumDestructorCalls());
192 }
Bill Wendlingf2850d92009-01-10 12:56:31 +0000193}
194
195// Clear test.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000196TYPED_TEST(SmallVectorTest, ClearTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000197 SCOPED_TRACE("ClearTest");
198
Chandler Carruthba1f5802012-07-30 22:17:52 +0000199 this->theVector.reserve(2);
200 this->makeSequence(this->theVector, 1, 2);
201 this->theVector.clear();
Bill Wendlingf2850d92009-01-10 12:56:31 +0000202
Chandler Carruthba1f5802012-07-30 22:17:52 +0000203 this->assertEmpty(this->theVector);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000204 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
205 EXPECT_EQ(4, Constructable::getNumDestructorCalls());
206}
207
208// Resize smaller test.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000209TYPED_TEST(SmallVectorTest, ResizeShrinkTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000210 SCOPED_TRACE("ResizeShrinkTest");
211
Chandler Carruthba1f5802012-07-30 22:17:52 +0000212 this->theVector.reserve(3);
213 this->makeSequence(this->theVector, 1, 3);
214 this->theVector.resize(1);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000215
Chandler Carruthba1f5802012-07-30 22:17:52 +0000216 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000217 EXPECT_EQ(6, Constructable::getNumConstructorCalls());
218 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
219}
220
221// Resize bigger test.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000222TYPED_TEST(SmallVectorTest, ResizeGrowTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000223 SCOPED_TRACE("ResizeGrowTest");
224
Chandler Carruthba1f5802012-07-30 22:17:52 +0000225 this->theVector.resize(2);
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000226
Daniel Dunbar614be082009-07-12 19:45:34 +0000227 // The extra constructor/destructor calls come from the temporary object used
228 // to initialize the contents of the resized array (via copy construction).
Bill Wendlingf2850d92009-01-10 12:56:31 +0000229 EXPECT_EQ(3, Constructable::getNumConstructorCalls());
230 EXPECT_EQ(1, Constructable::getNumDestructorCalls());
Chandler Carruthba1f5802012-07-30 22:17:52 +0000231 EXPECT_EQ(2u, this->theVector.size());
Bill Wendlingf2850d92009-01-10 12:56:31 +0000232}
233
234// Resize with fill value.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000235TYPED_TEST(SmallVectorTest, ResizeFillTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000236 SCOPED_TRACE("ResizeFillTest");
237
Chandler Carruthba1f5802012-07-30 22:17:52 +0000238 this->theVector.resize(3, Constructable(77));
239 this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000240}
241
242// Overflow past fixed size.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000243TYPED_TEST(SmallVectorTest, OverflowTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000244 SCOPED_TRACE("OverflowTest");
245
Daniel Dunbar614be082009-07-12 19:45:34 +0000246 // Push more elements than the fixed size.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000247 this->makeSequence(this->theVector, 1, 10);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000248
Daniel Dunbar614be082009-07-12 19:45:34 +0000249 // Test size and values.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000250 EXPECT_EQ(10u, this->theVector.size());
Bill Wendlingf2850d92009-01-10 12:56:31 +0000251 for (int i = 0; i < 10; ++i) {
Chandler Carruthba1f5802012-07-30 22:17:52 +0000252 EXPECT_EQ(i+1, this->theVector[i].getValue());
Bill Wendlingf2850d92009-01-10 12:56:31 +0000253 }
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000254
Daniel Dunbar614be082009-07-12 19:45:34 +0000255 // Now resize back to fixed size.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000256 this->theVector.resize(1);
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000257
Chandler Carruthba1f5802012-07-30 22:17:52 +0000258 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000259}
260
261// Iteration tests.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000262TYPED_TEST(SmallVectorTest, IterationTest) {
263 this->makeSequence(this->theVector, 1, 2);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000264
265 // Forward Iteration
Chandler Carruthba1f5802012-07-30 22:17:52 +0000266 typename TypeParam::iterator it = this->theVector.begin();
267 EXPECT_TRUE(*it == this->theVector.front());
268 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000269 EXPECT_EQ(1, it->getValue());
270 ++it;
Chandler Carruthba1f5802012-07-30 22:17:52 +0000271 EXPECT_TRUE(*it == this->theVector[1]);
272 EXPECT_TRUE(*it == this->theVector.back());
Bill Wendlingf2850d92009-01-10 12:56:31 +0000273 EXPECT_EQ(2, it->getValue());
274 ++it;
Chandler Carruthba1f5802012-07-30 22:17:52 +0000275 EXPECT_TRUE(it == this->theVector.end());
Bill Wendlingf2850d92009-01-10 12:56:31 +0000276 --it;
Chandler Carruthba1f5802012-07-30 22:17:52 +0000277 EXPECT_TRUE(*it == this->theVector[1]);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000278 EXPECT_EQ(2, it->getValue());
279 --it;
Chandler Carruthba1f5802012-07-30 22:17:52 +0000280 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000281 EXPECT_EQ(1, it->getValue());
282
283 // Reverse Iteration
Chandler Carruthba1f5802012-07-30 22:17:52 +0000284 typename TypeParam::reverse_iterator rit = this->theVector.rbegin();
285 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000286 EXPECT_EQ(2, rit->getValue());
287 ++rit;
Chandler Carruthba1f5802012-07-30 22:17:52 +0000288 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000289 EXPECT_EQ(1, rit->getValue());
290 ++rit;
Chandler Carruthba1f5802012-07-30 22:17:52 +0000291 EXPECT_TRUE(rit == this->theVector.rend());
Bill Wendlingf2850d92009-01-10 12:56:31 +0000292 --rit;
Chandler Carruthba1f5802012-07-30 22:17:52 +0000293 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000294 EXPECT_EQ(1, rit->getValue());
295 --rit;
Chandler Carruthba1f5802012-07-30 22:17:52 +0000296 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000297 EXPECT_EQ(2, rit->getValue());
298}
299
300// Swap test.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000301TYPED_TEST(SmallVectorTest, SwapTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000302 SCOPED_TRACE("SwapTest");
303
Chandler Carruthba1f5802012-07-30 22:17:52 +0000304 this->makeSequence(this->theVector, 1, 2);
305 std::swap(this->theVector, this->otherVector);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000306
Chandler Carruthba1f5802012-07-30 22:17:52 +0000307 this->assertEmpty(this->theVector);
308 this->assertValuesInOrder(this->otherVector, 2u, 1, 2);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000309}
310
311// Append test
Chandler Carruthba1f5802012-07-30 22:17:52 +0000312TYPED_TEST(SmallVectorTest, AppendTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000313 SCOPED_TRACE("AppendTest");
314
Chandler Carruthba1f5802012-07-30 22:17:52 +0000315 this->makeSequence(this->otherVector, 2, 3);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000316
Chandler Carruthba1f5802012-07-30 22:17:52 +0000317 this->theVector.push_back(Constructable(1));
318 this->theVector.append(this->otherVector.begin(), this->otherVector.end());
Bill Wendlingf2850d92009-01-10 12:56:31 +0000319
Chandler Carruthba1f5802012-07-30 22:17:52 +0000320 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000321}
322
323// Append repeated test
Chandler Carruthba1f5802012-07-30 22:17:52 +0000324TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000325 SCOPED_TRACE("AppendRepeatedTest");
326
Chandler Carruthba1f5802012-07-30 22:17:52 +0000327 this->theVector.push_back(Constructable(1));
328 this->theVector.append(2, Constructable(77));
329 this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000330}
331
332// Assign test
Chandler Carruthba1f5802012-07-30 22:17:52 +0000333TYPED_TEST(SmallVectorTest, AssignTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000334 SCOPED_TRACE("AssignTest");
335
Chandler Carruthba1f5802012-07-30 22:17:52 +0000336 this->theVector.push_back(Constructable(1));
337 this->theVector.assign(2, Constructable(77));
338 this->assertValuesInOrder(this->theVector, 2u, 77, 77);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000339}
340
341// Erase a single element
Chandler Carruthba1f5802012-07-30 22:17:52 +0000342TYPED_TEST(SmallVectorTest, EraseTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000343 SCOPED_TRACE("EraseTest");
344
Chandler Carruthba1f5802012-07-30 22:17:52 +0000345 this->makeSequence(this->theVector, 1, 3);
346 this->theVector.erase(this->theVector.begin());
347 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000348}
349
350// Erase a range of elements
Chandler Carruthba1f5802012-07-30 22:17:52 +0000351TYPED_TEST(SmallVectorTest, EraseRangeTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000352 SCOPED_TRACE("EraseRangeTest");
353
Chandler Carruthba1f5802012-07-30 22:17:52 +0000354 this->makeSequence(this->theVector, 1, 3);
355 this->theVector.erase(this->theVector.begin(), this->theVector.begin() + 2);
356 this->assertValuesInOrder(this->theVector, 1u, 3);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000357}
358
359// Insert a single element.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000360TYPED_TEST(SmallVectorTest, InsertTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000361 SCOPED_TRACE("InsertTest");
362
Chandler Carruthba1f5802012-07-30 22:17:52 +0000363 this->makeSequence(this->theVector, 1, 3);
364 typename TypeParam::iterator I =
365 this->theVector.insert(this->theVector.begin() + 1, Constructable(77));
366 EXPECT_EQ(this->theVector.begin() + 1, I);
367 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000368}
369
370// Insert repeated elements.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000371TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000372 SCOPED_TRACE("InsertRepeatedTest");
373
Chandler Carruthba1f5802012-07-30 22:17:52 +0000374 this->makeSequence(this->theVector, 10, 15);
375 typename TypeParam::iterator I =
376 this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
377 EXPECT_EQ(this->theVector.begin() + 1, I);
378 this->assertValuesInOrder(this->theVector, 8u,
379 10, 16, 16, 11, 12, 13, 14, 15);
Benjamin Kramer5f6c7cf2012-06-17 11:52:22 +0000380
Benjamin Kramerd45f7b62012-06-17 12:46:13 +0000381 // Insert at end.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000382 I = this->theVector.insert(this->theVector.end(), 2, Constructable(16));
383 EXPECT_EQ(this->theVector.begin() + 8, I);
384 this->assertValuesInOrder(this->theVector, 10u,
385 10, 16, 16, 11, 12, 13, 14, 15, 16, 16);
Benjamin Kramerd45f7b62012-06-17 12:46:13 +0000386
387 // Empty insert.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000388 EXPECT_EQ(this->theVector.end(),
389 this->theVector.insert(this->theVector.end(),
390 0, Constructable(42)));
391 EXPECT_EQ(this->theVector.begin() + 1,
392 this->theVector.insert(this->theVector.begin() + 1,
393 0, Constructable(42)));
Bill Wendlingf2850d92009-01-10 12:56:31 +0000394}
395
396// Insert range.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000397TYPED_TEST(SmallVectorTest, InsertRangeTest) {
Benjamin Kramerd45f7b62012-06-17 12:46:13 +0000398 SCOPED_TRACE("InsertRangeTest");
399
400 Constructable Arr[3] =
401 { Constructable(77), Constructable(77), Constructable(77) };
Bill Wendlingf2850d92009-01-10 12:56:31 +0000402
Chandler Carruthba1f5802012-07-30 22:17:52 +0000403 this->makeSequence(this->theVector, 1, 3);
404 typename TypeParam::iterator I =
405 this->theVector.insert(this->theVector.begin() + 1, Arr, Arr+3);
406 EXPECT_EQ(this->theVector.begin() + 1, I);
407 this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3);
Benjamin Kramer5f6c7cf2012-06-17 11:52:22 +0000408
Benjamin Kramerd45f7b62012-06-17 12:46:13 +0000409 // Insert at end.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000410 I = this->theVector.insert(this->theVector.end(), Arr, Arr+3);
411 EXPECT_EQ(this->theVector.begin() + 6, I);
412 this->assertValuesInOrder(this->theVector, 9u,
413 1, 77, 77, 77, 2, 3, 77, 77, 77);
Benjamin Kramerd45f7b62012-06-17 12:46:13 +0000414
415 // Empty insert.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000416 EXPECT_EQ(this->theVector.end(),
417 this->theVector.insert(this->theVector.end(),
418 this->theVector.begin(),
419 this->theVector.begin()));
420 EXPECT_EQ(this->theVector.begin() + 1,
421 this->theVector.insert(this->theVector.begin() + 1,
422 this->theVector.begin(),
423 this->theVector.begin()));
Bill Wendlingf2850d92009-01-10 12:56:31 +0000424}
425
426// Comparison tests.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000427TYPED_TEST(SmallVectorTest, ComparisonTest) {
Bill Wendlingf2850d92009-01-10 12:56:31 +0000428 SCOPED_TRACE("ComparisonTest");
429
Chandler Carruthba1f5802012-07-30 22:17:52 +0000430 this->makeSequence(this->theVector, 1, 3);
431 this->makeSequence(this->otherVector, 1, 3);
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000432
Chandler Carruthba1f5802012-07-30 22:17:52 +0000433 EXPECT_TRUE(this->theVector == this->otherVector);
434 EXPECT_FALSE(this->theVector != this->otherVector);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000435
Chandler Carruthba1f5802012-07-30 22:17:52 +0000436 this->otherVector.clear();
437 this->makeSequence(this->otherVector, 2, 4);
Owen Anderson9cbd7af2011-07-06 22:36:59 +0000438
Chandler Carruthba1f5802012-07-30 22:17:52 +0000439 EXPECT_FALSE(this->theVector == this->otherVector);
440 EXPECT_TRUE(this->theVector != this->otherVector);
Bill Wendlingf2850d92009-01-10 12:56:31 +0000441}
442
443// Constant vector tests.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000444TYPED_TEST(SmallVectorTest, ConstVectorTest) {
445 const TypeParam constVector;
Bill Wendlingf2850d92009-01-10 12:56:31 +0000446
447 EXPECT_EQ(0u, constVector.size());
448 EXPECT_TRUE(constVector.empty());
449 EXPECT_TRUE(constVector.begin() == constVector.end());
450}
451
Daniel Dunbarc2da6fb2009-08-19 17:48:28 +0000452// Direct array access.
Chandler Carruthba1f5802012-07-30 22:17:52 +0000453TYPED_TEST(SmallVectorTest, DirectVectorTest) {
454 EXPECT_EQ(0u, this->theVector.size());
455 this->theVector.reserve(4);
456 EXPECT_LE(4u, this->theVector.capacity());
Daniel Dunbarc2da6fb2009-08-19 17:48:28 +0000457 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
Chandler Carruthba1f5802012-07-30 22:17:52 +0000458 this->theVector.end()[0] = 1;
459 this->theVector.end()[1] = 2;
460 this->theVector.end()[2] = 3;
461 this->theVector.end()[3] = 4;
462 this->theVector.set_size(4);
463 EXPECT_EQ(4u, this->theVector.size());
Daniel Dunbarc2da6fb2009-08-19 17:48:28 +0000464 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
Chandler Carruthba1f5802012-07-30 22:17:52 +0000465 EXPECT_EQ(1, this->theVector[0].getValue());
466 EXPECT_EQ(2, this->theVector[1].getValue());
467 EXPECT_EQ(3, this->theVector[2].getValue());
468 EXPECT_EQ(4, this->theVector[3].getValue());
Daniel Dunbarc2da6fb2009-08-19 17:48:28 +0000469}
470
Chandler Carruthba1f5802012-07-30 22:17:52 +0000471TYPED_TEST(SmallVectorTest, IteratorTest) {
Dan Gohmana7a33fd2010-03-26 18:53:37 +0000472 std::list<int> L;
Chandler Carruthba1f5802012-07-30 22:17:52 +0000473 this->theVector.insert(this->theVector.end(), L.begin(), L.end());
Dan Gohmana7a33fd2010-03-26 18:53:37 +0000474}
475
Benjamin Kramer3703baa2012-04-29 10:53:29 +0000476struct notassignable {
477 int &x;
478 notassignable(int &x) : x(x) {}
479};
480
Chandler Carruthba1f5802012-07-30 22:17:52 +0000481TEST(SmallVectorCustomTest, NoAssignTest) {
Benjamin Kramer3703baa2012-04-29 10:53:29 +0000482 int x = 0;
483 SmallVector<notassignable, 2> vec;
484 vec.push_back(notassignable(x));
485 x = 42;
486 EXPECT_EQ(42, vec.pop_back_val().x);
487}
488
Bill Wendlingf2850d92009-01-10 12:56:31 +0000489}