2010-08-18 59 views

回答

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); 
} 
6

您可以使用伴侣模拟框架Google Mock。它的匹配的强大的类库(一拉Hamcrest),您可以用EXPECT_THAT/ASSERT_THAT宏使用:

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

,而不是创建一个新的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的解决方案优雅。不过,就我而言,行号就足够了。