variant2/test/variant_emplace_index.cpp
2017-05-31 04:19:40 +03:00

182 lines
4.8 KiB
C++

// 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;
#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<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X1>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X1>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X1>::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<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_constructible<X2>::value );
STATIC_ASSERT( !std::is_nothrow_copy_assignable<X2>::value );
STATIC_ASSERT( !std::is_nothrow_move_assignable<X2>::value );
int main()
{
{
variant<int> v;
BOOST_TEST_EQ( get<0>(v), 0 );
v.emplace<0>( 1 );
BOOST_TEST_EQ( get<0>(v), 1 );
v.emplace<0>();
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<int, float> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
v.emplace<0>( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
v.emplace<1>( 3.14f );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 3.14f );
v.emplace<1>();
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 0 );
v.emplace<0>();
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
}
{
variant<int, int, float, std::string> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 0 );
v.emplace<0>( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v), 1 );
v.emplace<1>();
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 0 );
v.emplace<1>( 1 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v), 1 );
v.emplace<2>( 3.14f );
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 3.14f );
v.emplace<2>();
BOOST_TEST_EQ( v.index(), 2 );
BOOST_TEST_EQ( get<2>(v), 0 );
v.emplace<3>( "s1" );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s1") );
v.emplace<3>( "s2" );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string("s2") );
v.emplace<3>();
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), std::string() );
v.emplace<3>( { 'a', 'b' } );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), (std::string{ 'a', 'b'}) );
v.emplace<3>( { 'c', 'd' }, std::allocator<char>() );
BOOST_TEST_EQ( v.index(), 3 );
BOOST_TEST_EQ( get<3>(v), (std::string{ 'c', 'd'}) );
}
{
variant<X1, X2> v;
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
v.emplace<0>( 1 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 1 );
v.emplace<1>( 2 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 2 );
v.emplace<0>();
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 0 );
v.emplace<0>( 4 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
v.emplace<1>();
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 0 );
v.emplace<1>( 6 );
BOOST_TEST_EQ( v.index(), 1 );
BOOST_TEST_EQ( get<1>(v).v, 6 );
v.emplace<0>( 3 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 3 );
v.emplace<0>( 4 );
BOOST_TEST_EQ( v.index(), 0 );
BOOST_TEST_EQ( get<0>(v).v, 4 );
}
return boost::report_errors();
}