From cf65408924c92e6d79e61574b2e56cd44085367c Mon Sep 17 00:00:00 2001 From: Masaharu TASHIRO Date: Sat, 20 Aug 2022 03:09:01 +0900 Subject: [PATCH] test(gaussian): add tests for value, values, and fit --- src/gaussian/gaussian.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/gaussian/gaussian.rs b/src/gaussian/gaussian.rs index 994c049..d0ebb9d 100644 --- a/src/gaussian/gaussian.rs +++ b/src/gaussian/gaussian.rs @@ -228,6 +228,8 @@ where #[cfg(test)] mod tests { use super::*; + use approx::assert_abs_diff_eq; + use ndarray::Array; use std::convert::TryInto; #[test] @@ -261,6 +263,42 @@ mod tests { assert_eq!(gaussian.a, a); } + #[test] + fn value() { + let gaussian = Gaussian::new(5., 3., 1.); + let x = 5.; + let y = gaussian.value(x); + assert_eq!(&y, gaussian.a()); + } + + #[test] + fn values() { + let gaussian = Gaussian::new(5., 3., 1.); + let x_vec: Array1 = Array::range(1., 10., 1.); + let y_vec: Array1 = gaussian.values(x_vec); + let expected_ans = array![ + 0.41111229050718745, + 0.6065306597126334, + 0.8007374029168081, + 0.9459594689067654, + 1., + 0.9459594689067654, + 0.8007374029168081, + 0.6065306597126334, + 0.41111229050718745 + ]; + assert_abs_diff_eq!(&y_vec, &expected_ans, epsilon = 1e-9); + } + + #[test] + fn fit() { + let gaussian = Gaussian::new(5., 3., 1.); + let x_vec: Array1 = Array::range(1., 10., 1.); + let y_vec: Array1 = gaussian.values(x_vec.clone()); + let estimated = Gaussian::fit(x_vec, y_vec).unwrap(); + assert_abs_diff_eq!(gaussian, estimated, epsilon = 1e-9); + } + #[test] fn as_tuple() { let (mu, sigma, a): (f64, f64, f64) = (1., 2., 3.);