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