mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 02:44:10 +00:00
BOOST_VERIFY added.
[SVN r40728]
This commit is contained in:
parent
42e0001370
commit
56b0846099
10
assert.html
10
assert.html
@ -47,9 +47,13 @@ void assertion_failed(char const * expr, char const * function, char const * fil
|
||||
<P>As is the case with <STRONG><cassert></STRONG>, <STRONG><boost/assert.hpp></STRONG>
|
||||
can be included multiple times in a single translation unit. <STRONG>BOOST_ASSERT</STRONG>
|
||||
will be redefined each time as specified above.</P>
|
||||
<p><STRONG><boost/assert.hpp></STRONG> also defines the macro <STRONG>BOOST_VERIFY</STRONG>.
|
||||
It has exactly the same behavior as <STRONG>BOOST_ASSERT</STRONG>, except
|
||||
that the expression that is passed to <STRONG>BOOST_VERIFY</STRONG> is always
|
||||
evaluated.</p>
|
||||
<p><br>
|
||||
<small>Copyright © 2002 by Peter Dimov. Distributed under the Boost Software License, Version
|
||||
1.0. See accompanying file <A href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or
|
||||
copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>.</small></p>
|
||||
<small>Copyright © 2002, 2007 by Peter Dimov. Distributed under the Boost
|
||||
Software License, Version 1.0. See accompanying file <A href="../../LICENSE_1_0.txt">
|
||||
LICENSE_1_0.txt</A> or copy at <A href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</A>.</small></p>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -2,6 +2,7 @@
|
||||
// boost/assert.hpp - BOOST_ASSERT(expr)
|
||||
//
|
||||
// Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
|
||||
// Copyright (c) 2007 Peter Dimov
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See
|
||||
// accompanying file LICENSE_1_0.txt or copy at
|
||||
@ -35,3 +36,15 @@ void assertion_failed(char const * expr, char const * function, char const * fil
|
||||
# include <assert.h> // .h to support old libraries w/o <cassert> - effect is the same
|
||||
# define BOOST_ASSERT(expr) assert(expr)
|
||||
#endif
|
||||
|
||||
#undef BOOST_VERIFY
|
||||
|
||||
#if defined(BOOST_DISABLE_ASSERTS) || ( !defined(BOOST_ENABLE_ASSERT_HANDLER) && defined(NDEBUG) )
|
||||
|
||||
# define BOOST_VERIFY(expr) ((void)(expr))
|
||||
|
||||
#else
|
||||
|
||||
# define BOOST_VERIFY(expr) BOOST_ASSERT(expr)
|
||||
|
||||
#endif
|
||||
|
@ -33,4 +33,5 @@ test-suite utility
|
||||
[ compile-fail ../value_init_test_fail1.cpp ]
|
||||
[ compile-fail ../value_init_test_fail2.cpp ]
|
||||
[ compile-fail ../value_init_test_fail3.cpp ]
|
||||
[ run verify_test.cpp ]
|
||||
;
|
||||
|
126
verify_test.cpp
Normal file
126
verify_test.cpp
Normal file
@ -0,0 +1,126 @@
|
||||
//
|
||||
// verify_test.cpp - a test for BOOST_VERIFY
|
||||
//
|
||||
// Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
|
||||
// Copyright (c) 2007 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 <boost/detail/lightweight_test.hpp>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
int f( int & x )
|
||||
{
|
||||
return ++x;
|
||||
}
|
||||
|
||||
void test_default()
|
||||
{
|
||||
int x = 1;
|
||||
|
||||
BOOST_VERIFY( 1 );
|
||||
BOOST_VERIFY( x == 1 );
|
||||
BOOST_VERIFY( ++x );
|
||||
BOOST_VERIFY( f(x) );
|
||||
BOOST_VERIFY( &x );
|
||||
|
||||
BOOST_TEST( x == 3 );
|
||||
}
|
||||
|
||||
#define BOOST_DISABLE_ASSERTS
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
void test_disabled()
|
||||
{
|
||||
int x = 1;
|
||||
|
||||
BOOST_VERIFY( 1 );
|
||||
BOOST_VERIFY( x == 1 );
|
||||
BOOST_VERIFY( ++x );
|
||||
BOOST_VERIFY( f(x) );
|
||||
BOOST_VERIFY( &x );
|
||||
|
||||
BOOST_TEST( x == 3 );
|
||||
|
||||
BOOST_VERIFY( 0 );
|
||||
BOOST_VERIFY( !x );
|
||||
BOOST_VERIFY( x == 0 );
|
||||
BOOST_VERIFY( !++x );
|
||||
BOOST_VERIFY( !f(x) );
|
||||
|
||||
BOOST_TEST( x == 5 );
|
||||
|
||||
void * p = 0;
|
||||
BOOST_VERIFY( p );
|
||||
}
|
||||
|
||||
#undef BOOST_DISABLE_ASSERTS
|
||||
|
||||
#define BOOST_ENABLE_ASSERT_HANDLER
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <cstdio>
|
||||
|
||||
int handler_invoked = 0;
|
||||
|
||||
void boost::assertion_failed(char const * expr, char const * function, char const * file, long line)
|
||||
{
|
||||
#if !defined(BOOST_NO_STDC_NAMESPACE)
|
||||
using std::printf;
|
||||
#endif
|
||||
|
||||
printf("Expression: %s\nFunction: %s\nFile: %s\nLine: %ld\n\n", expr, function, file, line);
|
||||
++handler_invoked;
|
||||
}
|
||||
|
||||
struct X
|
||||
{
|
||||
static bool f()
|
||||
{
|
||||
BOOST_VERIFY( 0 );
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
void test_handler()
|
||||
{
|
||||
int x = 1;
|
||||
|
||||
BOOST_VERIFY( 1 );
|
||||
BOOST_VERIFY( x == 1 );
|
||||
BOOST_VERIFY( ++x );
|
||||
BOOST_VERIFY( f(x) );
|
||||
BOOST_VERIFY( &x );
|
||||
|
||||
BOOST_TEST( x == 3 );
|
||||
|
||||
BOOST_VERIFY( 0 );
|
||||
BOOST_VERIFY( !x );
|
||||
BOOST_VERIFY( x == 0 );
|
||||
BOOST_VERIFY( !++x );
|
||||
BOOST_VERIFY( !f(x) );
|
||||
|
||||
BOOST_TEST( x == 5 );
|
||||
|
||||
void * p = 0;
|
||||
BOOST_VERIFY( p );
|
||||
|
||||
BOOST_VERIFY( X::f() );
|
||||
|
||||
BOOST_TEST( handler_invoked == 8 );
|
||||
}
|
||||
|
||||
#undef BOOST_ENABLE_ASSERT_HANDLER
|
||||
|
||||
int main()
|
||||
{
|
||||
test_default();
|
||||
test_disabled();
|
||||
test_handler();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user