[test] Test changes to accommodate LWG 2904 "Make variant move-assignment more exception safe"
Also: Move constexpr / triviality extension tests into the std tree and make them conditional on _LIBCPP_VERSION / _MSVC_STL_VERSION. https://reviews.llvm.org/D32671 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@304847 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -10,6 +10,10 @@
|
||||
|
||||
// UNSUPPORTED: c++98, c++03, c++11, c++14
|
||||
|
||||
// The following compilers don't generate constexpr special members correctly.
|
||||
// XFAIL: clang-3.5, clang-3.6, clang-3.7, clang-3.8
|
||||
// XFAIL: apple-clang-6, apple-clang-7, apple-clang-8.0
|
||||
|
||||
// XFAIL: with_system_cxx_lib=macosx10.12
|
||||
// XFAIL: with_system_cxx_lib=macosx10.11
|
||||
// XFAIL: with_system_cxx_lib=macosx10.10
|
||||
@@ -35,11 +39,6 @@ struct NoCopy {
|
||||
NoCopy &operator=(const NoCopy &) = default;
|
||||
};
|
||||
|
||||
struct NothrowCopy {
|
||||
NothrowCopy(const NothrowCopy &) noexcept = default;
|
||||
NothrowCopy &operator=(const NothrowCopy &) noexcept = default;
|
||||
};
|
||||
|
||||
struct CopyOnly {
|
||||
CopyOnly(const CopyOnly &) = default;
|
||||
CopyOnly(CopyOnly &&) = delete;
|
||||
@@ -73,7 +72,7 @@ struct CopyAssign {
|
||||
++alive;
|
||||
++copy_construct;
|
||||
}
|
||||
CopyAssign(CopyAssign &&o) : value(o.value) {
|
||||
CopyAssign(CopyAssign &&o) noexcept : value(o.value) {
|
||||
o.value = -1;
|
||||
++alive;
|
||||
++move_construct;
|
||||
@@ -83,7 +82,7 @@ struct CopyAssign {
|
||||
++copy_assign;
|
||||
return *this;
|
||||
}
|
||||
CopyAssign &operator=(CopyAssign &&o) {
|
||||
CopyAssign &operator=(CopyAssign &&o) noexcept {
|
||||
value = o.value;
|
||||
o.value = -1;
|
||||
++move_assign;
|
||||
@@ -115,6 +114,17 @@ struct CopyThrows {
|
||||
CopyThrows &operator=(const CopyThrows &) { throw 42; }
|
||||
};
|
||||
|
||||
struct CopyCannotThrow {
|
||||
static int alive;
|
||||
CopyCannotThrow() { ++alive; }
|
||||
CopyCannotThrow(const CopyCannotThrow &) noexcept { ++alive; }
|
||||
CopyCannotThrow(CopyCannotThrow &&) noexcept { assert(false); }
|
||||
CopyCannotThrow &operator=(const CopyCannotThrow &) noexcept = default;
|
||||
CopyCannotThrow &operator=(CopyCannotThrow &&) noexcept { assert(false); return *this; }
|
||||
};
|
||||
|
||||
int CopyCannotThrow::alive = 0;
|
||||
|
||||
struct MoveThrows {
|
||||
static int alive;
|
||||
MoveThrows() { ++alive; }
|
||||
@@ -127,6 +137,47 @@ struct MoveThrows {
|
||||
|
||||
int MoveThrows::alive = 0;
|
||||
|
||||
struct NTCopyAssign {
|
||||
constexpr NTCopyAssign(int v) : value(v) {}
|
||||
NTCopyAssign(const NTCopyAssign &) = default;
|
||||
NTCopyAssign(NTCopyAssign &&) = default;
|
||||
NTCopyAssign &operator=(const NTCopyAssign &that) {
|
||||
value = that.value;
|
||||
return *this;
|
||||
};
|
||||
NTCopyAssign &operator=(NTCopyAssign &&) = delete;
|
||||
int value;
|
||||
};
|
||||
|
||||
static_assert(!std::is_trivially_copy_assignable<NTCopyAssign>::value, "");
|
||||
static_assert(std::is_copy_assignable<NTCopyAssign>::value, "");
|
||||
|
||||
struct TCopyAssign {
|
||||
constexpr TCopyAssign(int v) : value(v) {}
|
||||
TCopyAssign(const TCopyAssign &) = default;
|
||||
TCopyAssign(TCopyAssign &&) = default;
|
||||
TCopyAssign &operator=(const TCopyAssign &) = default;
|
||||
TCopyAssign &operator=(TCopyAssign &&) = delete;
|
||||
int value;
|
||||
};
|
||||
|
||||
static_assert(std::is_trivially_copy_assignable<TCopyAssign>::value, "");
|
||||
|
||||
struct TCopyAssignNTMoveAssign {
|
||||
constexpr TCopyAssignNTMoveAssign(int v) : value(v) {}
|
||||
TCopyAssignNTMoveAssign(const TCopyAssignNTMoveAssign &) = default;
|
||||
TCopyAssignNTMoveAssign(TCopyAssignNTMoveAssign &&) = default;
|
||||
TCopyAssignNTMoveAssign &operator=(const TCopyAssignNTMoveAssign &) = default;
|
||||
TCopyAssignNTMoveAssign &operator=(TCopyAssignNTMoveAssign &&that) {
|
||||
value = that.value;
|
||||
that.value = -1;
|
||||
return *this;
|
||||
}
|
||||
int value;
|
||||
};
|
||||
|
||||
static_assert(std::is_trivially_copy_assignable_v<TCopyAssignNTMoveAssign>, "");
|
||||
|
||||
struct MakeEmptyT {
|
||||
static int alive;
|
||||
MakeEmptyT() { ++alive; }
|
||||
@@ -146,7 +197,7 @@ int MakeEmptyT::alive = 0;
|
||||
template <class Variant> void makeEmpty(Variant &v) {
|
||||
Variant v2(std::in_place_type<MakeEmptyT>);
|
||||
try {
|
||||
v = v2;
|
||||
v = std::move(v2);
|
||||
assert(false);
|
||||
} catch (...) {
|
||||
assert(v.valueless_by_exception());
|
||||
@@ -171,10 +222,14 @@ void test_copy_assignment_sfinae() {
|
||||
static_assert(std::is_copy_assignable<V>::value, "");
|
||||
}
|
||||
{
|
||||
using V = std::variant<int, CopyOnly>;
|
||||
#ifdef _LIBCPP_VERSION // LWG2904
|
||||
// variant only provides copy assignment when both the copy and move
|
||||
// constructors are well formed
|
||||
using V = std::variant<int, CopyOnly>;
|
||||
static_assert(!std::is_copy_assignable<V>::value, "");
|
||||
#else // _LIBCPP_VERSION // LWG2904
|
||||
static_assert(std::is_copy_assignable<V>::value, "");
|
||||
#endif // _LIBCPP_VERSION // LWG2904
|
||||
}
|
||||
{
|
||||
using V = std::variant<int, NoCopy>;
|
||||
@@ -188,6 +243,31 @@ void test_copy_assignment_sfinae() {
|
||||
using V = std::variant<int, MoveOnlyNT>;
|
||||
static_assert(!std::is_copy_assignable<V>::value, "");
|
||||
}
|
||||
|
||||
// The following tests are for not-yet-standardized behavior (P0602):
|
||||
{
|
||||
using V = std::variant<int, long>;
|
||||
static_assert(std::is_trivially_copy_assignable<V>::value, "");
|
||||
}
|
||||
{
|
||||
using V = std::variant<int, NTCopyAssign>;
|
||||
static_assert(!std::is_trivially_copy_assignable<V>::value, "");
|
||||
static_assert(std::is_copy_assignable<V>::value, "");
|
||||
}
|
||||
{
|
||||
using V = std::variant<int, TCopyAssign>;
|
||||
static_assert(std::is_trivially_copy_assignable<V>::value, "");
|
||||
}
|
||||
{
|
||||
using V = std::variant<int, TCopyAssignNTMoveAssign>;
|
||||
static_assert(std::is_trivially_copy_assignable<V>::value, "");
|
||||
}
|
||||
#ifndef _LIBCPP_VERSION // LWG2904
|
||||
{
|
||||
using V = std::variant<int, CopyOnly>;
|
||||
static_assert(std::is_trivially_copy_assignable<V>::value, "");
|
||||
}
|
||||
#endif // _LIBCPP_VERSION
|
||||
}
|
||||
|
||||
void test_copy_assignment_empty_empty() {
|
||||
@@ -204,7 +284,7 @@ void test_copy_assignment_empty_empty() {
|
||||
assert(v1.valueless_by_exception());
|
||||
assert(v1.index() == std::variant_npos);
|
||||
}
|
||||
#endif
|
||||
#endif // TEST_HAS_NO_EXCEPTIONS
|
||||
}
|
||||
|
||||
void test_copy_assignment_non_empty_empty() {
|
||||
@@ -230,7 +310,7 @@ void test_copy_assignment_non_empty_empty() {
|
||||
assert(v1.valueless_by_exception());
|
||||
assert(v1.index() == std::variant_npos);
|
||||
}
|
||||
#endif
|
||||
#endif // TEST_HAS_NO_EXCEPTIONS
|
||||
}
|
||||
|
||||
void test_copy_assignment_empty_non_empty() {
|
||||
@@ -256,9 +336,11 @@ void test_copy_assignment_empty_non_empty() {
|
||||
assert(v1.index() == 2);
|
||||
assert(std::get<2>(v1) == "hello");
|
||||
}
|
||||
#endif
|
||||
#endif // TEST_HAS_NO_EXCEPTIONS
|
||||
}
|
||||
|
||||
template <typename T> struct Result { size_t index; T value; };
|
||||
|
||||
void test_copy_assignment_same_index() {
|
||||
{
|
||||
using V = std::variant<int>;
|
||||
@@ -306,7 +388,65 @@ void test_copy_assignment_same_index() {
|
||||
assert(v1.index() == 1);
|
||||
assert(&std::get<1>(v1) == &mref);
|
||||
}
|
||||
#endif
|
||||
#endif // TEST_HAS_NO_EXCEPTIONS
|
||||
|
||||
// The following tests are for not-yet-standardized behavior (P0602):
|
||||
{
|
||||
struct {
|
||||
constexpr Result<int> operator()() const {
|
||||
using V = std::variant<int>;
|
||||
V v(43);
|
||||
V v2(42);
|
||||
v = v2;
|
||||
return {v.index(), std::get<0>(v)};
|
||||
}
|
||||
} test;
|
||||
constexpr auto result = test();
|
||||
static_assert(result.index == 0, "");
|
||||
static_assert(result.value == 42, "");
|
||||
}
|
||||
{
|
||||
struct {
|
||||
constexpr Result<long> operator()() const {
|
||||
using V = std::variant<int, long, unsigned>;
|
||||
V v(43l);
|
||||
V v2(42l);
|
||||
v = v2;
|
||||
return {v.index(), std::get<1>(v)};
|
||||
}
|
||||
} test;
|
||||
constexpr auto result = test();
|
||||
static_assert(result.index == 1, "");
|
||||
static_assert(result.value == 42l, "");
|
||||
}
|
||||
{
|
||||
struct {
|
||||
constexpr Result<int> operator()() const {
|
||||
using V = std::variant<int, TCopyAssign, unsigned>;
|
||||
V v(std::in_place_type<TCopyAssign>, 43);
|
||||
V v2(std::in_place_type<TCopyAssign>, 42);
|
||||
v = v2;
|
||||
return {v.index(), std::get<1>(v).value};
|
||||
}
|
||||
} test;
|
||||
constexpr auto result = test();
|
||||
static_assert(result.index == 1, "");
|
||||
static_assert(result.value == 42, "");
|
||||
}
|
||||
{
|
||||
struct {
|
||||
constexpr Result<int> operator()() const {
|
||||
using V = std::variant<int, TCopyAssignNTMoveAssign, unsigned>;
|
||||
V v(std::in_place_type<TCopyAssignNTMoveAssign>, 43);
|
||||
V v2(std::in_place_type<TCopyAssignNTMoveAssign>, 42);
|
||||
v = v2;
|
||||
return {v.index(), std::get<1>(v).value};
|
||||
}
|
||||
} test;
|
||||
constexpr auto result = test();
|
||||
static_assert(result.index == 1, "");
|
||||
static_assert(result.value == 42, "");
|
||||
}
|
||||
}
|
||||
|
||||
void test_copy_assignment_different_index() {
|
||||
@@ -338,8 +478,6 @@ void test_copy_assignment_different_index() {
|
||||
}
|
||||
#ifndef TEST_HAS_NO_EXCEPTIONS
|
||||
{
|
||||
// Test that if copy construction throws then original value is
|
||||
// unchanged.
|
||||
using V = std::variant<int, CopyThrows, std::string>;
|
||||
V v1(std::in_place_type<std::string>, "hello");
|
||||
V v2(std::in_place_type<CopyThrows>);
|
||||
@@ -348,16 +486,24 @@ void test_copy_assignment_different_index() {
|
||||
assert(false);
|
||||
} catch (...) { /* ... */
|
||||
}
|
||||
#ifdef _LIBCPP_VERSION // LWG2904
|
||||
// Test that if copy construction throws then original value is unchanged.
|
||||
assert(v1.index() == 2);
|
||||
assert(std::get<2>(v1) == "hello");
|
||||
#else // _LIBCPP_VERSION // LWG2904
|
||||
// Test that copy construction is used directly if move construction may throw,
|
||||
// resulting in a valueless variant if copy throws.
|
||||
assert(v1.valueless_by_exception());
|
||||
#endif // _LIBCPP_VERSION // LWG2904
|
||||
}
|
||||
{
|
||||
// Test that if move construction throws then the variant is left
|
||||
// valueless by exception.
|
||||
using V = std::variant<int, MoveThrows, std::string>;
|
||||
V v1(std::in_place_type<std::string>, "hello");
|
||||
V v2(std::in_place_type<MoveThrows>);
|
||||
assert(MoveThrows::alive == 1);
|
||||
#ifdef _LIBCPP_VERSION // LWG2904
|
||||
// Test that if move construction throws then the variant is left
|
||||
// valueless by exception.
|
||||
try {
|
||||
v1 = v2;
|
||||
assert(false);
|
||||
@@ -366,7 +512,27 @@ void test_copy_assignment_different_index() {
|
||||
assert(v1.valueless_by_exception());
|
||||
assert(v2.index() == 1);
|
||||
assert(MoveThrows::alive == 1);
|
||||
#else // _LIBCPP_VERSION // LWG2904
|
||||
// Test that copy construction is used directly if move construction may throw.
|
||||
v1 = v2;
|
||||
assert(v1.index() == 1);
|
||||
assert(v2.index() == 1);
|
||||
assert(MoveThrows::alive == 2);
|
||||
#endif // _LIBCPP_VERSION // LWG2904
|
||||
}
|
||||
#ifndef _LIBCPP_VERSION // LWG2904
|
||||
{
|
||||
// Test that direct copy construction is preferred when it cannot throw.
|
||||
using V = std::variant<int, CopyCannotThrow, std::string>;
|
||||
V v1(std::in_place_type<std::string>, "hello");
|
||||
V v2(std::in_place_type<CopyCannotThrow>);
|
||||
assert(CopyCannotThrow::alive == 1);
|
||||
v1 = v2;
|
||||
assert(v1.index() == 1);
|
||||
assert(v2.index() == 1);
|
||||
assert(CopyCannotThrow::alive == 2);
|
||||
}
|
||||
#endif // _LIBCPP_VERSION // LWG2904
|
||||
{
|
||||
using V = std::variant<int, CopyThrows, std::string>;
|
||||
V v1(std::in_place_type<CopyThrows>);
|
||||
@@ -389,9 +555,60 @@ void test_copy_assignment_different_index() {
|
||||
assert(v2.index() == 2);
|
||||
assert(std::get<2>(v2) == "hello");
|
||||
}
|
||||
#endif
|
||||
#endif // TEST_HAS_NO_EXCEPTIONS
|
||||
|
||||
// The following tests are for not-yet-standardized behavior (P0602):
|
||||
{
|
||||
struct {
|
||||
constexpr Result<long> operator()() const {
|
||||
using V = std::variant<int, long, unsigned>;
|
||||
V v(43);
|
||||
V v2(42l);
|
||||
v = v2;
|
||||
return {v.index(), std::get<1>(v)};
|
||||
}
|
||||
} test;
|
||||
constexpr auto result = test();
|
||||
static_assert(result.index == 1, "");
|
||||
static_assert(result.value == 42l, "");
|
||||
}
|
||||
{
|
||||
struct {
|
||||
constexpr Result<int> operator()() const {
|
||||
using V = std::variant<int, TCopyAssign, unsigned>;
|
||||
V v(std::in_place_type<unsigned>, 43);
|
||||
V v2(std::in_place_type<TCopyAssign>, 42);
|
||||
v = v2;
|
||||
return {v.index(), std::get<1>(v).value};
|
||||
}
|
||||
} test;
|
||||
constexpr auto result = test();
|
||||
static_assert(result.index == 1, "");
|
||||
static_assert(result.value == 42, "");
|
||||
}
|
||||
}
|
||||
|
||||
template <size_t NewIdx, class ValueType>
|
||||
constexpr bool test_constexpr_assign_extension_imp(
|
||||
std::variant<long, void*, int>&& v, ValueType&& new_value)
|
||||
{
|
||||
const std::variant<long, void*, int> cp(
|
||||
std::forward<ValueType>(new_value));
|
||||
v = cp;
|
||||
return v.index() == NewIdx &&
|
||||
std::get<NewIdx>(v) == std::get<NewIdx>(cp);
|
||||
}
|
||||
|
||||
void test_constexpr_copy_assignment_extension() {
|
||||
// The following tests are for not-yet-standardized behavior (P0602):
|
||||
using V = std::variant<long, void*, int>;
|
||||
static_assert(std::is_trivially_copyable<V>::value, "");
|
||||
static_assert(std::is_trivially_copy_assignable<V>::value, "");
|
||||
static_assert(test_constexpr_assign_extension_imp<0>(V(42l), 101l), "");
|
||||
static_assert(test_constexpr_assign_extension_imp<0>(V(nullptr), 101l), "");
|
||||
static_assert(test_constexpr_assign_extension_imp<1>(V(42l), nullptr), "");
|
||||
static_assert(test_constexpr_assign_extension_imp<2>(V(42l), 101), "");
|
||||
}
|
||||
|
||||
int main() {
|
||||
test_copy_assignment_empty_empty();
|
||||
@@ -401,4 +618,5 @@ int main() {
|
||||
test_copy_assignment_different_index();
|
||||
test_copy_assignment_sfinae();
|
||||
test_copy_assignment_not_noexcept();
|
||||
test_constexpr_copy_assignment_extension();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user