mirror of
https://github.com/boostorg/variant2.git
synced 2025-07-29 19:57:18 +02:00
Add throwing copy/move/convert tests
This commit is contained in:
@ -64,3 +64,7 @@ run variant_lt_gt.cpp ;
|
||||
run variant_convert_construct.cpp ;
|
||||
run variant_subset.cpp ;
|
||||
run variant_valueless.cpp ;
|
||||
|
||||
run variant_copy_construct_throw.cpp ;
|
||||
run variant_move_construct_throw.cpp ;
|
||||
run variant_convert_construct_throw.cpp ;
|
||||
|
91
test/variant_convert_construct_throw.cpp
Normal file
91
test/variant_convert_construct_throw.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
|
||||
// Copyright 2019 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 <stdexcept>
|
||||
|
||||
using namespace boost::variant2;
|
||||
|
||||
struct X
|
||||
{
|
||||
static int instances;
|
||||
|
||||
X()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
X( X const& )
|
||||
{
|
||||
throw std::runtime_error( "X(X const&)" );
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
};
|
||||
|
||||
int X::instances = 0;
|
||||
|
||||
void test_copy()
|
||||
{
|
||||
X::instances = 0;
|
||||
|
||||
{
|
||||
variant<X> v1;
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 1 );
|
||||
|
||||
try
|
||||
{
|
||||
variant<X, int, float> v2( v1 );
|
||||
BOOST_TEST_EQ( X::instances, 2 );
|
||||
}
|
||||
catch( std::exception const& )
|
||||
{
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 1 );
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 0 );
|
||||
}
|
||||
|
||||
void test_move()
|
||||
{
|
||||
X::instances = 0;
|
||||
|
||||
{
|
||||
variant<X> v1;
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 1 );
|
||||
|
||||
try
|
||||
{
|
||||
variant<X, int, float> v2( std::move( v1 ) );
|
||||
BOOST_TEST_EQ( X::instances, 2 );
|
||||
}
|
||||
catch( std::exception const& )
|
||||
{
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 1 );
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 0 );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_copy();
|
||||
test_move();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
65
test/variant_copy_construct_throw.cpp
Normal file
65
test/variant_copy_construct_throw.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
|
||||
// Copyright 2019 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 <stdexcept>
|
||||
|
||||
using namespace boost::variant2;
|
||||
|
||||
struct X
|
||||
{
|
||||
static int instances;
|
||||
|
||||
X()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
X( X const& )
|
||||
{
|
||||
throw std::runtime_error( "X(X const&)" );
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
};
|
||||
|
||||
int X::instances = 0;
|
||||
|
||||
void test()
|
||||
{
|
||||
X::instances = 0;
|
||||
|
||||
{
|
||||
variant<X> v1;
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 1 );
|
||||
|
||||
try
|
||||
{
|
||||
variant<X> v2( v1 );
|
||||
BOOST_TEST_EQ( X::instances, 2 );
|
||||
}
|
||||
catch( std::exception const& )
|
||||
{
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 1 );
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 0 );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test();
|
||||
return boost::report_errors();
|
||||
}
|
65
test/variant_move_construct_throw.cpp
Normal file
65
test/variant_move_construct_throw.cpp
Normal file
@ -0,0 +1,65 @@
|
||||
|
||||
// Copyright 2019 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 <stdexcept>
|
||||
|
||||
using namespace boost::variant2;
|
||||
|
||||
struct X
|
||||
{
|
||||
static int instances;
|
||||
|
||||
X()
|
||||
{
|
||||
++instances;
|
||||
}
|
||||
|
||||
X( X const& )
|
||||
{
|
||||
throw std::runtime_error( "X(X const&)" );
|
||||
}
|
||||
|
||||
~X()
|
||||
{
|
||||
--instances;
|
||||
}
|
||||
};
|
||||
|
||||
int X::instances = 0;
|
||||
|
||||
void test()
|
||||
{
|
||||
X::instances = 0;
|
||||
|
||||
{
|
||||
variant<X> v1;
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 1 );
|
||||
|
||||
try
|
||||
{
|
||||
variant<X> v2( std::move( v1 ) );
|
||||
BOOST_TEST_EQ( X::instances, 2 );
|
||||
}
|
||||
catch( std::exception const& )
|
||||
{
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 1 );
|
||||
}
|
||||
|
||||
BOOST_TEST_EQ( X::instances, 0 );
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test();
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user