[boost.test] floating point comparison with user defined type

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[boost.test] floating point comparison with user defined type

Boost - Users mailing list
Hello,

I have created a class to represent the euro currency and I am trying to use boost.test to check if two euro amounts are sufficiently close. I was expecting the following to pass the test given the tolerance level I have set, but instead it fails.
What is the proper way to compare two user-defined decimals? Thank you for your time.

#define BOOST_TEST_MODULE My Test
#include <boost/test/included/unit_test.hpp>


class eur {

    long double m_amount;

public:

    eur(long double amount)
    : m_amount(amount){}

    bool operator ==(const eur& rhs) const {
        return m_amount == rhs.m_amount;
    }

    bool operator !=(const eur& rhs) const {
        return m_amount != rhs.m_amount;
    }

    friend std::ostream& operator <<(std::ostream& os, const eur& rhs);
};

std::ostream& operator <<(std::ostream& os, const eur& rhs) {
    os << "EUR " << rhs.m_amount;
    return os;
}

BOOST_TEST_DECORATOR(* boost::unit_test::tolerance(eur(0.1)))

BOOST_AUTO_TEST_CASE(first_test)
{
    eur x(1.1);
    eur y(1.2);

    BOOST_TEST(x == y);
}


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [boost.test] floating point comparison with user defined type

Boost - Users mailing list
Le 20/03/2018 à 23:46, Georgios Sermaidis via Boost-users a écrit :
> Hello,
>
> I have created a class to represent the euro currency and I am trying to
> use boost.test to check if two euro amounts are sufficiently close. I
> was expecting the following to pass the test given the tolerance level I
> have set, but instead it fails.
> What is the proper way to compare two user-defined decimals? Thank you
> for your time.
>
[snip]

You have to advertise boost.test that your type can be compared using
tolerance. This is explained here:

http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point/customizing_for_tolerance.html

Also your type should implement the tolerancecompatible concept:
http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point/customizing_for_tolerance.html#boost_test.testing_tools.extended_comparison.floating_point.customizing_for_tolerance.concept_tolerance_based

Best,
Raffi


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users