Fix periodic test module

Add a simple test of periodic_access.
This commit is contained in:
Will Stott 2019-04-02 18:57:52 +00:00
parent dba627a1b4
commit 011090034f

View file

@ -116,3 +116,34 @@ where
self.input.total_duration()
}
}
#[cfg(test)]
mod tests {
use buffer::SamplesBuffer;
use std::time::Duration;
use source::Source;
use std::cell::RefCell;
#[test]
fn stereo_access() {
// Stereo, 1Hz audio buffer
let inner = SamplesBuffer::new(2, 1, vec![10i16, -10, 10, -10, 20, -20]);
let cnt = RefCell::new(0);
let mut source = inner.periodic_access(Duration::from_millis(1000), |_src| {
*cnt.borrow_mut() += 1;
});
assert_eq!(*cnt.borrow(), 0);
// Always called on first access!
assert_eq!(source.next(), Some(10)); assert_eq!(*cnt.borrow(), 1);
// Called every 1 second afterwards
assert_eq!(source.next(), Some(-10)); assert_eq!(*cnt.borrow(), 1);
assert_eq!(source.next(), Some(10)); assert_eq!(*cnt.borrow(), 2);
assert_eq!(source.next(), Some(-10)); assert_eq!(*cnt.borrow(), 2);
assert_eq!(source.next(), Some(20)); assert_eq!(*cnt.borrow(), 3);
assert_eq!(source.next(), Some(-20)); assert_eq!(*cnt.borrow(), 3);
}
}