Skip to main content

expect_float_eq

Macro expect_float_eq 

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

Marks test as failed and continues execution if the float given by the first argument is not approximately equal to the float given by the 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.

This is a not-fatal failure. The test continues execution even after the macro execution.

This can only be invoked inside tests with the gtest attribute. The failure must be generated in the same thread as that running the test itself.

Example:

use googletest::prelude::*;

#[gtest]
fn should_fail() {
    expect_float_eq!(1.0, 2.0);
    println!("This will print!");
}

One may include formatted arguments in the failure message:

 use googletest::prelude::*;

 #[gtest]
 fn should_fail() {
     let argument = "argument"
     expect_float_eq!(1.0, 2.0, "custom failure message: {argument}");
     println!("This will print!");
 }