Bequeme Methode in GoogleTest für einen doppelten Vergleich von ungleich?

Bequeme Methode in GoogleTest für einen doppelten Vergleich von ungleich?


Ich suche etwas Ähnliches wie ASSERT_EQ / ASSERT_NE für ASSERT_DOUBLE_EQ.


Vielleicht fehlt mir eine einfache Möglichkeit, dies ohne ASSERT_DOUBLE_NE zu tun?


Einige Code-Antworten


EXPECT_TRUE(my_condition) << "My condition is not true"; 
switch(expression) {   case 1:
... some checks ... case 2:
... some other checks ... default:
FAIL() << "We shouldn't get here."; }
#include "gmock/gmock.h"  using ::testing::AllOf; using ::testing::Gt; using ::testing::Lt; using ::testing::MatchesRegex; using ::testing::StartsWith;  ... EXPECT_THAT(value1, StartsWith("Hello")); EXPECT_THAT(value2, MatchesRegex("Line \\d+")); ASSERT_THAT(value3, AllOf(Gt(5), Lt(10))); 
Value of: value1   Actual: "Hi, world!" Expected: starts with "Hello" 
EXPECT_NO_THROW({   int n = 5;   DoSomething(&n); }); 
// Returns true if m and n have no common divisors except 1. bool MutuallyPrime(int m, int n) { ... } ... const int a = 3; const int b = 4; const int c = 10; ... EXPECT_PRED2(MutuallyPrime, a, b);  // Succeeds EXPECT_PRED2(MutuallyPrime, b, c);  // Fails 
MutuallyPrime(b, c) is false, where b is 4 c is 10 
EXPECT_PRED1(static_cast<bool (*)(int)>(IsPositive), 5); EXPECT_PRED1(static_cast<bool (*)(double)>(IsPositive), 3.14); 
template <typename T> bool IsNegative(T x) {   return x < 0; } ... EXPECT_PRED1(IsNegative<int>, -5);  // Must specify type for IsNegative 
ASSERT_PRED2((MyPredicate<int, int>), 5, 0); 
testing::AssertionResult PredicateFormatter(const char* expr1,
const char* expr2,
...
const char* exprn,
T1 val1,
T2 val2,
...
Tn valn);
// Returns the smallest prime common divisor of m and n, // or 1 when m and n are mutually prime. int SmallestPrimeCommonDivisor(int m, int n) { ... }  // Returns true if m and n have no common divisors except 1. bool MutuallyPrime(int m, int n) { ... }  // A predicate-formatter for asserting that two integers are mutually prime. testing::AssertionResult AssertMutuallyPrime(const char* m_expr,
const char* n_expr,
int m,
int n) { if (MutuallyPrime(m, n)) return testing::AssertionSuccess(); return testing::AssertionFailure() << m_expr << " and " << n_expr
<< " (" << m << " and " << n << ") are not mutually prime, "
<< "as they have a common divisor " << SmallestPrimeCommonDivisor(m, n); } ... const int a = 3; const int b = 4; const int c = 10; ... EXPECT_PRED_FORMAT2(AssertMutuallyPrime, a, b); // Succeeds EXPECT_PRED_FORMAT2(AssertMutuallyPrime, b, c); // Fails
b and c (4 and 10) are not mutually prime, as they have a common divisor 2 
CComPtr<IShellDispatch2> shell; ASSERT_HRESULT_SUCCEEDED(shell.CoCreateInstance(L"Shell.Application")); CComVariant empty; ASSERT_HRESULT_SUCCEEDED(shell->ShellExecute(CComBSTR(url), empty, empty, empty, empty)); 
EXPECT_DEATH({   int n = 5;   DoSomething(&n); }, "Error on line .* of DoSomething()"); 
EXPECT_DEATH(DoSomething(42), "My error"); 
// Returns true if the program exited normally with the given exit status code. ::testing::ExitedWithCode(exit_code);  // Returns true if the program was killed by the given signal. // Not available on Windows. ::testing::KilledBySignal(signal_number); 
EXPECT_EXIT(NormalExit(), testing::ExitedWithCode(0), "Success");