From 1ad477ee4f61479e624d5f3d456726de1b3514bf Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Wed, 2 Feb 2022 22:34:29 +0100 Subject: [PATCH] Add smoothing tests --- src/param/smoothing.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/param/smoothing.rs b/src/param/smoothing.rs index 489e3d7b..fdc9cf02 100644 --- a/src/param/smoothing.rs +++ b/src/param/smoothing.rs @@ -150,3 +150,39 @@ impl Smoother { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn linear_f32_smoothing() { + let mut smoother: Smoother = Smoother::new(SmoothingStyle::Linear(100.0)); + smoother.set_target(100.0, 10.0, true); + assert_eq!(smoother.next(), 10.0); + + // Instead of testing the actual values, we'll make sure that we reach the target values at + // the expected time. + smoother.set_target(100.0, 20.0, false); + for _ in 0..(10 - 2) { + dbg!(smoother.next()); + } + assert_ne!(smoother.next(), 20.0); + assert_eq!(smoother.next(), 20.0); + } + + #[test] + fn linear_i32_smoothing() { + let mut smoother: Smoother = Smoother::new(SmoothingStyle::Linear(100.0)); + smoother.set_target(100.0, 10, true); + assert_eq!(smoother.next(), 10); + + // Integers are rounded, but with these values we can still test this + smoother.set_target(100.0, 20, false); + for _ in 0..(10 - 2) { + dbg!(smoother.next()); + } + assert_ne!(smoother.next(), 20); + assert_eq!(smoother.next(), 20); + } +}