blob: 9c501bbdf1a3ccc3d43217cbacb21915e654a278 [file] [log] [blame]
Bill Wendling8d069ef2009-01-11 01:25:51 +00001//===- llvm/unittest/ADT/SmallVectorTest.cpp ------------------------------===//
Bill Wendlingc56c37f2009-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
Bill Wendlingc56c37f2009-01-10 12:56:31 +000014#include "llvm/ADT/SmallVector.h"
Chandler Carruth9a67b072017-06-06 11:06:56 +000015#include "llvm/ADT/ArrayRef.h"
Bill Wendlingbfba2f12010-08-19 18:52:02 +000016#include "llvm/Support/Compiler.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000017#include "gtest/gtest.h"
Dan Gohman42e77df2010-03-26 18:53:37 +000018#include <list>
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +000019#include <stdarg.h>
Bill Wendlingc56c37f2009-01-10 12:56:31 +000020
21using namespace llvm;
22
23namespace {
24
25/// A helper class that counts the total number of constructor and
26/// destructor calls.
27class Constructable {
28private:
29 static int numConstructorCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000030 static int numMoveConstructorCalls;
31 static int numCopyConstructorCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000032 static int numDestructorCalls;
33 static int numAssignmentCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000034 static int numMoveAssignmentCalls;
35 static int numCopyAssignmentCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000036
Douglas Gregor8451cdff2014-04-30 15:49:06 +000037 bool constructed;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000038 int value;
39
40public:
Douglas Gregor8451cdff2014-04-30 15:49:06 +000041 Constructable() : constructed(true), value(0) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +000042 ++numConstructorCalls;
43 }
Owen Anderson145a2602011-07-06 22:36:59 +000044
Douglas Gregor8451cdff2014-04-30 15:49:06 +000045 Constructable(int val) : constructed(true), value(val) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +000046 ++numConstructorCalls;
47 }
Owen Anderson145a2602011-07-06 22:36:59 +000048
Douglas Gregor8451cdff2014-04-30 15:49:06 +000049 Constructable(const Constructable & src) : constructed(true) {
50 value = src.value;
51 ++numConstructorCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000052 ++numCopyConstructorCalls;
Douglas Gregor8451cdff2014-04-30 15:49:06 +000053 }
54
55 Constructable(Constructable && src) : constructed(true) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +000056 value = src.value;
57 ++numConstructorCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000058 ++numMoveConstructorCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000059 }
Owen Anderson145a2602011-07-06 22:36:59 +000060
Bill Wendlingc56c37f2009-01-10 12:56:31 +000061 ~Constructable() {
Douglas Gregor8451cdff2014-04-30 15:49:06 +000062 EXPECT_TRUE(constructed);
Bill Wendlingc56c37f2009-01-10 12:56:31 +000063 ++numDestructorCalls;
Douglas Gregor8451cdff2014-04-30 15:49:06 +000064 constructed = false;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000065 }
Owen Anderson145a2602011-07-06 22:36:59 +000066
Bill Wendlingc56c37f2009-01-10 12:56:31 +000067 Constructable & operator=(const Constructable & src) {
Douglas Gregor8451cdff2014-04-30 15:49:06 +000068 EXPECT_TRUE(constructed);
69 value = src.value;
70 ++numAssignmentCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000071 ++numCopyAssignmentCalls;
Douglas Gregor8451cdff2014-04-30 15:49:06 +000072 return *this;
73 }
74
75 Constructable & operator=(Constructable && src) {
76 EXPECT_TRUE(constructed);
Bill Wendlingc56c37f2009-01-10 12:56:31 +000077 value = src.value;
78 ++numAssignmentCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000079 ++numMoveAssignmentCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000080 return *this;
81 }
Owen Anderson145a2602011-07-06 22:36:59 +000082
Bill Wendlingc56c37f2009-01-10 12:56:31 +000083 int getValue() const {
84 return abs(value);
85 }
86
87 static void reset() {
88 numConstructorCalls = 0;
David Blaikieae8a9322014-06-08 19:12:28 +000089 numMoveConstructorCalls = 0;
90 numCopyConstructorCalls = 0;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000091 numDestructorCalls = 0;
92 numAssignmentCalls = 0;
David Blaikieae8a9322014-06-08 19:12:28 +000093 numMoveAssignmentCalls = 0;
94 numCopyAssignmentCalls = 0;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000095 }
Owen Anderson145a2602011-07-06 22:36:59 +000096
Bill Wendlingc56c37f2009-01-10 12:56:31 +000097 static int getNumConstructorCalls() {
98 return numConstructorCalls;
99 }
100
David Blaikieae8a9322014-06-08 19:12:28 +0000101 static int getNumMoveConstructorCalls() {
102 return numMoveConstructorCalls;
103 }
104
105 static int getNumCopyConstructorCalls() {
106 return numCopyConstructorCalls;
107 }
108
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000109 static int getNumDestructorCalls() {
110 return numDestructorCalls;
111 }
112
David Blaikieae8a9322014-06-08 19:12:28 +0000113 static int getNumAssignmentCalls() {
114 return numAssignmentCalls;
115 }
116
117 static int getNumMoveAssignmentCalls() {
118 return numMoveAssignmentCalls;
119 }
120
121 static int getNumCopyAssignmentCalls() {
122 return numCopyAssignmentCalls;
123 }
124
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000125 friend bool operator==(const Constructable & c0, const Constructable & c1) {
126 return c0.getValue() == c1.getValue();
127 }
128
Chandler Carruth88c54b82010-10-23 08:10:43 +0000129 friend bool LLVM_ATTRIBUTE_UNUSED
Bill Wendlingbfba2f12010-08-19 18:52:02 +0000130 operator!=(const Constructable & c0, const Constructable & c1) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000131 return c0.getValue() != c1.getValue();
132 }
133};
134
135int Constructable::numConstructorCalls;
David Blaikieae8a9322014-06-08 19:12:28 +0000136int Constructable::numCopyConstructorCalls;
137int Constructable::numMoveConstructorCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000138int Constructable::numDestructorCalls;
139int Constructable::numAssignmentCalls;
David Blaikieae8a9322014-06-08 19:12:28 +0000140int Constructable::numCopyAssignmentCalls;
141int Constructable::numMoveAssignmentCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000142
David Blaikie669fc862014-06-09 22:26:20 +0000143struct NonCopyable {
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000144 NonCopyable() {}
145 NonCopyable(NonCopyable &&) {}
David Blaikie669fc862014-06-09 22:26:20 +0000146 NonCopyable &operator=(NonCopyable &&) { return *this; }
David Blaikie11e08762014-06-11 17:50:14 +0000147private:
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000148 NonCopyable(const NonCopyable &) = delete;
149 NonCopyable &operator=(const NonCopyable &) = delete;
David Blaikie669fc862014-06-09 22:26:20 +0000150};
151
152LLVM_ATTRIBUTE_USED void CompileTest() {
153 SmallVector<NonCopyable, 0> V;
154 V.resize(42);
155}
156
Lang Hamesac92bfc2015-01-23 06:25:17 +0000157class SmallVectorTestBase : public testing::Test {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000158protected:
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000159 void SetUp() override { Constructable::reset(); }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000160
Lang Hamesac92bfc2015-01-23 06:25:17 +0000161 template <typename VectorT>
Chandler Carruth0b012612012-07-30 22:17:52 +0000162 void assertEmpty(VectorT & v) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000163 // Size tests
164 EXPECT_EQ(0u, v.size());
165 EXPECT_TRUE(v.empty());
166
167 // Iterator tests
168 EXPECT_TRUE(v.begin() == v.end());
169 }
170
Lang Hamesac92bfc2015-01-23 06:25:17 +0000171 // Assert that v contains the specified values, in order.
172 template <typename VectorT>
Chandler Carruth0b012612012-07-30 22:17:52 +0000173 void assertValuesInOrder(VectorT & v, size_t size, ...) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000174 EXPECT_EQ(size, v.size());
Owen Anderson145a2602011-07-06 22:36:59 +0000175
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000176 va_list ap;
177 va_start(ap, size);
178 for (size_t i = 0; i < size; ++i) {
179 int value = va_arg(ap, int);
180 EXPECT_EQ(value, v[i].getValue());
181 }
182
183 va_end(ap);
184 }
Owen Anderson145a2602011-07-06 22:36:59 +0000185
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000186 // Generate a sequence of values to initialize the vector.
Lang Hamesac92bfc2015-01-23 06:25:17 +0000187 template <typename VectorT>
Chandler Carruth0b012612012-07-30 22:17:52 +0000188 void makeSequence(VectorT & v, int start, int end) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000189 for (int i = start; i <= end; ++i) {
190 v.push_back(Constructable(i));
191 }
192 }
193};
194
Lang Hamesac92bfc2015-01-23 06:25:17 +0000195// Test fixture class
196template <typename VectorT>
197class SmallVectorTest : public SmallVectorTestBase {
198protected:
199 VectorT theVector;
200 VectorT otherVector;
201};
202
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000203
Chandler Carruth0b012612012-07-30 22:17:52 +0000204typedef ::testing::Types<SmallVector<Constructable, 0>,
205 SmallVector<Constructable, 1>,
206 SmallVector<Constructable, 2>,
David Blaikie18486602014-06-08 19:33:40 +0000207 SmallVector<Constructable, 4>,
208 SmallVector<Constructable, 5>
Chandler Carruth0b012612012-07-30 22:17:52 +0000209 > SmallVectorTestTypes;
210TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes);
211
Francis Riccie22b6962017-06-09 20:31:53 +0000212// Constructor test.
213TYPED_TEST(SmallVectorTest, ConstructorNonIterTest) {
214 SCOPED_TRACE("ConstructorTest");
215 this->theVector = SmallVector<Constructable, 2>(2, 2);
216 this->assertValuesInOrder(this->theVector, 2u, 2, 2);
217}
218
219// Constructor test.
220TYPED_TEST(SmallVectorTest, ConstructorIterTest) {
221 SCOPED_TRACE("ConstructorTest");
222 int arr[] = {1, 2, 3};
223 this->theVector =
224 SmallVector<Constructable, 4>(std::begin(arr), std::end(arr));
225 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
226}
227
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000228// New vector test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000229TYPED_TEST(SmallVectorTest, EmptyVectorTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000230 SCOPED_TRACE("EmptyVectorTest");
Chandler Carruth0b012612012-07-30 22:17:52 +0000231 this->assertEmpty(this->theVector);
232 EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000233 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
234 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
235}
236
237// Simple insertions and deletions.
Chandler Carruth0b012612012-07-30 22:17:52 +0000238TYPED_TEST(SmallVectorTest, PushPopTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000239 SCOPED_TRACE("PushPopTest");
240
Chandler Carruth0b012612012-07-30 22:17:52 +0000241 // Track whether the vector will potentially have to grow.
242 bool RequiresGrowth = this->theVector.capacity() < 3;
243
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000244 // Push an element
Chandler Carruth0b012612012-07-30 22:17:52 +0000245 this->theVector.push_back(Constructable(1));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000246
247 // Size tests
Chandler Carruth0b012612012-07-30 22:17:52 +0000248 this->assertValuesInOrder(this->theVector, 1u, 1);
249 EXPECT_FALSE(this->theVector.begin() == this->theVector.end());
250 EXPECT_FALSE(this->theVector.empty());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000251
252 // Push another element
Chandler Carruth0b012612012-07-30 22:17:52 +0000253 this->theVector.push_back(Constructable(2));
254 this->assertValuesInOrder(this->theVector, 2u, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000255
Owen Anderson145a2602011-07-06 22:36:59 +0000256 // Insert at beginning
Chandler Carruth0b012612012-07-30 22:17:52 +0000257 this->theVector.insert(this->theVector.begin(), this->theVector[1]);
258 this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2);
Owen Anderson145a2602011-07-06 22:36:59 +0000259
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000260 // Pop one element
Chandler Carruth0b012612012-07-30 22:17:52 +0000261 this->theVector.pop_back();
262 this->assertValuesInOrder(this->theVector, 2u, 2, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000263
Owen Anderson145a2602011-07-06 22:36:59 +0000264 // Pop remaining elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000265 this->theVector.pop_back();
266 this->theVector.pop_back();
267 this->assertEmpty(this->theVector);
Owen Anderson145a2602011-07-06 22:36:59 +0000268
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000269 // Check number of constructor calls. Should be 2 for each list element,
Owen Anderson145a2602011-07-06 22:36:59 +0000270 // one for the argument to push_back, one for the argument to insert,
271 // and one for the list element itself.
Chandler Carruth0b012612012-07-30 22:17:52 +0000272 if (!RequiresGrowth) {
273 EXPECT_EQ(5, Constructable::getNumConstructorCalls());
274 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
275 } else {
276 // If we had to grow the vector, these only have a lower bound, but should
277 // always be equal.
278 EXPECT_LE(5, Constructable::getNumConstructorCalls());
279 EXPECT_EQ(Constructable::getNumConstructorCalls(),
280 Constructable::getNumDestructorCalls());
281 }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000282}
283
284// Clear test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000285TYPED_TEST(SmallVectorTest, ClearTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000286 SCOPED_TRACE("ClearTest");
287
Chandler Carruth0b012612012-07-30 22:17:52 +0000288 this->theVector.reserve(2);
289 this->makeSequence(this->theVector, 1, 2);
290 this->theVector.clear();
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000291
Chandler Carruth0b012612012-07-30 22:17:52 +0000292 this->assertEmpty(this->theVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000293 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
294 EXPECT_EQ(4, Constructable::getNumDestructorCalls());
295}
296
297// Resize smaller test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000298TYPED_TEST(SmallVectorTest, ResizeShrinkTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000299 SCOPED_TRACE("ResizeShrinkTest");
300
Chandler Carruth0b012612012-07-30 22:17:52 +0000301 this->theVector.reserve(3);
302 this->makeSequence(this->theVector, 1, 3);
303 this->theVector.resize(1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000304
Chandler Carruth0b012612012-07-30 22:17:52 +0000305 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000306 EXPECT_EQ(6, Constructable::getNumConstructorCalls());
307 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
308}
309
310// Resize bigger test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000311TYPED_TEST(SmallVectorTest, ResizeGrowTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000312 SCOPED_TRACE("ResizeGrowTest");
313
Chandler Carruth0b012612012-07-30 22:17:52 +0000314 this->theVector.resize(2);
Owen Anderson145a2602011-07-06 22:36:59 +0000315
David Blaikie669fc862014-06-09 22:26:20 +0000316 EXPECT_EQ(2, Constructable::getNumConstructorCalls());
317 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000318 EXPECT_EQ(2u, this->theVector.size());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000319}
320
David Blaikie669fc862014-06-09 22:26:20 +0000321TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) {
322 this->theVector.resize(2);
323
324 Constructable::reset();
325
326 this->theVector.resize(4);
327
328 size_t Ctors = Constructable::getNumConstructorCalls();
329 EXPECT_TRUE(Ctors == 2 || Ctors == 4);
330 size_t MoveCtors = Constructable::getNumMoveConstructorCalls();
331 EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2);
332 size_t Dtors = Constructable::getNumDestructorCalls();
333 EXPECT_TRUE(Dtors == 0 || Dtors == 2);
334}
335
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000336// Resize with fill value.
Chandler Carruth0b012612012-07-30 22:17:52 +0000337TYPED_TEST(SmallVectorTest, ResizeFillTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000338 SCOPED_TRACE("ResizeFillTest");
339
Chandler Carruth0b012612012-07-30 22:17:52 +0000340 this->theVector.resize(3, Constructable(77));
341 this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000342}
343
344// Overflow past fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000345TYPED_TEST(SmallVectorTest, OverflowTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000346 SCOPED_TRACE("OverflowTest");
347
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000348 // Push more elements than the fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000349 this->makeSequence(this->theVector, 1, 10);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000350
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000351 // Test size and values.
Chandler Carruth0b012612012-07-30 22:17:52 +0000352 EXPECT_EQ(10u, this->theVector.size());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000353 for (int i = 0; i < 10; ++i) {
Chandler Carruth0b012612012-07-30 22:17:52 +0000354 EXPECT_EQ(i+1, this->theVector[i].getValue());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000355 }
Owen Anderson145a2602011-07-06 22:36:59 +0000356
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000357 // Now resize back to fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000358 this->theVector.resize(1);
Owen Anderson145a2602011-07-06 22:36:59 +0000359
Chandler Carruth0b012612012-07-30 22:17:52 +0000360 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000361}
362
363// Iteration tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000364TYPED_TEST(SmallVectorTest, IterationTest) {
365 this->makeSequence(this->theVector, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000366
367 // Forward Iteration
Chandler Carruth0b012612012-07-30 22:17:52 +0000368 typename TypeParam::iterator it = this->theVector.begin();
369 EXPECT_TRUE(*it == this->theVector.front());
370 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000371 EXPECT_EQ(1, it->getValue());
372 ++it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000373 EXPECT_TRUE(*it == this->theVector[1]);
374 EXPECT_TRUE(*it == this->theVector.back());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000375 EXPECT_EQ(2, it->getValue());
376 ++it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000377 EXPECT_TRUE(it == this->theVector.end());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000378 --it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000379 EXPECT_TRUE(*it == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000380 EXPECT_EQ(2, it->getValue());
381 --it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000382 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000383 EXPECT_EQ(1, it->getValue());
384
385 // Reverse Iteration
Chandler Carruth0b012612012-07-30 22:17:52 +0000386 typename TypeParam::reverse_iterator rit = this->theVector.rbegin();
387 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000388 EXPECT_EQ(2, rit->getValue());
389 ++rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000390 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000391 EXPECT_EQ(1, rit->getValue());
392 ++rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000393 EXPECT_TRUE(rit == this->theVector.rend());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000394 --rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000395 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000396 EXPECT_EQ(1, rit->getValue());
397 --rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000398 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000399 EXPECT_EQ(2, rit->getValue());
400}
401
402// Swap test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000403TYPED_TEST(SmallVectorTest, SwapTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000404 SCOPED_TRACE("SwapTest");
405
Chandler Carruth0b012612012-07-30 22:17:52 +0000406 this->makeSequence(this->theVector, 1, 2);
407 std::swap(this->theVector, this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000408
Chandler Carruth0b012612012-07-30 22:17:52 +0000409 this->assertEmpty(this->theVector);
410 this->assertValuesInOrder(this->otherVector, 2u, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000411}
412
413// Append test
Chandler Carruth0b012612012-07-30 22:17:52 +0000414TYPED_TEST(SmallVectorTest, AppendTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000415 SCOPED_TRACE("AppendTest");
416
Chandler Carruth0b012612012-07-30 22:17:52 +0000417 this->makeSequence(this->otherVector, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000418
Chandler Carruth0b012612012-07-30 22:17:52 +0000419 this->theVector.push_back(Constructable(1));
420 this->theVector.append(this->otherVector.begin(), this->otherVector.end());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000421
Chandler Carruth0b012612012-07-30 22:17:52 +0000422 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000423}
424
425// Append repeated test
Chandler Carruth0b012612012-07-30 22:17:52 +0000426TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000427 SCOPED_TRACE("AppendRepeatedTest");
428
Chandler Carruth0b012612012-07-30 22:17:52 +0000429 this->theVector.push_back(Constructable(1));
430 this->theVector.append(2, Constructable(77));
431 this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000432}
433
Francis Riccie22b6962017-06-09 20:31:53 +0000434// Append test
435TYPED_TEST(SmallVectorTest, AppendNonIterTest) {
436 SCOPED_TRACE("AppendRepeatedTest");
437
438 this->theVector.push_back(Constructable(1));
439 this->theVector.append(2, 7);
440 this->assertValuesInOrder(this->theVector, 3u, 1, 7, 7);
441}
442
Francis Ricci41b4f1a2017-06-12 14:19:25 +0000443struct output_iterator {
444 typedef std::output_iterator_tag iterator_category;
445 typedef int value_type;
446 typedef int difference_type;
447 typedef value_type *pointer;
448 typedef value_type &reference;
449 operator int() { return 2; }
450 operator Constructable() { return 7; }
451};
452
Francis Riccie22b6962017-06-09 20:31:53 +0000453TYPED_TEST(SmallVectorTest, AppendRepeatedNonForwardIterator) {
454 SCOPED_TRACE("AppendRepeatedTest");
455
Francis Riccie22b6962017-06-09 20:31:53 +0000456 this->theVector.push_back(Constructable(1));
457 this->theVector.append(output_iterator(), output_iterator());
458 this->assertValuesInOrder(this->theVector, 3u, 1, 7, 7);
459}
460
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000461// Assign test
Chandler Carruth0b012612012-07-30 22:17:52 +0000462TYPED_TEST(SmallVectorTest, AssignTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000463 SCOPED_TRACE("AssignTest");
464
Chandler Carruth0b012612012-07-30 22:17:52 +0000465 this->theVector.push_back(Constructable(1));
466 this->theVector.assign(2, Constructable(77));
467 this->assertValuesInOrder(this->theVector, 2u, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000468}
469
David Blaikieb6b42e02017-06-02 17:24:26 +0000470// Assign test
471TYPED_TEST(SmallVectorTest, AssignRangeTest) {
472 SCOPED_TRACE("AssignTest");
473
474 this->theVector.push_back(Constructable(1));
475 int arr[] = {1, 2, 3};
476 this->theVector.assign(std::begin(arr), std::end(arr));
477 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
478}
479
Francis Riccie22b6962017-06-09 20:31:53 +0000480// Assign test
481TYPED_TEST(SmallVectorTest, AssignNonIterTest) {
482 SCOPED_TRACE("AssignTest");
483
484 this->theVector.push_back(Constructable(1));
485 this->theVector.assign(2, 7);
486 this->assertValuesInOrder(this->theVector, 2u, 7, 7);
487}
488
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000489// Move-assign test
490TYPED_TEST(SmallVectorTest, MoveAssignTest) {
491 SCOPED_TRACE("MoveAssignTest");
492
493 // Set up our vector with a single element, but enough capacity for 4.
494 this->theVector.reserve(4);
495 this->theVector.push_back(Constructable(1));
496
497 // Set up the other vector with 2 elements.
498 this->otherVector.push_back(Constructable(2));
499 this->otherVector.push_back(Constructable(3));
500
501 // Move-assign from the other vector.
502 this->theVector = std::move(this->otherVector);
503
504 // Make sure we have the right result.
505 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
506
507 // Make sure the # of constructor/destructor calls line up. There
508 // are two live objects after clearing the other vector.
509 this->otherVector.clear();
510 EXPECT_EQ(Constructable::getNumConstructorCalls()-2,
511 Constructable::getNumDestructorCalls());
512
513 // There shouldn't be any live objects any more.
514 this->theVector.clear();
515 EXPECT_EQ(Constructable::getNumConstructorCalls(),
516 Constructable::getNumDestructorCalls());
517}
518
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000519// Erase a single element
Chandler Carruth0b012612012-07-30 22:17:52 +0000520TYPED_TEST(SmallVectorTest, EraseTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000521 SCOPED_TRACE("EraseTest");
522
Chandler Carruth0b012612012-07-30 22:17:52 +0000523 this->makeSequence(this->theVector, 1, 3);
David Blaikie6ae4bc82016-03-24 20:25:51 +0000524 const auto &theConstVector = this->theVector;
525 this->theVector.erase(theConstVector.begin());
Chandler Carruth0b012612012-07-30 22:17:52 +0000526 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000527}
528
529// Erase a range of elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000530TYPED_TEST(SmallVectorTest, EraseRangeTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000531 SCOPED_TRACE("EraseRangeTest");
532
Chandler Carruth0b012612012-07-30 22:17:52 +0000533 this->makeSequence(this->theVector, 1, 3);
David Blaikie6ae4bc82016-03-24 20:25:51 +0000534 const auto &theConstVector = this->theVector;
535 this->theVector.erase(theConstVector.begin(), theConstVector.begin() + 2);
Chandler Carruth0b012612012-07-30 22:17:52 +0000536 this->assertValuesInOrder(this->theVector, 1u, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000537}
538
539// Insert a single element.
Chandler Carruth0b012612012-07-30 22:17:52 +0000540TYPED_TEST(SmallVectorTest, InsertTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000541 SCOPED_TRACE("InsertTest");
542
Chandler Carruth0b012612012-07-30 22:17:52 +0000543 this->makeSequence(this->theVector, 1, 3);
544 typename TypeParam::iterator I =
545 this->theVector.insert(this->theVector.begin() + 1, Constructable(77));
546 EXPECT_EQ(this->theVector.begin() + 1, I);
547 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000548}
549
David Blaikie40d4e342014-06-08 16:55:13 +0000550// Insert a copy of a single element.
551TYPED_TEST(SmallVectorTest, InsertCopy) {
552 SCOPED_TRACE("InsertTest");
553
554 this->makeSequence(this->theVector, 1, 3);
555 Constructable C(77);
556 typename TypeParam::iterator I =
557 this->theVector.insert(this->theVector.begin() + 1, C);
558 EXPECT_EQ(this->theVector.begin() + 1, I);
559 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
560}
561
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000562// Insert repeated elements.
Chandler Carruth0b012612012-07-30 22:17:52 +0000563TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000564 SCOPED_TRACE("InsertRepeatedTest");
565
David Blaikie18486602014-06-08 19:33:40 +0000566 this->makeSequence(this->theVector, 1, 4);
David Blaikieae8a9322014-06-08 19:12:28 +0000567 Constructable::reset();
David Blaikie402cb2c2014-06-08 19:12:31 +0000568 auto I =
569 this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
David Blaikie18486602014-06-08 19:33:40 +0000570 // Move construct the top element into newly allocated space, and optionally
571 // reallocate the whole buffer, move constructing into it.
572 // FIXME: This is inefficient, we shouldn't move things into newly allocated
573 // space, then move them up/around, there should only be 2 or 4 move
574 // constructions here.
575 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
576 Constructable::getNumMoveConstructorCalls() == 6);
David Blaikie402cb2c2014-06-08 19:12:31 +0000577 // Move assign the next two to shift them up and make a gap.
David Blaikie18486602014-06-08 19:33:40 +0000578 EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls());
David Blaikie402cb2c2014-06-08 19:12:31 +0000579 // Copy construct the two new elements from the parameter.
580 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
581 // All without any copy construction.
David Blaikieae8a9322014-06-08 19:12:28 +0000582 EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000583 EXPECT_EQ(this->theVector.begin() + 1, I);
David Blaikie18486602014-06-08 19:33:40 +0000584 this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000585}
Benjamin Kramer371b9b02012-06-17 11:52:22 +0000586
Francis Riccie22b6962017-06-09 20:31:53 +0000587TYPED_TEST(SmallVectorTest, InsertRepeatedNonIterTest) {
588 SCOPED_TRACE("InsertRepeatedTest");
589
590 this->makeSequence(this->theVector, 1, 4);
591 Constructable::reset();
592 auto I = this->theVector.insert(this->theVector.begin() + 1, 2, 7);
593 EXPECT_EQ(this->theVector.begin() + 1, I);
594 this->assertValuesInOrder(this->theVector, 6u, 1, 7, 7, 2, 3, 4);
595}
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000596
David Blaikie402cb2c2014-06-08 19:12:31 +0000597TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) {
598 SCOPED_TRACE("InsertRepeatedTest");
599
David Blaikie18486602014-06-08 19:33:40 +0000600 this->makeSequence(this->theVector, 1, 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000601 Constructable::reset();
602 auto I = this->theVector.insert(this->theVector.end(), 2, Constructable(16));
603 // Just copy construct them into newly allocated space
604 EXPECT_EQ(2, Constructable::getNumCopyConstructorCalls());
David Blaikie18486602014-06-08 19:33:40 +0000605 // Move everything across if reallocation is needed.
606 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
607 Constructable::getNumMoveConstructorCalls() == 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000608 // Without ever moving or copying anything else.
609 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
610 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
611
David Blaikie18486602014-06-08 19:33:40 +0000612 EXPECT_EQ(this->theVector.begin() + 4, I);
613 this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16);
David Blaikie402cb2c2014-06-08 19:12:31 +0000614}
615
616TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) {
617 SCOPED_TRACE("InsertRepeatedTest");
618
619 this->makeSequence(this->theVector, 10, 15);
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000620
621 // Empty insert.
Chandler Carruth0b012612012-07-30 22:17:52 +0000622 EXPECT_EQ(this->theVector.end(),
623 this->theVector.insert(this->theVector.end(),
624 0, Constructable(42)));
625 EXPECT_EQ(this->theVector.begin() + 1,
626 this->theVector.insert(this->theVector.begin() + 1,
627 0, Constructable(42)));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000628}
629
630// Insert range.
Chandler Carruth0b012612012-07-30 22:17:52 +0000631TYPED_TEST(SmallVectorTest, InsertRangeTest) {
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000632 SCOPED_TRACE("InsertRangeTest");
633
634 Constructable Arr[3] =
635 { Constructable(77), Constructable(77), Constructable(77) };
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000636
Chandler Carruth0b012612012-07-30 22:17:52 +0000637 this->makeSequence(this->theVector, 1, 3);
David Blaikie402cb2c2014-06-08 19:12:31 +0000638 Constructable::reset();
639 auto I = this->theVector.insert(this->theVector.begin() + 1, Arr, Arr + 3);
640 // Move construct the top 3 elements into newly allocated space.
641 // Possibly move the whole sequence into new space first.
642 // FIXME: This is inefficient, we shouldn't move things into newly allocated
643 // space, then move them up/around, there should only be 2 or 3 move
644 // constructions here.
645 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
646 Constructable::getNumMoveConstructorCalls() == 5);
647 // Copy assign the lower 2 new elements into existing space.
648 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
649 // Copy construct the third element into newly allocated space.
650 EXPECT_EQ(1, Constructable::getNumCopyConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000651 EXPECT_EQ(this->theVector.begin() + 1, I);
652 this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3);
David Blaikie402cb2c2014-06-08 19:12:31 +0000653}
654
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000655
David Blaikie402cb2c2014-06-08 19:12:31 +0000656TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) {
657 SCOPED_TRACE("InsertRangeTest");
658
659 Constructable Arr[3] =
660 { Constructable(77), Constructable(77), Constructable(77) };
661
662 this->makeSequence(this->theVector, 1, 3);
Benjamin Kramer371b9b02012-06-17 11:52:22 +0000663
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000664 // Insert at end.
David Blaikie402cb2c2014-06-08 19:12:31 +0000665 Constructable::reset();
666 auto I = this->theVector.insert(this->theVector.end(), Arr, Arr+3);
667 // Copy construct the 3 elements into new space at the top.
668 EXPECT_EQ(3, Constructable::getNumCopyConstructorCalls());
669 // Don't copy/move anything else.
670 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
671 // Reallocation might occur, causing all elements to be moved into the new
672 // buffer.
673 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
674 Constructable::getNumMoveConstructorCalls() == 3);
675 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
676 EXPECT_EQ(this->theVector.begin() + 3, I);
677 this->assertValuesInOrder(this->theVector, 6u,
678 1, 2, 3, 77, 77, 77);
679}
680
681TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
682 SCOPED_TRACE("InsertRangeTest");
683
684 this->makeSequence(this->theVector, 1, 3);
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000685
686 // Empty insert.
Chandler Carruth0b012612012-07-30 22:17:52 +0000687 EXPECT_EQ(this->theVector.end(),
688 this->theVector.insert(this->theVector.end(),
689 this->theVector.begin(),
690 this->theVector.begin()));
691 EXPECT_EQ(this->theVector.begin() + 1,
692 this->theVector.insert(this->theVector.begin() + 1,
693 this->theVector.begin(),
694 this->theVector.begin()));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000695}
696
697// Comparison tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000698TYPED_TEST(SmallVectorTest, ComparisonTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000699 SCOPED_TRACE("ComparisonTest");
700
Chandler Carruth0b012612012-07-30 22:17:52 +0000701 this->makeSequence(this->theVector, 1, 3);
702 this->makeSequence(this->otherVector, 1, 3);
Owen Anderson145a2602011-07-06 22:36:59 +0000703
Chandler Carruth0b012612012-07-30 22:17:52 +0000704 EXPECT_TRUE(this->theVector == this->otherVector);
705 EXPECT_FALSE(this->theVector != this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000706
Chandler Carruth0b012612012-07-30 22:17:52 +0000707 this->otherVector.clear();
708 this->makeSequence(this->otherVector, 2, 4);
Owen Anderson145a2602011-07-06 22:36:59 +0000709
Chandler Carruth0b012612012-07-30 22:17:52 +0000710 EXPECT_FALSE(this->theVector == this->otherVector);
711 EXPECT_TRUE(this->theVector != this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000712}
713
714// Constant vector tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000715TYPED_TEST(SmallVectorTest, ConstVectorTest) {
716 const TypeParam constVector;
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000717
718 EXPECT_EQ(0u, constVector.size());
719 EXPECT_TRUE(constVector.empty());
720 EXPECT_TRUE(constVector.begin() == constVector.end());
721}
722
Daniel Dunbar825e9552009-08-19 17:48:28 +0000723// Direct array access.
Chandler Carruth0b012612012-07-30 22:17:52 +0000724TYPED_TEST(SmallVectorTest, DirectVectorTest) {
725 EXPECT_EQ(0u, this->theVector.size());
726 this->theVector.reserve(4);
727 EXPECT_LE(4u, this->theVector.capacity());
Daniel Dunbar825e9552009-08-19 17:48:28 +0000728 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000729 this->theVector.push_back(1);
730 this->theVector.push_back(2);
731 this->theVector.push_back(3);
732 this->theVector.push_back(4);
Chandler Carruth0b012612012-07-30 22:17:52 +0000733 EXPECT_EQ(4u, this->theVector.size());
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000734 EXPECT_EQ(8, Constructable::getNumConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000735 EXPECT_EQ(1, this->theVector[0].getValue());
736 EXPECT_EQ(2, this->theVector[1].getValue());
737 EXPECT_EQ(3, this->theVector[2].getValue());
738 EXPECT_EQ(4, this->theVector[3].getValue());
Daniel Dunbar825e9552009-08-19 17:48:28 +0000739}
740
Chandler Carruth0b012612012-07-30 22:17:52 +0000741TYPED_TEST(SmallVectorTest, IteratorTest) {
Dan Gohman42e77df2010-03-26 18:53:37 +0000742 std::list<int> L;
Chandler Carruth0b012612012-07-30 22:17:52 +0000743 this->theVector.insert(this->theVector.end(), L.begin(), L.end());
Dan Gohman42e77df2010-03-26 18:53:37 +0000744}
745
Lang Hamesac92bfc2015-01-23 06:25:17 +0000746template <typename InvalidType> class DualSmallVectorsTest;
747
748template <typename VectorT1, typename VectorT2>
749class DualSmallVectorsTest<std::pair<VectorT1, VectorT2>> : public SmallVectorTestBase {
750protected:
751 VectorT1 theVector;
752 VectorT2 otherVector;
753
754 template <typename T, unsigned N>
755 static unsigned NumBuiltinElts(const SmallVector<T, N>&) { return N; }
756};
757
758typedef ::testing::Types<
759 // Small mode -> Small mode.
760 std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 4>>,
761 // Small mode -> Big mode.
762 std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 2>>,
763 // Big mode -> Small mode.
764 std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 4>>,
765 // Big mode -> Big mode.
766 std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 2>>
767 > DualSmallVectorTestTypes;
768
769TYPED_TEST_CASE(DualSmallVectorsTest, DualSmallVectorTestTypes);
770
771TYPED_TEST(DualSmallVectorsTest, MoveAssignment) {
772 SCOPED_TRACE("MoveAssignTest-DualVectorTypes");
773
774 // Set up our vector with four elements.
775 for (unsigned I = 0; I < 4; ++I)
776 this->otherVector.push_back(Constructable(I));
777
778 const Constructable *OrigDataPtr = this->otherVector.data();
779
780 // Move-assign from the other vector.
781 this->theVector =
782 std::move(static_cast<SmallVectorImpl<Constructable>&>(this->otherVector));
783
784 // Make sure we have the right result.
785 this->assertValuesInOrder(this->theVector, 4u, 0, 1, 2, 3);
786
787 // Make sure the # of constructor/destructor calls line up. There
788 // are two live objects after clearing the other vector.
789 this->otherVector.clear();
790 EXPECT_EQ(Constructable::getNumConstructorCalls()-4,
791 Constructable::getNumDestructorCalls());
792
793 // If the source vector (otherVector) was in small-mode, assert that we just
794 // moved the data pointer over.
795 EXPECT_TRUE(this->NumBuiltinElts(this->otherVector) == 4 ||
796 this->theVector.data() == OrigDataPtr);
797
798 // There shouldn't be any live objects any more.
799 this->theVector.clear();
800 EXPECT_EQ(Constructable::getNumConstructorCalls(),
801 Constructable::getNumDestructorCalls());
802
803 // We shouldn't have copied anything in this whole process.
804 EXPECT_EQ(Constructable::getNumCopyConstructorCalls(), 0);
805}
806
Benjamin Kramer74a12a42012-04-29 10:53:29 +0000807struct notassignable {
808 int &x;
809 notassignable(int &x) : x(x) {}
810};
811
Chandler Carruth0b012612012-07-30 22:17:52 +0000812TEST(SmallVectorCustomTest, NoAssignTest) {
Benjamin Kramer74a12a42012-04-29 10:53:29 +0000813 int x = 0;
814 SmallVector<notassignable, 2> vec;
815 vec.push_back(notassignable(x));
816 x = 42;
817 EXPECT_EQ(42, vec.pop_back_val().x);
818}
819
David Blaikie789df062014-06-08 16:00:02 +0000820struct MovedFrom {
821 bool hasValue;
822 MovedFrom() : hasValue(true) {
823 }
824 MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) {
825 m.hasValue = false;
826 }
827 MovedFrom &operator=(MovedFrom&& m) {
828 hasValue = m.hasValue;
829 m.hasValue = false;
830 return *this;
831 }
832};
833
834TEST(SmallVectorTest, MidInsert) {
835 SmallVector<MovedFrom, 3> v;
836 v.push_back(MovedFrom());
837 v.insert(v.begin(), MovedFrom());
838 for (MovedFrom &m : v)
839 EXPECT_TRUE(m.hasValue);
840}
841
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000842enum EmplaceableArgState {
843 EAS_Defaulted,
844 EAS_Arg,
845 EAS_LValue,
846 EAS_RValue,
847 EAS_Failure
848};
849template <int I> struct EmplaceableArg {
850 EmplaceableArgState State;
851 EmplaceableArg() : State(EAS_Defaulted) {}
852 EmplaceableArg(EmplaceableArg &&X)
853 : State(X.State == EAS_Arg ? EAS_RValue : EAS_Failure) {}
854 EmplaceableArg(EmplaceableArg &X)
855 : State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {}
856
857 explicit EmplaceableArg(bool) : State(EAS_Arg) {}
858
859private:
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000860 EmplaceableArg &operator=(EmplaceableArg &&) = delete;
861 EmplaceableArg &operator=(const EmplaceableArg &) = delete;
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000862};
863
864enum EmplaceableState { ES_Emplaced, ES_Moved };
865struct Emplaceable {
866 EmplaceableArg<0> A0;
867 EmplaceableArg<1> A1;
868 EmplaceableArg<2> A2;
869 EmplaceableArg<3> A3;
870 EmplaceableState State;
871
872 Emplaceable() : State(ES_Emplaced) {}
873
874 template <class A0Ty>
875 explicit Emplaceable(A0Ty &&A0)
876 : A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {}
877
878 template <class A0Ty, class A1Ty>
879 Emplaceable(A0Ty &&A0, A1Ty &&A1)
880 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
881 State(ES_Emplaced) {}
882
883 template <class A0Ty, class A1Ty, class A2Ty>
884 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2)
885 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
886 A2(std::forward<A2Ty>(A2)), State(ES_Emplaced) {}
887
888 template <class A0Ty, class A1Ty, class A2Ty, class A3Ty>
889 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2, A3Ty &&A3)
890 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
891 A2(std::forward<A2Ty>(A2)), A3(std::forward<A3Ty>(A3)),
892 State(ES_Emplaced) {}
893
894 Emplaceable(Emplaceable &&) : State(ES_Moved) {}
895 Emplaceable &operator=(Emplaceable &&) {
896 State = ES_Moved;
897 return *this;
898 }
899
900private:
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000901 Emplaceable(const Emplaceable &) = delete;
902 Emplaceable &operator=(const Emplaceable &) = delete;
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000903};
904
905TEST(SmallVectorTest, EmplaceBack) {
906 EmplaceableArg<0> A0(true);
907 EmplaceableArg<1> A1(true);
908 EmplaceableArg<2> A2(true);
909 EmplaceableArg<3> A3(true);
910 {
911 SmallVector<Emplaceable, 3> V;
912 V.emplace_back();
913 EXPECT_TRUE(V.size() == 1);
914 EXPECT_TRUE(V.back().State == ES_Emplaced);
915 EXPECT_TRUE(V.back().A0.State == EAS_Defaulted);
916 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
917 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
918 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
919 }
920 {
921 SmallVector<Emplaceable, 3> V;
922 V.emplace_back(std::move(A0));
923 EXPECT_TRUE(V.size() == 1);
924 EXPECT_TRUE(V.back().State == ES_Emplaced);
925 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
926 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
927 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
928 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
929 }
930 {
931 SmallVector<Emplaceable, 3> V;
932 V.emplace_back(A0);
933 EXPECT_TRUE(V.size() == 1);
934 EXPECT_TRUE(V.back().State == ES_Emplaced);
935 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
936 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
937 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
938 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
939 }
940 {
941 SmallVector<Emplaceable, 3> V;
942 V.emplace_back(A0, A1);
943 EXPECT_TRUE(V.size() == 1);
944 EXPECT_TRUE(V.back().State == ES_Emplaced);
945 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
946 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
947 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
948 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
949 }
950 {
951 SmallVector<Emplaceable, 3> V;
952 V.emplace_back(std::move(A0), std::move(A1));
953 EXPECT_TRUE(V.size() == 1);
954 EXPECT_TRUE(V.back().State == ES_Emplaced);
955 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
956 EXPECT_TRUE(V.back().A1.State == EAS_RValue);
957 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
958 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
959 }
960 {
961 SmallVector<Emplaceable, 3> V;
962 V.emplace_back(std::move(A0), A1, std::move(A2), A3);
963 EXPECT_TRUE(V.size() == 1);
964 EXPECT_TRUE(V.back().State == ES_Emplaced);
965 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
966 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
967 EXPECT_TRUE(V.back().A2.State == EAS_RValue);
968 EXPECT_TRUE(V.back().A3.State == EAS_LValue);
969 }
Benjamin Kramer1d563f42015-02-07 16:41:02 +0000970 {
971 SmallVector<int, 1> V;
972 V.emplace_back();
973 V.emplace_back(42);
974 EXPECT_EQ(2U, V.size());
975 EXPECT_EQ(0, V[0]);
976 EXPECT_EQ(42, V[1]);
977 }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000978}
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000979
Benjamin Kramera6d7acc2015-03-02 21:16:04 +0000980TEST(SmallVectorTest, InitializerList) {
981 SmallVector<int, 2> V1 = {};
982 EXPECT_TRUE(V1.empty());
983 V1 = {0, 0};
984 EXPECT_TRUE(makeArrayRef(V1).equals({0, 0}));
985 V1 = {-1, -1};
986 EXPECT_TRUE(makeArrayRef(V1).equals({-1, -1}));
987
988 SmallVector<int, 2> V2 = {1, 2, 3, 4};
989 EXPECT_TRUE(makeArrayRef(V2).equals({1, 2, 3, 4}));
990 V2.assign({4});
991 EXPECT_TRUE(makeArrayRef(V2).equals({4}));
992 V2.append({3, 2});
993 EXPECT_TRUE(makeArrayRef(V2).equals({4, 3, 2}));
994 V2.insert(V2.begin() + 1, 5);
995 EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2}));
996}
997
Duncan P. N. Exon Smith91d3cfe2016-04-05 20:45:04 +0000998} // end namespace