Bill Wendling | 8d069ef | 2009-01-11 01:25:51 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/SmallVectorTest.cpp ------------------------------===// |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 2 | // |
| 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 Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/ArrayRef.h" |
Bill Wendling | bfba2f1 | 2010-08-19 18:52:02 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | 130cec2 | 2012-12-04 10:23:08 +0000 | [diff] [blame] | 17 | #include "gtest/gtest.h" |
Dan Gohman | 42e77df | 2010-03-26 18:53:37 +0000 | [diff] [blame] | 18 | #include <list> |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 19 | #include <stdarg.h> |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | /// A helper class that counts the total number of constructor and |
| 26 | /// destructor calls. |
| 27 | class Constructable { |
| 28 | private: |
| 29 | static int numConstructorCalls; |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 30 | static int numMoveConstructorCalls; |
| 31 | static int numCopyConstructorCalls; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 32 | static int numDestructorCalls; |
| 33 | static int numAssignmentCalls; |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 34 | static int numMoveAssignmentCalls; |
| 35 | static int numCopyAssignmentCalls; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 36 | |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 37 | bool constructed; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 38 | int value; |
| 39 | |
| 40 | public: |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 41 | Constructable() : constructed(true), value(0) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 42 | ++numConstructorCalls; |
| 43 | } |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 45 | Constructable(int val) : constructed(true), value(val) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 46 | ++numConstructorCalls; |
| 47 | } |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 48 | |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 49 | Constructable(const Constructable & src) : constructed(true) { |
| 50 | value = src.value; |
| 51 | ++numConstructorCalls; |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 52 | ++numCopyConstructorCalls; |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | Constructable(Constructable && src) : constructed(true) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 56 | value = src.value; |
| 57 | ++numConstructorCalls; |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 58 | ++numMoveConstructorCalls; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 59 | } |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 60 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 61 | ~Constructable() { |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 62 | EXPECT_TRUE(constructed); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 63 | ++numDestructorCalls; |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 64 | constructed = false; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 65 | } |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 66 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 67 | Constructable & operator=(const Constructable & src) { |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 68 | EXPECT_TRUE(constructed); |
| 69 | value = src.value; |
| 70 | ++numAssignmentCalls; |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 71 | ++numCopyAssignmentCalls; |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | Constructable & operator=(Constructable && src) { |
| 76 | EXPECT_TRUE(constructed); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 77 | value = src.value; |
| 78 | ++numAssignmentCalls; |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 79 | ++numMoveAssignmentCalls; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 80 | return *this; |
| 81 | } |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 82 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 83 | int getValue() const { |
| 84 | return abs(value); |
| 85 | } |
| 86 | |
| 87 | static void reset() { |
| 88 | numConstructorCalls = 0; |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 89 | numMoveConstructorCalls = 0; |
| 90 | numCopyConstructorCalls = 0; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 91 | numDestructorCalls = 0; |
| 92 | numAssignmentCalls = 0; |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 93 | numMoveAssignmentCalls = 0; |
| 94 | numCopyAssignmentCalls = 0; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 95 | } |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 96 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 97 | static int getNumConstructorCalls() { |
| 98 | return numConstructorCalls; |
| 99 | } |
| 100 | |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 101 | static int getNumMoveConstructorCalls() { |
| 102 | return numMoveConstructorCalls; |
| 103 | } |
| 104 | |
| 105 | static int getNumCopyConstructorCalls() { |
| 106 | return numCopyConstructorCalls; |
| 107 | } |
| 108 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 109 | static int getNumDestructorCalls() { |
| 110 | return numDestructorCalls; |
| 111 | } |
| 112 | |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 113 | static int getNumAssignmentCalls() { |
| 114 | return numAssignmentCalls; |
| 115 | } |
| 116 | |
| 117 | static int getNumMoveAssignmentCalls() { |
| 118 | return numMoveAssignmentCalls; |
| 119 | } |
| 120 | |
| 121 | static int getNumCopyAssignmentCalls() { |
| 122 | return numCopyAssignmentCalls; |
| 123 | } |
| 124 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 125 | friend bool operator==(const Constructable & c0, const Constructable & c1) { |
| 126 | return c0.getValue() == c1.getValue(); |
| 127 | } |
| 128 | |
Chandler Carruth | 88c54b8 | 2010-10-23 08:10:43 +0000 | [diff] [blame] | 129 | friend bool LLVM_ATTRIBUTE_UNUSED |
Bill Wendling | bfba2f1 | 2010-08-19 18:52:02 +0000 | [diff] [blame] | 130 | operator!=(const Constructable & c0, const Constructable & c1) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 131 | return c0.getValue() != c1.getValue(); |
| 132 | } |
| 133 | }; |
| 134 | |
| 135 | int Constructable::numConstructorCalls; |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 136 | int Constructable::numCopyConstructorCalls; |
| 137 | int Constructable::numMoveConstructorCalls; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 138 | int Constructable::numDestructorCalls; |
| 139 | int Constructable::numAssignmentCalls; |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 140 | int Constructable::numCopyAssignmentCalls; |
| 141 | int Constructable::numMoveAssignmentCalls; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 142 | |
David Blaikie | 669fc86 | 2014-06-09 22:26:20 +0000 | [diff] [blame] | 143 | struct NonCopyable { |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 144 | NonCopyable() {} |
| 145 | NonCopyable(NonCopyable &&) {} |
David Blaikie | 669fc86 | 2014-06-09 22:26:20 +0000 | [diff] [blame] | 146 | NonCopyable &operator=(NonCopyable &&) { return *this; } |
David Blaikie | 11e0876 | 2014-06-11 17:50:14 +0000 | [diff] [blame] | 147 | private: |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 148 | NonCopyable(const NonCopyable &) = delete; |
| 149 | NonCopyable &operator=(const NonCopyable &) = delete; |
David Blaikie | 669fc86 | 2014-06-09 22:26:20 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | LLVM_ATTRIBUTE_USED void CompileTest() { |
| 153 | SmallVector<NonCopyable, 0> V; |
| 154 | V.resize(42); |
| 155 | } |
| 156 | |
Lang Hames | ac92bfc | 2015-01-23 06:25:17 +0000 | [diff] [blame] | 157 | class SmallVectorTestBase : public testing::Test { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 158 | protected: |
Alexander Kornienko | f817c1c | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 159 | void SetUp() override { Constructable::reset(); } |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 160 | |
Lang Hames | ac92bfc | 2015-01-23 06:25:17 +0000 | [diff] [blame] | 161 | template <typename VectorT> |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 162 | void assertEmpty(VectorT & v) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 163 | // Size tests |
| 164 | EXPECT_EQ(0u, v.size()); |
| 165 | EXPECT_TRUE(v.empty()); |
| 166 | |
| 167 | // Iterator tests |
| 168 | EXPECT_TRUE(v.begin() == v.end()); |
| 169 | } |
| 170 | |
Lang Hames | ac92bfc | 2015-01-23 06:25:17 +0000 | [diff] [blame] | 171 | // Assert that v contains the specified values, in order. |
| 172 | template <typename VectorT> |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 173 | void assertValuesInOrder(VectorT & v, size_t size, ...) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 174 | EXPECT_EQ(size, v.size()); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 175 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 176 | va_list ap; |
| 177 | va_start(ap, size); |
| 178 | for (size_t i = 0; i < size; ++i) { |
| 179 | int value = va_arg(ap, int); |
| 180 | EXPECT_EQ(value, v[i].getValue()); |
| 181 | } |
| 182 | |
| 183 | va_end(ap); |
| 184 | } |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 185 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 186 | // Generate a sequence of values to initialize the vector. |
Lang Hames | ac92bfc | 2015-01-23 06:25:17 +0000 | [diff] [blame] | 187 | template <typename VectorT> |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 188 | void makeSequence(VectorT & v, int start, int end) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 189 | for (int i = start; i <= end; ++i) { |
| 190 | v.push_back(Constructable(i)); |
| 191 | } |
| 192 | } |
| 193 | }; |
| 194 | |
Lang Hames | ac92bfc | 2015-01-23 06:25:17 +0000 | [diff] [blame] | 195 | // Test fixture class |
| 196 | template <typename VectorT> |
| 197 | class SmallVectorTest : public SmallVectorTestBase { |
| 198 | protected: |
| 199 | VectorT theVector; |
| 200 | VectorT otherVector; |
| 201 | }; |
| 202 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 203 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 204 | typedef ::testing::Types<SmallVector<Constructable, 0>, |
| 205 | SmallVector<Constructable, 1>, |
| 206 | SmallVector<Constructable, 2>, |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 207 | SmallVector<Constructable, 4>, |
| 208 | SmallVector<Constructable, 5> |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 209 | > SmallVectorTestTypes; |
| 210 | TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes); |
| 211 | |
Francis Ricci | e22b696 | 2017-06-09 20:31:53 +0000 | [diff] [blame] | 212 | // Constructor test. |
| 213 | TYPED_TEST(SmallVectorTest, ConstructorNonIterTest) { |
| 214 | SCOPED_TRACE("ConstructorTest"); |
| 215 | this->theVector = SmallVector<Constructable, 2>(2, 2); |
| 216 | this->assertValuesInOrder(this->theVector, 2u, 2, 2); |
| 217 | } |
| 218 | |
| 219 | // Constructor test. |
| 220 | TYPED_TEST(SmallVectorTest, ConstructorIterTest) { |
| 221 | SCOPED_TRACE("ConstructorTest"); |
| 222 | int arr[] = {1, 2, 3}; |
| 223 | this->theVector = |
| 224 | SmallVector<Constructable, 4>(std::begin(arr), std::end(arr)); |
| 225 | this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3); |
| 226 | } |
| 227 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 228 | // New vector test. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 229 | TYPED_TEST(SmallVectorTest, EmptyVectorTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 230 | SCOPED_TRACE("EmptyVectorTest"); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 231 | this->assertEmpty(this->theVector); |
| 232 | EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 233 | EXPECT_EQ(0, Constructable::getNumConstructorCalls()); |
| 234 | EXPECT_EQ(0, Constructable::getNumDestructorCalls()); |
| 235 | } |
| 236 | |
| 237 | // Simple insertions and deletions. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 238 | TYPED_TEST(SmallVectorTest, PushPopTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 239 | SCOPED_TRACE("PushPopTest"); |
| 240 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 241 | // Track whether the vector will potentially have to grow. |
| 242 | bool RequiresGrowth = this->theVector.capacity() < 3; |
| 243 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 244 | // Push an element |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 245 | this->theVector.push_back(Constructable(1)); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 246 | |
| 247 | // Size tests |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 248 | this->assertValuesInOrder(this->theVector, 1u, 1); |
| 249 | EXPECT_FALSE(this->theVector.begin() == this->theVector.end()); |
| 250 | EXPECT_FALSE(this->theVector.empty()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 251 | |
| 252 | // Push another element |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 253 | this->theVector.push_back(Constructable(2)); |
| 254 | this->assertValuesInOrder(this->theVector, 2u, 1, 2); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 255 | |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 256 | // Insert at beginning |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 257 | this->theVector.insert(this->theVector.begin(), this->theVector[1]); |
| 258 | this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 259 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 260 | // Pop one element |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 261 | this->theVector.pop_back(); |
| 262 | this->assertValuesInOrder(this->theVector, 2u, 2, 1); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 263 | |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 264 | // Pop remaining elements |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 265 | this->theVector.pop_back(); |
| 266 | this->theVector.pop_back(); |
| 267 | this->assertEmpty(this->theVector); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 268 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 269 | // Check number of constructor calls. Should be 2 for each list element, |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 270 | // one for the argument to push_back, one for the argument to insert, |
| 271 | // and one for the list element itself. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 272 | if (!RequiresGrowth) { |
| 273 | EXPECT_EQ(5, Constructable::getNumConstructorCalls()); |
| 274 | EXPECT_EQ(5, Constructable::getNumDestructorCalls()); |
| 275 | } else { |
| 276 | // If we had to grow the vector, these only have a lower bound, but should |
| 277 | // always be equal. |
| 278 | EXPECT_LE(5, Constructable::getNumConstructorCalls()); |
| 279 | EXPECT_EQ(Constructable::getNumConstructorCalls(), |
| 280 | Constructable::getNumDestructorCalls()); |
| 281 | } |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | // Clear test. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 285 | TYPED_TEST(SmallVectorTest, ClearTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 286 | SCOPED_TRACE("ClearTest"); |
| 287 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 288 | this->theVector.reserve(2); |
| 289 | this->makeSequence(this->theVector, 1, 2); |
| 290 | this->theVector.clear(); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 291 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 292 | this->assertEmpty(this->theVector); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 293 | EXPECT_EQ(4, Constructable::getNumConstructorCalls()); |
| 294 | EXPECT_EQ(4, Constructable::getNumDestructorCalls()); |
| 295 | } |
| 296 | |
| 297 | // Resize smaller test. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 298 | TYPED_TEST(SmallVectorTest, ResizeShrinkTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 299 | SCOPED_TRACE("ResizeShrinkTest"); |
| 300 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 301 | this->theVector.reserve(3); |
| 302 | this->makeSequence(this->theVector, 1, 3); |
| 303 | this->theVector.resize(1); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 304 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 305 | this->assertValuesInOrder(this->theVector, 1u, 1); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 306 | EXPECT_EQ(6, Constructable::getNumConstructorCalls()); |
| 307 | EXPECT_EQ(5, Constructable::getNumDestructorCalls()); |
| 308 | } |
| 309 | |
| 310 | // Resize bigger test. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 311 | TYPED_TEST(SmallVectorTest, ResizeGrowTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 312 | SCOPED_TRACE("ResizeGrowTest"); |
| 313 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 314 | this->theVector.resize(2); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 315 | |
David Blaikie | 669fc86 | 2014-06-09 22:26:20 +0000 | [diff] [blame] | 316 | EXPECT_EQ(2, Constructable::getNumConstructorCalls()); |
| 317 | EXPECT_EQ(0, Constructable::getNumDestructorCalls()); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 318 | EXPECT_EQ(2u, this->theVector.size()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 319 | } |
| 320 | |
David Blaikie | 669fc86 | 2014-06-09 22:26:20 +0000 | [diff] [blame] | 321 | TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) { |
| 322 | this->theVector.resize(2); |
| 323 | |
| 324 | Constructable::reset(); |
| 325 | |
| 326 | this->theVector.resize(4); |
| 327 | |
| 328 | size_t Ctors = Constructable::getNumConstructorCalls(); |
| 329 | EXPECT_TRUE(Ctors == 2 || Ctors == 4); |
| 330 | size_t MoveCtors = Constructable::getNumMoveConstructorCalls(); |
| 331 | EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2); |
| 332 | size_t Dtors = Constructable::getNumDestructorCalls(); |
| 333 | EXPECT_TRUE(Dtors == 0 || Dtors == 2); |
| 334 | } |
| 335 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 336 | // Resize with fill value. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 337 | TYPED_TEST(SmallVectorTest, ResizeFillTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 338 | SCOPED_TRACE("ResizeFillTest"); |
| 339 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 340 | this->theVector.resize(3, Constructable(77)); |
| 341 | this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | // Overflow past fixed size. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 345 | TYPED_TEST(SmallVectorTest, OverflowTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 346 | SCOPED_TRACE("OverflowTest"); |
| 347 | |
Daniel Dunbar | 6d6023b | 2009-07-12 19:45:34 +0000 | [diff] [blame] | 348 | // Push more elements than the fixed size. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 349 | this->makeSequence(this->theVector, 1, 10); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 350 | |
Daniel Dunbar | 6d6023b | 2009-07-12 19:45:34 +0000 | [diff] [blame] | 351 | // Test size and values. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 352 | EXPECT_EQ(10u, this->theVector.size()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 353 | for (int i = 0; i < 10; ++i) { |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 354 | EXPECT_EQ(i+1, this->theVector[i].getValue()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 355 | } |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 356 | |
Daniel Dunbar | 6d6023b | 2009-07-12 19:45:34 +0000 | [diff] [blame] | 357 | // Now resize back to fixed size. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 358 | this->theVector.resize(1); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 359 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 360 | this->assertValuesInOrder(this->theVector, 1u, 1); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Iteration tests. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 364 | TYPED_TEST(SmallVectorTest, IterationTest) { |
| 365 | this->makeSequence(this->theVector, 1, 2); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 366 | |
| 367 | // Forward Iteration |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 368 | typename TypeParam::iterator it = this->theVector.begin(); |
| 369 | EXPECT_TRUE(*it == this->theVector.front()); |
| 370 | EXPECT_TRUE(*it == this->theVector[0]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 371 | EXPECT_EQ(1, it->getValue()); |
| 372 | ++it; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 373 | EXPECT_TRUE(*it == this->theVector[1]); |
| 374 | EXPECT_TRUE(*it == this->theVector.back()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 375 | EXPECT_EQ(2, it->getValue()); |
| 376 | ++it; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 377 | EXPECT_TRUE(it == this->theVector.end()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 378 | --it; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 379 | EXPECT_TRUE(*it == this->theVector[1]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 380 | EXPECT_EQ(2, it->getValue()); |
| 381 | --it; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 382 | EXPECT_TRUE(*it == this->theVector[0]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 383 | EXPECT_EQ(1, it->getValue()); |
| 384 | |
| 385 | // Reverse Iteration |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 386 | typename TypeParam::reverse_iterator rit = this->theVector.rbegin(); |
| 387 | EXPECT_TRUE(*rit == this->theVector[1]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 388 | EXPECT_EQ(2, rit->getValue()); |
| 389 | ++rit; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 390 | EXPECT_TRUE(*rit == this->theVector[0]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 391 | EXPECT_EQ(1, rit->getValue()); |
| 392 | ++rit; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 393 | EXPECT_TRUE(rit == this->theVector.rend()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 394 | --rit; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 395 | EXPECT_TRUE(*rit == this->theVector[0]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 396 | EXPECT_EQ(1, rit->getValue()); |
| 397 | --rit; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 398 | EXPECT_TRUE(*rit == this->theVector[1]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 399 | EXPECT_EQ(2, rit->getValue()); |
| 400 | } |
| 401 | |
| 402 | // Swap test. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 403 | TYPED_TEST(SmallVectorTest, SwapTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 404 | SCOPED_TRACE("SwapTest"); |
| 405 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 406 | this->makeSequence(this->theVector, 1, 2); |
| 407 | std::swap(this->theVector, this->otherVector); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 408 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 409 | this->assertEmpty(this->theVector); |
| 410 | this->assertValuesInOrder(this->otherVector, 2u, 1, 2); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // Append test |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 414 | TYPED_TEST(SmallVectorTest, AppendTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 415 | SCOPED_TRACE("AppendTest"); |
| 416 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 417 | this->makeSequence(this->otherVector, 2, 3); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 418 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 419 | this->theVector.push_back(Constructable(1)); |
| 420 | this->theVector.append(this->otherVector.begin(), this->otherVector.end()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 421 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 422 | this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | // Append repeated test |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 426 | TYPED_TEST(SmallVectorTest, AppendRepeatedTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 427 | SCOPED_TRACE("AppendRepeatedTest"); |
| 428 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 429 | this->theVector.push_back(Constructable(1)); |
| 430 | this->theVector.append(2, Constructable(77)); |
| 431 | this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Francis Ricci | e22b696 | 2017-06-09 20:31:53 +0000 | [diff] [blame] | 434 | // Append test |
| 435 | TYPED_TEST(SmallVectorTest, AppendNonIterTest) { |
| 436 | SCOPED_TRACE("AppendRepeatedTest"); |
| 437 | |
| 438 | this->theVector.push_back(Constructable(1)); |
| 439 | this->theVector.append(2, 7); |
| 440 | this->assertValuesInOrder(this->theVector, 3u, 1, 7, 7); |
| 441 | } |
| 442 | |
Francis Ricci | 41b4f1a | 2017-06-12 14:19:25 +0000 | [diff] [blame] | 443 | struct output_iterator { |
| 444 | typedef std::output_iterator_tag iterator_category; |
| 445 | typedef int value_type; |
| 446 | typedef int difference_type; |
| 447 | typedef value_type *pointer; |
| 448 | typedef value_type &reference; |
| 449 | operator int() { return 2; } |
| 450 | operator Constructable() { return 7; } |
| 451 | }; |
| 452 | |
Francis Ricci | e22b696 | 2017-06-09 20:31:53 +0000 | [diff] [blame] | 453 | TYPED_TEST(SmallVectorTest, AppendRepeatedNonForwardIterator) { |
| 454 | SCOPED_TRACE("AppendRepeatedTest"); |
| 455 | |
Francis Ricci | e22b696 | 2017-06-09 20:31:53 +0000 | [diff] [blame] | 456 | this->theVector.push_back(Constructable(1)); |
| 457 | this->theVector.append(output_iterator(), output_iterator()); |
| 458 | this->assertValuesInOrder(this->theVector, 3u, 1, 7, 7); |
| 459 | } |
| 460 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 461 | // Assign test |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 462 | TYPED_TEST(SmallVectorTest, AssignTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 463 | SCOPED_TRACE("AssignTest"); |
| 464 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 465 | this->theVector.push_back(Constructable(1)); |
| 466 | this->theVector.assign(2, Constructable(77)); |
| 467 | this->assertValuesInOrder(this->theVector, 2u, 77, 77); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 468 | } |
| 469 | |
David Blaikie | b6b42e0 | 2017-06-02 17:24:26 +0000 | [diff] [blame] | 470 | // Assign test |
| 471 | TYPED_TEST(SmallVectorTest, AssignRangeTest) { |
| 472 | SCOPED_TRACE("AssignTest"); |
| 473 | |
| 474 | this->theVector.push_back(Constructable(1)); |
| 475 | int arr[] = {1, 2, 3}; |
| 476 | this->theVector.assign(std::begin(arr), std::end(arr)); |
| 477 | this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3); |
| 478 | } |
| 479 | |
Francis Ricci | e22b696 | 2017-06-09 20:31:53 +0000 | [diff] [blame] | 480 | // Assign test |
| 481 | TYPED_TEST(SmallVectorTest, AssignNonIterTest) { |
| 482 | SCOPED_TRACE("AssignTest"); |
| 483 | |
| 484 | this->theVector.push_back(Constructable(1)); |
| 485 | this->theVector.assign(2, 7); |
| 486 | this->assertValuesInOrder(this->theVector, 2u, 7, 7); |
| 487 | } |
| 488 | |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 489 | // Move-assign test |
| 490 | TYPED_TEST(SmallVectorTest, MoveAssignTest) { |
| 491 | SCOPED_TRACE("MoveAssignTest"); |
| 492 | |
| 493 | // Set up our vector with a single element, but enough capacity for 4. |
| 494 | this->theVector.reserve(4); |
| 495 | this->theVector.push_back(Constructable(1)); |
| 496 | |
| 497 | // Set up the other vector with 2 elements. |
| 498 | this->otherVector.push_back(Constructable(2)); |
| 499 | this->otherVector.push_back(Constructable(3)); |
| 500 | |
| 501 | // Move-assign from the other vector. |
| 502 | this->theVector = std::move(this->otherVector); |
| 503 | |
| 504 | // Make sure we have the right result. |
| 505 | this->assertValuesInOrder(this->theVector, 2u, 2, 3); |
| 506 | |
| 507 | // Make sure the # of constructor/destructor calls line up. There |
| 508 | // are two live objects after clearing the other vector. |
| 509 | this->otherVector.clear(); |
| 510 | EXPECT_EQ(Constructable::getNumConstructorCalls()-2, |
| 511 | Constructable::getNumDestructorCalls()); |
| 512 | |
| 513 | // There shouldn't be any live objects any more. |
| 514 | this->theVector.clear(); |
| 515 | EXPECT_EQ(Constructable::getNumConstructorCalls(), |
| 516 | Constructable::getNumDestructorCalls()); |
| 517 | } |
| 518 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 519 | // Erase a single element |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 520 | TYPED_TEST(SmallVectorTest, EraseTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 521 | SCOPED_TRACE("EraseTest"); |
| 522 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 523 | this->makeSequence(this->theVector, 1, 3); |
David Blaikie | 6ae4bc8 | 2016-03-24 20:25:51 +0000 | [diff] [blame] | 524 | const auto &theConstVector = this->theVector; |
| 525 | this->theVector.erase(theConstVector.begin()); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 526 | this->assertValuesInOrder(this->theVector, 2u, 2, 3); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | // Erase a range of elements |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 530 | TYPED_TEST(SmallVectorTest, EraseRangeTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 531 | SCOPED_TRACE("EraseRangeTest"); |
| 532 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 533 | this->makeSequence(this->theVector, 1, 3); |
David Blaikie | 6ae4bc8 | 2016-03-24 20:25:51 +0000 | [diff] [blame] | 534 | const auto &theConstVector = this->theVector; |
| 535 | this->theVector.erase(theConstVector.begin(), theConstVector.begin() + 2); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 536 | this->assertValuesInOrder(this->theVector, 1u, 3); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | // Insert a single element. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 540 | TYPED_TEST(SmallVectorTest, InsertTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 541 | SCOPED_TRACE("InsertTest"); |
| 542 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 543 | this->makeSequence(this->theVector, 1, 3); |
| 544 | typename TypeParam::iterator I = |
| 545 | this->theVector.insert(this->theVector.begin() + 1, Constructable(77)); |
| 546 | EXPECT_EQ(this->theVector.begin() + 1, I); |
| 547 | this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 548 | } |
| 549 | |
David Blaikie | 40d4e34 | 2014-06-08 16:55:13 +0000 | [diff] [blame] | 550 | // Insert a copy of a single element. |
| 551 | TYPED_TEST(SmallVectorTest, InsertCopy) { |
| 552 | SCOPED_TRACE("InsertTest"); |
| 553 | |
| 554 | this->makeSequence(this->theVector, 1, 3); |
| 555 | Constructable C(77); |
| 556 | typename TypeParam::iterator I = |
| 557 | this->theVector.insert(this->theVector.begin() + 1, C); |
| 558 | EXPECT_EQ(this->theVector.begin() + 1, I); |
| 559 | this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3); |
| 560 | } |
| 561 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 562 | // Insert repeated elements. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 563 | TYPED_TEST(SmallVectorTest, InsertRepeatedTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 564 | SCOPED_TRACE("InsertRepeatedTest"); |
| 565 | |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 566 | this->makeSequence(this->theVector, 1, 4); |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 567 | Constructable::reset(); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 568 | auto I = |
| 569 | this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16)); |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 570 | // Move construct the top element into newly allocated space, and optionally |
| 571 | // reallocate the whole buffer, move constructing into it. |
| 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 4 move |
| 574 | // constructions here. |
| 575 | EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 || |
| 576 | Constructable::getNumMoveConstructorCalls() == 6); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 577 | // Move assign the next two to shift them up and make a gap. |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 578 | EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls()); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 579 | // Copy construct the two new elements from the parameter. |
| 580 | EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls()); |
| 581 | // All without any copy construction. |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 582 | EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls()); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 583 | EXPECT_EQ(this->theVector.begin() + 1, I); |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 584 | this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 585 | } |
Benjamin Kramer | 371b9b0 | 2012-06-17 11:52:22 +0000 | [diff] [blame] | 586 | |
Francis Ricci | e22b696 | 2017-06-09 20:31:53 +0000 | [diff] [blame] | 587 | TYPED_TEST(SmallVectorTest, InsertRepeatedNonIterTest) { |
| 588 | SCOPED_TRACE("InsertRepeatedTest"); |
| 589 | |
| 590 | this->makeSequence(this->theVector, 1, 4); |
| 591 | Constructable::reset(); |
| 592 | auto I = this->theVector.insert(this->theVector.begin() + 1, 2, 7); |
| 593 | EXPECT_EQ(this->theVector.begin() + 1, I); |
| 594 | this->assertValuesInOrder(this->theVector, 6u, 1, 7, 7, 2, 3, 4); |
| 595 | } |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 596 | |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 597 | TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) { |
| 598 | SCOPED_TRACE("InsertRepeatedTest"); |
| 599 | |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 600 | this->makeSequence(this->theVector, 1, 4); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 601 | Constructable::reset(); |
| 602 | auto I = this->theVector.insert(this->theVector.end(), 2, Constructable(16)); |
| 603 | // Just copy construct them into newly allocated space |
| 604 | EXPECT_EQ(2, Constructable::getNumCopyConstructorCalls()); |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 605 | // Move everything across if reallocation is needed. |
| 606 | EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 || |
| 607 | Constructable::getNumMoveConstructorCalls() == 4); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 608 | // Without ever moving or copying anything else. |
| 609 | EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls()); |
| 610 | EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls()); |
| 611 | |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 612 | EXPECT_EQ(this->theVector.begin() + 4, I); |
| 613 | this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) { |
| 617 | SCOPED_TRACE("InsertRepeatedTest"); |
| 618 | |
| 619 | this->makeSequence(this->theVector, 10, 15); |
Benjamin Kramer | 23a9c3e0 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 620 | |
| 621 | // Empty insert. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 622 | EXPECT_EQ(this->theVector.end(), |
| 623 | this->theVector.insert(this->theVector.end(), |
| 624 | 0, Constructable(42))); |
| 625 | EXPECT_EQ(this->theVector.begin() + 1, |
| 626 | this->theVector.insert(this->theVector.begin() + 1, |
| 627 | 0, Constructable(42))); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | // Insert range. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 631 | TYPED_TEST(SmallVectorTest, InsertRangeTest) { |
Benjamin Kramer | 23a9c3e0 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 632 | SCOPED_TRACE("InsertRangeTest"); |
| 633 | |
| 634 | Constructable Arr[3] = |
| 635 | { Constructable(77), Constructable(77), Constructable(77) }; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 636 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 637 | this->makeSequence(this->theVector, 1, 3); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 638 | Constructable::reset(); |
| 639 | auto I = this->theVector.insert(this->theVector.begin() + 1, Arr, Arr + 3); |
| 640 | // Move construct the top 3 elements into newly allocated space. |
| 641 | // Possibly move the whole sequence into new space first. |
| 642 | // FIXME: This is inefficient, we shouldn't move things into newly allocated |
| 643 | // space, then move them up/around, there should only be 2 or 3 move |
| 644 | // constructions here. |
| 645 | EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 || |
| 646 | Constructable::getNumMoveConstructorCalls() == 5); |
| 647 | // Copy assign the lower 2 new elements into existing space. |
| 648 | EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls()); |
| 649 | // Copy construct the third element into newly allocated space. |
| 650 | EXPECT_EQ(1, Constructable::getNumCopyConstructorCalls()); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 651 | EXPECT_EQ(this->theVector.begin() + 1, I); |
| 652 | this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 655 | |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 656 | TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) { |
| 657 | SCOPED_TRACE("InsertRangeTest"); |
| 658 | |
| 659 | Constructable Arr[3] = |
| 660 | { Constructable(77), Constructable(77), Constructable(77) }; |
| 661 | |
| 662 | this->makeSequence(this->theVector, 1, 3); |
Benjamin Kramer | 371b9b0 | 2012-06-17 11:52:22 +0000 | [diff] [blame] | 663 | |
Benjamin Kramer | 23a9c3e0 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 664 | // Insert at end. |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 665 | Constructable::reset(); |
| 666 | auto I = this->theVector.insert(this->theVector.end(), Arr, Arr+3); |
| 667 | // Copy construct the 3 elements into new space at the top. |
| 668 | EXPECT_EQ(3, Constructable::getNumCopyConstructorCalls()); |
| 669 | // Don't copy/move anything else. |
| 670 | EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls()); |
| 671 | // Reallocation might occur, causing all elements to be moved into the new |
| 672 | // buffer. |
| 673 | EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 || |
| 674 | Constructable::getNumMoveConstructorCalls() == 3); |
| 675 | EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls()); |
| 676 | EXPECT_EQ(this->theVector.begin() + 3, I); |
| 677 | this->assertValuesInOrder(this->theVector, 6u, |
| 678 | 1, 2, 3, 77, 77, 77); |
| 679 | } |
| 680 | |
| 681 | TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) { |
| 682 | SCOPED_TRACE("InsertRangeTest"); |
| 683 | |
| 684 | this->makeSequence(this->theVector, 1, 3); |
Benjamin Kramer | 23a9c3e0 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 685 | |
| 686 | // Empty insert. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 687 | EXPECT_EQ(this->theVector.end(), |
| 688 | this->theVector.insert(this->theVector.end(), |
| 689 | this->theVector.begin(), |
| 690 | this->theVector.begin())); |
| 691 | EXPECT_EQ(this->theVector.begin() + 1, |
| 692 | this->theVector.insert(this->theVector.begin() + 1, |
| 693 | this->theVector.begin(), |
| 694 | this->theVector.begin())); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | // Comparison tests. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 698 | TYPED_TEST(SmallVectorTest, ComparisonTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 699 | SCOPED_TRACE("ComparisonTest"); |
| 700 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 701 | this->makeSequence(this->theVector, 1, 3); |
| 702 | this->makeSequence(this->otherVector, 1, 3); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 703 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 704 | EXPECT_TRUE(this->theVector == this->otherVector); |
| 705 | EXPECT_FALSE(this->theVector != this->otherVector); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 706 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 707 | this->otherVector.clear(); |
| 708 | this->makeSequence(this->otherVector, 2, 4); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 709 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 710 | EXPECT_FALSE(this->theVector == this->otherVector); |
| 711 | EXPECT_TRUE(this->theVector != this->otherVector); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | // Constant vector tests. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 715 | TYPED_TEST(SmallVectorTest, ConstVectorTest) { |
| 716 | const TypeParam constVector; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 717 | |
| 718 | EXPECT_EQ(0u, constVector.size()); |
| 719 | EXPECT_TRUE(constVector.empty()); |
| 720 | EXPECT_TRUE(constVector.begin() == constVector.end()); |
| 721 | } |
| 722 | |
Daniel Dunbar | 825e955 | 2009-08-19 17:48:28 +0000 | [diff] [blame] | 723 | // Direct array access. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 724 | TYPED_TEST(SmallVectorTest, DirectVectorTest) { |
| 725 | EXPECT_EQ(0u, this->theVector.size()); |
| 726 | this->theVector.reserve(4); |
| 727 | EXPECT_LE(4u, this->theVector.capacity()); |
Daniel Dunbar | 825e955 | 2009-08-19 17:48:28 +0000 | [diff] [blame] | 728 | EXPECT_EQ(0, Constructable::getNumConstructorCalls()); |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 729 | this->theVector.push_back(1); |
| 730 | this->theVector.push_back(2); |
| 731 | this->theVector.push_back(3); |
| 732 | this->theVector.push_back(4); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 733 | EXPECT_EQ(4u, this->theVector.size()); |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 734 | EXPECT_EQ(8, Constructable::getNumConstructorCalls()); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 735 | EXPECT_EQ(1, this->theVector[0].getValue()); |
| 736 | EXPECT_EQ(2, this->theVector[1].getValue()); |
| 737 | EXPECT_EQ(3, this->theVector[2].getValue()); |
| 738 | EXPECT_EQ(4, this->theVector[3].getValue()); |
Daniel Dunbar | 825e955 | 2009-08-19 17:48:28 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 741 | TYPED_TEST(SmallVectorTest, IteratorTest) { |
Dan Gohman | 42e77df | 2010-03-26 18:53:37 +0000 | [diff] [blame] | 742 | std::list<int> L; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 743 | this->theVector.insert(this->theVector.end(), L.begin(), L.end()); |
Dan Gohman | 42e77df | 2010-03-26 18:53:37 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Lang Hames | ac92bfc | 2015-01-23 06:25:17 +0000 | [diff] [blame] | 746 | template <typename InvalidType> class DualSmallVectorsTest; |
| 747 | |
| 748 | template <typename VectorT1, typename VectorT2> |
| 749 | class DualSmallVectorsTest<std::pair<VectorT1, VectorT2>> : public SmallVectorTestBase { |
| 750 | protected: |
| 751 | VectorT1 theVector; |
| 752 | VectorT2 otherVector; |
| 753 | |
| 754 | template <typename T, unsigned N> |
| 755 | static unsigned NumBuiltinElts(const SmallVector<T, N>&) { return N; } |
| 756 | }; |
| 757 | |
| 758 | typedef ::testing::Types< |
| 759 | // Small mode -> Small mode. |
| 760 | std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 4>>, |
| 761 | // Small mode -> Big mode. |
| 762 | std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 2>>, |
| 763 | // Big mode -> Small mode. |
| 764 | std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 4>>, |
| 765 | // Big mode -> Big mode. |
| 766 | std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 2>> |
| 767 | > DualSmallVectorTestTypes; |
| 768 | |
| 769 | TYPED_TEST_CASE(DualSmallVectorsTest, DualSmallVectorTestTypes); |
| 770 | |
| 771 | TYPED_TEST(DualSmallVectorsTest, MoveAssignment) { |
| 772 | SCOPED_TRACE("MoveAssignTest-DualVectorTypes"); |
| 773 | |
| 774 | // Set up our vector with four elements. |
| 775 | for (unsigned I = 0; I < 4; ++I) |
| 776 | this->otherVector.push_back(Constructable(I)); |
| 777 | |
| 778 | const Constructable *OrigDataPtr = this->otherVector.data(); |
| 779 | |
| 780 | // Move-assign from the other vector. |
| 781 | this->theVector = |
| 782 | std::move(static_cast<SmallVectorImpl<Constructable>&>(this->otherVector)); |
| 783 | |
| 784 | // Make sure we have the right result. |
| 785 | this->assertValuesInOrder(this->theVector, 4u, 0, 1, 2, 3); |
| 786 | |
| 787 | // Make sure the # of constructor/destructor calls line up. There |
| 788 | // are two live objects after clearing the other vector. |
| 789 | this->otherVector.clear(); |
| 790 | EXPECT_EQ(Constructable::getNumConstructorCalls()-4, |
| 791 | Constructable::getNumDestructorCalls()); |
| 792 | |
| 793 | // If the source vector (otherVector) was in small-mode, assert that we just |
| 794 | // moved the data pointer over. |
| 795 | EXPECT_TRUE(this->NumBuiltinElts(this->otherVector) == 4 || |
| 796 | this->theVector.data() == OrigDataPtr); |
| 797 | |
| 798 | // There shouldn't be any live objects any more. |
| 799 | this->theVector.clear(); |
| 800 | EXPECT_EQ(Constructable::getNumConstructorCalls(), |
| 801 | Constructable::getNumDestructorCalls()); |
| 802 | |
| 803 | // We shouldn't have copied anything in this whole process. |
| 804 | EXPECT_EQ(Constructable::getNumCopyConstructorCalls(), 0); |
| 805 | } |
| 806 | |
Benjamin Kramer | 74a12a4 | 2012-04-29 10:53:29 +0000 | [diff] [blame] | 807 | struct notassignable { |
| 808 | int &x; |
| 809 | notassignable(int &x) : x(x) {} |
| 810 | }; |
| 811 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 812 | TEST(SmallVectorCustomTest, NoAssignTest) { |
Benjamin Kramer | 74a12a4 | 2012-04-29 10:53:29 +0000 | [diff] [blame] | 813 | int x = 0; |
| 814 | SmallVector<notassignable, 2> vec; |
| 815 | vec.push_back(notassignable(x)); |
| 816 | x = 42; |
| 817 | EXPECT_EQ(42, vec.pop_back_val().x); |
| 818 | } |
| 819 | |
David Blaikie | 789df06 | 2014-06-08 16:00:02 +0000 | [diff] [blame] | 820 | struct MovedFrom { |
| 821 | bool hasValue; |
| 822 | MovedFrom() : hasValue(true) { |
| 823 | } |
| 824 | MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) { |
| 825 | m.hasValue = false; |
| 826 | } |
| 827 | MovedFrom &operator=(MovedFrom&& m) { |
| 828 | hasValue = m.hasValue; |
| 829 | m.hasValue = false; |
| 830 | return *this; |
| 831 | } |
| 832 | }; |
| 833 | |
| 834 | TEST(SmallVectorTest, MidInsert) { |
| 835 | SmallVector<MovedFrom, 3> v; |
| 836 | v.push_back(MovedFrom()); |
| 837 | v.insert(v.begin(), MovedFrom()); |
| 838 | for (MovedFrom &m : v) |
| 839 | EXPECT_TRUE(m.hasValue); |
| 840 | } |
| 841 | |
Duncan P. N. Exon Smith | f2396e6 | 2014-12-03 04:45:09 +0000 | [diff] [blame] | 842 | enum EmplaceableArgState { |
| 843 | EAS_Defaulted, |
| 844 | EAS_Arg, |
| 845 | EAS_LValue, |
| 846 | EAS_RValue, |
| 847 | EAS_Failure |
| 848 | }; |
| 849 | template <int I> struct EmplaceableArg { |
| 850 | EmplaceableArgState State; |
| 851 | EmplaceableArg() : State(EAS_Defaulted) {} |
| 852 | EmplaceableArg(EmplaceableArg &&X) |
| 853 | : State(X.State == EAS_Arg ? EAS_RValue : EAS_Failure) {} |
| 854 | EmplaceableArg(EmplaceableArg &X) |
| 855 | : State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {} |
| 856 | |
| 857 | explicit EmplaceableArg(bool) : State(EAS_Arg) {} |
| 858 | |
| 859 | private: |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 860 | EmplaceableArg &operator=(EmplaceableArg &&) = delete; |
| 861 | EmplaceableArg &operator=(const EmplaceableArg &) = delete; |
Duncan P. N. Exon Smith | f2396e6 | 2014-12-03 04:45:09 +0000 | [diff] [blame] | 862 | }; |
| 863 | |
| 864 | enum EmplaceableState { ES_Emplaced, ES_Moved }; |
| 865 | struct Emplaceable { |
| 866 | EmplaceableArg<0> A0; |
| 867 | EmplaceableArg<1> A1; |
| 868 | EmplaceableArg<2> A2; |
| 869 | EmplaceableArg<3> A3; |
| 870 | EmplaceableState State; |
| 871 | |
| 872 | Emplaceable() : State(ES_Emplaced) {} |
| 873 | |
| 874 | template <class A0Ty> |
| 875 | explicit Emplaceable(A0Ty &&A0) |
| 876 | : A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {} |
| 877 | |
| 878 | template <class A0Ty, class A1Ty> |
| 879 | Emplaceable(A0Ty &&A0, A1Ty &&A1) |
| 880 | : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)), |
| 881 | State(ES_Emplaced) {} |
| 882 | |
| 883 | template <class A0Ty, class A1Ty, class A2Ty> |
| 884 | Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2) |
| 885 | : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)), |
| 886 | A2(std::forward<A2Ty>(A2)), State(ES_Emplaced) {} |
| 887 | |
| 888 | template <class A0Ty, class A1Ty, class A2Ty, class A3Ty> |
| 889 | Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2, A3Ty &&A3) |
| 890 | : A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)), |
| 891 | A2(std::forward<A2Ty>(A2)), A3(std::forward<A3Ty>(A3)), |
| 892 | State(ES_Emplaced) {} |
| 893 | |
| 894 | Emplaceable(Emplaceable &&) : State(ES_Moved) {} |
| 895 | Emplaceable &operator=(Emplaceable &&) { |
| 896 | State = ES_Moved; |
| 897 | return *this; |
| 898 | } |
| 899 | |
| 900 | private: |
Aaron Ballman | f9a1897 | 2015-02-15 22:54:22 +0000 | [diff] [blame] | 901 | Emplaceable(const Emplaceable &) = delete; |
| 902 | Emplaceable &operator=(const Emplaceable &) = delete; |
Duncan P. N. Exon Smith | f2396e6 | 2014-12-03 04:45:09 +0000 | [diff] [blame] | 903 | }; |
| 904 | |
| 905 | TEST(SmallVectorTest, EmplaceBack) { |
| 906 | EmplaceableArg<0> A0(true); |
| 907 | EmplaceableArg<1> A1(true); |
| 908 | EmplaceableArg<2> A2(true); |
| 909 | EmplaceableArg<3> A3(true); |
| 910 | { |
| 911 | SmallVector<Emplaceable, 3> V; |
| 912 | V.emplace_back(); |
| 913 | EXPECT_TRUE(V.size() == 1); |
| 914 | EXPECT_TRUE(V.back().State == ES_Emplaced); |
| 915 | EXPECT_TRUE(V.back().A0.State == EAS_Defaulted); |
| 916 | EXPECT_TRUE(V.back().A1.State == EAS_Defaulted); |
| 917 | EXPECT_TRUE(V.back().A2.State == EAS_Defaulted); |
| 918 | EXPECT_TRUE(V.back().A3.State == EAS_Defaulted); |
| 919 | } |
| 920 | { |
| 921 | SmallVector<Emplaceable, 3> V; |
| 922 | V.emplace_back(std::move(A0)); |
| 923 | EXPECT_TRUE(V.size() == 1); |
| 924 | EXPECT_TRUE(V.back().State == ES_Emplaced); |
| 925 | EXPECT_TRUE(V.back().A0.State == EAS_RValue); |
| 926 | EXPECT_TRUE(V.back().A1.State == EAS_Defaulted); |
| 927 | EXPECT_TRUE(V.back().A2.State == EAS_Defaulted); |
| 928 | EXPECT_TRUE(V.back().A3.State == EAS_Defaulted); |
| 929 | } |
| 930 | { |
| 931 | SmallVector<Emplaceable, 3> V; |
| 932 | V.emplace_back(A0); |
| 933 | EXPECT_TRUE(V.size() == 1); |
| 934 | EXPECT_TRUE(V.back().State == ES_Emplaced); |
| 935 | EXPECT_TRUE(V.back().A0.State == EAS_LValue); |
| 936 | EXPECT_TRUE(V.back().A1.State == EAS_Defaulted); |
| 937 | EXPECT_TRUE(V.back().A2.State == EAS_Defaulted); |
| 938 | EXPECT_TRUE(V.back().A3.State == EAS_Defaulted); |
| 939 | } |
| 940 | { |
| 941 | SmallVector<Emplaceable, 3> V; |
| 942 | V.emplace_back(A0, A1); |
| 943 | EXPECT_TRUE(V.size() == 1); |
| 944 | EXPECT_TRUE(V.back().State == ES_Emplaced); |
| 945 | EXPECT_TRUE(V.back().A0.State == EAS_LValue); |
| 946 | EXPECT_TRUE(V.back().A1.State == EAS_LValue); |
| 947 | EXPECT_TRUE(V.back().A2.State == EAS_Defaulted); |
| 948 | EXPECT_TRUE(V.back().A3.State == EAS_Defaulted); |
| 949 | } |
| 950 | { |
| 951 | SmallVector<Emplaceable, 3> V; |
| 952 | V.emplace_back(std::move(A0), std::move(A1)); |
| 953 | EXPECT_TRUE(V.size() == 1); |
| 954 | EXPECT_TRUE(V.back().State == ES_Emplaced); |
| 955 | EXPECT_TRUE(V.back().A0.State == EAS_RValue); |
| 956 | EXPECT_TRUE(V.back().A1.State == EAS_RValue); |
| 957 | EXPECT_TRUE(V.back().A2.State == EAS_Defaulted); |
| 958 | EXPECT_TRUE(V.back().A3.State == EAS_Defaulted); |
| 959 | } |
| 960 | { |
| 961 | SmallVector<Emplaceable, 3> V; |
| 962 | V.emplace_back(std::move(A0), A1, std::move(A2), A3); |
| 963 | EXPECT_TRUE(V.size() == 1); |
| 964 | EXPECT_TRUE(V.back().State == ES_Emplaced); |
| 965 | EXPECT_TRUE(V.back().A0.State == EAS_RValue); |
| 966 | EXPECT_TRUE(V.back().A1.State == EAS_LValue); |
| 967 | EXPECT_TRUE(V.back().A2.State == EAS_RValue); |
| 968 | EXPECT_TRUE(V.back().A3.State == EAS_LValue); |
| 969 | } |
Benjamin Kramer | 1d563f4 | 2015-02-07 16:41:02 +0000 | [diff] [blame] | 970 | { |
| 971 | SmallVector<int, 1> V; |
| 972 | V.emplace_back(); |
| 973 | V.emplace_back(42); |
| 974 | EXPECT_EQ(2U, V.size()); |
| 975 | EXPECT_EQ(0, V[0]); |
| 976 | EXPECT_EQ(42, V[1]); |
| 977 | } |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 978 | } |
Duncan P. N. Exon Smith | f2396e6 | 2014-12-03 04:45:09 +0000 | [diff] [blame] | 979 | |
Benjamin Kramer | a6d7acc | 2015-03-02 21:16:04 +0000 | [diff] [blame] | 980 | TEST(SmallVectorTest, InitializerList) { |
| 981 | SmallVector<int, 2> V1 = {}; |
| 982 | EXPECT_TRUE(V1.empty()); |
| 983 | V1 = {0, 0}; |
| 984 | EXPECT_TRUE(makeArrayRef(V1).equals({0, 0})); |
| 985 | V1 = {-1, -1}; |
| 986 | EXPECT_TRUE(makeArrayRef(V1).equals({-1, -1})); |
| 987 | |
| 988 | SmallVector<int, 2> V2 = {1, 2, 3, 4}; |
| 989 | EXPECT_TRUE(makeArrayRef(V2).equals({1, 2, 3, 4})); |
| 990 | V2.assign({4}); |
| 991 | EXPECT_TRUE(makeArrayRef(V2).equals({4})); |
| 992 | V2.append({3, 2}); |
| 993 | EXPECT_TRUE(makeArrayRef(V2).equals({4, 3, 2})); |
| 994 | V2.insert(V2.begin() + 1, 5); |
| 995 | EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2})); |
| 996 | } |
| 997 | |
Duncan P. N. Exon Smith | 91d3cfe | 2016-04-05 20:45:04 +0000 | [diff] [blame] | 998 | } // end namespace |