如何使用Boost.Test比较载体? [英] How to compare vectors with Boost.Test?
问题描述
我使用Boost测试单元测试的一些C ++ code。
I am using Boost Test to unit test some C++ code.
我有我需要与预期成果进行比较值的向量,但我不想手动检查在一个循环中值:
I have a vector of values that I need to compare with expected results, but I don't want to manually check the values in a loop:
BOOST_REQUIRE_EQUAL(values.size(), expected.size());
for( int i = 0; i < size; ++i )
{
BOOST_CHECK_EQUAL(values[i], expected[i]);
}
的主要问题是,循环检查不打印的索引,因此它需要一些搜索找到失配。
The main problem is that the loop check doesn't print the index, so it requires some searching to find the mismatch.
我可以使用的std ::等于
或的std ::不匹配
上的两个向量,但是这将需要很多样板的为好。
I could use std::equal
or std::mismatch
on the two vectors, but that will require a lot of boilerplate as well.
有一个更清洁的方式做到这一点?
Is there a cleaner way to do this?
推荐答案
使用<一个href=\"http://www.boost.org/doc/libs/release/libs/test/doc/html/utf/testing-tools/reference.html\"><$c$c>BOOST_CHECK_EQUAL_COLLECTIONS$c$c>.它在 test_tools.hpp
了一个宏以迭代器两对:
Use BOOST_CHECK_EQUAL_COLLECTIONS
. It's a macro in test_tools.hpp
that takes two pairs of iterators:
BOOST_CHECK_EQUAL_COLLECTIONS(values.begin(), values.end(),
expected.begin(), expected.end());
这将报告索引和不匹配的值。如果大小不匹配,将报告以及(并不会只运行了向量的结尾)。
It will report the indexes and the values that mismatch. If the sizes don't match, it will report that as well (and won't just run off the end of the vector).
请注意,如果你想使用 BOOST_CHECK_EQUAL
或 BOOST_CHECK_EQUAL_COLLECTIONS
与非POD类型,你将需要实现
Note that if you want to use BOOST_CHECK_EQUAL
or BOOST_CHECK_EQUAL_COLLECTIONS
with non-POD types, you will need to implement
bool YourType::operator!=(const YourType &rhs) // or OtherType
std::ostream &operator<<(std::ostream &os, const YourType &yt)
分别比较和记录。结果
传递给 BOOST_CHECK_EQUAL_COLLECTIONS
的迭代器的顺序确定这是的RHS和LHS =
比较 - !第一次迭代范围将在比较在LHS
for the comparison and logging, respectively.
The order of the iterators passed to BOOST_CHECK_EQUAL_COLLECTIONS
determines which is the RHS and LHS of the !=
comparison - the first iterator range will be the LHS in the comparisons.
这篇关于如何使用Boost.Test比较载体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!