Skip to content

Commit

Permalink
Add nearly comparison support for Unit type.
Browse files Browse the repository at this point in the history
Comparing the Unit type will always return true.
  • Loading branch information
se-mo committed Aug 15, 2023
1 parent 07c1340 commit 3c1d960
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
26 changes: 26 additions & 0 deletions nearly/src/nearly_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self>) -> bool {
true
}
}

impl NearlyEqUlps for () {
fn nearly_eq_ulps(&self, _other: &Self, _ulps: UlpsToleranceType<Self>) -> bool {
true
}
}
24 changes: 24 additions & 0 deletions nearly/tests/unit_test.rs
Original file line number Diff line number Diff line change
@@ -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));
}

0 comments on commit 3c1d960

Please sign in to comment.