c++boostboost.test

Boost.Test check if structs are equals


I'm new on Boost.Test on Visual Studio 2017.

I'm trying to test a method with the following Test:

#define BOOST_TEST_MODULE astroTimeTests
#include <boost/test/included/unit_test.hpp>
#include <ctime>
#include "../../AstroTime/Convert.h"

struct TestFixture
{
    Convert convert_instance;

    TestFixture()
        : convert_instance()
    {}

    ~TestFixture() = default;
};

BOOST_FIXTURE_TEST_SUITE(TestConvert, TestFixture)

BOOST_AUTO_TEST_CASE(julianToGreenWichCase)
{
    // http://www.onlineconversion.com/julian_date.htm
    tm day1 = { 12, 28, 16, 2, 10, 119, 0, 0, 0};
    BOOST_REQUIRE_EQUAL(day1, convert_instance.JulianToGreenWich(2458790.18625f));
}

BOOST_AUTO_TEST_SUITE_END()

JulianToGreenWich returns a tm struct from ctime.

But I'm doing something wrong, because I get the following errors:

Error C2678 binary '==': no operator found which takes a left-hand operand of type 'const Left' (or there is no acceptable conversion)

Error C2338 Type has to implement operator<< to be printable

Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'const T' (or there is no acceptable conversion)

How can I test if both values, day1 and the result of JulianToGreenWich are equal?


Solution

  • This is how I have fixed this error:

    #define BOOST_TEST_MODULE astroTimeTests
    #include <boost/test/included/unit_test.hpp>
    #include <ctime>
    #include <iostream>
    
    #include "../../AstroTime/Convert.h"
    
    struct TestFixture
    {
        Convert convert_instance;
    
        TestFixture()
            : convert_instance()
        {}
    
        ~TestFixture() = default;
    };
    
    bool operator ==(tm const &left, tm const &right)
    {
        return(
               left.tm_sec == right.tm_sec
            && left.tm_min == right.tm_min
            && left.tm_hour == right.tm_hour
            && left.tm_mday == right.tm_mday
            && left.tm_mon == right.tm_mon
            && left.tm_year == right.tm_year);
    }
    
    std::ostream& operator<<(std::ostream& os, const tm& dt)
    {
        os << dt.tm_hour << " " << dt.tm_min << " " << dt.tm_sec << ", "
           << dt.tm_mday << " " << dt.tm_mon << " " << (1900 + dt.tm_year) << std::endl;
    
        return os;
    }
    
    BOOST_FIXTURE_TEST_SUITE(TestConvert, TestFixture)
    
    BOOST_AUTO_TEST_CASE(julianToGreenWichCase)
    {
        // http://www.onlineconversion.com/julian_date.htm
        tm day1 = { 12, 28, 16, 2, 10, 119, 0, 0, 0};
        BOOST_REQUIRE_EQUAL(day1, convert_instance.JulianToGreenWich(2458790.18625f));
    }
    
    BOOST_AUTO_TEST_SUITE_END()