2

For some reason boost::test can't manage to compile the following code

#define BOOST_TEST_MODULE EPUTests

#include <iostream>
#include <boost/test/unit_test.hpp>

using epu8 = uint8_t __attribute__((vector_size(16)));

std::ostream &operator<<(std::ostream &stream, epu8 const &term) {
    stream << "[" << unsigned(term[0]);
    for (unsigned i = 1; i < 16; ++i)
        stream << "," << unsigned(term[i]);
    stream << "]";
    return stream;
}

bool failtest(epu8 x) { return false; }


//****************************************************************************//
BOOST_AUTO_TEST_SUITE(EPU8_test)

BOOST_AUTO_TEST_CASE(EPU8_equal) {
    epu8 x {};
    BOOST_CHECK_PREDICATE(failtest, (x));
}

BOOST_AUTO_TEST_SUITE_END()
//****************************************************************************//

The error message is

In file included from /usr/include/boost/test/tools/floating_point_comparison.hpp:21:0,
             from /usr/include/boost/test/tools/old/impl.hpp:21,
             from /usr/include/boost/test/test_tools.hpp:46,
             from /usr/include/boost/test/unit_test.hpp:18,
             from boost_test_epu.cpp:4:
/usr/include/boost/test/tools/detail/print_helper.hpp: In instantiation of 'struct boost::test_tools::tt_detail::print_log_value<__vector(16) unsigned char>':
/usr/include/boost/test/tools/detail/print_helper.hpp:178:5:   required from 'std::ostream& boost::test_tools::tt_detail::operator<<(std::ostream&, const boost::test_tools::tt_detail::print_helper_t<T>&) [with T = __vector(16) unsigned char; std::ostream = std::basic_ostream<char>]'
/usr/include/boost/test/utils/lazy_ostream.hpp:66:29:   required from 'std::ostream& boost::unit_test::lazy_ostream_impl<PrevType, T, StorageT>::operator()(std::ostream&) const [with PrevType = boost::unit_test::lazy_ostream; T = boost::test_tools::tt_detail::print_helper_t<__vector(16) unsigned char>; StorageT = const boost::test_tools::tt_detail::print_helper_t<__vector(16) unsigned char>&; std::ostream = std::basic_ostream<char>]'
boost_test_epu.cpp:29:1:   required from here
/usr/include/boost/test/tools/detail/print_helper.hpp:47:5: error: static assertion failed: Type has to implement operator<< to be printable
 BOOST_STATIC_ASSERT_MSG( (boost::has_left_shift<std::ostream,T>::value),

I understand that g++ is complaining that my epu8 type is not printable. However I know it is because changing

    BOOST_CHECK_PREDICATE(failtest, (x));

by

    std::cout << x << std::endl;
    BOOST_CHECK(failtest(x));

works as expected and print by vector variable correctly.

The same error is reported with various version of clang++ and g++

What am I doing wrong ?

hivert
  • 10,579
  • 3
  • 31
  • 56
  • could this be a namespace issue; maybe the PREDICATE version calls the predicate in a way that can't see your print operator? I know when I was trying to get boost to custom print things putting it in the correct namespace solved an issue. – esoterik Jun 20 '18 at 22:06
  • @esoterik: this is exactly the issue: operator<< must be defined in std ! Please post an answer and I'll happily accept it. – hivert Jun 22 '18 at 05:36

1 Answers1

6

BOOST::test requires the console output operator<< to be in the std namespace

namespace std
{
    ostream &operator<<(...)
    ...
}
esoterik
  • 245
  • 1
  • 7