Add some tests to Sink

This commit is contained in:
Will Stott 2019-04-02 20:42:09 +00:00
parent 074368480e
commit 941fec3fe5

View file

@ -33,13 +33,20 @@ struct Controls {
} }
impl Sink { impl Sink {
/// Builds a new `Sink`. /// Builds a new `Sink`, beginning playback on a Device.
#[inline] #[inline]
pub fn new(device: &Device) -> Sink { pub fn new(device: &Device) -> Sink {
let (queue_tx, queue_rx) = queue::queue(true); let (sink, queue_rx) = Sink::new_idle();
play_raw(device, queue_rx); play_raw(device, queue_rx);
sink
}
Sink { /// Builds a new `Sink`.
#[inline]
pub fn new_idle() -> (Sink, queue::SourcesQueueOutput<f32>) {
let (queue_tx, queue_rx) = queue::queue(true);
let sink = Sink {
queue_tx: queue_tx, queue_tx: queue_tx,
sleep_until_end: Mutex::new(None), sleep_until_end: Mutex::new(None),
controls: Arc::new(Controls { controls: Arc::new(Controls {
@ -49,7 +56,8 @@ impl Sink {
}), }),
sound_count: Arc::new(AtomicUsize::new(0)), sound_count: Arc::new(AtomicUsize::new(0)),
detached: false, detached: false,
} };
(sink, queue_rx)
} }
/// Appends a sound to the queue of sounds to play. /// Appends a sound to the queue of sounds to play.
@ -168,3 +176,60 @@ impl Drop for Sink {
} }
} }
} }
#[cfg(test)]
mod tests {
use buffer::SamplesBuffer;
use source::Source;
use sink::Sink;
#[test]
fn test_pause_and_stop() {
let (sink, mut queue_rx) = Sink::new_idle();
// assert_eq!(queue_rx.next(), Some(0.0));
let v = vec![10i16, -10, 20, -20, 30, -30];
// Low rate to ensure immediate control.
sink.append(SamplesBuffer::new(1, 1, v.clone()));
let mut src = SamplesBuffer::new(1, 1, v.clone()).convert_samples();
assert_eq!(queue_rx.next(), src.next());
assert_eq!(queue_rx.next(), src.next());
sink.pause();
assert_eq!(queue_rx.next(), Some(0.0));
sink.play();
assert_eq!(queue_rx.next(), src.next());
assert_eq!(queue_rx.next(), src.next());
sink.stop();
assert_eq!(queue_rx.next(), Some(0.0));
assert_eq!(sink.empty(), true);
}
#[test]
fn test_volume() {
let (sink, mut queue_rx) = Sink::new_idle();
let v = vec![10i16, -10, 20, -20, 30, -30];
// High rate to avoid immediate control.
sink.append(SamplesBuffer::new(2, 44100, v.clone()));
let src = SamplesBuffer::new(2, 44100, v.clone()).convert_samples();
let mut src = src.amplify(0.5);
sink.set_volume(0.5);
for _ in 0..v.len() {
assert_eq!(queue_rx.next(), src.next());
}
}
}