Subject: Re: [boost] boost-test, why is there no DOUBLES_EQUAL?
From: Hervé Brönnimann (hervebronnimann_at_[hidden])
Date: 2011-04-05 21:29:36
I've used and liked something like:
BOOST_CHECK2(condition, p1, p2)
BOOST_CHECK3(condition, p1, p2, p3)
with the meaning that if condition fails, then the first arguments (p1, p2, p3... depending on the number of arguments) are printed along with the condition that fails. In your case, you would write:
BOOST_CHECK2(std::fabs(first - second) <= 1.0e-3, first, second)
Now I'm not that familiar with BOOST_CHECK so I looked at the online documentation, and found the equivalent form:
BOOST_CHECK_MESSAGE(std::fabs(first - second) <= 1.0e-3, "first:" << first << " second:" << second);
Granted, the advantage of BOOST_CHECK_DOUBLE_EQUALS is that you do not have to repeat first and second. The use case is frequent enough that you might want the dedicated macro.
The advantage of BOOST_CHECK_MESSAGE is that the message can be anything you want indicating some context, and it is the most general form you will want.
The advantage of BOOST_CHECK as I put it above is that p1, p2, p3... the message can be automatically constructed from the names and values of the variables (it is a macro after all, and it can use #p1 for creating the string "p1" so you don't have to do it). I've found it quite useful.
-- Hervé Brönnimann On Apr 5, 2011, at 5:25 PM, Sebastian Nowozin wrote: > Hello Grennadiy, > > On Tue, Mar 29, 2011 at 6:19 PM, Gennadiy Rozental <rogeeff_at_[hidden]> wrote: > >>> BOOST_CHECK_DOUBLES_EQUAL(first, second, absolute_tolerance); >> Use BOOST_CHECK_SMALL. > > This does not do the job, as > BOOST_CHECK_SMALL(std::fabs(first - second), 1.0e-3) > will be about as useful as > BOOST_CHECK(std::fabs(first - second) <= 1.0e-3) > > It is not useful because in case this check fails I do not get to see > the original values of both 'first' and 'second', but only their > difference. > > In my application, first and second are in the range of 0 to 1, but > can in fact be quite close to zero. The qualitative test is correct > if the absolute difference between first and second is small enough, > irrespectively of the absolute value of first. Therefore, I fail to > see why the boost test library provides only BOOST_CHECK_CLOSE and > BOOST_CHECK_SMALL, but no analog to CPPUNIT_ASSERT_DOUBLES_EQUAL. > > Thanks, > Sebastian > _______________________________________________ > Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk