From bb6e9c237f78e7e5a756f72c34c80953b2a4ce25 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sun, 4 Sep 2022 18:20:42 +0200 Subject: [PATCH] Add equivalence tests for next() and next_step() --- Cargo.lock | 1 + Cargo.toml | 3 +++ src/param/smoothing.rs | 52 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index b49ff0c0..310bae65 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2583,6 +2583,7 @@ name = "nih_plug" version = "0.0.0" dependencies = [ "anyhow", + "approx 0.5.1", "assert_no_alloc", "atomic_float", "atomic_refcell", diff --git a/Cargo.toml b/Cargo.toml index a30c09cb..1fd74bf9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,6 +108,9 @@ vst3-sys = { git = "https://github.com/robbert-vdh/vst3-sys.git", branch = "fix/ # Used for the `zstd` feature zstd = { version = "0.11.2", optional = true } +[dev-dependencies] +approx = "0.5.1" + [target.'cfg(all(target_family = "unix", not(target_os = "macos")))'.dependencies] libc = "0.2.124" diff --git a/src/param/smoothing.rs b/src/param/smoothing.rs index 4ecf6557..5bd4a85e 100644 --- a/src/param/smoothing.rs +++ b/src/param/smoothing.rs @@ -371,6 +371,58 @@ impl Smoothable for i32 { mod tests { use super::*; + /// Applying `next()` `n` times should be the same as `next_step()` for `n` steps. + #[test] + fn linear_f32_next_equivalance() { + let style = SmoothingStyle::Linear(100.0); + + let mut current = 0.4; + let target = 0.8; + let steps = 15; + let step_size = style.step_size(current, target, steps); + + let expected_result = style.next_step(current, target, step_size, steps); + for _ in 0..steps { + current = style.next(current, target, step_size); + } + + approx::assert_relative_eq!(current, expected_result, epsilon = 1e-5); + } + + #[test] + fn logarithmic_f32_next_equivalance() { + let style = SmoothingStyle::Logarithmic(100.0); + + let mut current = 0.4; + let target = 0.8; + let steps = 15; + let step_size = style.step_size(current, target, steps); + + let expected_result = style.next_step(current, target, step_size, steps); + for _ in 0..steps { + current = style.next(current, target, step_size); + } + + approx::assert_relative_eq!(current, expected_result, epsilon = 1e-5); + } + + #[test] + fn exponential_f32_next_equivalance() { + let style = SmoothingStyle::Exponential(100.0); + + let mut current = 0.4; + let target = 0.8; + let steps = 15; + let step_size = style.step_size(current, target, steps); + + let expected_result = style.next_step(current, target, step_size, steps); + for _ in 0..steps { + current = style.next(current, target, step_size); + } + + approx::assert_relative_eq!(current, expected_result, epsilon = 1e-5); + } + #[test] fn linear_f32_smoothing() { let mut smoother: Smoother = Smoother::new(SmoothingStyle::Linear(100.0));