From dcbe62c6bf59cf98adc1d42a27c19689c1f488f9 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 6 Feb 2019 02:02:40 +0200 Subject: [PATCH] Add BOOST_TEST_TRAIT_SAME --- include/boost/core/lightweight_test_trait.hpp | 29 ++++++++++++++- test/Jamfile.v2 | 2 + test/lightweight_test_test4.cpp | 36 ++++++++++++++++++ test/lightweight_test_test5.cpp | 37 +++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 test/lightweight_test_test4.cpp create mode 100644 test/lightweight_test_test5.cpp diff --git a/include/boost/core/lightweight_test_trait.hpp b/include/boost/core/lightweight_test_trait.hpp index 13aa3f0..0e7e1ab 100644 --- a/include/boost/core/lightweight_test_trait.hpp +++ b/include/boost/core/lightweight_test_trait.hpp @@ -9,7 +9,7 @@ // boost/core/lightweight_test_trait.hpp // -// BOOST_TEST_TRAIT_TRUE, BOOST_TEST_TRAIT_FALSE +// BOOST_TEST_TRAIT_TRUE, BOOST_TEST_TRAIT_FALSE, BOOST_TEST_TRAIT_SAME // // Copyright 2014 Peter Dimov // @@ -19,6 +19,7 @@ #include #include +#include namespace boost { @@ -46,11 +47,37 @@ template< class T > inline void test_trait_impl( char const * trait, void (*)( T } } +template inline bool test_trait_same_impl_( boost::core::is_same ) +{ + return boost::core::is_same::value; +} + +template inline void test_trait_same_impl( char const * types, void (*)( boost::core::is_same ), + char const * file, int line, char const * function ) +{ + if( test_trait_same_impl_( boost::core::is_same() ) ) + { + test_results(); + } + else + { + BOOST_LIGHTWEIGHT_TEST_OSTREAM + << file << "(" << line << "): test 'is_same<" << types << ">'" + << " failed in function '" << function + << "' ('" << boost::core::demangled_name( BOOST_CORE_TYPEID(T1) ) + << "' != '" << boost::core::demangled_name( BOOST_CORE_TYPEID(T2) ) << "')" + << 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__, (void(*)(::boost::core::is_same<__VA_ARGS__>))0, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) ) #endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 7b7875d..ea53b58 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -76,6 +76,8 @@ run lightweight_test_lt_le_test.cpp ; run lightweight_test_gt_ge_test.cpp ; run lightweight_test_eq_nullptr.cpp ; run lightweight_test_test3.cpp ; +run lightweight_test_test4.cpp ; +run lightweight_test_test5.cpp ; run-fail lightweight_test_fail.cpp ; run-fail lightweight_test_fail2.cpp ; diff --git a/test/lightweight_test_test4.cpp b/test/lightweight_test_test4.cpp new file mode 100644 index 0000000..a3e8b04 --- /dev/null +++ b/test/lightweight_test_test4.cpp @@ -0,0 +1,36 @@ +// +// Test for BOOST_TEST_TRAIT_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_TRAIT_SAME(X, X); + BOOST_TEST_TRAIT_SAME(void, void); + BOOST_TEST_TRAIT_SAME(char[1], char[1]); + BOOST_TEST_TRAIT_SAME(char[], char[]); + BOOST_TEST_TRAIT_SAME(void(), void()); + BOOST_TEST_TRAIT_SAME(X::type, X::type); + BOOST_TEST_TRAIT_SAME(X::type, Y::type); + BOOST_TEST_TRAIT_SAME(Y, Y); + BOOST_TEST_TRAIT_SAME(Y::type, void); + + return boost::report_errors(); +} diff --git a/test/lightweight_test_test5.cpp b/test/lightweight_test_test5.cpp new file mode 100644 index 0000000..122dced --- /dev/null +++ b/test/lightweight_test_test5.cpp @@ -0,0 +1,37 @@ +// +// Negative test for BOOST_TEST_TRAIT_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_TRAIT_SAME(char[1], char[2]); + BOOST_TEST_TRAIT_SAME(char[1], char[]); + BOOST_TEST_TRAIT_SAME(char[1], char*); + BOOST_TEST_TRAIT_SAME(void(), void(int)); + BOOST_TEST_TRAIT_SAME(void(), void(*)()); + BOOST_TEST_TRAIT_SAME(X, void); + BOOST_TEST_TRAIT_SAME(X::type, void); + BOOST_TEST_TRAIT_SAME(X, Y); + BOOST_TEST_TRAIT_SAME(X::type, Y::type); + BOOST_TEST_TRAIT_SAME(Y, Y); + + return boost::report_errors() == 10; +}