Question

Is there a version of BOOST_CHECK_CLOSE that works with absolute differences as opposed to percentage? In some cases absolute differences make more sense, e.g. when we are working with money (yeah, I know that double is not the best type for that), we might check that the answer is fine to the closest penny. There are many other examples.

It is rather trivial to write one of these myself using e.g. BOOST_CHECK_SMALL, but I the verbose output of right macro would make the error much easier to track down.

Was it helpful?

Solution

TBH, I don't understand the issue with BOOST_CHECK_SMALL:

double val = func(); //should be between 95 and 105
BOOST_CHECK_SMALL(val - 100, 5);

However, if it has to look more like CHECK_CLOSE, you'll have to roll your own macro the way boost does - just dig into the corresponding headers. You'll probably have to add your own check type enum value, create a predicate and use it in your macro.

Or use BOOST_CHECK_PREDICATE which honestly looks a lot uglier than CHECK_SMALL with a difference. Or simply use BOOST_CHECK with a custom function returning a predicate - but I think that's overkill.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top