1 //===- llvm/unittest/ADT/SmallVectorTest.cpp ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // SmallVector unit tests.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/Support/Compiler.h"
16 #include "gtest/gtest.h"
24 /// A helper class that counts the total number of constructor and
28 static int numConstructorCalls;
29 static int numMoveConstructorCalls;
30 static int numCopyConstructorCalls;
31 static int numDestructorCalls;
32 static int numAssignmentCalls;
33 static int numMoveAssignmentCalls;
34 static int numCopyAssignmentCalls;
40 Constructable() : constructed(true), value(0) {
41 ++numConstructorCalls;
44 Constructable(int val) : constructed(true), value(val) {
45 ++numConstructorCalls;
48 Constructable(const Constructable & src) : constructed(true) {
50 ++numConstructorCalls;
51 ++numCopyConstructorCalls;
54 Constructable(Constructable && src) : constructed(true) {
56 ++numConstructorCalls;
57 ++numMoveConstructorCalls;
61 EXPECT_TRUE(constructed);
66 Constructable & operator=(const Constructable & src) {
67 EXPECT_TRUE(constructed);
70 ++numCopyAssignmentCalls;
74 Constructable & operator=(Constructable && src) {
75 EXPECT_TRUE(constructed);
78 ++numMoveAssignmentCalls;
82 int getValue() const {
87 numConstructorCalls = 0;
88 numMoveConstructorCalls = 0;
89 numCopyConstructorCalls = 0;
90 numDestructorCalls = 0;
91 numAssignmentCalls = 0;
92 numMoveAssignmentCalls = 0;
93 numCopyAssignmentCalls = 0;
96 static int getNumConstructorCalls() {
97 return numConstructorCalls;
100 static int getNumMoveConstructorCalls() {
101 return numMoveConstructorCalls;
104 static int getNumCopyConstructorCalls() {
105 return numCopyConstructorCalls;
108 static int getNumDestructorCalls() {
109 return numDestructorCalls;
112 static int getNumAssignmentCalls() {
113 return numAssignmentCalls;
116 static int getNumMoveAssignmentCalls() {
117 return numMoveAssignmentCalls;
120 static int getNumCopyAssignmentCalls() {
121 return numCopyAssignmentCalls;
124 friend bool operator==(const Constructable & c0, const Constructable & c1) {
125 return c0.getValue() == c1.getValue();
128 friend bool LLVM_ATTRIBUTE_UNUSED
129 operator!=(const Constructable & c0, const Constructable & c1) {
130 return c0.getValue() != c1.getValue();
134 int Constructable::numConstructorCalls;
135 int Constructable::numCopyConstructorCalls;
136 int Constructable::numMoveConstructorCalls;
137 int Constructable::numDestructorCalls;
138 int Constructable::numAssignmentCalls;
139 int Constructable::numCopyAssignmentCalls;
140 int Constructable::numMoveAssignmentCalls;
144 NonCopyable(NonCopyable &&) {}
145 NonCopyable &operator=(NonCopyable &&) { return *this; }
147 NonCopyable(const NonCopyable &) = delete;
148 NonCopyable &operator=(const NonCopyable &) = delete;
151 LLVM_ATTRIBUTE_USED void CompileTest() {
152 SmallVector<NonCopyable, 0> V;
156 class SmallVectorTestBase : public testing::Test {
160 Constructable::reset();
163 template <typename VectorT>
164 void assertEmpty(VectorT & v) {
166 EXPECT_EQ(0u, v.size());
167 EXPECT_TRUE(v.empty());
170 EXPECT_TRUE(v.begin() == v.end());
173 // Assert that v contains the specified values, in order.
174 template <typename VectorT>
175 void assertValuesInOrder(VectorT & v, size_t size, ...) {
176 EXPECT_EQ(size, v.size());
180 for (size_t i = 0; i < size; ++i) {
181 int value = va_arg(ap, int);
182 EXPECT_EQ(value, v[i].getValue());
188 // Generate a sequence of values to initialize the vector.
189 template <typename VectorT>
190 void makeSequence(VectorT & v, int start, int end) {
191 for (int i = start; i <= end; ++i) {
192 v.push_back(Constructable(i));
197 // Test fixture class
198 template <typename VectorT>
199 class SmallVectorTest : public SmallVectorTestBase {
206 typedef ::testing::Types<SmallVector<Constructable, 0>,
207 SmallVector<Constructable, 1>,
208 SmallVector<Constructable, 2>,
209 SmallVector<Constructable, 4>,
210 SmallVector<Constructable, 5>
211 > SmallVectorTestTypes;
212 TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes);
215 TYPED_TEST(SmallVectorTest, EmptyVectorTest) {
216 SCOPED_TRACE("EmptyVectorTest");
217 this->assertEmpty(this->theVector);
218 EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend());
219 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
220 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
223 // Simple insertions and deletions.
224 TYPED_TEST(SmallVectorTest, PushPopTest) {
225 SCOPED_TRACE("PushPopTest");
227 // Track whether the vector will potentially have to grow.
228 bool RequiresGrowth = this->theVector.capacity() < 3;
231 this->theVector.push_back(Constructable(1));
234 this->assertValuesInOrder(this->theVector, 1u, 1);
235 EXPECT_FALSE(this->theVector.begin() == this->theVector.end());
236 EXPECT_FALSE(this->theVector.empty());
238 // Push another element
239 this->theVector.push_back(Constructable(2));
240 this->assertValuesInOrder(this->theVector, 2u, 1, 2);
242 // Insert at beginning
243 this->theVector.insert(this->theVector.begin(), this->theVector[1]);
244 this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2);
247 this->theVector.pop_back();
248 this->assertValuesInOrder(this->theVector, 2u, 2, 1);
250 // Pop remaining elements
251 this->theVector.pop_back();
252 this->theVector.pop_back();
253 this->assertEmpty(this->theVector);
255 // Check number of constructor calls. Should be 2 for each list element,
256 // one for the argument to push_back, one for the argument to insert,
257 // and one for the list element itself.
258 if (!RequiresGrowth) {
259 EXPECT_EQ(5, Constructable::getNumConstructorCalls());
260 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
262 // If we had to grow the vector, these only have a lower bound, but should
264 EXPECT_LE(5, Constructable::getNumConstructorCalls());
265 EXPECT_EQ(Constructable::getNumConstructorCalls(),
266 Constructable::getNumDestructorCalls());
271 TYPED_TEST(SmallVectorTest, ClearTest) {
272 SCOPED_TRACE("ClearTest");
274 this->theVector.reserve(2);
275 this->makeSequence(this->theVector, 1, 2);
276 this->theVector.clear();
278 this->assertEmpty(this->theVector);
279 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
280 EXPECT_EQ(4, Constructable::getNumDestructorCalls());
283 // Resize smaller test.
284 TYPED_TEST(SmallVectorTest, ResizeShrinkTest) {
285 SCOPED_TRACE("ResizeShrinkTest");
287 this->theVector.reserve(3);
288 this->makeSequence(this->theVector, 1, 3);
289 this->theVector.resize(1);
291 this->assertValuesInOrder(this->theVector, 1u, 1);
292 EXPECT_EQ(6, Constructable::getNumConstructorCalls());
293 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
296 // Resize bigger test.
297 TYPED_TEST(SmallVectorTest, ResizeGrowTest) {
298 SCOPED_TRACE("ResizeGrowTest");
300 this->theVector.resize(2);
302 EXPECT_EQ(2, Constructable::getNumConstructorCalls());
303 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
304 EXPECT_EQ(2u, this->theVector.size());
307 TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) {
308 this->theVector.resize(2);
310 Constructable::reset();
312 this->theVector.resize(4);
314 size_t Ctors = Constructable::getNumConstructorCalls();
315 EXPECT_TRUE(Ctors == 2 || Ctors == 4);
316 size_t MoveCtors = Constructable::getNumMoveConstructorCalls();
317 EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2);
318 size_t Dtors = Constructable::getNumDestructorCalls();
319 EXPECT_TRUE(Dtors == 0 || Dtors == 2);
322 // Resize with fill value.
323 TYPED_TEST(SmallVectorTest, ResizeFillTest) {
324 SCOPED_TRACE("ResizeFillTest");
326 this->theVector.resize(3, Constructable(77));
327 this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77);
330 // Overflow past fixed size.
331 TYPED_TEST(SmallVectorTest, OverflowTest) {
332 SCOPED_TRACE("OverflowTest");
334 // Push more elements than the fixed size.
335 this->makeSequence(this->theVector, 1, 10);
337 // Test size and values.
338 EXPECT_EQ(10u, this->theVector.size());
339 for (int i = 0; i < 10; ++i) {
340 EXPECT_EQ(i+1, this->theVector[i].getValue());
343 // Now resize back to fixed size.
344 this->theVector.resize(1);
346 this->assertValuesInOrder(this->theVector, 1u, 1);
350 TYPED_TEST(SmallVectorTest, IterationTest) {
351 this->makeSequence(this->theVector, 1, 2);
354 typename TypeParam::iterator it = this->theVector.begin();
355 EXPECT_TRUE(*it == this->theVector.front());
356 EXPECT_TRUE(*it == this->theVector[0]);
357 EXPECT_EQ(1, it->getValue());
359 EXPECT_TRUE(*it == this->theVector[1]);
360 EXPECT_TRUE(*it == this->theVector.back());
361 EXPECT_EQ(2, it->getValue());
363 EXPECT_TRUE(it == this->theVector.end());
365 EXPECT_TRUE(*it == this->theVector[1]);
366 EXPECT_EQ(2, it->getValue());
368 EXPECT_TRUE(*it == this->theVector[0]);
369 EXPECT_EQ(1, it->getValue());
372 typename TypeParam::reverse_iterator rit = this->theVector.rbegin();
373 EXPECT_TRUE(*rit == this->theVector[1]);
374 EXPECT_EQ(2, rit->getValue());
376 EXPECT_TRUE(*rit == this->theVector[0]);
377 EXPECT_EQ(1, rit->getValue());
379 EXPECT_TRUE(rit == this->theVector.rend());
381 EXPECT_TRUE(*rit == this->theVector[0]);
382 EXPECT_EQ(1, rit->getValue());
384 EXPECT_TRUE(*rit == this->theVector[1]);
385 EXPECT_EQ(2, rit->getValue());
389 TYPED_TEST(SmallVectorTest, SwapTest) {
390 SCOPED_TRACE("SwapTest");
392 this->makeSequence(this->theVector, 1, 2);
393 std::swap(this->theVector, this->otherVector);
395 this->assertEmpty(this->theVector);
396 this->assertValuesInOrder(this->otherVector, 2u, 1, 2);
400 TYPED_TEST(SmallVectorTest, AppendTest) {
401 SCOPED_TRACE("AppendTest");
403 this->makeSequence(this->otherVector, 2, 3);
405 this->theVector.push_back(Constructable(1));
406 this->theVector.append(this->otherVector.begin(), this->otherVector.end());
408 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
411 // Append repeated test
412 TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
413 SCOPED_TRACE("AppendRepeatedTest");
415 this->theVector.push_back(Constructable(1));
416 this->theVector.append(2, Constructable(77));
417 this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77);
421 TYPED_TEST(SmallVectorTest, AssignTest) {
422 SCOPED_TRACE("AssignTest");
424 this->theVector.push_back(Constructable(1));
425 this->theVector.assign(2, Constructable(77));
426 this->assertValuesInOrder(this->theVector, 2u, 77, 77);
430 TYPED_TEST(SmallVectorTest, MoveAssignTest) {
431 SCOPED_TRACE("MoveAssignTest");
433 // Set up our vector with a single element, but enough capacity for 4.
434 this->theVector.reserve(4);
435 this->theVector.push_back(Constructable(1));
437 // Set up the other vector with 2 elements.
438 this->otherVector.push_back(Constructable(2));
439 this->otherVector.push_back(Constructable(3));
441 // Move-assign from the other vector.
442 this->theVector = std::move(this->otherVector);
444 // Make sure we have the right result.
445 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
447 // Make sure the # of constructor/destructor calls line up. There
448 // are two live objects after clearing the other vector.
449 this->otherVector.clear();
450 EXPECT_EQ(Constructable::getNumConstructorCalls()-2,
451 Constructable::getNumDestructorCalls());
453 // There shouldn't be any live objects any more.
454 this->theVector.clear();
455 EXPECT_EQ(Constructable::getNumConstructorCalls(),
456 Constructable::getNumDestructorCalls());
459 // Erase a single element
460 TYPED_TEST(SmallVectorTest, EraseTest) {
461 SCOPED_TRACE("EraseTest");
463 this->makeSequence(this->theVector, 1, 3);
464 this->theVector.erase(this->theVector.begin());
465 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
468 // Erase a range of elements
469 TYPED_TEST(SmallVectorTest, EraseRangeTest) {
470 SCOPED_TRACE("EraseRangeTest");
472 this->makeSequence(this->theVector, 1, 3);
473 this->theVector.erase(this->theVector.begin(), this->theVector.begin() + 2);
474 this->assertValuesInOrder(this->theVector, 1u, 3);
477 // Insert a single element.
478 TYPED_TEST(SmallVectorTest, InsertTest) {
479 SCOPED_TRACE("InsertTest");
481 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);
488 // Insert a copy of a single element.
489 TYPED_TEST(SmallVectorTest, InsertCopy) {
490 SCOPED_TRACE("InsertTest");
492 this->makeSequence(this->theVector, 1, 3);
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);
500 // Insert repeated elements.
501 TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
502 SCOPED_TRACE("InsertRepeatedTest");
504 this->makeSequence(this->theVector, 1, 4);
505 Constructable::reset();
507 this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
508 // 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);
515 // Move assign the next two to shift them up and make a gap.
516 EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls());
517 // Copy construct the two new elements from the parameter.
518 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
519 // All without any copy construction.
520 EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls());
521 EXPECT_EQ(this->theVector.begin() + 1, I);
522 this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4);
526 TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) {
527 SCOPED_TRACE("InsertRepeatedTest");
529 this->makeSequence(this->theVector, 1, 4);
530 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());
534 // Move everything across if reallocation is needed.
535 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
536 Constructable::getNumMoveConstructorCalls() == 4);
537 // Without ever moving or copying anything else.
538 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
539 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
541 EXPECT_EQ(this->theVector.begin() + 4, I);
542 this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16);
545 TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) {
546 SCOPED_TRACE("InsertRepeatedTest");
548 this->makeSequence(this->theVector, 10, 15);
551 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)));
560 TYPED_TEST(SmallVectorTest, InsertRangeTest) {
561 SCOPED_TRACE("InsertRangeTest");
563 Constructable Arr[3] =
564 { Constructable(77), Constructable(77), Constructable(77) };
566 this->makeSequence(this->theVector, 1, 3);
567 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());
580 EXPECT_EQ(this->theVector.begin() + 1, I);
581 this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3);
585 TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) {
586 SCOPED_TRACE("InsertRangeTest");
588 Constructable Arr[3] =
589 { Constructable(77), Constructable(77), Constructable(77) };
591 this->makeSequence(this->theVector, 1, 3);
594 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
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);
610 TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
611 SCOPED_TRACE("InsertRangeTest");
613 this->makeSequence(this->theVector, 1, 3);
616 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()));
627 TYPED_TEST(SmallVectorTest, ComparisonTest) {
628 SCOPED_TRACE("ComparisonTest");
630 this->makeSequence(this->theVector, 1, 3);
631 this->makeSequence(this->otherVector, 1, 3);
633 EXPECT_TRUE(this->theVector == this->otherVector);
634 EXPECT_FALSE(this->theVector != this->otherVector);
636 this->otherVector.clear();
637 this->makeSequence(this->otherVector, 2, 4);
639 EXPECT_FALSE(this->theVector == this->otherVector);
640 EXPECT_TRUE(this->theVector != this->otherVector);
643 // Constant vector tests.
644 TYPED_TEST(SmallVectorTest, ConstVectorTest) {
645 const TypeParam constVector;
647 EXPECT_EQ(0u, constVector.size());
648 EXPECT_TRUE(constVector.empty());
649 EXPECT_TRUE(constVector.begin() == constVector.end());
652 // Direct array access.
653 TYPED_TEST(SmallVectorTest, DirectVectorTest) {
654 EXPECT_EQ(0u, this->theVector.size());
655 this->theVector.reserve(4);
656 EXPECT_LE(4u, this->theVector.capacity());
657 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
658 this->theVector.push_back(1);
659 this->theVector.push_back(2);
660 this->theVector.push_back(3);
661 this->theVector.push_back(4);
662 EXPECT_EQ(4u, this->theVector.size());
663 EXPECT_EQ(8, Constructable::getNumConstructorCalls());
664 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());
670 TYPED_TEST(SmallVectorTest, IteratorTest) {
672 this->theVector.insert(this->theVector.end(), L.begin(), L.end());
675 template <typename InvalidType> class DualSmallVectorsTest;
677 template <typename VectorT1, typename VectorT2>
678 class DualSmallVectorsTest<std::pair<VectorT1, VectorT2>> : public SmallVectorTestBase {
681 VectorT2 otherVector;
683 template <typename T, unsigned N>
684 static unsigned NumBuiltinElts(const SmallVector<T, N>&) { return N; }
687 typedef ::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;
698 TYPED_TEST_CASE(DualSmallVectorsTest, DualSmallVectorTestTypes);
700 TYPED_TEST(DualSmallVectorsTest, MoveAssignment) {
701 SCOPED_TRACE("MoveAssignTest-DualVectorTypes");
703 // Set up our vector with four elements.
704 for (unsigned I = 0; I < 4; ++I)
705 this->otherVector.push_back(Constructable(I));
707 const Constructable *OrigDataPtr = this->otherVector.data();
709 // Move-assign from the other vector.
711 std::move(static_cast<SmallVectorImpl<Constructable>&>(this->otherVector));
713 // Make sure we have the right result.
714 this->assertValuesInOrder(this->theVector, 4u, 0, 1, 2, 3);
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());
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);
727 // There shouldn't be any live objects any more.
728 this->theVector.clear();
729 EXPECT_EQ(Constructable::getNumConstructorCalls(),
730 Constructable::getNumDestructorCalls());
732 // We shouldn't have copied anything in this whole process.
733 EXPECT_EQ(Constructable::getNumCopyConstructorCalls(), 0);
736 struct notassignable {
738 notassignable(int &x) : x(x) {}
741 TEST(SmallVectorCustomTest, NoAssignTest) {
743 SmallVector<notassignable, 2> vec;
744 vec.push_back(notassignable(x));
746 EXPECT_EQ(42, vec.pop_back_val().x);
751 MovedFrom() : hasValue(true) {
753 MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) {
756 MovedFrom &operator=(MovedFrom&& m) {
757 hasValue = m.hasValue;
763 TEST(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);
771 enum EmplaceableArgState {
778 template <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) {}
786 explicit EmplaceableArg(bool) : State(EAS_Arg) {}
789 EmplaceableArg &operator=(EmplaceableArg &&) = delete;
790 EmplaceableArg &operator=(const EmplaceableArg &) = delete;
793 enum EmplaceableState { ES_Emplaced, ES_Moved };
795 EmplaceableArg<0> A0;
796 EmplaceableArg<1> A1;
797 EmplaceableArg<2> A2;
798 EmplaceableArg<3> A3;
799 EmplaceableState State;
801 Emplaceable() : State(ES_Emplaced) {}
803 template <class A0Ty>
804 explicit Emplaceable(A0Ty &&A0)
805 : A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {}
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) {}
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) {}
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) {}
823 Emplaceable(Emplaceable &&) : State(ES_Moved) {}
824 Emplaceable &operator=(Emplaceable &&) {
830 Emplaceable(const Emplaceable &) = delete;
831 Emplaceable &operator=(const Emplaceable &) = delete;
834 TEST(SmallVectorTest, EmplaceBack) {
835 EmplaceableArg<0> A0(true);
836 EmplaceableArg<1> A1(true);
837 EmplaceableArg<2> A2(true);
838 EmplaceableArg<3> A3(true);
840 SmallVector<Emplaceable, 3> V;
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);
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);
860 SmallVector<Emplaceable, 3> V;
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);
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);
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);
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);
900 SmallVector<int, 1> V;
903 EXPECT_EQ(2U, V.size());