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