133 lines
4.6 KiB
C++
133 lines
4.6 KiB
C++
#include <boost/test/unit_test.hpp>
|
|
#include <fc/log/logger.hpp>
|
|
|
|
#include <fc/container/flat.hpp>
|
|
#include <fc/io/raw.hpp>
|
|
#include <fc/reflect/variant.hpp>
|
|
#include <fc/static_variant.hpp>
|
|
|
|
namespace fc { namespace test {
|
|
|
|
struct item;
|
|
inline bool operator == ( const item& a, const item& b );
|
|
inline bool operator < ( const item& a, const item& b );
|
|
|
|
struct item_wrapper
|
|
{
|
|
item_wrapper() {}
|
|
item_wrapper(item&& it) { v.reserve(1); v.insert( it ); }
|
|
boost::container::flat_set<struct item> v;
|
|
};
|
|
|
|
inline bool operator == ( const item_wrapper& a, const item_wrapper& b )
|
|
{ return ( std::tie( a.v ) == std::tie( b.v ) ); }
|
|
inline bool operator < ( const item_wrapper& a, const item_wrapper& b )
|
|
{ return ( std::tie( a.v ) < std::tie( b.v ) ); }
|
|
|
|
struct item
|
|
{
|
|
item(int32_t lvl = 0) : level(lvl) {}
|
|
item(item_wrapper&& wp, int32_t lvl = 0) : level(lvl), w(wp) {}
|
|
int32_t level;
|
|
item_wrapper w;
|
|
};
|
|
|
|
inline bool operator == ( const item& a, const item& b )
|
|
{ return ( std::tie( a.level, a.w ) == std::tie( b.level, b.w ) ); }
|
|
inline bool operator < ( const item& a, const item& b )
|
|
{ return ( std::tie( a.level, a.w ) < std::tie( b.level, b.w ) ); }
|
|
|
|
|
|
} } // namespace fc::test
|
|
|
|
FC_REFLECT( fc::test::item_wrapper, (v) );
|
|
FC_REFLECT( fc::test::item, (level)(w) );
|
|
|
|
BOOST_AUTO_TEST_SUITE(fc_variant_and_log)
|
|
|
|
BOOST_AUTO_TEST_CASE( nested_objects_test )
|
|
{ try {
|
|
|
|
auto create_nested_object = []( uint32_t level )
|
|
{
|
|
ilog( "Creating nested object with ${lv} level(s)", ("lv",level) );
|
|
fc::test::item nested;
|
|
for( uint32_t i = 1; i <= level; i++ )
|
|
{
|
|
if( i % 10 == 0 )
|
|
ilog( "Creating level ${lv}", ("lv",i) );
|
|
fc::test::item_wrapper wp( std::move(nested) );
|
|
nested = fc::test::item( std::move(wp), i );
|
|
}
|
|
return nested;
|
|
};
|
|
|
|
for (int nested_loops = 0; nested_loops <= 100; ++nested_loops )
|
|
{
|
|
int nested_levels = nested_loops * 3 + 2;
|
|
auto nested = create_nested_object( nested_loops );
|
|
|
|
fc::variant v;
|
|
|
|
BOOST_TEST_MESSAGE( "About to convert to variant." );
|
|
BOOST_CHECK_THROW( to_variant( nested, v, nested_levels ), fc::assert_exception );
|
|
// CAPTURE_AND_LOG should never throw again
|
|
try { to_variant( nested, v, nested_levels ); } FC_CAPTURE_AND_LOG ( (nested) );
|
|
to_variant( nested, v, nested_levels + 1 );
|
|
|
|
BOOST_TEST_MESSAGE( "About to convert from variant." );
|
|
fc::test::item unpacked;
|
|
BOOST_CHECK_THROW( from_variant( v, unpacked, nested_levels ), fc::assert_exception );
|
|
from_variant( v, unpacked, nested_levels + 1 );
|
|
|
|
BOOST_CHECK( unpacked == nested );
|
|
|
|
fc::static_variant<fc::test::item> sv( nested ), sv1;
|
|
BOOST_TEST_MESSAGE( "About to convert static_variant to variant." );
|
|
BOOST_CHECK_THROW( to_variant( sv, v, nested_levels + 1 ), fc::assert_exception );
|
|
// CAPTURE_AND_LOG should never throw again
|
|
try { to_variant( sv, v, nested_levels + 1 ); } FC_CAPTURE_AND_LOG ( (sv) );
|
|
to_variant( sv, v, nested_levels + 2 );
|
|
|
|
BOOST_TEST_MESSAGE( "About to convert static_variant from variant." );
|
|
BOOST_CHECK_THROW( from_variant( v, sv1, nested_levels + 1 ), fc::assert_exception );
|
|
from_variant( v, sv1, nested_levels + 2 );
|
|
|
|
BOOST_CHECK( sv == sv1 );
|
|
|
|
std::vector<fc::static_variant<fc::test::item>> vec(300), vec1;
|
|
for( int i = 0; i < 300; i++ )
|
|
vec.push_back(sv);
|
|
BOOST_TEST_MESSAGE( "About to convert vector to variant." );
|
|
BOOST_CHECK_THROW( to_variant( vec, v, nested_levels + 2 ), fc::assert_exception );
|
|
// CAPTURE_AND_LOG should never throw again
|
|
try { to_variant( vec, v, nested_levels + 2 ); } FC_CAPTURE_AND_LOG ( (vec) );
|
|
to_variant( vec, v, nested_levels + 3 );
|
|
|
|
BOOST_TEST_MESSAGE( "About to convert vector from variant." );
|
|
BOOST_CHECK_THROW( from_variant( v, vec1, nested_levels + 2 ), fc::assert_exception );
|
|
from_variant( v, vec1, nested_levels + 3 );
|
|
|
|
BOOST_CHECK( vec == vec1 );
|
|
|
|
// both log and dump should never throw
|
|
BOOST_TEST_MESSAGE( "About to log obj." );
|
|
ilog( "The obj is ${a}", ("a",nested) );
|
|
BOOST_TEST_MESSAGE( "About to dump obj." );
|
|
idump( (nested) );
|
|
|
|
BOOST_TEST_MESSAGE( "About to log static_variant." );
|
|
ilog( "The static_variant is ${a}", ("a",sv) );
|
|
BOOST_TEST_MESSAGE( "About to dump static_variant." );
|
|
idump((sv));
|
|
|
|
BOOST_TEST_MESSAGE( "About to log vector." );
|
|
ilog( "The vector is ${a}", ("a",vec) );
|
|
BOOST_TEST_MESSAGE( "About to dump vector." );
|
|
idump((vec));
|
|
|
|
}
|
|
|
|
} FC_CAPTURE_LOG_AND_RETHROW ( (0) ) }
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|