Boost logo

Boost Users :

Subject: Re: [Boost-users] [Units] Units in unit tests
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2017-05-08 02:17:31


On 8/05/2017 02:08, Stephan Menzel wrote:
> I have lots of lines like:
>
> vec3 v = get_some_value();
>
> and then I test the individual component values like this:
> BOOST_CHECK_CLOSE(X(v), 42.0, 0.001); // check for length of 42 meters
>
> Now this would have to be rewritten to:
>
> BOOST_CHECK_CLOSE(X(v).value(), 42.0, 0.001);
>
> But doing this, I imply the length is in meters, right? Doesn't that
> defeat the purpose a little? I feel this would be better:
>
> BOOST_CHECK_CLOSE(X(v), 42.0 * meters, 0.001);
>
> but the Macro won't accept this as this is not a floating point type
> anymore.
> Does anybody have some hint as to how this is best addressed while still
> have maximum readability?

I don't know if there is a better way to do this within the confines of
Boost.Test itself (perhaps making a custom macro, or extending what the
macro already does in some way), but there *is* a better way to get
basic values out of unit quantities rather than using .value().

Instead of:

BOOST_CHECK_CLOSE(X(v).value(), 42.0, 0.001);

Try:

BOOST_CHECK_CLOSE(X(v) / meters, 42.0, 0.001);

(You might also need an explicit static_cast<double>. If so, you might
want to wrap this whole thing in a helper method to reduce typing.)

This explicitly specifies what unit and scale you want the comparison to
be performed in -- and you can for example request a comparison in
millimeters or inches if you use the appropriate definitions. And it
won't compile if you use the wrong dimension -- eg. if you try to
extract seconds from a length measurement.


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