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 {
161 Constructable::reset();
164 template <typename VectorT>
165 void assertEmpty(VectorT & v) {
167 EXPECT_EQ(0u, v.size());
168 EXPECT_TRUE(v.empty());
171 EXPECT_TRUE(v.begin() == v.end());
174 // Assert that v contains the specified values, in order.
175 template <typename VectorT>
176 void assertValuesInOrder(VectorT & v, size_t size, ...) {
177 EXPECT_EQ(size, v.size());
181 for (size_t i = 0; i < size; ++i) {
182 int value = va_arg(ap, int);
183 EXPECT_EQ(value, v[i].getValue());
189 // Generate a sequence of values to initialize the vector.
190 template <typename VectorT>
191 void makeSequence(VectorT & v, int start, int end) {
192 for (int i = start; i <= end; ++i) {
193 v.push_back(Constructable(i));
198 // Test fixture class
199 template <typename VectorT>
200 class SmallVectorTest : public SmallVectorTestBase {
207 typedef ::testing::Types<SmallVector<Constructable, 0>,
208 SmallVector<Constructable, 1>,
209 SmallVector<Constructable, 2>,
210 SmallVector<Constructable, 4>,
211 SmallVector<Constructable, 5>
212 > SmallVectorTestTypes;
213 TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes);
216 TYPED_TEST(SmallVectorTest, EmptyVectorTest) {
217 SCOPED_TRACE("EmptyVectorTest");
218 this->assertEmpty(this->theVector);
219 EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend());
220 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
221 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
224 // Simple insertions and deletions.
225 TYPED_TEST(SmallVectorTest, PushPopTest) {
226 SCOPED_TRACE("PushPopTest");
228 // Track whether the vector will potentially have to grow.
229 bool RequiresGrowth = this->theVector.capacity() < 3;
232 this->theVector.push_back(Constructable(1));
235 this->assertValuesInOrder(this->theVector, 1u, 1);
236 EXPECT_FALSE(this->theVector.begin() == this->theVector.end());
237 EXPECT_FALSE(this->theVector.empty());
239 // Push another element
240 this->theVector.push_back(Constructable(2));
241 this->assertValuesInOrder(this->theVector, 2u, 1, 2);
243 // Insert at beginning
244 this->theVector.insert(this->theVector.begin(), this->theVector[1]);
245 this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2);
248 this->theVector.pop_back();
249 this->assertValuesInOrder(this->theVector, 2u, 2, 1);
251 // Pop remaining elements
252 this->theVector.pop_back();
253 this->theVector.pop_back();
254 this->assertEmpty(this->theVector);
256 // Check number of constructor calls. Should be 2 for each list element,
257 // one for the argument to push_back, one for the argument to insert,
258 // and one for the list element itself.
259 if (!RequiresGrowth) {
260 EXPECT_EQ(5, Constructable::getNumConstructorCalls());
261 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
263 // If we had to grow the vector, these only have a lower bound, but should
265 EXPECT_LE(5, Constructable::getNumConstructorCalls());
266 EXPECT_EQ(Constructable::getNumConstructorCalls(),
267 Constructable::getNumDestructorCalls());
272 TYPED_TEST(SmallVectorTest, ClearTest) {
273 SCOPED_TRACE("ClearTest");
275 this->theVector.reserve(2);
276 this->makeSequence(this->theVector, 1, 2);
277 this->theVector.clear();
279 this->assertEmpty(this->theVector);
280 EXPECT_EQ(4, Constructable::getNumConstructorCalls());
281 EXPECT_EQ(4, Constructable::getNumDestructorCalls());
284 // Resize smaller test.
285 TYPED_TEST(SmallVectorTest, ResizeShrinkTest) {
286 SCOPED_TRACE("ResizeShrinkTest");
288 this->theVector.reserve(3);
289 this->makeSequence(this->theVector, 1, 3);
290 this->theVector.resize(1);
292 this->assertValuesInOrder(this->theVector, 1u, 1);
293 EXPECT_EQ(6, Constructable::getNumConstructorCalls());
294 EXPECT_EQ(5, Constructable::getNumDestructorCalls());
297 // Resize bigger test.
298 TYPED_TEST(SmallVectorTest, ResizeGrowTest) {
299 SCOPED_TRACE("ResizeGrowTest");
301 this->theVector.resize(2);
303 EXPECT_EQ(2, Constructable::getNumConstructorCalls());
304 EXPECT_EQ(0, Constructable::getNumDestructorCalls());
305 EXPECT_EQ(2u, this->theVector.size());
308 TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) {
309 this->theVector.resize(2);
311 Constructable::reset();
313 this->theVector.resize(4);
315 size_t Ctors = Constructable::getNumConstructorCalls();
316 EXPECT_TRUE(Ctors == 2 || Ctors == 4);
317 size_t MoveCtors = Constructable::getNumMoveConstructorCalls();
318 EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2);
319 size_t Dtors = Constructable::getNumDestructorCalls();
320 EXPECT_TRUE(Dtors == 0 || Dtors == 2);
323 // Resize with fill value.
324 TYPED_TEST(SmallVectorTest, ResizeFillTest) {
325 SCOPED_TRACE("ResizeFillTest");
327 this->theVector.resize(3, Constructable(77));
328 this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77);
331 // Overflow past fixed size.
332 TYPED_TEST(SmallVectorTest, OverflowTest) {
333 SCOPED_TRACE("OverflowTest");
335 // Push more elements than the fixed size.
336 this->makeSequence(this->theVector, 1, 10);
338 // Test size and values.
339 EXPECT_EQ(10u, this->theVector.size());
340 for (int i = 0; i < 10; ++i) {
341 EXPECT_EQ(i+1, this->theVector[i].getValue());
344 // Now resize back to fixed size.
345 this->theVector.resize(1);
347 this->assertValuesInOrder(this->theVector, 1u, 1);
351 TYPED_TEST(SmallVectorTest, IterationTest) {
352 this->makeSequence(this->theVector, 1, 2);
355 typename TypeParam::iterator it = this->theVector.begin();
356 EXPECT_TRUE(*it == this->theVector.front());
357 EXPECT_TRUE(*it == this->theVector[0]);
358 EXPECT_EQ(1, it->getValue());
360 EXPECT_TRUE(*it == this->theVector[1]);
361 EXPECT_TRUE(*it == this->theVector.back());
362 EXPECT_EQ(2, it->getValue());
364 EXPECT_TRUE(it == this->theVector.end());
366 EXPECT_TRUE(*it == this->theVector[1]);
367 EXPECT_EQ(2, it->getValue());
369 EXPECT_TRUE(*it == this->theVector[0]);
370 EXPECT_EQ(1, it->getValue());
373 typename TypeParam::reverse_iterator rit = this->theVector.rbegin();
374 EXPECT_TRUE(*rit == this->theVector[1]);
375 EXPECT_EQ(2, rit->getValue());
377 EXPECT_TRUE(*rit == this->theVector[0]);
378 EXPECT_EQ(1, rit->getValue());
380 EXPECT_TRUE(rit == this->theVector.rend());
382 EXPECT_TRUE(*rit == this->theVector[0]);
383 EXPECT_EQ(1, rit->getValue());
385 EXPECT_TRUE(*rit == this->theVector[1]);
386 EXPECT_EQ(2, rit->getValue());
390 TYPED_TEST(SmallVectorTest, SwapTest) {
391 SCOPED_TRACE("SwapTest");
393 this->makeSequence(this->theVector, 1, 2);
394 std::swap(this->theVector, this->otherVector);
396 this->assertEmpty(this->theVector);
397 this->assertValuesInOrder(this->otherVector, 2u, 1, 2);
401 TYPED_TEST(SmallVectorTest, AppendTest) {
402 SCOPED_TRACE("AppendTest");
404 this->makeSequence(this->otherVector, 2, 3);
406 this->theVector.push_back(Constructable(1));
407 this->theVector.append(this->otherVector.begin(), this->otherVector.end());
409 this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
412 // Append repeated test
413 TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
414 SCOPED_TRACE("AppendRepeatedTest");
416 this->theVector.push_back(Constructable(1));
417 this->theVector.append(2, Constructable(77));
418 this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77);
422 TYPED_TEST(SmallVectorTest, AssignTest) {
423 SCOPED_TRACE("AssignTest");
425 this->theVector.push_back(Constructable(1));
426 this->theVector.assign(2, Constructable(77));
427 this->assertValuesInOrder(this->theVector, 2u, 77, 77);
431 TYPED_TEST(SmallVectorTest, MoveAssignTest) {
432 SCOPED_TRACE("MoveAssignTest");
434 // Set up our vector with a single element, but enough capacity for 4.
435 this->theVector.reserve(4);
436 this->theVector.push_back(Constructable(1));
438 // Set up the other vector with 2 elements.
439 this->otherVector.push_back(Constructable(2));
440 this->otherVector.push_back(Constructable(3));
442 // Move-assign from the other vector.
443 this->theVector = std::move(this->otherVector);
445 // Make sure we have the right result.
446 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
448 // Make sure the # of constructor/destructor calls line up. There
449 // are two live objects after clearing the other vector.
450 this->otherVector.clear();
451 EXPECT_EQ(Constructable::getNumConstructorCalls()-2,
452 Constructable::getNumDestructorCalls());
454 // There shouldn't be any live objects any more.
455 this->theVector.clear();
456 EXPECT_EQ(Constructable::getNumConstructorCalls(),
457 Constructable::getNumDestructorCalls());
460 // Erase a single element
461 TYPED_TEST(SmallVectorTest, EraseTest) {
462 SCOPED_TRACE("EraseTest");
464 this->makeSequence(this->theVector, 1, 3);
465 this->theVector.erase(this->theVector.begin());
466 this->assertValuesInOrder(this->theVector, 2u, 2, 3);
469 // Erase a range of elements
470 TYPED_TEST(SmallVectorTest, EraseRangeTest) {
471 SCOPED_TRACE("EraseRangeTest");
473 this->makeSequence(this->theVector, 1, 3);
474 this->theVector.erase(this->theVector.begin(), this->theVector.begin() + 2);
475 this->assertValuesInOrder(this->theVector, 1u, 3);
478 // Insert a single element.
479 TYPED_TEST(SmallVectorTest, InsertTest) {
480 SCOPED_TRACE("InsertTest");
482 this->makeSequence(this->theVector, 1, 3);
483 typename TypeParam::iterator I =
484 this->theVector.insert(this->theVector.begin() + 1, Constructable(77));
485 EXPECT_EQ(this->theVector.begin() + 1, I);
486 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
489 // Insert a copy of a single element.
490 TYPED_TEST(SmallVectorTest, InsertCopy) {
491 SCOPED_TRACE("InsertTest");
493 this->makeSequence(this->theVector, 1, 3);
495 typename TypeParam::iterator I =
496 this->theVector.insert(this->theVector.begin() + 1, C);
497 EXPECT_EQ(this->theVector.begin() + 1, I);
498 this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
501 // Insert repeated elements.
502 TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
503 SCOPED_TRACE("InsertRepeatedTest");
505 this->makeSequence(this->theVector, 1, 4);
506 Constructable::reset();
508 this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
509 // Move construct the top element into newly allocated space, and optionally
510 // reallocate the whole buffer, move constructing into it.
511 // FIXME: This is inefficient, we shouldn't move things into newly allocated
512 // space, then move them up/around, there should only be 2 or 4 move
513 // constructions here.
514 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
515 Constructable::getNumMoveConstructorCalls() == 6);
516 // Move assign the next two to shift them up and make a gap.
517 EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls());
518 // Copy construct the two new elements from the parameter.
519 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
520 // All without any copy construction.
521 EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls());
522 EXPECT_EQ(this->theVector.begin() + 1, I);
523 this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4);
527 TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) {
528 SCOPED_TRACE("InsertRepeatedTest");
530 this->makeSequence(this->theVector, 1, 4);
531 Constructable::reset();
532 auto I = this->theVector.insert(this->theVector.end(), 2, Constructable(16));
533 // Just copy construct them into newly allocated space
534 EXPECT_EQ(2, Constructable::getNumCopyConstructorCalls());
535 // Move everything across if reallocation is needed.
536 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
537 Constructable::getNumMoveConstructorCalls() == 4);
538 // Without ever moving or copying anything else.
539 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
540 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
542 EXPECT_EQ(this->theVector.begin() + 4, I);
543 this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16);
546 TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) {
547 SCOPED_TRACE("InsertRepeatedTest");
549 this->makeSequence(this->theVector, 10, 15);
552 EXPECT_EQ(this->theVector.end(),
553 this->theVector.insert(this->theVector.end(),
554 0, Constructable(42)));
555 EXPECT_EQ(this->theVector.begin() + 1,
556 this->theVector.insert(this->theVector.begin() + 1,
557 0, Constructable(42)));
561 TYPED_TEST(SmallVectorTest, InsertRangeTest) {
562 SCOPED_TRACE("InsertRangeTest");
564 Constructable Arr[3] =
565 { Constructable(77), Constructable(77), Constructable(77) };
567 this->makeSequence(this->theVector, 1, 3);
568 Constructable::reset();
569 auto I = this->theVector.insert(this->theVector.begin() + 1, Arr, Arr + 3);
570 // Move construct the top 3 elements into newly allocated space.
571 // Possibly move the whole sequence into new space first.
572 // FIXME: This is inefficient, we shouldn't move things into newly allocated
573 // space, then move them up/around, there should only be 2 or 3 move
574 // constructions here.
575 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
576 Constructable::getNumMoveConstructorCalls() == 5);
577 // Copy assign the lower 2 new elements into existing space.
578 EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
579 // Copy construct the third element into newly allocated space.
580 EXPECT_EQ(1, Constructable::getNumCopyConstructorCalls());
581 EXPECT_EQ(this->theVector.begin() + 1, I);
582 this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3);
586 TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) {
587 SCOPED_TRACE("InsertRangeTest");
589 Constructable Arr[3] =
590 { Constructable(77), Constructable(77), Constructable(77) };
592 this->makeSequence(this->theVector, 1, 3);
595 Constructable::reset();
596 auto I = this->theVector.insert(this->theVector.end(), Arr, Arr+3);
597 // Copy construct the 3 elements into new space at the top.
598 EXPECT_EQ(3, Constructable::getNumCopyConstructorCalls());
599 // Don't copy/move anything else.
600 EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
601 // Reallocation might occur, causing all elements to be moved into the new
603 EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
604 Constructable::getNumMoveConstructorCalls() == 3);
605 EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
606 EXPECT_EQ(this->theVector.begin() + 3, I);
607 this->assertValuesInOrder(this->theVector, 6u,
608 1, 2, 3, 77, 77, 77);
611 TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
612 SCOPED_TRACE("InsertRangeTest");
614 this->makeSequence(this->theVector, 1, 3);
617 EXPECT_EQ(this->theVector.end(),
618 this->theVector.insert(this->theVector.end(),
619 this->theVector.begin(),
620 this->theVector.begin()));
621 EXPECT_EQ(this->theVector.begin() + 1,
622 this->theVector.insert(this->theVector.begin() + 1,
623 this->theVector.begin(),
624 this->theVector.begin()));
628 TYPED_TEST(SmallVectorTest, ComparisonTest) {
629 SCOPED_TRACE("ComparisonTest");
631 this->makeSequence(this->theVector, 1, 3);
632 this->makeSequence(this->otherVector, 1, 3);
634 EXPECT_TRUE(this->theVector == this->otherVector);
635 EXPECT_FALSE(this->theVector != this->otherVector);
637 this->otherVector.clear();
638 this->makeSequence(this->otherVector, 2, 4);
640 EXPECT_FALSE(this->theVector == this->otherVector);
641 EXPECT_TRUE(this->theVector != this->otherVector);
644 // Constant vector tests.
645 TYPED_TEST(SmallVectorTest, ConstVectorTest) {
646 const TypeParam constVector;
648 EXPECT_EQ(0u, constVector.size());
649 EXPECT_TRUE(constVector.empty());
650 EXPECT_TRUE(constVector.begin() == constVector.end());
653 // Direct array access.
654 TYPED_TEST(SmallVectorTest, DirectVectorTest) {
655 EXPECT_EQ(0u, this->theVector.size());
656 this->theVector.reserve(4);
657 EXPECT_LE(4u, this->theVector.capacity());
658 EXPECT_EQ(0, Constructable::getNumConstructorCalls());
659 this->theVector.push_back(1);
660 this->theVector.push_back(2);
661 this->theVector.push_back(3);
662 this->theVector.push_back(4);
663 EXPECT_EQ(4u, this->theVector.size());
664 EXPECT_EQ(8, Constructable::getNumConstructorCalls());
665 EXPECT_EQ(1, this->theVector[0].getValue());
666 EXPECT_EQ(2, this->theVector[1].getValue());
667 EXPECT_EQ(3, this->theVector[2].getValue());
668 EXPECT_EQ(4, this->theVector[3].getValue());
671 TYPED_TEST(SmallVectorTest, IteratorTest) {
673 this->theVector.insert(this->theVector.end(), L.begin(), L.end());
676 template <typename InvalidType> class DualSmallVectorsTest;
678 template <typename VectorT1, typename VectorT2>
679 class DualSmallVectorsTest<std::pair<VectorT1, VectorT2>> : public SmallVectorTestBase {
682 VectorT2 otherVector;
684 template <typename T, unsigned N>
685 static unsigned NumBuiltinElts(const SmallVector<T, N>&) { return N; }
688 typedef ::testing::Types<
689 // Small mode -> Small mode.
690 std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 4>>,
691 // Small mode -> Big mode.
692 std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 2>>,
693 // Big mode -> Small mode.
694 std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 4>>,
695 // Big mode -> Big mode.
696 std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 2>>
697 > DualSmallVectorTestTypes;
699 TYPED_TEST_CASE(DualSmallVectorsTest, DualSmallVectorTestTypes);
701 TYPED_TEST(DualSmallVectorsTest, MoveAssignment) {
702 SCOPED_TRACE("MoveAssignTest-DualVectorTypes");
704 // Set up our vector with four elements.
705 for (unsigned I = 0; I < 4; ++I)
706 this->otherVector.push_back(Constructable(I));
708 const Constructable *OrigDataPtr = this->otherVector.data();
710 // Move-assign from the other vector.
712 std::move(static_cast<SmallVectorImpl<Constructable>&>(this->otherVector));
714 // Make sure we have the right result.
715 this->assertValuesInOrder(this->theVector, 4u, 0, 1, 2, 3);
717 // Make sure the # of constructor/destructor calls line up. There
718 // are two live objects after clearing the other vector.
719 this->otherVector.clear();
720 EXPECT_EQ(Constructable::getNumConstructorCalls()-4,
721 Constructable::getNumDestructorCalls());
723 // If the source vector (otherVector) was in small-mode, assert that we just
724 // moved the data pointer over.
725 EXPECT_TRUE(this->NumBuiltinElts(this->otherVector) == 4 ||
726 this->theVector.data() == OrigDataPtr);
728 // There shouldn't be any live objects any more.
729 this->theVector.clear();
730 EXPECT_EQ(Constructable::getNumConstructorCalls(),
731 Constructable::getNumDestructorCalls());
733 // We shouldn't have copied anything in this whole process.
734 EXPECT_EQ(Constructable::getNumCopyConstructorCalls(), 0);
737 struct notassignable {
739 notassignable(int &x) : x(x) {}
742 TEST(SmallVectorCustomTest, NoAssignTest) {
744 SmallVector<notassignable, 2> vec;
745 vec.push_back(notassignable(x));
747 EXPECT_EQ(42, vec.pop_back_val().x);
752 MovedFrom() : hasValue(true) {
754 MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) {
757 MovedFrom &operator=(MovedFrom&& m) {
758 hasValue = m.hasValue;
764 TEST(SmallVectorTest, MidInsert) {
765 SmallVector<MovedFrom, 3> v;
766 v.push_back(MovedFrom());
767 v.insert(v.begin(), MovedFrom());
768 for (MovedFrom &m : v)
769 EXPECT_TRUE(m.hasValue);
772 enum EmplaceableArgState {
779 template <int I> struct EmplaceableArg {
780 EmplaceableArgState State;
781 EmplaceableArg() : State(EAS_Defaulted) {}
782 EmplaceableArg(EmplaceableArg &&X)
783 : State(X.State == EAS_Arg ? EAS_RValue : EAS_Failure) {}
784 EmplaceableArg(EmplaceableArg &X)
785 : State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {}
787 explicit EmplaceableArg(bool) : State(EAS_Arg) {}
790 EmplaceableArg &operator=(EmplaceableArg &&) = delete;
791 EmplaceableArg &operator=(const EmplaceableArg &) = delete;
794 enum EmplaceableState { ES_Emplaced, ES_Moved };
796 EmplaceableArg<0> A0;
797 EmplaceableArg<1> A1;
798 EmplaceableArg<2> A2;
799 EmplaceableArg<3> A3;
800 EmplaceableState State;
802 Emplaceable() : State(ES_Emplaced) {}
804 template <class A0Ty>
805 explicit Emplaceable(A0Ty &&A0)
806 : A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {}
808 template <class A0Ty, class A1Ty>
809 Emplaceable(A0Ty &&A0, A1Ty &&A1)
810 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
811 State(ES_Emplaced) {}
813 template <class A0Ty, class A1Ty, class A2Ty>
814 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2)
815 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
816 A2(std::forward<A2Ty>(A2)), State(ES_Emplaced) {}
818 template <class A0Ty, class A1Ty, class A2Ty, class A3Ty>
819 Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2, A3Ty &&A3)
820 : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
821 A2(std::forward<A2Ty>(A2)), A3(std::forward<A3Ty>(A3)),
822 State(ES_Emplaced) {}
824 Emplaceable(Emplaceable &&) : State(ES_Moved) {}
825 Emplaceable &operator=(Emplaceable &&) {
831 Emplaceable(const Emplaceable &) = delete;
832 Emplaceable &operator=(const Emplaceable &) = delete;
835 TEST(SmallVectorTest, EmplaceBack) {
836 EmplaceableArg<0> A0(true);
837 EmplaceableArg<1> A1(true);
838 EmplaceableArg<2> A2(true);
839 EmplaceableArg<3> A3(true);
841 SmallVector<Emplaceable, 3> V;
843 EXPECT_TRUE(V.size() == 1);
844 EXPECT_TRUE(V.back().State == ES_Emplaced);
845 EXPECT_TRUE(V.back().A0.State == EAS_Defaulted);
846 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
847 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
848 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
851 SmallVector<Emplaceable, 3> V;
852 V.emplace_back(std::move(A0));
853 EXPECT_TRUE(V.size() == 1);
854 EXPECT_TRUE(V.back().State == ES_Emplaced);
855 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
856 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
857 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
858 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
861 SmallVector<Emplaceable, 3> V;
863 EXPECT_TRUE(V.size() == 1);
864 EXPECT_TRUE(V.back().State == ES_Emplaced);
865 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
866 EXPECT_TRUE(V.back().A1.State == EAS_Defaulted);
867 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
868 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
871 SmallVector<Emplaceable, 3> V;
872 V.emplace_back(A0, A1);
873 EXPECT_TRUE(V.size() == 1);
874 EXPECT_TRUE(V.back().State == ES_Emplaced);
875 EXPECT_TRUE(V.back().A0.State == EAS_LValue);
876 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
877 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
878 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
881 SmallVector<Emplaceable, 3> V;
882 V.emplace_back(std::move(A0), std::move(A1));
883 EXPECT_TRUE(V.size() == 1);
884 EXPECT_TRUE(V.back().State == ES_Emplaced);
885 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
886 EXPECT_TRUE(V.back().A1.State == EAS_RValue);
887 EXPECT_TRUE(V.back().A2.State == EAS_Defaulted);
888 EXPECT_TRUE(V.back().A3.State == EAS_Defaulted);
891 SmallVector<Emplaceable, 3> V;
892 V.emplace_back(std::move(A0), A1, std::move(A2), A3);
893 EXPECT_TRUE(V.size() == 1);
894 EXPECT_TRUE(V.back().State == ES_Emplaced);
895 EXPECT_TRUE(V.back().A0.State == EAS_RValue);
896 EXPECT_TRUE(V.back().A1.State == EAS_LValue);
897 EXPECT_TRUE(V.back().A2.State == EAS_RValue);
898 EXPECT_TRUE(V.back().A3.State == EAS_LValue);
901 SmallVector<int, 1> V;
904 EXPECT_EQ(2U, V.size());
910 TEST(SmallVectorTest, InitializerList) {
911 SmallVector<int, 2> V1 = {};
912 EXPECT_TRUE(V1.empty());
914 EXPECT_TRUE(makeArrayRef(V1).equals({0, 0}));
916 EXPECT_TRUE(makeArrayRef(V1).equals({-1, -1}));
918 SmallVector<int, 2> V2 = {1, 2, 3, 4};
919 EXPECT_TRUE(makeArrayRef(V2).equals({1, 2, 3, 4}));
921 EXPECT_TRUE(makeArrayRef(V2).equals({4}));
923 EXPECT_TRUE(makeArrayRef(V2).equals({4, 3, 2}));
924 V2.insert(V2.begin() + 1, 5);
925 EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2}));