From 46545326d8f4c761a2326e3d888f415aa3db598b Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 16 Mar 2017 01:31:43 +0200 Subject: [PATCH 1/2] Add BOOST_TEST_LT, BOOST_TEST_LE --- doc/lightweight_test.qbk | 34 +++++++++++++++++++---- include/boost/core/lightweight_test.hpp | 37 +++++++++++++++++++++++++ test/Jamfile.v2 | 3 ++ test/lightweight_test_le_fail.cpp | 20 +++++++++++++ test/lightweight_test_lt_fail.cpp | 20 +++++++++++++ test/lightweight_test_lt_le_test.cpp | 34 +++++++++++++++++++++++ 6 files changed, 143 insertions(+), 5 deletions(-) create mode 100644 test/lightweight_test_le_fail.cpp create mode 100644 test/lightweight_test_lt_fail.cpp create mode 100644 test/lightweight_test_lt_le_test.cpp diff --git a/doc/lightweight_test.qbk b/doc/lightweight_test.qbk index ed0990b..8391692 100644 --- a/doc/lightweight_test.qbk +++ b/doc/lightweight_test.qbk @@ -1,7 +1,7 @@ [/ Copyright 2010, 2011 Beman Dawes Copyright 2013 Ion Gaztanaga - Copyright 2014 Peter Dimov + Copyright 2014, 2017 Peter Dimov Distributed under the Boost Software License, Version 1.0. @@ -36,6 +36,8 @@ When using `lightweight_test.hpp`, *do not forget* to #define BOOST_ERROR(message) /*unspecified*/ #define BOOST_TEST_EQ(expr1, expr2) /*unspecified*/ #define BOOST_TEST_NE(expr1, expr2) /*unspecified*/ +#define BOOST_TEST_LT(expr1, expr2) /*unspecified*/ +#define BOOST_TEST_LE(expr1, expr2) /*unspecified*/ #define BOOST_TEST_CSTR_EQ(expr1, expr2) /*unspecified*/ #define BOOST_TEST_CSTR_NE(expr1, expr2) /*unspecified*/ #define BOOST_TEST_ALL_EQ(begin1, end1, begin2, end2) /* unspecified */ @@ -89,7 +91,7 @@ Increases error count and outputs a message containing BOOST_TEST_EQ(expr1, expr2) `` -If `expr1 != expr2` increases the error count and outputs a +If `expr1 == expr2` is not true increases the error count and outputs a message containing both expressions. [endsect] @@ -100,7 +102,29 @@ message containing both expressions. BOOST_TEST_NE(expr1, expr2) `` -If `expr1 == expr2` increases the error count and outputs a +If `expr1 != expr2` is not true increases the error count and outputs a +message containing both expressions. + +[endsect] + +[section BOOST_TEST_LT] + +`` +BOOST_TEST_LT(expr1, expr2) +`` + +If `expr1 < expr2` is not true increases the error count and outputs a +message containing both expressions. + +[endsect] + +[section BOOST_TEST_LE] + +`` +BOOST_TEST_LE(expr1, expr2) +`` + +If `expr1 <= expr2` is not true increases the error count and outputs a message containing both expressions. [endsect] @@ -111,7 +135,7 @@ message containing both expressions. BOOST_TEST_CSTR_EQ(expr1, expr2) `` -Specialization of BOOST_TEST_EQ which interprets expr1 and expr2 as pointers to null-terminated byte strings (C strings). If `std::strcmp(expr1, expr2) != 0`, increase the error count and output a message containing both expressions. +Specialization of `BOOST_TEST_EQ` which interprets `expr1` and `expr2` as pointers to null-terminated byte strings (C strings). If `std::strcmp(expr1, expr2) != 0`, increase the error count and output a message containing both expressions. [endsect] @@ -121,7 +145,7 @@ Specialization of BOOST_TEST_EQ which interprets expr1 and expr2 as pointers to BOOST_TEST_CSTR_NE(expr1, expr2) `` -Specialization of BOOST_TEST_NE which interprets expr1 and expr2 as pointers to null-terminated byte strings (C strings). If `std::strcmp(expr1, expr2) == 0`, increase the error count and output a message containing both expressions. +Specialization of `BOOST_TEST_NE` which interprets `expr1` and `expr2` as pointers to null-terminated byte strings (C strings). If `std::strcmp(expr1, expr2) == 0`, increase the error count and output a message containing both expressions. [endsect] diff --git a/include/boost/core/lightweight_test.hpp b/include/boost/core/lightweight_test.hpp index b580dd2..260a1ac 100644 --- a/include/boost/core/lightweight_test.hpp +++ b/include/boost/core/lightweight_test.hpp @@ -147,6 +147,40 @@ template inline void test_ne_impl( char const * expr1, char co } } +template inline void test_lt_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 << "': " + << "'" << test_output_impl(t) << "' >= '" << test_output_impl(u) << "'" << std::endl; + ++test_errors(); + } +} + +template inline void test_le_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 << "': " + << "'" << test_output_impl(t) << "' > '" << test_output_impl(u) << "'" << std::endl; + ++test_errors(); + } +} + 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 ) { @@ -360,6 +394,9 @@ inline int report_errors() #define BOOST_TEST_EQ(expr1,expr2) ( ::boost::detail::test_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) #define BOOST_TEST_NE(expr1,expr2) ( ::boost::detail::test_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) +#define BOOST_TEST_LT(expr1,expr2) ( ::boost::detail::test_lt_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) +#define BOOST_TEST_LE(expr1,expr2) ( ::boost::detail::test_le_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) + #define BOOST_TEST_CSTR_EQ(expr1,expr2) ( ::boost::detail::test_cstr_eq_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) #define BOOST_TEST_CSTR_NE(expr1,expr2) ( ::boost::detail::test_cstr_ne_impl(#expr1, #expr2, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION, expr1, expr2) ) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 1490cf2..72e62a2 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -68,6 +68,7 @@ run lightweight_test_test2.cpp ; run lightweight_test_all_eq_test.cpp ; run lightweight_test_all_with_test.cpp ; run lightweight_test_all_with_fail.cpp ; +run lightweight_test_lt_le_test.cpp ; run-fail lightweight_test_fail.cpp ; run-fail lightweight_test_fail2.cpp ; @@ -81,6 +82,8 @@ 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-fail lightweight_test_lt_fail.cpp ; +run-fail lightweight_test_le_fail.cpp ; run is_same_test.cpp ; diff --git a/test/lightweight_test_le_fail.cpp b/test/lightweight_test_le_fail.cpp new file mode 100644 index 0000000..9706be4 --- /dev/null +++ b/test/lightweight_test_le_fail.cpp @@ -0,0 +1,20 @@ +// +// Negative test for BOOST_TEST_LE +// +// Copyright 2017 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 + +int main() +{ + int x = 1; + + BOOST_TEST_LE( x, 0 ); + + return boost::report_errors(); +} diff --git a/test/lightweight_test_lt_fail.cpp b/test/lightweight_test_lt_fail.cpp new file mode 100644 index 0000000..ba6d243 --- /dev/null +++ b/test/lightweight_test_lt_fail.cpp @@ -0,0 +1,20 @@ +// +// Negative test for BOOST_TEST_LT +// +// Copyright 2014, 2017 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 + +int main() +{ + int x = 0; + + BOOST_TEST_LT( x, 0 ); + + return boost::report_errors(); +} diff --git a/test/lightweight_test_lt_le_test.cpp b/test/lightweight_test_lt_le_test.cpp new file mode 100644 index 0000000..7f08783 --- /dev/null +++ b/test/lightweight_test_lt_le_test.cpp @@ -0,0 +1,34 @@ +// +// Test for BOOST_TEST_LT, BOOST_TEST_LE +// +// Copyright 2014, 2017 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 + +int main() +{ + int x = 0; + + BOOST_TEST_LT( x, 1 ); + BOOST_TEST_LT( ++x, 2 ); + BOOST_TEST_LT( x++, 3 ); + + BOOST_TEST_LE( x, 2 ); + BOOST_TEST_LE( ++x, 3 ); + BOOST_TEST_LE( x++, 4 ); + + int y = 3; + + BOOST_TEST_LT( ++y, ++x ); + BOOST_TEST_LT( y++, x++ ); + + BOOST_TEST_LE( ++y, x ); + BOOST_TEST_LE( y++, x++ ); + + return boost::report_errors(); +} From dc6003e26f97c574affcdde0620d2ab966963244 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Sat, 22 Apr 2017 23:50:00 -0400 Subject: [PATCH 2/2] Disable addressof for r-values when possible --- include/boost/core/addressof.hpp | 10 ++++++++++ test/Jamfile.v2 | 1 + test/addressof_constexpr_test.cpp | 7 +++---- test/addressof_fail_rvalue.cpp | 26 ++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 test/addressof_fail_rvalue.cpp diff --git a/include/boost/core/addressof.hpp b/include/boost/core/addressof.hpp index 0a2b46b..efde98a 100644 --- a/include/boost/core/addressof.hpp +++ b/include/boost/core/addressof.hpp @@ -262,4 +262,14 @@ addressof(T& o) BOOST_NOEXCEPT } /* boost */ #endif +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \ + !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) +namespace boost { + +template +const T* addressof(const T&&) = delete; + +} /* boost */ +#endif + #endif diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 72e62a2..fded1cd 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -14,6 +14,7 @@ run addressof_test2.cpp ; run addressof_np_test.cpp ; run addressof_fn_test.cpp ; compile addressof_constexpr_test.cpp ; +compile-fail addressof_fail_rvalue.cpp ; run checked_delete_test.cpp ; compile-fail checked_delete_fail.cpp ; diff --git a/test/addressof_constexpr_test.cpp b/test/addressof_constexpr_test.cpp index 9d93565..d7255a9 100644 --- a/test/addressof_constexpr_test.cpp +++ b/test/addressof_constexpr_test.cpp @@ -1,10 +1,9 @@ /* Copyright 2017 Glen Joseph Fernandes - +(glenjofe@gmail.com) -Distributed under the Boost Software -License, Version 1.0. -http://boost.org/LICENSE_1_0.txt +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) */ #include diff --git a/test/addressof_fail_rvalue.cpp b/test/addressof_fail_rvalue.cpp new file mode 100644 index 0000000..258eaaa --- /dev/null +++ b/test/addressof_fail_rvalue.cpp @@ -0,0 +1,26 @@ +/* +Copyright 2017 Glen Joseph Fernandes +(glenjofe@gmail.com) + +Distributed under the Boost Software License, Version 1.0. +(http://www.boost.org/LICENSE_1_0.txt) +*/ + +#include + +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && \ + !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) +struct type { }; + +inline const type function() +{ + return type(); +} + +int main() +{ + (void)boost::addressof(function()); +} +#else +#error Requires rvalue references and deleted functions +#endif