From 3c1d960d3a1e249d5e717a12670668dbd5577ab5 Mon Sep 17 00:00:00 2001 From: Sebastian Mock Date: Tue, 15 Aug 2023 16:49:33 +0200 Subject: [PATCH] Add nearly comparison support for Unit type. Comparing the Unit type will always return true. --- nearly/src/nearly_eq.rs | 26 ++++++++++++++++++++++++++ nearly/tests/unit_test.rs | 24 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 nearly/tests/unit_test.rs diff --git a/nearly/src/nearly_eq.rs b/nearly/src/nearly_eq.rs index de3d64f..85f2e45 100644 --- a/nearly/src/nearly_eq.rs +++ b/nearly/src/nearly_eq.rs @@ -474,3 +474,29 @@ impl_nearly_tuple!( Lhs Lhs Lhs Lhs Lhs Lhs Lhs Lhs Lhs Lhs Lhs Lhs, Rhs Rhs Rhs Rhs Rhs Rhs Rhs Rhs Rhs Rhs Rhs Rhs, 11 10 9 8 7 6 5 4 3 2 1 0); + +////////// +// unit // +////////// + +impl EpsTolerance for () { + type T = (); + const DEFAULT: () = (); +} + +impl UlpsTolerance for () { + type T = (); + const DEFAULT: () = (); +} + +impl NearlyEqEps for () { + fn nearly_eq_eps(&self, _other: &Self, _eps: EpsToleranceType) -> bool { + true + } +} + +impl NearlyEqUlps for () { + fn nearly_eq_ulps(&self, _other: &Self, _ulps: UlpsToleranceType) -> bool { + true + } +} diff --git a/nearly/tests/unit_test.rs b/nearly/tests/unit_test.rs new file mode 100644 index 0000000..534e1ab --- /dev/null +++ b/nearly/tests/unit_test.rs @@ -0,0 +1,24 @@ +use nearly::Tolerance; +use nearly::{NearlyEq, NearlyEqEps, NearlyEqTol, NearlyEqUlps}; + +#[test] +fn nearly_eq_unit() { + let a = (); + let b = (); + + assert!(a.nearly_eq_eps(&b, ())); + assert!(a.nearly_eq_ulps(&b, ())); + assert!(a.nearly_eq_tol(&b, Tolerance::<()>::new((), ()))); + assert!(a.nearly_eq(&b)); +} + +#[test] +fn nearly_ne_unit() { + let a = (); + let b = (); + + assert!(!a.nearly_ne_eps(&b, ())); + assert!(!a.nearly_ne_ulps(&b, ())); + assert!(!a.nearly_ne_tol(&b, Tolerance::<()>::new((), ()))); + assert!(!a.nearly_ne(&b)); +}