lightweight_test_trait.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, 2021 Peter Dimov
  12. //
  13. // Copyright 2019 Glen Joseph Fernandes
  14. // ([email protected])
  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/type_name.hpp>
  21. #include <boost/core/detail/is_same.hpp>
  22. #include <boost/config.hpp>
  23. namespace boost
  24. {
  25. namespace detail
  26. {
  27. template< class T > inline void test_trait_impl( char const * trait, void (*)( T ),
  28. bool expected, char const * file, int line, char const * function )
  29. {
  30. if( T::value == expected )
  31. {
  32. test_results();
  33. }
  34. else
  35. {
  36. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  37. << file << "(" << line << "): predicate '" << trait << "' ["
  38. << boost::core::type_name<T>() << "]"
  39. << " test failed in function '" << function
  40. << "' (should have been " << ( expected? "true": "false" ) << ")"
  41. << std::endl;
  42. ++test_results().errors();
  43. }
  44. }
  45. template<class T> inline bool test_trait_same_impl_( T )
  46. {
  47. return T::value;
  48. }
  49. template<class T1, class T2> inline void test_trait_same_impl( char const * types,
  50. boost::core::detail::is_same<T1, T2> same, char const * file, int line, char const * function )
  51. {
  52. if( test_trait_same_impl_( same ) )
  53. {
  54. test_results();
  55. }
  56. else
  57. {
  58. BOOST_LIGHTWEIGHT_TEST_OSTREAM
  59. << file << "(" << line << "): test 'is_same<" << types << ">'"
  60. << " failed in function '" << function
  61. << "' ('" << boost::core::type_name<T1>()
  62. << "' != '" << boost::core::type_name<T2>() << "')"
  63. << std::endl;
  64. ++test_results().errors();
  65. }
  66. }
  67. } // namespace detail
  68. } // namespace boost
  69. #define BOOST_TEST_TRAIT_TRUE(type) ( ::boost::detail::test_trait_impl(#type, (void(*)type)0, true, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
  70. #define BOOST_TEST_TRAIT_FALSE(type) ( ::boost::detail::test_trait_impl(#type, (void(*)type)0, false, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
  71. #if defined(__GNUC__)
  72. // ignoring -Wvariadic-macros with #pragma doesn't work under GCC
  73. # pragma GCC system_header
  74. #endif
  75. #define BOOST_TEST_TRAIT_SAME(...) ( ::boost::detail::test_trait_same_impl(#__VA_ARGS__, ::boost::core::detail::is_same< __VA_ARGS__ >(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
  76. #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP