Gromacs  2025.0-dev-20241011-013a99c
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Macros | Functions | Variables
testasserts.h File Reference
#include "config.h"
#include <optional>
#include <string>
#include <gtest/gtest.h>
#include "gromacs/utility/basedefinitions.h"
#include "gromacs/utility/exceptions.h"
#include "gromacs/utility/real.h"
+ Include dependency graph for testasserts.h:
+ This graph shows which files directly or indirectly include this file:

Description

Extra assertions for unit tests.

This file provides assertion macros that extend/replace Google Test assertions for:

Todo:
The implementation is somewhat ugly, and accesses some Google Test internals. Could be nice to clean it up a bit.
Author
Teemu Murtola teemu.nosp@m..mur.nosp@m.tola@.nosp@m.gmai.nosp@m.l.com

Assertions for floating-point comparison

These routines extend (EXPECT|ASSERT)_(FLOAT|DOUBLE)_EQ and (EXPECT|ASSERT)_NEAR from Google Test to provide more flexible assertions for floating-point values.

See gmx::test::FloatingPointTolerance for the possible ways to specify the tolerance, and gmx::test::FloatingPointDifference for some additional details of the difference calculation.

#define EXPECT_FLOAT_EQ_TOL(value1, value2, tolerance)
 Asserts that two single-precision values are within the given tolerance.
 
#define EXPECT_DOUBLE_EQ_TOL(value1, value2, tolerance)
 Asserts that two double-precision values are within the given tolerance.
 
#define ASSERT_FLOAT_EQ_TOL(value1, value2, tolerance)
 Asserts that two single-precision values are within the given tolerance.
 
#define ASSERT_DOUBLE_EQ_TOL(value1, value2, tolerance)
 Asserts that two double-precision values are within the given tolerance.
 
#define EXPECT_REAL_EQ_TOL(value1, value2, tolerance)
 Asserts that two real values are within the given tolerance.
 
#define ASSERT_REAL_EQ_TOL(value1, value2, tolerance)
 Asserts that two real values are within the given tolerance.
 
#define EXPECT_REAL_EQ(value1, value2)   EXPECT_REAL_EQ_TOL(value1, value2, ::gmx::test::defaultRealTolerance())
 EXPECT_REAL_EQ_TOL with default tolerance.
 
#define ASSERT_REAL_EQ(value1, value2)   ASSERT_REAL_EQ_TOL(value1, value2, ::gmx::test::defaultRealTolerance())
 ASSERT_REAL_EQ_TOL with default tolerance.
 
template<typename FloatType >
static
inline::testing::AssertionResult 
gmx::test::assertEqualWithinTolerance (const char *expr1, const char *expr2, const char *, FloatType value1, FloatType value2, const FloatingPointTolerance &tolerance)
 Assertion predicate formatter for comparing two floating-point values. More...
 

Classes

class  gmx::test::FloatingPointDifference
 Computes and represents a floating-point difference value. More...
 
class  gmx::test::FloatingPointTolerance
 Specifies a floating-point comparison tolerance and checks whether a difference is within the tolerance. More...
 

Macros

#define EXPECT_PLAIN(expr)
 Assert for predicates that return AssertionResult and produce a full failure message. More...
 
#define ASSERT_PLAIN(expr)
 Assert for predicates that return AssertionResult and produce a full failure message. More...
 
Assertions for exceptions

These macros replace (ASSERT|EXPECT)(_NO)?_THROW from Google Test. They are used exactly like the Google Test ones, but also print details of any unexpected exceptions using GROMACS-specific routines. This makes it much easier to see at one glance what went wrong. See Google Test documentation for details on how to use the macros.

#define GMX_TEST_THROW_(statement, expected_exception, fail)
 Internal implementation macro for exception assertations. More...
 
#define GMX_TEST_NO_THROW_(statement, fail)
 Internal implementation macro for exception assertations. More...
 
#define EXPECT_THROW_GMX(statement, expected_exception)
 Asserts that a statement throws a given exception.
 
#define EXPECT_NO_THROW_GMX(statement)
 Asserts that a statement does not throw.
 
#define ASSERT_THROW_GMX(statement, expected_exception)
 Asserts that a statement throws a given exception.
 
#define ASSERT_NO_THROW_GMX(statement)
 Asserts that a statement does not throw.
 
#define GMX_EXPECT_DEATH_IF_SUPPORTED(expr, msg)
 Wrapper around EXPECT_DEATH_IF_SUPPORTED gtest macro for thread safe execution. More...
 
#define GMX_ASSERT_DEATH_IF_SUPPORTED(expr, msg)
 Wrapper around ASSERT_DEATH_IF_SUPPORTED gtest macro for thread safe execution. More...
 

Functions

void gmx::test::internal::processExpectedException (const std::exception &ex)
 Called for an expected exception from EXPECT_THROW_GMX(). More...
 
static
inline::testing::AssertionResult 
gmx::test::plainAssertHelper (const char *, const ::testing::AssertionResult &expr)
 Helper method for (EXPECT|ASSERT)_PLAIN. More...
 
void gmx::test::checkTestNameLength (std::optional< std::string > testName=std::nullopt)
 Gives a GoogleTest assertion if the test's name is too long. More...
 

Variables

constexpr uint64_t gmx::test::detail::g_defaultUlpTolerance = 4
 Default tolerance in ULPs for two floating-point values to compare equal.