Bill Wendling | 3d9fbee | 2009-01-11 01:25:51 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/ADT/SmallVectorTest.cpp ------------------------------===// |
Bill Wendling | f2850d9 | 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 | |
| 14 | #include "gtest/gtest.h" |
| 15 | #include "llvm/ADT/SmallVector.h" |
Bill Wendling | d400850 | 2010-08-19 18:52:02 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compiler.h" |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 17 | #include <stdarg.h> |
Dan Gohman | a7a33fd | 2010-03-26 18:53:37 +0000 | [diff] [blame] | 18 | #include <list> |
Bill Wendling | f2850d9 | 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; |
| 29 | static int numDestructorCalls; |
| 30 | static int numAssignmentCalls; |
| 31 | |
| 32 | int value; |
| 33 | |
| 34 | public: |
| 35 | Constructable() : value(0) { |
| 36 | ++numConstructorCalls; |
| 37 | } |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 38 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 39 | Constructable(int val) : value(val) { |
| 40 | ++numConstructorCalls; |
| 41 | } |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 42 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 43 | Constructable(const Constructable & src) { |
| 44 | value = src.value; |
| 45 | ++numConstructorCalls; |
| 46 | } |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 47 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 48 | ~Constructable() { |
| 49 | ++numDestructorCalls; |
| 50 | } |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 51 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 52 | Constructable & operator=(const Constructable & src) { |
| 53 | value = src.value; |
| 54 | ++numAssignmentCalls; |
| 55 | return *this; |
| 56 | } |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 57 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 58 | int getValue() const { |
| 59 | return abs(value); |
| 60 | } |
| 61 | |
| 62 | static void reset() { |
| 63 | numConstructorCalls = 0; |
| 64 | numDestructorCalls = 0; |
| 65 | numAssignmentCalls = 0; |
| 66 | } |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 67 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 68 | static int getNumConstructorCalls() { |
| 69 | return numConstructorCalls; |
| 70 | } |
| 71 | |
| 72 | static int getNumDestructorCalls() { |
| 73 | return numDestructorCalls; |
| 74 | } |
| 75 | |
| 76 | friend bool operator==(const Constructable & c0, const Constructable & c1) { |
| 77 | return c0.getValue() == c1.getValue(); |
| 78 | } |
| 79 | |
Chandler Carruth | 100c267 | 2010-10-23 08:10:43 +0000 | [diff] [blame] | 80 | friend bool LLVM_ATTRIBUTE_UNUSED |
Bill Wendling | d400850 | 2010-08-19 18:52:02 +0000 | [diff] [blame] | 81 | operator!=(const Constructable & c0, const Constructable & c1) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 82 | return c0.getValue() != c1.getValue(); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | int Constructable::numConstructorCalls; |
| 87 | int Constructable::numDestructorCalls; |
| 88 | int Constructable::numAssignmentCalls; |
| 89 | |
| 90 | // Test fixture class |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 91 | template <typename VectorT> |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 92 | class SmallVectorTest : public testing::Test { |
| 93 | protected: |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 94 | VectorT theVector; |
| 95 | VectorT otherVector; |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 96 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 97 | void SetUp() { |
| 98 | Constructable::reset(); |
| 99 | } |
| 100 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 101 | void assertEmpty(VectorT & v) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 102 | // Size tests |
| 103 | EXPECT_EQ(0u, v.size()); |
| 104 | EXPECT_TRUE(v.empty()); |
| 105 | |
| 106 | // Iterator tests |
| 107 | EXPECT_TRUE(v.begin() == v.end()); |
| 108 | } |
| 109 | |
| 110 | // Assert that theVector contains the specified values, in order. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 111 | void assertValuesInOrder(VectorT & v, size_t size, ...) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 112 | EXPECT_EQ(size, v.size()); |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 113 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 114 | va_list ap; |
| 115 | va_start(ap, size); |
| 116 | for (size_t i = 0; i < size; ++i) { |
| 117 | int value = va_arg(ap, int); |
| 118 | EXPECT_EQ(value, v[i].getValue()); |
| 119 | } |
| 120 | |
| 121 | va_end(ap); |
| 122 | } |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 123 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 124 | // Generate a sequence of values to initialize the vector. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 125 | void makeSequence(VectorT & v, int start, int end) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 126 | for (int i = start; i <= end; ++i) { |
| 127 | v.push_back(Constructable(i)); |
| 128 | } |
| 129 | } |
| 130 | }; |
| 131 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 132 | typedef ::testing::Types<SmallVector<Constructable, 0>, |
| 133 | SmallVector<Constructable, 1>, |
| 134 | SmallVector<Constructable, 2>, |
| 135 | SmallVector<Constructable, 4> |
| 136 | > SmallVectorTestTypes; |
| 137 | TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes); |
| 138 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 139 | // New vector test. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 140 | TYPED_TEST(SmallVectorTest, EmptyVectorTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 141 | SCOPED_TRACE("EmptyVectorTest"); |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 142 | this->assertEmpty(this->theVector); |
| 143 | EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend()); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 144 | EXPECT_EQ(0, Constructable::getNumConstructorCalls()); |
| 145 | EXPECT_EQ(0, Constructable::getNumDestructorCalls()); |
| 146 | } |
| 147 | |
| 148 | // Simple insertions and deletions. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 149 | TYPED_TEST(SmallVectorTest, PushPopTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 150 | SCOPED_TRACE("PushPopTest"); |
| 151 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 152 | // Track whether the vector will potentially have to grow. |
| 153 | bool RequiresGrowth = this->theVector.capacity() < 3; |
| 154 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 155 | // Push an element |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 156 | this->theVector.push_back(Constructable(1)); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 157 | |
| 158 | // Size tests |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 159 | this->assertValuesInOrder(this->theVector, 1u, 1); |
| 160 | EXPECT_FALSE(this->theVector.begin() == this->theVector.end()); |
| 161 | EXPECT_FALSE(this->theVector.empty()); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 162 | |
| 163 | // Push another element |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 164 | this->theVector.push_back(Constructable(2)); |
| 165 | this->assertValuesInOrder(this->theVector, 2u, 1, 2); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 166 | |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 167 | // Insert at beginning |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 168 | this->theVector.insert(this->theVector.begin(), this->theVector[1]); |
| 169 | this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2); |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 170 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 171 | // Pop one element |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 172 | this->theVector.pop_back(); |
| 173 | this->assertValuesInOrder(this->theVector, 2u, 2, 1); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 174 | |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 175 | // Pop remaining elements |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 176 | this->theVector.pop_back(); |
| 177 | this->theVector.pop_back(); |
| 178 | this->assertEmpty(this->theVector); |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 179 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 180 | // Check number of constructor calls. Should be 2 for each list element, |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 181 | // one for the argument to push_back, one for the argument to insert, |
| 182 | // and one for the list element itself. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 183 | if (!RequiresGrowth) { |
| 184 | EXPECT_EQ(5, Constructable::getNumConstructorCalls()); |
| 185 | EXPECT_EQ(5, Constructable::getNumDestructorCalls()); |
| 186 | } else { |
| 187 | // If we had to grow the vector, these only have a lower bound, but should |
| 188 | // always be equal. |
| 189 | EXPECT_LE(5, Constructable::getNumConstructorCalls()); |
| 190 | EXPECT_EQ(Constructable::getNumConstructorCalls(), |
| 191 | Constructable::getNumDestructorCalls()); |
| 192 | } |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | // Clear test. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 196 | TYPED_TEST(SmallVectorTest, ClearTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 197 | SCOPED_TRACE("ClearTest"); |
| 198 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 199 | this->theVector.reserve(2); |
| 200 | this->makeSequence(this->theVector, 1, 2); |
| 201 | this->theVector.clear(); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 202 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 203 | this->assertEmpty(this->theVector); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 204 | EXPECT_EQ(4, Constructable::getNumConstructorCalls()); |
| 205 | EXPECT_EQ(4, Constructable::getNumDestructorCalls()); |
| 206 | } |
| 207 | |
| 208 | // Resize smaller test. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 209 | TYPED_TEST(SmallVectorTest, ResizeShrinkTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 210 | SCOPED_TRACE("ResizeShrinkTest"); |
| 211 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 212 | this->theVector.reserve(3); |
| 213 | this->makeSequence(this->theVector, 1, 3); |
| 214 | this->theVector.resize(1); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 215 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 216 | this->assertValuesInOrder(this->theVector, 1u, 1); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 217 | EXPECT_EQ(6, Constructable::getNumConstructorCalls()); |
| 218 | EXPECT_EQ(5, Constructable::getNumDestructorCalls()); |
| 219 | } |
| 220 | |
| 221 | // Resize bigger test. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 222 | TYPED_TEST(SmallVectorTest, ResizeGrowTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 223 | SCOPED_TRACE("ResizeGrowTest"); |
| 224 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 225 | this->theVector.resize(2); |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 226 | |
Daniel Dunbar | 614be08 | 2009-07-12 19:45:34 +0000 | [diff] [blame] | 227 | // The extra constructor/destructor calls come from the temporary object used |
| 228 | // to initialize the contents of the resized array (via copy construction). |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 229 | EXPECT_EQ(3, Constructable::getNumConstructorCalls()); |
| 230 | EXPECT_EQ(1, Constructable::getNumDestructorCalls()); |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 231 | EXPECT_EQ(2u, this->theVector.size()); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // Resize with fill value. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 235 | TYPED_TEST(SmallVectorTest, ResizeFillTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 236 | SCOPED_TRACE("ResizeFillTest"); |
| 237 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 238 | this->theVector.resize(3, Constructable(77)); |
| 239 | this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Overflow past fixed size. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 243 | TYPED_TEST(SmallVectorTest, OverflowTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 244 | SCOPED_TRACE("OverflowTest"); |
| 245 | |
Daniel Dunbar | 614be08 | 2009-07-12 19:45:34 +0000 | [diff] [blame] | 246 | // Push more elements than the fixed size. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 247 | this->makeSequence(this->theVector, 1, 10); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 248 | |
Daniel Dunbar | 614be08 | 2009-07-12 19:45:34 +0000 | [diff] [blame] | 249 | // Test size and values. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 250 | EXPECT_EQ(10u, this->theVector.size()); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 251 | for (int i = 0; i < 10; ++i) { |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 252 | EXPECT_EQ(i+1, this->theVector[i].getValue()); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 253 | } |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 254 | |
Daniel Dunbar | 614be08 | 2009-07-12 19:45:34 +0000 | [diff] [blame] | 255 | // Now resize back to fixed size. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 256 | this->theVector.resize(1); |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 257 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 258 | this->assertValuesInOrder(this->theVector, 1u, 1); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Iteration tests. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 262 | TYPED_TEST(SmallVectorTest, IterationTest) { |
| 263 | this->makeSequence(this->theVector, 1, 2); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 264 | |
| 265 | // Forward Iteration |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 266 | typename TypeParam::iterator it = this->theVector.begin(); |
| 267 | EXPECT_TRUE(*it == this->theVector.front()); |
| 268 | EXPECT_TRUE(*it == this->theVector[0]); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 269 | EXPECT_EQ(1, it->getValue()); |
| 270 | ++it; |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 271 | EXPECT_TRUE(*it == this->theVector[1]); |
| 272 | EXPECT_TRUE(*it == this->theVector.back()); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 273 | EXPECT_EQ(2, it->getValue()); |
| 274 | ++it; |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 275 | EXPECT_TRUE(it == this->theVector.end()); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 276 | --it; |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 277 | EXPECT_TRUE(*it == this->theVector[1]); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 278 | EXPECT_EQ(2, it->getValue()); |
| 279 | --it; |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 280 | EXPECT_TRUE(*it == this->theVector[0]); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 281 | EXPECT_EQ(1, it->getValue()); |
| 282 | |
| 283 | // Reverse Iteration |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 284 | typename TypeParam::reverse_iterator rit = this->theVector.rbegin(); |
| 285 | EXPECT_TRUE(*rit == this->theVector[1]); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 286 | EXPECT_EQ(2, rit->getValue()); |
| 287 | ++rit; |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 288 | EXPECT_TRUE(*rit == this->theVector[0]); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 289 | EXPECT_EQ(1, rit->getValue()); |
| 290 | ++rit; |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 291 | EXPECT_TRUE(rit == this->theVector.rend()); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 292 | --rit; |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 293 | EXPECT_TRUE(*rit == this->theVector[0]); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 294 | EXPECT_EQ(1, rit->getValue()); |
| 295 | --rit; |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 296 | EXPECT_TRUE(*rit == this->theVector[1]); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 297 | EXPECT_EQ(2, rit->getValue()); |
| 298 | } |
| 299 | |
| 300 | // Swap test. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 301 | TYPED_TEST(SmallVectorTest, SwapTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 302 | SCOPED_TRACE("SwapTest"); |
| 303 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 304 | this->makeSequence(this->theVector, 1, 2); |
| 305 | std::swap(this->theVector, this->otherVector); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 306 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 307 | this->assertEmpty(this->theVector); |
| 308 | this->assertValuesInOrder(this->otherVector, 2u, 1, 2); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | // Append test |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 312 | TYPED_TEST(SmallVectorTest, AppendTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 313 | SCOPED_TRACE("AppendTest"); |
| 314 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 315 | this->makeSequence(this->otherVector, 2, 3); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 316 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 317 | this->theVector.push_back(Constructable(1)); |
| 318 | this->theVector.append(this->otherVector.begin(), this->otherVector.end()); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 319 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 320 | this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Append repeated test |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 324 | TYPED_TEST(SmallVectorTest, AppendRepeatedTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 325 | SCOPED_TRACE("AppendRepeatedTest"); |
| 326 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 327 | this->theVector.push_back(Constructable(1)); |
| 328 | this->theVector.append(2, Constructable(77)); |
| 329 | this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Assign test |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 333 | TYPED_TEST(SmallVectorTest, AssignTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 334 | SCOPED_TRACE("AssignTest"); |
| 335 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 336 | this->theVector.push_back(Constructable(1)); |
| 337 | this->theVector.assign(2, Constructable(77)); |
| 338 | this->assertValuesInOrder(this->theVector, 2u, 77, 77); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // Erase a single element |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 342 | TYPED_TEST(SmallVectorTest, EraseTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 343 | SCOPED_TRACE("EraseTest"); |
| 344 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 345 | this->makeSequence(this->theVector, 1, 3); |
| 346 | this->theVector.erase(this->theVector.begin()); |
| 347 | this->assertValuesInOrder(this->theVector, 2u, 2, 3); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // Erase a range of elements |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 351 | TYPED_TEST(SmallVectorTest, EraseRangeTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 352 | SCOPED_TRACE("EraseRangeTest"); |
| 353 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 354 | this->makeSequence(this->theVector, 1, 3); |
| 355 | this->theVector.erase(this->theVector.begin(), this->theVector.begin() + 2); |
| 356 | this->assertValuesInOrder(this->theVector, 1u, 3); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | // Insert a single element. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 360 | TYPED_TEST(SmallVectorTest, InsertTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 361 | SCOPED_TRACE("InsertTest"); |
| 362 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 363 | this->makeSequence(this->theVector, 1, 3); |
| 364 | typename TypeParam::iterator I = |
| 365 | this->theVector.insert(this->theVector.begin() + 1, Constructable(77)); |
| 366 | EXPECT_EQ(this->theVector.begin() + 1, I); |
| 367 | this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | // Insert repeated elements. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 371 | TYPED_TEST(SmallVectorTest, InsertRepeatedTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 372 | SCOPED_TRACE("InsertRepeatedTest"); |
| 373 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 374 | this->makeSequence(this->theVector, 10, 15); |
| 375 | typename TypeParam::iterator I = |
| 376 | this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16)); |
| 377 | EXPECT_EQ(this->theVector.begin() + 1, I); |
| 378 | this->assertValuesInOrder(this->theVector, 8u, |
| 379 | 10, 16, 16, 11, 12, 13, 14, 15); |
Benjamin Kramer | 5f6c7cf | 2012-06-17 11:52:22 +0000 | [diff] [blame] | 380 | |
Benjamin Kramer | d45f7b6 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 381 | // Insert at end. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 382 | I = this->theVector.insert(this->theVector.end(), 2, Constructable(16)); |
| 383 | EXPECT_EQ(this->theVector.begin() + 8, I); |
| 384 | this->assertValuesInOrder(this->theVector, 10u, |
| 385 | 10, 16, 16, 11, 12, 13, 14, 15, 16, 16); |
Benjamin Kramer | d45f7b6 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 386 | |
| 387 | // Empty insert. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 388 | EXPECT_EQ(this->theVector.end(), |
| 389 | this->theVector.insert(this->theVector.end(), |
| 390 | 0, Constructable(42))); |
| 391 | EXPECT_EQ(this->theVector.begin() + 1, |
| 392 | this->theVector.insert(this->theVector.begin() + 1, |
| 393 | 0, Constructable(42))); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | // Insert range. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 397 | TYPED_TEST(SmallVectorTest, InsertRangeTest) { |
Benjamin Kramer | d45f7b6 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 398 | SCOPED_TRACE("InsertRangeTest"); |
| 399 | |
| 400 | Constructable Arr[3] = |
| 401 | { Constructable(77), Constructable(77), Constructable(77) }; |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 402 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 403 | this->makeSequence(this->theVector, 1, 3); |
| 404 | typename TypeParam::iterator I = |
| 405 | this->theVector.insert(this->theVector.begin() + 1, Arr, Arr+3); |
| 406 | EXPECT_EQ(this->theVector.begin() + 1, I); |
| 407 | this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3); |
Benjamin Kramer | 5f6c7cf | 2012-06-17 11:52:22 +0000 | [diff] [blame] | 408 | |
Benjamin Kramer | d45f7b6 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 409 | // Insert at end. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 410 | I = this->theVector.insert(this->theVector.end(), Arr, Arr+3); |
| 411 | EXPECT_EQ(this->theVector.begin() + 6, I); |
| 412 | this->assertValuesInOrder(this->theVector, 9u, |
| 413 | 1, 77, 77, 77, 2, 3, 77, 77, 77); |
Benjamin Kramer | d45f7b6 | 2012-06-17 12:46:13 +0000 | [diff] [blame] | 414 | |
| 415 | // Empty insert. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 416 | EXPECT_EQ(this->theVector.end(), |
| 417 | this->theVector.insert(this->theVector.end(), |
| 418 | this->theVector.begin(), |
| 419 | this->theVector.begin())); |
| 420 | EXPECT_EQ(this->theVector.begin() + 1, |
| 421 | this->theVector.insert(this->theVector.begin() + 1, |
| 422 | this->theVector.begin(), |
| 423 | this->theVector.begin())); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // Comparison tests. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 427 | TYPED_TEST(SmallVectorTest, ComparisonTest) { |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 428 | SCOPED_TRACE("ComparisonTest"); |
| 429 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 430 | this->makeSequence(this->theVector, 1, 3); |
| 431 | this->makeSequence(this->otherVector, 1, 3); |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 432 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 433 | EXPECT_TRUE(this->theVector == this->otherVector); |
| 434 | EXPECT_FALSE(this->theVector != this->otherVector); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 435 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 436 | this->otherVector.clear(); |
| 437 | this->makeSequence(this->otherVector, 2, 4); |
Owen Anderson | 9cbd7af | 2011-07-06 22:36:59 +0000 | [diff] [blame] | 438 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 439 | EXPECT_FALSE(this->theVector == this->otherVector); |
| 440 | EXPECT_TRUE(this->theVector != this->otherVector); |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // Constant vector tests. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 444 | TYPED_TEST(SmallVectorTest, ConstVectorTest) { |
| 445 | const TypeParam constVector; |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 446 | |
| 447 | EXPECT_EQ(0u, constVector.size()); |
| 448 | EXPECT_TRUE(constVector.empty()); |
| 449 | EXPECT_TRUE(constVector.begin() == constVector.end()); |
| 450 | } |
| 451 | |
Daniel Dunbar | c2da6fb | 2009-08-19 17:48:28 +0000 | [diff] [blame] | 452 | // Direct array access. |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 453 | TYPED_TEST(SmallVectorTest, DirectVectorTest) { |
| 454 | EXPECT_EQ(0u, this->theVector.size()); |
| 455 | this->theVector.reserve(4); |
| 456 | EXPECT_LE(4u, this->theVector.capacity()); |
Daniel Dunbar | c2da6fb | 2009-08-19 17:48:28 +0000 | [diff] [blame] | 457 | EXPECT_EQ(0, Constructable::getNumConstructorCalls()); |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 458 | this->theVector.end()[0] = 1; |
| 459 | this->theVector.end()[1] = 2; |
| 460 | this->theVector.end()[2] = 3; |
| 461 | this->theVector.end()[3] = 4; |
| 462 | this->theVector.set_size(4); |
| 463 | EXPECT_EQ(4u, this->theVector.size()); |
Daniel Dunbar | c2da6fb | 2009-08-19 17:48:28 +0000 | [diff] [blame] | 464 | EXPECT_EQ(4, Constructable::getNumConstructorCalls()); |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 465 | EXPECT_EQ(1, this->theVector[0].getValue()); |
| 466 | EXPECT_EQ(2, this->theVector[1].getValue()); |
| 467 | EXPECT_EQ(3, this->theVector[2].getValue()); |
| 468 | EXPECT_EQ(4, this->theVector[3].getValue()); |
Daniel Dunbar | c2da6fb | 2009-08-19 17:48:28 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 471 | TYPED_TEST(SmallVectorTest, IteratorTest) { |
Dan Gohman | a7a33fd | 2010-03-26 18:53:37 +0000 | [diff] [blame] | 472 | std::list<int> L; |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 473 | this->theVector.insert(this->theVector.end(), L.begin(), L.end()); |
Dan Gohman | a7a33fd | 2010-03-26 18:53:37 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Benjamin Kramer | 3703baa | 2012-04-29 10:53:29 +0000 | [diff] [blame] | 476 | struct notassignable { |
| 477 | int &x; |
| 478 | notassignable(int &x) : x(x) {} |
| 479 | }; |
| 480 | |
Chandler Carruth | ba1f580 | 2012-07-30 22:17:52 +0000 | [diff] [blame^] | 481 | TEST(SmallVectorCustomTest, NoAssignTest) { |
Benjamin Kramer | 3703baa | 2012-04-29 10:53:29 +0000 | [diff] [blame] | 482 | int x = 0; |
| 483 | SmallVector<notassignable, 2> vec; |
| 484 | vec.push_back(notassignable(x)); |
| 485 | x = 42; |
| 486 | EXPECT_EQ(42, vec.pop_back_val().x); |
| 487 | } |
| 488 | |
Bill Wendling | f2850d9 | 2009-01-10 12:56:31 +0000 | [diff] [blame] | 489 | } |