Skip to main content

verify_float_eq

Macro verify_float_eq 

Source
macro_rules! verify_float_eq {
    ($actual:expr, $expected:expr $(,)?) => { ... };
}
Expand description

Checks whether the float given by first argument is approximately equal to second argument.

This automatically computes a tolerance from the magnitude of expected and matches any actual value within this tolerance of the expected value. The tolerance is chosen to account for the inaccuracies in most ordinary floating point calculations. To see details of how the tolerance is calculated look at the implementation of googletest::approx_eq.

Evaluates to Result::Ok(()) if the first argument is approximately equal to the second and Result::Err(TestAssertionFailure) if it is not. The caller must then decide how to handle the Err variant. It has a few options:

  • Abort the current function with the ? operator. This requires that the function return a suitable Result.
  • Log the test failure and continue by calling the method and_log_failure.

Of course, one can also use all other standard methods on Result.

Invoking this macro by itself does not cause a test failure to be recorded or output. The resulting Result must be handled as described above to cause the test to be recorded as a failure.

Example:

use googletest::prelude::*;

#[test]
fn should_fail() -> Result<()> {
    verify_float_eq!(1.0, 2.0)
}