Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BOOST_CHECK fails to compile operator<< for custom types

I wrote this really trivial class so that it's clear what my problem is:

class A
{
public:
    int x;
    A(int y) {x=y;}
    bool operator==(const A &other) const {return x==other.x;}
};

Now, if I define A first(1) and A second(1), it would seem natural to me that BOOST_CHECK_EQUAL(first, second) should pass. However, I got 50 errors when trying to do this, the first one sounds like: no math for operator << in ostr << t which is somewhere in the boost code... Other tests work just fine, comparing known types or even pointers, but there is something different that appears to happen with class objects.

like image 690
Ioana Avatar asked Jul 10 '13 13:07

Ioana


2 Answers

There are three ways I have identified to solve the problem with operator<<.

The first way is to provide an operator<< for your type. This is needed because when boost_check_equal fails, it also logs the failure by calling operator<< with the objects. See the verbose addendum after the break to see how this is actually accomplished. It's harder than it might seem.

The second way is to not do the logging I just mentioned. You can do this by #definineing BOOST_TEST_DONT_PRINT_LOG_VALUE. To disable logging for just one test, you might surround the test in question with this #define, then immediately #undef it:

#define BOOST_TEST_DONT_PRINT_LOG_VALUE
BOOST_CHECK_EQUAL (first, second);
#undef BOOST_TEST_DONT_PRINT_LOG_VALUE

The third way is to sidestep the need for an operator<< that works with your type by not comparing one item to another, but just checking a bool:

BOOST_CHECK (first == second);

Select your preferred method.


My preference is the first, but implementing that is suprisingly challenging. If you simply define an operator<< in global scope, it won't work. I think the reason for this is because of a problem with name resolution. One popular suggestion to fix this is to put the operator<< in the std namespace. This works, at least in practice on some compilers, but I don't like it because the Standard prohibits adding anything to the std namespace.

A better method I found is to implement a custom print_log_value class template specialization for your type. print_log_value is a class template useb by the Boost.Test internals to actually call the correct operator<< for the specified type. It delegates to an operator<< to do the heavy lifting. Specializing print_log_value for your custom types is officially supported by Boost [citation needed], and is accomplished thusly.

Assuming your type is called Timestamp (it is in my code), first define a global free operator<< for Timestamp:

static inline std::ostream& operator<< (std::ostream& os, const Mdi::Timestamp& ts)
{
    os << "Timestamp";
    return os;
}   

...and then provide the print_log_value specialization for it, delegating to the operator<< you just defined:

namespace boost { namespace test_tools {
template<>           
struct print_log_value<Mdi::Timestamp > {
void operator()( std::ostream& os,
    Mdi::Timestamp const& ts)
{
    ::operator<<(os,ts);
}
};                                                          
}}
like image 74
John Dibling Avatar answered Nov 15 '22 20:11

John Dibling


There is a clean way starting Boost 1.64 to log user defined types via the customization points. The full documentation of this feature can be found here.

An example from the documentation is given below. The idea is to define the function boost_test_print_type for the type you want to print, and to bring this function into the test case (found via ADL):

#define BOOST_TEST_MODULE logger-customization-point
#include <boost/test/included/unit_test.hpp>

namespace user_defined_namespace {
  struct user_defined_type {
      int value;

      user_defined_type(int value_) : value(value_)
      {}

      bool operator==(int right) const {
          return right == value;
      }
  };
}

namespace user_defined_namespace {
  std::ostream& boost_test_print_type(std::ostream& ostr, user_defined_type const& right) {
      ostr << "** value of user_defined_type is " << right.value << " **";
      return ostr;
  }
}

BOOST_AUTO_TEST_CASE(test1)
{
    user_defined_namespace::user_defined_type t(10);
    BOOST_TEST(t == 11);

    using namespace user_defined_namespace;
    user_defined_type t2(11);
    BOOST_TEST(t2 == 11);
}
like image 44
Raffi Avatar answered Nov 15 '22 20:11

Raffi