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/ArrayRef.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/Support/Compiler.h"
17 #include "gtest/gtest.h"
25 /// A helper class that counts the total number of constructor and
29 static int numConstructorCalls;
30 static int numMoveConstructorCalls;
31 static int numCopyConstructorCalls;
32 static int numDestructorCalls;
33 static int numAssignmentCalls;
34 static int numMoveAssignmentCalls;
35 static int numCopyAssignmentCalls;
41 Constructable() : constructed(true), value(0) {
42 ++numConstructorCalls;
45 Constructable(int val) : constructed(true), value(val) {
46 ++numConstructorCalls;
49 Constructable(const Constructable & src) : constructed(true) {
51 ++numConstructorCalls;
52 ++numCopyConstructorCalls;
55 Constructable(Constructable && src) : constructed(true) {
57 ++numConstructorCalls;
58 ++numMoveConstructorCalls;
62 EXPECT_TRUE(constructed);
67 Constructable & operator=(const Constructable & src) {
68 EXPECT_TRUE(constructed);
71 ++numCopyAssignmentCalls;
75 Constructable & operator=(Constructable && src) {
76 EXPECT_TRUE(constructed);
79 ++numMoveAssignmentCalls;
83 int getValue() const {
88 numConstructorCalls = 0;
89 numMoveConstructorCalls = 0;
90 numCopyConstructorCalls = 0;
91 numDestructorCalls = 0;
92 numAssignmentCalls = 0;
93 numMoveAssignmentCalls = 0;
94 numCopyAssignmentCalls = 0;
97 static int getNumConstructorCalls() {
98 return numConstructorCalls;
101 static int getNumMoveConstructorCalls() {
102 return numMoveConstructorCalls;
105 static int getNumCopyConstructorCalls() {
106 return numCopyConstructorCalls;
109 static int getNumDestructorCalls() {
110 return numDestructorCalls;
113 static int getNumAssignmentCalls() {
114 return numAssignmentCalls;
117 static int getNumMoveAssignmentCalls() {
118 return numMoveAssignmentCalls;
121 static int getNumCopyAssignmentCalls() {
122 return numCopyAssignmentCalls;
125 friend bool operator==(const Constructable & c0, const Constructable & c1) {
126 return c0.getValue() == c1.getValue();
129 friend bool LLVM_ATTRIBUTE_UNUSED
130 operator!=(const Constructable & c0, const Constructable & c1) {
131 return c0.getValue() != c1.getValue();
135 int Constructable::numConstructorCalls;
136 int Constructable::numCopyConstructorCalls;
137 int Constructable::numMoveConstructorCalls;
138 int Constructable::numDestructorCalls;
139 int Constructable::numAssignmentCalls;
140 int Constructable::numCopyAssignmentCalls;
141 int Constructable::numMoveAssignmentCalls;
145 NonCopyable(NonCopyable &&) {}
146 NonCopyable &operator=(NonCopyable &&) { return *this; }
148 NonCopyable(const NonCopyable &) = delete;
149 NonCopyable &operator=(const NonCopyable &) = delete;
152 LLVM_ATTRIBUTE_USED void CompileTest() {
153 SmallVector<NonCopyable, 0> V;
157 class SmallVectorTestBase : public testing::Test {
159 void SetUp() override { Constructable::reset(); }
161 template <typename VectorT>
162 void assertEmpty(VectorT & v) {
164 EXPECT_EQ(0u, v.size());
165 EXPECT_TRUE(v.empty());
168 EXPECT_TRUE(v.begin() == v.end());
171 // Assert that v contains the specified values, in order.
172 template <typename VectorT>
173 void assertValuesInOrder(VectorT & v, size_t size, ...) {
174 EXPECT_EQ(size, v.size());
178 for (size_t i = 0; i < size; ++i) {
179 int value = va_arg(ap, int);
180 EXPECT_EQ(value, v[i].getValue());
186 // Generate a sequence of values to initialize the vector.
187 template <typename VectorT>
188 void makeSequence(VectorT & v, int start, int end) {
189 for (int i = start; i <= end; ++i) {
190 v.push_back(Constructable(i));
195 // Test fixture class
196 template <typename VectorT>
197 class SmallVectorTest : public SmallVectorTestBase {
204 typedef ::testing::Types<SmallVector<Constructable, 0>,
205 SmallVector<Constructable, 1>,
206 SmallVector<Constructable, 2>,
207 SmallVector<Constructable, 4>,
208 SmallVector<Constructable, 5>
209 > SmallVectorTestTypes;
210 TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes);
213 TYPED_TEST(SmallVectorTest, EmptyVectorTest) {
214 SCOPED_TRACE("EmptyVectorTest");
215 this->assertEmpty(this->theVector);
216 EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend());
217 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
218 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
221 // Simple insertions and deletions.
222 TYPED_TEST(SmallVectorTest, PushPopTest) {
223 SCOPED_TRACE("PushPopTest");
225 // Track whether the vector will potentially have to grow.
226 bool RequiresGrowth = this->theVector.capacity() < 3;
229 this->theVector.push_back(Constructable(1));
232 this->assertValuesInOrder(this->theVector, 1u, 1);
233 EXPECT_FALSE(this->theVector.begin() == this->theVector.end());
234 EXPECT_FALSE(this->theVector.empty());
236 // Push another element
237 this->theVector.push_back(Constructable(2));
238 this->assertValuesInOrder(this->theVector, 2u, 1, 2);
240 // Insert at beginning
241 this->theVector.insert(this->theVector.begin(), this->theVector[1]);
242 this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2);
245 this->theVector.pop_back();
246 this->assertValuesInOrder(this->theVector, 2u, 2, 1);
248 // Pop remaining elements
249 this->theVector.pop_back();
250 this->theVector.pop_back();
251 this->assertEmpty(this->theVector);
253 // Check number of constructor calls. Should be 2 for each list element,
254 // one for the argument to push_back, one for the argument to insert,
255 // and one for the list element itself.
256 if (!RequiresGrowth) {
257 EXPECT_EQ(5, Constructable::getNumConstructorCalls());
258 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
260 // If we had to grow the vector, these only have a lower bound, but should
262 EXPECT_LE(5, Constructable::getNumConstructorCalls());
263 EXPECT_EQ(Constructable::getNumConstructorCalls(),
264 Constructable::getNumDestructorCalls());
269 TYPED_TEST(SmallVectorTest, ClearTest) {
270 SCOPED_TRACE("ClearTest");
272 this->theVector.reserve(2);
273 this->makeSequence(this->theVector, 1, 2);
274 this->theVector.clear();
276 this->assertEmpty(this->theVector);
277 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
278 EXPECT_EQ(4, Constructable::getNumDestructorCalls());
281 // Resize smaller test.
282 TYPED_TEST(SmallVectorTest, ResizeShrinkTest) {
283 SCOPED_TRACE("ResizeShrinkTest");
285 this->theVector.reserve(3);
286 this->makeSequence(this->theVector, 1, 3);
287 this->theVector.resize(1);
289 this->assertValuesInOrder(this->theVector, 1u, 1);
290 EXPECT_EQ(6, Constructable::getNumConstructorCalls());
291 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
294 // Resize bigger test.
295 TYPED_TEST(SmallVectorTest, ResizeGrowTest) {
296 SCOPED_TRACE("ResizeGrowTest");
298 this->theVector.resize(2);
300 EXPECT_EQ(2, Constructable::getNumConstructorCalls());
301 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
302 EXPECT_EQ(2u, this->theVector.size());
305 TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) {
306 this->theVector.resize(2);
308 Constructable::reset();
310 this->theVector.resize(4);
312 size_t Ctors = Constructable::getNumConstructorCalls();
313 EXPECT_TRUE(Ctors == 2 || Ctors == 4);
314 size_t MoveCtors = Constructable::getNumMoveConstructorCalls();
315 EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2);
316 size_t Dtors = Constructable::getNumDestructorCalls();
317 EXPECT_TRUE(Dtors == 0 || Dtors == 2);
320 // Resize with fill value.
321 TYPED_TEST(SmallVectorTest, ResizeFillTest) {
322 SCOPED_TRACE("ResizeFillTest");
324 this->theVector.resize(3, Constructable(77));
325 this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77);
328 // Overflow past fixed size.
329 TYPED_TEST(SmallVectorTest, OverflowTest) {
330 SCOPED_TRACE("OverflowTest");
332 // Push more elements than the fixed size.
333 this->makeSequence(this->theVector, 1, 10);
335 // Test size and values.
336 EXPECT_EQ(10u, this->theVector.size());
337 for (int i = 0; i < 10; ++i) {
338 EXPECT_EQ(i+1, this->theVector[i].getValue());
341 // Now resize back to fixed size.
342 this->theVector.resize(1);
344 this->assertValuesInOrder(this->theVector, 1u, 1);
348 TYPED_TEST(SmallVectorTest, IterationTest) {
349 this->makeSequence(this->theVector, 1, 2);
352 typename TypeParam::iterator it = this->theVector.begin();
353 EXPECT_TRUE(*it == this->theVector.front());
354 EXPECT_TRUE(*it == this->theVector[0]);
355 EXPECT_EQ(1, it->getValue());
357 EXPECT_TRUE(*it == this->theVector[1]);
358 EXPECT_TRUE(*it == this->theVector.back());
359 EXPECT_EQ(2, it->getValue());
361 EXPECT_TRUE(it == this->theVector.end());
363 EXPECT_TRUE(*it == this->theVector[1]);
364 EXPECT_EQ(2, it->getValue());
366 EXPECT_TRUE(*it == this->theVector[0]);
367 EXPECT_EQ(1, it->getValue());
370 typename TypeParam::reverse_iterator rit = this->theVector.rbegin();
371 EXPECT_TRUE(*rit == this->theVector[1]);
372 EXPECT_EQ(2, rit->getValue());
374 EXPECT_TRUE(*rit == this->theVector[0]);
375 EXPECT_EQ(1, rit->getValue());
377 EXPECT_TRUE(rit == this->theVector.rend());
379 EXPECT_TRUE(*rit == this->theVector[0]);
380 EXPECT_EQ(1, rit->getValue());
382 EXPECT_TRUE(*rit == this->theVector[1]);
383 EXPECT_EQ(2, rit->getValue());
387 TYPED_TEST(SmallVectorTest, SwapTest) {
388 SCOPED_TRACE("SwapTest");
390 this->makeSequence(this->theVector, 1, 2);
391 std::swap(this->theVector, this->otherVector);
393 this->assertEmpty(this->theVector);
394 this->assertValuesInOrder(this->otherVector, 2u, 1, 2);
398 TYPED_TEST(SmallVectorTest, AppendTest) {
399 SCOPED_TRACE("AppendTest");
401 this->makeSequence(this->otherVector, 2, 3);
403 this->theVector.push_back(Constructable(1));
404 this->theVector.append(this->otherVector.begin(), this->otherVector.end());
406 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
409 // Append repeated test
410 TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
411 SCOPED_TRACE("AppendRepeatedTest");
413 this->theVector.push_back(Constructable(1));
414 this->theVector.append(2, Constructable(77));
415 this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77);
419 TYPED_TEST(SmallVectorTest, AssignTest) {
420 SCOPED_TRACE("AssignTest");
422 this->theVector.push_back(Constructable(1));
423 this->theVector.assign(2, Constructable(77));
424 this->assertValuesInOrder(this->theVector, 2u, 77, 77);
428 TYPED_TEST(SmallVectorTest, MoveAssignTest) {
429 SCOPED_TRACE("MoveAssignTest");
431 // Set up our vector with a single element, but enough capacity for 4.
432 this->theVector.reserve(4);
433 this->theVector.push_back(Constructable(1));
435 // Set up the other vector with 2 elements.
436 this->otherVector.push_back(Constructable(2));
437 this->otherVector.push_back(Constructable(3));
439 // Move-assign from the other vector.
440 this->theVector = std::move(this->otherVector);
442 // Make sure we have the right result.
443 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
445 // Make sure the # of constructor/destructor calls line up. There
446 // are two live objects after clearing the other vector.
447 this->otherVector.clear();
448 EXPECT_EQ(Constructable::getNumConstructorCalls()-2,
449 Constructable::getNumDestructorCalls());
451 // There shouldn't be any live objects any more.
452 this->theVector.clear();
453 EXPECT_EQ(Constructable::getNumConstructorCalls(),
454 Constructable::getNumDestructorCalls());
457 // Erase a single element
458 TYPED_TEST(SmallVectorTest, EraseTest) {
459 SCOPED_TRACE("EraseTest");
461 this->makeSequence(this->theVector, 1, 3);
462 this->theVector.erase(this->theVector.begin());
463 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
466 // Erase a range of elements
467 TYPED_TEST(SmallVectorTest, EraseRangeTest) {
468 SCOPED_TRACE("EraseRangeTest");
470 this->makeSequence(this->theVector, 1, 3);
471 this->theVector.erase(this->theVector.begin(), this->theVector.begin() + 2);
472 this->assertValuesInOrder(this->theVector, 1u, 3);
475 // Insert a single element.
476 TYPED_TEST(SmallVectorTest, InsertTest) {
477 SCOPED_TRACE("InsertTest");
479 this->makeSequence(this->theVector, 1, 3);
480 typename TypeParam::iterator I =
481 this->theVector.insert(this->theVector.begin() + 1, Constructable(77));
482 EXPECT_EQ(this->theVector.begin() + 1, I);
483 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
486 // Insert a copy of a single element.
487 TYPED_TEST(SmallVectorTest, InsertCopy) {
488 SCOPED_TRACE("InsertTest");
490 this->makeSequence(this->theVector, 1, 3);
492 typename TypeParam::iterator I =
493 this->theVector.insert(this->theVector.begin() + 1, C);
494 EXPECT_EQ(this->theVector.begin() + 1, I);
495 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
498 // Insert repeated elements.
499 TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
500 SCOPED_TRACE("InsertRepeatedTest");
502 this->makeSequence(this->theVector, 1, 4);
503 Constructable::reset();
505 this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
506 // Move construct the top element into newly allocated space, and optionally
507 // reallocate the whole buffer, move constructing into it.
508 // FIXME: This is inefficient, we shouldn't move things into newly allocated
509 // space, then move them up/around, there should only be 2 or 4 move
510 // constructions here.
511 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
512 Constructable::getNumMoveConstructorCalls() == 6);
513 // Move assign the next two to shift them up and make a gap.
514 EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls());
515 // Copy construct the two new elements from the parameter.
516 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
517 // All without any copy construction.
518 EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls());
519 EXPECT_EQ(this->theVector.begin() + 1, I);
520 this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4);
524 TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) {
525 SCOPED_TRACE("InsertRepeatedTest");
527 this->makeSequence(this->theVector, 1, 4);
528 Constructable::reset();
529 auto I = this->theVector.insert(this->theVector.end(), 2, Constructable(16));
530 // Just copy construct them into newly allocated space
531 EXPECT_EQ(2, Constructable::getNumCopyConstructorCalls());
532 // Move everything across if reallocation is needed.
533 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
534 Constructable::getNumMoveConstructorCalls() == 4);
535 // Without ever moving or copying anything else.
536 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
537 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
539 EXPECT_EQ(this->theVector.begin() + 4, I);
540 this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16);
543 TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) {
544 SCOPED_TRACE("InsertRepeatedTest");
546 this->makeSequence(this->theVector, 10, 15);
549 EXPECT_EQ(this->theVector.end(),
550 this->theVector.insert(this->theVector.end(),
551 0, Constructable(42)));
552 EXPECT_EQ(this->theVector.begin() + 1,
553 this->theVector.insert(this->theVector.begin() + 1,
554 0, Constructable(42)));
558 TYPED_TEST(SmallVectorTest, InsertRangeTest) {
559 SCOPED_TRACE("InsertRangeTest");
561 Constructable Arr[3] =
562 { Constructable(77), Constructable(77), Constructable(77) };
564 this->makeSequence(this->theVector, 1, 3);
565 Constructable::reset();
566 auto I = this->theVector.insert(this->theVector.begin() + 1, Arr, Arr + 3);
567 // Move construct the top 3 elements into newly allocated space.
568 // Possibly move the whole sequence into new space first.
569 // FIXME: This is inefficient, we shouldn't move things into newly allocated
570 // space, then move them up/around, there should only be 2 or 3 move
571 // constructions here.
572 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
573 Constructable::getNumMoveConstructorCalls() == 5);
574 // Copy assign the lower 2 new elements into existing space.
575 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
576 // Copy construct the third element into newly allocated space.
577 EXPECT_EQ(1, Constructable::getNumCopyConstructorCalls());
578 EXPECT_EQ(this->theVector.begin() + 1, I);
579 this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3);
583 TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) {
584 SCOPED_TRACE("InsertRangeTest");
586 Constructable Arr[3] =
587 { Constructable(77), Constructable(77), Constructable(77) };
589 this->makeSequence(this->theVector, 1, 3);
592 Constructable::reset();
593 auto I = this->theVector.insert(this->theVector.end(), Arr, Arr+3);
594 // Copy construct the 3 elements into new space at the top.
595 EXPECT_EQ(3, Constructable::getNumCopyConstructorCalls());
596 // Don't copy/move anything else.
597 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
598 // Reallocation might occur, causing all elements to be moved into the new
600 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
601 Constructable::getNumMoveConstructorCalls() == 3);
602 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
603 EXPECT_EQ(this->theVector.begin() + 3, I);
604 this->assertValuesInOrder(this->theVector, 6u,
605 1, 2, 3, 77, 77, 77);
608 TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
609 SCOPED_TRACE("InsertRangeTest");
611 this->makeSequence(this->theVector, 1, 3);
614 EXPECT_EQ(this->theVector.end(),
615 this->theVector.insert(this->theVector.end(),
616 this->theVector.begin(),
617 this->theVector.begin()));
618 EXPECT_EQ(this->theVector.begin() + 1,
619 this->theVector.insert(this->theVector.begin() + 1,
620 this->theVector.begin(),
621 this->theVector.begin()));
625 TYPED_TEST(SmallVectorTest, ComparisonTest) {
626 SCOPED_TRACE("ComparisonTest");
628 this->makeSequence(this->theVector, 1, 3);
629 this->makeSequence(this->otherVector, 1, 3);
631 EXPECT_TRUE(this->theVector == this->otherVector);
632 EXPECT_FALSE(this->theVector != this->otherVector);
634 this->otherVector.clear();
635 this->makeSequence(this->otherVector, 2, 4);
637 EXPECT_FALSE(this->theVector == this->otherVector);
638 EXPECT_TRUE(this->theVector != this->otherVector);
641 // Constant vector tests.
642 TYPED_TEST(SmallVectorTest, ConstVectorTest) {
643 const TypeParam constVector;
645 EXPECT_EQ(0u, constVector.size());
646 EXPECT_TRUE(constVector.empty());
647 EXPECT_TRUE(constVector.begin() == constVector.end());
650 // Direct array access.
651 TYPED_TEST(SmallVectorTest, DirectVectorTest) {
652 EXPECT_EQ(0u, this->theVector.size());
653 this->theVector.reserve(4);
654 EXPECT_LE(4u, this->theVector.capacity());
655 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
656 this->theVector.push_back(1);
657 this->theVector.push_back(2);
658 this->theVector.push_back(3);
659 this->theVector.push_back(4);
660 EXPECT_EQ(4u, this->theVector.size());
661 EXPECT_EQ(8, Constructable::getNumConstructorCalls());
662 EXPECT_EQ(1, this->theVector[0].getValue());
663 EXPECT_EQ(2, this->theVector[1].getValue());
664 EXPECT_EQ(3, this->theVector[2].getValue());
665 EXPECT_EQ(4, this->theVector[3].getValue());
668 TYPED_TEST(SmallVectorTest, IteratorTest) {
670 this->theVector.insert(this->theVector.end(), L.begin(), L.end());
673 template <typename InvalidType> class DualSmallVectorsTest;
675 template <typename VectorT1, typename VectorT2>
676 class DualSmallVectorsTest<std::pair<VectorT1, VectorT2>> : public SmallVectorTestBase {
679 VectorT2 otherVector;
681 template <typename T, unsigned N>
682 static unsigned NumBuiltinElts(const SmallVector<T, N>&) { return N; }
685 typedef ::testing::Types<
686 // Small mode -> Small mode.
687 std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 4>>,
688 // Small mode -> Big mode.
689 std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 2>>,
690 // Big mode -> Small mode.
691 std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 4>>,
692 // Big mode -> Big mode.
693 std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 2>>
694 > DualSmallVectorTestTypes;
696 TYPED_TEST_CASE(DualSmallVectorsTest, DualSmallVectorTestTypes);
698 TYPED_TEST(DualSmallVectorsTest, MoveAssignment) {
699 SCOPED_TRACE("MoveAssignTest-DualVectorTypes");
701 // Set up our vector with four elements.
702 for (unsigned I = 0; I < 4; ++I)
703 this->otherVector.push_back(Constructable(I));
705 const Constructable *OrigDataPtr = this->otherVector.data();
707 // Move-assign from the other vector.
709 std::move(static_cast<SmallVectorImpl<Constructable>&>(this->otherVector));
711 // Make sure we have the right result.
712 this->assertValuesInOrder(this->theVector, 4u, 0, 1, 2, 3);
714 // Make sure the # of constructor/destructor calls line up. There
715 // are two live objects after clearing the other vector.
716 this->otherVector.clear();
717 EXPECT_EQ(Constructable::getNumConstructorCalls()-4,
718 Constructable::getNumDestructorCalls());
720 // If the source vector (otherVector) was in small-mode, assert that we just
721 // moved the data pointer over.
722 EXPECT_TRUE(this->NumBuiltinElts(this->otherVector) == 4 ||
723 this->theVector.data() == OrigDataPtr);
725 // There shouldn't be any live objects any more.
726 this->theVector.clear();
727 EXPECT_EQ(Constructable::getNumConstructorCalls(),
728 Constructable::getNumDestructorCalls());
730 // We shouldn't have copied anything in this whole process.
731 EXPECT_EQ(Constructable::getNumCopyConstructorCalls(), 0);
734 struct notassignable {
736 notassignable(int &x) : x(x) {}
739 TEST(SmallVectorCustomTest, NoAssignTest) {
741 SmallVector<notassignable, 2> vec;
742 vec.push_back(notassignable(x));
744 EXPECT_EQ(42, vec.pop_back_val().x);
749 MovedFrom() : hasValue(true) {
751 MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) {
754 MovedFrom &operator=(MovedFrom&& m) {
755 hasValue = m.hasValue;
761 TEST(SmallVectorTest, MidInsert) {
762 SmallVector<MovedFrom, 3> v;
763 v.push_back(MovedFrom());
764 v.insert(v.begin(), MovedFrom());
765 for (MovedFrom &m : v)
766 EXPECT_TRUE(m.hasValue);
769 enum EmplaceableArgState {
776 template <int I> struct EmplaceableArg {
777 EmplaceableArgState State;
778 EmplaceableArg() : State(EAS_Defaulted) {}
779 EmplaceableArg(EmplaceableArg &&X)
780 : State(X.State == EAS_Arg ? EAS_RValue : EAS_Failure) {}
781 EmplaceableArg(EmplaceableArg &X)
782 : State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {}
784 explicit EmplaceableArg(bool) : State(EAS_Arg) {}
787 EmplaceableArg &operator=(EmplaceableArg &&) = delete;
788 EmplaceableArg &operator=(const EmplaceableArg &) = delete;
791 enum EmplaceableState { ES_Emplaced, ES_Moved };
793 EmplaceableArg<0> A0;
794 EmplaceableArg<1> A1;
795 EmplaceableArg<2> A2;
796 EmplaceableArg<3> A3;
797 EmplaceableState State;
799 Emplaceable() : State(ES_Emplaced) {}
801 template <class A0Ty>
802 explicit Emplaceable(A0Ty &&A0)
803 : A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {}
805 template <class A0Ty, class A1Ty>
806 Emplaceable(A0Ty &&A0, A1Ty &&A1)
807 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
808 State(ES_Emplaced) {}
810 template <class A0Ty, class A1Ty, class A2Ty>
811 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2)
812 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
813 A2(std::forward<A2Ty>(A2)), State(ES_Emplaced) {}
815 template <class A0Ty, class A1Ty, class A2Ty, class A3Ty>
816 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2, A3Ty &&A3)
817 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
818 A2(std::forward<A2Ty>(A2)), A3(std::forward<A3Ty>(A3)),
819 State(ES_Emplaced) {}
821 Emplaceable(Emplaceable &&) : State(ES_Moved) {}
822 Emplaceable &operator=(Emplaceable &&) {
828 Emplaceable(const Emplaceable &) = delete;
829 Emplaceable &operator=(const Emplaceable &) = delete;
832 TEST(SmallVectorTest, EmplaceBack) {
833 EmplaceableArg<0> A0(true);
834 EmplaceableArg<1> A1(true);
835 EmplaceableArg<2> A2(true);
836 EmplaceableArg<3> A3(true);
838 SmallVector<Emplaceable, 3> V;
840 EXPECT_TRUE(V.size() == 1);
841 EXPECT_TRUE(V.back().State == ES_Emplaced);
842 EXPECT_TRUE(V.back().A0.State == EAS_Defaulted);
843 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
844 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
845 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
848 SmallVector<Emplaceable, 3> V;
849 V.emplace_back(std::move(A0));
850 EXPECT_TRUE(V.size() == 1);
851 EXPECT_TRUE(V.back().State == ES_Emplaced);
852 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
853 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
854 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
855 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
858 SmallVector<Emplaceable, 3> V;
860 EXPECT_TRUE(V.size() == 1);
861 EXPECT_TRUE(V.back().State == ES_Emplaced);
862 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
863 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
864 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
865 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
868 SmallVector<Emplaceable, 3> V;
869 V.emplace_back(A0, A1);
870 EXPECT_TRUE(V.size() == 1);
871 EXPECT_TRUE(V.back().State == ES_Emplaced);
872 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
873 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
874 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
875 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
878 SmallVector<Emplaceable, 3> V;
879 V.emplace_back(std::move(A0), std::move(A1));
880 EXPECT_TRUE(V.size() == 1);
881 EXPECT_TRUE(V.back().State == ES_Emplaced);
882 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
883 EXPECT_TRUE(V.back().A1.State == EAS_RValue);
884 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
885 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
888 SmallVector<Emplaceable, 3> V;
889 V.emplace_back(std::move(A0), A1, std::move(A2), A3);
890 EXPECT_TRUE(V.size() == 1);
891 EXPECT_TRUE(V.back().State == ES_Emplaced);
892 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
893 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
894 EXPECT_TRUE(V.back().A2.State == EAS_RValue);
895 EXPECT_TRUE(V.back().A3.State == EAS_LValue);
898 SmallVector<int, 1> V;
901 EXPECT_EQ(2U, V.size());
907 TEST(SmallVectorTest, InitializerList) {
908 SmallVector<int, 2> V1 = {};
909 EXPECT_TRUE(V1.empty());
911 EXPECT_TRUE(makeArrayRef(V1).equals({0, 0}));
913 EXPECT_TRUE(makeArrayRef(V1).equals({-1, -1}));
915 SmallVector<int, 2> V2 = {1, 2, 3, 4};
916 EXPECT_TRUE(makeArrayRef(V2).equals({1, 2, 3, 4}));
918 EXPECT_TRUE(makeArrayRef(V2).equals({4}));
920 EXPECT_TRUE(makeArrayRef(V2).equals({4, 3, 2}));
921 V2.insert(V2.begin() + 1, 5);
922 EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2}));