-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Playtime: Fix audio interaction handler tests
- Loading branch information
Showing
3 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,3 +44,6 @@ pub mod byte_pattern; | |
pub mod serde_json_util; | ||
|
||
pub mod panic_util; | ||
|
||
mod approx_f64; | ||
pub use approx_f64::*; |
Submodule playtime-clip-engine
updated
from 497a50 to 93782c