C++ 的 CATCH 单元测试比较 std::array

CATCH unit testing for C++ compare std::array

我喜欢使用 catch 进行我的 C++ 单元测试。 我的目标是比较 std::arraystd::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) 扩展: {?} == {?}

如何显示实际值和预期值?我喜欢在 std::array 的违反 REQUIRE 条件下显示与 std::vector.

完全相同的显示

我用的是最新版的catch(v1.10.0)。

我没有检查 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 header 中的 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() );
}

从根本上说,这是一个类型如何被字符串化的问题,为此总是存在 documentation.

精简版就是有一个简单的算法

  1. 检查给定类型的 Catch::StringMaker 专业化。如果存在,就使用它。

  2. 检查给定类型的 operator<< 重载。如果存在,就使用它。

  3. 使用“{?}”。

直到最近,Catch 为 std::vector 提供了开箱即用的专门化,但没有为 std::array 提供专门化,因为 std::array 是 C++11 的一部分并且通常较少使用。 Since version 2.1.0 Catch 而是检查类型是否提供类似容器的接口,特别是响应 begin(T)end(T)。这为许多不同的类型提供了自动字符串化,包括 std::vectorstd::array,还有静态数组。