mirror of
https://github.com/boostorg/endian.git
synced 2025-07-30 20:47:26 +02:00
Add Endian specific lightweight_test.hpp with new macro BOOST_TEST_MEMCMP_EQ that uses std::memcmp(...)==0 to determine equality. Bitwise equality is the standard we want floating point round trips to be held to, since NaN's are then equal.
This commit is contained in:
@ -5,8 +5,8 @@
|
|||||||
// Distributed under the Boost Software License, Version 1.0.
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
// http://www.boost.org/LICENSE_1_0.txt
|
// http://www.boost.org/LICENSE_1_0.txt
|
||||||
|
|
||||||
#ifndef BOOST_ENDIAN_CONVERTERS_HPP
|
#ifndef BOOST_ENDIAN_CONVERSION_HPP
|
||||||
#define BOOST_ENDIAN_CONVERTERS_HPP
|
#define BOOST_ENDIAN_CONVERSION_HPP
|
||||||
|
|
||||||
#include <boost/config.hpp>
|
#include <boost/config.hpp>
|
||||||
#include <boost/predef/detail/endian_compat.h>
|
#include <boost/predef/detail/endian_compat.h>
|
||||||
@ -505,4 +505,4 @@ namespace endian
|
|||||||
} // namespace endian
|
} // namespace endian
|
||||||
} // namespace boost
|
} // namespace boost
|
||||||
|
|
||||||
#endif // BOOST_ENDIAN_CONVERTERS_HPP
|
#endif // BOOST_ENDIAN_CONVERSION_HPP
|
||||||
|
199
include/boost/endian/detail/lightweight_test.hpp
Normal file
199
include/boost/endian/detail/lightweight_test.hpp
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
// boost/endian/detail/lightweight_test.hpp --------------------------------------------//
|
||||||
|
|
||||||
|
#ifndef BOOST_ENDIAN_LIGHTWEIGHT_TEST_HPP
|
||||||
|
#define BOOST_ENDIAN_LIGHTWEIGHT_TEST_HPP
|
||||||
|
|
||||||
|
// MS compatible compilers support #pragma once
|
||||||
|
|
||||||
|
#if defined(_MSC_VER)
|
||||||
|
# pragma once
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//
|
||||||
|
// Copyright (c) 2002, 2009, 2014 Peter Dimov
|
||||||
|
// Copyright (2) Beman Dawes 2010, 2011
|
||||||
|
// Copyright (3) Ion Gaztanaga 2013
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0.
|
||||||
|
// See http://www.boost.org/LICENSE_1_0.txt
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <boost/assert.hpp>
|
||||||
|
#include <boost/current_function.hpp>
|
||||||
|
#include <boost/core/no_exceptions_support.hpp>
|
||||||
|
#include <cstring> // for memcmp
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
// IDE's like Visual Studio perform better if output goes to std::cout or
|
||||||
|
// some other stream, so allow user to configure output stream:
|
||||||
|
#ifndef BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||||
|
# define BOOST_LIGHTWEIGHT_TEST_OSTREAM std::cerr
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace boost
|
||||||
|
{
|
||||||
|
namespace endian
|
||||||
|
{
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
struct report_errors_reminder
|
||||||
|
{
|
||||||
|
bool called_report_errors_function;
|
||||||
|
|
||||||
|
report_errors_reminder() : called_report_errors_function(false) {}
|
||||||
|
|
||||||
|
~report_errors_reminder()
|
||||||
|
{
|
||||||
|
BOOST_ASSERT(called_report_errors_function); // verify report_errors() was called
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
inline report_errors_reminder& report_errors_remind()
|
||||||
|
{
|
||||||
|
static report_errors_reminder r;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline int & test_errors()
|
||||||
|
{
|
||||||
|
static int x = 0;
|
||||||
|
report_errors_remind();
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void test_failed_impl(char const * expr, char const * file, int line, char const * function)
|
||||||
|
{
|
||||||
|
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||||
|
<< file << "(" << line << "): test '" << expr << "' failed in function '"
|
||||||
|
<< function << "'" << std::endl;
|
||||||
|
++test_errors();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void error_impl(char const * msg, char const * file, int line, char const * function)
|
||||||
|
{
|
||||||
|
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||||
|
<< file << "(" << line << "): " << msg << " in function '"
|
||||||
|
<< function << "'" << std::endl;
|
||||||
|
++test_errors();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void throw_failed_impl(char const * excep, char const * file, int line, char const * function)
|
||||||
|
{
|
||||||
|
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||||
|
<< file << "(" << line << "): Exception '" << excep << "' not thrown in function '"
|
||||||
|
<< function << "'" << std::endl;
|
||||||
|
++test_errors();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> inline void test_eq_impl( char const * expr1, char const * expr2,
|
||||||
|
char const * file, int line, char const * function, T const & t, U const & u )
|
||||||
|
{
|
||||||
|
if( t == u )
|
||||||
|
{
|
||||||
|
report_errors_remind();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||||
|
<< file << "(" << line << "): test '" << expr1 << " == " << expr2
|
||||||
|
<< "' failed in function '" << function << "': "
|
||||||
|
<< "'" << t << "' != '" << u << "'" << std::endl;
|
||||||
|
++test_errors();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> inline void test_ne_impl( char const * expr1, char const * expr2,
|
||||||
|
char const * file, int line, char const * function, T const & t, U const & u )
|
||||||
|
{
|
||||||
|
if( t != u )
|
||||||
|
{
|
||||||
|
report_errors_remind();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||||
|
<< file << "(" << line << "): test '" << expr1 << " != " << expr2
|
||||||
|
<< "' failed in function '" << function << "': "
|
||||||
|
<< "'" << t << "' == '" << u << "'" << std::endl;
|
||||||
|
++test_errors();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T, class U> inline void test_memcmp_eq_impl(char const * expr1,
|
||||||
|
char const * expr2, char const * file, int line, char const * function, T const & t,
|
||||||
|
U const & u)
|
||||||
|
{
|
||||||
|
if (sizeof(T) == sizeof(U)
|
||||||
|
&& std::memcmp(&t, &u, sizeof(T)) == 0)
|
||||||
|
{
|
||||||
|
report_errors_remind();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||||
|
<< file << "(" << line << "): test 'std::memcmp(" << expr1 << ", " << expr2
|
||||||
|
<< ") == 0' fails in function '" << function << "': "
|
||||||
|
<< " with values '" << t << "' and '" << u << "'" << std::endl;
|
||||||
|
++test_errors();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
inline int report_errors()
|
||||||
|
{
|
||||||
|
boost::endian::detail::report_errors_remind().called_report_errors_function = true;
|
||||||
|
|
||||||
|
int errors = boost::endian::detail::test_errors();
|
||||||
|
|
||||||
|
if( errors == 0 )
|
||||||
|
{
|
||||||
|
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||||
|
<< "No errors detected." << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
BOOST_LIGHTWEIGHT_TEST_OSTREAM
|
||||||
|
<< errors << " error" << (errors == 1? "": "s") << " detected." << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace endian
|
||||||
|
} // namespace boost
|
||||||
|
|
||||||
|
#define BOOST_TEST(expr) \
|
||||||
|
((expr)? (void)0: ::boost::endian::detail::test_failed_impl(#expr, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION))
|
||||||
|
|
||||||
|
#define BOOST_ERROR(msg) \
|
||||||
|
( ::boost::endian::detail::error_impl(msg, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION) )
|
||||||
|
|
||||||
|
#define BOOST_TEST_EQ(expr1,expr2) \
|
||||||
|
( ::boost::endian::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
|
||||||
|
#define BOOST_TEST_NE(expr1,expr2) \
|
||||||
|
( ::boost::endian::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) )
|
||||||
|
|
||||||
|
#define BOOST_TEST_MEMCMP_EQ(expr1,expr2) \
|
||||||
|
(::boost::endian::detail::test_memcmp_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2))
|
||||||
|
|
||||||
|
#ifndef BOOST_NO_EXCEPTIONS
|
||||||
|
#define BOOST_TEST_THROWS( EXPR, EXCEP ) \
|
||||||
|
try { \
|
||||||
|
EXPR; \
|
||||||
|
::boost::detail::throw_failed_impl \
|
||||||
|
(#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
|
||||||
|
} \
|
||||||
|
catch(EXCEP const&) { \
|
||||||
|
} \
|
||||||
|
catch(...) { \
|
||||||
|
::boost::detail::throw_failed_impl \
|
||||||
|
(#EXCEP, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
|
||||||
|
} \
|
||||||
|
//
|
||||||
|
#else
|
||||||
|
#define BOOST_TEST_THROWS( EXPR, EXCEP )
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // #ifndef BOOST_ENDIAN_LIGHTWEIGHT_TEST_HPP
|
@ -14,7 +14,7 @@
|
|||||||
//#define BOOST_ENDIAN_LOG
|
//#define BOOST_ENDIAN_LOG
|
||||||
#include <boost/endian/buffers.hpp>
|
#include <boost/endian/buffers.hpp>
|
||||||
#include <boost/detail/lightweight_main.hpp>
|
#include <boost/detail/lightweight_main.hpp>
|
||||||
#include <boost/detail/lightweight_test.hpp>
|
#include <boost/core/lightweight_test.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
#include <boost/endian/conversion.hpp>
|
#include <boost/endian/conversion.hpp>
|
||||||
#include <boost/detail/lightweight_main.hpp>
|
#include <boost/detail/lightweight_main.hpp>
|
||||||
#include <boost/detail/lightweight_test.hpp>
|
#include <boost/core/lightweight_test.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
#define BOOST_ENDIAN_DEPRECATED_NAMES
|
#define BOOST_ENDIAN_DEPRECATED_NAMES
|
||||||
#include <boost/endian/endian.hpp>
|
#include <boost/endian/endian.hpp>
|
||||||
#include <boost/detail/lightweight_main.hpp>
|
#include <boost/detail/lightweight_main.hpp>
|
||||||
#include <boost/detail/lightweight_test.hpp>
|
#include <boost/core/lightweight_test.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
@ -12,9 +12,9 @@
|
|||||||
#include <boost/endian/detail/disable_warnings.hpp>
|
#include <boost/endian/detail/disable_warnings.hpp>
|
||||||
|
|
||||||
//#define BOOST_ENDIAN_LOG
|
//#define BOOST_ENDIAN_LOG
|
||||||
#include <boost/endian/buffers.hpp>
|
#include <boost/endian/conversion.hpp>
|
||||||
#include <boost/detail/lightweight_main.hpp>
|
#include <boost/detail/lightweight_main.hpp>
|
||||||
#include <boost/detail/lightweight_test.hpp>
|
#include <boost/endian/detail/lightweight_test.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
@ -23,6 +23,7 @@ using std::cout;
|
|||||||
using std::endl;
|
using std::endl;
|
||||||
using std::numeric_limits;
|
using std::numeric_limits;
|
||||||
|
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
template <class T>
|
template <class T>
|
||||||
@ -62,6 +63,30 @@ namespace
|
|||||||
cout << " denorm_min() " << numeric_limits<T>::denorm_min() << "\n";
|
cout << " denorm_min() " << numeric_limits<T>::denorm_min() << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void round_trip_test(const char* type)
|
||||||
|
{
|
||||||
|
BOOST_TEST_MEMCMP_EQ(static_cast<T>(1.0), static_cast<T>(1.0)); // reality check
|
||||||
|
BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits<T>::min())),
|
||||||
|
numeric_limits<T>::min());
|
||||||
|
BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits<T>::max())),
|
||||||
|
numeric_limits<T>::max());
|
||||||
|
BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits<T>::lowest())),
|
||||||
|
numeric_limits<T>::lowest());
|
||||||
|
BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits<T>::epsilon())),
|
||||||
|
numeric_limits<T>::epsilon());
|
||||||
|
BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits<T>::round_error())),
|
||||||
|
numeric_limits<T>::round_error());
|
||||||
|
BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits<T>::infinity())),
|
||||||
|
numeric_limits<T>::infinity());
|
||||||
|
BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits<T>::quiet_NaN())),
|
||||||
|
numeric_limits<T>::quiet_NaN());
|
||||||
|
BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits<T>::signaling_NaN())),
|
||||||
|
numeric_limits<T>::signaling_NaN());
|
||||||
|
BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits<T>::denorm_min())),
|
||||||
|
numeric_limits<T>::denorm_min());
|
||||||
|
}
|
||||||
|
|
||||||
} // unnamed namespace
|
} // unnamed namespace
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------//
|
//--------------------------------------------------------------------------------------//
|
||||||
@ -70,12 +95,22 @@ int cpp_main(int, char *[])
|
|||||||
{
|
{
|
||||||
cout << "byte swap intrinsics: " BOOST_ENDIAN_INTRINSIC_MSG << endl;
|
cout << "byte swap intrinsics: " BOOST_ENDIAN_INTRINSIC_MSG << endl;
|
||||||
|
|
||||||
|
//#define BOOST_ENDIAN_FORCE_ERROR
|
||||||
|
#ifdef BOOST_ENDIAN_FORCE_ERROR
|
||||||
|
BOOST_TEST_MEMCMP_EQ(1.0f, 1.0);
|
||||||
|
BOOST_TEST_MEMCMP_EQ(1.0f, 1.1f);
|
||||||
|
BOOST_TEST_MEMCMP_EQ(1.0, 1.1);
|
||||||
|
#endif
|
||||||
|
|
||||||
report_limits<float>("float");
|
report_limits<float>("float");
|
||||||
|
round_trip_test<float>("float");
|
||||||
|
|
||||||
report_limits<double>("double");
|
report_limits<double>("double");
|
||||||
|
round_trip_test<double>("double");
|
||||||
|
|
||||||
cout << "\n done" << endl;
|
cout << "\n done" << endl;
|
||||||
|
|
||||||
return ::boost::report_errors();
|
return ::boost::endian::report_errors();
|
||||||
}
|
}
|
||||||
|
|
||||||
#include <boost/endian/detail/disable_warnings_pop.hpp>
|
#include <boost/endian/detail/disable_warnings_pop.hpp>
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
<ClCompile>
|
<ClCompile>
|
||||||
<AdditionalIncludeDirectories>..\..\..\..\..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
<AdditionalIncludeDirectories>..\..\..\..\..;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||||
<WarningLevel>EnableAllWarnings</WarningLevel>
|
<WarningLevel>EnableAllWarnings</WarningLevel>
|
||||||
<PreprocessorDefinitions>BOOST_ALL_DYN_LINK;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
<PreprocessorDefinitions>BOOST_LIGHTWEIGHT_TEST_OSTREAM=std::cout;BOOST_ALL_DYN_LINK;_UNICODE;UNICODE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<PostBuildEvent>
|
<PostBuildEvent>
|
||||||
<Message>Executing test $(TargetName).exe...</Message>
|
<Message>Executing test $(TargetName).exe...</Message>
|
||||||
|
Reference in New Issue
Block a user