Add test/get_by_index.cpp

This commit is contained in:
Peter Dimov
2017-05-29 21:38:04 +03:00
parent 99747c7807
commit 781977a237
3 changed files with 189 additions and 40 deletions

View File

@ -114,61 +114,25 @@ template<class U, class... T> constexpr bool holds_alternative( variant<T...> co
template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>& get(variant<T...>& v)
{
static_assert( I < sizeof...(T), "Index out of bounds" );
#if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
return v.index() == I? v._get_impl( mp_size_t<I>() ): ( throw bad_variant_access(), v._get_impl( mp_size_t<I>() ) );
#else
return v.index() == I? v._get_impl( mp_size_t<I>() ): throw bad_variant_access();
#endif
return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t<I>() );
}
template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...>>&& get(variant<T...>&& v)
{
static_assert( I < sizeof...(T), "Index out of bounds" );
#if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
return std::move( v.index() == I? v._get_impl( mp_size_t<I>() ): ( throw bad_variant_access(), v._get_impl( mp_size_t<I>() ) ) );
#else
return v.index() == I? std::move( v._get_impl( mp_size_t<I>() ) ): throw bad_variant_access();
#endif
return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t<I>() ) );
}
template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...> const>& get(variant<T...> const& v)
{
static_assert( I < sizeof...(T), "Index out of bounds" );
#if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
return v.index() == I? v._get_impl( mp_size_t<I>() ): ( throw bad_variant_access(), v._get_impl( mp_size_t<I>() ) );
#else
return v.index() == I? v._get_impl( mp_size_t<I>() ): throw bad_variant_access();
#endif
return ( v.index() == I? (void)0: throw bad_variant_access() ), v._get_impl( mp_size_t<I>() );
}
template<std::size_t I, class... T> constexpr variant_alternative_t<I, variant<T...> const>&& get(variant<T...> const&& v)
{
static_assert( I < sizeof...(T), "Index out of bounds" );
#if BOOST_WORKAROUND( BOOST_MSVC, <= 1910 )
return std::move( v.index() == I? v._get_impl( mp_size_t<I>() ): ( throw bad_variant_access(), v._get_impl( mp_size_t<I>() ) ) );
#else
return v.index() == I? std::move( v._get_impl( mp_size_t<I>() ) ): throw bad_variant_access();
#endif
return ( v.index() == I? (void)0: throw bad_variant_access() ), std::move( v._get_impl( mp_size_t<I>() ) );
}
// get

View File

@ -15,3 +15,4 @@ run variant_size.cpp : : : $(REQ) ;
run variant_alternative.cpp : : : $(REQ) ;
run holds_alternative.cpp : : : $(REQ) ;
run holds_alternative_cx.cpp : : : $(REQ) ;
run get_by_index.cpp : : : $(REQ) ;

184
test/get_by_index.cpp Normal file
View File

@ -0,0 +1,184 @@
// 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;
int main()
{
{
variant<int> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int*>));
}
{
variant<int> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const*>));
}
{
variant<int const> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const*>));
}
{
variant<int volatile> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const volatile&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const volatile&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const volatile*>));
}
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
}
{
variant<int> v( 1 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
}
{
variant<int, float> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(v)), float&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(std::move(v))), float&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<1>(&v)), float*>));
}
{
variant<int, float> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(v)), float const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(std::move(v))), float const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<1>(&v)), float const*>));
}
{
variant<int const, float volatile> v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(v)), float volatile&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(std::move(v))), float volatile&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<1>(&v)), float volatile*>));
}
{
variant<int const, float volatile> const v;
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(v)), int const&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<0>(std::move(v))), int const&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<0>(&v)), int const*>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(v)), float const volatile&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get<1>(std::move(v))), float const volatile&&>));
BOOST_TEST_TRAIT_TRUE((std::is_same<decltype(get_if<1>(&v)), float const volatile*>));
}
{
variant<int, float> v;
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
}
{
variant<int, float> v( 1 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
}
{
variant<int, float> v( 3.14f );
BOOST_TEST_THROWS( get<0>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<0>(&v), nullptr );
BOOST_TEST_EQ( get<1>(v), 3.14f );
BOOST_TEST_EQ( get_if<1>(&v), &get<1>(v) );
}
{
variant<int, float, float> v;
BOOST_TEST_EQ( get<0>(v), 0 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
BOOST_TEST_THROWS( get<2>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<2>(&v), nullptr );
}
{
variant<int, float, float> v( 1 );
BOOST_TEST_EQ( get<0>(v), 1 );
BOOST_TEST_EQ( get_if<0>(&v), &get<0>(v) );
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
BOOST_TEST_THROWS( get<2>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<2>(&v), nullptr );
}
{
variant<int, int, float> v( 3.14f );
BOOST_TEST_THROWS( get<0>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<0>(&v), nullptr );
BOOST_TEST_THROWS( get<1>(v), bad_variant_access );
BOOST_TEST_EQ( get_if<1>(&v), nullptr );
BOOST_TEST_EQ( get<2>(v), 3.14f );
BOOST_TEST_EQ( get_if<2>(&v), &get<2>(v) );
}
return boost::report_errors();
}