Skip to content

Commit

Permalink
Playtime: Fix audio interaction handler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helgoboss committed May 7, 2024
1 parent e7d3c40 commit 1b15f29
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
43 changes: 43 additions & 0 deletions base/src/approx_f64.rs
Original file line number Diff line number Diff line change
@@ -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<const E: u32>(pub f64);

impl<const E: u32> ApproxF64<E> {
pub fn new(raw: f64) -> Self {
Self(raw)
}
}

impl<const E: u32> PartialEq for ApproxF64<E> {
fn eq(&self, other: &Self) -> bool {
(self.0 - other.0).abs() < 1.0 / E as f64
}
}

impl<const E: u32> Display for ApproxF64<E> {
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));
}
}
3 changes: 3 additions & 0 deletions base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ pub mod byte_pattern;
pub mod serde_json_util;

pub mod panic_util;

mod approx_f64;
pub use approx_f64::*;
2 changes: 1 addition & 1 deletion playtime-clip-engine

0 comments on commit 1b15f29

Please sign in to comment.