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; } |
| 146 | NonCopyable(const NonCopyable &) = delete; |
| 147 | NonCopyable &operator=(const NonCopyable &) = delete; |
| 148 | }; |
| 149 | |
| 150 | LLVM_ATTRIBUTE_USED void CompileTest() { |
| 151 | SmallVector<NonCopyable, 0> V; |
| 152 | V.resize(42); |
| 153 | } |
| 154 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 155 | // Test fixture class |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 156 | template <typename VectorT> |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 157 | class SmallVectorTest : public testing::Test { |
| 158 | protected: |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 159 | VectorT theVector; |
| 160 | VectorT otherVector; |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 161 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 162 | void SetUp() { |
| 163 | Constructable::reset(); |
| 164 | } |
| 165 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 166 | void assertEmpty(VectorT & v) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 167 | // Size tests |
| 168 | EXPECT_EQ(0u, v.size()); |
| 169 | EXPECT_TRUE(v.empty()); |
| 170 | |
| 171 | // Iterator tests |
| 172 | EXPECT_TRUE(v.begin() == v.end()); |
| 173 | } |
| 174 | |
| 175 | // Assert that theVector contains the specified values, in order. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 176 | void assertValuesInOrder(VectorT & v, size_t size, ...) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 177 | EXPECT_EQ(size, v.size()); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 178 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 179 | va_list ap; |
| 180 | va_start(ap, size); |
| 181 | for (size_t i = 0; i < size; ++i) { |
| 182 | int value = va_arg(ap, int); |
| 183 | EXPECT_EQ(value, v[i].getValue()); |
| 184 | } |
| 185 | |
| 186 | va_end(ap); |
| 187 | } |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 188 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 189 | // Generate a sequence of values to initialize the vector. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 190 | void makeSequence(VectorT & v, int start, int end) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 191 | for (int i = start; i <= end; ++i) { |
| 192 | v.push_back(Constructable(i)); |
| 193 | } |
| 194 | } |
| 195 | }; |
| 196 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 197 | typedef ::testing::Types<SmallVector<Constructable, 0>, |
| 198 | SmallVector<Constructable, 1>, |
| 199 | SmallVector<Constructable, 2>, |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 200 | SmallVector<Constructable, 4>, |
| 201 | SmallVector<Constructable, 5> |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 202 | > SmallVectorTestTypes; |
| 203 | TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes); |
| 204 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 205 | // New vector test. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 206 | TYPED_TEST(SmallVectorTest, EmptyVectorTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 207 | SCOPED_TRACE("EmptyVectorTest"); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 208 | this->assertEmpty(this->theVector); |
| 209 | EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 210 | EXPECT_EQ(0, Constructable::getNumConstructorCalls()); |
| 211 | EXPECT_EQ(0, Constructable::getNumDestructorCalls()); |
| 212 | } |
| 213 | |
| 214 | // Simple insertions and deletions. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 215 | TYPED_TEST(SmallVectorTest, PushPopTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 216 | SCOPED_TRACE("PushPopTest"); |
| 217 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 218 | // Track whether the vector will potentially have to grow. |
| 219 | bool RequiresGrowth = this->theVector.capacity() < 3; |
| 220 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 221 | // Push an element |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 222 | this->theVector.push_back(Constructable(1)); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 223 | |
| 224 | // Size tests |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 225 | this->assertValuesInOrder(this->theVector, 1u, 1); |
| 226 | EXPECT_FALSE(this->theVector.begin() == this->theVector.end()); |
| 227 | EXPECT_FALSE(this->theVector.empty()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 228 | |
| 229 | // Push another element |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 230 | this->theVector.push_back(Constructable(2)); |
| 231 | this->assertValuesInOrder(this->theVector, 2u, 1, 2); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 232 | |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 233 | // Insert at beginning |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 234 | this->theVector.insert(this->theVector.begin(), this->theVector[1]); |
| 235 | this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 236 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 237 | // Pop one element |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 238 | this->theVector.pop_back(); |
| 239 | this->assertValuesInOrder(this->theVector, 2u, 2, 1); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 240 | |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 241 | // Pop remaining elements |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 242 | this->theVector.pop_back(); |
| 243 | this->theVector.pop_back(); |
| 244 | this->assertEmpty(this->theVector); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 245 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 246 | // Check number of constructor calls. Should be 2 for each list element, |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 247 | // one for the argument to push_back, one for the argument to insert, |
| 248 | // and one for the list element itself. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 249 | if (!RequiresGrowth) { |
| 250 | EXPECT_EQ(5, Constructable::getNumConstructorCalls()); |
| 251 | EXPECT_EQ(5, Constructable::getNumDestructorCalls()); |
| 252 | } else { |
| 253 | // If we had to grow the vector, these only have a lower bound, but should |
| 254 | // always be equal. |
| 255 | EXPECT_LE(5, Constructable::getNumConstructorCalls()); |
| 256 | EXPECT_EQ(Constructable::getNumConstructorCalls(), |
| 257 | Constructable::getNumDestructorCalls()); |
| 258 | } |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Clear test. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 262 | TYPED_TEST(SmallVectorTest, ClearTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 263 | SCOPED_TRACE("ClearTest"); |
| 264 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 265 | this->theVector.reserve(2); |
| 266 | this->makeSequence(this->theVector, 1, 2); |
| 267 | this->theVector.clear(); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 268 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 269 | this->assertEmpty(this->theVector); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 270 | EXPECT_EQ(4, Constructable::getNumConstructorCalls()); |
| 271 | EXPECT_EQ(4, Constructable::getNumDestructorCalls()); |
| 272 | } |
| 273 | |
| 274 | // Resize smaller test. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 275 | TYPED_TEST(SmallVectorTest, ResizeShrinkTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 276 | SCOPED_TRACE("ResizeShrinkTest"); |
| 277 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 278 | this->theVector.reserve(3); |
| 279 | this->makeSequence(this->theVector, 1, 3); |
| 280 | this->theVector.resize(1); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 281 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 282 | this->assertValuesInOrder(this->theVector, 1u, 1); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 283 | EXPECT_EQ(6, Constructable::getNumConstructorCalls()); |
| 284 | EXPECT_EQ(5, Constructable::getNumDestructorCalls()); |
| 285 | } |
| 286 | |
| 287 | // Resize bigger test. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 288 | TYPED_TEST(SmallVectorTest, ResizeGrowTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 289 | SCOPED_TRACE("ResizeGrowTest"); |
| 290 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 291 | this->theVector.resize(2); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 292 | |
David Blaikie | 669fc86 | 2014-06-09 22:26:20 +0000 | [diff] [blame^] | 293 | EXPECT_EQ(2, Constructable::getNumConstructorCalls()); |
| 294 | EXPECT_EQ(0, Constructable::getNumDestructorCalls()); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 295 | EXPECT_EQ(2u, this->theVector.size()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 296 | } |
| 297 | |
David Blaikie | 669fc86 | 2014-06-09 22:26:20 +0000 | [diff] [blame^] | 298 | TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) { |
| 299 | this->theVector.resize(2); |
| 300 | |
| 301 | Constructable::reset(); |
| 302 | |
| 303 | this->theVector.resize(4); |
| 304 | |
| 305 | size_t Ctors = Constructable::getNumConstructorCalls(); |
| 306 | EXPECT_TRUE(Ctors == 2 || Ctors == 4); |
| 307 | size_t MoveCtors = Constructable::getNumMoveConstructorCalls(); |
| 308 | EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2); |
| 309 | size_t Dtors = Constructable::getNumDestructorCalls(); |
| 310 | EXPECT_TRUE(Dtors == 0 || Dtors == 2); |
| 311 | } |
| 312 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 313 | // Resize with fill value. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 314 | TYPED_TEST(SmallVectorTest, ResizeFillTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 315 | SCOPED_TRACE("ResizeFillTest"); |
| 316 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 317 | this->theVector.resize(3, Constructable(77)); |
| 318 | this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // Overflow past fixed size. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 322 | TYPED_TEST(SmallVectorTest, OverflowTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 323 | SCOPED_TRACE("OverflowTest"); |
| 324 | |
Daniel Dunbar | 6d6023b | 2009-07-12 19:45:34 +0000 | [diff] [blame] | 325 | // Push more elements than the fixed size. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 326 | this->makeSequence(this->theVector, 1, 10); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 327 | |
Daniel Dunbar | 6d6023b | 2009-07-12 19:45:34 +0000 | [diff] [blame] | 328 | // Test size and values. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 329 | EXPECT_EQ(10u, this->theVector.size()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 330 | for (int i = 0; i < 10; ++i) { |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 331 | EXPECT_EQ(i+1, this->theVector[i].getValue()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 332 | } |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 333 | |
Daniel Dunbar | 6d6023b | 2009-07-12 19:45:34 +0000 | [diff] [blame] | 334 | // Now resize back to fixed size. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 335 | this->theVector.resize(1); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 336 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 337 | this->assertValuesInOrder(this->theVector, 1u, 1); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // Iteration tests. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 341 | TYPED_TEST(SmallVectorTest, IterationTest) { |
| 342 | this->makeSequence(this->theVector, 1, 2); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 343 | |
| 344 | // Forward Iteration |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 345 | typename TypeParam::iterator it = this->theVector.begin(); |
| 346 | EXPECT_TRUE(*it == this->theVector.front()); |
| 347 | EXPECT_TRUE(*it == this->theVector[0]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 348 | EXPECT_EQ(1, it->getValue()); |
| 349 | ++it; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 350 | EXPECT_TRUE(*it == this->theVector[1]); |
| 351 | EXPECT_TRUE(*it == this->theVector.back()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 352 | EXPECT_EQ(2, it->getValue()); |
| 353 | ++it; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 354 | EXPECT_TRUE(it == this->theVector.end()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 355 | --it; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 356 | EXPECT_TRUE(*it == this->theVector[1]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 357 | EXPECT_EQ(2, it->getValue()); |
| 358 | --it; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 359 | EXPECT_TRUE(*it == this->theVector[0]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 360 | EXPECT_EQ(1, it->getValue()); |
| 361 | |
| 362 | // Reverse Iteration |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 363 | typename TypeParam::reverse_iterator rit = this->theVector.rbegin(); |
| 364 | EXPECT_TRUE(*rit == this->theVector[1]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 365 | EXPECT_EQ(2, rit->getValue()); |
| 366 | ++rit; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 367 | EXPECT_TRUE(*rit == this->theVector[0]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 368 | EXPECT_EQ(1, rit->getValue()); |
| 369 | ++rit; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 370 | EXPECT_TRUE(rit == this->theVector.rend()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 371 | --rit; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 372 | EXPECT_TRUE(*rit == this->theVector[0]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 373 | EXPECT_EQ(1, rit->getValue()); |
| 374 | --rit; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 375 | EXPECT_TRUE(*rit == this->theVector[1]); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 376 | EXPECT_EQ(2, rit->getValue()); |
| 377 | } |
| 378 | |
| 379 | // Swap test. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 380 | TYPED_TEST(SmallVectorTest, SwapTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 381 | SCOPED_TRACE("SwapTest"); |
| 382 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 383 | this->makeSequence(this->theVector, 1, 2); |
| 384 | std::swap(this->theVector, this->otherVector); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 385 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 386 | this->assertEmpty(this->theVector); |
| 387 | this->assertValuesInOrder(this->otherVector, 2u, 1, 2); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Append test |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 391 | TYPED_TEST(SmallVectorTest, AppendTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 392 | SCOPED_TRACE("AppendTest"); |
| 393 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 394 | this->makeSequence(this->otherVector, 2, 3); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 395 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 396 | this->theVector.push_back(Constructable(1)); |
| 397 | this->theVector.append(this->otherVector.begin(), this->otherVector.end()); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 398 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 399 | this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // Append repeated test |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 403 | TYPED_TEST(SmallVectorTest, AppendRepeatedTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 404 | SCOPED_TRACE("AppendRepeatedTest"); |
| 405 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 406 | this->theVector.push_back(Constructable(1)); |
| 407 | this->theVector.append(2, Constructable(77)); |
| 408 | this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // Assign test |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 412 | TYPED_TEST(SmallVectorTest, AssignTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 413 | SCOPED_TRACE("AssignTest"); |
| 414 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 415 | this->theVector.push_back(Constructable(1)); |
| 416 | this->theVector.assign(2, Constructable(77)); |
| 417 | this->assertValuesInOrder(this->theVector, 2u, 77, 77); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 420 | // Move-assign test |
| 421 | TYPED_TEST(SmallVectorTest, MoveAssignTest) { |
| 422 | SCOPED_TRACE("MoveAssignTest"); |
| 423 | |
| 424 | // Set up our vector with a single element, but enough capacity for 4. |
| 425 | this->theVector.reserve(4); |
| 426 | this->theVector.push_back(Constructable(1)); |
| 427 | |
| 428 | // Set up the other vector with 2 elements. |
| 429 | this->otherVector.push_back(Constructable(2)); |
| 430 | this->otherVector.push_back(Constructable(3)); |
| 431 | |
| 432 | // Move-assign from the other vector. |
| 433 | this->theVector = std::move(this->otherVector); |
| 434 | |
| 435 | // Make sure we have the right result. |
| 436 | this->assertValuesInOrder(this->theVector, 2u, 2, 3); |
| 437 | |
| 438 | // Make sure the # of constructor/destructor calls line up. There |
| 439 | // are two live objects after clearing the other vector. |
| 440 | this->otherVector.clear(); |
| 441 | EXPECT_EQ(Constructable::getNumConstructorCalls()-2, |
| 442 | Constructable::getNumDestructorCalls()); |
| 443 | |
| 444 | // There shouldn't be any live objects any more. |
| 445 | this->theVector.clear(); |
| 446 | EXPECT_EQ(Constructable::getNumConstructorCalls(), |
| 447 | Constructable::getNumDestructorCalls()); |
| 448 | } |
| 449 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 450 | // Erase a single element |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 451 | TYPED_TEST(SmallVectorTest, EraseTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 452 | SCOPED_TRACE("EraseTest"); |
| 453 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 454 | this->makeSequence(this->theVector, 1, 3); |
| 455 | this->theVector.erase(this->theVector.begin()); |
| 456 | this->assertValuesInOrder(this->theVector, 2u, 2, 3); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | // Erase a range of elements |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 460 | TYPED_TEST(SmallVectorTest, EraseRangeTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 461 | SCOPED_TRACE("EraseRangeTest"); |
| 462 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 463 | this->makeSequence(this->theVector, 1, 3); |
| 464 | this->theVector.erase(this->theVector.begin(), this->theVector.begin() + 2); |
| 465 | this->assertValuesInOrder(this->theVector, 1u, 3); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | // Insert a single element. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 469 | TYPED_TEST(SmallVectorTest, InsertTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 470 | SCOPED_TRACE("InsertTest"); |
| 471 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 472 | this->makeSequence(this->theVector, 1, 3); |
| 473 | typename TypeParam::iterator I = |
| 474 | this->theVector.insert(this->theVector.begin() + 1, Constructable(77)); |
| 475 | EXPECT_EQ(this->theVector.begin() + 1, I); |
| 476 | this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 477 | } |
| 478 | |
David Blaikie | 40d4e34 | 2014-06-08 16:55:13 +0000 | [diff] [blame] | 479 | // Insert a copy of a single element. |
| 480 | TYPED_TEST(SmallVectorTest, InsertCopy) { |
| 481 | SCOPED_TRACE("InsertTest"); |
| 482 | |
| 483 | this->makeSequence(this->theVector, 1, 3); |
| 484 | Constructable C(77); |
| 485 | typename TypeParam::iterator I = |
| 486 | this->theVector.insert(this->theVector.begin() + 1, C); |
| 487 | EXPECT_EQ(this->theVector.begin() + 1, I); |
| 488 | this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3); |
| 489 | } |
| 490 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 491 | // Insert repeated elements. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 492 | TYPED_TEST(SmallVectorTest, InsertRepeatedTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 493 | SCOPED_TRACE("InsertRepeatedTest"); |
| 494 | |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 495 | this->makeSequence(this->theVector, 1, 4); |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 496 | Constructable::reset(); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 497 | auto I = |
| 498 | this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16)); |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 499 | // Move construct the top element into newly allocated space, and optionally |
| 500 | // reallocate the whole buffer, move constructing into it. |
| 501 | // FIXME: This is inefficient, we shouldn't move things into newly allocated |
| 502 | // space, then move them up/around, there should only be 2 or 4 move |
| 503 | // constructions here. |
| 504 | EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 || |
| 505 | Constructable::getNumMoveConstructorCalls() == 6); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 506 | // 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] | 507 | EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls()); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 508 | // Copy construct the two new elements from the parameter. |
| 509 | EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls()); |
| 510 | // All without any copy construction. |
David Blaikie | ae8a932 | 2014-06-08 19:12:28 +0000 | [diff] [blame] | 511 | EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls()); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 512 | EXPECT_EQ(this->theVector.begin() + 1, I); |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 513 | this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 514 | } |
Benjamin Kramer | 371b9b0 | 2012-06-17 11:52:22 +0000 | [diff] [blame] | 515 | |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 516 | |
| 517 | TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) { |
| 518 | SCOPED_TRACE("InsertRepeatedTest"); |
| 519 | |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 520 | this->makeSequence(this->theVector, 1, 4); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 521 | Constructable::reset(); |
| 522 | auto I = this->theVector.insert(this->theVector.end(), 2, Constructable(16)); |
| 523 | // Just copy construct them into newly allocated space |
| 524 | EXPECT_EQ(2, Constructable::getNumCopyConstructorCalls()); |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 525 | // Move everything across if reallocation is needed. |
| 526 | EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 || |
| 527 | Constructable::getNumMoveConstructorCalls() == 4); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 528 | // Without ever moving or copying anything else. |
| 529 | EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls()); |
| 530 | EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls()); |
| 531 | |
David Blaikie | 1848660 | 2014-06-08 19:33:40 +0000 | [diff] [blame] | 532 | EXPECT_EQ(this->theVector.begin() + 4, I); |
| 533 | this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) { |
| 537 | SCOPED_TRACE("InsertRepeatedTest"); |
| 538 | |
| 539 | this->makeSequence(this->theVector, 10, 15); |
Benjamin Kramer | 23a9c3e0 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 540 | |
| 541 | // Empty insert. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 542 | EXPECT_EQ(this->theVector.end(), |
| 543 | this->theVector.insert(this->theVector.end(), |
| 544 | 0, Constructable(42))); |
| 545 | EXPECT_EQ(this->theVector.begin() + 1, |
| 546 | this->theVector.insert(this->theVector.begin() + 1, |
| 547 | 0, Constructable(42))); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | // Insert range. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 551 | TYPED_TEST(SmallVectorTest, InsertRangeTest) { |
Benjamin Kramer | 23a9c3e0 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 552 | SCOPED_TRACE("InsertRangeTest"); |
| 553 | |
| 554 | Constructable Arr[3] = |
| 555 | { Constructable(77), Constructable(77), Constructable(77) }; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 556 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 557 | this->makeSequence(this->theVector, 1, 3); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 558 | Constructable::reset(); |
| 559 | auto I = this->theVector.insert(this->theVector.begin() + 1, Arr, Arr + 3); |
| 560 | // Move construct the top 3 elements into newly allocated space. |
| 561 | // Possibly move the whole sequence into new space first. |
| 562 | // FIXME: This is inefficient, we shouldn't move things into newly allocated |
| 563 | // space, then move them up/around, there should only be 2 or 3 move |
| 564 | // constructions here. |
| 565 | EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 || |
| 566 | Constructable::getNumMoveConstructorCalls() == 5); |
| 567 | // Copy assign the lower 2 new elements into existing space. |
| 568 | EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls()); |
| 569 | // Copy construct the third element into newly allocated space. |
| 570 | EXPECT_EQ(1, Constructable::getNumCopyConstructorCalls()); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 571 | EXPECT_EQ(this->theVector.begin() + 1, I); |
| 572 | this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3); |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | |
| 576 | TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) { |
| 577 | SCOPED_TRACE("InsertRangeTest"); |
| 578 | |
| 579 | Constructable Arr[3] = |
| 580 | { Constructable(77), Constructable(77), Constructable(77) }; |
| 581 | |
| 582 | this->makeSequence(this->theVector, 1, 3); |
Benjamin Kramer | 371b9b0 | 2012-06-17 11:52:22 +0000 | [diff] [blame] | 583 | |
Benjamin Kramer | 23a9c3e0 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 584 | // Insert at end. |
David Blaikie | 402cb2c | 2014-06-08 19:12:31 +0000 | [diff] [blame] | 585 | Constructable::reset(); |
| 586 | auto I = this->theVector.insert(this->theVector.end(), Arr, Arr+3); |
| 587 | // Copy construct the 3 elements into new space at the top. |
| 588 | EXPECT_EQ(3, Constructable::getNumCopyConstructorCalls()); |
| 589 | // Don't copy/move anything else. |
| 590 | EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls()); |
| 591 | // Reallocation might occur, causing all elements to be moved into the new |
| 592 | // buffer. |
| 593 | EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 || |
| 594 | Constructable::getNumMoveConstructorCalls() == 3); |
| 595 | EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls()); |
| 596 | EXPECT_EQ(this->theVector.begin() + 3, I); |
| 597 | this->assertValuesInOrder(this->theVector, 6u, |
| 598 | 1, 2, 3, 77, 77, 77); |
| 599 | } |
| 600 | |
| 601 | TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) { |
| 602 | SCOPED_TRACE("InsertRangeTest"); |
| 603 | |
| 604 | this->makeSequence(this->theVector, 1, 3); |
Benjamin Kramer | 23a9c3e0 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 605 | |
| 606 | // Empty insert. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 607 | EXPECT_EQ(this->theVector.end(), |
| 608 | this->theVector.insert(this->theVector.end(), |
| 609 | this->theVector.begin(), |
| 610 | this->theVector.begin())); |
| 611 | EXPECT_EQ(this->theVector.begin() + 1, |
| 612 | this->theVector.insert(this->theVector.begin() + 1, |
| 613 | this->theVector.begin(), |
| 614 | this->theVector.begin())); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // Comparison tests. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 618 | TYPED_TEST(SmallVectorTest, ComparisonTest) { |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 619 | SCOPED_TRACE("ComparisonTest"); |
| 620 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 621 | this->makeSequence(this->theVector, 1, 3); |
| 622 | this->makeSequence(this->otherVector, 1, 3); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 623 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 624 | EXPECT_TRUE(this->theVector == this->otherVector); |
| 625 | EXPECT_FALSE(this->theVector != this->otherVector); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 626 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 627 | this->otherVector.clear(); |
| 628 | this->makeSequence(this->otherVector, 2, 4); |
Owen Anderson | 145a260 | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 629 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 630 | EXPECT_FALSE(this->theVector == this->otherVector); |
| 631 | EXPECT_TRUE(this->theVector != this->otherVector); |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // Constant vector tests. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 635 | TYPED_TEST(SmallVectorTest, ConstVectorTest) { |
| 636 | const TypeParam constVector; |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 637 | |
| 638 | EXPECT_EQ(0u, constVector.size()); |
| 639 | EXPECT_TRUE(constVector.empty()); |
| 640 | EXPECT_TRUE(constVector.begin() == constVector.end()); |
| 641 | } |
| 642 | |
Daniel Dunbar | 825e955 | 2009-08-19 17:48:28 +0000 | [diff] [blame] | 643 | // Direct array access. |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 644 | TYPED_TEST(SmallVectorTest, DirectVectorTest) { |
| 645 | EXPECT_EQ(0u, this->theVector.size()); |
| 646 | this->theVector.reserve(4); |
| 647 | EXPECT_LE(4u, this->theVector.capacity()); |
Daniel Dunbar | 825e955 | 2009-08-19 17:48:28 +0000 | [diff] [blame] | 648 | EXPECT_EQ(0, Constructable::getNumConstructorCalls()); |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 649 | this->theVector.push_back(1); |
| 650 | this->theVector.push_back(2); |
| 651 | this->theVector.push_back(3); |
| 652 | this->theVector.push_back(4); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 653 | EXPECT_EQ(4u, this->theVector.size()); |
Douglas Gregor | 8451cdff | 2014-04-30 15:49:06 +0000 | [diff] [blame] | 654 | EXPECT_EQ(8, Constructable::getNumConstructorCalls()); |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 655 | EXPECT_EQ(1, this->theVector[0].getValue()); |
| 656 | EXPECT_EQ(2, this->theVector[1].getValue()); |
| 657 | EXPECT_EQ(3, this->theVector[2].getValue()); |
| 658 | EXPECT_EQ(4, this->theVector[3].getValue()); |
Daniel Dunbar | 825e955 | 2009-08-19 17:48:28 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 661 | TYPED_TEST(SmallVectorTest, IteratorTest) { |
Dan Gohman | 42e77df | 2010-03-26 18:53:37 +0000 | [diff] [blame] | 662 | std::list<int> L; |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 663 | this->theVector.insert(this->theVector.end(), L.begin(), L.end()); |
Dan Gohman | 42e77df | 2010-03-26 18:53:37 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Benjamin Kramer | 74a12a4 | 2012-04-29 10:53:29 +0000 | [diff] [blame] | 666 | struct notassignable { |
| 667 | int &x; |
| 668 | notassignable(int &x) : x(x) {} |
| 669 | }; |
| 670 | |
Chandler Carruth | 0b01261 | 2012-07-30 22:17:52 +0000 | [diff] [blame] | 671 | TEST(SmallVectorCustomTest, NoAssignTest) { |
Benjamin Kramer | 74a12a4 | 2012-04-29 10:53:29 +0000 | [diff] [blame] | 672 | int x = 0; |
| 673 | SmallVector<notassignable, 2> vec; |
| 674 | vec.push_back(notassignable(x)); |
| 675 | x = 42; |
| 676 | EXPECT_EQ(42, vec.pop_back_val().x); |
| 677 | } |
| 678 | |
David Blaikie | 789df06 | 2014-06-08 16:00:02 +0000 | [diff] [blame] | 679 | struct MovedFrom { |
| 680 | bool hasValue; |
| 681 | MovedFrom() : hasValue(true) { |
| 682 | } |
| 683 | MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) { |
| 684 | m.hasValue = false; |
| 685 | } |
| 686 | MovedFrom &operator=(MovedFrom&& m) { |
| 687 | hasValue = m.hasValue; |
| 688 | m.hasValue = false; |
| 689 | return *this; |
| 690 | } |
| 691 | }; |
| 692 | |
| 693 | TEST(SmallVectorTest, MidInsert) { |
| 694 | SmallVector<MovedFrom, 3> v; |
| 695 | v.push_back(MovedFrom()); |
| 696 | v.insert(v.begin(), MovedFrom()); |
| 697 | for (MovedFrom &m : v) |
| 698 | EXPECT_TRUE(m.hasValue); |
| 699 | } |
| 700 | |
Bill Wendling | c56c37f | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 701 | } |