|
Boost Users : |
Subject: [Boost-users] Unit Tests and function calling
From: Charles Yates (charles.yates_at_[hidden])
Date: 2014-02-02 07:17:33
Hi,
Just wanted a second opinion on how best to handle calling functions which
invoke BOOST_REQUIRE and BOOST_CHECK type logic.
For example, if I have a test like:
void stuff( ... )
{
BOOST_REQUIRE( ... );
BOOST_CHECK( ... );
}
BOOST_AUTO_TEST( check_stuff )
{
stuff( case1 );
stuff( case2 );
stuff( case3 );
}
in the CLF log case at least, I only see which lines in stuff failed, but
not which which case.
I can change things so that we have:
bool stuff( ... )
{
bool result = true;
if ( ( result = require-predicate ) == true )
result = check-predicate;
return result;
}
BOOST_AUTO_TEST( check_stuff )
{
BOOST_CHECK( stuff( case1 ) );
BOOST_CHECK( stuff( case2 ) );
BOOST_CHECK( stuff( case3 ) );
}
but then I lose the info in the logs over which predicates are failing and
it's generally a bit nasty :).
As a result, I've been playing with this which seems to encapsulate things
pretty well for me (though may introduce more parenthesis than is strictly
necessary and may introduce shadowed variables and other issues?):
#define BOOL_CHECK( result, pred ) { bool pred_result; BOOST_CHECK(
pred_result = ( pred ) ); result = pred_result && result; }
#define BOOL_REQUIRE( pred ) { bool pred_result; BOOST_CHECK( pred_result =
( pred ) ); if ( !pred_result ) return pred_result; }
#define BOOL_REQUIRE_EQUAL( name, value ) { bool pred_result; BOOST_CHECK(
pred_result = ( ( name ) == ( value ) ) ); if ( !pred_result ) return
pred_result; }
void stuff( ... )
{
bool result = true;
BOOL_REQUIRE( ... );
BOOL_CHECK( result, ... );
return result;
}
BOOST_AUTO_TEST( check_stuff )
{
BOOST_CHECK( stuff( case1 ) );
BOOST_CHECK( stuff( case2 ) );
BOOST_CHECK( stuff( case3 ) );
}
when one of the predicates fails, I get 2 errors in the log - the first
being the specific line in 'stuff' and the second being failing case.
I've only tested it with g++ and don't know if it's a sensible approach -
wondered if any of you guys had any suggestions?
Cheers,
Charlie
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net