diff --git a/include/boost/core/lightweight_test_same.hpp b/include/boost/core/lightweight_test_same.hpp new file mode 100644 index 0000000..9e1dfd1 --- /dev/null +++ b/include/boost/core/lightweight_test_same.hpp @@ -0,0 +1,112 @@ +#ifndef BOOST_CORE_LIGHTWEIGHT_TEST_SAME_HPP +#define BOOST_CORE_LIGHTWEIGHT_TEST_SAME_HPP + +// MS compatible compilers support #pragma once + +#if defined(_MSC_VER) +# pragma once +#endif + +// boost/core/lightweight_test_same.hpp +// +// BOOST_TEST_TRAIT_SAME +// +// Copyright 2014 Peter Dimov +// +// Copyright 2019 Glen Joseph Fernandes +// (glenjofe@gmail.com) +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// +// Split from lightweight_test_trait because __VA_ARGS__ +// warns under -pedantic + +#include +#include +#include +#include + +namespace boost +{ + +namespace detail +{ + +template struct test_print { }; + +template inline std::ostream& operator<<(std::ostream& o, test_print) +{ + return o << boost::core::demangled_name(BOOST_CORE_TYPEID(T)); +} + +template inline std::ostream& operator<<(std::ostream& o, test_print) +{ + return o << test_print(); +} + +template inline std::ostream& operator<<(std::ostream& o, test_print) +{ + return o << test_print() << " const"; +} + +template inline std::ostream& operator<<(std::ostream& o, test_print) +{ + return o << test_print() << " volatile"; +} + +template inline std::ostream& operator<<(std::ostream& o, test_print) +{ + return o << test_print() << " const volatile"; +} + +template inline std::ostream& operator<<(std::ostream& o, test_print) +{ + return o << test_print(); +} + +template inline std::ostream& operator<<(std::ostream& o, test_print) +{ + return o << test_print() << " &"; +} + +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) +template inline std::ostream& operator<<(std::ostream& o, test_print) +{ + return o << test_print() << " &&"; +} +#endif + +template inline bool test_same_impl_( T ) +{ + return T::value; +} + +template inline void test_same_impl( char const * types, + boost::core::is_same same, char const * file, int line, char const * function ) +{ + if( test_same_impl_( same ) ) + { + test_results(); + } + else + { + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << file << "(" << line << "): test 'is_same<" << types << ">'" + << " failed in function '" << function + << "' ('" << test_print() + << "' != '" << test_print() << "')" + << std::endl; + + ++test_results().errors(); + } +} + +} // namespace detail + +} // namespace boost + +#define BOOST_TEST_SAME(...) ( ::boost::detail::test_same_impl(#__VA_ARGS__, ::boost::core::is_same<__VA_ARGS__>(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) ) + +#endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_SAME_HPP diff --git a/include/boost/core/lightweight_test_trait.hpp b/include/boost/core/lightweight_test_trait.hpp index 26042c6..8f83475 100644 --- a/include/boost/core/lightweight_test_trait.hpp +++ b/include/boost/core/lightweight_test_trait.hpp @@ -9,7 +9,8 @@ // boost/core/lightweight_test_trait.hpp // -// BOOST_TEST_TRAIT_TRUE, BOOST_TEST_TRAIT_FALSE, BOOST_TEST_TRAIT_SAME +// BOOST_TEST_TRAIT_TRUE, BOOST_TEST_TRAIT_FALSE +// (BOOST_TEST_TRAIT_SAME deprecated in favor of BOOST_TEST_SAME) // // Copyright 2014 Peter Dimov // @@ -21,9 +22,11 @@ // http://www.boost.org/LICENSE_1_0.txt #include +#include #include #include #include +#include namespace boost { @@ -31,50 +34,6 @@ namespace boost namespace detail { -template struct test_print { }; - -template inline std::ostream& operator<<(std::ostream& o, test_print) -{ - return o << boost::core::demangled_name(BOOST_CORE_TYPEID(T)); -} - -template inline std::ostream& operator<<(std::ostream& o, test_print) -{ - return o << test_print(); -} - -template inline std::ostream& operator<<(std::ostream& o, test_print) -{ - return o << test_print() << " const"; -} - -template inline std::ostream& operator<<(std::ostream& o, test_print) -{ - return o << test_print() << " volatile"; -} - -template inline std::ostream& operator<<(std::ostream& o, test_print) -{ - return o << test_print() << " const volatile"; -} - -template inline std::ostream& operator<<(std::ostream& o, test_print) -{ - return o << test_print(); -} - -template inline std::ostream& operator<<(std::ostream& o, test_print) -{ - return o << test_print() << " &"; -} - -#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) -template inline std::ostream& operator<<(std::ostream& o, test_print) -{ - return o << test_print() << " &&"; -} -#endif - template< class T > inline void test_trait_impl( char const * trait, void (*)( T ), bool expected, char const * file, int line, char const * function ) { @@ -95,37 +54,17 @@ template< class T > inline void test_trait_impl( char const * trait, void (*)( T } } -template inline bool test_trait_same_impl_( T ) -{ - return T::value; -} - -template inline void test_trait_same_impl( char const * types, - boost::core::is_same same, char const * file, int line, char const * function ) -{ - if( test_trait_same_impl_( same ) ) - { - test_results(); - } - else - { - BOOST_LIGHTWEIGHT_TEST_OSTREAM - << file << "(" << line << "): test 'is_same<" << types << ">'" - << " failed in function '" << function - << "' ('" << test_print() - << "' != '" << test_print() << "')" - << std::endl; - - ++test_results().errors(); - } -} - } // namespace detail } // namespace boost #define BOOST_TEST_TRAIT_TRUE(type) ( ::boost::detail::test_trait_impl(#type, (void(*)type)0, true, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) ) #define BOOST_TEST_TRAIT_FALSE(type) ( ::boost::detail::test_trait_impl(#type, (void(*)type)0, false, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) ) -#define BOOST_TEST_TRAIT_SAME(...) ( ::boost::detail::test_trait_same_impl(#__VA_ARGS__, ::boost::core::is_same<__VA_ARGS__>(), __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) ) + +#if defined(BOOST_ALLOW_DEPRECATED_HEADERS) +# define BOOST_TEST_TRAIT_SAME BOOST_TEST_SAME +#else +# define BOOST_TEST_TRAIT_SAME BOOST_PRAGMA_MESSAGE("BOOST_TEST_TRAIT_SAME is deprecated. Please use BOOST_TEST_SAME from instead.") BOOST_TEST_SAME +#endif #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index dfe0b6a..f9f9685 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -104,6 +104,9 @@ run-fail lightweight_test_le_fail.cpp ; run-fail lightweight_test_gt_fail.cpp ; run-fail lightweight_test_ge_fail.cpp ; +run lightweight_test_same.cpp ; +run-fail lightweight_test_same_fail.cpp ; + run is_same_test.cpp ; run typeinfo_test.cpp ; diff --git a/test/lightweight_test_fail12.cpp b/test/lightweight_test_fail12.cpp index fb3e0ac..5253528 100644 --- a/test/lightweight_test_fail12.cpp +++ b/test/lightweight_test_fail12.cpp @@ -8,6 +8,7 @@ // http://www.boost.org/LICENSE_1_0.txt // +#define BOOST_ALLOW_DEPRECATED_HEADERS #include #include diff --git a/test/lightweight_test_same.cpp b/test/lightweight_test_same.cpp new file mode 100644 index 0000000..297ffed --- /dev/null +++ b/test/lightweight_test_same.cpp @@ -0,0 +1,36 @@ +// +// Test for BOOST_TEST_SAME +// +// Copyright 2014, 2019 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include + +struct X +{ + typedef int type; +}; + +template struct Y +{ + typedef T1 type; +}; + +int main() +{ + BOOST_TEST_SAME(X, X); + BOOST_TEST_SAME(void, void); + BOOST_TEST_SAME(char[1], char[1]); + BOOST_TEST_SAME(char[], char[]); + BOOST_TEST_SAME(void(), void()); + BOOST_TEST_SAME(X::type, X::type); + BOOST_TEST_SAME(X::type, Y::type); + BOOST_TEST_SAME(Y, Y); + BOOST_TEST_SAME(Y::type, void); + + return boost::report_errors(); +} diff --git a/test/lightweight_test_same_fail.cpp b/test/lightweight_test_same_fail.cpp new file mode 100644 index 0000000..2a528e3 --- /dev/null +++ b/test/lightweight_test_same_fail.cpp @@ -0,0 +1,68 @@ +// +// Negative test for BOOST_TEST_SAME +// +// Copyright 2014, 2019 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// + +#include +#include + +struct X +{ + typedef int type; +}; + +template struct Y +{ + typedef T1 type; +}; + +typedef int I1; +typedef const int I2; +typedef volatile int I3; +typedef const volatile int I4; +typedef int& I5; +typedef const int& I6; +typedef volatile int& I7; +typedef const volatile int& I8; +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) +typedef int&& I9; +typedef const int&& I10; +typedef volatile int&& I11; +typedef const volatile int&& I12; +#endif + +int main() +{ + BOOST_TEST_SAME(char[1], char[2]); + BOOST_TEST_SAME(char[1], char[]); + BOOST_TEST_SAME(char[1], char*); + BOOST_TEST_SAME(void(), void(int)); + BOOST_TEST_SAME(void(), void(*)()); + BOOST_TEST_SAME(X, void); + BOOST_TEST_SAME(X::type, void); + BOOST_TEST_SAME(X, Y); + BOOST_TEST_SAME(X::type, Y::type); + BOOST_TEST_SAME(Y, Y); + BOOST_TEST_SAME(I1, I2); + BOOST_TEST_SAME(I3, I4); + BOOST_TEST_SAME(I5, I6); + BOOST_TEST_SAME(I7, I8); + + int expected = 14; + +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) + + BOOST_TEST_SAME(I9, I10); + BOOST_TEST_SAME(I11, I12); + + expected += 2; + +#endif + + return boost::report_errors() == expected; +} diff --git a/test/lightweight_test_test4.cpp b/test/lightweight_test_test4.cpp index a3e8b04..085476e 100644 --- a/test/lightweight_test_test4.cpp +++ b/test/lightweight_test_test4.cpp @@ -8,6 +8,7 @@ // http://www.boost.org/LICENSE_1_0.txt // +#define BOOST_ALLOW_DEPRECATED_HEADERS #include struct X