1
0
Fork 0

Add equivalence tests for next() and next_step()

This commit is contained in:
Robbert van der Helm 2022-09-04 18:20:42 +02:00
parent 9f990cc3b9
commit bb6e9c237f
3 changed files with 56 additions and 0 deletions

1
Cargo.lock generated
View file

@ -2583,6 +2583,7 @@ name = "nih_plug"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"approx 0.5.1",
"assert_no_alloc", "assert_no_alloc",
"atomic_float", "atomic_float",
"atomic_refcell", "atomic_refcell",

View file

@ -108,6 +108,9 @@ vst3-sys = { git = "https://github.com/robbert-vdh/vst3-sys.git", branch = "fix/
# Used for the `zstd` feature # Used for the `zstd` feature
zstd = { version = "0.11.2", optional = true } zstd = { version = "0.11.2", optional = true }
[dev-dependencies]
approx = "0.5.1"
[target.'cfg(all(target_family = "unix", not(target_os = "macos")))'.dependencies] [target.'cfg(all(target_family = "unix", not(target_os = "macos")))'.dependencies]
libc = "0.2.124" libc = "0.2.124"

View file

@ -371,6 +371,58 @@ impl Smoothable for i32 {
mod tests { mod tests {
use super::*; 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] #[test]
fn linear_f32_smoothing() { fn linear_f32_smoothing() {
let mut smoother: Smoother<f32> = Smoother::new(SmoothingStyle::Linear(100.0)); let mut smoother: Smoother<f32> = Smoother::new(SmoothingStyle::Linear(100.0));