added testcase types_edge_cases_test for static_variant

This commit is contained in:
crypto-ape 2018-10-29 18:10:34 +01:00
parent 384d4f14c4
commit f8b86fc757
2 changed files with 53 additions and 3 deletions

View file

@ -245,6 +245,7 @@ std::vector<std::function<typename Visitor::result_type(Visitor&,Data)>> init_co
template<typename... Types> template<typename... Types>
class static_variant { class static_variant {
protected:
static_assert(impl::type_info<Types...>::no_reference_types, "Reference types are not permitted in static_variant."); static_assert(impl::type_info<Types...>::no_reference_types, "Reference types are not permitted in static_variant.");
static_assert(impl::type_info<Types...>::no_duplicates, "static_variant type arguments contain duplicate types."); static_assert(impl::type_info<Types...>::no_duplicates, "static_variant type arguments contain duplicate types.");
@ -269,7 +270,7 @@ class static_variant {
new(storage.data()) X( std::move(x) ); new(storage.data()) X( std::move(x) );
} }
void init(tag_type tag) void init_from_tag(tag_type tag)
{ {
FC_ASSERT( tag >= 0 ); FC_ASSERT( tag >= 0 );
FC_ASSERT( tag < count() ); FC_ASSERT( tag < count() );
@ -298,7 +299,7 @@ public:
static_variant() static_variant()
{ {
init(0); init_from_tag(0);
} }
template<typename... Other> template<typename... Other>
@ -428,7 +429,7 @@ public:
FC_ASSERT( w >= 0 ); FC_ASSERT( w >= 0 );
FC_ASSERT( w < count() ); FC_ASSERT( w < count() );
clean(); clean();
init(w); init_from_tag(w);
} }
tag_type which() const {return _tag;} tag_type which() const {return _tag;}

View file

@ -46,6 +46,55 @@ FC_REFLECT( fc::test::item, (level)(w) );
BOOST_AUTO_TEST_SUITE(fc_variant_and_log) BOOST_AUTO_TEST_SUITE(fc_variant_and_log)
BOOST_AUTO_TEST_CASE( types_edge_cases_test )
{
using namespace fc::test;
class sv : public fc::static_variant<>
{
public:
BOOST_ATTRIBUTE_UNUSED typedef fc::static_variant<>::tag_type tag_type;
};
BOOST_TEST_MESSAGE( "========== Test empty static_variant ==========" );
BOOST_CHECK_THROW( fc::static_variant<>(), fc::assert_exception );
BOOST_TEST_MESSAGE( "========== Test static_variant with tag_type ==========" );
sv::tag_type init_value = 2;
fc::static_variant< sv::tag_type, std::string, fc::variant > variant_with_tagtype(init_value);
BOOST_CHECK_EQUAL( variant_with_tagtype.count(), 3 );
BOOST_CHECK_EQUAL( variant_with_tagtype.which(), 0 );
sv::tag_type current_value = variant_with_tagtype.get<sv::tag_type>();
BOOST_CHECK_EQUAL( current_value, init_value );
BOOST_CHECK( variant_with_tagtype == init_value );
for (sv::tag_type i = variant_with_tagtype.count(); i-->0;)
{
variant_with_tagtype.set_which(i);
BOOST_CHECK_EQUAL(variant_with_tagtype.which(), i);
}
BOOST_TEST_MESSAGE( "========== Test static_variant with static_variant ==========" );
using sv_double = fc::static_variant<double>;
using sv_float = fc::static_variant<float>;
fc::static_variant< sv_float, std::string, sv_double > variant;
sv_float variant_float = 1.5f;
variant = variant_float;
BOOST_CHECK_EQUAL( variant.which(), 0 );
BOOST_CHECK_EQUAL( variant.get<sv_float>().get<float>(), 1.5f );
sv_double variant_double = 1.0;
variant = variant_double;
BOOST_CHECK_EQUAL( variant.which() , 2);
BOOST_CHECK_EQUAL( variant.get<sv_double>().get<double>(), 1.0 );
}
BOOST_AUTO_TEST_CASE( nested_objects_test ) BOOST_AUTO_TEST_CASE( nested_objects_test )
{ try { { try {