Split BOOST_TEST_TRAIT_SAME into its own header, rename to BOOST_TEST_SAME

This commit is contained in:
Peter Dimov
2019-12-24 06:20:42 +02:00
parent 253b07f630
commit 0041cd9d9b
7 changed files with 231 additions and 71 deletions

View File

@@ -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 <boost/core/lightweight_test.hpp>
#include <boost/core/typeinfo.hpp>
#include <boost/core/is_same.hpp>
#include <boost/config.hpp>
namespace boost
{
namespace detail
{
template<class, int = 0> struct test_print { };
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T, 2>)
{
return o << boost::core::demangled_name(BOOST_CORE_TYPEID(T));
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T, 1>)
{
return o << test_print<T, 2>();
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<const T, 1>)
{
return o << test_print<T, 2>() << " const";
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<volatile T, 1>)
{
return o << test_print<T, 2>() << " volatile";
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<const volatile T, 1>)
{
return o << test_print<T, 2>() << " const volatile";
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T>)
{
return o << test_print<T, 1>();
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T&>)
{
return o << test_print<T, 1>() << " &";
}
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T&&>)
{
return o << test_print<T, 1>() << " &&";
}
#endif
template<class T> inline bool test_same_impl_( T )
{
return T::value;
}
template<class T1, class T2> inline void test_same_impl( char const * types,
boost::core::is_same<T1, T2> 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<T1>()
<< "' != '" << test_print<T2>() << "')"
<< 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

View File

@@ -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 <boost/core/lightweight_test.hpp>
#include <boost/core/lightweight_test_same.hpp>
#include <boost/core/typeinfo.hpp>
#include <boost/core/is_same.hpp>
#include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
namespace boost
{
@@ -31,50 +34,6 @@ namespace boost
namespace detail
{
template<class, int = 0> struct test_print { };
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T, 2>)
{
return o << boost::core::demangled_name(BOOST_CORE_TYPEID(T));
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T, 1>)
{
return o << test_print<T, 2>();
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<const T, 1>)
{
return o << test_print<T, 2>() << " const";
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<volatile T, 1>)
{
return o << test_print<T, 2>() << " volatile";
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<const volatile T, 1>)
{
return o << test_print<T, 2>() << " const volatile";
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T>)
{
return o << test_print<T, 1>();
}
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T&>)
{
return o << test_print<T, 1>() << " &";
}
#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
template<class T> inline std::ostream& operator<<(std::ostream& o, test_print<T&&>)
{
return o << test_print<T, 1>() << " &&";
}
#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<class T> inline bool test_trait_same_impl_( T )
{
return T::value;
}
template<class T1, class T2> inline void test_trait_same_impl( char const * types,
boost::core::is_same<T1, T2> 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<T1>()
<< "' != '" << test_print<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__, ::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 <boost/core/lightweight_test_same.hpp> instead.") BOOST_TEST_SAME
#endif
#endif // #ifndef BOOST_CORE_LIGHTWEIGHT_TEST_TRAIT_HPP

View File

@@ -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 ;

View File

@@ -8,6 +8,7 @@
// http://www.boost.org/LICENSE_1_0.txt
//
#define BOOST_ALLOW_DEPRECATED_HEADERS
#include <boost/core/lightweight_test_trait.hpp>
#include <boost/config.hpp>

View File

@@ -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 <boost/core/lightweight_test_same.hpp>
struct X
{
typedef int type;
};
template<class T1, class T2> 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<int, float>::type);
BOOST_TEST_SAME(Y<int, float>, Y<int, float>);
BOOST_TEST_SAME(Y<void, float>::type, void);
return boost::report_errors();
}

View File

@@ -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 <boost/core/lightweight_test_same.hpp>
#include <boost/config.hpp>
struct X
{
typedef int type;
};
template<class T1, class T2> 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<void, void>);
BOOST_TEST_SAME(X::type, Y<float, int>::type);
BOOST_TEST_SAME(Y<int, float>, Y<int, double>);
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;
}

View File

@@ -8,6 +8,7 @@
// http://www.boost.org/LICENSE_1_0.txt
//
#define BOOST_ALLOW_DEPRECATED_HEADERS
#include <boost/core/lightweight_test_trait.hpp>
struct X