CATCH 单元测试C++比较 std::array
CATCH unit testing for C++ compare std::array
我喜欢在c ++单元测试中使用catch。我的目标是比较std::array
和std::vector
。 我创建了这个失败的示例。
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
TEST_CASE("Vector") {
std::vector<double> direction = {0.1, 0.3, 0.4};
std::vector<double> false_direction = {0.1, 0.0, 0.4};
REQUIRE(direction == false_direction);
}
TEST_CASE("Array") {
std::array<double, 3> direction = {0.1, 0.3, 0.4};
std::array<double, 3> false_direction = {0.1, 0.0, 0.4};
REQUIRE(direction == false_direction);
}
此测试的输出用于检查std::vector
要求( 方向 == false_direction )扩展: { 0.1, 0.3, 0.4 } == { 0.1, 0.0, 0.4 }
和std::array
要求( 方向 == false_direction )扩展: {?} == {?}
如何显示实际值和预期值?我喜欢在违反REQUIRE
条件下为std::array
和std::vector
显示完全相同。
我使用最新版本的 catch (v1.10.0)。
从根本上说,这是一个如何字符串化类型的问题,为此总是有文档。
删节版是有一个简单的算法
-
检查给定类型的
Catch::StringMaker
的专用化。如果存在,请使用它。 -
检查给定类型的
operator<<
重载。如果存在,请使用它。 -
使用"{?}"。
直到最近,Catch还为开箱即用的std::vector
提供了专门的功能,但没有为std::array
提供,因为std::array
是C++11的一部分,通常较少使用。从 2.1.0 版开始,Catch 会检查该类型是否提供类似容器的接口,特别是响应begin(T)
和end(T)
。这为许多不同类型的数组提供了自动字符串化,包括std::vector
数组、std::array
数组,以及静态数组。
我没有检查 Catch 的源代码,看看他们是如何实现 REQUIRE
子句的,以及为什么它不起作用,但vector
确实如此。但这里有一个解决方法:
#define COMPARE_ARRAYS(lhs, rhs) compareArrays(Catch::getResultCapture().getCurrentTestName(), __LINE__, lhs, rhs)
template < typename T, size_t N >
void compareArrays(const std::string & test, unsigned line, std::array<T, N> lhs, std::array<T, N> rhs) {
std::vector<T> lv(lhs.begin(), lhs.end());
std::vector<T> rv(rhs.begin(), rhs.end());
INFO("Test case [" << test << "] failed at line " << line); // Reported only if REQUIRE fails
REQUIRE(lv == rv);
}
TEST_CASE("Array") {
std::array<double, 3> direction = {0.1, 0.3, 0.4};
std::array<double, 3> true_direction = {0.1, 0.3, 0.4};
COMPARE_ARRAYS(direction, true_direction);
std::array<double, 3> false_direction = {0.1, 0.0, 0.4};
COMPARE_ARRAYS(direction, false_direction);
}
我沿着 catch 标头中的 toString
方法追踪了问题。它缺少std::array
的重载,std::vector
已经被实例化了。 我会将此更改提交到 catch 项目。
// already exists in the catch header
template<typename T, typename Allocator>
std::string toString( std::vector<T,Allocator> const& v ) {
return Detail::rangeToString( v.begin(), v.end() );
}
// my modification in the catch header
template<typename T, std::size_t _Nm>
std::string toString( std::array<T, _Nm> const& v ) {
return Detail::rangeToString( v.begin(), v.end() );
}
- C++11 中不同类型的对象的 std::array 的替代方案
- constexpr begin of a std::array
- C++如果必须在编译时确定大小,std::array 有什么意义?
- 标准::unordered_map 中的 std::array 的值初始化
- 确保编译时的特定 std::array 位置
- std::array的长度有大小限制吗?
- 将 std::array 移动到另一个 std::array
- 为什么 std::shared_ptr 被认为是"heavy"和"expensive",但 std::array "same perfprmance as plain (c-style) arrays
- std::bind on statd::array 的运算符 []
- 检查输入 std::array 指针数据是否等于某个常量数组
- 如何读/写或遍历 std::array 中的特定元素范围?
- 是否可以使用 std::array 作为 POD 结构的数据容器?
- 如何在C++中传递一个大小未知的 std::array?
- 在现代C++中将 std::array<std:::array<T,N>> 转换为 std::vector<T>
- 在 C++ 中访问 std::array<std::array> 元素
- 将标量+std::array+std::tuple转换为一个大元组
- 如何初始化 std::array<std::array<T, 2>, 2> 的对象?
- 初始化 std::p air<double, std::array<std::p air<double, double>, 3> >
- 单独移动数组(raw array, std::array, std::vector)的每个元素
- 包装方法返回 c++ std::array<std::string, 4> in cython