use crate::Angle;
use approx::{AbsDiffEq, RelativeEq, UlpsEq};
impl AbsDiffEq for Angle {
type Epsilon = f32;
#[inline]
fn default_epsilon() -> Self::Epsilon {
f32::default_epsilon()
}
#[inline]
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
self.radians.abs_diff_eq(&other.radians, epsilon)
}
}
impl RelativeEq for Angle {
#[inline]
fn default_max_relative() -> Self::Epsilon {
f32::default_max_relative()
}
#[inline]
fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool {
let radians = &self.radians;
let other = &other.radians;
radians.relative_eq(other, epsilon, max_relative)
}
}
impl UlpsEq for Angle {
#[inline]
fn default_max_ulps() -> u32 {
f32::default_max_ulps()
}
#[inline]
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
self.radians.ulps_eq(&other.radians, epsilon, max_ulps)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn abs_diff_eq_angle() {
let a = Angle::from_radians(1.0);
let b = Angle::from_radians(1.0 + f32::EPSILON);
assert!(a.abs_diff_eq(&b, f32::EPSILON));
assert!(!a.abs_diff_eq(&b, 0.0));
}
#[test]
fn abs_diff_default_epsilon() {
let a = Angle::from_radians(1.0);
let b = Angle::from_radians(1.0 + f32::EPSILON);
assert!(a.abs_diff_eq(&b, Angle::default_epsilon()));
}
#[test]
fn relative_eq_angle() {
let a = Angle::from_radians(1.0);
let b = Angle::from_radians(1.0 + f32::EPSILON);
assert!(a.relative_eq(&b, f32::EPSILON, f32::default_max_relative()));
let c = Angle::from_radians(1.0);
let d = Angle::from_radians(2.0);
assert!(!c.relative_eq(&d, f32::EPSILON, f32::default_max_relative()));
}
#[test]
fn relative_eq_default_max_relative() {
let a = Angle::from_radians(1.0);
let b = Angle::from_radians(1.0 + f32::EPSILON);
assert!(a.relative_eq(&b, f32::EPSILON, Angle::default_max_relative()));
}
#[test]
fn ulps_eq_angle() {
let a = Angle::from_radians(1.0);
let b = Angle::from_radians(1.0 + f32::EPSILON);
assert!(a.ulps_eq(&b, f32::EPSILON, 4));
assert!(a.ulps_eq(&b, f32::EPSILON, 0));
let c = Angle::from_radians(1.0);
let d = Angle::from_radians(2.0);
assert!(!c.ulps_eq(&d, f32::EPSILON, 4));
assert!(!c.ulps_eq(&d, f32::EPSILON, 0));
}
#[test]
fn ulps_eq_default_max_ulps() {
let a = Angle::from_radians(1.0);
let b = Angle::from_radians(1.0 + f32::EPSILON);
assert!(a.ulps_eq(&b, f32::EPSILON, Angle::default_max_ulps()));
}
}