You can use BOOST_REQUIRE_EQUAL_COLLECTIONS
with std::vector<T>
, but you have to teach Boost.Test how to print a std::vector
when you have a vector of vectors or a map whose values are vectors. When you have a map, Boost.Test needs to be taught how to print std::pair
. Since you can't change the definition of std::vector
or std::pair
, you have to do this in such a way that the stream insertion operator you define will be used by Boost.Test without being part of the class definition of std::vector
. Also, this technique is useful if you don't want to add stream insertion operators to your system under test just to make Boost.Test happy.
Here is the recipe for any std::vector
:
namespace boost
{
// teach Boost.Test how to print std::vector
template <typename T>
inline wrap_stringstream&
operator<<(wrap_stringstream& wrapped, std::vector<T> const& item)
{
wrapped << '[';
bool first = true;
for (auto const& element : item) {
wrapped << (!first ? "," : "") << element;
first = false;
}
return wrapped << ']';
}
}
This formats the vectors as [e1,e2,e3,...,eN]
for a vector with N
elements and will work for any number of nested vectors, e.g. where the elements of the vector are also vectors.
Here is the similar recipe for std::pair
:
namespace boost
{
// teach Boost.Test how to print std::pair
template <typename K, typename V>
inline wrap_stringstream&
operator<<(wrap_stringstream& wrapped, std::pair<const K, V> const& item)
{
return wrapped << '<' << item.first << ',' << item.second << '>';
}
}
BOOST_REQUIRE_EQUAL_COLLECTIONS
will tell you the index of the mismatched items, as well as the contents of the two collections, assuming the two collections are of the same size. If they are of different sizes, then that is deemed a mismatch and the differing sizes are printed.