From abf5a45b0901af2c1a925d1f4c94c53681a5c20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Fri, 10 Jul 2015 20:49:53 +0200 Subject: [PATCH 1/3] Added BOOST_ASSERT_IS_VOID macro --- assert.html | 35 +++++++++++- include/boost/assert.hpp | 9 ++- test/Jamfile.v2 | 1 + test/assert_is_void_test.cpp | 108 +++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 test/assert_is_void_test.cpp diff --git a/assert.html b/assert.html index 4723ff8..9cd0706 100644 --- a/assert.html +++ b/assert.html @@ -22,6 +22,7 @@ BOOST_ASSERT_MSG
BOOST_VERIFY
BOOST_VERIFY_MSG
+ BOOST_ASSERT_IS_VOID

BOOST_ASSERT

@@ -113,8 +114,40 @@ defined, to BOOST_ASSERT_MSG(expr,msg) when it's not.


+

BOOST_ASSERT_IS_VOID

+

The macro BOOST_ASSERT_IS_VOID is defined when BOOST_ASSERT and BOOST_ASSERT_MSG, are expanded to ((void)0). + This macro is useful to avoid compiling and potentially running code that is only intended to prepare data to be used in the assertion.

+
+
+void MyContainer::erase(iterator i)
+{
+  //Some sanity checks, data must be ordered
+  #ifndef BOOST_ASSERT_IS_EMPTY
+     if(i != c.begin()){
+        iterator prev = i;
+        --prev;
+        BOOST_ASSERT(*prev < *i);
+     }
+     else if(i != c.end()){
+        iterator next = i;
+        ++next;
+        BOOST_ASSERT(*i < *next);
+     }
+  #endif
+  this->erase_impl(i);
+}
+
+
+ + +

• By default, BOOST_ASSERT_IS_VOID is defined if NDEBUG is defined.

+

• If the macro BOOST_DISABLE_ASSERTS is defined BOOST_ASSERT_IS_VOID is always defined.

+

• If the macro BOOST_ENABLE_ASSERT_HANDLER is defined BOOST_ASSERT_IS_VOID is never defined.

+

• If the macro BOOST_ENABLE_ASSERT_DEBUG_HANDLER, then BOOST_ASSERT_IS_VOID is defined when NDEBUG is defined.

+
+

Copyright © 2002, 2007, 2014 by Peter Dimov.  Copyright © 2011 - by Beman Dawes. Distributed under the Boost Software + by Beman Dawes.  Copyright © 2015 by Ion Gaztanaga. 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.

diff --git a/include/boost/assert.hpp b/include/boost/assert.hpp index 1713d9b..9650d7a 100644 --- a/include/boost/assert.hpp +++ b/include/boost/assert.hpp @@ -3,10 +3,12 @@ // BOOST_ASSERT_MSG(expr, msg) // BOOST_VERIFY(expr) // BOOST_VERIFY_MSG(expr, msg) +// BOOST_ASSERT_IS_VOID // // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd. // Copyright (c) 2007, 2014 Peter Dimov // Copyright (c) Beman Dawes 2011 +// Copyright (c) 2015 Ion Gaztanaga // // Distributed under the Boost Software License, Version 1.0. // See accompanying file LICENSE_1_0.txt or copy at @@ -24,16 +26,18 @@ // // -// BOOST_ASSERT, BOOST_ASSERT_MSG +// BOOST_ASSERT, BOOST_ASSERT_MSG, BOOST_ASSERT_IS_VOID // #undef BOOST_ASSERT #undef BOOST_ASSERT_MSG +#undef BOOST_ASSERT_IS_VOID #if defined(BOOST_DISABLE_ASSERTS) || ( defined(BOOST_ENABLE_ASSERT_DEBUG_HANDLER) && defined(NDEBUG) ) # define BOOST_ASSERT(expr) ((void)0) # define BOOST_ASSERT_MSG(expr, msg) ((void)0) +# define BOOST_ASSERT_IS_VOID #elif defined(BOOST_ENABLE_ASSERT_HANDLER) || ( defined(BOOST_ENABLE_ASSERT_DEBUG_HANDLER) && !defined(NDEBUG) ) @@ -55,6 +59,9 @@ namespace boost # define BOOST_ASSERT(expr) assert(expr) # define BOOST_ASSERT_MSG(expr, msg) assert((expr)&&(msg)) +#if defined(NDEBUG) +# define BOOST_ASSERT_IS_VOID +#endif #endif diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 22fed2a..094b96a 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -14,6 +14,7 @@ test-suite "assert" [ run assert_test.cpp ] [ run current_function_test.cpp : : : always_show_run_output ] [ run verify_test.cpp ] + [ run assert_is_void_test.cpp ] # expansion tests are in exp/ so that there is a backslash in the path on Windows [ run exp/assert_exp_test.cpp ] [ run exp/assert_msg_exp_test.cpp ] diff --git a/test/assert_is_void_test.cpp b/test/assert_is_void_test.cpp new file mode 100644 index 0000000..86ee329 --- /dev/null +++ b/test/assert_is_void_test.cpp @@ -0,0 +1,108 @@ +// +// assert_is_void_test.cpp - tests BOOST_ASSERT_IS_VOID +// +// Copyright (c) 2015 Ion Gaztanaga +// +// 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 + +// default case, !NDEBUG +// BOOST_ASSERT(x) -> assert(x) + +#undef NDEBUG +#include + +#ifdef BOOST_ASSERT_IS_VOID +#error "Error: BOOST_ASSERT should be void in NDEBUG" +#endif + +// default case, NDEBUG +// BOOST_ASSERT(x) -> assert(x) + +#define NDEBUG +#include + +#ifndef BOOST_ASSERT_IS_VOID +#error "Error: BOOST_ASSERT should be void in NDEBUG" +#endif + +// BOOST_DISABLE_ASSERTS, !NDEBUG +// BOOST_ASSERT(x) -> ((void)0) + +#define BOOST_DISABLE_ASSERTS + +#undef NDEBUG +#include + +#ifndef BOOST_ASSERT_IS_VOID +#error "Error: BOOST_ASSERT should be void with BOOST_DISABLE_ASSERTS" +#endif + +// BOOST_DISABLE_ASSERTS, NDEBUG +// BOOST_ASSERT(x) -> ((void)0) + +#define NDEBUG +#include + +#ifndef BOOST_ASSERT_IS_VOID +#error "Error: BOOST_ASSERT should be void with BOOST_DISABLE_ASSERTS and NDEBUG" +#endif + +#undef BOOST_DISABLE_ASSERTS + +// BOOST_ENABLE_ASSERT_HANDLER, !NDEBUG +// BOOST_ASSERT(expr) -> (BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__)) + +#define BOOST_ENABLE_ASSERT_HANDLER + +#undef NDEBUG +#include + +#ifdef BOOST_ASSERT_IS_VOID +#error "Error: BOOST_ASSERT should NOT be void with BOOST_ENABLE_ASSERT_HANDLER" +#endif + +// BOOST_ENABLE_ASSERT_HANDLER, NDEBUG +// BOOST_ASSERT(expr) -> (BOOST_LIKELY(!!(expr))? ((void)0): ::boost::assertion_failed(#expr, BOOST_CURRENT_FUNCTION, __FILE__, __LINE__)) + +#define NDEBUG +#include + +#ifdef BOOST_ASSERT_IS_VOID +#error "Error: BOOST_ASSERT should NOT be void with BOOST_ENABLE_ASSERT_HANDLER" +#endif + +#undef BOOST_ENABLE_ASSERT_HANDLER + +// BOOST_ENABLE_ASSERT_DEBUG_HANDLER, !NDEBUG +// same as BOOST_ENABLE_ASSERT_HANDLER + +#define BOOST_ENABLE_ASSERT_DEBUG_HANDLER + +#undef NDEBUG +#include + +#ifdef BOOST_ASSERT_IS_VOID +#error "Error: BOOST_ASSERT should NOT be void with BOOST_ENABLE_ASSERT_DEBUG_HANDLER and !NDEBUG" +#endif + +// BOOST_ENABLE_ASSERT_DEBUG_HANDLER, NDEBUG +// BOOST_ASSERT(x) -> ((void)0) + +#define NDEBUG +#include + +#ifndef BOOST_ASSERT_IS_VOID +#error "Error: BOOST_ASSERT should be void with BOOST_ENABLE_ASSERT_DEBUG_HANDLER and NDEBUG" +#endif + +#undef BOOST_ENABLE_ASSERT_DEBUG_HANDLER + +int main() +{ + return 0; +} From 9f21dfea1b58154021e4aea7bd24552dc1a77aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Thu, 23 Jul 2015 10:47:05 +0200 Subject: [PATCH 2/3] Fixed documentation and test-case typos, after Peter Dimov's review of the PR#5 (https://github.com/boostorg/assert/pull/5) --- assert.html | 2 +- test/assert_is_void_test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/assert.html b/assert.html index 9cd0706..e1c5b4f 100644 --- a/assert.html +++ b/assert.html @@ -122,7 +122,7 @@ void MyContainer::erase(iterator i) { //Some sanity checks, data must be ordered - #ifndef BOOST_ASSERT_IS_EMPTY + #ifndef BOOST_ASSERT_IS_VOID if(i != c.begin()){ iterator prev = i; --prev; diff --git a/test/assert_is_void_test.cpp b/test/assert_is_void_test.cpp index 86ee329..7a2d0ba 100644 --- a/test/assert_is_void_test.cpp +++ b/test/assert_is_void_test.cpp @@ -27,7 +27,7 @@ #include #ifndef BOOST_ASSERT_IS_VOID -#error "Error: BOOST_ASSERT should be void in NDEBUG" +#error "BOOST_ASSERT should NOT be void if NDEBUG is not defined" #endif // BOOST_DISABLE_ASSERTS, !NDEBUG From 1b7207ac44dcbfecfcc30bac171ef28e6d0e51fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Thu, 23 Jul 2015 10:53:16 +0200 Subject: [PATCH 3/3] Fixed wrong patch for the test --- test/assert_is_void_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/assert_is_void_test.cpp b/test/assert_is_void_test.cpp index 7a2d0ba..5e64a68 100644 --- a/test/assert_is_void_test.cpp +++ b/test/assert_is_void_test.cpp @@ -17,7 +17,7 @@ #include #ifdef BOOST_ASSERT_IS_VOID -#error "Error: BOOST_ASSERT should be void in NDEBUG" +#error "BOOST_ASSERT should NOT be void if NDEBUG is not defined" #endif // default case, NDEBUG @@ -27,7 +27,7 @@ #include #ifndef BOOST_ASSERT_IS_VOID -#error "BOOST_ASSERT should NOT be void if NDEBUG is not defined" +#error "Error: BOOST_ASSERT should be void in NDEBUG" #endif // BOOST_DISABLE_ASSERTS, !NDEBUG