Changeset View
Changeset View
Standalone View
Standalone View
llvm/trunk/unittests/ADT/OptionalTest.cpp
Show All 10 Lines | |||||
#include "llvm/Support/raw_ostream.h" | #include "llvm/Support/raw_ostream.h" | ||||
#include "gtest/gtest-spi.h" | #include "gtest/gtest-spi.h" | ||||
#include "gtest/gtest.h" | #include "gtest/gtest.h" | ||||
using namespace llvm; | using namespace llvm; | ||||
namespace { | namespace { | ||||
static_assert(llvm::is_trivially_copyable<Optional<int>>::value, | |||||
"trivially copyable"); | |||||
static_assert(llvm::is_trivially_copyable<Optional<std::array<int, 3>>>::value, | |||||
"trivially copyable"); | |||||
struct NonDefaultConstructible { | struct NonDefaultConstructible { | ||||
static unsigned CopyConstructions; | static unsigned CopyConstructions; | ||||
static unsigned Destructions; | static unsigned Destructions; | ||||
static unsigned CopyAssignments; | static unsigned CopyAssignments; | ||||
explicit NonDefaultConstructible(int) { | explicit NonDefaultConstructible(int) { | ||||
} | } | ||||
NonDefaultConstructible(const NonDefaultConstructible&) { | NonDefaultConstructible(const NonDefaultConstructible&) { | ||||
++CopyConstructions; | ++CopyConstructions; | ||||
Show All 11 Lines | static void ResetCounts() { | ||||
CopyAssignments = 0; | CopyAssignments = 0; | ||||
} | } | ||||
}; | }; | ||||
unsigned NonDefaultConstructible::CopyConstructions = 0; | unsigned NonDefaultConstructible::CopyConstructions = 0; | ||||
unsigned NonDefaultConstructible::Destructions = 0; | unsigned NonDefaultConstructible::Destructions = 0; | ||||
unsigned NonDefaultConstructible::CopyAssignments = 0; | unsigned NonDefaultConstructible::CopyAssignments = 0; | ||||
static_assert( | |||||
!llvm::is_trivially_copyable<Optional<NonDefaultConstructible>>::value, | |||||
"not trivially copyable"); | |||||
// Test fixture | // Test fixture | ||||
class OptionalTest : public testing::Test { | class OptionalTest : public testing::Test { | ||||
}; | }; | ||||
TEST_F(OptionalTest, NonDefaultConstructibleTest) { | TEST_F(OptionalTest, NonDefaultConstructibleTest) { | ||||
Optional<NonDefaultConstructible> O; | Optional<NonDefaultConstructible> O; | ||||
EXPECT_FALSE(O); | EXPECT_FALSE(O); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | ~MultiArgConstructor() { | ||||
++Destructions; | ++Destructions; | ||||
} | } | ||||
static void ResetCounts() { | static void ResetCounts() { | ||||
Destructions = 0; | Destructions = 0; | ||||
} | } | ||||
}; | }; | ||||
unsigned MultiArgConstructor::Destructions = 0; | unsigned MultiArgConstructor::Destructions = 0; | ||||
static_assert( | |||||
!llvm::is_trivially_copyable<Optional<MultiArgConstructor>>::value, | |||||
"not trivially copyable"); | |||||
TEST_F(OptionalTest, Emplace) { | TEST_F(OptionalTest, Emplace) { | ||||
MultiArgConstructor::ResetCounts(); | MultiArgConstructor::ResetCounts(); | ||||
Optional<MultiArgConstructor> A; | Optional<MultiArgConstructor> A; | ||||
A.emplace(1, 2); | A.emplace(1, 2); | ||||
EXPECT_TRUE(A.hasValue()); | EXPECT_TRUE(A.hasValue()); | ||||
EXPECT_EQ(1, A->x); | EXPECT_EQ(1, A->x); | ||||
EXPECT_EQ(2, A->y); | EXPECT_EQ(2, A->y); | ||||
Show All 31 Lines | static void ResetCounts() { | ||||
MoveAssignments = 0; | MoveAssignments = 0; | ||||
} | } | ||||
}; | }; | ||||
unsigned MoveOnly::MoveConstructions = 0; | unsigned MoveOnly::MoveConstructions = 0; | ||||
unsigned MoveOnly::Destructions = 0; | unsigned MoveOnly::Destructions = 0; | ||||
unsigned MoveOnly::MoveAssignments = 0; | unsigned MoveOnly::MoveAssignments = 0; | ||||
static_assert(!llvm::is_trivially_copyable<Optional<MoveOnly>>::value, | |||||
"not trivially copyable"); | |||||
TEST_F(OptionalTest, MoveOnlyNull) { | TEST_F(OptionalTest, MoveOnlyNull) { | ||||
MoveOnly::ResetCounts(); | MoveOnly::ResetCounts(); | ||||
Optional<MoveOnly> O; | Optional<MoveOnly> O; | ||||
EXPECT_EQ(0u, MoveOnly::MoveConstructions); | EXPECT_EQ(0u, MoveOnly::MoveConstructions); | ||||
EXPECT_EQ(0u, MoveOnly::MoveAssignments); | EXPECT_EQ(0u, MoveOnly::MoveAssignments); | ||||
EXPECT_EQ(0u, MoveOnly::Destructions); | EXPECT_EQ(0u, MoveOnly::Destructions); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 85 Lines • ▼ Show 20 Lines | |||||
private: | private: | ||||
// This should disable all move/copy operations. | // This should disable all move/copy operations. | ||||
Immovable(Immovable&& other) = delete; | Immovable(Immovable&& other) = delete; | ||||
}; | }; | ||||
unsigned Immovable::Constructions = 0; | unsigned Immovable::Constructions = 0; | ||||
unsigned Immovable::Destructions = 0; | unsigned Immovable::Destructions = 0; | ||||
static_assert(!llvm::is_trivially_copyable<Optional<Immovable>>::value, | |||||
"not trivially copyable"); | |||||
TEST_F(OptionalTest, ImmovableEmplace) { | TEST_F(OptionalTest, ImmovableEmplace) { | ||||
Optional<Immovable> A; | Optional<Immovable> A; | ||||
Immovable::ResetCounts(); | Immovable::ResetCounts(); | ||||
A.emplace(4); | A.emplace(4); | ||||
EXPECT_TRUE((bool)A); | EXPECT_TRUE((bool)A); | ||||
EXPECT_EQ(4, A->val); | EXPECT_EQ(4, A->val); | ||||
EXPECT_EQ(1u, Immovable::Constructions); | EXPECT_EQ(1u, Immovable::Constructions); | ||||
EXPECT_EQ(0u, Immovable::Destructions); | EXPECT_EQ(0u, Immovable::Destructions); | ||||
▲ Show 20 Lines • Show All 212 Lines • Show Last 20 Lines |