blob: 46f7021ac165d537dcd1891e3f124aa782234ccb [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
Benjamin Kramera6d7acc2015-03-02 21:16:04 +000014#include "llvm/ADT/ArrayRef.h"
Bill Wendlingc56c37f2009-01-10 12:56:31 +000015#include "llvm/ADT/SmallVector.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>
Chandler Carruth130cec22012-12-04 10:23:08 +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 {
144 NonCopyable() {}
145 NonCopyable(NonCopyable &&) {}
146 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
203
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
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000212// New vector test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000213TYPED_TEST(SmallVectorTest, EmptyVectorTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000214 SCOPED_TRACE("EmptyVectorTest");
Chandler Carruth0b012612012-07-30 22:17:52 +0000215 this->assertEmpty(this->theVector);
216 EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000217 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
218 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
219}
220
221// Simple insertions and deletions.
Chandler Carruth0b012612012-07-30 22:17:52 +0000222TYPED_TEST(SmallVectorTest, PushPopTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000223 SCOPED_TRACE("PushPopTest");
224
Chandler Carruth0b012612012-07-30 22:17:52 +0000225 // Track whether the vector will potentially have to grow.
226 bool RequiresGrowth = this->theVector.capacity() < 3;
227
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000228 // Push an element
Chandler Carruth0b012612012-07-30 22:17:52 +0000229 this->theVector.push_back(Constructable(1));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000230
231 // Size tests
Chandler Carruth0b012612012-07-30 22:17:52 +0000232 this->assertValuesInOrder(this->theVector, 1u, 1);
233 EXPECT_FALSE(this->theVector.begin() == this->theVector.end());
234 EXPECT_FALSE(this->theVector.empty());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000235
236 // Push another element
Chandler Carruth0b012612012-07-30 22:17:52 +0000237 this->theVector.push_back(Constructable(2));
238 this->assertValuesInOrder(this->theVector, 2u, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000239
Owen Anderson145a2602011-07-06 22:36:59 +0000240 // Insert at beginning
Chandler Carruth0b012612012-07-30 22:17:52 +0000241 this->theVector.insert(this->theVector.begin(), this->theVector[1]);
242 this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2);
Owen Anderson145a2602011-07-06 22:36:59 +0000243
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000244 // Pop one element
Chandler Carruth0b012612012-07-30 22:17:52 +0000245 this->theVector.pop_back();
246 this->assertValuesInOrder(this->theVector, 2u, 2, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000247
Owen Anderson145a2602011-07-06 22:36:59 +0000248 // Pop remaining elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000249 this->theVector.pop_back();
250 this->theVector.pop_back();
251 this->assertEmpty(this->theVector);
Owen Anderson145a2602011-07-06 22:36:59 +0000252
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000253 // Check number of constructor calls. Should be 2 for each list element,
Owen Anderson145a2602011-07-06 22:36:59 +0000254 // one for the argument to push_back, one for the argument to insert,
255 // and one for the list element itself.
Chandler Carruth0b012612012-07-30 22:17:52 +0000256 if (!RequiresGrowth) {
257 EXPECT_EQ(5, Constructable::getNumConstructorCalls());
258 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
259 } else {
260 // If we had to grow the vector, these only have a lower bound, but should
261 // always be equal.
262 EXPECT_LE(5, Constructable::getNumConstructorCalls());
263 EXPECT_EQ(Constructable::getNumConstructorCalls(),
264 Constructable::getNumDestructorCalls());
265 }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000266}
267
268// Clear test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000269TYPED_TEST(SmallVectorTest, ClearTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000270 SCOPED_TRACE("ClearTest");
271
Chandler Carruth0b012612012-07-30 22:17:52 +0000272 this->theVector.reserve(2);
273 this->makeSequence(this->theVector, 1, 2);
274 this->theVector.clear();
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000275
Chandler Carruth0b012612012-07-30 22:17:52 +0000276 this->assertEmpty(this->theVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000277 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
278 EXPECT_EQ(4, Constructable::getNumDestructorCalls());
279}
280
281// Resize smaller test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000282TYPED_TEST(SmallVectorTest, ResizeShrinkTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000283 SCOPED_TRACE("ResizeShrinkTest");
284
Chandler Carruth0b012612012-07-30 22:17:52 +0000285 this->theVector.reserve(3);
286 this->makeSequence(this->theVector, 1, 3);
287 this->theVector.resize(1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000288
Chandler Carruth0b012612012-07-30 22:17:52 +0000289 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000290 EXPECT_EQ(6, Constructable::getNumConstructorCalls());
291 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
292}
293
294// Resize bigger test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000295TYPED_TEST(SmallVectorTest, ResizeGrowTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000296 SCOPED_TRACE("ResizeGrowTest");
297
Chandler Carruth0b012612012-07-30 22:17:52 +0000298 this->theVector.resize(2);
Owen Anderson145a2602011-07-06 22:36:59 +0000299
David Blaikie669fc862014-06-09 22:26:20 +0000300 EXPECT_EQ(2, Constructable::getNumConstructorCalls());
301 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000302 EXPECT_EQ(2u, this->theVector.size());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000303}
304
David Blaikie669fc862014-06-09 22:26:20 +0000305TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) {
306 this->theVector.resize(2);
307
308 Constructable::reset();
309
310 this->theVector.resize(4);
311
312 size_t Ctors = Constructable::getNumConstructorCalls();
313 EXPECT_TRUE(Ctors == 2 || Ctors == 4);
314 size_t MoveCtors = Constructable::getNumMoveConstructorCalls();
315 EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2);
316 size_t Dtors = Constructable::getNumDestructorCalls();
317 EXPECT_TRUE(Dtors == 0 || Dtors == 2);
318}
319
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000320// Resize with fill value.
Chandler Carruth0b012612012-07-30 22:17:52 +0000321TYPED_TEST(SmallVectorTest, ResizeFillTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000322 SCOPED_TRACE("ResizeFillTest");
323
Chandler Carruth0b012612012-07-30 22:17:52 +0000324 this->theVector.resize(3, Constructable(77));
325 this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000326}
327
328// Overflow past fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000329TYPED_TEST(SmallVectorTest, OverflowTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000330 SCOPED_TRACE("OverflowTest");
331
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000332 // Push more elements than the fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000333 this->makeSequence(this->theVector, 1, 10);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000334
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000335 // Test size and values.
Chandler Carruth0b012612012-07-30 22:17:52 +0000336 EXPECT_EQ(10u, this->theVector.size());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000337 for (int i = 0; i < 10; ++i) {
Chandler Carruth0b012612012-07-30 22:17:52 +0000338 EXPECT_EQ(i+1, this->theVector[i].getValue());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000339 }
Owen Anderson145a2602011-07-06 22:36:59 +0000340
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000341 // Now resize back to fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000342 this->theVector.resize(1);
Owen Anderson145a2602011-07-06 22:36:59 +0000343
Chandler Carruth0b012612012-07-30 22:17:52 +0000344 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000345}
346
347// Iteration tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000348TYPED_TEST(SmallVectorTest, IterationTest) {
349 this->makeSequence(this->theVector, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000350
351 // Forward Iteration
Chandler Carruth0b012612012-07-30 22:17:52 +0000352 typename TypeParam::iterator it = this->theVector.begin();
353 EXPECT_TRUE(*it == this->theVector.front());
354 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000355 EXPECT_EQ(1, it->getValue());
356 ++it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000357 EXPECT_TRUE(*it == this->theVector[1]);
358 EXPECT_TRUE(*it == this->theVector.back());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000359 EXPECT_EQ(2, it->getValue());
360 ++it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000361 EXPECT_TRUE(it == this->theVector.end());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000362 --it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000363 EXPECT_TRUE(*it == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000364 EXPECT_EQ(2, it->getValue());
365 --it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000366 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000367 EXPECT_EQ(1, it->getValue());
368
369 // Reverse Iteration
Chandler Carruth0b012612012-07-30 22:17:52 +0000370 typename TypeParam::reverse_iterator rit = this->theVector.rbegin();
371 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000372 EXPECT_EQ(2, rit->getValue());
373 ++rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000374 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000375 EXPECT_EQ(1, rit->getValue());
376 ++rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000377 EXPECT_TRUE(rit == this->theVector.rend());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000378 --rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000379 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000380 EXPECT_EQ(1, rit->getValue());
381 --rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000382 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000383 EXPECT_EQ(2, rit->getValue());
384}
385
386// Swap test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000387TYPED_TEST(SmallVectorTest, SwapTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000388 SCOPED_TRACE("SwapTest");
389
Chandler Carruth0b012612012-07-30 22:17:52 +0000390 this->makeSequence(this->theVector, 1, 2);
391 std::swap(this->theVector, this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000392
Chandler Carruth0b012612012-07-30 22:17:52 +0000393 this->assertEmpty(this->theVector);
394 this->assertValuesInOrder(this->otherVector, 2u, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000395}
396
397// Append test
Chandler Carruth0b012612012-07-30 22:17:52 +0000398TYPED_TEST(SmallVectorTest, AppendTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000399 SCOPED_TRACE("AppendTest");
400
Chandler Carruth0b012612012-07-30 22:17:52 +0000401 this->makeSequence(this->otherVector, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000402
Chandler Carruth0b012612012-07-30 22:17:52 +0000403 this->theVector.push_back(Constructable(1));
404 this->theVector.append(this->otherVector.begin(), this->otherVector.end());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000405
Chandler Carruth0b012612012-07-30 22:17:52 +0000406 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000407}
408
409// Append repeated test
Chandler Carruth0b012612012-07-30 22:17:52 +0000410TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000411 SCOPED_TRACE("AppendRepeatedTest");
412
Chandler Carruth0b012612012-07-30 22:17:52 +0000413 this->theVector.push_back(Constructable(1));
414 this->theVector.append(2, Constructable(77));
415 this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000416}
417
418// Assign test
Chandler Carruth0b012612012-07-30 22:17:52 +0000419TYPED_TEST(SmallVectorTest, AssignTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000420 SCOPED_TRACE("AssignTest");
421
Chandler Carruth0b012612012-07-30 22:17:52 +0000422 this->theVector.push_back(Constructable(1));
423 this->theVector.assign(2, Constructable(77));
424 this->assertValuesInOrder(this->theVector, 2u, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000425}
426
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000427// Move-assign test
428TYPED_TEST(SmallVectorTest, MoveAssignTest) {
429 SCOPED_TRACE("MoveAssignTest");
430
431 // Set up our vector with a single element, but enough capacity for 4.
432 this->theVector.reserve(4);
433 this->theVector.push_back(Constructable(1));
434
435 // Set up the other vector with 2 elements.
436 this->otherVector.push_back(Constructable(2));
437 this->otherVector.push_back(Constructable(3));
438
439 // Move-assign from the other vector.
440 this->theVector = std::move(this->otherVector);
441
442 // Make sure we have the right result.
443 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
444
445 // Make sure the # of constructor/destructor calls line up. There
446 // are two live objects after clearing the other vector.
447 this->otherVector.clear();
448 EXPECT_EQ(Constructable::getNumConstructorCalls()-2,
449 Constructable::getNumDestructorCalls());
450
451 // There shouldn't be any live objects any more.
452 this->theVector.clear();
453 EXPECT_EQ(Constructable::getNumConstructorCalls(),
454 Constructable::getNumDestructorCalls());
455}
456
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000457// Erase a single element
Chandler Carruth0b012612012-07-30 22:17:52 +0000458TYPED_TEST(SmallVectorTest, EraseTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000459 SCOPED_TRACE("EraseTest");
460
Chandler Carruth0b012612012-07-30 22:17:52 +0000461 this->makeSequence(this->theVector, 1, 3);
462 this->theVector.erase(this->theVector.begin());
463 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000464}
465
466// Erase a range of elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000467TYPED_TEST(SmallVectorTest, EraseRangeTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000468 SCOPED_TRACE("EraseRangeTest");
469
Chandler Carruth0b012612012-07-30 22:17:52 +0000470 this->makeSequence(this->theVector, 1, 3);
471 this->theVector.erase(this->theVector.begin(), this->theVector.begin() + 2);
472 this->assertValuesInOrder(this->theVector, 1u, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000473}
474
475// Insert a single element.
Chandler Carruth0b012612012-07-30 22:17:52 +0000476TYPED_TEST(SmallVectorTest, InsertTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000477 SCOPED_TRACE("InsertTest");
478
Chandler Carruth0b012612012-07-30 22:17:52 +0000479 this->makeSequence(this->theVector, 1, 3);
480 typename TypeParam::iterator I =
481 this->theVector.insert(this->theVector.begin() + 1, Constructable(77));
482 EXPECT_EQ(this->theVector.begin() + 1, I);
483 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000484}
485
David Blaikie40d4e342014-06-08 16:55:13 +0000486// Insert a copy of a single element.
487TYPED_TEST(SmallVectorTest, InsertCopy) {
488 SCOPED_TRACE("InsertTest");
489
490 this->makeSequence(this->theVector, 1, 3);
491 Constructable C(77);
492 typename TypeParam::iterator I =
493 this->theVector.insert(this->theVector.begin() + 1, C);
494 EXPECT_EQ(this->theVector.begin() + 1, I);
495 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
496}
497
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000498// Insert repeated elements.
Chandler Carruth0b012612012-07-30 22:17:52 +0000499TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000500 SCOPED_TRACE("InsertRepeatedTest");
501
David Blaikie18486602014-06-08 19:33:40 +0000502 this->makeSequence(this->theVector, 1, 4);
David Blaikieae8a9322014-06-08 19:12:28 +0000503 Constructable::reset();
David Blaikie402cb2c2014-06-08 19:12:31 +0000504 auto I =
505 this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
David Blaikie18486602014-06-08 19:33:40 +0000506 // Move construct the top element into newly allocated space, and optionally
507 // reallocate the whole buffer, move constructing into it.
508 // FIXME: This is inefficient, we shouldn't move things into newly allocated
509 // space, then move them up/around, there should only be 2 or 4 move
510 // constructions here.
511 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
512 Constructable::getNumMoveConstructorCalls() == 6);
David Blaikie402cb2c2014-06-08 19:12:31 +0000513 // Move assign the next two to shift them up and make a gap.
David Blaikie18486602014-06-08 19:33:40 +0000514 EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls());
David Blaikie402cb2c2014-06-08 19:12:31 +0000515 // Copy construct the two new elements from the parameter.
516 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
517 // All without any copy construction.
David Blaikieae8a9322014-06-08 19:12:28 +0000518 EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000519 EXPECT_EQ(this->theVector.begin() + 1, I);
David Blaikie18486602014-06-08 19:33:40 +0000520 this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000521}
Benjamin Kramer371b9b02012-06-17 11:52:22 +0000522
David Blaikie402cb2c2014-06-08 19:12:31 +0000523
524TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) {
525 SCOPED_TRACE("InsertRepeatedTest");
526
David Blaikie18486602014-06-08 19:33:40 +0000527 this->makeSequence(this->theVector, 1, 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000528 Constructable::reset();
529 auto I = this->theVector.insert(this->theVector.end(), 2, Constructable(16));
530 // Just copy construct them into newly allocated space
531 EXPECT_EQ(2, Constructable::getNumCopyConstructorCalls());
David Blaikie18486602014-06-08 19:33:40 +0000532 // Move everything across if reallocation is needed.
533 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
534 Constructable::getNumMoveConstructorCalls() == 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000535 // Without ever moving or copying anything else.
536 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
537 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
538
David Blaikie18486602014-06-08 19:33:40 +0000539 EXPECT_EQ(this->theVector.begin() + 4, I);
540 this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16);
David Blaikie402cb2c2014-06-08 19:12:31 +0000541}
542
543TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) {
544 SCOPED_TRACE("InsertRepeatedTest");
545
546 this->makeSequence(this->theVector, 10, 15);
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000547
548 // Empty insert.
Chandler Carruth0b012612012-07-30 22:17:52 +0000549 EXPECT_EQ(this->theVector.end(),
550 this->theVector.insert(this->theVector.end(),
551 0, Constructable(42)));
552 EXPECT_EQ(this->theVector.begin() + 1,
553 this->theVector.insert(this->theVector.begin() + 1,
554 0, Constructable(42)));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000555}
556
557// Insert range.
Chandler Carruth0b012612012-07-30 22:17:52 +0000558TYPED_TEST(SmallVectorTest, InsertRangeTest) {
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000559 SCOPED_TRACE("InsertRangeTest");
560
561 Constructable Arr[3] =
562 { Constructable(77), Constructable(77), Constructable(77) };
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000563
Chandler Carruth0b012612012-07-30 22:17:52 +0000564 this->makeSequence(this->theVector, 1, 3);
David Blaikie402cb2c2014-06-08 19:12:31 +0000565 Constructable::reset();
566 auto I = this->theVector.insert(this->theVector.begin() + 1, Arr, Arr + 3);
567 // Move construct the top 3 elements into newly allocated space.
568 // Possibly move the whole sequence into new space first.
569 // FIXME: This is inefficient, we shouldn't move things into newly allocated
570 // space, then move them up/around, there should only be 2 or 3 move
571 // constructions here.
572 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
573 Constructable::getNumMoveConstructorCalls() == 5);
574 // Copy assign the lower 2 new elements into existing space.
575 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
576 // Copy construct the third element into newly allocated space.
577 EXPECT_EQ(1, Constructable::getNumCopyConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000578 EXPECT_EQ(this->theVector.begin() + 1, I);
579 this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3);
David Blaikie402cb2c2014-06-08 19:12:31 +0000580}
581
582
583TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) {
584 SCOPED_TRACE("InsertRangeTest");
585
586 Constructable Arr[3] =
587 { Constructable(77), Constructable(77), Constructable(77) };
588
589 this->makeSequence(this->theVector, 1, 3);
Benjamin Kramer371b9b02012-06-17 11:52:22 +0000590
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000591 // Insert at end.
David Blaikie402cb2c2014-06-08 19:12:31 +0000592 Constructable::reset();
593 auto I = this->theVector.insert(this->theVector.end(), Arr, Arr+3);
594 // Copy construct the 3 elements into new space at the top.
595 EXPECT_EQ(3, Constructable::getNumCopyConstructorCalls());
596 // Don't copy/move anything else.
597 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
598 // Reallocation might occur, causing all elements to be moved into the new
599 // buffer.
600 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
601 Constructable::getNumMoveConstructorCalls() == 3);
602 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
603 EXPECT_EQ(this->theVector.begin() + 3, I);
604 this->assertValuesInOrder(this->theVector, 6u,
605 1, 2, 3, 77, 77, 77);
606}
607
608TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
609 SCOPED_TRACE("InsertRangeTest");
610
611 this->makeSequence(this->theVector, 1, 3);
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000612
613 // Empty insert.
Chandler Carruth0b012612012-07-30 22:17:52 +0000614 EXPECT_EQ(this->theVector.end(),
615 this->theVector.insert(this->theVector.end(),
616 this->theVector.begin(),
617 this->theVector.begin()));
618 EXPECT_EQ(this->theVector.begin() + 1,
619 this->theVector.insert(this->theVector.begin() + 1,
620 this->theVector.begin(),
621 this->theVector.begin()));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000622}
623
624// Comparison tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000625TYPED_TEST(SmallVectorTest, ComparisonTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000626 SCOPED_TRACE("ComparisonTest");
627
Chandler Carruth0b012612012-07-30 22:17:52 +0000628 this->makeSequence(this->theVector, 1, 3);
629 this->makeSequence(this->otherVector, 1, 3);
Owen Anderson145a2602011-07-06 22:36:59 +0000630
Chandler Carruth0b012612012-07-30 22:17:52 +0000631 EXPECT_TRUE(this->theVector == this->otherVector);
632 EXPECT_FALSE(this->theVector != this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000633
Chandler Carruth0b012612012-07-30 22:17:52 +0000634 this->otherVector.clear();
635 this->makeSequence(this->otherVector, 2, 4);
Owen Anderson145a2602011-07-06 22:36:59 +0000636
Chandler Carruth0b012612012-07-30 22:17:52 +0000637 EXPECT_FALSE(this->theVector == this->otherVector);
638 EXPECT_TRUE(this->theVector != this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000639}
640
641// Constant vector tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000642TYPED_TEST(SmallVectorTest, ConstVectorTest) {
643 const TypeParam constVector;
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000644
645 EXPECT_EQ(0u, constVector.size());
646 EXPECT_TRUE(constVector.empty());
647 EXPECT_TRUE(constVector.begin() == constVector.end());
648}
649
Daniel Dunbar825e9552009-08-19 17:48:28 +0000650// Direct array access.
Chandler Carruth0b012612012-07-30 22:17:52 +0000651TYPED_TEST(SmallVectorTest, DirectVectorTest) {
652 EXPECT_EQ(0u, this->theVector.size());
653 this->theVector.reserve(4);
654 EXPECT_LE(4u, this->theVector.capacity());
Daniel Dunbar825e9552009-08-19 17:48:28 +0000655 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000656 this->theVector.push_back(1);
657 this->theVector.push_back(2);
658 this->theVector.push_back(3);
659 this->theVector.push_back(4);
Chandler Carruth0b012612012-07-30 22:17:52 +0000660 EXPECT_EQ(4u, this->theVector.size());
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000661 EXPECT_EQ(8, Constructable::getNumConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000662 EXPECT_EQ(1, this->theVector[0].getValue());
663 EXPECT_EQ(2, this->theVector[1].getValue());
664 EXPECT_EQ(3, this->theVector[2].getValue());
665 EXPECT_EQ(4, this->theVector[3].getValue());
Daniel Dunbar825e9552009-08-19 17:48:28 +0000666}
667
Chandler Carruth0b012612012-07-30 22:17:52 +0000668TYPED_TEST(SmallVectorTest, IteratorTest) {
Dan Gohman42e77df2010-03-26 18:53:37 +0000669 std::list<int> L;
Chandler Carruth0b012612012-07-30 22:17:52 +0000670 this->theVector.insert(this->theVector.end(), L.begin(), L.end());
Dan Gohman42e77df2010-03-26 18:53:37 +0000671}
672
Lang Hamesac92bfc2015-01-23 06:25:17 +0000673template <typename InvalidType> class DualSmallVectorsTest;
674
675template <typename VectorT1, typename VectorT2>
676class DualSmallVectorsTest<std::pair<VectorT1, VectorT2>> : public SmallVectorTestBase {
677protected:
678 VectorT1 theVector;
679 VectorT2 otherVector;
680
681 template <typename T, unsigned N>
682 static unsigned NumBuiltinElts(const SmallVector<T, N>&) { return N; }
683};
684
685typedef ::testing::Types<
686 // Small mode -> Small mode.
687 std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 4>>,
688 // Small mode -> Big mode.
689 std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 2>>,
690 // Big mode -> Small mode.
691 std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 4>>,
692 // Big mode -> Big mode.
693 std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 2>>
694 > DualSmallVectorTestTypes;
695
696TYPED_TEST_CASE(DualSmallVectorsTest, DualSmallVectorTestTypes);
697
698TYPED_TEST(DualSmallVectorsTest, MoveAssignment) {
699 SCOPED_TRACE("MoveAssignTest-DualVectorTypes");
700
701 // Set up our vector with four elements.
702 for (unsigned I = 0; I < 4; ++I)
703 this->otherVector.push_back(Constructable(I));
704
705 const Constructable *OrigDataPtr = this->otherVector.data();
706
707 // Move-assign from the other vector.
708 this->theVector =
709 std::move(static_cast<SmallVectorImpl<Constructable>&>(this->otherVector));
710
711 // Make sure we have the right result.
712 this->assertValuesInOrder(this->theVector, 4u, 0, 1, 2, 3);
713
714 // Make sure the # of constructor/destructor calls line up. There
715 // are two live objects after clearing the other vector.
716 this->otherVector.clear();
717 EXPECT_EQ(Constructable::getNumConstructorCalls()-4,
718 Constructable::getNumDestructorCalls());
719
720 // If the source vector (otherVector) was in small-mode, assert that we just
721 // moved the data pointer over.
722 EXPECT_TRUE(this->NumBuiltinElts(this->otherVector) == 4 ||
723 this->theVector.data() == OrigDataPtr);
724
725 // There shouldn't be any live objects any more.
726 this->theVector.clear();
727 EXPECT_EQ(Constructable::getNumConstructorCalls(),
728 Constructable::getNumDestructorCalls());
729
730 // We shouldn't have copied anything in this whole process.
731 EXPECT_EQ(Constructable::getNumCopyConstructorCalls(), 0);
732}
733
Benjamin Kramer74a12a42012-04-29 10:53:29 +0000734struct notassignable {
735 int &x;
736 notassignable(int &x) : x(x) {}
737};
738
Chandler Carruth0b012612012-07-30 22:17:52 +0000739TEST(SmallVectorCustomTest, NoAssignTest) {
Benjamin Kramer74a12a42012-04-29 10:53:29 +0000740 int x = 0;
741 SmallVector<notassignable, 2> vec;
742 vec.push_back(notassignable(x));
743 x = 42;
744 EXPECT_EQ(42, vec.pop_back_val().x);
745}
746
David Blaikie789df062014-06-08 16:00:02 +0000747struct MovedFrom {
748 bool hasValue;
749 MovedFrom() : hasValue(true) {
750 }
751 MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) {
752 m.hasValue = false;
753 }
754 MovedFrom &operator=(MovedFrom&& m) {
755 hasValue = m.hasValue;
756 m.hasValue = false;
757 return *this;
758 }
759};
760
761TEST(SmallVectorTest, MidInsert) {
762 SmallVector<MovedFrom, 3> v;
763 v.push_back(MovedFrom());
764 v.insert(v.begin(), MovedFrom());
765 for (MovedFrom &m : v)
766 EXPECT_TRUE(m.hasValue);
767}
768
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000769enum EmplaceableArgState {
770 EAS_Defaulted,
771 EAS_Arg,
772 EAS_LValue,
773 EAS_RValue,
774 EAS_Failure
775};
776template <int I> struct EmplaceableArg {
777 EmplaceableArgState State;
778 EmplaceableArg() : State(EAS_Defaulted) {}
779 EmplaceableArg(EmplaceableArg &&X)
780 : State(X.State == EAS_Arg ? EAS_RValue : EAS_Failure) {}
781 EmplaceableArg(EmplaceableArg &X)
782 : State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {}
783
784 explicit EmplaceableArg(bool) : State(EAS_Arg) {}
785
786private:
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000787 EmplaceableArg &operator=(EmplaceableArg &&) = delete;
788 EmplaceableArg &operator=(const EmplaceableArg &) = delete;
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000789};
790
791enum EmplaceableState { ES_Emplaced, ES_Moved };
792struct Emplaceable {
793 EmplaceableArg<0> A0;
794 EmplaceableArg<1> A1;
795 EmplaceableArg<2> A2;
796 EmplaceableArg<3> A3;
797 EmplaceableState State;
798
799 Emplaceable() : State(ES_Emplaced) {}
800
801 template <class A0Ty>
802 explicit Emplaceable(A0Ty &&A0)
803 : A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {}
804
805 template <class A0Ty, class A1Ty>
806 Emplaceable(A0Ty &&A0, A1Ty &&A1)
807 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
808 State(ES_Emplaced) {}
809
810 template <class A0Ty, class A1Ty, class A2Ty>
811 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2)
812 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
813 A2(std::forward<A2Ty>(A2)), State(ES_Emplaced) {}
814
815 template <class A0Ty, class A1Ty, class A2Ty, class A3Ty>
816 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2, A3Ty &&A3)
817 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
818 A2(std::forward<A2Ty>(A2)), A3(std::forward<A3Ty>(A3)),
819 State(ES_Emplaced) {}
820
821 Emplaceable(Emplaceable &&) : State(ES_Moved) {}
822 Emplaceable &operator=(Emplaceable &&) {
823 State = ES_Moved;
824 return *this;
825 }
826
827private:
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000828 Emplaceable(const Emplaceable &) = delete;
829 Emplaceable &operator=(const Emplaceable &) = delete;
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000830};
831
832TEST(SmallVectorTest, EmplaceBack) {
833 EmplaceableArg<0> A0(true);
834 EmplaceableArg<1> A1(true);
835 EmplaceableArg<2> A2(true);
836 EmplaceableArg<3> A3(true);
837 {
838 SmallVector<Emplaceable, 3> V;
839 V.emplace_back();
840 EXPECT_TRUE(V.size() == 1);
841 EXPECT_TRUE(V.back().State == ES_Emplaced);
842 EXPECT_TRUE(V.back().A0.State == EAS_Defaulted);
843 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
844 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
845 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
846 }
847 {
848 SmallVector<Emplaceable, 3> V;
849 V.emplace_back(std::move(A0));
850 EXPECT_TRUE(V.size() == 1);
851 EXPECT_TRUE(V.back().State == ES_Emplaced);
852 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
853 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
854 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
855 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
856 }
857 {
858 SmallVector<Emplaceable, 3> V;
859 V.emplace_back(A0);
860 EXPECT_TRUE(V.size() == 1);
861 EXPECT_TRUE(V.back().State == ES_Emplaced);
862 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
863 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
864 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
865 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
866 }
867 {
868 SmallVector<Emplaceable, 3> V;
869 V.emplace_back(A0, A1);
870 EXPECT_TRUE(V.size() == 1);
871 EXPECT_TRUE(V.back().State == ES_Emplaced);
872 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
873 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
874 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
875 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
876 }
877 {
878 SmallVector<Emplaceable, 3> V;
879 V.emplace_back(std::move(A0), std::move(A1));
880 EXPECT_TRUE(V.size() == 1);
881 EXPECT_TRUE(V.back().State == ES_Emplaced);
882 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
883 EXPECT_TRUE(V.back().A1.State == EAS_RValue);
884 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
885 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
886 }
887 {
888 SmallVector<Emplaceable, 3> V;
889 V.emplace_back(std::move(A0), A1, std::move(A2), A3);
890 EXPECT_TRUE(V.size() == 1);
891 EXPECT_TRUE(V.back().State == ES_Emplaced);
892 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
893 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
894 EXPECT_TRUE(V.back().A2.State == EAS_RValue);
895 EXPECT_TRUE(V.back().A3.State == EAS_LValue);
896 }
Benjamin Kramer1d563f42015-02-07 16:41:02 +0000897 {
898 SmallVector<int, 1> V;
899 V.emplace_back();
900 V.emplace_back(42);
901 EXPECT_EQ(2U, V.size());
902 EXPECT_EQ(0, V[0]);
903 EXPECT_EQ(42, V[1]);
904 }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000905}
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000906
Benjamin Kramera6d7acc2015-03-02 21:16:04 +0000907TEST(SmallVectorTest, InitializerList) {
908 SmallVector<int, 2> V1 = {};
909 EXPECT_TRUE(V1.empty());
910 V1 = {0, 0};
911 EXPECT_TRUE(makeArrayRef(V1).equals({0, 0}));
912 V1 = {-1, -1};
913 EXPECT_TRUE(makeArrayRef(V1).equals({-1, -1}));
914
915 SmallVector<int, 2> V2 = {1, 2, 3, 4};
916 EXPECT_TRUE(makeArrayRef(V2).equals({1, 2, 3, 4}));
917 V2.assign({4});
918 EXPECT_TRUE(makeArrayRef(V2).equals({4}));
919 V2.append({3, 2});
920 EXPECT_TRUE(makeArrayRef(V2).equals({4, 3, 2}));
921 V2.insert(V2.begin() + 1, 5);
922 EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2}));
923}
924
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000925} // end namespace