Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#12876 closed Bugs (invalid)

BOOST_CHECK_EQUAL fail for vector<int> with operator<< for vector<T>

Reported by: 401819174@… Owned by: Gennadiy Rozental
Milestone: To Be Determined Component: test
Version: Boost 1.61.0 Severity: Problem
Keywords: Cc:

Description

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

using namespace std;

class PrintClass {

}; bool operator== (const PrintClass &l, const PrintClass &r) {

std::cout << "value compare" << std::endl; return false;

}

template <typename T> std::ostream & operator<< (std::ostream & os, std::vector<T> const &v) {

os << "has " << &v; return os;

}

template <typename T> bool operator== (const std::vector<T> &l, const std::vector<T> &r) {

std::cout << "const vector compare" << std::endl; return false;

}

BOOST_AUTO_TEST_CASE(test_print_vector_int) {

const std::vector<int> a{1}; const std::vector<int> b{2};

std::cout << a << std::endl; work BOOST_CHECK_EQUAL(a, b); don't work. error: static assertion failed: Type has to implement operator<< to be printable

std::cout << boost::has_left_shift<std::ostream, vector<int>>::value << endl;

use user define class is ok std::vector<PrintClass> c{PrintClass()}; std::vector<PrintClass> d{PrintClass()}; std::cout << c << std::endl; work BOOST_CHECK_EQUAL(c, d); work

}

Change History (4)

comment:1 by 401819174@…, 6 years ago

BOOST_AUTO_TEST_CASE(test_print_vector_int) {

const std::vector<int> a{1};

const std::vector<int> b{2};

std::cout << a << std::endl; /* work */

BOOST_CHECK_EQUAL(a, b); /* don't work. */

/* error: static assertion failed: Type has to implement operator<< to be printable */

/* std::cout << boost::has_left_shift<std::ostream, vector<int>>::value << endl;*/

/* use user define class is ok */

std::vector<PrintClass> c{PrintClass()}; std::vector<PrintClass> d{PrintClass()}; std::cout << c << std::endl; /* work */ BOOST_CHECK_EQUAL(c, d); /* work */

}

comment:2 by Raffi Enficiaud, 6 years ago

Resolution: invalid
Status: newclosed

You have BOOST_CHECK_EQUAL_COLLECTIONS or BOOST_TEST that works on collections as well.

If the underlying type is not printable, then you should consider using BOOST_TEST_DONT_PRINT_LOG_VALUE

Best, Raffi

comment:3 by 401819174@…, 6 years ago

thank you

the C++ function overload mechanism require all overload function in the same domain. vector/int, make search the function from std or boost::*(boost::has_left_shift). in the std has the define of operator<<, so in the global define of operator<< is not visiable。

comment:4 by Raffi Enficiaud, 6 years ago

I do not understand everything you are saying (please use the Wiki Markup), but here is what I think:

const std::vector<int> a{1};
const std::vector<int> b{2};
BOOST_CHECK_EQUAL(a, b); /* does not work. */

This does not work because it requires std::vector<int> to be printable, which is usually never the case by default. However

BOOST_CHECK_EQUAL_COLLECTIONS(a.begin(), a.end(), b.begin(), b.end());

works as expected, because the underlying int is already printable.

Concerning

std::vector<PrintClass> c{PrintClass()};
std::vector<PrintClass> d{PrintClass()};
BOOST_CHECK_EQUAL(c, d);

In your example, this works because you have this template that is capture the prints to std::cout:

template <typename T> std::ostream & operator<< (
    std::ostream & os, 
    std::vector<T> const &v)

Your bug is invalid.

Note: See TracTickets for help on using tickets.