From 369781b6237dc7058ac50febd3f15592c11836cc Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 3 Jun 2017 00:27:52 +0300 Subject: [PATCH] Add test/variant_subset --- include/boost/variant2/expected.hpp | 4 +- include/boost/variant2/variant.hpp | 8 +- test/Jamfile | 1 + test/variant_subset.cpp | 155 ++++++++++++++++++++++++++++ 4 files changed, 162 insertions(+), 6 deletions(-) create mode 100644 test/variant_subset.cpp diff --git a/include/boost/variant2/expected.hpp b/include/boost/variant2/expected.hpp index e490625..87022e8 100644 --- a/include/boost/variant2/expected.hpp +++ b/include/boost/variant2/expected.hpp @@ -370,7 +370,7 @@ public: return mp_with_index>( v_.index(), [&]( auto I ) { - return _remap_error( I, f, get(v_) ); + return this->_remap_error( I, f, get(v_) ); }); } @@ -383,7 +383,7 @@ public: return mp_with_index>( v_.index(), [&]( auto I ) { - return _remap_error( I, f, get(v_) ); + return this->_remap_error( I, f, get(v_) ); }); } diff --git a/include/boost/variant2/variant.hpp b/include/boost/variant2/variant.hpp index 7e78a6a..4a0937c 100644 --- a/include/boost/variant2/variant.hpp +++ b/include/boost/variant2/variant.hpp @@ -1064,7 +1064,7 @@ public: using J = mp_find, mp_at_c, I>>; - return _subset_impl( J{}, this->_get_impl( I ) ); + return this->_subset_impl( J{}, this->_get_impl( I ) ); }); } @@ -1077,7 +1077,7 @@ public: using J = mp_find, mp_at_c, I>>; - return _subset_impl( J{}, this->_get_impl( I ) ); + return this->_subset_impl( J{}, this->_get_impl( I ) ); }); } @@ -1090,7 +1090,7 @@ public: using J = mp_find, mp_at_c, I>>; - return _subset_impl( J{}, std::move( this->_get_impl( I ) ) ); + return this->_subset_impl( J{}, std::move( this->_get_impl( I ) ) ); }); } @@ -1103,7 +1103,7 @@ public: using J = mp_find, mp_at_c, I>>; - return _subset_impl( J{}, std::move( this->_get_impl( I ) ) ); + return this->_subset_impl( J{}, std::move( this->_get_impl( I ) ) ); }); } diff --git a/test/Jamfile b/test/Jamfile index 8c8076b..c11eef5 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -40,3 +40,4 @@ run variant_destroy.cpp : : : $(REQ) ; run variant_visit.cpp : : : $(REQ) ; run variant_lt_gt.cpp : : : $(REQ) ; run variant_convert_construct.cpp : : : $(REQ) ; +run variant_subset.cpp : : : $(REQ) ; diff --git a/test/variant_subset.cpp b/test/variant_subset.cpp new file mode 100644 index 0000000..86cd505 --- /dev/null +++ b/test/variant_subset.cpp @@ -0,0 +1,155 @@ + +// Copyright 2017 Peter Dimov. +// +// Distributed under the Boost Software License, Version 1.0. +// +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include +#include + +using namespace boost::variant2; + +#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) + +struct X1 +{ + int v; + + X1(): v(0) {} + explicit X1(int v): v(v) {} + X1(X1 const& r): v(r.v) {} + X1(X1&& r): v(r.v) {} + X1& operator=( X1 const& r ) { v = r.v; return *this; } + X1& operator=( X1&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X1 const& a, X1 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +struct X2 +{ + int v; + + X2(): v(0) {} + explicit X2(int v): v(v) {} + X2(X2 const& r): v(r.v) {} + X2(X2&& r): v(r.v) {} + X2& operator=( X2 const& r ) { v = r.v; return *this; } + X2& operator=( X2&& r ) { v = r.v; return *this; } +}; + +inline bool operator==( X2 const& a, X2 const& b ) { return a.v == b.v; } + +STATIC_ASSERT( !std::is_nothrow_default_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_move_constructible::value ); +STATIC_ASSERT( !std::is_nothrow_copy_assignable::value ); +STATIC_ASSERT( !std::is_nothrow_move_assignable::value ); + +int main() +{ + { + variant v1( 1 ); + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ), get( v2 ) ); + + BOOST_TEST_THROWS( v1.subset(), bad_variant_access ); + + variant v3 = std::move(v1).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + + BOOST_TEST_THROWS( std::move(v1).subset(), bad_variant_access ); + } + + { + variant const v1( 1 ); + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ), get( v2 ) ); + + BOOST_TEST_THROWS( v1.subset(), bad_variant_access ); + + variant v3 = std::move(v1).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + + BOOST_TEST_THROWS( std::move(v1).subset(), bad_variant_access ); + } + + { + variant v1( 1 ); + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ), get( v2 ) ); + + variant v3 = std::move(v1).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v1( 1 ); + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ), get( v2 ) ); + + variant v3 = std::move(v1).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v1( "s1" ); + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ), get( v2 ) ); + + variant v3 = std::move(v1).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ), get( v3 ) ); + } + + { + variant v1{ X1{1} }; + + variant v2 = v1.subset(); + + BOOST_TEST( holds_alternative( v2 ) ); + BOOST_TEST_EQ( get( v1 ).v, get( v2 ).v ); + + variant v3 = std::move( v1 ).subset(); + + BOOST_TEST( holds_alternative( v3 ) ); + BOOST_TEST_EQ( get( v2 ).v, get( v3 ).v ); + } + + return boost::report_errors(); +}