How can I use Boost Test with Boost Units

172 views Asked by At

I expected this to compile properly:

#include <boost/test/unit_test.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/systems/angle/degrees.hpp>

using boost::units::quantity;
using boost::units::degree::degrees;
using boost::units::degree::plane_angle;

int main() {
    quantity<plane_angle> q1 = 15 * degrees;

    BOOST_CHECK_CLOSE(q1, 15 * degrees, 1e-8);
    return 0;
}

Unfortunately this produces a couple hundreds lines of errors with GCC.

Of course I could do this instead.

BOOST_CHECK_CLOSE(q1.value(), 15, 1e-8);

But I would like to keep the units explicit in the test case, just in case that someone else decides to change the unit type of q1.

Is there any way to keep the unit explicit?

1

There are 1 answers

0
Jeffrey Faust On

One option is to divide the equal values and compare to 1:

BOOST_CHECK_CLOSE(q1 / (15 * degrees), 1, 1e-8);

When dividing boost::units::quantity by the same type, you end up with a dimensionless quantity, which is allows to be implicitly cast to double.