1 #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP
2 #define BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP
4 // MS compatible compilers support #pragma once
10 // boost/core/lightweight_test_trait.hpp
12 // BOOST_TEST_TRAIT_TRUE, BOOST_TEST_TRAIT_FALSE, BOOST_TEST_TRAIT_SAME
14 // Copyright 2014 Peter Dimov
16 // Copyright 2019 Glen Joseph Fernandes
17 // (glenjofe@gmail.com)
19 // Distributed under the Boost Software License, Version 1.0.
20 // See accompanying file LICENSE_1_0.txt or copy at
21 // http://www.boost.org/LICENSE_1_0.txt
23 #include <boost/core/lightweight_test.hpp>
24 #include <boost/core/typeinfo.hpp>
25 #include <boost/core/is_same.hpp>
26 #include <boost/config.hpp>
34 template<class, int = 0> struct test_print { };
36 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T, 2>)
38 return o << boost::core::demangled_name(BOOST_CORE_TYPEID(T));
41 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T, 1>)
43 return o << test_print<T, 2>();
46 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<const T, 1>)
48 return o << test_print<T, 2>() << " const";
51 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<volatile T, 1>)
53 return o << test_print<T, 2>() << " volatile";
56 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<const volatile T, 1>)
58 return o << test_print<T, 2>() << " const volatile";
61 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T>)
63 return o << test_print<T, 1>();
66 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T&>)
68 return o << test_print<T, 1>() << " &";
71 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
72 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T&&>)
74 return o << test_print<T, 1>() << " &&";
78 template< class T > inline void test_trait_impl( char const * trait, void (*)( T ),
79 bool expected, char const * file, int line, char const * function )
81 if( T::value == expected )
87 BOOST_LIGHTWEIGHT_TEST_OSTREAM
88 << file << "(" << line << "): predicate '" << trait << "' ["
89 << boost::core::demangled_name( BOOST_CORE_TYPEID(T) ) << "]"
90 << " test failed in function '" << function
91 << "' (should have been " << ( expected? "true": "false" ) << ")"
94 ++test_results().errors();
98 template<class T> inline bool test_trait_same_impl_( T )
103 template<class T1, class T2> inline void test_trait_same_impl( char const * types,
104 boost::core::is_same<T1, T2> same, char const * file, int line, char const * function )
106 if( test_trait_same_impl_( same ) )
112 BOOST_LIGHTWEIGHT_TEST_OSTREAM
113 << file << "(" << line << "): test 'is_same<" << types << ">'"
114 << " failed in function '" << function
115 << "' ('" << test_print<T1>()
116 << "' != '" << test_print<T2>() << "')"
119 ++test_results().errors();
123 } // namespace detail
127 #define BOOST_TEST_TRAIT_TRUE(type) ( ::boost::detail::test_trait_impl(#type, (void(*)type)0, true, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
128 #define BOOST_TEST_TRAIT_FALSE(type) ( ::boost::detail::test_trait_impl(#type, (void(*)type)0, false, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
130 #if defined(__GNUC__)
131 // ignoring -Wvariadic-macros with #pragma doesn't work under GCC
132 # pragma GCC system_header
135 #define BOOST_TEST_TRAIT_SAME(...) ( ::boost::detail::test_trait_same_impl(#__VA_ARGS__, ::boost::core::is_same<__VA_ARGS__>(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
137 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP