1 #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP
2 #define BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP
3
4 // MS compatible compilers support #pragma once
5
6 #if defined(_MSC_VER)
7 # pragma once
8 #endif
9
10 // boost/core/lightweight_test_trait.hpp
11 //
12 // BOOST_TEST_TRAIT_TRUE, BOOST_TEST_TRAIT_FALSE, BOOST_TEST_TRAIT_SAME
13 //
14 // Copyright 2014 Peter Dimov
15 //
16 // Copyright 2019 Glen Joseph Fernandes
17 // (glenjofe@gmail.com)
18 //
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
22
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>
27
28 namespace boost
29 {
30
31 namespace detail
32 {
33
34 template<class, int = 0> struct test_print { };
35
operator <<(std::ostream & o,test_print<T,2>)36 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T, 2>)
37 {
38 return o << boost::core::demangled_name(BOOST_CORE_TYPEID(T));
39 }
40
operator <<(std::ostream & o,test_print<T,1>)41 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T, 1>)
42 {
43 return o << test_print<T, 2>();
44 }
45
operator <<(std::ostream & o,test_print<const T,1>)46 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<const T, 1>)
47 {
48 return o << test_print<T, 2>() << " const";
49 }
50
operator <<(std::ostream & o,test_print<volatile T,1>)51 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<volatile T, 1>)
52 {
53 return o << test_print<T, 2>() << " volatile";
54 }
55
operator <<(std::ostream & o,test_print<const volatile T,1>)56 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<const volatile T, 1>)
57 {
58 return o << test_print<T, 2>() << " const volatile";
59 }
60
operator <<(std::ostream & o,test_print<T>)61 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T>)
62 {
63 return o << test_print<T, 1>();
64 }
65
operator <<(std::ostream & o,test_print<T &>)66 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T&>)
67 {
68 return o << test_print<T, 1>() << " &";
69 }
70
71 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
operator <<(std::ostream & o,test_print<T &&>)72 template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T&&>)
73 {
74 return o << test_print<T, 1>() << " &&";
75 }
76 #endif
77
test_trait_impl(char const * trait,void (*)(T),bool expected,char const * file,int line,char const * function)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 )
80 {
81 if( T::value == expected )
82 {
83 test_results();
84 }
85 else
86 {
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" ) << ")"
92 << std::endl;
93
94 ++test_results().errors();
95 }
96 }
97
test_trait_same_impl_(T)98 template<class T> inline bool test_trait_same_impl_( T )
99 {
100 return T::value;
101 }
102
test_trait_same_impl(char const * types,boost::core::is_same<T1,T2> same,char const * file,int line,char const * function)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 )
105 {
106 if( test_trait_same_impl_( same ) )
107 {
108 test_results();
109 }
110 else
111 {
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>() << "')"
117 << std::endl;
118
119 ++test_results().errors();
120 }
121 }
122
123 } // namespace detail
124
125 } // namespace boost
126
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) )
129
130 #if defined(__GNUC__)
131 // ignoring -Wvariadic-macros with #pragma doesn't work under GCC
132 # pragma GCC system_header
133 #endif
134
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) )
136
137 #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP
138