lightweight_test_trait.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP
  2. #define BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. // boost/core/lightweight_test_trait.hpp
  8. //
  9. // BOOST_TEST_TRAIT_TRUE, BOOST_TEST_TRAIT_FALSE, BOOST_TEST_TRAIT_SAME
  10. //
  11. // Copyright 2014 Peter Dimov
  12. //
  13. // Copyright 2019 Glen Joseph Fernandes
  14. // (glenjofe@gmail.com)
  15. //
  16. // Distributed under the Boost Software License, Version 1.0.
  17. // See accompanying file LICENSE_1_0.txt or copy at
  18. // http://www.boost.org/LICENSE_1_0.txt
  19. #include <boost/core/lightweight_test.hpp>
  20. #include <boost/core/typeinfo.hpp>
  21. #include <boost/core/is_same.hpp>
  22. #include <boost/config.hpp>
  23. namespace boost
  24. {
  25. namespace detail
  26. {
  27. template<class, int = 0> struct test_print { };
  28. template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T, 2>)
  29. {
  30. return o << boost::core::demangled_name(BOOST_CORE_TYPEID(T));
  31. }
  32. template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T, 1>)
  33. {
  34. return o << test_print<T, 2>();
  35. }
  36. template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<const T, 1>)
  37. {
  38. return o << test_print<T, 2>() << " const";
  39. }
  40. template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<volatile T, 1>)
  41. {
  42. return o << test_print<T, 2>() << " volatile";
  43. }
  44. template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<const volatile T, 1>)
  45. {
  46. return o << test_print<T, 2>() << " const volatile";
  47. }
  48. template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T>)
  49. {
  50. return o << test_print<T, 1>();
  51. }
  52. template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T&>)
  53. {
  54. return o << test_print<T, 1>() << " &";
  55. }
  56. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  57. template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T&&>)
  58. {
  59. return o << test_print<T, 1>() << " &&";
  60. }
  61. #endif
  62. template< class T > inline void test_trait_impl( char const * trait, void (*)( T ),
  63. bool expected, char const * file, int line, char const * function )
  64. {
  65. if( T::value == expected )
  66. {
  67. test_results();
  68. }
  69. else
  70. {
  71. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  72. << file << "(" << line << "): predicate '" << trait << "' ["
  73. << boost::core::demangled_name( BOOST_CORE_TYPEID(T) ) << "]"
  74. << " test failed in function '" << function
  75. << "' (should have been " << ( expected? "true": "false" ) << ")"
  76. << std::endl;
  77. ++test_results().errors();
  78. }
  79. }
  80. template<class T> inline bool test_trait_same_impl_( T )
  81. {
  82. return T::value;
  83. }
  84. template<class T1, class T2> inline void test_trait_same_impl( char const * types,
  85. boost::core::is_same<T1, T2> same, char const * file, int line, char const * function )
  86. {
  87. if( test_trait_same_impl_( same ) )
  88. {
  89. test_results();
  90. }
  91. else
  92. {
  93. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  94. << file << "(" << line << "): test 'is_same<" << types << ">'"
  95. << " failed in function '" << function
  96. << "' ('" << test_print<T1>()
  97. << "' != '" << test_print<T2>() << "')"
  98. << std::endl;
  99. ++test_results().errors();
  100. }
  101. }
  102. } // namespace detail
  103. } // namespace boost
  104. #define BOOST_TEST_TRAIT_TRUE(type) ( ::boost::detail::test_trait_impl(#type, (void(*)type)0, true, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
  105. #define BOOST_TEST_TRAIT_FALSE(type) ( ::boost::detail::test_trait_impl(#type, (void(*)type)0, false, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
  106. #define BOOST_TEST_TRAIT_SAME(...) ( ::boost::detail::test_trait_same_impl(#__VA_ARGS__, ::boost::core::is_same<__VA_ARGS__>(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
  107. #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP