GoogleTest 中用于不等于双重比较的便捷方法?

发布于 2024-09-14 22:06:16 字数 113 浏览 8 评论 0原文

我正在寻找类似于 ASSERT_DOUBLE_EQ 的 ASSERT_EQ / ASSERT_NE 的东西。

也许我缺少一种在没有 ASSERT_DOUBLE_NE 的情况下执行此操作的简单方法?

I'm looking for something similar to the ASSERT_EQ / ASSERT_NE for ASSERT_DOUBLE_EQ.

Maybe I'm missing an easy way of doing this without having a ASSERT_DOUBLE_NE?

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

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

发布评论

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

评论(3

陪你搞怪i 2024-09-21 22:06:16

您可以使用配套的模拟框架 Google Mock。它有一个强大的匹配器库(类似于 Hamcrest),您可以将其与 EXPECT_THAT/ASSERT_THAT 宏一起使用:

EXPECT_THAT(value, FloatEq(1));
EXPECT_THAT(another_value, Not(DoubleEq(3.14)));

You can use the companion mocking framework Google Mock. It has a powerful library of matchers (a la Hamcrest), which you can use with the EXPECT_THAT/ASSERT_THAT macros:

EXPECT_THAT(value, FloatEq(1));
EXPECT_THAT(another_value, Not(DoubleEq(3.14)));
与往事干杯 2024-09-21 22:06:16

看来你运气不好。不过,您可以自己添加一个。我使用 ASSERT_DOUBLE_EQ 和 ASSERT_NE 作为模式构建了以下代码。

#define ASSERT_DOUBLE_NE(expected, actual)\
  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointNE<double>, \
                      expected, actual)


// Helper template function for comparing floating-points.
//
// Template parameter:
//
//   RawType: the raw floating-point type (either float or double)
//
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
template <typename RawType>
AssertionResult CmpHelperFloatingPointNE(const char* expected_expression,
                                         const char* actual_expression,
                                         RawType expected,
                                         RawType actual) {
  const FloatingPoint<RawType> lhs(expected), rhs(actual);

  if ( ! lhs.AlmostEquals(rhs)) {
    return AssertionSuccess();
  }

  StrStream expected_ss;
  expected_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
              << expected;

  StrStream actual_ss;
  actual_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
            << actual;

  Message msg;
  msg << "Expected: (" << expected_expression << ") != (" << actual_expression
      << "), actual: (" << StrStreamToString(expected_ss) << ") == ("
      << StrStreamToString(actual_ss) << ")";
  return AssertionFailure(msg);   
}

It looks like you're out of luck. However, you could add one yourself. I built the following code using ASSERT_DOUBLE_EQ and ASSERT_NE as a pattern.

#define ASSERT_DOUBLE_NE(expected, actual)\
  ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointNE<double>, \
                      expected, actual)


// Helper template function for comparing floating-points.
//
// Template parameter:
//
//   RawType: the raw floating-point type (either float or double)
//
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
template <typename RawType>
AssertionResult CmpHelperFloatingPointNE(const char* expected_expression,
                                         const char* actual_expression,
                                         RawType expected,
                                         RawType actual) {
  const FloatingPoint<RawType> lhs(expected), rhs(actual);

  if ( ! lhs.AlmostEquals(rhs)) {
    return AssertionSuccess();
  }

  StrStream expected_ss;
  expected_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
              << expected;

  StrStream actual_ss;
  actual_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2)
            << actual;

  Message msg;
  msg << "Expected: (" << expected_expression << ") != (" << actual_expression
      << "), actual: (" << StrStreamToString(expected_ss) << ") == ("
      << StrStreamToString(actual_ss) << ")";
  return AssertionFailure(msg);   
}
情话已封尘 2024-09-21 22:06:16

您可以将宏定义为现有助手的逆,而不是创建新的 CmpHelperFloatingPointNE 助手:

#include "gtest/gtest.h"

#define ASSERT_FLOAT_NE(val1, val2) ASSERT_PRED_FORMAT2( \
  !::testing::internal::CmpHelperFloatingPointEQ<float>, val1, val2 \
)

#define ASSERT_DOUBLE_NE(val1, val2) ASSERT_PRED_FORMAT2( \
  !::testing::internal::CmpHelperFloatingPointEQ<double>, val1, val2 \
)

这不像 deft_code 的解决方案那么优雅,因为当断言失败时,没有诸如“期望值”和“实际值”之类的具体细节”,只是断言的行号和文件。不过,就我的目的而言,行号就足够了。

instead of creating a new CmpHelperFloatingPointNE helper, you can just define the macro as the inverse of the existing helper:

#include "gtest/gtest.h"

#define ASSERT_FLOAT_NE(val1, val2) ASSERT_PRED_FORMAT2( \
  !::testing::internal::CmpHelperFloatingPointEQ<float>, val1, val2 \
)

#define ASSERT_DOUBLE_NE(val1, val2) ASSERT_PRED_FORMAT2( \
  !::testing::internal::CmpHelperFloatingPointEQ<double>, val1, val2 \
)

This is not as graceful as deft_code's solution because when the assertion fails, there are no specific details like "expected value" and "actual value", just the line number and file of the assertion. For my purposes, though, the line number was enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文