blob: 99deb0c68ab4fefa13cf3b3004f296aae5ac2705 [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; }
146 NonCopyable(const NonCopyable &) = delete;
147 NonCopyable &operator=(const NonCopyable &) = delete;
148};
149
150LLVM_ATTRIBUTE_USED void CompileTest() {
151 SmallVector<NonCopyable, 0> V;
152 V.resize(42);
153}
154
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000155// Test fixture class
Chandler Carruth0b012612012-07-30 22:17:52 +0000156template <typename VectorT>
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000157class SmallVectorTest : public testing::Test {
158protected:
Chandler Carruth0b012612012-07-30 22:17:52 +0000159 VectorT theVector;
160 VectorT otherVector;
Owen Anderson145a2602011-07-06 22:36:59 +0000161
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000162 void SetUp() {
163 Constructable::reset();
164 }
165
Chandler Carruth0b012612012-07-30 22:17:52 +0000166 void assertEmpty(VectorT & v) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000167 // Size tests
168 EXPECT_EQ(0u, v.size());
169 EXPECT_TRUE(v.empty());
170
171 // Iterator tests
172 EXPECT_TRUE(v.begin() == v.end());
173 }
174
175 // Assert that theVector contains the specified values, in order.
Chandler Carruth0b012612012-07-30 22:17:52 +0000176 void assertValuesInOrder(VectorT & v, size_t size, ...) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000177 EXPECT_EQ(size, v.size());
Owen Anderson145a2602011-07-06 22:36:59 +0000178
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000179 va_list ap;
180 va_start(ap, size);
181 for (size_t i = 0; i < size; ++i) {
182 int value = va_arg(ap, int);
183 EXPECT_EQ(value, v[i].getValue());
184 }
185
186 va_end(ap);
187 }
Owen Anderson145a2602011-07-06 22:36:59 +0000188
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000189 // Generate a sequence of values to initialize the vector.
Chandler Carruth0b012612012-07-30 22:17:52 +0000190 void makeSequence(VectorT & v, int start, int end) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000191 for (int i = start; i <= end; ++i) {
192 v.push_back(Constructable(i));
193 }
194 }
195};
196
Chandler Carruth0b012612012-07-30 22:17:52 +0000197typedef ::testing::Types<SmallVector<Constructable, 0>,
198 SmallVector<Constructable, 1>,
199 SmallVector<Constructable, 2>,
David Blaikie18486602014-06-08 19:33:40 +0000200 SmallVector<Constructable, 4>,
201 SmallVector<Constructable, 5>
Chandler Carruth0b012612012-07-30 22:17:52 +0000202 > SmallVectorTestTypes;
203TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes);
204
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000205// New vector test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000206TYPED_TEST(SmallVectorTest, EmptyVectorTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000207 SCOPED_TRACE("EmptyVectorTest");
Chandler Carruth0b012612012-07-30 22:17:52 +0000208 this->assertEmpty(this->theVector);
209 EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000210 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
211 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
212}
213
214// Simple insertions and deletions.
Chandler Carruth0b012612012-07-30 22:17:52 +0000215TYPED_TEST(SmallVectorTest, PushPopTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000216 SCOPED_TRACE("PushPopTest");
217
Chandler Carruth0b012612012-07-30 22:17:52 +0000218 // Track whether the vector will potentially have to grow.
219 bool RequiresGrowth = this->theVector.capacity() < 3;
220
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000221 // Push an element
Chandler Carruth0b012612012-07-30 22:17:52 +0000222 this->theVector.push_back(Constructable(1));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000223
224 // Size tests
Chandler Carruth0b012612012-07-30 22:17:52 +0000225 this->assertValuesInOrder(this->theVector, 1u, 1);
226 EXPECT_FALSE(this->theVector.begin() == this->theVector.end());
227 EXPECT_FALSE(this->theVector.empty());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000228
229 // Push another element
Chandler Carruth0b012612012-07-30 22:17:52 +0000230 this->theVector.push_back(Constructable(2));
231 this->assertValuesInOrder(this->theVector, 2u, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000232
Owen Anderson145a2602011-07-06 22:36:59 +0000233 // Insert at beginning
Chandler Carruth0b012612012-07-30 22:17:52 +0000234 this->theVector.insert(this->theVector.begin(), this->theVector[1]);
235 this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2);
Owen Anderson145a2602011-07-06 22:36:59 +0000236
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000237 // Pop one element
Chandler Carruth0b012612012-07-30 22:17:52 +0000238 this->theVector.pop_back();
239 this->assertValuesInOrder(this->theVector, 2u, 2, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000240
Owen Anderson145a2602011-07-06 22:36:59 +0000241 // Pop remaining elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000242 this->theVector.pop_back();
243 this->theVector.pop_back();
244 this->assertEmpty(this->theVector);
Owen Anderson145a2602011-07-06 22:36:59 +0000245
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000246 // Check number of constructor calls. Should be 2 for each list element,
Owen Anderson145a2602011-07-06 22:36:59 +0000247 // one for the argument to push_back, one for the argument to insert,
248 // and one for the list element itself.
Chandler Carruth0b012612012-07-30 22:17:52 +0000249 if (!RequiresGrowth) {
250 EXPECT_EQ(5, Constructable::getNumConstructorCalls());
251 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
252 } else {
253 // If we had to grow the vector, these only have a lower bound, but should
254 // always be equal.
255 EXPECT_LE(5, Constructable::getNumConstructorCalls());
256 EXPECT_EQ(Constructable::getNumConstructorCalls(),
257 Constructable::getNumDestructorCalls());
258 }
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000259}
260
261// Clear test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000262TYPED_TEST(SmallVectorTest, ClearTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000263 SCOPED_TRACE("ClearTest");
264
Chandler Carruth0b012612012-07-30 22:17:52 +0000265 this->theVector.reserve(2);
266 this->makeSequence(this->theVector, 1, 2);
267 this->theVector.clear();
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000268
Chandler Carruth0b012612012-07-30 22:17:52 +0000269 this->assertEmpty(this->theVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000270 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
271 EXPECT_EQ(4, Constructable::getNumDestructorCalls());
272}
273
274// Resize smaller test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000275TYPED_TEST(SmallVectorTest, ResizeShrinkTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000276 SCOPED_TRACE("ResizeShrinkTest");
277
Chandler Carruth0b012612012-07-30 22:17:52 +0000278 this->theVector.reserve(3);
279 this->makeSequence(this->theVector, 1, 3);
280 this->theVector.resize(1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000281
Chandler Carruth0b012612012-07-30 22:17:52 +0000282 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000283 EXPECT_EQ(6, Constructable::getNumConstructorCalls());
284 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
285}
286
287// Resize bigger test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000288TYPED_TEST(SmallVectorTest, ResizeGrowTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000289 SCOPED_TRACE("ResizeGrowTest");
290
Chandler Carruth0b012612012-07-30 22:17:52 +0000291 this->theVector.resize(2);
Owen Anderson145a2602011-07-06 22:36:59 +0000292
David Blaikie669fc862014-06-09 22:26:20 +0000293 EXPECT_EQ(2, Constructable::getNumConstructorCalls());
294 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000295 EXPECT_EQ(2u, this->theVector.size());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000296}
297
David Blaikie669fc862014-06-09 22:26:20 +0000298TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) {
299 this->theVector.resize(2);
300
301 Constructable::reset();
302
303 this->theVector.resize(4);
304
305 size_t Ctors = Constructable::getNumConstructorCalls();
306 EXPECT_TRUE(Ctors == 2 || Ctors == 4);
307 size_t MoveCtors = Constructable::getNumMoveConstructorCalls();
308 EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2);
309 size_t Dtors = Constructable::getNumDestructorCalls();
310 EXPECT_TRUE(Dtors == 0 || Dtors == 2);
311}
312
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000313// Resize with fill value.
Chandler Carruth0b012612012-07-30 22:17:52 +0000314TYPED_TEST(SmallVectorTest, ResizeFillTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000315 SCOPED_TRACE("ResizeFillTest");
316
Chandler Carruth0b012612012-07-30 22:17:52 +0000317 this->theVector.resize(3, Constructable(77));
318 this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000319}
320
321// Overflow past fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000322TYPED_TEST(SmallVectorTest, OverflowTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000323 SCOPED_TRACE("OverflowTest");
324
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000325 // Push more elements than the fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000326 this->makeSequence(this->theVector, 1, 10);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000327
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000328 // Test size and values.
Chandler Carruth0b012612012-07-30 22:17:52 +0000329 EXPECT_EQ(10u, this->theVector.size());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000330 for (int i = 0; i < 10; ++i) {
Chandler Carruth0b012612012-07-30 22:17:52 +0000331 EXPECT_EQ(i+1, this->theVector[i].getValue());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000332 }
Owen Anderson145a2602011-07-06 22:36:59 +0000333
Daniel Dunbar6d6023b2009-07-12 19:45:34 +0000334 // Now resize back to fixed size.
Chandler Carruth0b012612012-07-30 22:17:52 +0000335 this->theVector.resize(1);
Owen Anderson145a2602011-07-06 22:36:59 +0000336
Chandler Carruth0b012612012-07-30 22:17:52 +0000337 this->assertValuesInOrder(this->theVector, 1u, 1);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000338}
339
340// Iteration tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000341TYPED_TEST(SmallVectorTest, IterationTest) {
342 this->makeSequence(this->theVector, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000343
344 // Forward Iteration
Chandler Carruth0b012612012-07-30 22:17:52 +0000345 typename TypeParam::iterator it = this->theVector.begin();
346 EXPECT_TRUE(*it == this->theVector.front());
347 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000348 EXPECT_EQ(1, it->getValue());
349 ++it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000350 EXPECT_TRUE(*it == this->theVector[1]);
351 EXPECT_TRUE(*it == this->theVector.back());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000352 EXPECT_EQ(2, it->getValue());
353 ++it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000354 EXPECT_TRUE(it == this->theVector.end());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000355 --it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000356 EXPECT_TRUE(*it == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000357 EXPECT_EQ(2, it->getValue());
358 --it;
Chandler Carruth0b012612012-07-30 22:17:52 +0000359 EXPECT_TRUE(*it == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000360 EXPECT_EQ(1, it->getValue());
361
362 // Reverse Iteration
Chandler Carruth0b012612012-07-30 22:17:52 +0000363 typename TypeParam::reverse_iterator rit = this->theVector.rbegin();
364 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000365 EXPECT_EQ(2, rit->getValue());
366 ++rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000367 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000368 EXPECT_EQ(1, rit->getValue());
369 ++rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000370 EXPECT_TRUE(rit == this->theVector.rend());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000371 --rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000372 EXPECT_TRUE(*rit == this->theVector[0]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000373 EXPECT_EQ(1, rit->getValue());
374 --rit;
Chandler Carruth0b012612012-07-30 22:17:52 +0000375 EXPECT_TRUE(*rit == this->theVector[1]);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000376 EXPECT_EQ(2, rit->getValue());
377}
378
379// Swap test.
Chandler Carruth0b012612012-07-30 22:17:52 +0000380TYPED_TEST(SmallVectorTest, SwapTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000381 SCOPED_TRACE("SwapTest");
382
Chandler Carruth0b012612012-07-30 22:17:52 +0000383 this->makeSequence(this->theVector, 1, 2);
384 std::swap(this->theVector, this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000385
Chandler Carruth0b012612012-07-30 22:17:52 +0000386 this->assertEmpty(this->theVector);
387 this->assertValuesInOrder(this->otherVector, 2u, 1, 2);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000388}
389
390// Append test
Chandler Carruth0b012612012-07-30 22:17:52 +0000391TYPED_TEST(SmallVectorTest, AppendTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000392 SCOPED_TRACE("AppendTest");
393
Chandler Carruth0b012612012-07-30 22:17:52 +0000394 this->makeSequence(this->otherVector, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000395
Chandler Carruth0b012612012-07-30 22:17:52 +0000396 this->theVector.push_back(Constructable(1));
397 this->theVector.append(this->otherVector.begin(), this->otherVector.end());
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000398
Chandler Carruth0b012612012-07-30 22:17:52 +0000399 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000400}
401
402// Append repeated test
Chandler Carruth0b012612012-07-30 22:17:52 +0000403TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000404 SCOPED_TRACE("AppendRepeatedTest");
405
Chandler Carruth0b012612012-07-30 22:17:52 +0000406 this->theVector.push_back(Constructable(1));
407 this->theVector.append(2, Constructable(77));
408 this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000409}
410
411// Assign test
Chandler Carruth0b012612012-07-30 22:17:52 +0000412TYPED_TEST(SmallVectorTest, AssignTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000413 SCOPED_TRACE("AssignTest");
414
Chandler Carruth0b012612012-07-30 22:17:52 +0000415 this->theVector.push_back(Constructable(1));
416 this->theVector.assign(2, Constructable(77));
417 this->assertValuesInOrder(this->theVector, 2u, 77, 77);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000418}
419
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000420// Move-assign test
421TYPED_TEST(SmallVectorTest, MoveAssignTest) {
422 SCOPED_TRACE("MoveAssignTest");
423
424 // Set up our vector with a single element, but enough capacity for 4.
425 this->theVector.reserve(4);
426 this->theVector.push_back(Constructable(1));
427
428 // Set up the other vector with 2 elements.
429 this->otherVector.push_back(Constructable(2));
430 this->otherVector.push_back(Constructable(3));
431
432 // Move-assign from the other vector.
433 this->theVector = std::move(this->otherVector);
434
435 // Make sure we have the right result.
436 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
437
438 // Make sure the # of constructor/destructor calls line up. There
439 // are two live objects after clearing the other vector.
440 this->otherVector.clear();
441 EXPECT_EQ(Constructable::getNumConstructorCalls()-2,
442 Constructable::getNumDestructorCalls());
443
444 // There shouldn't be any live objects any more.
445 this->theVector.clear();
446 EXPECT_EQ(Constructable::getNumConstructorCalls(),
447 Constructable::getNumDestructorCalls());
448}
449
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000450// Erase a single element
Chandler Carruth0b012612012-07-30 22:17:52 +0000451TYPED_TEST(SmallVectorTest, EraseTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000452 SCOPED_TRACE("EraseTest");
453
Chandler Carruth0b012612012-07-30 22:17:52 +0000454 this->makeSequence(this->theVector, 1, 3);
455 this->theVector.erase(this->theVector.begin());
456 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000457}
458
459// Erase a range of elements
Chandler Carruth0b012612012-07-30 22:17:52 +0000460TYPED_TEST(SmallVectorTest, EraseRangeTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000461 SCOPED_TRACE("EraseRangeTest");
462
Chandler Carruth0b012612012-07-30 22:17:52 +0000463 this->makeSequence(this->theVector, 1, 3);
464 this->theVector.erase(this->theVector.begin(), this->theVector.begin() + 2);
465 this->assertValuesInOrder(this->theVector, 1u, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000466}
467
468// Insert a single element.
Chandler Carruth0b012612012-07-30 22:17:52 +0000469TYPED_TEST(SmallVectorTest, InsertTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000470 SCOPED_TRACE("InsertTest");
471
Chandler Carruth0b012612012-07-30 22:17:52 +0000472 this->makeSequence(this->theVector, 1, 3);
473 typename TypeParam::iterator I =
474 this->theVector.insert(this->theVector.begin() + 1, Constructable(77));
475 EXPECT_EQ(this->theVector.begin() + 1, I);
476 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000477}
478
David Blaikie40d4e342014-06-08 16:55:13 +0000479// Insert a copy of a single element.
480TYPED_TEST(SmallVectorTest, InsertCopy) {
481 SCOPED_TRACE("InsertTest");
482
483 this->makeSequence(this->theVector, 1, 3);
484 Constructable C(77);
485 typename TypeParam::iterator I =
486 this->theVector.insert(this->theVector.begin() + 1, C);
487 EXPECT_EQ(this->theVector.begin() + 1, I);
488 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
489}
490
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000491// Insert repeated elements.
Chandler Carruth0b012612012-07-30 22:17:52 +0000492TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000493 SCOPED_TRACE("InsertRepeatedTest");
494
David Blaikie18486602014-06-08 19:33:40 +0000495 this->makeSequence(this->theVector, 1, 4);
David Blaikieae8a9322014-06-08 19:12:28 +0000496 Constructable::reset();
David Blaikie402cb2c2014-06-08 19:12:31 +0000497 auto I =
498 this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
David Blaikie18486602014-06-08 19:33:40 +0000499 // Move construct the top element into newly allocated space, and optionally
500 // reallocate the whole buffer, move constructing into it.
501 // FIXME: This is inefficient, we shouldn't move things into newly allocated
502 // space, then move them up/around, there should only be 2 or 4 move
503 // constructions here.
504 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
505 Constructable::getNumMoveConstructorCalls() == 6);
David Blaikie402cb2c2014-06-08 19:12:31 +0000506 // Move assign the next two to shift them up and make a gap.
David Blaikie18486602014-06-08 19:33:40 +0000507 EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls());
David Blaikie402cb2c2014-06-08 19:12:31 +0000508 // Copy construct the two new elements from the parameter.
509 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
510 // All without any copy construction.
David Blaikieae8a9322014-06-08 19:12:28 +0000511 EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000512 EXPECT_EQ(this->theVector.begin() + 1, I);
David Blaikie18486602014-06-08 19:33:40 +0000513 this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000514}
Benjamin Kramer371b9b02012-06-17 11:52:22 +0000515
David Blaikie402cb2c2014-06-08 19:12:31 +0000516
517TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) {
518 SCOPED_TRACE("InsertRepeatedTest");
519
David Blaikie18486602014-06-08 19:33:40 +0000520 this->makeSequence(this->theVector, 1, 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000521 Constructable::reset();
522 auto I = this->theVector.insert(this->theVector.end(), 2, Constructable(16));
523 // Just copy construct them into newly allocated space
524 EXPECT_EQ(2, Constructable::getNumCopyConstructorCalls());
David Blaikie18486602014-06-08 19:33:40 +0000525 // Move everything across if reallocation is needed.
526 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
527 Constructable::getNumMoveConstructorCalls() == 4);
David Blaikie402cb2c2014-06-08 19:12:31 +0000528 // Without ever moving or copying anything else.
529 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
530 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
531
David Blaikie18486602014-06-08 19:33:40 +0000532 EXPECT_EQ(this->theVector.begin() + 4, I);
533 this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16);
David Blaikie402cb2c2014-06-08 19:12:31 +0000534}
535
536TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) {
537 SCOPED_TRACE("InsertRepeatedTest");
538
539 this->makeSequence(this->theVector, 10, 15);
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000540
541 // Empty insert.
Chandler Carruth0b012612012-07-30 22:17:52 +0000542 EXPECT_EQ(this->theVector.end(),
543 this->theVector.insert(this->theVector.end(),
544 0, Constructable(42)));
545 EXPECT_EQ(this->theVector.begin() + 1,
546 this->theVector.insert(this->theVector.begin() + 1,
547 0, Constructable(42)));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000548}
549
550// Insert range.
Chandler Carruth0b012612012-07-30 22:17:52 +0000551TYPED_TEST(SmallVectorTest, InsertRangeTest) {
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000552 SCOPED_TRACE("InsertRangeTest");
553
554 Constructable Arr[3] =
555 { Constructable(77), Constructable(77), Constructable(77) };
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000556
Chandler Carruth0b012612012-07-30 22:17:52 +0000557 this->makeSequence(this->theVector, 1, 3);
David Blaikie402cb2c2014-06-08 19:12:31 +0000558 Constructable::reset();
559 auto I = this->theVector.insert(this->theVector.begin() + 1, Arr, Arr + 3);
560 // Move construct the top 3 elements into newly allocated space.
561 // Possibly move the whole sequence into new space first.
562 // FIXME: This is inefficient, we shouldn't move things into newly allocated
563 // space, then move them up/around, there should only be 2 or 3 move
564 // constructions here.
565 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
566 Constructable::getNumMoveConstructorCalls() == 5);
567 // Copy assign the lower 2 new elements into existing space.
568 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
569 // Copy construct the third element into newly allocated space.
570 EXPECT_EQ(1, Constructable::getNumCopyConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000571 EXPECT_EQ(this->theVector.begin() + 1, I);
572 this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3);
David Blaikie402cb2c2014-06-08 19:12:31 +0000573}
574
575
576TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) {
577 SCOPED_TRACE("InsertRangeTest");
578
579 Constructable Arr[3] =
580 { Constructable(77), Constructable(77), Constructable(77) };
581
582 this->makeSequence(this->theVector, 1, 3);
Benjamin Kramer371b9b02012-06-17 11:52:22 +0000583
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000584 // Insert at end.
David Blaikie402cb2c2014-06-08 19:12:31 +0000585 Constructable::reset();
586 auto I = this->theVector.insert(this->theVector.end(), Arr, Arr+3);
587 // Copy construct the 3 elements into new space at the top.
588 EXPECT_EQ(3, Constructable::getNumCopyConstructorCalls());
589 // Don't copy/move anything else.
590 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
591 // Reallocation might occur, causing all elements to be moved into the new
592 // buffer.
593 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
594 Constructable::getNumMoveConstructorCalls() == 3);
595 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
596 EXPECT_EQ(this->theVector.begin() + 3, I);
597 this->assertValuesInOrder(this->theVector, 6u,
598 1, 2, 3, 77, 77, 77);
599}
600
601TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
602 SCOPED_TRACE("InsertRangeTest");
603
604 this->makeSequence(this->theVector, 1, 3);
Benjamin Kramer23a9c3e02012-06-17 12:46:13 +0000605
606 // Empty insert.
Chandler Carruth0b012612012-07-30 22:17:52 +0000607 EXPECT_EQ(this->theVector.end(),
608 this->theVector.insert(this->theVector.end(),
609 this->theVector.begin(),
610 this->theVector.begin()));
611 EXPECT_EQ(this->theVector.begin() + 1,
612 this->theVector.insert(this->theVector.begin() + 1,
613 this->theVector.begin(),
614 this->theVector.begin()));
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000615}
616
617// Comparison tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000618TYPED_TEST(SmallVectorTest, ComparisonTest) {
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000619 SCOPED_TRACE("ComparisonTest");
620
Chandler Carruth0b012612012-07-30 22:17:52 +0000621 this->makeSequence(this->theVector, 1, 3);
622 this->makeSequence(this->otherVector, 1, 3);
Owen Anderson145a2602011-07-06 22:36:59 +0000623
Chandler Carruth0b012612012-07-30 22:17:52 +0000624 EXPECT_TRUE(this->theVector == this->otherVector);
625 EXPECT_FALSE(this->theVector != this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000626
Chandler Carruth0b012612012-07-30 22:17:52 +0000627 this->otherVector.clear();
628 this->makeSequence(this->otherVector, 2, 4);
Owen Anderson145a2602011-07-06 22:36:59 +0000629
Chandler Carruth0b012612012-07-30 22:17:52 +0000630 EXPECT_FALSE(this->theVector == this->otherVector);
631 EXPECT_TRUE(this->theVector != this->otherVector);
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000632}
633
634// Constant vector tests.
Chandler Carruth0b012612012-07-30 22:17:52 +0000635TYPED_TEST(SmallVectorTest, ConstVectorTest) {
636 const TypeParam constVector;
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000637
638 EXPECT_EQ(0u, constVector.size());
639 EXPECT_TRUE(constVector.empty());
640 EXPECT_TRUE(constVector.begin() == constVector.end());
641}
642
Daniel Dunbar825e9552009-08-19 17:48:28 +0000643// Direct array access.
Chandler Carruth0b012612012-07-30 22:17:52 +0000644TYPED_TEST(SmallVectorTest, DirectVectorTest) {
645 EXPECT_EQ(0u, this->theVector.size());
646 this->theVector.reserve(4);
647 EXPECT_LE(4u, this->theVector.capacity());
Daniel Dunbar825e9552009-08-19 17:48:28 +0000648 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000649 this->theVector.push_back(1);
650 this->theVector.push_back(2);
651 this->theVector.push_back(3);
652 this->theVector.push_back(4);
Chandler Carruth0b012612012-07-30 22:17:52 +0000653 EXPECT_EQ(4u, this->theVector.size());
Douglas Gregor8451cdff2014-04-30 15:49:06 +0000654 EXPECT_EQ(8, Constructable::getNumConstructorCalls());
Chandler Carruth0b012612012-07-30 22:17:52 +0000655 EXPECT_EQ(1, this->theVector[0].getValue());
656 EXPECT_EQ(2, this->theVector[1].getValue());
657 EXPECT_EQ(3, this->theVector[2].getValue());
658 EXPECT_EQ(4, this->theVector[3].getValue());
Daniel Dunbar825e9552009-08-19 17:48:28 +0000659}
660
Chandler Carruth0b012612012-07-30 22:17:52 +0000661TYPED_TEST(SmallVectorTest, IteratorTest) {
Dan Gohman42e77df2010-03-26 18:53:37 +0000662 std::list<int> L;
Chandler Carruth0b012612012-07-30 22:17:52 +0000663 this->theVector.insert(this->theVector.end(), L.begin(), L.end());
Dan Gohman42e77df2010-03-26 18:53:37 +0000664}
665
Benjamin Kramer74a12a42012-04-29 10:53:29 +0000666struct notassignable {
667 int &x;
668 notassignable(int &x) : x(x) {}
669};
670
Chandler Carruth0b012612012-07-30 22:17:52 +0000671TEST(SmallVectorCustomTest, NoAssignTest) {
Benjamin Kramer74a12a42012-04-29 10:53:29 +0000672 int x = 0;
673 SmallVector<notassignable, 2> vec;
674 vec.push_back(notassignable(x));
675 x = 42;
676 EXPECT_EQ(42, vec.pop_back_val().x);
677}
678
David Blaikie789df062014-06-08 16:00:02 +0000679struct MovedFrom {
680 bool hasValue;
681 MovedFrom() : hasValue(true) {
682 }
683 MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) {
684 m.hasValue = false;
685 }
686 MovedFrom &operator=(MovedFrom&& m) {
687 hasValue = m.hasValue;
688 m.hasValue = false;
689 return *this;
690 }
691};
692
693TEST(SmallVectorTest, MidInsert) {
694 SmallVector<MovedFrom, 3> v;
695 v.push_back(MovedFrom());
696 v.insert(v.begin(), MovedFrom());
697 for (MovedFrom &m : v)
698 EXPECT_TRUE(m.hasValue);
699}
700
Bill Wendlingc56c37f2009-01-10 12:56:31 +0000701}