你可以使用BOOST_REQUIRE_EQUAL_COLLECTIONS
with ,但你必须教 Boost.Test当你有一个向量的向量或一个值为向量的映射时std::vector<T>
如何打印 a 。std::vector
当您有地图时,需要教 Boost.Test 如何打印std::pair
. 由于您无法更改std::vector
or的定义std::pair
,因此您必须以这样一种方式执行此操作,即您定义的流插入运算符将由 Boost.Test 使用,而不是std::vector
. 此外,如果您不想为了让 Boost.Test 满意而将流插入操作符添加到您的系统中,此技术也很有用。
这是任何的食谱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 << ']';
}
}
这会将向量格式化为[e1,e2,e3,...,eN]
带有N
元素的向量,并且适用于任意数量的嵌套向量,例如,向量的元素也是向量。
这是类似的配方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
会告诉你不匹配项的索引,以及两个集合的内容,假设两个集合大小相同。如果它们的尺寸不同,则视为不匹配,并打印不同的尺寸。