Convenient method in GoogleTest for a double comparison of not equal?

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?

8
задан Brandon Leiran 18 August 2010 в 18:19
поделиться

1 ответ

Похоже, вам не повезло. Однако вы можете добавить его самостоятельно. Я создал следующий код, используя 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);   
}
1
ответ дан 5 December 2019 в 21:15
поделиться
Другие вопросы по тегам:

Похожие вопросы: