Skip to main content

conspire/math/assert/
mod.rs

1mod eq;
2mod error;
3mod fd;
4mod fd_eq;
5
6#[cfg(test)]
7mod test;
8
9pub use self::{eq::AssertEq, error::AssertionError, fd::FiniteDifference, fd_eq::AssertFd};
10
11use self::eq::{zero_impl, zero_within_tols_impl};
12use crate::{
13    ABS_TOL, EPSILON, REL_TOL,
14    math::{Scalar, Tensor},
15};
16use std::fmt::Display;
17
18/// Specifies tolerances used by [`AssertEq`] functionalities.
19pub struct Assert {
20    pub abs_tol: Scalar,
21    pub rel_tol: Scalar,
22    pub fd_tol: Scalar,
23}
24
25impl Default for Assert {
26    fn default() -> Self {
27        Self {
28            abs_tol: ABS_TOL,
29            rel_tol: REL_TOL,
30            fd_tol: 3.0 * EPSILON,
31        }
32    }
33}
34
35impl Assert {
36    /// Asserts exact equality.
37    pub fn eq<T, Rhs>(a: T, b: Rhs) -> Result<(), AssertionError>
38    where
39        T: AssertEq<Rhs>,
40    {
41        T::eq(a, b)
42    }
43    /// Asserts equality within `self.abs_tol` and `self.rel_tol`.
44    pub fn eq_within_tols<T, Rhs>(&self, a: T, b: Rhs) -> Result<(), AssertionError>
45    where
46        T: AssertEq<Rhs>,
47    {
48        T::eq_within_tols(self, a, b)
49    }
50    /// Asserts finite-difference equality within `self.fd_tol`.
51    pub fn eq_within_fd_tol<T, Rhs>(&self, a: T, b: Rhs) -> Result<(), AssertionError>
52    where
53        T: AssertFd<Rhs>,
54    {
55        T::eq_within_fd_tol(self, a, b)
56    }
57    /// Asserts exact equality with zero.
58    pub fn zero<T>(a: &T) -> Result<(), AssertionError>
59    where
60        T: Display + Tensor,
61    {
62        zero_impl(a)
63    }
64    /// Asserts equality with zero within `self.abs_tol` and `self.rel_tol`.
65    pub fn zero_within_tols<T>(&self, a: &T) -> Result<(), AssertionError>
66    where
67        T: Display + Tensor,
68    {
69        zero_within_tols_impl(self, a)
70    }
71}