From 1b15f29eac48f0e78abef184fd934b25555b9c6b Mon Sep 17 00:00:00 2001 From: Benjamin Klum Date: Tue, 7 May 2024 08:31:21 +0200 Subject: [PATCH] Playtime: Fix audio interaction handler tests --- base/src/approx_f64.rs | 43 ++++++++++++++++++++++++++++++++++++++++++ base/src/lib.rs | 3 +++ playtime-clip-engine | 2 +- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 base/src/approx_f64.rs diff --git a/base/src/approx_f64.rs b/base/src/approx_f64.rs new file mode 100644 index 000000000..2cd954d65 --- /dev/null +++ b/base/src/approx_f64.rs @@ -0,0 +1,43 @@ +use std::fmt::{Display, Formatter}; + +/// An approximate floating-point type that uses the same epsilon for comparison as the == operator in +/// [EEL2](https://www.cockos.com/EEL2/): +/// Two values are considered equal if the difference is less than 0.00001 (1/100000), 0 if not. +pub type AudioF64 = ApproxF64<100000>; + +/// Simple newtype that allows for approximate comparison of 64-bit floating-point numbers. +/// +/// The const type parameter `E` ("epsilon") defines how tolerant floating-point comparison is. Two values are considered +/// equal if the difference is less than 1/E. +#[derive(Copy, Clone, PartialOrd, Debug, Default)] +pub struct ApproxF64(pub f64); + +impl ApproxF64 { + pub fn new(raw: f64) -> Self { + Self(raw) + } +} + +impl PartialEq for ApproxF64 { + fn eq(&self, other: &Self) -> bool { + (self.0 - other.0).abs() < 1.0 / E as f64 + } +} + +impl Display for ApproxF64 { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn basics() { + assert_eq!(AudioF64::new(0.75), AudioF64::new(0.75)); + assert_ne!(AudioF64::new(0.00001), AudioF64::new(0.00002)); + assert_eq!(AudioF64::new(0.000001), AudioF64::new(0.000002)); + } +} diff --git a/base/src/lib.rs b/base/src/lib.rs index 81368080e..a5f7f08cc 100644 --- a/base/src/lib.rs +++ b/base/src/lib.rs @@ -44,3 +44,6 @@ pub mod byte_pattern; pub mod serde_json_util; pub mod panic_util; + +mod approx_f64; +pub use approx_f64::*; diff --git a/playtime-clip-engine b/playtime-clip-engine index 497a501c9..93782cea5 160000 --- a/playtime-clip-engine +++ b/playtime-clip-engine @@ -1 +1 @@ -Subproject commit 497a501c9ed2ca7314675379f10d7f061f925dbd +Subproject commit 93782cea559f8f259f5162148390157e6bed6b80