Google 测试中的自定义 EXPECT_NEAR 宏

发布于 2024-11-30 04:45:45 字数 567 浏览 0 评论 0原文

范围:使用 Google Test 和 OpenCV。

我想测试我的 Vec3f 是否等于另一个 Vec3fVec3f 是 OpenCV 中的一个维度为 3、类型为 float 的向量。定义了 == 运算符,因此 EXPECT_EQ(Vec3f(), Vec3f()) 可以工作。

但由于它们是浮点数,我想使用 EXPECT_NEAR(float a, float b, float delta) 宏。我该怎么做才能像 EXPECT_NEAR(vec_a, vec_b, float delta) 一样使用它?

目前我正在循环遍历向量的每个元素并在那里执行 EXPECT_NEAR 。

这可能与以下内容相关:GoogleTest for 中的便捷方法不等于的双重比较?

Scope: Using Google Test and OpenCV.

I'd like to test that my Vec3f equals another Vec3f. Vec3f is a vector in OpenCV of dimension 3 and type float. The ==-operator is defined, so EXPECT_EQ(Vec3f(), Vec3f()) works.

But as they are floats, I'd like to use the EXPECT_NEAR(float a, float b, float delta) macro. What can I do so that I can use it like EXPECT_NEAR(vec_a, vec_b, float delta)?

At the moment I am looping through each element of the vector and doing an EXPECT_NEAR there.

This might be related: Convenient method in GoogleTest for a double comparison of not equal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半﹌身腐败 2024-12-07 04:45:45

您可以使用Pointwise() 来自 Google Mock 的匹配器。将其与自定义匹配器结合起来,检查两个参数是否接近:

#include <gmock/gmock.h>

using testing::Pointwise;

MATCHER_P(NearWithPrecision, precision, "") {
  return std::abs(std::get<0>(arg) - std::get<1>(arg)) < precision;
}

TEST(FooTest, ArraysNear) {
  EXPECT_THAT(result_array, Pointwise(NearWithPrecision(0.1), expected_array));
}

You can use the Pointwise() matcher from Google Mock. Combine it with a custom matcher that checks that the two arguments are near:

#include <gmock/gmock.h>

using testing::Pointwise;

MATCHER_P(NearWithPrecision, precision, "") {
  return std::abs(std::get<0>(arg) - std::get<1>(arg)) < precision;
}

TEST(FooTest, ArraysNear) {
  EXPECT_THAT(result_array, Pointwise(NearWithPrecision(0.1), expected_array));
}
聽兲甴掵 2024-12-07 04:45:45

你基本上正在做正确的事情。但是,我会使用自定义断言函数,例如:

::testing::AssertionResult AreAllElementsInVectorNear(const Vec3f& a, const Vect3f& b, float delta) {
  if ([MAGIC])
    return ::testing::AssertionSuccess();
  else
    return ::testing::AssertionFailure() << "Vectors differ by more than " << delta;
}

MAGIC 然后会包含您的代码,例如比较两个向量是否具有相同的大小,然后迭代所有元素并相互检查同一索引处的元素是否相差不超过三角洲。请注意,该代码假设 <<为 Vec3f 提供了运算符。

然后使用该函数:

EXPECT_TRUE(AreAllElementsInVectorNear(a, b, 0.1))

如果期望失败,输出可能是:

Value of: AreAllElementsInVectorNear(a, b, 0.1)
  Actual: false (Vectors differ by more then 0.1)
Expected: true

You are doing basically the correct thing. However, I would use a custom assertion function like:

::testing::AssertionResult AreAllElementsInVectorNear(const Vec3f& a, const Vect3f& b, float delta) {
  if ([MAGIC])
    return ::testing::AssertionSuccess();
  else
    return ::testing::AssertionFailure() << "Vectors differ by more than " << delta;
}

MAGIC would then include your code to e.g. compare if both vectors have the same size, followed by iterating over all elements and mutually check if the elements at the same index differ by no more than the delta. Note that the code assumes that the << operator is provided for Vec3f.

The function then is used:

EXPECT_TRUE(AreAllElementsInVectorNear(a, b, 0.1))

If the expect fails the output might be:

Value of: AreAllElementsInVectorNear(a, b, 0.1)
  Actual: false (Vectors differ by more then 0.1)
Expected: true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文