[Boost.Test] improving BOOST_CHECK_CLOSE for arguments == 0
I've been having some problems using BOOST_CHECK_CLOSE() (1.33.1) in cases where one of the two arguments is exactly equal to zero: the tolerance check fails in this case (d'oh). I know that I can use BOOST_CHECK_SMALL() in those cases, but it's a real drag to have to write the boilerplate code to handle specific cases where one operand is zero. So this is what I've done: #define MY_CHECK_CLOSE(a,b,c) \ if (a == 0) { BOOST_CHECK_SMALL(b,c); } \ else if (b == 0) { BOOST_CHECK_SMALL(a,c); } \ else { BOOST_CHECK_CLOSE(a,b,c); } I wonder: did I miss something, is there a better way? If not, perhaps this logic could be integrated in the _CLOSE tools. pj
"Pierre-Jules Tremblay"
I've been having some problems using BOOST_CHECK_CLOSE() (1.33.1) in cases where one of the two arguments is exactly equal to zero: the tolerance check fails in this case (d'oh). I know that I can use BOOST_CHECK_SMALL() in those cases, but it's a real drag to have to write the boilerplate code to handle specific cases where one operand is zero. So this is what I've done:
#define MY_CHECK_CLOSE(a,b,c) \ if (a == 0) { BOOST_CHECK_SMALL(b,c); } \ else if (b == 0) { BOOST_CHECK_SMALL(a,c); } \ else { BOOST_CHECK_CLOSE(a,b,c); }
I wonder: did I miss something, is there a better way? If not, perhaps this logic could be integrated in the _CLOSE tools.
BOOST_CHECK_CLOSE and BOOST_CHECK_SMALL are two very different tools. Former is based on comparisons of relative values using percentage tolerance. Later is based on comparison of absolute value against the actual tolerance. Combining them into one is simply wrong. You couldn't use the same tolerance value for both of them. Also there is a reason why BOOST_CHECK_CLOSE behaves the way it behaves with zero value: in general no value is close to zero. Now if you need to follow above logic for your purposes you are free to do this. But this could not be a part of Boost.Test. Gennadiy
I understand.
So I'm not sure I can build the tool I need with the existing tools.
What I need to do is essentially compare two vector<float> objects of
same size, element-by-element. I would be ok with using an absolute
difference test in all cases (so that zero and non-zero comparison
tests remain valid), however if I'm not mistaken my only choice is
BOOST_CHECK_SMALL(a-b,tol) where a and b are corresponding elements of
the two vectors. The downside of this approach is that if the test
fails, only one value gets printed out, i.e. the result of the
subtraction.
Can you suggest a way to combine existing tools to get what I'm looking for?
Cheers,
pj
On 3/13/07, Gennadiy Rozental
I wonder: did I miss something, is there a better way? If not, perhaps this logic could be integrated in the _CLOSE tools.
BOOST_CHECK_CLOSE and BOOST_CHECK_SMALL are two very different tools. Former is based on comparisons of relative values using percentage tolerance. Later is based on comparison of absolute value against the actual tolerance. Combining them into one is simply wrong. You couldn't use the same tolerance value for both of them. Also there is a reason why BOOST_CHECK_CLOSE behaves the way it behaves with zero value: in general no value is close to zero. Now if you need to follow above logic for your purposes you are free to do this. But this could not be a part of Boost.Test.
Gennadiy
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
"Pierre-Jules Tremblay"
I understand.
So I'm not sure I can build the tool I need with the existing tools. What I need to do is essentially compare two vector<float> objects of same size, element-by-element. I would be ok with using an absolute difference test in all cases (so that zero and non-zero comparison tests remain valid), however if I'm not mistaken my only choice is BOOST_CHECK_SMALL(a-b,tol) where a and b are corresponding elements of the two vectors. The downside of this approach is that if the test fails, only one value gets printed out, i.e. the result of the subtraction.
Can you suggest a way to combine existing tools to get what I'm looking for?
You could write your own comparison predicate that is build on boost::test_tools::check_is_small: boost::test_tools::predicate_result my_compare_coll( vector<float> const& v1, vector<float> const& v2, float tol ) { boost::test_tools::predicate_result res; // compare sizes here // compare elem by elem // if ith elems mismatched produce an error // see equal_coll_impl( Left left_begin, Left left_end, Right right_begin, Right right_end ) for example // atomic check is based on boost::test_tools::check_is_small // boost::test_tools::check_is_small( *v1_it - *v2_it, tol ); return res; } Gennadiy
participants (2)
-
Gennadiy Rozental
-
Pierre-Jules Tremblay