blob: 7367ad470e3a254ecb058fd41b2080e0708587d1 [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);
David Blaikie6ae4bc82016-03-24 20:25:51 +0000462 const auto &theConstVector = this->theVector;
463 this->theVector.erase(theConstVector.begin());
Chandler Carruth0b012612012-07-30 22:17:52 +0000464 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000465}
466
467// Erase a range of elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000468TYPED_TEST(SmallVectorTest, EraseRangeTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000469 SCOPED_TRACE("EraseRangeTest");
470
Chandler Carruth0b012612012-07-30 22:17:52 +0000471 this->makeSequence(this->theVector, 1, 3);
David Blaikie6ae4bc82016-03-24 20:25:51 +0000472 const auto &theConstVector = this->theVector;
473 this->theVector.erase(theConstVector.begin(), theConstVector.begin() + 2);
Chandler Carruth0b012612012-07-30 22:17:52 +0000474 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:
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000789 EmplaceableArg &operator=(EmplaceableArg &&) = delete;
790 EmplaceableArg &operator=(const EmplaceableArg &) = delete;
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000791};
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:
Aaron Ballmanf9a18972015-02-15 22:54:22 +0000830 Emplaceable(const Emplaceable &) = delete;
831 Emplaceable &operator=(const Emplaceable &) = delete;
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000832};
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 }
Benjamin Kramer1d563f42015-02-07 16:41:02 +0000899 {
900 SmallVector<int, 1> V;
901 V.emplace_back();
902 V.emplace_back(42);
903 EXPECT_EQ(2U, V.size());
904 EXPECT_EQ(0, V[0]);
905 EXPECT_EQ(42, V[1]);
906 }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000907}
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000908
Benjamin Kramera6d7acc2015-03-02 21:16:04 +0000909TEST(SmallVectorTest, InitializerList) {
910 SmallVector<int, 2> V1 = {};
911 EXPECT_TRUE(V1.empty());
912 V1 = {0, 0};
913 EXPECT_TRUE(makeArrayRef(V1).equals({0, 0}));
914 V1 = {-1, -1};
915 EXPECT_TRUE(makeArrayRef(V1).equals({-1, -1}));
916
917 SmallVector<int, 2> V2 = {1, 2, 3, 4};
918 EXPECT_TRUE(makeArrayRef(V2).equals({1, 2, 3, 4}));
919 V2.assign({4});
920 EXPECT_TRUE(makeArrayRef(V2).equals({4}));
921 V2.append({3, 2});
922 EXPECT_TRUE(makeArrayRef(V2).equals({4, 3, 2}));
923 V2.insert(V2.begin() + 1, 5);
924 EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2}));
925}
926
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000927} // end namespace