Add basic tests for ObjecitiveFun struct

This commit is contained in:
Alex Selimov 2025-04-06 23:12:12 -04:00
parent 2993580861
commit f1abeb26d7

View File

@ -82,3 +82,32 @@ impl<T: XVar<E>, E> Fun<T, E> {
Fun { function, prime }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_eval() {
let function = Box::new(|x: &[f64]| -> f64 { x[0] + x[1] });
let fun_with_numerical_diff = FunWithNumericalDiff {
function,
dx: 1e-6,
style: DiffStyle::ForwardDifference,
};
assert_eq!(fun_with_numerical_diff.eval(&vec![1.0, 2.0]), 3.0);
}
#[test]
fn test_prime() {
let function = Box::new(|x: &[f64]| -> f64 { x[0] + x[1] });
let fun_with_numerical_diff = FunWithNumericalDiff {
function,
dx: 1e-6,
style: DiffStyle::ForwardDifference,
};
let prime = fun_with_numerical_diff.prime(&vec![1.0, 2.0]);
assert!(prime[0] - 1.0 < 1e-6);
assert!(prime[1] - 1.0 < 1e-6);
}
}