diff --git a/dsp/src/lib.rs b/dsp/src/lib.rs index ca1daec..389bda5 100644 --- a/dsp/src/lib.rs +++ b/dsp/src/lib.rs @@ -5,3 +5,6 @@ pub type Complex = (T, T); pub mod iir; pub mod lockin; pub mod pll; + +#[cfg(test)] +mod testing; diff --git a/dsp/src/lockin.rs b/dsp/src/lockin.rs index 66b3a9a..6b6017b 100644 --- a/dsp/src/lockin.rs +++ b/dsp/src/lockin.rs @@ -271,60 +271,9 @@ pub fn magnitude_phase(signal: &mut [Complex]) { #[cfg(test)] mod tests { use super::*; - - fn f32_is_close(a: f32, b: f32) -> bool { - (a - b).abs() <= a.abs().max(b.abs()) * f32::EPSILON - } - - fn complex_is_close(a: Complex, b: Complex) -> bool { - f32_is_close(a.0, b.0) && f32_is_close(a.1, b.1) - } - - fn complex_array_is_close(a: &[Complex], b: &[Complex]) -> bool { - let mut result: bool = true; - a.iter().zip(b.iter()).for_each(|(i, j)| { - result &= complex_is_close(*i, *j); - }); - result - } - - fn within_tolerance( - a: f32, - b: f32, - relative_tolerance: f32, - fixed_tolerance: f32, - ) -> bool { - (a - b).abs() - <= a.abs().max(b.abs()) * relative_tolerance + fixed_tolerance - } - - fn complex_within_tolerance( - a: Complex, - b: Complex, - relative_tolerance: f32, - fixed_tolerance: f32, - ) -> bool { - within_tolerance(a.0, b.0, relative_tolerance, fixed_tolerance) - && within_tolerance(a.1, b.1, relative_tolerance, fixed_tolerance) - } - - fn complex_array_within_tolerance( - a: &[Complex], - b: &[Complex], - relative_tolerance: f32, - fixed_tolerance: f32, - ) -> bool { - let mut result: bool = true; - a.iter().zip(b.iter()).for_each(|(i, j)| { - result &= complex_within_tolerance( - *i, - *j, - relative_tolerance, - fixed_tolerance, - ); - }); - result - } + use crate::testing::{ + complex_array_is_close, complex_array_within_tolerance, + }; #[test] fn array_push() { diff --git a/dsp/src/testing.rs b/dsp/src/testing.rs new file mode 100644 index 0000000..0fe9e4f --- /dev/null +++ b/dsp/src/testing.rs @@ -0,0 +1,54 @@ +use super::Complex; + +pub fn f32_is_close(a: f32, b: f32) -> bool { + (a - b).abs() <= a.abs().max(b.abs()) * f32::EPSILON +} + +pub fn complex_is_close(a: Complex, b: Complex) -> bool { + f32_is_close(a.0, b.0) && f32_is_close(a.1, b.1) +} + +pub fn complex_array_is_close(a: &[Complex], b: &[Complex]) -> bool { + let mut result: bool = true; + a.iter().zip(b.iter()).for_each(|(i, j)| { + result &= complex_is_close(*i, *j); + }); + result +} + +pub fn within_tolerance( + a: f32, + b: f32, + relative_tolerance: f32, + fixed_tolerance: f32, +) -> bool { + (a - b).abs() <= a.abs().max(b.abs()) * relative_tolerance + fixed_tolerance +} + +pub fn complex_within_tolerance( + a: Complex, + b: Complex, + relative_tolerance: f32, + fixed_tolerance: f32, +) -> bool { + within_tolerance(a.0, b.0, relative_tolerance, fixed_tolerance) + && within_tolerance(a.1, b.1, relative_tolerance, fixed_tolerance) +} + +pub fn complex_array_within_tolerance( + a: &[Complex], + b: &[Complex], + relative_tolerance: f32, + fixed_tolerance: f32, +) -> bool { + let mut result: bool = true; + a.iter().zip(b.iter()).for_each(|(i, j)| { + result &= complex_within_tolerance( + *i, + *j, + relative_tolerance, + fixed_tolerance, + ); + }); + result +}