Function googletest::matchers::near
source · pub fn near<T: Debug + Float + Copy>(
expected: T,
max_abs_error: T
) -> NearMatcher<T>
Expand description
Matches a value equal within max_abs_error
of expected
.
The type T
of the actual, expected
, and max_abs_error
values must
implement Float
.
The values expected
and max_abs_error
may not be NaN. The value
max_abs_error
must be non-negative. The matcher panics on construction
otherwise.
verify_that!(1.0, near(1.0, 0.1))?; // Passes
verify_that!(1.01, near(1.0, 0.1))?; // Passes
verify_that!(1.25, near(1.0, 0.25))?; // Passes
verify_that!(0.75, near(1.0, 0.25))?; // Passes
verify_that!(1.101, near(1.0, 0.1))?; // Fails
verify_that!(0.899, near(1.0, 0.1))?; // Fails
verify_that!(100.25, near(100.0, 0.25))?; // Passes
The default behaviour for special values is consistent with the IEEE floating point standard. Thus infinity is infinitely far away from any floating point value:
verify_that!(f64::INFINITY, near(0.0, f64::MAX))?; // Fails
verify_that!(0.0, near(f64::INFINITY, f64::MAX))?; // Fails
verify_that!(f64::INFINITY, near(f64::INFINITY, f64::MAX))?; // Fails
Similarly, by default, NaN
is infinitely far away from any value:
verify_that!(f64::NAN, near(0.0, f64::MAX))?; // Fails
verify_that!(0.0, near(f64::NAN, f64::MAX))?; // Fails
verify_that!(f64::NAN, near(f64::NAN, f64::MAX))?; // Fails
To treat two NaN
values as equal, use the method
NearMatcher::nans_are_equal
.
verify_that!(f64::NAN, near(f64::NAN, f64::MAX).nans_are_equal())?; // Passes