blob: ba6c395e69fa165bad2ce438711f0a9f73ae3e48 [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
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000156// Test fixture class
Chandler Carruth0b012612012-07-30 22:17:52 +0000157template <typename VectorT>
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000158class SmallVectorTest : public testing::Test {
159protected:
Chandler Carruth0b012612012-07-30 22:17:52 +0000160 VectorT theVector;
161 VectorT otherVector;
Owen Anderson145a2602011-07-06 22:36:59 +0000162
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000163 void SetUp() {
164 Constructable::reset();
165 }
166
Chandler Carruth0b012612012-07-30 22:17:52 +0000167 void assertEmpty(VectorT & v) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000168 // Size tests
169 EXPECT_EQ(0u, v.size());
170 EXPECT_TRUE(v.empty());
171
172 // Iterator tests
173 EXPECT_TRUE(v.begin() == v.end());
174 }
175
176 // Assert that theVector contains the specified values, in order.
Chandler Carruth0b012612012-07-30 22:17:52 +0000177 void assertValuesInOrder(VectorT & v, size_t size, ...) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000178 EXPECT_EQ(size, v.size());
Owen Anderson145a2602011-07-06 22:36:59 +0000179
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000180 va_list ap;
181 va_start(ap, size);
182 for (size_t i = 0; i < size; ++i) {
183 int value = va_arg(ap, int);
184 EXPECT_EQ(value, v[i].getValue());
185 }
186
187 va_end(ap);
188 }
Owen Anderson145a2602011-07-06 22:36:59 +0000189
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000190 // Generate a sequence of values to initialize the vector.
Chandler Carruth0b012612012-07-30 22:17:52 +0000191 void makeSequence(VectorT & v, int start, int end) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000192 for (int i = start; i <= end; ++i) {
193 v.push_back(Constructable(i));
194 }
195 }
196};
197
Chandler Carruth0b012612012-07-30 22:17:52 +0000198typedef ::testing::Types<SmallVector<Constructable, 0>,
199 SmallVector<Constructable, 1>,
200 SmallVector<Constructable, 2>,
David Blaikie18486602014-06-08 19:33:40 +0000201 SmallVector<Constructable, 4>,
202 SmallVector<Constructable, 5>
Chandler Carruth0b012612012-07-30 22:17:52 +0000203 > SmallVectorTestTypes;
204TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes);
205
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000206// New vector test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000207TYPED_TEST(SmallVectorTest, EmptyVectorTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000208 SCOPED_TRACE("EmptyVectorTest");
Chandler Carruth0b012612012-07-30 22:17:52 +0000209 this->assertEmpty(this->theVector);
210 EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000211 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
212 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
213}
214
215// Simple insertions and deletions.
Chandler Carruth0b012612012-07-30 22:17:52 +0000216TYPED_TEST(SmallVectorTest, PushPopTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000217 SCOPED_TRACE("PushPopTest");
218
Chandler Carruth0b012612012-07-30 22:17:52 +0000219 // Track whether the vector will potentially have to grow.
220 bool RequiresGrowth = this->theVector.capacity() < 3;
221
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000222 // Push an element
Chandler Carruth0b012612012-07-30 22:17:52 +0000223 this->theVector.push_back(Constructable(1));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000224
225 // Size tests
Chandler Carruth0b012612012-07-30 22:17:52 +0000226 this->assertValuesInOrder(this->theVector, 1u, 1);
227 EXPECT_FALSE(this->theVector.begin() == this->theVector.end());
228 EXPECT_FALSE(this->theVector.empty());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000229
230 // Push another element
Chandler Carruth0b012612012-07-30 22:17:52 +0000231 this->theVector.push_back(Constructable(2));
232 this->assertValuesInOrder(this->theVector, 2u, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000233
Owen Anderson145a2602011-07-06 22:36:59 +0000234 // Insert at beginning
Chandler Carruth0b012612012-07-30 22:17:52 +0000235 this->theVector.insert(this->theVector.begin(), this->theVector[1]);
236 this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2);
Owen Anderson145a2602011-07-06 22:36:59 +0000237
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000238 // Pop one element
Chandler Carruth0b012612012-07-30 22:17:52 +0000239 this->theVector.pop_back();
240 this->assertValuesInOrder(this->theVector, 2u, 2, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000241
Owen Anderson145a2602011-07-06 22:36:59 +0000242 // Pop remaining elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000243 this->theVector.pop_back();
244 this->theVector.pop_back();
245 this->assertEmpty(this->theVector);
Owen Anderson145a2602011-07-06 22:36:59 +0000246
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000247 // Check number of constructor calls. Should be 2 for each list element,
Owen Anderson145a2602011-07-06 22:36:59 +0000248 // one for the argument to push_back, one for the argument to insert,
249 // and one for the list element itself.
Chandler Carruth0b012612012-07-30 22:17:52 +0000250 if (!RequiresGrowth) {
251 EXPECT_EQ(5, Constructable::getNumConstructorCalls());
252 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
253 } else {
254 // If we had to grow the vector, these only have a lower bound, but should
255 // always be equal.
256 EXPECT_LE(5, Constructable::getNumConstructorCalls());
257 EXPECT_EQ(Constructable::getNumConstructorCalls(),
258 Constructable::getNumDestructorCalls());
259 }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000260}
261
262// Clear test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000263TYPED_TEST(SmallVectorTest, ClearTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000264 SCOPED_TRACE("ClearTest");
265
Chandler Carruth0b012612012-07-30 22:17:52 +0000266 this->theVector.reserve(2);
267 this->makeSequence(this->theVector, 1, 2);
268 this->theVector.clear();
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000269
Chandler Carruth0b012612012-07-30 22:17:52 +0000270 this->assertEmpty(this->theVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000271 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
272 EXPECT_EQ(4, Constructable::getNumDestructorCalls());
273}
274
275// Resize smaller test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000276TYPED_TEST(SmallVectorTest, ResizeShrinkTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000277 SCOPED_TRACE("ResizeShrinkTest");
278
Chandler Carruth0b012612012-07-30 22:17:52 +0000279 this->theVector.reserve(3);
280 this->makeSequence(this->theVector, 1, 3);
281 this->theVector.resize(1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000282
Chandler Carruth0b012612012-07-30 22:17:52 +0000283 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000284 EXPECT_EQ(6, Constructable::getNumConstructorCalls());
285 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
286}
287
288// Resize bigger test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000289TYPED_TEST(SmallVectorTest, ResizeGrowTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000290 SCOPED_TRACE("ResizeGrowTest");
291
Chandler Carruth0b012612012-07-30 22:17:52 +0000292 this->theVector.resize(2);
Owen Anderson145a2602011-07-06 22:36:59 +0000293
David Blaikie669fc862014-06-09 22:26:20 +0000294 EXPECT_EQ(2, Constructable::getNumConstructorCalls());
295 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000296 EXPECT_EQ(2u, this->theVector.size());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000297}
298
David Blaikie669fc862014-06-09 22:26:20 +0000299TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) {
300 this->theVector.resize(2);
301
302 Constructable::reset();
303
304 this->theVector.resize(4);
305
306 size_t Ctors = Constructable::getNumConstructorCalls();
307 EXPECT_TRUE(Ctors == 2 || Ctors == 4);
308 size_t MoveCtors = Constructable::getNumMoveConstructorCalls();
309 EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2);
310 size_t Dtors = Constructable::getNumDestructorCalls();
311 EXPECT_TRUE(Dtors == 0 || Dtors == 2);
312}
313
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000314// Resize with fill value.
Chandler Carruth0b012612012-07-30 22:17:52 +0000315TYPED_TEST(SmallVectorTest, ResizeFillTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000316 SCOPED_TRACE("ResizeFillTest");
317
Chandler Carruth0b012612012-07-30 22:17:52 +0000318 this->theVector.resize(3, Constructable(77));
319 this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000320}
321
322// Overflow past fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000323TYPED_TEST(SmallVectorTest, OverflowTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000324 SCOPED_TRACE("OverflowTest");
325
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000326 // Push more elements than the fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000327 this->makeSequence(this->theVector, 1, 10);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000328
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000329 // Test size and values.
Chandler Carruth0b012612012-07-30 22:17:52 +0000330 EXPECT_EQ(10u, this->theVector.size());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000331 for (int i = 0; i < 10; ++i) {
Chandler Carruth0b012612012-07-30 22:17:52 +0000332 EXPECT_EQ(i+1, this->theVector[i].getValue());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000333 }
Owen Anderson145a2602011-07-06 22:36:59 +0000334
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000335 // Now resize back to fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000336 this->theVector.resize(1);
Owen Anderson145a2602011-07-06 22:36:59 +0000337
Chandler Carruth0b012612012-07-30 22:17:52 +0000338 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000339}
340
341// Iteration tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000342TYPED_TEST(SmallVectorTest, IterationTest) {
343 this->makeSequence(this->theVector, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000344
345 // Forward Iteration
Chandler Carruth0b012612012-07-30 22:17:52 +0000346 typename TypeParam::iterator it = this->theVector.begin();
347 EXPECT_TRUE(*it == this->theVector.front());
348 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000349 EXPECT_EQ(1, it->getValue());
350 ++it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000351 EXPECT_TRUE(*it == this->theVector[1]);
352 EXPECT_TRUE(*it == this->theVector.back());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000353 EXPECT_EQ(2, it->getValue());
354 ++it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000355 EXPECT_TRUE(it == this->theVector.end());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000356 --it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000357 EXPECT_TRUE(*it == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000358 EXPECT_EQ(2, it->getValue());
359 --it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000360 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000361 EXPECT_EQ(1, it->getValue());
362
363 // Reverse Iteration
Chandler Carruth0b012612012-07-30 22:17:52 +0000364 typename TypeParam::reverse_iterator rit = this->theVector.rbegin();
365 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000366 EXPECT_EQ(2, rit->getValue());
367 ++rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000368 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000369 EXPECT_EQ(1, rit->getValue());
370 ++rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000371 EXPECT_TRUE(rit == this->theVector.rend());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000372 --rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000373 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000374 EXPECT_EQ(1, rit->getValue());
375 --rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000376 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000377 EXPECT_EQ(2, rit->getValue());
378}
379
380// Swap test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000381TYPED_TEST(SmallVectorTest, SwapTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000382 SCOPED_TRACE("SwapTest");
383
Chandler Carruth0b012612012-07-30 22:17:52 +0000384 this->makeSequence(this->theVector, 1, 2);
385 std::swap(this->theVector, this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000386
Chandler Carruth0b012612012-07-30 22:17:52 +0000387 this->assertEmpty(this->theVector);
388 this->assertValuesInOrder(this->otherVector, 2u, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000389}
390
391// Append test
Chandler Carruth0b012612012-07-30 22:17:52 +0000392TYPED_TEST(SmallVectorTest, AppendTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000393 SCOPED_TRACE("AppendTest");
394
Chandler Carruth0b012612012-07-30 22:17:52 +0000395 this->makeSequence(this->otherVector, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000396
Chandler Carruth0b012612012-07-30 22:17:52 +0000397 this->theVector.push_back(Constructable(1));
398 this->theVector.append(this->otherVector.begin(), this->otherVector.end());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000399
Chandler Carruth0b012612012-07-30 22:17:52 +0000400 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000401}
402
403// Append repeated test
Chandler Carruth0b012612012-07-30 22:17:52 +0000404TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000405 SCOPED_TRACE("AppendRepeatedTest");
406
Chandler Carruth0b012612012-07-30 22:17:52 +0000407 this->theVector.push_back(Constructable(1));
408 this->theVector.append(2, Constructable(77));
409 this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000410}
411
412// Assign test
Chandler Carruth0b012612012-07-30 22:17:52 +0000413TYPED_TEST(SmallVectorTest, AssignTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000414 SCOPED_TRACE("AssignTest");
415
Chandler Carruth0b012612012-07-30 22:17:52 +0000416 this->theVector.push_back(Constructable(1));
417 this->theVector.assign(2, Constructable(77));
418 this->assertValuesInOrder(this->theVector, 2u, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000419}
420
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000421// Move-assign test
422TYPED_TEST(SmallVectorTest, MoveAssignTest) {
423 SCOPED_TRACE("MoveAssignTest");
424
425 // Set up our vector with a single element, but enough capacity for 4.
426 this->theVector.reserve(4);
427 this->theVector.push_back(Constructable(1));
428
429 // Set up the other vector with 2 elements.
430 this->otherVector.push_back(Constructable(2));
431 this->otherVector.push_back(Constructable(3));
432
433 // Move-assign from the other vector.
434 this->theVector = std::move(this->otherVector);
435
436 // Make sure we have the right result.
437 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
438
439 // Make sure the # of constructor/destructor calls line up. There
440 // are two live objects after clearing the other vector.
441 this->otherVector.clear();
442 EXPECT_EQ(Constructable::getNumConstructorCalls()-2,
443 Constructable::getNumDestructorCalls());
444
445 // There shouldn't be any live objects any more.
446 this->theVector.clear();
447 EXPECT_EQ(Constructable::getNumConstructorCalls(),
448 Constructable::getNumDestructorCalls());
449}
450
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000451// Erase a single element
Chandler Carruth0b012612012-07-30 22:17:52 +0000452TYPED_TEST(SmallVectorTest, EraseTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000453 SCOPED_TRACE("EraseTest");
454
Chandler Carruth0b012612012-07-30 22:17:52 +0000455 this->makeSequence(this->theVector, 1, 3);
456 this->theVector.erase(this->theVector.begin());
457 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000458}
459
460// Erase a range of elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000461TYPED_TEST(SmallVectorTest, EraseRangeTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000462 SCOPED_TRACE("EraseRangeTest");
463
Chandler Carruth0b012612012-07-30 22:17:52 +0000464 this->makeSequence(this->theVector, 1, 3);
465 this->theVector.erase(this->theVector.begin(), this->theVector.begin() + 2);
466 this->assertValuesInOrder(this->theVector, 1u, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000467}
468
469// Insert a single element.
Chandler Carruth0b012612012-07-30 22:17:52 +0000470TYPED_TEST(SmallVectorTest, InsertTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000471 SCOPED_TRACE("InsertTest");
472
Chandler Carruth0b012612012-07-30 22:17:52 +0000473 this->makeSequence(this->theVector, 1, 3);
474 typename TypeParam::iterator I =
475 this->theVector.insert(this->theVector.begin() + 1, Constructable(77));
476 EXPECT_EQ(this->theVector.begin() + 1, I);
477 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000478}
479
David Blaikie40d4e342014-06-08 16:55:13 +0000480// Insert a copy of a single element.
481TYPED_TEST(SmallVectorTest, InsertCopy) {
482 SCOPED_TRACE("InsertTest");
483
484 this->makeSequence(this->theVector, 1, 3);
485 Constructable C(77);
486 typename TypeParam::iterator I =
487 this->theVector.insert(this->theVector.begin() + 1, C);
488 EXPECT_EQ(this->theVector.begin() + 1, I);
489 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
490}
491
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000492// Insert repeated elements.
Chandler Carruth0b012612012-07-30 22:17:52 +0000493TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000494 SCOPED_TRACE("InsertRepeatedTest");
495
David Blaikie18486602014-06-08 19:33:40 +0000496 this->makeSequence(this->theVector, 1, 4);
David Blaikieae8a9322014-06-08 19:12:28 +0000497 Constructable::reset();
David Blaikie402cb2c2014-06-08 19:12:31 +0000498 auto I =
499 this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
David Blaikie18486602014-06-08 19:33:40 +0000500 // Move construct the top element into newly allocated space, and optionally
501 // reallocate the whole buffer, move constructing into it.
502 // FIXME: This is inefficient, we shouldn't move things into newly allocated
503 // space, then move them up/around, there should only be 2 or 4 move
504 // constructions here.
505 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
506 Constructable::getNumMoveConstructorCalls() == 6);
David Blaikie402cb2c2014-06-08 19:12:31 +0000507 // Move assign the next two to shift them up and make a gap.
David Blaikie18486602014-06-08 19:33:40 +0000508 EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls());
David Blaikie402cb2c2014-06-08 19:12:31 +0000509 // Copy construct the two new elements from the parameter.
510 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
511 // All without any copy construction.
David Blaikieae8a9322014-06-08 19:12:28 +0000512 EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000513 EXPECT_EQ(this->theVector.begin() + 1, I);
David Blaikie18486602014-06-08 19:33:40 +0000514 this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000515}
Benjamin Kramer371b9b02012-06-17 11:52:22 +0000516
David Blaikie402cb2c2014-06-08 19:12:31 +0000517
518TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) {
519 SCOPED_TRACE("InsertRepeatedTest");
520
David Blaikie18486602014-06-08 19:33:40 +0000521 this->makeSequence(this->theVector, 1, 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000522 Constructable::reset();
523 auto I = this->theVector.insert(this->theVector.end(), 2, Constructable(16));
524 // Just copy construct them into newly allocated space
525 EXPECT_EQ(2, Constructable::getNumCopyConstructorCalls());
David Blaikie18486602014-06-08 19:33:40 +0000526 // Move everything across if reallocation is needed.
527 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
528 Constructable::getNumMoveConstructorCalls() == 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000529 // Without ever moving or copying anything else.
530 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
531 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
532
David Blaikie18486602014-06-08 19:33:40 +0000533 EXPECT_EQ(this->theVector.begin() + 4, I);
534 this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16);
David Blaikie402cb2c2014-06-08 19:12:31 +0000535}
536
537TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) {
538 SCOPED_TRACE("InsertRepeatedTest");
539
540 this->makeSequence(this->theVector, 10, 15);
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000541
542 // Empty insert.
Chandler Carruth0b012612012-07-30 22:17:52 +0000543 EXPECT_EQ(this->theVector.end(),
544 this->theVector.insert(this->theVector.end(),
545 0, Constructable(42)));
546 EXPECT_EQ(this->theVector.begin() + 1,
547 this->theVector.insert(this->theVector.begin() + 1,
548 0, Constructable(42)));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000549}
550
551// Insert range.
Chandler Carruth0b012612012-07-30 22:17:52 +0000552TYPED_TEST(SmallVectorTest, InsertRangeTest) {
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000553 SCOPED_TRACE("InsertRangeTest");
554
555 Constructable Arr[3] =
556 { Constructable(77), Constructable(77), Constructable(77) };
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000557
Chandler Carruth0b012612012-07-30 22:17:52 +0000558 this->makeSequence(this->theVector, 1, 3);
David Blaikie402cb2c2014-06-08 19:12:31 +0000559 Constructable::reset();
560 auto I = this->theVector.insert(this->theVector.begin() + 1, Arr, Arr + 3);
561 // Move construct the top 3 elements into newly allocated space.
562 // Possibly move the whole sequence into new space first.
563 // FIXME: This is inefficient, we shouldn't move things into newly allocated
564 // space, then move them up/around, there should only be 2 or 3 move
565 // constructions here.
566 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
567 Constructable::getNumMoveConstructorCalls() == 5);
568 // Copy assign the lower 2 new elements into existing space.
569 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
570 // Copy construct the third element into newly allocated space.
571 EXPECT_EQ(1, Constructable::getNumCopyConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000572 EXPECT_EQ(this->theVector.begin() + 1, I);
573 this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3);
David Blaikie402cb2c2014-06-08 19:12:31 +0000574}
575
576
577TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) {
578 SCOPED_TRACE("InsertRangeTest");
579
580 Constructable Arr[3] =
581 { Constructable(77), Constructable(77), Constructable(77) };
582
583 this->makeSequence(this->theVector, 1, 3);
Benjamin Kramer371b9b02012-06-17 11:52:22 +0000584
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000585 // Insert at end.
David Blaikie402cb2c2014-06-08 19:12:31 +0000586 Constructable::reset();
587 auto I = this->theVector.insert(this->theVector.end(), Arr, Arr+3);
588 // Copy construct the 3 elements into new space at the top.
589 EXPECT_EQ(3, Constructable::getNumCopyConstructorCalls());
590 // Don't copy/move anything else.
591 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
592 // Reallocation might occur, causing all elements to be moved into the new
593 // buffer.
594 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
595 Constructable::getNumMoveConstructorCalls() == 3);
596 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
597 EXPECT_EQ(this->theVector.begin() + 3, I);
598 this->assertValuesInOrder(this->theVector, 6u,
599 1, 2, 3, 77, 77, 77);
600}
601
602TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
603 SCOPED_TRACE("InsertRangeTest");
604
605 this->makeSequence(this->theVector, 1, 3);
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000606
607 // Empty insert.
Chandler Carruth0b012612012-07-30 22:17:52 +0000608 EXPECT_EQ(this->theVector.end(),
609 this->theVector.insert(this->theVector.end(),
610 this->theVector.begin(),
611 this->theVector.begin()));
612 EXPECT_EQ(this->theVector.begin() + 1,
613 this->theVector.insert(this->theVector.begin() + 1,
614 this->theVector.begin(),
615 this->theVector.begin()));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000616}
617
618// Comparison tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000619TYPED_TEST(SmallVectorTest, ComparisonTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000620 SCOPED_TRACE("ComparisonTest");
621
Chandler Carruth0b012612012-07-30 22:17:52 +0000622 this->makeSequence(this->theVector, 1, 3);
623 this->makeSequence(this->otherVector, 1, 3);
Owen Anderson145a2602011-07-06 22:36:59 +0000624
Chandler Carruth0b012612012-07-30 22:17:52 +0000625 EXPECT_TRUE(this->theVector == this->otherVector);
626 EXPECT_FALSE(this->theVector != this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000627
Chandler Carruth0b012612012-07-30 22:17:52 +0000628 this->otherVector.clear();
629 this->makeSequence(this->otherVector, 2, 4);
Owen Anderson145a2602011-07-06 22:36:59 +0000630
Chandler Carruth0b012612012-07-30 22:17:52 +0000631 EXPECT_FALSE(this->theVector == this->otherVector);
632 EXPECT_TRUE(this->theVector != this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000633}
634
635// Constant vector tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000636TYPED_TEST(SmallVectorTest, ConstVectorTest) {
637 const TypeParam constVector;
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000638
639 EXPECT_EQ(0u, constVector.size());
640 EXPECT_TRUE(constVector.empty());
641 EXPECT_TRUE(constVector.begin() == constVector.end());
642}
643
Daniel Dunbar825e9552009-08-19 17:48:28 +0000644// Direct array access.
Chandler Carruth0b012612012-07-30 22:17:52 +0000645TYPED_TEST(SmallVectorTest, DirectVectorTest) {
646 EXPECT_EQ(0u, this->theVector.size());
647 this->theVector.reserve(4);
648 EXPECT_LE(4u, this->theVector.capacity());
Daniel Dunbar825e9552009-08-19 17:48:28 +0000649 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000650 this->theVector.push_back(1);
651 this->theVector.push_back(2);
652 this->theVector.push_back(3);
653 this->theVector.push_back(4);
Chandler Carruth0b012612012-07-30 22:17:52 +0000654 EXPECT_EQ(4u, this->theVector.size());
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000655 EXPECT_EQ(8, Constructable::getNumConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000656 EXPECT_EQ(1, this->theVector[0].getValue());
657 EXPECT_EQ(2, this->theVector[1].getValue());
658 EXPECT_EQ(3, this->theVector[2].getValue());
659 EXPECT_EQ(4, this->theVector[3].getValue());
Daniel Dunbar825e9552009-08-19 17:48:28 +0000660}
661
Chandler Carruth0b012612012-07-30 22:17:52 +0000662TYPED_TEST(SmallVectorTest, IteratorTest) {
Dan Gohman42e77df2010-03-26 18:53:37 +0000663 std::list<int> L;
Chandler Carruth0b012612012-07-30 22:17:52 +0000664 this->theVector.insert(this->theVector.end(), L.begin(), L.end());
Dan Gohman42e77df2010-03-26 18:53:37 +0000665}
666
Benjamin Kramer74a12a42012-04-29 10:53:29 +0000667struct notassignable {
668 int &x;
669 notassignable(int &x) : x(x) {}
670};
671
Chandler Carruth0b012612012-07-30 22:17:52 +0000672TEST(SmallVectorCustomTest, NoAssignTest) {
Benjamin Kramer74a12a42012-04-29 10:53:29 +0000673 int x = 0;
674 SmallVector<notassignable, 2> vec;
675 vec.push_back(notassignable(x));
676 x = 42;
677 EXPECT_EQ(42, vec.pop_back_val().x);
678}
679
David Blaikie789df062014-06-08 16:00:02 +0000680struct MovedFrom {
681 bool hasValue;
682 MovedFrom() : hasValue(true) {
683 }
684 MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) {
685 m.hasValue = false;
686 }
687 MovedFrom &operator=(MovedFrom&& m) {
688 hasValue = m.hasValue;
689 m.hasValue = false;
690 return *this;
691 }
692};
693
694TEST(SmallVectorTest, MidInsert) {
695 SmallVector<MovedFrom, 3> v;
696 v.push_back(MovedFrom());
697 v.insert(v.begin(), MovedFrom());
698 for (MovedFrom &m : v)
699 EXPECT_TRUE(m.hasValue);
700}
701
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000702enum EmplaceableArgState {
703 EAS_Defaulted,
704 EAS_Arg,
705 EAS_LValue,
706 EAS_RValue,
707 EAS_Failure
708};
709template <int I> struct EmplaceableArg {
710 EmplaceableArgState State;
711 EmplaceableArg() : State(EAS_Defaulted) {}
712 EmplaceableArg(EmplaceableArg &&X)
713 : State(X.State == EAS_Arg ? EAS_RValue : EAS_Failure) {}
714 EmplaceableArg(EmplaceableArg &X)
715 : State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {}
716
717 explicit EmplaceableArg(bool) : State(EAS_Arg) {}
718
719private:
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000720 EmplaceableArg &operator=(EmplaceableArg &&) LLVM_DELETED_FUNCTION;
721 EmplaceableArg &operator=(const EmplaceableArg &) LLVM_DELETED_FUNCTION;
722};
723
724enum EmplaceableState { ES_Emplaced, ES_Moved };
725struct Emplaceable {
726 EmplaceableArg<0> A0;
727 EmplaceableArg<1> A1;
728 EmplaceableArg<2> A2;
729 EmplaceableArg<3> A3;
730 EmplaceableState State;
731
732 Emplaceable() : State(ES_Emplaced) {}
733
734 template <class A0Ty>
735 explicit Emplaceable(A0Ty &&A0)
736 : A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {}
737
738 template <class A0Ty, class A1Ty>
739 Emplaceable(A0Ty &&A0, A1Ty &&A1)
740 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
741 State(ES_Emplaced) {}
742
743 template <class A0Ty, class A1Ty, class A2Ty>
744 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2)
745 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
746 A2(std::forward<A2Ty>(A2)), State(ES_Emplaced) {}
747
748 template <class A0Ty, class A1Ty, class A2Ty, class A3Ty>
749 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2, A3Ty &&A3)
750 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
751 A2(std::forward<A2Ty>(A2)), A3(std::forward<A3Ty>(A3)),
752 State(ES_Emplaced) {}
753
754 Emplaceable(Emplaceable &&) : State(ES_Moved) {}
755 Emplaceable &operator=(Emplaceable &&) {
756 State = ES_Moved;
757 return *this;
758 }
759
760private:
761 Emplaceable(const Emplaceable &) LLVM_DELETED_FUNCTION;
762 Emplaceable &operator=(const Emplaceable &) LLVM_DELETED_FUNCTION;
763};
764
765TEST(SmallVectorTest, EmplaceBack) {
766 EmplaceableArg<0> A0(true);
767 EmplaceableArg<1> A1(true);
768 EmplaceableArg<2> A2(true);
769 EmplaceableArg<3> A3(true);
770 {
771 SmallVector<Emplaceable, 3> V;
772 V.emplace_back();
773 EXPECT_TRUE(V.size() == 1);
774 EXPECT_TRUE(V.back().State == ES_Emplaced);
775 EXPECT_TRUE(V.back().A0.State == EAS_Defaulted);
776 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
777 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
778 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
779 }
780 {
781 SmallVector<Emplaceable, 3> V;
782 V.emplace_back(std::move(A0));
783 EXPECT_TRUE(V.size() == 1);
784 EXPECT_TRUE(V.back().State == ES_Emplaced);
785 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
786 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
787 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
788 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
789 }
790 {
791 SmallVector<Emplaceable, 3> V;
792 V.emplace_back(A0);
793 EXPECT_TRUE(V.size() == 1);
794 EXPECT_TRUE(V.back().State == ES_Emplaced);
795 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
796 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
797 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
798 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
799 }
800 {
801 SmallVector<Emplaceable, 3> V;
802 V.emplace_back(A0, A1);
803 EXPECT_TRUE(V.size() == 1);
804 EXPECT_TRUE(V.back().State == ES_Emplaced);
805 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
806 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
807 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
808 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
809 }
810 {
811 SmallVector<Emplaceable, 3> V;
812 V.emplace_back(std::move(A0), std::move(A1));
813 EXPECT_TRUE(V.size() == 1);
814 EXPECT_TRUE(V.back().State == ES_Emplaced);
815 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
816 EXPECT_TRUE(V.back().A1.State == EAS_RValue);
817 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
818 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
819 }
820 {
821 SmallVector<Emplaceable, 3> V;
822 V.emplace_back(std::move(A0), A1, std::move(A2), A3);
823 EXPECT_TRUE(V.size() == 1);
824 EXPECT_TRUE(V.back().State == ES_Emplaced);
825 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
826 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
827 EXPECT_TRUE(V.back().A2.State == EAS_RValue);
828 EXPECT_TRUE(V.back().A3.State == EAS_LValue);
829 }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000830}
Duncan P. N. Exon Smithf2396e62014-12-03 04:45:09 +0000831
832} // end namespace