Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve #4612 Feature add EXPECT/ASSERT_FLOAT/DOUBLE_NE (not equal) #4670

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions googletest/include/gtest/gtest.h
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,29 @@ AssertionResult CmpHelperFloatingPointEQ(const char* lhs_expression,
false);
}

template <typename RawType>
AssertionResult CmpHelperFloatingPointNE(const char* lhs_expression,
const char* rhs_expression,
RawType lhs_value, RawType rhs_value) {
const FloatingPoint<RawType> lhs(lhs_value), rhs(rhs_value);

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

::std::stringstream lhs_ss;
lhs_ss.precision(std::numeric_limits<RawType>::digits10 + 2);
lhs_ss << lhs_value;

::std::stringstream rhs_ss;
rhs_ss.precision(std::numeric_limits<RawType>::digits10 + 2);
rhs_ss << rhs_value;

return NeFailure(lhs_expression, rhs_expression,
StringStreamToString(&lhs_ss), StringStreamToString(&rhs_ss),
false);
}

// Helper function for implementing ASSERT_NEAR.
//
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
Expand Down Expand Up @@ -1998,6 +2021,22 @@ class TestWithParam : public Test, public WithParamInterface<T> {};
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \
val1, val2)

#define EXPECT_FLOAT_NE(val1, val2) \
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointNE<float>, \
val1, val2)

#define EXPECT_DOUBLE_NE(val1, val2) \
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointNE<double>, \
val1, val2)

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

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

#define EXPECT_NEAR(val1, val2, abs_error) \
EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, val1, val2, \
abs_error)
Expand Down
6 changes: 6 additions & 0 deletions googletest/include/gtest/internal/gtest-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ GTEST_API_ AssertionResult EqFailure(const char* expected_expression,
const std::string& actual_value,
bool ignoring_case);

GTEST_API_ AssertionResult NeFailure(const char* expected_expression,
const char* actual_expression,
const std::string& expected_value,
const std::string& actual_value,
bool ignoring_case);

// Constructs a failure message for Boolean assertions such as EXPECT_TRUE.
GTEST_API_ std::string GetBoolAssertionFailureMessage(
const AssertionResult& assertion_result, const char* expression_text,
Expand Down
31 changes: 31 additions & 0 deletions googletest/src/gtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,37 @@ AssertionResult EqFailure(const char* lhs_expression,
return AssertionFailure() << msg;
}

AssertionResult NeFailure(const char* lhs_expression,
const char* rhs_expression,
const std::string& lhs_value,
const std::string& rhs_value, bool ignoring_case) {
Message msg;
msg << "Expected inequality of these values:";
msg << "\n " << lhs_expression;
if (lhs_value != lhs_expression) {
msg << "\n Which is: " << lhs_value;
}
msg << "\n " << rhs_expression;
if (rhs_value != rhs_expression) {
msg << "\n Which is: " << rhs_value;
}

if (ignoring_case) {
msg << "\nIgnoring case";
}

if (!lhs_value.empty() && !rhs_value.empty()) {
const std::vector<std::string> lhs_lines = SplitEscapedString(lhs_value);
const std::vector<std::string> rhs_lines = SplitEscapedString(rhs_value);
if (lhs_lines.size() > 1 || rhs_lines.size() > 1) {
msg << "\nWith diff:\n"
<< edit_distance::CreateUnifiedDiff(lhs_lines, rhs_lines);
}
}

return AssertionFailure() << msg;
}

// Constructs a failure message for Boolean assertions such as EXPECT_TRUE.
std::string GetBoolAssertionFailureMessage(
const AssertionResult& assertion_result, const char* expression_text,
Expand Down
69 changes: 69 additions & 0 deletions googletest/test/gtest_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2826,6 +2826,10 @@ TEST_F(FloatTest, Zeros) {
EXPECT_FLOAT_EQ(0.0, -0.0);
EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(-0.0, 1.0), "1.0");
EXPECT_FATAL_FAILURE(ASSERT_FLOAT_EQ(0.0, 1.5), "1.5");

EXPECT_FLOAT_NE(-0.0, 1.0);
EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_NE(-0.0,0.0), "0.0");
EXPECT_FATAL_FAILURE(ASSERT_FLOAT_NE(-0.0, 0.0), "0.0");
}

// Tests comparing numbers close to 0.
Expand All @@ -2851,18 +2855,41 @@ TEST_F(FloatTest, AlmostZeros) {
ASSERT_FLOAT_EQ(v.close_to_positive_zero, v.further_from_negative_zero);
},
"v.further_from_negative_zero");

EXPECT_FLOAT_NE(v.close_to_positive_zero, v.further_from_negative_zero);

EXPECT_FATAL_FAILURE(
{ // NOLINT
ASSERT_FLOAT_NE(0.0, v.close_to_positive_zero);
ASSERT_FLOAT_NE(v.close_to_negative_zero, v.close_to_positive_zero);
},
"v.close_to_positive_zero");

EXPECT_FATAL_FAILURE(
{ // NOLINT
ASSERT_FLOAT_NE(-0.0, v.close_to_negative_zero);
ASSERT_FLOAT_NE(v.close_to_positive_zero, v.close_to_negative_zero);
},
"v.close_to_negative_zero");

}

// Tests comparing numbers close to each other.
TEST_F(FloatTest, SmallDiff) {
EXPECT_FLOAT_EQ(1.0, values_.close_to_one);
EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(1.0, values_.further_from_one),
"values_.further_from_one");

EXPECT_FLOAT_NE(1.0, values_.further_from_one);
EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_NE(1.0, values_.close_to_one),
"values_.close_to_one");
}

// Tests comparing numbers far apart.
TEST_F(FloatTest, LargeDiff) {
EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(2.5, 3.0), "3.0");

EXPECT_FLOAT_NE(2.5, 3.5);
}

// Tests comparing with infinity.
Expand Down Expand Up @@ -2920,6 +2947,12 @@ TEST_F(FloatTest, Commutative) {
// We already tested EXPECT_FLOAT_EQ(1.0, values_.further_from_one).
EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_EQ(values_.further_from_one, 1.0),
"1.0");

// We already tested EXPECT_FLOAT_NE(1.0, values_.close_to_one).
EXPECT_NONFATAL_FAILURE(EXPECT_FLOAT_NE(values_.close_to_one, 1.0), "1.0");

// We already tested EXPECT_FLOAT_NE(1.0, values_.further_from_one).
EXPECT_FLOAT_NE(values_.further_from_one, 1.0);
}

// Tests EXPECT_NEAR.
Expand Down Expand Up @@ -3007,6 +3040,10 @@ TEST_F(DoubleTest, Zeros) {
EXPECT_DOUBLE_EQ(0.0, -0.0);
EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(-0.0, 1.0), "1.0");
EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_EQ(0.0, 1.0), "1.0");

EXPECT_DOUBLE_NE(-0.0, 1.0);
EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_NE(-0.0, 0.0), "0.0");
EXPECT_FATAL_FAILURE(ASSERT_DOUBLE_NE(-0.0, 0.0), "0.0");
}

// Tests comparing numbers close to 0.
Expand All @@ -3033,18 +3070,44 @@ TEST_F(DoubleTest, AlmostZeros) {
v.further_from_negative_zero);
},
"v.further_from_negative_zero");

EXPECT_DOUBLE_NE(v.close_to_positive_zero, v.further_from_negative_zero);

EXPECT_FATAL_FAILURE(
{ // NOLINT
ASSERT_DOUBLE_NE(0.0,
v.close_to_positive_zero);
ASSERT_DOUBLE_NE(v.close_to_negative_zero,
v.close_to_positive_zero);
},
"v.close_to_positive_zero");

EXPECT_FATAL_FAILURE(
{ // NOLINT
ASSERT_DOUBLE_NE(-0.0,
v.close_to_negative_zero);
ASSERT_DOUBLE_NE(v.close_to_positive_zero,
v.close_to_negative_zero);
},
"v.close_to_negative_zero");
}

// Tests comparing numbers close to each other.
TEST_F(DoubleTest, SmallDiff) {
EXPECT_DOUBLE_EQ(1.0, values_.close_to_one);
EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(1.0, values_.further_from_one),
"values_.further_from_one");

EXPECT_DOUBLE_NE(1.0, values_.further_from_one);
EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_NE(1.0, values_.close_to_one),
"values_.close_to_one");
}

// Tests comparing numbers far apart.
TEST_F(DoubleTest, LargeDiff) {
EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(2.0, 3.0), "3.0");

EXPECT_DOUBLE_NE(2.0, 3.0);
}

// Tests comparing with infinity.
Expand Down Expand Up @@ -3097,6 +3160,12 @@ TEST_F(DoubleTest, Commutative) {
// We already tested EXPECT_DOUBLE_EQ(1.0, values_.further_from_one).
EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_EQ(values_.further_from_one, 1.0),
"1.0");

// We already tested EXPECT_DOUBLE_NE(1.0, values_.close_to_one).
EXPECT_NONFATAL_FAILURE(EXPECT_DOUBLE_NE(values_.close_to_one, 1.0), "1.0");

// We already tested EXPECT_FLOAT_NE(1.0, values_.further_from_one).
EXPECT_DOUBLE_NE(values_.further_from_one, 1.0);
}

// Tests EXPECT_NEAR.
Expand Down