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

后端 未结 3 1363
挽巷
挽巷 2021-01-18 01:00

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?

3条回答
  •  感情败类
    2021-01-18 01:15

    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, \
                          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 
    AssertionResult CmpHelperFloatingPointNE(const char* expected_expression,
                                             const char* actual_expression,
                                             RawType expected,
                                             RawType actual) {
      const FloatingPoint lhs(expected), rhs(actual);
    
      if ( ! lhs.AlmostEquals(rhs)) {
        return AssertionSuccess();
      }
    
      StrStream expected_ss;
      expected_ss << std::setprecision(std::numeric_limits::digits10 + 2)
                  << expected;
    
      StrStream actual_ss;
      actual_ss << std::setprecision(std::numeric_limits::digits10 + 2)
                << actual;
    
      Message msg;
      msg << "Expected: (" << expected_expression << ") != (" << actual_expression
          << "), actual: (" << StrStreamToString(expected_ss) << ") == ("
          << StrStreamToString(actual_ss) << ")";
      return AssertionFailure(msg);   
    }
    

提交回复
热议问题