mirror of
https://github.com/google/googletest.git
synced 2026-07-30 16:26:24 +08:00
Compare commits
3 Commits
b4d8b04cb4
...
bf846710cd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf846710cd | ||
|
|
a0f06a70e3 | ||
|
|
d38cdaefc2 |
@ -945,13 +945,13 @@ template <typename T>
|
||||
class [[nodiscard]] UniversalPrinter<std::optional<T>> {
|
||||
public:
|
||||
static void Print(const std::optional<T>& value, ::std::ostream* os) {
|
||||
*os << '(';
|
||||
if (!value) {
|
||||
*os << "nullopt";
|
||||
UniversalPrint(std::nullopt, os);
|
||||
} else {
|
||||
*os << '(';
|
||||
UniversalPrint(*value, os);
|
||||
*os << ')';
|
||||
}
|
||||
*os << ')';
|
||||
}
|
||||
};
|
||||
|
||||
@ -961,27 +961,38 @@ class [[nodiscard]] UniversalPrinter<std::nullopt_t> {
|
||||
static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; }
|
||||
};
|
||||
|
||||
struct UniversalPrinterVisitor {
|
||||
template <typename T>
|
||||
void operator()(const T& arg) const {
|
||||
*os << "'" << GetTypeName<T>() << "(index = " << index << ")' with value ";
|
||||
UniversalPrint(arg, os);
|
||||
}
|
||||
::std::ostream* os;
|
||||
std::size_t index;
|
||||
};
|
||||
|
||||
// Printer for std::variant
|
||||
template <typename... T>
|
||||
class [[nodiscard]] UniversalPrinter<std::variant<T...>> {
|
||||
public:
|
||||
static void Print(const std::variant<T...>& value, ::std::ostream* os) {
|
||||
*os << '(';
|
||||
std::visit(Visitor{os, value.index()}, value);
|
||||
*os << ')';
|
||||
}
|
||||
|
||||
private:
|
||||
struct Visitor {
|
||||
template <typename U>
|
||||
void operator()(const U& u) const {
|
||||
*os << "'" << GetTypeName<U>() << "(index = " << index
|
||||
<< ")' with value ";
|
||||
UniversalPrint(u, os);
|
||||
if (value.valueless_by_exception()) {
|
||||
*os << "(valueless)";
|
||||
} else {
|
||||
*os << '(';
|
||||
std::visit(UniversalPrinterVisitor{os, value.index()}, value);
|
||||
*os << ')';
|
||||
}
|
||||
::std::ostream* os;
|
||||
std::size_t index;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// Printer for std::monostate
|
||||
template <>
|
||||
class [[nodiscard]] UniversalPrinter<std::monostate> {
|
||||
public:
|
||||
static void Print(std::monostate, ::std::ostream* os) {
|
||||
*os << "(monostate)";
|
||||
}
|
||||
};
|
||||
|
||||
// UniversalPrintArray(begin, len, os) prints an array of 'len'
|
||||
|
||||
@ -1606,6 +1606,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.
|
||||
@ -2008,6 +2031,22 @@ class [[nodiscard]] TestWithParam : public Test,
|
||||
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)
|
||||
|
||||
@ -214,6 +214,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,
|
||||
|
||||
@ -1691,6 +1691,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,
|
||||
|
||||
@ -2030,6 +2030,26 @@ TEST(PrintOneofTest, Basic) {
|
||||
PrintToString(Type(NonPrintable{})));
|
||||
}
|
||||
|
||||
TEST(PrintVariantTest, Monostate) {
|
||||
EXPECT_EQ("(monostate)", PrintToString(std::monostate()));
|
||||
|
||||
#if GTEST_HAS_EXCEPTIONS
|
||||
struct ThrowOnMove {
|
||||
ThrowOnMove() = default;
|
||||
ThrowOnMove(ThrowOnMove&& other) { *this = std::move(other); }
|
||||
ThrowOnMove& operator=(ThrowOnMove&&) {
|
||||
(void)std::vector<bool>().at(0);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
std::variant<std::monostate, ThrowOnMove> v = std::monostate();
|
||||
EXPECT_EQ("('std::monostate(index = 0)' with value (monostate))",
|
||||
PrintToString(v));
|
||||
EXPECT_THROW(v = ThrowOnMove(), std::out_of_range);
|
||||
EXPECT_EQ("(valueless)", PrintToString(v));
|
||||
#endif
|
||||
}
|
||||
|
||||
#if GTEST_INTERNAL_HAS_COMPARE_LIB
|
||||
TEST(PrintOrderingTest, Basic) {
|
||||
EXPECT_EQ("(less)", PrintToString(std::strong_ordering::less));
|
||||
|
||||
@ -2836,6 +2836,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.
|
||||
@ -2861,6 +2865,23 @@ 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.
|
||||
@ -2868,11 +2889,17 @@ 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.
|
||||
@ -2930,6 +2957,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.
|
||||
@ -3017,6 +3050,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.
|
||||
@ -3043,6 +3080,26 @@ 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.
|
||||
@ -3050,11 +3107,17 @@ 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.
|
||||
@ -3107,6 +3170,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.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user