2010-08-18 52 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的解決方案優雅。不過,就我而言,行號就足夠了。