diff --git a/include/boost/endian/conversion.hpp b/include/boost/endian/conversion.hpp index ca29176..5edba2c 100644 --- a/include/boost/endian/conversion.hpp +++ b/include/boost/endian/conversion.hpp @@ -5,8 +5,8 @@ // Distributed under the Boost Software License, Version 1.0. // http://www.boost.org/LICENSE_1_0.txt -#ifndef BOOST_ENDIAN_CONVERTERS_HPP -#define BOOST_ENDIAN_CONVERTERS_HPP +#ifndef BOOST_ENDIAN_CONVERSION_HPP +#define BOOST_ENDIAN_CONVERSION_HPP #include #include @@ -505,4 +505,4 @@ namespace endian } // namespace endian } // namespace boost -#endif // BOOST_ENDIAN_CONVERTERS_HPP +#endif // BOOST_ENDIAN_CONVERSION_HPP diff --git a/include/boost/endian/detail/lightweight_test.hpp b/include/boost/endian/detail/lightweight_test.hpp new file mode 100644 index 0000000..beaf0f4 --- /dev/null +++ b/include/boost/endian/detail/lightweight_test.hpp @@ -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 +#include +#include +#include // for memcmp +#include + +// 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 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 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 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 diff --git a/test/buffer_test.cpp b/test/buffer_test.cpp index f04fdaa..5f4342c 100644 --- a/test/buffer_test.cpp +++ b/test/buffer_test.cpp @@ -14,7 +14,7 @@ //#define BOOST_ENDIAN_LOG #include #include -#include +#include #include #include diff --git a/test/conversion_test.cpp b/test/conversion_test.cpp index 82588d0..ada5929 100644 --- a/test/conversion_test.cpp +++ b/test/conversion_test.cpp @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include diff --git a/test/deprecated_test.cpp b/test/deprecated_test.cpp index ddc33eb..e4417f6 100644 --- a/test/deprecated_test.cpp +++ b/test/deprecated_test.cpp @@ -14,7 +14,7 @@ #define BOOST_ENDIAN_DEPRECATED_NAMES #include #include -#include +#include #include #include diff --git a/test/float_test.cpp b/test/float_test.cpp index 2ba5618..cb21901 100644 --- a/test/float_test.cpp +++ b/test/float_test.cpp @@ -12,9 +12,9 @@ #include //#define BOOST_ENDIAN_LOG -#include +#include #include -#include +#include #include #include @@ -23,6 +23,7 @@ using std::cout; using std::endl; using std::numeric_limits; + namespace { template @@ -62,6 +63,30 @@ namespace cout << " denorm_min() " << numeric_limits::denorm_min() << "\n"; } + template + void round_trip_test(const char* type) + { + BOOST_TEST_MEMCMP_EQ(static_cast(1.0), static_cast(1.0)); // reality check + BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits::min())), + numeric_limits::min()); + BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits::max())), + numeric_limits::max()); + BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits::lowest())), + numeric_limits::lowest()); + BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits::epsilon())), + numeric_limits::epsilon()); + BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits::round_error())), + numeric_limits::round_error()); + BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits::infinity())), + numeric_limits::infinity()); + BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits::quiet_NaN())), + numeric_limits::quiet_NaN()); + BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits::signaling_NaN())), + numeric_limits::signaling_NaN()); + BOOST_TEST_MEMCMP_EQ(endian_reverse(endian_reverse(numeric_limits::denorm_min())), + numeric_limits::denorm_min()); + } + } // unnamed namespace //--------------------------------------------------------------------------------------// @@ -70,12 +95,22 @@ int cpp_main(int, char *[]) { 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"); + round_trip_test("float"); + report_limits("double"); + round_trip_test("double"); cout << "\n done" << endl; - return ::boost::report_errors(); + return ::boost::endian::report_errors(); } #include diff --git a/test/msvc/common.props b/test/msvc/common.props index f2f0204..8b8b2f0 100644 --- a/test/msvc/common.props +++ b/test/msvc/common.props @@ -10,7 +10,7 @@ ..\..\..\..\..;%(AdditionalIncludeDirectories) EnableAllWarnings - BOOST_ALL_DYN_LINK;_UNICODE;UNICODE;%(PreprocessorDefinitions) + BOOST_LIGHTWEIGHT_TEST_OSTREAM=std::cout;BOOST_ALL_DYN_LINK;_UNICODE;UNICODE;%(PreprocessorDefinitions) Executing test $(TargetName).exe...