unwrap: more tests

This commit is contained in:
Robert Jördens 2020-12-05 11:58:59 +01:00
parent 9b6f9c5744
commit 526fea8e23

View File

@ -2,15 +2,16 @@ use serde::{Deserialize, Serialize};
/// Get phase wrap from x to y. /// Get phase wrap from x to y.
/// ///
/// Phases are modulo integer overflow. /// Phases are modulo integer overflow. The wraps have the same bias as
/// the base data type itself.
/// ///
/// Args: /// Args:
/// * `x`: Old phase sample /// * `x`: Old phase sample
/// * `y`: New phase sample /// * `y`: New phase sample
/// ///
/// Returns: /// Returns:
/// A tuple containg the (wrapped) phase difference and /// A tuple containg the (wrapped) phase difference and the signum of the wrap.
/// one times the direction of the wrap. #[inline(always)]
pub fn get_wrap(x: i32, y: i32) -> (i32, i8) { pub fn get_wrap(x: i32, y: i32) -> (i32, i8) {
let delta = y.wrapping_sub(x); let delta = y.wrapping_sub(x);
let wrap = (delta >= 0) as i8 - (y >= x) as i8; let wrap = (delta >= 0) as i8 - (y >= x) as i8;
@ -27,15 +28,15 @@ pub struct Unwrapper {
} }
impl Unwrapper { impl Unwrapper {
/// Unwrap a new sample from a phase sequence and update the /// Unwrap a new sample from a phase sequence and update the unwrapper
/// unwrapper state. /// state.
/// ///
/// Args: /// Args:
/// * `x`: New phase sample /// * `x`: New phase sample
/// ///
/// Returns: /// Returns:
/// A tuple containing the (wrapped) phase difference /// A tuple containing the (wrapped) phase difference and the signed
/// and the signed number of phase wraps corresponding to the new sample. /// number of phase wraps accumulated to the new sample.
pub fn update(&mut self, x: i32) -> (i32, i32) { pub fn update(&mut self, x: i32) -> (i32, i32) {
let (dx, v) = get_wrap(self.x, x); let (dx, v) = get_wrap(self.x, x);
self.x = x; self.x = x;
@ -51,18 +52,24 @@ mod tests {
fn mini() { fn mini() {
for (x0, x1, v) in [ for (x0, x1, v) in [
(0i32, 0i32, 0i8), (0i32, 0i32, 0i8),
(1, 1, 0), (0, 1, 0),
(-1, -1, 0), (0, -1, 0),
(1, -1, 0), (1, 0, 0),
(-1, 1, 0), (-1, 0, 0),
(0, 0x7fff_ffff, 0), (0, 0x7fff_ffff, 0),
(-1, 0x7fff_ffff, -1), (-1, 0x7fff_ffff, -1),
(-2, 0x7fff_ffff, -1),
(-1, -0x8000_0000, 0),
(0, -0x8000_0000, 0), (0, -0x8000_0000, 0),
(1, -0x8000_0000, 1), (1, -0x8000_0000, 1),
(-0x6000_0000, 0x6000_0000, -1), (-0x6000_0000, 0x6000_0000, -1),
(0x6000_0000, -0x6000_0000, 1), (0x6000_0000, -0x6000_0000, 1),
(0x6000_0000, -0x6000_0000, 1), (-0x4000_0000, 0x3fff_ffff, 0),
(0x6000_0000, -0x6000_0000, 1), (-0x4000_0000, 0x4000_0000, -1),
(-0x4000_0000, 0x4000_0001, -1),
(0x4000_0000, -0x3fff_ffff, 0),
(0x4000_0000, -0x4000_0000, 0),
(0x4000_0000, -0x4000_0001, 1),
] ]
.iter() .iter()
{ {