diff --git a/include/boost/core/lightweight_test.hpp b/include/boost/core/lightweight_test.hpp index 2608582..804c8f4 100644 --- a/include/boost/core/lightweight_test.hpp +++ b/include/boost/core/lightweight_test.hpp @@ -102,6 +102,12 @@ inline void throw_failed_impl(char const * excep, char const * file, int line, c # pragma GCC diagnostic ignored "-Wsign-compare" #endif +// specialize test output for char pointers to avoid printing as cstring +template inline const T& test_output_impl(const T& v) { return v; } +inline const void* test_output_impl(const char* v) { return v; } +inline const void* test_output_impl(const unsigned char* v) { return v; } +inline const void* test_output_impl(const signed char* v) { return v; } + 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 ) { @@ -114,43 +120,7 @@ template inline void test_eq_impl( char const * expr1, char co BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << "(" << line << "): test '" << expr1 << " == " << expr2 << "' failed in function '" << function << "': " - << "'" << t << "' != '" << u << "'" << std::endl; - ++test_errors(); - } -} - -// overloads for pointers compare and print addresses -template inline void test_eq_impl( char const * expr1, char const * expr2, - char const * file, int line, char const * function, const T* const t, const U* const u ) -{ - if( t == u ) - { - report_errors_remind(); - } - else - { - BOOST_LIGHTWEIGHT_TEST_OSTREAM - << file << "(" << line << "): test '" << expr1 << " == " << expr2 - << "' failed in function '" << function << "': " - << "'" << (const void*)t << "' != '" << (const void*)u << "'" << std::endl; - ++test_errors(); - } -} - -// impl for cstring -inline void test_cstr_eq_impl( char const * expr1, char const * expr2, - char const * file, int line, char const * function, const char* const t, const char* const u ) -{ - if( std::strcmp(t, u) == 0 ) - { - report_errors_remind(); - } - else - { - BOOST_LIGHTWEIGHT_TEST_OSTREAM - << file << "(" << line << "): test '" << expr1 << " == " << expr2 - << "' failed in function '" << function << "': " - << "'" << t << "' != '" << u << "'" << std::endl; + << "'" << test_output_impl(t) << "' != '" << test_output_impl(u) << "'" << std::endl; ++test_errors(); } } @@ -167,16 +137,15 @@ template inline void test_ne_impl( char const * expr1, char co BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << "(" << line << "): test '" << expr1 << " != " << expr2 << "' failed in function '" << function << "': " - << "'" << t << "' == '" << u << "'" << std::endl; + << "'" << test_output_impl(t) << "' == '" << test_output_impl(u) << "'" << std::endl; ++test_errors(); } } -// overloads for pointers compare and print addresses -template inline void test_ne_impl( char const * expr1, char const * expr2, - char const * file, int line, char const * function, const T* const t, const U* const u ) +inline void test_cstr_eq_impl( char const * expr1, char const * expr2, + char const * file, int line, char const * function, char const * const t, char const * const u ) { - if( t != u ) + if( std::strcmp(t, u) == 0 ) { report_errors_remind(); } @@ -185,14 +154,13 @@ template inline void test_ne_impl( char const * expr1, char co BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << "(" << line << "): test '" << expr1 << " == " << expr2 << "' failed in function '" << function << "': " - << "'" << (const void*)t << "' == '" << (const void*)u << "'" << std::endl; + << "'" << t << "' != '" << u << "'" << std::endl; ++test_errors(); } } -// impl for cstring inline void test_cstr_ne_impl( char const * expr1, char const * expr2, - char const * file, int line, char const * function, const char* const t, const char* const u ) + char const * file, int line, char const * function, char const * const t, char const * const u ) { if( std::strcmp(t, u) != 0 ) { diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index f93ff51..31ba00f 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -76,6 +76,8 @@ run-fail lightweight_test_fail7.cpp ; run-fail lightweight_test_fail7.cpp : : : off : lightweight_test_fail7_no_rtti ; run-fail lightweight_test_fail8.cpp ; run-fail lightweight_test_fail8.cpp : : : off : lightweight_test_fail8_no_rtti ; +run-fail lightweight_test_fail9.cpp ; +run-fail lightweight_test_fail10.cpp ; run is_same_test.cpp ; diff --git a/test/lightweight_test_fail10.cpp b/test/lightweight_test_fail10.cpp new file mode 100644 index 0000000..388c47e --- /dev/null +++ b/test/lightweight_test_fail10.cpp @@ -0,0 +1,18 @@ +// +// Negative test for BOOST_TEST_EQ on const char* +// +// Copyright (c) 2017 Hans Dembinski +// +// 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 + +int main() +{ + BOOST_TEST_EQ("xab"+1 , "yab"+1); // compares addresses, not cstrings + + return boost::report_errors(); +} diff --git a/test/lightweight_test_fail9.cpp b/test/lightweight_test_fail9.cpp new file mode 100644 index 0000000..a87a4f1 --- /dev/null +++ b/test/lightweight_test_fail9.cpp @@ -0,0 +1,18 @@ +// +// Negative test for BOOST_TEST_CSTR_EQ +// +// Copyright (c) 2017 Hans Dembinski +// +// 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 + +int main() +{ + BOOST_TEST_CSTR_EQ("x" , "y"); + + return boost::report_errors(); +}