Skip to content

Commit

Permalink
test(gaussian): add tests for value, values, and fit
Browse files Browse the repository at this point in the history
  • Loading branch information
tasshi-me committed Aug 19, 2022
1 parent 1f23247 commit cf65408
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/gaussian/gaussian.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<f64> = Array::range(1., 10., 1.);
let y_vec: Array1<f64> = 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<f64> = Array::range(1., 10., 1.);
let y_vec: Array1<f64> = 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.);
Expand Down

0 comments on commit cf65408

Please # to comment.