⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 traits_test.cpp

📁 C++的一个好库。。。现在很流行
💻 CPP
📖 第 1 页 / 共 2 页
字号:

DEFINE_CONVERSION(boost::uint8_t  , long double, !cTrivial, float_to_integral, signed_to_unsigned, builtin_to_builtin,  cSubRanged );
DEFINE_CONVERSION(boost::int8_t   , long double, !cTrivial, float_to_integral, signed_to_signed  , builtin_to_builtin,  cSubRanged );
DEFINE_CONVERSION(boost::uint16_t , long double, !cTrivial, float_to_integral, signed_to_unsigned, builtin_to_builtin,  cSubRanged );
DEFINE_CONVERSION(boost::int16_t  , long double, !cTrivial, float_to_integral, signed_to_signed  , builtin_to_builtin,  cSubRanged );
DEFINE_CONVERSION(boost::uint32_t , long double, !cTrivial, float_to_integral, signed_to_unsigned, builtin_to_builtin,  cSubRanged );
DEFINE_CONVERSION(boost::int32_t  , long double, !cTrivial, float_to_integral, signed_to_signed  , builtin_to_builtin,  cSubRanged );
DEFINE_CONVERSION(float           , long double, !cTrivial, float_to_float   , signed_to_signed  , builtin_to_builtin, ( sizeof(long double) > sizeof(float) ) );
DEFINE_CONVERSION(double          , long double, !cTrivial, float_to_float   , signed_to_signed  , builtin_to_builtin, ( sizeof(long double) > sizeof(double) ) );
DEFINE_CONVERSION(long double     , long double,  cTrivial, float_to_float   , signed_to_signed  , builtin_to_builtin, !cSubRanged );
DEFINE_CONVERSION(MyInt           , long double, !cTrivial, float_to_integral, signed_to_signed  , builtin_to_udt    , !cSubRanged );
DEFINE_CONVERSION(MyFloat         , long double, !cTrivial, float_to_float   , signed_to_signed  , builtin_to_udt    , !cSubRanged );

DEFINE_CONVERSION(boost::uint8_t  , MyInt, !cTrivial, integral_to_integral, signed_to_unsigned, udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(boost::int8_t   , MyInt, !cTrivial, integral_to_integral, signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(boost::uint16_t , MyInt, !cTrivial, integral_to_integral, signed_to_unsigned, udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(boost::int16_t  , MyInt, !cTrivial, integral_to_integral, signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(boost::uint32_t , MyInt, !cTrivial, integral_to_integral, signed_to_unsigned, udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(boost::int32_t  , MyInt, !cTrivial, integral_to_integral, signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(float           , MyInt, !cTrivial, integral_to_float   , signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(double          , MyInt, !cTrivial, integral_to_float   , signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(long double     , MyInt, !cTrivial, integral_to_float   , signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(MyInt           , MyInt,  cTrivial, integral_to_integral, signed_to_signed  , udt_to_udt     ,!cSubRanged );
DEFINE_CONVERSION(MyFloat         , MyInt, !cTrivial, integral_to_float   , signed_to_signed  , udt_to_udt     ,!cSubRanged );

DEFINE_CONVERSION(boost::uint8_t  , MyFloat, !cTrivial, float_to_integral, signed_to_unsigned, udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(boost::int8_t   , MyFloat, !cTrivial, float_to_integral, signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(boost::uint16_t , MyFloat, !cTrivial, float_to_integral, signed_to_unsigned, udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(boost::int16_t  , MyFloat, !cTrivial, float_to_integral, signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(boost::uint32_t , MyFloat, !cTrivial, float_to_integral, signed_to_unsigned, udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(boost::int32_t  , MyFloat, !cTrivial, float_to_integral, signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(float           , MyFloat, !cTrivial, float_to_float   , signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(double          , MyFloat, !cTrivial, float_to_float   , signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(long double     , MyFloat, !cTrivial, float_to_float   , signed_to_signed  , udt_to_builtin , cSubRanged );
DEFINE_CONVERSION(MyInt           , MyFloat, !cTrivial, float_to_integral, signed_to_signed  , udt_to_udt     ,!cSubRanged );
DEFINE_CONVERSION(MyFloat         , MyFloat,  cTrivial, float_to_float   , signed_to_signed  , udt_to_udt     ,!cSubRanged );

//
// The test is performed by comparing each field of
//   boost::numeric::conversion_traits<T,S>
// with the fields of
//   expected_traits<T,S>
// which is a traits class constructed explicitely for each combination
// of the built-in arithmetic types.
//
template<class T,
         class S,
         class Supertype,
         class Subtype,
         class Subranged,
         class Trivial
        >
struct expected_traits
{
  typedef typename my_int_float_mixture   <T,S>::type int_float_mixture ;
  typedef typename my_sign_mixture        <T,S>::type sign_mixture ;
  typedef typename my_udt_builtin_mixture <T,S>::type udt_builtin_mixture ;

  typedef Subranged subranged ;
  typedef Trivial   trivial   ;
  typedef Supertype supertype ;
  typedef Subtype   subtype   ;
} ;

// This is used by the test engine to generate a expected_traits from T and S.
//
template<class T, class S>
struct generate_expected_traits
{
  typedef expected_traits<T, S, T, S, mpl::false_, mpl::true_  > trivial ;
  typedef expected_traits<T, S, S, T, mpl::true_ , mpl::false_ > subranged ;
  typedef expected_traits<T, S, T, S, mpl::false_, mpl::false_ > non_subranged ;

  typedef typename my_is_subranged<T,S>::type IsSubranged ;
  typedef typename my_is_trivial  <T,S>::type IsTrivial   ;

  typedef typename mpl::if_<IsSubranged,subranged,non_subranged>::type non_trivial ;

  typedef typename mpl::if_<IsTrivial,trivial,non_trivial>::type  type ;
} ;

// This macro generates the code that compares a non-type field
// in boost::numeric::conversion_traits<> with its corresponding field
// in expected_traits<>
//

#define TEST_VALUE_FIELD(Name) \
        typedef typename traits::Name   BOOST_PP_CAT(t,Name) ; \
        typedef typename expected::Name BOOST_PP_CAT(x,Name) ; \
        BOOST_CHECK_MESSAGE ( ( BOOST_PP_CAT(t,Name)::value == BOOST_PP_CAT(x,Name)::value ) , \
                              "conversion_traits<" << typeid(T).name() << "," << typeid(S).name() \
                              << ">::" << #Name << " = " << to_string(BOOST_PP_CAT(t,Name)::value) \
                              << ". Expected: "  << to_string(BOOST_PP_CAT(x,Name)::value) \
                            ) ;

// This macro generates the code that compares a type field
// in numeric::conversion_traits<> with its corresponding field
// in expected_traits<>
//
#define TEST_TYPE_FIELD(Name) \
        typedef typename traits::Name   BOOST_PP_CAT(t,Name) ; \
        typedef typename expected::Name BOOST_PP_CAT(x,Name) ; \
        BOOST_CHECK_MESSAGE ( ( typeid(BOOST_PP_CAT(t,Name)) == typeid(BOOST_PP_CAT(x,Name)) ) , \
                              "conversion_traits<" << typeid(T).name() << "," << typeid(S).name() \
                              << ">::" << #Name << " = " <<  typeid(BOOST_PP_CAT(t,Name)).name() \
                              << ". Expected: "  << typeid(BOOST_PP_CAT(x,Name)).name() \
                            ) ;

//
// Test core.
// Compares each field of boost::numeric::conversion_traits<T,S>
// with the corresponding field of expected_traits<T,S>
//
template<class T, class S>
void test_traits_base(  MATCH_FNTPL_ARG(T),  MATCH_FNTPL_ARG(S) )
{
  typedef boost::numeric::conversion_traits<T,S> traits ;
  typedef typename generate_expected_traits<T,S>::type expected ;

  TEST_VALUE_FIELD(int_float_mixture) ;
  TEST_VALUE_FIELD(sign_mixture) ;
  TEST_VALUE_FIELD(udt_builtin_mixture) ;
  TEST_VALUE_FIELD(subranged) ;
  TEST_VALUE_FIELD(trivial) ;
  TEST_TYPE_FIELD (supertype) ;
  TEST_TYPE_FIELD (subtype) ;
}


template<class S>
void test_traits_from(  MATCH_FNTPL_ARG(S) )
{
  test_traits_base( SET_FNTPL_ARG(boost::uint8_t)  ,SET_FNTPL_ARG(S) );
  test_traits_base( SET_FNTPL_ARG(boost::int8_t)   ,SET_FNTPL_ARG(S) );
  test_traits_base( SET_FNTPL_ARG(boost::uint16_t) ,SET_FNTPL_ARG(S) );
  test_traits_base( SET_FNTPL_ARG(boost::int16_t)  ,SET_FNTPL_ARG(S) );
  test_traits_base( SET_FNTPL_ARG(boost::uint32_t) ,SET_FNTPL_ARG(S) );
  test_traits_base( SET_FNTPL_ARG(boost::int32_t)  ,SET_FNTPL_ARG(S) );
  test_traits_base( SET_FNTPL_ARG(float)           ,SET_FNTPL_ARG(S) );
  test_traits_base( SET_FNTPL_ARG(double)          ,SET_FNTPL_ARG(S) );
  test_traits_base( SET_FNTPL_ARG(long double)     ,SET_FNTPL_ARG(S) );
  test_traits_base( SET_FNTPL_ARG(MyInt)           ,SET_FNTPL_ARG(S) );
  test_traits_base( SET_FNTPL_ARG(MyFloat)         ,SET_FNTPL_ARG(S) );
}

void test_traits()
{
  test_traits_from( SET_FNTPL_ARG(boost::uint8_t)  );
  test_traits_from( SET_FNTPL_ARG(boost::int8_t)   );
  test_traits_from( SET_FNTPL_ARG(boost::uint16_t) );
  test_traits_from( SET_FNTPL_ARG(boost::int16_t)  );
  test_traits_from( SET_FNTPL_ARG(boost::uint32_t) );
  test_traits_from( SET_FNTPL_ARG(boost::int32_t)  );
  test_traits_from( SET_FNTPL_ARG(float)           );
  test_traits_from( SET_FNTPL_ARG(double)          );
  test_traits_from( SET_FNTPL_ARG(long double)     );
  test_traits_from( SET_FNTPL_ARG(MyInt)           );
  test_traits_from( SET_FNTPL_ARG(MyFloat)         );
}

int test_main( int, char * [])
{
  std::cout << std::setprecision( std::numeric_limits<long double>::digits10 ) ;
  
  test_traits();

  return 0;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -