Add test/variant_in_place_type_construct(_cx)

This commit is contained in:
Peter Dimov
2017-05-30 17:12:26 +03:00
parent 2357f4d2f4
commit 21acb2bda0
3 changed files with 228 additions and 0 deletions

View File

@ -27,3 +27,5 @@ run variant_value_construct.cpp : : : $(REQ) ;
compile variant_value_construct_cx.cpp : : : $(REQ) ;
run variant_in_place_index_construct.cpp : : : $(REQ) ;
compile variant_in_place_index_construct_cx.cpp : : : $(REQ) ;
run variant_in_place_type_construct.cpp : : : $(REQ) ;
compile variant_in_place_type_construct_cx.cpp : : : $(REQ) ;

View File

@ -0,0 +1,116 @@
// 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 <boost/variant2/variant.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_trait.hpp>
#include <type_traits>
#include <utility>
#include <string>
using namespace boost::variant2;
struct X
{
X() = default;
template<class T> X( in_place_type_t<T> ) = delete;
};
int main()
{
{
variant<int> v( in_place_type<int> );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST( holds_alternative<int>(v) );
}
{
variant<X> v( in_place_type<X> );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST( holds_alternative<X>(v) );
}
{
variant<int> v( in_place_type<int>, 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST( holds_alternative<int>(v) );
}
{
variant<int, float> v( in_place_type<int> );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST( holds_alternative<int>(v) );
}
{
variant<int, float> v( in_place_type<int>, 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST( holds_alternative<int>(v) );
}
{
variant<int, float> v( in_place_type<float> );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 0 );
BOOST_TEST( holds_alternative<float>(v) );
}
{
variant<int, float> v( in_place_type<float>, 3.14f );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
BOOST_TEST( holds_alternative<float>(v) );
}
{
variant<int, int, float, std::string> v( in_place_type<float>, 3.14f );
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
BOOST_TEST( holds_alternative<float>(v) );
}
{
variant<int, int, float, float, std::string> v( in_place_type<std::string>, "text" );
BOOST_TEST_EQ( v.index(), 4 );
BOOST_TEST_EQ( get<4>(v), std::string("text") );
BOOST_TEST( holds_alternative<std::string>(v) );
}
{
variant<int, int, float, float, std::string> v( in_place_type<std::string>, 4, 'a' );
BOOST_TEST_EQ( v.index(), 4 );
BOOST_TEST_EQ( get<4>(v), std::string( 4, 'a' ) );
BOOST_TEST( holds_alternative<std::string>(v) );
}
return boost::report_errors();
}

View File

@ -0,0 +1,110 @@
// 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 <boost/variant2/variant.hpp>
using namespace boost::variant2;
struct X
{
constexpr X() = default;
constexpr explicit X(int, int) {}
template<class T> X( in_place_type_t<T> ) = delete;
};
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
int main()
{
{
constexpr variant<int> v( in_place_type<int> );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
STATIC_ASSERT( holds_alternative<int>(v) );
}
{
constexpr variant<X> v( in_place_type<X> );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( holds_alternative<X>(v) );
}
{
constexpr variant<int> v( in_place_type<int>, 1 );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 1 );
STATIC_ASSERT( holds_alternative<int>(v) );
}
{
constexpr variant<int, float> v( in_place_type<int> );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 0 );
STATIC_ASSERT( holds_alternative<int>(v) );
}
{
constexpr variant<int, float> v( in_place_type<int>, 1 );
STATIC_ASSERT( v.index() == 0 );
STATIC_ASSERT( get<0>(v) == 1 );
STATIC_ASSERT( holds_alternative<int>(v) );
}
{
constexpr variant<int, float> v( in_place_type<float> );
STATIC_ASSERT( v.index() == 1 );
STATIC_ASSERT( get<1>(v) == 0 );
STATIC_ASSERT( holds_alternative<float>(v) );
}
{
constexpr variant<int, float> v( in_place_type<float>, 3.14f );
STATIC_ASSERT( v.index() == 1 );
STATIC_ASSERT( get<1>(v) == 3.14f );
STATIC_ASSERT( holds_alternative<float>(v) );
}
{
constexpr variant<int, int, float, X> v( in_place_type<float>, 3.14f );
STATIC_ASSERT( v.index() == 2 );
STATIC_ASSERT( get<2>(v) == 3.14f );
STATIC_ASSERT( holds_alternative<float>(v) );
}
{
constexpr variant<int, int, float, float, X> v( in_place_type<X> );
STATIC_ASSERT( v.index() == 4 );
STATIC_ASSERT( holds_alternative<X>(v) );
}
{
constexpr variant<int, int, float, float, X> v( in_place_type<X>, 0, 0 );
STATIC_ASSERT( v.index() == 4 );
STATIC_ASSERT( holds_alternative<X>(v) );
}
}