Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
Namespaces | Macros
gtest.hpp File Reference

GoogleTest integration for ULP-based floating-point comparisons. More...

#include "floating_eq.hpp"
#include "trace.hpp"
#include <gtest/gtest.h>
#include <uni20/core/scalar_io.hpp>
Include dependency graph for gtest.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  uni20
 
namespace  uni20::check
 

Macros

#define ASSERT_FLOATING_EQ(a, b, ...)
 
#define EXPECT_FLOATING_EQ(a, b, ...)
 

Detailed Description

GoogleTest integration for ULP-based floating-point comparisons.

This header defines two macros, EXPECT_FLOATING_EQ and ASSERT_FLOATING_EQ, for use inside GoogleTest unit tests. They extend the standard GTest floating-point comparison macros (EXPECT_FLOAT_EQ, EXPECT_DOUBLE_EQ) to:

Usage patterns:

float a = 1.0f;
float b = std::nextafter(a, 2.0f);
// Default tolerance of 4 ULPs
// Explicit tolerance of 1 ULP
// ASSERT_ variant aborts the current test case on failure
#define EXPECT_FLOATING_EQ(a, b,...)
Definition gtest.hpp:88
#define ASSERT_FLOATING_EQ(a, b,...)
Definition gtest.hpp:102

Failure output shows:

Note
These macros are intended for unit tests only. For assertions in library code, use CHECK_FLOATING_EQ / PRECONDITION_FLOATING_EQ from trace.hpp.
Precondition
The compared type must satisfy uni20::check::UlpComparable.
Postcondition
On failure, the macros report via GoogleTest (ADD_FAILURE or FAIL).
See also
CHECK_FLOATING_EQ, PRECONDITION_FLOATING_EQ, uni20::check::float_distance

Macro Definition Documentation

◆ ASSERT_FLOATING_EQ

#define ASSERT_FLOATING_EQ (   a,
  b,
  ... 
)
Value:
do \
{ \
auto va = (a); \
auto vb = (b); \
auto ulps = ::trace::detail::get_ulps(va, vb __VA_OPT__(, __VA_ARGS__)); \
auto result = ::uni20::check::detail::floating_eq_assertion("ASSERT_FLOATING_EQ", #a, #b, va, vb, ulps); \
if (!result) \
{ \
FAIL() << result.message(); \
} \
} \
while (0)

◆ EXPECT_FLOATING_EQ

#define EXPECT_FLOATING_EQ (   a,
  b,
  ... 
)
Value:
do \
{ \
auto va = (a); \
auto vb = (b); \
auto ulps = ::trace::detail::get_ulps(va, vb __VA_OPT__(, __VA_ARGS__)); \
auto result = ::uni20::check::detail::floating_eq_assertion("EXPECT_FLOATING_EQ", #a, #b, va, vb, ulps); \
if (!result) \
{ \
ADD_FAILURE() << result.message(); \
} \
} \
while (0)