blob: f6e9bb965c67553cb8d2a60a8965286ac536f309 [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"
Bill Wendlingbfba2f12010-08-19 18:52:02 +000015#include "llvm/Support/Compiler.h"
Chandler Carruth130cec22012-12-04 10:23:08 +000016#include "gtest/gtest.h"
Dan Gohman42e77df2010-03-26 18:53:37 +000017#include <list>
Chandler Carruth130cec22012-12-04 10:23:08 +000018#include <stdarg.h>
Bill Wendlingc56c37f2009-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;
David Blaikieae8a9322014-06-08 19:12:28 +000029 static int numMoveConstructorCalls;
30 static int numCopyConstructorCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000031 static int numDestructorCalls;
32 static int numAssignmentCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000033 static int numMoveAssignmentCalls;
34 static int numCopyAssignmentCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000035
Douglas Gregor8451cdff2014-04-30 15:49:06 +000036 bool constructed;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000037 int value;
38
39public:
Douglas Gregor8451cdff2014-04-30 15:49:06 +000040 Constructable() : constructed(true), value(0) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +000041 ++numConstructorCalls;
42 }
Owen Anderson145a2602011-07-06 22:36:59 +000043
Douglas Gregor8451cdff2014-04-30 15:49:06 +000044 Constructable(int val) : constructed(true), value(val) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +000045 ++numConstructorCalls;
46 }
Owen Anderson145a2602011-07-06 22:36:59 +000047
Douglas Gregor8451cdff2014-04-30 15:49:06 +000048 Constructable(const Constructable & src) : constructed(true) {
49 value = src.value;
50 ++numConstructorCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000051 ++numCopyConstructorCalls;
Douglas Gregor8451cdff2014-04-30 15:49:06 +000052 }
53
54 Constructable(Constructable && src) : constructed(true) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +000055 value = src.value;
56 ++numConstructorCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000057 ++numMoveConstructorCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000058 }
Owen Anderson145a2602011-07-06 22:36:59 +000059
Bill Wendlingc56c37f2009-01-10 12:56:31 +000060 ~Constructable() {
Douglas Gregor8451cdff2014-04-30 15:49:06 +000061 EXPECT_TRUE(constructed);
Bill Wendlingc56c37f2009-01-10 12:56:31 +000062 ++numDestructorCalls;
Douglas Gregor8451cdff2014-04-30 15:49:06 +000063 constructed = false;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000064 }
Owen Anderson145a2602011-07-06 22:36:59 +000065
Bill Wendlingc56c37f2009-01-10 12:56:31 +000066 Constructable & operator=(const Constructable & src) {
Douglas Gregor8451cdff2014-04-30 15:49:06 +000067 EXPECT_TRUE(constructed);
68 value = src.value;
69 ++numAssignmentCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000070 ++numCopyAssignmentCalls;
Douglas Gregor8451cdff2014-04-30 15:49:06 +000071 return *this;
72 }
73
74 Constructable & operator=(Constructable && src) {
75 EXPECT_TRUE(constructed);
Bill Wendlingc56c37f2009-01-10 12:56:31 +000076 value = src.value;
77 ++numAssignmentCalls;
David Blaikieae8a9322014-06-08 19:12:28 +000078 ++numMoveAssignmentCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000079 return *this;
80 }
Owen Anderson145a2602011-07-06 22:36:59 +000081
Bill Wendlingc56c37f2009-01-10 12:56:31 +000082 int getValue() const {
83 return abs(value);
84 }
85
86 static void reset() {
87 numConstructorCalls = 0;
David Blaikieae8a9322014-06-08 19:12:28 +000088 numMoveConstructorCalls = 0;
89 numCopyConstructorCalls = 0;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000090 numDestructorCalls = 0;
91 numAssignmentCalls = 0;
David Blaikieae8a9322014-06-08 19:12:28 +000092 numMoveAssignmentCalls = 0;
93 numCopyAssignmentCalls = 0;
Bill Wendlingc56c37f2009-01-10 12:56:31 +000094 }
Owen Anderson145a2602011-07-06 22:36:59 +000095
Bill Wendlingc56c37f2009-01-10 12:56:31 +000096 static int getNumConstructorCalls() {
97 return numConstructorCalls;
98 }
99
David Blaikieae8a9322014-06-08 19:12:28 +0000100 static int getNumMoveConstructorCalls() {
101 return numMoveConstructorCalls;
102 }
103
104 static int getNumCopyConstructorCalls() {
105 return numCopyConstructorCalls;
106 }
107
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000108 static int getNumDestructorCalls() {
109 return numDestructorCalls;
110 }
111
David Blaikieae8a9322014-06-08 19:12:28 +0000112 static int getNumAssignmentCalls() {
113 return numAssignmentCalls;
114 }
115
116 static int getNumMoveAssignmentCalls() {
117 return numMoveAssignmentCalls;
118 }
119
120 static int getNumCopyAssignmentCalls() {
121 return numCopyAssignmentCalls;
122 }
123
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000124 friend bool operator==(const Constructable & c0, const Constructable & c1) {
125 return c0.getValue() == c1.getValue();
126 }
127
Chandler Carruth88c54b82010-10-23 08:10:43 +0000128 friend bool LLVM_ATTRIBUTE_UNUSED
Bill Wendlingbfba2f12010-08-19 18:52:02 +0000129 operator!=(const Constructable & c0, const Constructable & c1) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000130 return c0.getValue() != c1.getValue();
131 }
132};
133
134int Constructable::numConstructorCalls;
David Blaikieae8a9322014-06-08 19:12:28 +0000135int Constructable::numCopyConstructorCalls;
136int Constructable::numMoveConstructorCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000137int Constructable::numDestructorCalls;
138int Constructable::numAssignmentCalls;
David Blaikieae8a9322014-06-08 19:12:28 +0000139int Constructable::numCopyAssignmentCalls;
140int Constructable::numMoveAssignmentCalls;
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000141
David Blaikie669fc862014-06-09 22:26:20 +0000142struct NonCopyable {
143 NonCopyable() {}
144 NonCopyable(NonCopyable &&) {}
145 NonCopyable &operator=(NonCopyable &&) { return *this; }
David Blaikie11e08762014-06-11 17:50:14 +0000146private:
NAKAMURA Takumi10467ca2014-06-10 04:06:56 +0000147 NonCopyable(const NonCopyable &) LLVM_DELETED_FUNCTION;
148 NonCopyable &operator=(const NonCopyable &) LLVM_DELETED_FUNCTION;
David Blaikie669fc862014-06-09 22:26:20 +0000149};
150
151LLVM_ATTRIBUTE_USED void CompileTest() {
152 SmallVector<NonCopyable, 0> V;
153 V.resize(42);
154}
155
Lang Hamesac92bfc2015-01-23 06:25:17 +0000156class SmallVectorTestBase : public testing::Test {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000157protected:
Owen Anderson145a2602011-07-06 22:36:59 +0000158
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000159 void SetUp() {
160 Constructable::reset();
161 }
162
Lang Hamesac92bfc2015-01-23 06:25:17 +0000163 template <typename VectorT>
Chandler Carruth0b012612012-07-30 22:17:52 +0000164 void assertEmpty(VectorT & v) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000165 // Size tests
166 EXPECT_EQ(0u, v.size());
167 EXPECT_TRUE(v.empty());
168
169 // Iterator tests
170 EXPECT_TRUE(v.begin() == v.end());
171 }
172
Lang Hamesac92bfc2015-01-23 06:25:17 +0000173 // Assert that v contains the specified values, in order.
174 template <typename VectorT>
Chandler Carruth0b012612012-07-30 22:17:52 +0000175 void assertValuesInOrder(VectorT & v, size_t size, ...) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000176 EXPECT_EQ(size, v.size());
Owen Anderson145a2602011-07-06 22:36:59 +0000177
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000178 va_list ap;
179 va_start(ap, size);
180 for (size_t i = 0; i < size; ++i) {
181 int value = va_arg(ap, int);
182 EXPECT_EQ(value, v[i].getValue());
183 }
184
185 va_end(ap);
186 }
Owen Anderson145a2602011-07-06 22:36:59 +0000187
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000188 // Generate a sequence of values to initialize the vector.
Lang Hamesac92bfc2015-01-23 06:25:17 +0000189 template <typename VectorT>
Chandler Carruth0b012612012-07-30 22:17:52 +0000190 void makeSequence(VectorT & v, int start, int end) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000191 for (int i = start; i <= end; ++i) {
192 v.push_back(Constructable(i));
193 }
194 }
195};
196
Lang Hamesac92bfc2015-01-23 06:25:17 +0000197// Test fixture class
198template <typename VectorT>
199class SmallVectorTest : public SmallVectorTestBase {
200protected:
201 VectorT theVector;
202 VectorT otherVector;
203};
204
205
Chandler Carruth0b012612012-07-30 22:17:52 +0000206typedef ::testing::Types<SmallVector<Constructable, 0>,
207 SmallVector<Constructable, 1>,
208 SmallVector<Constructable, 2>,
David Blaikie18486602014-06-08 19:33:40 +0000209 SmallVector<Constructable, 4>,
210 SmallVector<Constructable, 5>
Chandler Carruth0b012612012-07-30 22:17:52 +0000211 > SmallVectorTestTypes;
212TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes);
213
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000214// New vector test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000215TYPED_TEST(SmallVectorTest, EmptyVectorTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000216 SCOPED_TRACE("EmptyVectorTest");
Chandler Carruth0b012612012-07-30 22:17:52 +0000217 this->assertEmpty(this->theVector);
218 EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000219 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
220 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
221}
222
223// Simple insertions and deletions.
Chandler Carruth0b012612012-07-30 22:17:52 +0000224TYPED_TEST(SmallVectorTest, PushPopTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000225 SCOPED_TRACE("PushPopTest");
226
Chandler Carruth0b012612012-07-30 22:17:52 +0000227 // Track whether the vector will potentially have to grow.
228 bool RequiresGrowth = this->theVector.capacity() < 3;
229
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000230 // Push an element
Chandler Carruth0b012612012-07-30 22:17:52 +0000231 this->theVector.push_back(Constructable(1));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000232
233 // Size tests
Chandler Carruth0b012612012-07-30 22:17:52 +0000234 this->assertValuesInOrder(this->theVector, 1u, 1);
235 EXPECT_FALSE(this->theVector.begin() == this->theVector.end());
236 EXPECT_FALSE(this->theVector.empty());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000237
238 // Push another element
Chandler Carruth0b012612012-07-30 22:17:52 +0000239 this->theVector.push_back(Constructable(2));
240 this->assertValuesInOrder(this->theVector, 2u, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000241
Owen Anderson145a2602011-07-06 22:36:59 +0000242 // Insert at beginning
Chandler Carruth0b012612012-07-30 22:17:52 +0000243 this->theVector.insert(this->theVector.begin(), this->theVector[1]);
244 this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2);
Owen Anderson145a2602011-07-06 22:36:59 +0000245
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000246 // Pop one element
Chandler Carruth0b012612012-07-30 22:17:52 +0000247 this->theVector.pop_back();
248 this->assertValuesInOrder(this->theVector, 2u, 2, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000249
Owen Anderson145a2602011-07-06 22:36:59 +0000250 // Pop remaining elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000251 this->theVector.pop_back();
252 this->theVector.pop_back();
253 this->assertEmpty(this->theVector);
Owen Anderson145a2602011-07-06 22:36:59 +0000254
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000255 // Check number of constructor calls. Should be 2 for each list element,
Owen Anderson145a2602011-07-06 22:36:59 +0000256 // one for the argument to push_back, one for the argument to insert,
257 // and one for the list element itself.
Chandler Carruth0b012612012-07-30 22:17:52 +0000258 if (!RequiresGrowth) {
259 EXPECT_EQ(5, Constructable::getNumConstructorCalls());
260 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
261 } else {
262 // If we had to grow the vector, these only have a lower bound, but should
263 // always be equal.
264 EXPECT_LE(5, Constructable::getNumConstructorCalls());
265 EXPECT_EQ(Constructable::getNumConstructorCalls(),
266 Constructable::getNumDestructorCalls());
267 }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000268}
269
270// Clear test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000271TYPED_TEST(SmallVectorTest, ClearTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000272 SCOPED_TRACE("ClearTest");
273
Chandler Carruth0b012612012-07-30 22:17:52 +0000274 this->theVector.reserve(2);
275 this->makeSequence(this->theVector, 1, 2);
276 this->theVector.clear();
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000277
Chandler Carruth0b012612012-07-30 22:17:52 +0000278 this->assertEmpty(this->theVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000279 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
280 EXPECT_EQ(4, Constructable::getNumDestructorCalls());
281}
282
283// Resize smaller test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000284TYPED_TEST(SmallVectorTest, ResizeShrinkTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000285 SCOPED_TRACE("ResizeShrinkTest");
286
Chandler Carruth0b012612012-07-30 22:17:52 +0000287 this->theVector.reserve(3);
288 this->makeSequence(this->theVector, 1, 3);
289 this->theVector.resize(1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000290
Chandler Carruth0b012612012-07-30 22:17:52 +0000291 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000292 EXPECT_EQ(6, Constructable::getNumConstructorCalls());
293 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
294}
295
296// Resize bigger test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000297TYPED_TEST(SmallVectorTest, ResizeGrowTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000298 SCOPED_TRACE("ResizeGrowTest");
299
Chandler Carruth0b012612012-07-30 22:17:52 +0000300 this->theVector.resize(2);
Owen Anderson145a2602011-07-06 22:36:59 +0000301
David Blaikie669fc862014-06-09 22:26:20 +0000302 EXPECT_EQ(2, Constructable::getNumConstructorCalls());
303 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000304 EXPECT_EQ(2u, this->theVector.size());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000305}
306
David Blaikie669fc862014-06-09 22:26:20 +0000307TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) {
308 this->theVector.resize(2);
309
310 Constructable::reset();
311
312 this->theVector.resize(4);
313
314 size_t Ctors = Constructable::getNumConstructorCalls();
315 EXPECT_TRUE(Ctors == 2 || Ctors == 4);
316 size_t MoveCtors = Constructable::getNumMoveConstructorCalls();
317 EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2);
318 size_t Dtors = Constructable::getNumDestructorCalls();
319 EXPECT_TRUE(Dtors == 0 || Dtors == 2);
320}
321
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000322// Resize with fill value.
Chandler Carruth0b012612012-07-30 22:17:52 +0000323TYPED_TEST(SmallVectorTest, ResizeFillTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000324 SCOPED_TRACE("ResizeFillTest");
325
Chandler Carruth0b012612012-07-30 22:17:52 +0000326 this->theVector.resize(3, Constructable(77));
327 this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000328}
329
330// Overflow past fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000331TYPED_TEST(SmallVectorTest, OverflowTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000332 SCOPED_TRACE("OverflowTest");
333
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000334 // Push more elements than the fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000335 this->makeSequence(this->theVector, 1, 10);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000336
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000337 // Test size and values.
Chandler Carruth0b012612012-07-30 22:17:52 +0000338 EXPECT_EQ(10u, this->theVector.size());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000339 for (int i = 0; i < 10; ++i) {
Chandler Carruth0b012612012-07-30 22:17:52 +0000340 EXPECT_EQ(i+1, this->theVector[i].getValue());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000341 }
Owen Anderson145a2602011-07-06 22:36:59 +0000342
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000343 // Now resize back to fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000344 this->theVector.resize(1);
Owen Anderson145a2602011-07-06 22:36:59 +0000345
Chandler Carruth0b012612012-07-30 22:17:52 +0000346 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000347}
348
349// Iteration tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000350TYPED_TEST(SmallVectorTest, IterationTest) {
351 this->makeSequence(this->theVector, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000352
353 // Forward Iteration
Chandler Carruth0b012612012-07-30 22:17:52 +0000354 typename TypeParam::iterator it = this->theVector.begin();
355 EXPECT_TRUE(*it == this->theVector.front());
356 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000357 EXPECT_EQ(1, it->getValue());
358 ++it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000359 EXPECT_TRUE(*it == this->theVector[1]);
360 EXPECT_TRUE(*it == this->theVector.back());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000361 EXPECT_EQ(2, it->getValue());
362 ++it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000363 EXPECT_TRUE(it == this->theVector.end());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000364 --it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000365 EXPECT_TRUE(*it == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000366 EXPECT_EQ(2, it->getValue());
367 --it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000368 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000369 EXPECT_EQ(1, it->getValue());
370
371 // Reverse Iteration
Chandler Carruth0b012612012-07-30 22:17:52 +0000372 typename TypeParam::reverse_iterator rit = this->theVector.rbegin();
373 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000374 EXPECT_EQ(2, rit->getValue());
375 ++rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000376 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000377 EXPECT_EQ(1, rit->getValue());
378 ++rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000379 EXPECT_TRUE(rit == this->theVector.rend());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000380 --rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000381 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000382 EXPECT_EQ(1, rit->getValue());
383 --rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000384 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000385 EXPECT_EQ(2, rit->getValue());
386}
387
388// Swap test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000389TYPED_TEST(SmallVectorTest, SwapTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000390 SCOPED_TRACE("SwapTest");
391
Chandler Carruth0b012612012-07-30 22:17:52 +0000392 this->makeSequence(this->theVector, 1, 2);
393 std::swap(this->theVector, this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000394
Chandler Carruth0b012612012-07-30 22:17:52 +0000395 this->assertEmpty(this->theVector);
396 this->assertValuesInOrder(this->otherVector, 2u, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000397}
398
399// Append test
Chandler Carruth0b012612012-07-30 22:17:52 +0000400TYPED_TEST(SmallVectorTest, AppendTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000401 SCOPED_TRACE("AppendTest");
402
Chandler Carruth0b012612012-07-30 22:17:52 +0000403 this->makeSequence(this->otherVector, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000404
Chandler Carruth0b012612012-07-30 22:17:52 +0000405 this->theVector.push_back(Constructable(1));
406 this->theVector.append(this->otherVector.begin(), this->otherVector.end());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000407
Chandler Carruth0b012612012-07-30 22:17:52 +0000408 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000409}
410
411// Append repeated test
Chandler Carruth0b012612012-07-30 22:17:52 +0000412TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000413 SCOPED_TRACE("AppendRepeatedTest");
414
Chandler Carruth0b012612012-07-30 22:17:52 +0000415 this->theVector.push_back(Constructable(1));
416 this->theVector.append(2, Constructable(77));
417 this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000418}
419
420// Assign test
Chandler Carruth0b012612012-07-30 22:17:52 +0000421TYPED_TEST(SmallVectorTest, AssignTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000422 SCOPED_TRACE("AssignTest");
423
Chandler Carruth0b012612012-07-30 22:17:52 +0000424 this->theVector.push_back(Constructable(1));
425 this->theVector.assign(2, Constructable(77));
426 this->assertValuesInOrder(this->theVector, 2u, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000427}
428
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000429// Move-assign test
430TYPED_TEST(SmallVectorTest, MoveAssignTest) {
431 SCOPED_TRACE("MoveAssignTest");
432
433 // Set up our vector with a single element, but enough capacity for 4.
434 this->theVector.reserve(4);
435 this->theVector.push_back(Constructable(1));
436
437 // Set up the other vector with 2 elements.
438 this->otherVector.push_back(Constructable(2));
439 this->otherVector.push_back(Constructable(3));
440
441 // Move-assign from the other vector.
442 this->theVector = std::move(this->otherVector);
443
444 // Make sure we have the right result.
445 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
446
447 // Make sure the # of constructor/destructor calls line up. There
448 // are two live objects after clearing the other vector.
449 this->otherVector.clear();
450 EXPECT_EQ(Constructable::getNumConstructorCalls()-2,
451 Constructable::getNumDestructorCalls());
452
453 // There shouldn't be any live objects any more.
454 this->theVector.clear();
455 EXPECT_EQ(Constructable::getNumConstructorCalls(),
456 Constructable::getNumDestructorCalls());
457}
458
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000459// Erase a single element
Chandler Carruth0b012612012-07-30 22:17:52 +0000460TYPED_TEST(SmallVectorTest, EraseTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000461 SCOPED_TRACE("EraseTest");
462
Chandler Carruth0b012612012-07-30 22:17:52 +0000463 this->makeSequence(this->theVector, 1, 3);
464 this->theVector.erase(this->theVector.begin());
465 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000466}
467
468// Erase a range of elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000469TYPED_TEST(SmallVectorTest, EraseRangeTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000470 SCOPED_TRACE("EraseRangeTest");
471
Chandler Carruth0b012612012-07-30 22:17:52 +0000472 this->makeSequence(this->theVector, 1, 3);
473 this->theVector.erase(this->theVector.begin(), this->theVector.begin() + 2);
474 this->assertValuesInOrder(this->theVector, 1u, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000475}
476
477// Insert a single element.
Chandler Carruth0b012612012-07-30 22:17:52 +0000478TYPED_TEST(SmallVectorTest, InsertTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000479 SCOPED_TRACE("InsertTest");
480
Chandler Carruth0b012612012-07-30 22:17:52 +0000481 this->makeSequence(this->theVector, 1, 3);
482 typename TypeParam::iterator I =
483 this->theVector.insert(this->theVector.begin() + 1, Constructable(77));
484 EXPECT_EQ(this->theVector.begin() + 1, I);
485 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000486}
487
David Blaikie40d4e342014-06-08 16:55:13 +0000488// Insert a copy of a single element.
489TYPED_TEST(SmallVectorTest, InsertCopy) {
490 SCOPED_TRACE("InsertTest");
491
492 this->makeSequence(this->theVector, 1, 3);
493 Constructable C(77);
494 typename TypeParam::iterator I =
495 this->theVector.insert(this->theVector.begin() + 1, C);
496 EXPECT_EQ(this->theVector.begin() + 1, I);
497 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
498}
499
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000500// Insert repeated elements.
Chandler Carruth0b012612012-07-30 22:17:52 +0000501TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000502 SCOPED_TRACE("InsertRepeatedTest");
503
David Blaikie18486602014-06-08 19:33:40 +0000504 this->makeSequence(this->theVector, 1, 4);
David Blaikieae8a9322014-06-08 19:12:28 +0000505 Constructable::reset();
David Blaikie402cb2c2014-06-08 19:12:31 +0000506 auto I =
507 this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
David Blaikie18486602014-06-08 19:33:40 +0000508 // Move construct the top element into newly allocated space, and optionally
509 // reallocate the whole buffer, move constructing into it.
510 // FIXME: This is inefficient, we shouldn't move things into newly allocated
511 // space, then move them up/around, there should only be 2 or 4 move
512 // constructions here.
513 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
514 Constructable::getNumMoveConstructorCalls() == 6);
David Blaikie402cb2c2014-06-08 19:12:31 +0000515 // Move assign the next two to shift them up and make a gap.
David Blaikie18486602014-06-08 19:33:40 +0000516 EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls());
David Blaikie402cb2c2014-06-08 19:12:31 +0000517 // Copy construct the two new elements from the parameter.
518 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
519 // All without any copy construction.
David Blaikieae8a9322014-06-08 19:12:28 +0000520 EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000521 EXPECT_EQ(this->theVector.begin() + 1, I);
David Blaikie18486602014-06-08 19:33:40 +0000522 this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000523}
Benjamin Kramer371b9b02012-06-17 11:52:22 +0000524
David Blaikie402cb2c2014-06-08 19:12:31 +0000525
526TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) {
527 SCOPED_TRACE("InsertRepeatedTest");
528
David Blaikie18486602014-06-08 19:33:40 +0000529 this->makeSequence(this->theVector, 1, 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000530 Constructable::reset();
531 auto I = this->theVector.insert(this->theVector.end(), 2, Constructable(16));
532 // Just copy construct them into newly allocated space
533 EXPECT_EQ(2, Constructable::getNumCopyConstructorCalls());
David Blaikie18486602014-06-08 19:33:40 +0000534 // Move everything across if reallocation is needed.
535 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
536 Constructable::getNumMoveConstructorCalls() == 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000537 // Without ever moving or copying anything else.
538 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
539 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
540
David Blaikie18486602014-06-08 19:33:40 +0000541 EXPECT_EQ(this->theVector.begin() + 4, I);
542 this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16);
David Blaikie402cb2c2014-06-08 19:12:31 +0000543}
544
545TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) {
546 SCOPED_TRACE("InsertRepeatedTest");
547
548 this->makeSequence(this->theVector, 10, 15);
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000549
550 // Empty insert.
Chandler Carruth0b012612012-07-30 22:17:52 +0000551 EXPECT_EQ(this->theVector.end(),
552 this->theVector.insert(this->theVector.end(),
553 0, Constructable(42)));
554 EXPECT_EQ(this->theVector.begin() + 1,
555 this->theVector.insert(this->theVector.begin() + 1,
556 0, Constructable(42)));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000557}
558
559// Insert range.
Chandler Carruth0b012612012-07-30 22:17:52 +0000560TYPED_TEST(SmallVectorTest, InsertRangeTest) {
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000561 SCOPED_TRACE("InsertRangeTest");
562
563 Constructable Arr[3] =
564 { Constructable(77), Constructable(77), Constructable(77) };
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000565
Chandler Carruth0b012612012-07-30 22:17:52 +0000566 this->makeSequence(this->theVector, 1, 3);
David Blaikie402cb2c2014-06-08 19:12:31 +0000567 Constructable::reset();
568 auto I = this->theVector.insert(this->theVector.begin() + 1, Arr, Arr + 3);
569 // Move construct the top 3 elements into newly allocated space.
570 // Possibly move the whole sequence into new space first.
571 // FIXME: This is inefficient, we shouldn't move things into newly allocated
572 // space, then move them up/around, there should only be 2 or 3 move
573 // constructions here.
574 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
575 Constructable::getNumMoveConstructorCalls() == 5);
576 // Copy assign the lower 2 new elements into existing space.
577 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
578 // Copy construct the third element into newly allocated space.
579 EXPECT_EQ(1, Constructable::getNumCopyConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000580 EXPECT_EQ(this->theVector.begin() + 1, I);
581 this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3);
David Blaikie402cb2c2014-06-08 19:12:31 +0000582}
583
584
585TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) {
586 SCOPED_TRACE("InsertRangeTest");
587
588 Constructable Arr[3] =
589 { Constructable(77), Constructable(77), Constructable(77) };
590
591 this->makeSequence(this->theVector, 1, 3);
Benjamin Kramer371b9b02012-06-17 11:52:22 +0000592
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000593 // Insert at end.
David Blaikie402cb2c2014-06-08 19:12:31 +0000594 Constructable::reset();
595 auto I = this->theVector.insert(this->theVector.end(), Arr, Arr+3);
596 // Copy construct the 3 elements into new space at the top.
597 EXPECT_EQ(3, Constructable::getNumCopyConstructorCalls());
598 // Don't copy/move anything else.
599 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
600 // Reallocation might occur, causing all elements to be moved into the new
601 // buffer.
602 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
603 Constructable::getNumMoveConstructorCalls() == 3);
604 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
605 EXPECT_EQ(this->theVector.begin() + 3, I);
606 this->assertValuesInOrder(this->theVector, 6u,
607 1, 2, 3, 77, 77, 77);
608}
609
610TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
611 SCOPED_TRACE("InsertRangeTest");
612
613 this->makeSequence(this->theVector, 1, 3);
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000614
615 // Empty insert.
Chandler Carruth0b012612012-07-30 22:17:52 +0000616 EXPECT_EQ(this->theVector.end(),
617 this->theVector.insert(this->theVector.end(),
618 this->theVector.begin(),
619 this->theVector.begin()));
620 EXPECT_EQ(this->theVector.begin() + 1,
621 this->theVector.insert(this->theVector.begin() + 1,
622 this->theVector.begin(),
623 this->theVector.begin()));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000624}
625
626// Comparison tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000627TYPED_TEST(SmallVectorTest, ComparisonTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000628 SCOPED_TRACE("ComparisonTest");
629
Chandler Carruth0b012612012-07-30 22:17:52 +0000630 this->makeSequence(this->theVector, 1, 3);
631 this->makeSequence(this->otherVector, 1, 3);
Owen Anderson145a2602011-07-06 22:36:59 +0000632
Chandler Carruth0b012612012-07-30 22:17:52 +0000633 EXPECT_TRUE(this->theVector == this->otherVector);
634 EXPECT_FALSE(this->theVector != this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000635
Chandler Carruth0b012612012-07-30 22:17:52 +0000636 this->otherVector.clear();
637 this->makeSequence(this->otherVector, 2, 4);
Owen Anderson145a2602011-07-06 22:36:59 +0000638
Chandler Carruth0b012612012-07-30 22:17:52 +0000639 EXPECT_FALSE(this->theVector == this->otherVector);
640 EXPECT_TRUE(this->theVector != this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000641}
642
643// Constant vector tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000644TYPED_TEST(SmallVectorTest, ConstVectorTest) {
645 const TypeParam constVector;
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000646
647 EXPECT_EQ(0u, constVector.size());
648 EXPECT_TRUE(constVector.empty());
649 EXPECT_TRUE(constVector.begin() == constVector.end());
650}
651
Daniel Dunbar825e9552009-08-19 17:48:28 +0000652// Direct array access.
Chandler Carruth0b012612012-07-30 22:17:52 +0000653TYPED_TEST(SmallVectorTest, DirectVectorTest) {
654 EXPECT_EQ(0u, this->theVector.size());
655 this->theVector.reserve(4);
656 EXPECT_LE(4u, this->theVector.capacity());
Daniel Dunbar825e9552009-08-19 17:48:28 +0000657 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000658 this->theVector.push_back(1);
659 this->theVector.push_back(2);
660 this->theVector.push_back(3);
661 this->theVector.push_back(4);
Chandler Carruth0b012612012-07-30 22:17:52 +0000662 EXPECT_EQ(4u, this->theVector.size());
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000663 EXPECT_EQ(8, Constructable::getNumConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000664 EXPECT_EQ(1, this->theVector[0].getValue());
665 EXPECT_EQ(2, this->theVector[1].getValue());
666 EXPECT_EQ(3, this->theVector[2].getValue());
667 EXPECT_EQ(4, this->theVector[3].getValue());
Daniel Dunbar825e9552009-08-19 17:48:28 +0000668}
669
Chandler Carruth0b012612012-07-30 22:17:52 +0000670TYPED_TEST(SmallVectorTest, IteratorTest) {
Dan Gohman42e77df2010-03-26 18:53:37 +0000671 std::list<int> L;
Chandler Carruth0b012612012-07-30 22:17:52 +0000672 this->theVector.insert(this->theVector.end(), L.begin(), L.end());
Dan Gohman42e77df2010-03-26 18:53:37 +0000673}
674
Lang Hamesac92bfc2015-01-23 06:25:17 +0000675template <typename InvalidType> class DualSmallVectorsTest;
676
677template <typename VectorT1, typename VectorT2>
678class DualSmallVectorsTest<std::pair<VectorT1, VectorT2>> : public SmallVectorTestBase {
679protected:
680 VectorT1 theVector;
681 VectorT2 otherVector;
682
683 template <typename T, unsigned N>
684 static unsigned NumBuiltinElts(const SmallVector<T, N>&) { return N; }
685};
686
687typedef ::testing::Types<
688 // Small mode -> Small mode.
689 std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 4>>,
690 // Small mode -> Big mode.
691 std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 2>>,
692 // Big mode -> Small mode.
693 std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 4>>,
694 // Big mode -> Big mode.
695 std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 2>>
696 > DualSmallVectorTestTypes;
697
698TYPED_TEST_CASE(DualSmallVectorsTest, DualSmallVectorTestTypes);
699
700TYPED_TEST(DualSmallVectorsTest, MoveAssignment) {
701 SCOPED_TRACE("MoveAssignTest-DualVectorTypes");
702
703 // Set up our vector with four elements.
704 for (unsigned I = 0; I < 4; ++I)
705 this->otherVector.push_back(Constructable(I));
706
707 const Constructable *OrigDataPtr = this->otherVector.data();
708
709 // Move-assign from the other vector.
710 this->theVector =
711 std::move(static_cast<SmallVectorImpl<Constructable>&>(this->otherVector));
712
713 // Make sure we have the right result.
714 this->assertValuesInOrder(this->theVector, 4u, 0, 1, 2, 3);
715
716 // Make sure the # of constructor/destructor calls line up. There
717 // are two live objects after clearing the other vector.
718 this->otherVector.clear();
719 EXPECT_EQ(Constructable::getNumConstructorCalls()-4,
720 Constructable::getNumDestructorCalls());
721
722 // If the source vector (otherVector) was in small-mode, assert that we just
723 // moved the data pointer over.
724 EXPECT_TRUE(this->NumBuiltinElts(this->otherVector) == 4 ||
725 this->theVector.data() == OrigDataPtr);
726
727 // There shouldn't be any live objects any more.
728 this->theVector.clear();
729 EXPECT_EQ(Constructable::getNumConstructorCalls(),
730 Constructable::getNumDestructorCalls());
731
732 // We shouldn't have copied anything in this whole process.
733 EXPECT_EQ(Constructable::getNumCopyConstructorCalls(), 0);
734}
735
Benjamin Kramer74a12a42012-04-29 10:53:29 +0000736struct notassignable {
737 int &x;
738 notassignable(int &x) : x(x) {}
739};
740
Chandler Carruth0b012612012-07-30 22:17:52 +0000741TEST(SmallVectorCustomTest, NoAssignTest) {
Benjamin Kramer74a12a42012-04-29 10:53:29 +0000742 int x = 0;
743 SmallVector<notassignable, 2> vec;
744 vec.push_back(notassignable(x));
745 x = 42;
746 EXPECT_EQ(42, vec.pop_back_val().x);
747}
748
David Blaikie789df062014-06-08 16:00:02 +0000749struct MovedFrom {
750 bool hasValue;
751 MovedFrom() : hasValue(true) {
752 }
753 MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) {
754 m.hasValue = false;
755 }
756 MovedFrom &operator=(MovedFrom&& m) {
757 hasValue = m.hasValue;
758 m.hasValue = false;
759 return *this;
760 }
761};
762
763TEST(SmallVectorTest, MidInsert) {
764 SmallVector<MovedFrom, 3> v;
765 v.push_back(MovedFrom());
766 v.insert(v.begin(), MovedFrom());
767 for (MovedFrom &m : v)
768 EXPECT_TRUE(m.hasValue);
769}
770
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000771enum EmplaceableArgState {
772 EAS_Defaulted,
773 EAS_Arg,
774 EAS_LValue,
775 EAS_RValue,
776 EAS_Failure
777};
778template <int I> struct EmplaceableArg {
779 EmplaceableArgState State;
780 EmplaceableArg() : State(EAS_Defaulted) {}
781 EmplaceableArg(EmplaceableArg &&X)
782 : State(X.State == EAS_Arg ? EAS_RValue : EAS_Failure) {}
783 EmplaceableArg(EmplaceableArg &X)
784 : State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {}
785
786 explicit EmplaceableArg(bool) : State(EAS_Arg) {}
787
788private:
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000789 EmplaceableArg &operator=(EmplaceableArg &&) LLVM_DELETED_FUNCTION;
790 EmplaceableArg &operator=(const EmplaceableArg &) LLVM_DELETED_FUNCTION;
791};
792
793enum EmplaceableState { ES_Emplaced, ES_Moved };
794struct Emplaceable {
795 EmplaceableArg<0> A0;
796 EmplaceableArg<1> A1;
797 EmplaceableArg<2> A2;
798 EmplaceableArg<3> A3;
799 EmplaceableState State;
800
801 Emplaceable() : State(ES_Emplaced) {}
802
803 template <class A0Ty>
804 explicit Emplaceable(A0Ty &&A0)
805 : A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {}
806
807 template <class A0Ty, class A1Ty>
808 Emplaceable(A0Ty &&A0, A1Ty &&A1)
809 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
810 State(ES_Emplaced) {}
811
812 template <class A0Ty, class A1Ty, class A2Ty>
813 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2)
814 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
815 A2(std::forward<A2Ty>(A2)), State(ES_Emplaced) {}
816
817 template <class A0Ty, class A1Ty, class A2Ty, class A3Ty>
818 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2, A3Ty &&A3)
819 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
820 A2(std::forward<A2Ty>(A2)), A3(std::forward<A3Ty>(A3)),
821 State(ES_Emplaced) {}
822
823 Emplaceable(Emplaceable &&) : State(ES_Moved) {}
824 Emplaceable &operator=(Emplaceable &&) {
825 State = ES_Moved;
826 return *this;
827 }
828
829private:
830 Emplaceable(const Emplaceable &) LLVM_DELETED_FUNCTION;
831 Emplaceable &operator=(const Emplaceable &) LLVM_DELETED_FUNCTION;
832};
833
834TEST(SmallVectorTest, EmplaceBack) {
835 EmplaceableArg<0> A0(true);
836 EmplaceableArg<1> A1(true);
837 EmplaceableArg<2> A2(true);
838 EmplaceableArg<3> A3(true);
839 {
840 SmallVector<Emplaceable, 3> V;
841 V.emplace_back();
842 EXPECT_TRUE(V.size() == 1);
843 EXPECT_TRUE(V.back().State == ES_Emplaced);
844 EXPECT_TRUE(V.back().A0.State == EAS_Defaulted);
845 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
846 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
847 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
848 }
849 {
850 SmallVector<Emplaceable, 3> V;
851 V.emplace_back(std::move(A0));
852 EXPECT_TRUE(V.size() == 1);
853 EXPECT_TRUE(V.back().State == ES_Emplaced);
854 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
855 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
856 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
857 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
858 }
859 {
860 SmallVector<Emplaceable, 3> V;
861 V.emplace_back(A0);
862 EXPECT_TRUE(V.size() == 1);
863 EXPECT_TRUE(V.back().State == ES_Emplaced);
864 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
865 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
866 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
867 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
868 }
869 {
870 SmallVector<Emplaceable, 3> V;
871 V.emplace_back(A0, A1);
872 EXPECT_TRUE(V.size() == 1);
873 EXPECT_TRUE(V.back().State == ES_Emplaced);
874 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
875 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
876 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
877 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
878 }
879 {
880 SmallVector<Emplaceable, 3> V;
881 V.emplace_back(std::move(A0), std::move(A1));
882 EXPECT_TRUE(V.size() == 1);
883 EXPECT_TRUE(V.back().State == ES_Emplaced);
884 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
885 EXPECT_TRUE(V.back().A1.State == EAS_RValue);
886 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
887 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
888 }
889 {
890 SmallVector<Emplaceable, 3> V;
891 V.emplace_back(std::move(A0), A1, std::move(A2), A3);
892 EXPECT_TRUE(V.size() == 1);
893 EXPECT_TRUE(V.back().State == ES_Emplaced);
894 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
895 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
896 EXPECT_TRUE(V.back().A2.State == EAS_RValue);
897 EXPECT_TRUE(V.back().A3.State == EAS_LValue);
898 }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000899}
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000900
901} // end namespace