2009-01-11 09:25:51 +08:00
|
|
|
//===- llvm/unittest/ADT/SmallVectorTest.cpp ------------------------------===//
|
2009-01-10 20:56:31 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-01-10 20:56:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// SmallVector unit tests.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2017-06-06 19:06:56 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2010-08-20 02:52:02 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2012-12-04 18:23:08 +08:00
|
|
|
#include "gtest/gtest.h"
|
2010-03-27 02:53:37 +08:00
|
|
|
#include <list>
|
2016-04-06 04:45:04 +08:00
|
|
|
#include <stdarg.h>
|
2009-01-10 20:56:31 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// A helper class that counts the total number of constructor and
|
|
|
|
/// destructor calls.
|
|
|
|
class Constructable {
|
|
|
|
private:
|
|
|
|
static int numConstructorCalls;
|
2014-06-09 03:12:28 +08:00
|
|
|
static int numMoveConstructorCalls;
|
|
|
|
static int numCopyConstructorCalls;
|
2009-01-10 20:56:31 +08:00
|
|
|
static int numDestructorCalls;
|
|
|
|
static int numAssignmentCalls;
|
2014-06-09 03:12:28 +08:00
|
|
|
static int numMoveAssignmentCalls;
|
|
|
|
static int numCopyAssignmentCalls;
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2014-04-30 23:49:06 +08:00
|
|
|
bool constructed;
|
2009-01-10 20:56:31 +08:00
|
|
|
int value;
|
|
|
|
|
|
|
|
public:
|
2014-04-30 23:49:06 +08:00
|
|
|
Constructable() : constructed(true), value(0) {
|
2009-01-10 20:56:31 +08:00
|
|
|
++numConstructorCalls;
|
|
|
|
}
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2014-04-30 23:49:06 +08:00
|
|
|
Constructable(int val) : constructed(true), value(val) {
|
2009-01-10 20:56:31 +08:00
|
|
|
++numConstructorCalls;
|
|
|
|
}
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2014-04-30 23:49:06 +08:00
|
|
|
Constructable(const Constructable & src) : constructed(true) {
|
|
|
|
value = src.value;
|
|
|
|
++numConstructorCalls;
|
2014-06-09 03:12:28 +08:00
|
|
|
++numCopyConstructorCalls;
|
2014-04-30 23:49:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Constructable(Constructable && src) : constructed(true) {
|
2009-01-10 20:56:31 +08:00
|
|
|
value = src.value;
|
|
|
|
++numConstructorCalls;
|
2014-06-09 03:12:28 +08:00
|
|
|
++numMoveConstructorCalls;
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
~Constructable() {
|
2014-04-30 23:49:06 +08:00
|
|
|
EXPECT_TRUE(constructed);
|
2009-01-10 20:56:31 +08:00
|
|
|
++numDestructorCalls;
|
2014-04-30 23:49:06 +08:00
|
|
|
constructed = false;
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
Constructable & operator=(const Constructable & src) {
|
2014-04-30 23:49:06 +08:00
|
|
|
EXPECT_TRUE(constructed);
|
|
|
|
value = src.value;
|
|
|
|
++numAssignmentCalls;
|
2014-06-09 03:12:28 +08:00
|
|
|
++numCopyAssignmentCalls;
|
2014-04-30 23:49:06 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Constructable & operator=(Constructable && src) {
|
|
|
|
EXPECT_TRUE(constructed);
|
2009-01-10 20:56:31 +08:00
|
|
|
value = src.value;
|
|
|
|
++numAssignmentCalls;
|
2014-06-09 03:12:28 +08:00
|
|
|
++numMoveAssignmentCalls;
|
2009-01-10 20:56:31 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
int getValue() const {
|
|
|
|
return abs(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset() {
|
|
|
|
numConstructorCalls = 0;
|
2014-06-09 03:12:28 +08:00
|
|
|
numMoveConstructorCalls = 0;
|
|
|
|
numCopyConstructorCalls = 0;
|
2009-01-10 20:56:31 +08:00
|
|
|
numDestructorCalls = 0;
|
|
|
|
numAssignmentCalls = 0;
|
2014-06-09 03:12:28 +08:00
|
|
|
numMoveAssignmentCalls = 0;
|
|
|
|
numCopyAssignmentCalls = 0;
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
static int getNumConstructorCalls() {
|
|
|
|
return numConstructorCalls;
|
|
|
|
}
|
|
|
|
|
2014-06-09 03:12:28 +08:00
|
|
|
static int getNumMoveConstructorCalls() {
|
|
|
|
return numMoveConstructorCalls;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getNumCopyConstructorCalls() {
|
|
|
|
return numCopyConstructorCalls;
|
|
|
|
}
|
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
static int getNumDestructorCalls() {
|
|
|
|
return numDestructorCalls;
|
|
|
|
}
|
|
|
|
|
2014-06-09 03:12:28 +08:00
|
|
|
static int getNumAssignmentCalls() {
|
|
|
|
return numAssignmentCalls;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getNumMoveAssignmentCalls() {
|
|
|
|
return numMoveAssignmentCalls;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getNumCopyAssignmentCalls() {
|
|
|
|
return numCopyAssignmentCalls;
|
|
|
|
}
|
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
friend bool operator==(const Constructable & c0, const Constructable & c1) {
|
|
|
|
return c0.getValue() == c1.getValue();
|
|
|
|
}
|
|
|
|
|
2010-10-23 16:10:43 +08:00
|
|
|
friend bool LLVM_ATTRIBUTE_UNUSED
|
2010-08-20 02:52:02 +08:00
|
|
|
operator!=(const Constructable & c0, const Constructable & c1) {
|
2009-01-10 20:56:31 +08:00
|
|
|
return c0.getValue() != c1.getValue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int Constructable::numConstructorCalls;
|
2014-06-09 03:12:28 +08:00
|
|
|
int Constructable::numCopyConstructorCalls;
|
|
|
|
int Constructable::numMoveConstructorCalls;
|
2009-01-10 20:56:31 +08:00
|
|
|
int Constructable::numDestructorCalls;
|
|
|
|
int Constructable::numAssignmentCalls;
|
2014-06-09 03:12:28 +08:00
|
|
|
int Constructable::numCopyAssignmentCalls;
|
|
|
|
int Constructable::numMoveAssignmentCalls;
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2014-06-10 06:26:20 +08:00
|
|
|
struct NonCopyable {
|
2016-04-06 04:45:04 +08:00
|
|
|
NonCopyable() {}
|
|
|
|
NonCopyable(NonCopyable &&) {}
|
2014-06-10 06:26:20 +08:00
|
|
|
NonCopyable &operator=(NonCopyable &&) { return *this; }
|
2014-06-12 01:50:14 +08:00
|
|
|
private:
|
2015-02-16 06:54:22 +08:00
|
|
|
NonCopyable(const NonCopyable &) = delete;
|
|
|
|
NonCopyable &operator=(const NonCopyable &) = delete;
|
2014-06-10 06:26:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
LLVM_ATTRIBUTE_USED void CompileTest() {
|
|
|
|
SmallVector<NonCopyable, 0> V;
|
|
|
|
V.resize(42);
|
|
|
|
}
|
|
|
|
|
2015-01-23 14:25:17 +08:00
|
|
|
class SmallVectorTestBase : public testing::Test {
|
2009-01-10 20:56:31 +08:00
|
|
|
protected:
|
2015-04-11 10:11:45 +08:00
|
|
|
void SetUp() override { Constructable::reset(); }
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2015-01-23 14:25:17 +08:00
|
|
|
template <typename VectorT>
|
2012-07-31 06:17:52 +08:00
|
|
|
void assertEmpty(VectorT & v) {
|
2009-01-10 20:56:31 +08:00
|
|
|
// Size tests
|
|
|
|
EXPECT_EQ(0u, v.size());
|
|
|
|
EXPECT_TRUE(v.empty());
|
|
|
|
|
|
|
|
// Iterator tests
|
|
|
|
EXPECT_TRUE(v.begin() == v.end());
|
|
|
|
}
|
|
|
|
|
2015-01-23 14:25:17 +08:00
|
|
|
// Assert that v contains the specified values, in order.
|
|
|
|
template <typename VectorT>
|
2012-07-31 06:17:52 +08:00
|
|
|
void assertValuesInOrder(VectorT & v, size_t size, ...) {
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(size, v.size());
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, size);
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
int value = va_arg(ap, int);
|
|
|
|
EXPECT_EQ(value, v[i].getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
// Generate a sequence of values to initialize the vector.
|
2015-01-23 14:25:17 +08:00
|
|
|
template <typename VectorT>
|
2012-07-31 06:17:52 +08:00
|
|
|
void makeSequence(VectorT & v, int start, int end) {
|
2009-01-10 20:56:31 +08:00
|
|
|
for (int i = start; i <= end; ++i) {
|
|
|
|
v.push_back(Constructable(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-23 14:25:17 +08:00
|
|
|
// Test fixture class
|
|
|
|
template <typename VectorT>
|
|
|
|
class SmallVectorTest : public SmallVectorTestBase {
|
|
|
|
protected:
|
|
|
|
VectorT theVector;
|
|
|
|
VectorT otherVector;
|
|
|
|
};
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
typedef ::testing::Types<SmallVector<Constructable, 0>,
|
|
|
|
SmallVector<Constructable, 1>,
|
|
|
|
SmallVector<Constructable, 2>,
|
2014-06-09 03:33:40 +08:00
|
|
|
SmallVector<Constructable, 4>,
|
|
|
|
SmallVector<Constructable, 5>
|
2012-07-31 06:17:52 +08:00
|
|
|
> SmallVectorTestTypes;
|
|
|
|
TYPED_TEST_CASE(SmallVectorTest, SmallVectorTestTypes);
|
|
|
|
|
2017-06-10 04:31:53 +08:00
|
|
|
// Constructor test.
|
|
|
|
TYPED_TEST(SmallVectorTest, ConstructorNonIterTest) {
|
|
|
|
SCOPED_TRACE("ConstructorTest");
|
|
|
|
this->theVector = SmallVector<Constructable, 2>(2, 2);
|
|
|
|
this->assertValuesInOrder(this->theVector, 2u, 2, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor test.
|
|
|
|
TYPED_TEST(SmallVectorTest, ConstructorIterTest) {
|
|
|
|
SCOPED_TRACE("ConstructorTest");
|
|
|
|
int arr[] = {1, 2, 3};
|
|
|
|
this->theVector =
|
|
|
|
SmallVector<Constructable, 4>(std::begin(arr), std::end(arr));
|
|
|
|
this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
|
|
|
|
}
|
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
// New vector test.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, EmptyVectorTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("EmptyVectorTest");
|
2012-07-31 06:17:52 +08:00
|
|
|
this->assertEmpty(this->theVector);
|
|
|
|
EXPECT_TRUE(this->theVector.rbegin() == this->theVector.rend());
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(0, Constructable::getNumConstructorCalls());
|
|
|
|
EXPECT_EQ(0, Constructable::getNumDestructorCalls());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple insertions and deletions.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, PushPopTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("PushPopTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
// Track whether the vector will potentially have to grow.
|
|
|
|
bool RequiresGrowth = this->theVector.capacity() < 3;
|
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
// Push an element
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.push_back(Constructable(1));
|
2009-01-10 20:56:31 +08:00
|
|
|
|
|
|
|
// Size tests
|
2012-07-31 06:17:52 +08:00
|
|
|
this->assertValuesInOrder(this->theVector, 1u, 1);
|
|
|
|
EXPECT_FALSE(this->theVector.begin() == this->theVector.end());
|
|
|
|
EXPECT_FALSE(this->theVector.empty());
|
2009-01-10 20:56:31 +08:00
|
|
|
|
|
|
|
// Push another element
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.push_back(Constructable(2));
|
|
|
|
this->assertValuesInOrder(this->theVector, 2u, 1, 2);
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2020-11-14 07:29:32 +08:00
|
|
|
// Insert at beginning. Reserve space to avoid reference invalidation from
|
|
|
|
// this->theVector[1].
|
|
|
|
this->theVector.reserve(this->theVector.size() + 1);
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.insert(this->theVector.begin(), this->theVector[1]);
|
|
|
|
this->assertValuesInOrder(this->theVector, 3u, 2, 1, 2);
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
// Pop one element
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.pop_back();
|
|
|
|
this->assertValuesInOrder(this->theVector, 2u, 2, 1);
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2011-07-07 06:36:59 +08:00
|
|
|
// Pop remaining elements
|
2020-11-03 22:57:08 +08:00
|
|
|
this->theVector.pop_back_n(2);
|
2012-07-31 06:17:52 +08:00
|
|
|
this->assertEmpty(this->theVector);
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
// Check number of constructor calls. Should be 2 for each list element,
|
2011-07-07 06:36:59 +08:00
|
|
|
// one for the argument to push_back, one for the argument to insert,
|
|
|
|
// and one for the list element itself.
|
2012-07-31 06:17:52 +08:00
|
|
|
if (!RequiresGrowth) {
|
|
|
|
EXPECT_EQ(5, Constructable::getNumConstructorCalls());
|
|
|
|
EXPECT_EQ(5, Constructable::getNumDestructorCalls());
|
|
|
|
} else {
|
|
|
|
// If we had to grow the vector, these only have a lower bound, but should
|
|
|
|
// always be equal.
|
|
|
|
EXPECT_LE(5, Constructable::getNumConstructorCalls());
|
|
|
|
EXPECT_EQ(Constructable::getNumConstructorCalls(),
|
|
|
|
Constructable::getNumDestructorCalls());
|
|
|
|
}
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear test.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, ClearTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("ClearTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.reserve(2);
|
|
|
|
this->makeSequence(this->theVector, 1, 2);
|
|
|
|
this->theVector.clear();
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->assertEmpty(this->theVector);
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(4, Constructable::getNumConstructorCalls());
|
|
|
|
EXPECT_EQ(4, Constructable::getNumDestructorCalls());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize smaller test.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, ResizeShrinkTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("ResizeShrinkTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.reserve(3);
|
|
|
|
this->makeSequence(this->theVector, 1, 3);
|
|
|
|
this->theVector.resize(1);
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->assertValuesInOrder(this->theVector, 1u, 1);
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(6, Constructable::getNumConstructorCalls());
|
|
|
|
EXPECT_EQ(5, Constructable::getNumDestructorCalls());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize bigger test.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, ResizeGrowTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("ResizeGrowTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.resize(2);
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2014-06-10 06:26:20 +08:00
|
|
|
EXPECT_EQ(2, Constructable::getNumConstructorCalls());
|
|
|
|
EXPECT_EQ(0, Constructable::getNumDestructorCalls());
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_EQ(2u, this->theVector.size());
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
2014-06-10 06:26:20 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, ResizeWithElementsTest) {
|
|
|
|
this->theVector.resize(2);
|
|
|
|
|
|
|
|
Constructable::reset();
|
|
|
|
|
|
|
|
this->theVector.resize(4);
|
|
|
|
|
|
|
|
size_t Ctors = Constructable::getNumConstructorCalls();
|
|
|
|
EXPECT_TRUE(Ctors == 2 || Ctors == 4);
|
|
|
|
size_t MoveCtors = Constructable::getNumMoveConstructorCalls();
|
|
|
|
EXPECT_TRUE(MoveCtors == 0 || MoveCtors == 2);
|
|
|
|
size_t Dtors = Constructable::getNumDestructorCalls();
|
|
|
|
EXPECT_TRUE(Dtors == 0 || Dtors == 2);
|
|
|
|
}
|
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
// Resize with fill value.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, ResizeFillTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("ResizeFillTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.resize(3, Constructable(77));
|
|
|
|
this->assertValuesInOrder(this->theVector, 3u, 77, 77, 77);
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overflow past fixed size.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, OverflowTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("OverflowTest");
|
|
|
|
|
2009-07-13 03:45:34 +08:00
|
|
|
// Push more elements than the fixed size.
|
2012-07-31 06:17:52 +08:00
|
|
|
this->makeSequence(this->theVector, 1, 10);
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2009-07-13 03:45:34 +08:00
|
|
|
// Test size and values.
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_EQ(10u, this->theVector.size());
|
2009-01-10 20:56:31 +08:00
|
|
|
for (int i = 0; i < 10; ++i) {
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_EQ(i+1, this->theVector[i].getValue());
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2009-07-13 03:45:34 +08:00
|
|
|
// Now resize back to fixed size.
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.resize(1);
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->assertValuesInOrder(this->theVector, 1u, 1);
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iteration tests.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, IterationTest) {
|
|
|
|
this->makeSequence(this->theVector, 1, 2);
|
2009-01-10 20:56:31 +08:00
|
|
|
|
|
|
|
// Forward Iteration
|
2012-07-31 06:17:52 +08:00
|
|
|
typename TypeParam::iterator it = this->theVector.begin();
|
|
|
|
EXPECT_TRUE(*it == this->theVector.front());
|
|
|
|
EXPECT_TRUE(*it == this->theVector[0]);
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(1, it->getValue());
|
|
|
|
++it;
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_TRUE(*it == this->theVector[1]);
|
|
|
|
EXPECT_TRUE(*it == this->theVector.back());
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(2, it->getValue());
|
|
|
|
++it;
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_TRUE(it == this->theVector.end());
|
2009-01-10 20:56:31 +08:00
|
|
|
--it;
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_TRUE(*it == this->theVector[1]);
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(2, it->getValue());
|
|
|
|
--it;
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_TRUE(*it == this->theVector[0]);
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(1, it->getValue());
|
|
|
|
|
|
|
|
// Reverse Iteration
|
2012-07-31 06:17:52 +08:00
|
|
|
typename TypeParam::reverse_iterator rit = this->theVector.rbegin();
|
|
|
|
EXPECT_TRUE(*rit == this->theVector[1]);
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(2, rit->getValue());
|
|
|
|
++rit;
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_TRUE(*rit == this->theVector[0]);
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(1, rit->getValue());
|
|
|
|
++rit;
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_TRUE(rit == this->theVector.rend());
|
2009-01-10 20:56:31 +08:00
|
|
|
--rit;
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_TRUE(*rit == this->theVector[0]);
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(1, rit->getValue());
|
|
|
|
--rit;
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_TRUE(*rit == this->theVector[1]);
|
2009-01-10 20:56:31 +08:00
|
|
|
EXPECT_EQ(2, rit->getValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap test.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, SwapTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("SwapTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->makeSequence(this->theVector, 1, 2);
|
|
|
|
std::swap(this->theVector, this->otherVector);
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->assertEmpty(this->theVector);
|
|
|
|
this->assertValuesInOrder(this->otherVector, 2u, 1, 2);
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append test
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, AppendTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("AppendTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->makeSequence(this->otherVector, 2, 3);
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.push_back(Constructable(1));
|
|
|
|
this->theVector.append(this->otherVector.begin(), this->otherVector.end());
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append repeated test
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, AppendRepeatedTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("AppendRepeatedTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.push_back(Constructable(1));
|
|
|
|
this->theVector.append(2, Constructable(77));
|
|
|
|
this->assertValuesInOrder(this->theVector, 3u, 1, 77, 77);
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
2017-06-10 04:31:53 +08:00
|
|
|
// Append test
|
|
|
|
TYPED_TEST(SmallVectorTest, AppendNonIterTest) {
|
|
|
|
SCOPED_TRACE("AppendRepeatedTest");
|
|
|
|
|
|
|
|
this->theVector.push_back(Constructable(1));
|
|
|
|
this->theVector.append(2, 7);
|
|
|
|
this->assertValuesInOrder(this->theVector, 3u, 1, 7, 7);
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:19:25 +08:00
|
|
|
struct output_iterator {
|
|
|
|
typedef std::output_iterator_tag iterator_category;
|
|
|
|
typedef int value_type;
|
|
|
|
typedef int difference_type;
|
|
|
|
typedef value_type *pointer;
|
|
|
|
typedef value_type &reference;
|
|
|
|
operator int() { return 2; }
|
|
|
|
operator Constructable() { return 7; }
|
|
|
|
};
|
|
|
|
|
2017-06-10 04:31:53 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, AppendRepeatedNonForwardIterator) {
|
|
|
|
SCOPED_TRACE("AppendRepeatedTest");
|
|
|
|
|
|
|
|
this->theVector.push_back(Constructable(1));
|
|
|
|
this->theVector.append(output_iterator(), output_iterator());
|
|
|
|
this->assertValuesInOrder(this->theVector, 3u, 1, 7, 7);
|
|
|
|
}
|
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
// Assign test
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, AssignTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("AssignTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.push_back(Constructable(1));
|
|
|
|
this->theVector.assign(2, Constructable(77));
|
|
|
|
this->assertValuesInOrder(this->theVector, 2u, 77, 77);
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
Tidy up a bit of r304516, use SmallVector::assign rather than for loop
This might give a few better opportunities to optimize these to memcpy
rather than loops - also a few minor cleanups (StringRef-izing,
templating (to avoid std::function indirection), etc).
The SmallVector::assign(iter, iter) could be improved with the use of
SFINAE, but the (iter, iter) ctor and append(iter, iter) need it to and
don't have it - so, workaround it for now rather than bothering with the
added complexity.
(also, as noted in the added FIXME, these assign ops could potentially
be optimized better at least for non-trivially-copyable types)
llvm-svn: 304566
2017-06-03 01:24:26 +08:00
|
|
|
// Assign test
|
|
|
|
TYPED_TEST(SmallVectorTest, AssignRangeTest) {
|
|
|
|
SCOPED_TRACE("AssignTest");
|
|
|
|
|
|
|
|
this->theVector.push_back(Constructable(1));
|
|
|
|
int arr[] = {1, 2, 3};
|
|
|
|
this->theVector.assign(std::begin(arr), std::end(arr));
|
|
|
|
this->assertValuesInOrder(this->theVector, 3u, 1, 2, 3);
|
|
|
|
}
|
|
|
|
|
2017-06-10 04:31:53 +08:00
|
|
|
// Assign test
|
|
|
|
TYPED_TEST(SmallVectorTest, AssignNonIterTest) {
|
|
|
|
SCOPED_TRACE("AssignTest");
|
|
|
|
|
|
|
|
this->theVector.push_back(Constructable(1));
|
|
|
|
this->theVector.assign(2, 7);
|
|
|
|
this->assertValuesInOrder(this->theVector, 2u, 7, 7);
|
|
|
|
}
|
|
|
|
|
2014-04-30 23:49:06 +08:00
|
|
|
// Move-assign test
|
|
|
|
TYPED_TEST(SmallVectorTest, MoveAssignTest) {
|
|
|
|
SCOPED_TRACE("MoveAssignTest");
|
|
|
|
|
|
|
|
// Set up our vector with a single element, but enough capacity for 4.
|
|
|
|
this->theVector.reserve(4);
|
|
|
|
this->theVector.push_back(Constructable(1));
|
|
|
|
|
|
|
|
// Set up the other vector with 2 elements.
|
|
|
|
this->otherVector.push_back(Constructable(2));
|
|
|
|
this->otherVector.push_back(Constructable(3));
|
|
|
|
|
|
|
|
// Move-assign from the other vector.
|
|
|
|
this->theVector = std::move(this->otherVector);
|
|
|
|
|
|
|
|
// Make sure we have the right result.
|
|
|
|
this->assertValuesInOrder(this->theVector, 2u, 2, 3);
|
|
|
|
|
|
|
|
// Make sure the # of constructor/destructor calls line up. There
|
|
|
|
// are two live objects after clearing the other vector.
|
|
|
|
this->otherVector.clear();
|
|
|
|
EXPECT_EQ(Constructable::getNumConstructorCalls()-2,
|
|
|
|
Constructable::getNumDestructorCalls());
|
|
|
|
|
|
|
|
// There shouldn't be any live objects any more.
|
|
|
|
this->theVector.clear();
|
|
|
|
EXPECT_EQ(Constructable::getNumConstructorCalls(),
|
|
|
|
Constructable::getNumDestructorCalls());
|
|
|
|
}
|
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
// Erase a single element
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, EraseTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("EraseTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->makeSequence(this->theVector, 1, 3);
|
2016-03-25 04:25:51 +08:00
|
|
|
const auto &theConstVector = this->theVector;
|
|
|
|
this->theVector.erase(theConstVector.begin());
|
2012-07-31 06:17:52 +08:00
|
|
|
this->assertValuesInOrder(this->theVector, 2u, 2, 3);
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Erase a range of elements
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, EraseRangeTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("EraseRangeTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->makeSequence(this->theVector, 1, 3);
|
2016-03-25 04:25:51 +08:00
|
|
|
const auto &theConstVector = this->theVector;
|
|
|
|
this->theVector.erase(theConstVector.begin(), theConstVector.begin() + 2);
|
2012-07-31 06:17:52 +08:00
|
|
|
this->assertValuesInOrder(this->theVector, 1u, 3);
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert a single element.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, InsertTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("InsertTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->makeSequence(this->theVector, 1, 3);
|
|
|
|
typename TypeParam::iterator I =
|
|
|
|
this->theVector.insert(this->theVector.begin() + 1, Constructable(77));
|
|
|
|
EXPECT_EQ(this->theVector.begin() + 1, I);
|
|
|
|
this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
2014-06-09 00:55:13 +08:00
|
|
|
// Insert a copy of a single element.
|
|
|
|
TYPED_TEST(SmallVectorTest, InsertCopy) {
|
|
|
|
SCOPED_TRACE("InsertTest");
|
|
|
|
|
|
|
|
this->makeSequence(this->theVector, 1, 3);
|
|
|
|
Constructable C(77);
|
|
|
|
typename TypeParam::iterator I =
|
|
|
|
this->theVector.insert(this->theVector.begin() + 1, C);
|
|
|
|
EXPECT_EQ(this->theVector.begin() + 1, I);
|
|
|
|
this->assertValuesInOrder(this->theVector, 4u, 1, 77, 2, 3);
|
|
|
|
}
|
|
|
|
|
2009-01-10 20:56:31 +08:00
|
|
|
// Insert repeated elements.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, InsertRepeatedTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("InsertRepeatedTest");
|
|
|
|
|
2014-06-09 03:33:40 +08:00
|
|
|
this->makeSequence(this->theVector, 1, 4);
|
2014-06-09 03:12:28 +08:00
|
|
|
Constructable::reset();
|
2014-06-09 03:12:31 +08:00
|
|
|
auto I =
|
|
|
|
this->theVector.insert(this->theVector.begin() + 1, 2, Constructable(16));
|
2014-06-09 03:33:40 +08:00
|
|
|
// Move construct the top element into newly allocated space, and optionally
|
|
|
|
// reallocate the whole buffer, move constructing into it.
|
|
|
|
// FIXME: This is inefficient, we shouldn't move things into newly allocated
|
|
|
|
// space, then move them up/around, there should only be 2 or 4 move
|
|
|
|
// constructions here.
|
|
|
|
EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
|
|
|
|
Constructable::getNumMoveConstructorCalls() == 6);
|
2014-06-09 03:12:31 +08:00
|
|
|
// Move assign the next two to shift them up and make a gap.
|
2014-06-09 03:33:40 +08:00
|
|
|
EXPECT_EQ(1, Constructable::getNumMoveAssignmentCalls());
|
2014-06-09 03:12:31 +08:00
|
|
|
// Copy construct the two new elements from the parameter.
|
|
|
|
EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
|
|
|
|
// All without any copy construction.
|
2014-06-09 03:12:28 +08:00
|
|
|
EXPECT_EQ(0, Constructable::getNumCopyConstructorCalls());
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_EQ(this->theVector.begin() + 1, I);
|
2014-06-09 03:33:40 +08:00
|
|
|
this->assertValuesInOrder(this->theVector, 6u, 1, 16, 16, 2, 3, 4);
|
2014-06-09 03:12:31 +08:00
|
|
|
}
|
2012-06-17 19:52:22 +08:00
|
|
|
|
2017-06-10 04:31:53 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, InsertRepeatedNonIterTest) {
|
|
|
|
SCOPED_TRACE("InsertRepeatedTest");
|
|
|
|
|
|
|
|
this->makeSequence(this->theVector, 1, 4);
|
|
|
|
Constructable::reset();
|
|
|
|
auto I = this->theVector.insert(this->theVector.begin() + 1, 2, 7);
|
|
|
|
EXPECT_EQ(this->theVector.begin() + 1, I);
|
|
|
|
this->assertValuesInOrder(this->theVector, 6u, 1, 7, 7, 2, 3, 4);
|
|
|
|
}
|
2016-04-06 04:45:04 +08:00
|
|
|
|
2014-06-09 03:12:31 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, InsertRepeatedAtEndTest) {
|
|
|
|
SCOPED_TRACE("InsertRepeatedTest");
|
|
|
|
|
2014-06-09 03:33:40 +08:00
|
|
|
this->makeSequence(this->theVector, 1, 4);
|
2014-06-09 03:12:31 +08:00
|
|
|
Constructable::reset();
|
|
|
|
auto I = this->theVector.insert(this->theVector.end(), 2, Constructable(16));
|
|
|
|
// Just copy construct them into newly allocated space
|
|
|
|
EXPECT_EQ(2, Constructable::getNumCopyConstructorCalls());
|
2014-06-09 03:33:40 +08:00
|
|
|
// Move everything across if reallocation is needed.
|
|
|
|
EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
|
|
|
|
Constructable::getNumMoveConstructorCalls() == 4);
|
2014-06-09 03:12:31 +08:00
|
|
|
// Without ever moving or copying anything else.
|
|
|
|
EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
|
|
|
|
EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
|
|
|
|
|
2014-06-09 03:33:40 +08:00
|
|
|
EXPECT_EQ(this->theVector.begin() + 4, I);
|
|
|
|
this->assertValuesInOrder(this->theVector, 6u, 1, 2, 3, 4, 16, 16);
|
2014-06-09 03:12:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorTest, InsertRepeatedEmptyTest) {
|
|
|
|
SCOPED_TRACE("InsertRepeatedTest");
|
|
|
|
|
|
|
|
this->makeSequence(this->theVector, 10, 15);
|
2012-06-17 20:46:13 +08:00
|
|
|
|
|
|
|
// Empty insert.
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_EQ(this->theVector.end(),
|
|
|
|
this->theVector.insert(this->theVector.end(),
|
|
|
|
0, Constructable(42)));
|
|
|
|
EXPECT_EQ(this->theVector.begin() + 1,
|
|
|
|
this->theVector.insert(this->theVector.begin() + 1,
|
|
|
|
0, Constructable(42)));
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert range.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, InsertRangeTest) {
|
2012-06-17 20:46:13 +08:00
|
|
|
SCOPED_TRACE("InsertRangeTest");
|
|
|
|
|
|
|
|
Constructable Arr[3] =
|
|
|
|
{ Constructable(77), Constructable(77), Constructable(77) };
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->makeSequence(this->theVector, 1, 3);
|
2014-06-09 03:12:31 +08:00
|
|
|
Constructable::reset();
|
|
|
|
auto I = this->theVector.insert(this->theVector.begin() + 1, Arr, Arr + 3);
|
|
|
|
// Move construct the top 3 elements into newly allocated space.
|
|
|
|
// Possibly move the whole sequence into new space first.
|
|
|
|
// FIXME: This is inefficient, we shouldn't move things into newly allocated
|
|
|
|
// space, then move them up/around, there should only be 2 or 3 move
|
|
|
|
// constructions here.
|
|
|
|
EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 2 ||
|
|
|
|
Constructable::getNumMoveConstructorCalls() == 5);
|
|
|
|
// Copy assign the lower 2 new elements into existing space.
|
|
|
|
EXPECT_EQ(2, Constructable::getNumCopyAssignmentCalls());
|
|
|
|
// Copy construct the third element into newly allocated space.
|
|
|
|
EXPECT_EQ(1, Constructable::getNumCopyConstructorCalls());
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_EQ(this->theVector.begin() + 1, I);
|
|
|
|
this->assertValuesInOrder(this->theVector, 6u, 1, 77, 77, 77, 2, 3);
|
2014-06-09 03:12:31 +08:00
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
|
2014-06-09 03:12:31 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, InsertRangeAtEndTest) {
|
|
|
|
SCOPED_TRACE("InsertRangeTest");
|
|
|
|
|
|
|
|
Constructable Arr[3] =
|
|
|
|
{ Constructable(77), Constructable(77), Constructable(77) };
|
|
|
|
|
|
|
|
this->makeSequence(this->theVector, 1, 3);
|
2012-06-17 19:52:22 +08:00
|
|
|
|
2012-06-17 20:46:13 +08:00
|
|
|
// Insert at end.
|
2014-06-09 03:12:31 +08:00
|
|
|
Constructable::reset();
|
|
|
|
auto I = this->theVector.insert(this->theVector.end(), Arr, Arr+3);
|
|
|
|
// Copy construct the 3 elements into new space at the top.
|
|
|
|
EXPECT_EQ(3, Constructable::getNumCopyConstructorCalls());
|
|
|
|
// Don't copy/move anything else.
|
|
|
|
EXPECT_EQ(0, Constructable::getNumCopyAssignmentCalls());
|
|
|
|
// Reallocation might occur, causing all elements to be moved into the new
|
|
|
|
// buffer.
|
|
|
|
EXPECT_TRUE(Constructable::getNumMoveConstructorCalls() == 0 ||
|
|
|
|
Constructable::getNumMoveConstructorCalls() == 3);
|
|
|
|
EXPECT_EQ(0, Constructable::getNumMoveAssignmentCalls());
|
|
|
|
EXPECT_EQ(this->theVector.begin() + 3, I);
|
|
|
|
this->assertValuesInOrder(this->theVector, 6u,
|
|
|
|
1, 2, 3, 77, 77, 77);
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorTest, InsertEmptyRangeTest) {
|
|
|
|
SCOPED_TRACE("InsertRangeTest");
|
|
|
|
|
|
|
|
this->makeSequence(this->theVector, 1, 3);
|
2012-06-17 20:46:13 +08:00
|
|
|
|
|
|
|
// Empty insert.
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_EQ(this->theVector.end(),
|
|
|
|
this->theVector.insert(this->theVector.end(),
|
|
|
|
this->theVector.begin(),
|
|
|
|
this->theVector.begin()));
|
|
|
|
EXPECT_EQ(this->theVector.begin() + 1,
|
|
|
|
this->theVector.insert(this->theVector.begin() + 1,
|
|
|
|
this->theVector.begin(),
|
|
|
|
this->theVector.begin()));
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Comparison tests.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, ComparisonTest) {
|
2009-01-10 20:56:31 +08:00
|
|
|
SCOPED_TRACE("ComparisonTest");
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->makeSequence(this->theVector, 1, 3);
|
|
|
|
this->makeSequence(this->otherVector, 1, 3);
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_TRUE(this->theVector == this->otherVector);
|
|
|
|
EXPECT_FALSE(this->theVector != this->otherVector);
|
2009-01-10 20:56:31 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
this->otherVector.clear();
|
|
|
|
this->makeSequence(this->otherVector, 2, 4);
|
2011-07-07 06:36:59 +08:00
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_FALSE(this->theVector == this->otherVector);
|
|
|
|
EXPECT_TRUE(this->theVector != this->otherVector);
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Constant vector tests.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, ConstVectorTest) {
|
|
|
|
const TypeParam constVector;
|
2009-01-10 20:56:31 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(0u, constVector.size());
|
|
|
|
EXPECT_TRUE(constVector.empty());
|
|
|
|
EXPECT_TRUE(constVector.begin() == constVector.end());
|
|
|
|
}
|
|
|
|
|
2009-08-20 01:48:28 +08:00
|
|
|
// Direct array access.
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, DirectVectorTest) {
|
|
|
|
EXPECT_EQ(0u, this->theVector.size());
|
|
|
|
this->theVector.reserve(4);
|
|
|
|
EXPECT_LE(4u, this->theVector.capacity());
|
2009-08-20 01:48:28 +08:00
|
|
|
EXPECT_EQ(0, Constructable::getNumConstructorCalls());
|
2014-04-30 23:49:06 +08:00
|
|
|
this->theVector.push_back(1);
|
|
|
|
this->theVector.push_back(2);
|
|
|
|
this->theVector.push_back(3);
|
|
|
|
this->theVector.push_back(4);
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_EQ(4u, this->theVector.size());
|
2014-04-30 23:49:06 +08:00
|
|
|
EXPECT_EQ(8, Constructable::getNumConstructorCalls());
|
2012-07-31 06:17:52 +08:00
|
|
|
EXPECT_EQ(1, this->theVector[0].getValue());
|
|
|
|
EXPECT_EQ(2, this->theVector[1].getValue());
|
|
|
|
EXPECT_EQ(3, this->theVector[2].getValue());
|
|
|
|
EXPECT_EQ(4, this->theVector[3].getValue());
|
2009-08-20 01:48:28 +08:00
|
|
|
}
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
TYPED_TEST(SmallVectorTest, IteratorTest) {
|
2010-03-27 02:53:37 +08:00
|
|
|
std::list<int> L;
|
2012-07-31 06:17:52 +08:00
|
|
|
this->theVector.insert(this->theVector.end(), L.begin(), L.end());
|
2010-03-27 02:53:37 +08:00
|
|
|
}
|
|
|
|
|
2015-01-23 14:25:17 +08:00
|
|
|
template <typename InvalidType> class DualSmallVectorsTest;
|
|
|
|
|
|
|
|
template <typename VectorT1, typename VectorT2>
|
|
|
|
class DualSmallVectorsTest<std::pair<VectorT1, VectorT2>> : public SmallVectorTestBase {
|
|
|
|
protected:
|
|
|
|
VectorT1 theVector;
|
|
|
|
VectorT2 otherVector;
|
|
|
|
|
|
|
|
template <typename T, unsigned N>
|
|
|
|
static unsigned NumBuiltinElts(const SmallVector<T, N>&) { return N; }
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef ::testing::Types<
|
|
|
|
// Small mode -> Small mode.
|
|
|
|
std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 4>>,
|
|
|
|
// Small mode -> Big mode.
|
|
|
|
std::pair<SmallVector<Constructable, 4>, SmallVector<Constructable, 2>>,
|
|
|
|
// Big mode -> Small mode.
|
|
|
|
std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 4>>,
|
|
|
|
// Big mode -> Big mode.
|
|
|
|
std::pair<SmallVector<Constructable, 2>, SmallVector<Constructable, 2>>
|
|
|
|
> DualSmallVectorTestTypes;
|
|
|
|
|
|
|
|
TYPED_TEST_CASE(DualSmallVectorsTest, DualSmallVectorTestTypes);
|
|
|
|
|
|
|
|
TYPED_TEST(DualSmallVectorsTest, MoveAssignment) {
|
|
|
|
SCOPED_TRACE("MoveAssignTest-DualVectorTypes");
|
|
|
|
|
|
|
|
// Set up our vector with four elements.
|
|
|
|
for (unsigned I = 0; I < 4; ++I)
|
|
|
|
this->otherVector.push_back(Constructable(I));
|
|
|
|
|
|
|
|
const Constructable *OrigDataPtr = this->otherVector.data();
|
|
|
|
|
|
|
|
// Move-assign from the other vector.
|
|
|
|
this->theVector =
|
|
|
|
std::move(static_cast<SmallVectorImpl<Constructable>&>(this->otherVector));
|
|
|
|
|
|
|
|
// Make sure we have the right result.
|
|
|
|
this->assertValuesInOrder(this->theVector, 4u, 0, 1, 2, 3);
|
|
|
|
|
|
|
|
// Make sure the # of constructor/destructor calls line up. There
|
|
|
|
// are two live objects after clearing the other vector.
|
|
|
|
this->otherVector.clear();
|
|
|
|
EXPECT_EQ(Constructable::getNumConstructorCalls()-4,
|
|
|
|
Constructable::getNumDestructorCalls());
|
|
|
|
|
|
|
|
// If the source vector (otherVector) was in small-mode, assert that we just
|
|
|
|
// moved the data pointer over.
|
|
|
|
EXPECT_TRUE(this->NumBuiltinElts(this->otherVector) == 4 ||
|
|
|
|
this->theVector.data() == OrigDataPtr);
|
|
|
|
|
|
|
|
// There shouldn't be any live objects any more.
|
|
|
|
this->theVector.clear();
|
|
|
|
EXPECT_EQ(Constructable::getNumConstructorCalls(),
|
|
|
|
Constructable::getNumDestructorCalls());
|
|
|
|
|
|
|
|
// We shouldn't have copied anything in this whole process.
|
|
|
|
EXPECT_EQ(Constructable::getNumCopyConstructorCalls(), 0);
|
|
|
|
}
|
|
|
|
|
2012-04-29 18:53:29 +08:00
|
|
|
struct notassignable {
|
|
|
|
int &x;
|
|
|
|
notassignable(int &x) : x(x) {}
|
|
|
|
};
|
|
|
|
|
2012-07-31 06:17:52 +08:00
|
|
|
TEST(SmallVectorCustomTest, NoAssignTest) {
|
2012-04-29 18:53:29 +08:00
|
|
|
int x = 0;
|
|
|
|
SmallVector<notassignable, 2> vec;
|
|
|
|
vec.push_back(notassignable(x));
|
|
|
|
x = 42;
|
|
|
|
EXPECT_EQ(42, vec.pop_back_val().x);
|
|
|
|
}
|
|
|
|
|
2014-06-09 00:00:02 +08:00
|
|
|
struct MovedFrom {
|
|
|
|
bool hasValue;
|
|
|
|
MovedFrom() : hasValue(true) {
|
|
|
|
}
|
|
|
|
MovedFrom(MovedFrom&& m) : hasValue(m.hasValue) {
|
|
|
|
m.hasValue = false;
|
|
|
|
}
|
|
|
|
MovedFrom &operator=(MovedFrom&& m) {
|
|
|
|
hasValue = m.hasValue;
|
|
|
|
m.hasValue = false;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(SmallVectorTest, MidInsert) {
|
|
|
|
SmallVector<MovedFrom, 3> v;
|
|
|
|
v.push_back(MovedFrom());
|
|
|
|
v.insert(v.begin(), MovedFrom());
|
|
|
|
for (MovedFrom &m : v)
|
|
|
|
EXPECT_TRUE(m.hasValue);
|
|
|
|
}
|
|
|
|
|
2014-12-03 12:45:09 +08:00
|
|
|
enum EmplaceableArgState {
|
|
|
|
EAS_Defaulted,
|
|
|
|
EAS_Arg,
|
|
|
|
EAS_LValue,
|
|
|
|
EAS_RValue,
|
|
|
|
EAS_Failure
|
|
|
|
};
|
|
|
|
template <int I> struct EmplaceableArg {
|
|
|
|
EmplaceableArgState State;
|
|
|
|
EmplaceableArg() : State(EAS_Defaulted) {}
|
|
|
|
EmplaceableArg(EmplaceableArg &&X)
|
|
|
|
: State(X.State == EAS_Arg ? EAS_RValue : EAS_Failure) {}
|
|
|
|
EmplaceableArg(EmplaceableArg &X)
|
|
|
|
: State(X.State == EAS_Arg ? EAS_LValue : EAS_Failure) {}
|
|
|
|
|
|
|
|
explicit EmplaceableArg(bool) : State(EAS_Arg) {}
|
|
|
|
|
|
|
|
private:
|
2015-02-16 06:54:22 +08:00
|
|
|
EmplaceableArg &operator=(EmplaceableArg &&) = delete;
|
|
|
|
EmplaceableArg &operator=(const EmplaceableArg &) = delete;
|
2014-12-03 12:45:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
enum EmplaceableState { ES_Emplaced, ES_Moved };
|
|
|
|
struct Emplaceable {
|
|
|
|
EmplaceableArg<0> A0;
|
|
|
|
EmplaceableArg<1> A1;
|
|
|
|
EmplaceableArg<2> A2;
|
|
|
|
EmplaceableArg<3> A3;
|
|
|
|
EmplaceableState State;
|
|
|
|
|
|
|
|
Emplaceable() : State(ES_Emplaced) {}
|
|
|
|
|
|
|
|
template <class A0Ty>
|
|
|
|
explicit Emplaceable(A0Ty &&A0)
|
|
|
|
: A0(std::forward<A0Ty>(A0)), State(ES_Emplaced) {}
|
|
|
|
|
|
|
|
template <class A0Ty, class A1Ty>
|
|
|
|
Emplaceable(A0Ty &&A0, A1Ty &&A1)
|
|
|
|
: A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
|
|
|
|
State(ES_Emplaced) {}
|
|
|
|
|
|
|
|
template <class A0Ty, class A1Ty, class A2Ty>
|
|
|
|
Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2)
|
|
|
|
: A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
|
|
|
|
A2(std::forward<A2Ty>(A2)), State(ES_Emplaced) {}
|
|
|
|
|
|
|
|
template <class A0Ty, class A1Ty, class A2Ty, class A3Ty>
|
|
|
|
Emplaceable(A0Ty &&A0, A1Ty &&A1, A2Ty &&A2, A3Ty &&A3)
|
|
|
|
: A0(std::forward<A0Ty>(A0)), A1(std::forward<A1Ty>(A1)),
|
|
|
|
A2(std::forward<A2Ty>(A2)), A3(std::forward<A3Ty>(A3)),
|
|
|
|
State(ES_Emplaced) {}
|
|
|
|
|
|
|
|
Emplaceable(Emplaceable &&) : State(ES_Moved) {}
|
|
|
|
Emplaceable &operator=(Emplaceable &&) {
|
|
|
|
State = ES_Moved;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-02-16 06:54:22 +08:00
|
|
|
Emplaceable(const Emplaceable &) = delete;
|
|
|
|
Emplaceable &operator=(const Emplaceable &) = delete;
|
2014-12-03 12:45:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(SmallVectorTest, EmplaceBack) {
|
|
|
|
EmplaceableArg<0> A0(true);
|
|
|
|
EmplaceableArg<1> A1(true);
|
|
|
|
EmplaceableArg<2> A2(true);
|
|
|
|
EmplaceableArg<3> A3(true);
|
|
|
|
{
|
|
|
|
SmallVector<Emplaceable, 3> V;
|
2019-03-26 13:33:52 +08:00
|
|
|
Emplaceable &back = V.emplace_back();
|
|
|
|
EXPECT_TRUE(&back == &V.back());
|
2014-12-03 12:45:09 +08:00
|
|
|
EXPECT_TRUE(V.size() == 1);
|
2019-03-26 13:33:52 +08:00
|
|
|
EXPECT_TRUE(back.State == ES_Emplaced);
|
|
|
|
EXPECT_TRUE(back.A0.State == EAS_Defaulted);
|
|
|
|
EXPECT_TRUE(back.A1.State == EAS_Defaulted);
|
|
|
|
EXPECT_TRUE(back.A2.State == EAS_Defaulted);
|
|
|
|
EXPECT_TRUE(back.A3.State == EAS_Defaulted);
|
2014-12-03 12:45:09 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SmallVector<Emplaceable, 3> V;
|
2019-03-26 13:33:52 +08:00
|
|
|
Emplaceable &back = V.emplace_back(std::move(A0));
|
|
|
|
EXPECT_TRUE(&back == &V.back());
|
2014-12-03 12:45:09 +08:00
|
|
|
EXPECT_TRUE(V.size() == 1);
|
2019-03-26 13:33:52 +08:00
|
|
|
EXPECT_TRUE(back.State == ES_Emplaced);
|
|
|
|
EXPECT_TRUE(back.A0.State == EAS_RValue);
|
|
|
|
EXPECT_TRUE(back.A1.State == EAS_Defaulted);
|
|
|
|
EXPECT_TRUE(back.A2.State == EAS_Defaulted);
|
|
|
|
EXPECT_TRUE(back.A3.State == EAS_Defaulted);
|
2014-12-03 12:45:09 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SmallVector<Emplaceable, 3> V;
|
2019-03-26 13:33:52 +08:00
|
|
|
Emplaceable &back = V.emplace_back(A0);
|
|
|
|
EXPECT_TRUE(&back == &V.back());
|
2014-12-03 12:45:09 +08:00
|
|
|
EXPECT_TRUE(V.size() == 1);
|
2019-03-26 13:33:52 +08:00
|
|
|
EXPECT_TRUE(back.State == ES_Emplaced);
|
|
|
|
EXPECT_TRUE(back.A0.State == EAS_LValue);
|
|
|
|
EXPECT_TRUE(back.A1.State == EAS_Defaulted);
|
|
|
|
EXPECT_TRUE(back.A2.State == EAS_Defaulted);
|
|
|
|
EXPECT_TRUE(back.A3.State == EAS_Defaulted);
|
2014-12-03 12:45:09 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SmallVector<Emplaceable, 3> V;
|
2019-03-26 13:33:52 +08:00
|
|
|
Emplaceable &back = V.emplace_back(A0, A1);
|
|
|
|
EXPECT_TRUE(&back == &V.back());
|
2014-12-03 12:45:09 +08:00
|
|
|
EXPECT_TRUE(V.size() == 1);
|
2019-03-26 13:33:52 +08:00
|
|
|
EXPECT_TRUE(back.State == ES_Emplaced);
|
|
|
|
EXPECT_TRUE(back.A0.State == EAS_LValue);
|
|
|
|
EXPECT_TRUE(back.A1.State == EAS_LValue);
|
|
|
|
EXPECT_TRUE(back.A2.State == EAS_Defaulted);
|
|
|
|
EXPECT_TRUE(back.A3.State == EAS_Defaulted);
|
2014-12-03 12:45:09 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SmallVector<Emplaceable, 3> V;
|
2019-03-26 13:33:52 +08:00
|
|
|
Emplaceable &back = V.emplace_back(std::move(A0), std::move(A1));
|
|
|
|
EXPECT_TRUE(&back == &V.back());
|
2014-12-03 12:45:09 +08:00
|
|
|
EXPECT_TRUE(V.size() == 1);
|
2019-03-26 13:33:52 +08:00
|
|
|
EXPECT_TRUE(back.State == ES_Emplaced);
|
|
|
|
EXPECT_TRUE(back.A0.State == EAS_RValue);
|
|
|
|
EXPECT_TRUE(back.A1.State == EAS_RValue);
|
|
|
|
EXPECT_TRUE(back.A2.State == EAS_Defaulted);
|
|
|
|
EXPECT_TRUE(back.A3.State == EAS_Defaulted);
|
2014-12-03 12:45:09 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
SmallVector<Emplaceable, 3> V;
|
2019-03-26 13:33:52 +08:00
|
|
|
Emplaceable &back = V.emplace_back(std::move(A0), A1, std::move(A2), A3);
|
|
|
|
EXPECT_TRUE(&back == &V.back());
|
2014-12-03 12:45:09 +08:00
|
|
|
EXPECT_TRUE(V.size() == 1);
|
2019-03-26 13:33:52 +08:00
|
|
|
EXPECT_TRUE(back.State == ES_Emplaced);
|
|
|
|
EXPECT_TRUE(back.A0.State == EAS_RValue);
|
|
|
|
EXPECT_TRUE(back.A1.State == EAS_LValue);
|
|
|
|
EXPECT_TRUE(back.A2.State == EAS_RValue);
|
|
|
|
EXPECT_TRUE(back.A3.State == EAS_LValue);
|
2014-12-03 12:45:09 +08:00
|
|
|
}
|
2015-02-08 00:41:02 +08:00
|
|
|
{
|
|
|
|
SmallVector<int, 1> V;
|
|
|
|
V.emplace_back();
|
|
|
|
V.emplace_back(42);
|
|
|
|
EXPECT_EQ(2U, V.size());
|
|
|
|
EXPECT_EQ(0, V[0]);
|
|
|
|
EXPECT_EQ(42, V[1]);
|
|
|
|
}
|
2009-01-10 20:56:31 +08:00
|
|
|
}
|
2014-12-03 12:45:09 +08:00
|
|
|
|
2015-03-03 05:16:04 +08:00
|
|
|
TEST(SmallVectorTest, InitializerList) {
|
|
|
|
SmallVector<int, 2> V1 = {};
|
|
|
|
EXPECT_TRUE(V1.empty());
|
|
|
|
V1 = {0, 0};
|
|
|
|
EXPECT_TRUE(makeArrayRef(V1).equals({0, 0}));
|
|
|
|
V1 = {-1, -1};
|
|
|
|
EXPECT_TRUE(makeArrayRef(V1).equals({-1, -1}));
|
|
|
|
|
|
|
|
SmallVector<int, 2> V2 = {1, 2, 3, 4};
|
|
|
|
EXPECT_TRUE(makeArrayRef(V2).equals({1, 2, 3, 4}));
|
|
|
|
V2.assign({4});
|
|
|
|
EXPECT_TRUE(makeArrayRef(V2).equals({4}));
|
|
|
|
V2.append({3, 2});
|
|
|
|
EXPECT_TRUE(makeArrayRef(V2).equals({4, 3, 2}));
|
|
|
|
V2.insert(V2.begin() + 1, 5);
|
|
|
|
EXPECT_TRUE(makeArrayRef(V2).equals({4, 5, 3, 2}));
|
|
|
|
}
|
|
|
|
|
2020-11-14 07:29:32 +08:00
|
|
|
template <class VectorT>
|
|
|
|
class SmallVectorReferenceInvalidationTest : public SmallVectorTestBase {
|
|
|
|
protected:
|
|
|
|
const char *AssertionMessage =
|
|
|
|
"Attempting to reference an element of the vector in an operation \" "
|
|
|
|
"\"that invalidates it";
|
|
|
|
|
|
|
|
VectorT V;
|
|
|
|
|
|
|
|
template <typename T, unsigned N>
|
|
|
|
static unsigned NumBuiltinElts(const SmallVector<T, N> &) {
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetUp() override {
|
|
|
|
SmallVectorTestBase::SetUp();
|
|
|
|
|
|
|
|
// Fill up the small size so that insertions move the elements.
|
|
|
|
V.append({0, 0, 0});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test one type that's trivially copyable (int) and one that isn't
|
|
|
|
// (Constructable) since reference invalidation may be fixed differently for
|
|
|
|
// each.
|
|
|
|
using SmallVectorReferenceInvalidationTestTypes =
|
|
|
|
::testing::Types<SmallVector<int, 3>, SmallVector<Constructable, 3>>;
|
|
|
|
|
|
|
|
TYPED_TEST_CASE(SmallVectorReferenceInvalidationTest,
|
|
|
|
SmallVectorReferenceInvalidationTestTypes);
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, PushBack) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.push_back(V.back()), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, PushBackMoved) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.push_back(std::move(V.back())), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, Resize) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.resize(2, V.back()), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, Append) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.append(1, V.back()), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, AppendRange) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.append(V.begin(), V.begin() + 1), this->AssertionMessage);
|
|
|
|
|
|
|
|
ASSERT_EQ(3u, this->NumBuiltinElts(V));
|
|
|
|
ASSERT_EQ(3u, V.size());
|
|
|
|
V.pop_back();
|
|
|
|
ASSERT_EQ(2u, V.size());
|
|
|
|
|
|
|
|
// Confirm this checks for growth when there's more than one element
|
|
|
|
// appended.
|
|
|
|
EXPECT_DEATH(V.append(V.begin(), V.end()), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, Assign) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
// Regardless of capacity, assign should never reference an internal element.
|
|
|
|
EXPECT_DEATH(V.assign(1, V.back()), this->AssertionMessage);
|
|
|
|
EXPECT_DEATH(V.assign(this->NumBuiltinElts(V), V.back()),
|
|
|
|
this->AssertionMessage);
|
|
|
|
EXPECT_DEATH(V.assign(this->NumBuiltinElts(V) + 1, V.back()),
|
|
|
|
this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, AssignRange) {
|
|
|
|
auto &V = this->V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.assign(V.begin(), V.end()), this->AssertionMessage);
|
|
|
|
EXPECT_DEATH(V.assign(V.begin(), V.end() - 1), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
V.assign(V.begin(), V.begin());
|
|
|
|
EXPECT_TRUE(V.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, Insert) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.insert(V.begin(), V.back()), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, InsertMoved) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.insert(V.begin(), std::move(V.back())),
|
|
|
|
this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, InsertN) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.insert(V.begin(), 2, V.back()), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, InsertRange) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.insert(V.begin(), V.begin(), V.begin() + 1),
|
|
|
|
this->AssertionMessage);
|
|
|
|
|
|
|
|
ASSERT_EQ(3u, this->NumBuiltinElts(V));
|
|
|
|
ASSERT_EQ(3u, V.size());
|
|
|
|
V.pop_back();
|
|
|
|
ASSERT_EQ(2u, V.size());
|
|
|
|
|
|
|
|
// Confirm this checks for growth when there's more than one element
|
|
|
|
// inserted.
|
|
|
|
EXPECT_DEATH(V.insert(V.begin(), V.begin(), V.end()), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorReferenceInvalidationTest, EmplaceBack) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.emplace_back(V.back()), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class VectorT>
|
|
|
|
class SmallVectorInternalReferenceInvalidationTest
|
|
|
|
: public SmallVectorTestBase {
|
|
|
|
protected:
|
|
|
|
const char *AssertionMessage =
|
|
|
|
"Attempting to reference an element of the vector in an operation \" "
|
|
|
|
"\"that invalidates it";
|
|
|
|
|
|
|
|
VectorT V;
|
|
|
|
|
|
|
|
template <typename T, unsigned N>
|
|
|
|
static unsigned NumBuiltinElts(const SmallVector<T, N> &) {
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetUp() override {
|
|
|
|
SmallVectorTestBase::SetUp();
|
|
|
|
|
|
|
|
// Fill up the small size so that insertions move the elements.
|
|
|
|
V.push_back(std::make_pair(0, 0));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test pairs of the same types from SmallVectorReferenceInvalidationTestTypes.
|
|
|
|
using SmallVectorInternalReferenceInvalidationTestTypes =
|
|
|
|
::testing::Types<SmallVector<std::pair<int, int>, 1>,
|
|
|
|
SmallVector<std::pair<Constructable, Constructable>, 1>>;
|
|
|
|
|
|
|
|
TYPED_TEST_CASE(SmallVectorInternalReferenceInvalidationTest,
|
|
|
|
SmallVectorInternalReferenceInvalidationTestTypes);
|
|
|
|
|
|
|
|
TYPED_TEST(SmallVectorInternalReferenceInvalidationTest, EmplaceBack) {
|
|
|
|
auto &V = this->V;
|
|
|
|
(void)V;
|
|
|
|
#if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
|
|
|
|
EXPECT_DEATH(V.emplace_back(V.back().first, 0), this->AssertionMessage);
|
|
|
|
EXPECT_DEATH(V.emplace_back(0, V.back().second), this->AssertionMessage);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-04-06 04:45:04 +08:00
|
|
|
} // end namespace
|