Add tests for libc++'s constexpr variant copy/move extension

llvm-svn: 288536
This commit is contained in:
Eric Fiselier 2016-12-02 21:17:51 +00:00
parent 9ee6003146
commit 96be8df23e
4 changed files with 93 additions and 0 deletions

View File

@ -385,6 +385,29 @@ void test_copy_assignment_different_index() {
#endif
}
template <size_t NewIdx, class ValueType>
constexpr bool test_constexpr_assign_extension_imp(
std::variant<long, void*, const int>&& v, ValueType&& new_value)
{
const std::variant<long, void*, const int> cp(
std::forward<ValueType>(new_value));
v = cp;
return v.index() == NewIdx &&
std::get<NewIdx>(v) == std::get<NewIdx>(cp);
}
void test_constexpr_move_ctor_extension() {
#ifdef _LIBCPP_VERSION
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), "");
#endif
}
int main() {
test_copy_assignment_empty_empty();
test_copy_assignment_non_empty_empty();
@ -393,4 +416,5 @@ int main() {
test_copy_assignment_different_index();
test_copy_assignment_sfinae();
test_copy_assignment_not_noexcept();
test_constexpr_copy_assignment_extension();
}

View File

@ -308,6 +308,29 @@ void test_move_assignment_different_index() {
#endif
}
template <size_t NewIdx, class ValueType>
constexpr bool test_constexpr_assign_extension_imp(
std::variant<long, void*, const int>&& v, ValueType&& new_value)
{
std::variant<long, void*, const int> v2(
std::forward<ValueType>(new_value));
const auto cp = v2;
v = std::move(v2);
return v.index() == NewIdx &&
std::get<NewIdx>(v) == std::get<NewIdx>(cp);
}
void test_constexpr_move_ctor_extension() {
#ifdef _LIBCPP_VERSION
using V = std::variant<long, void*, int>;
static_assert(std::is_trivially_copyable<V>::value, "");
static_assert(std::is_trivially_move_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), "");
#endif
}
int main() {
test_move_assignment_empty_empty();
test_move_assignment_non_empty_empty();
@ -316,4 +339,5 @@ int main() {
test_move_assignment_different_index();
test_move_assignment_sfinae();
test_move_assignment_noexcept();
test_constexpr_move_assignment_extension();
}

View File

@ -130,8 +130,30 @@ void test_copy_ctor_valueless_by_exception() {
#endif
}
template <size_t Idx>
constexpr bool test_constexpr_copy_ctor_extension_imp(
std::variant<long, void*, const int> const& v)
{
auto v2 = v;
return v2.index() == v.index() &&
v2.index() == Idx &&
std::get<Idx>(v2) == std::get<Idx>(v);
}
void test_constexpr_copy_ctor_extension() {
#ifdef _LIBCPP_VERSION
using V = std::variant<long, void*, const int>;
static_assert(std::is_trivially_copyable<V>::value, "");
static_assert(std::is_trivially_copy_constructible<V>::value, "");
static_assert(test_constexpr_copy_ctor_extension_imp<0>(V(42l)), "");
static_assert(test_constexpr_copy_ctor_extension_imp<1>(V(nullptr)), "");
static_assert(test_constexpr_copy_ctor_extension_imp<2>(V(101)), "");
#endif
}
int main() {
test_copy_ctor_basic();
test_copy_ctor_valueless_by_exception();
test_copy_ctor_sfinae();
test_constexpr_copy_ctor_extension();
}

View File

@ -166,9 +166,32 @@ void test_move_ctor_valueless_by_exception() {
#endif
}
template <size_t Idx>
constexpr bool test_constexpr_ctor_extension_imp(
std::variant<long, void*, const int> const& v)
{
auto copy = v;
auto v2 = std::move(copy);
return v2.index() == v.index() &&
v2.index() == Idx &&
std::get<Idx>(v2) == std::get<Idx>(v);
}
void test_constexpr_move_ctor_extension() {
#ifdef _LIBCPP_VERSION
using V = std::variant<long, void*, const int>;
static_assert(std::is_trivially_copyable<V>::value, "");
static_assert(std::is_trivially_move_constructible<V>::value, "");
static_assert(test_constexpr_ctor_extension_imp<0>(V(42l)), "");
static_assert(test_constexpr_ctor_extension_imp<1>(V(nullptr)), "");
static_assert(test_constexpr_ctor_extension_imp<2>(V(101)), "");
#endif
}
int main() {
test_move_ctor_basic();
test_move_ctor_valueless_by_exception();
test_move_noexcept();
test_move_ctor_sfinae();
test_constexpr_move_ctor_extension();
}