Skip to content

Commit 481eed9

Browse files
committed
adds integration tests for repeat
1 parent 69a840a commit 481eed9

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

src/source/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub use self::noise::{pink, white, PinkNoise, WhiteNoise};
159159
pub trait Source: Iterator<Item = Sample> {
160160
/// Whether the value of `channels()` and/or `sample_rate()` have changed.
161161
/// This is true before the next call to `Source::next`. After that
162-
/// `Source::next` call this will be false again.
162+
/// `Source::next` call this will be false again.
163163
///
164164
/// The value before the first call to next is not defined.
165165
fn parameters_changed(&self) -> bool;
@@ -203,7 +203,7 @@ pub trait Source: Iterator<Item = Sample> {
203203
where
204204
Self: Sized,
205205
{
206-
repeat::repeat(self)
206+
Repeat::new(self)
207207
}
208208

209209
/// Takes a certain duration of this source and then stops.

src/source/repeat.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ use super::SeekError;
77
use crate::common::{ChannelCount, SampleRate};
88
use crate::Source;
99

10-
/// Internal function that builds a `Repeat` object.
11-
pub fn repeat<I>(input: I) -> Repeat<I>
12-
where
13-
I: Source,
14-
{
15-
let input = input.buffered();
16-
Repeat {
17-
inner: PeekableSource::new(input.clone()),
18-
next: input,
19-
}
20-
}
21-
2210
/// A source that repeats the given source.
2311
pub struct Repeat<I>
2412
where
@@ -28,10 +16,17 @@ where
2816
next: Buffered<I>,
2917
}
3018

31-
impl<I> Iterator for Repeat<I>
32-
where
33-
I: Source,
34-
{
19+
impl<I: Source> Repeat<I> {
20+
pub(crate) fn new(input: I) -> Repeat<I> {
21+
let input = input.buffered();
22+
Repeat {
23+
inner: PeekableSource::new(input.clone()),
24+
next: input,
25+
}
26+
}
27+
}
28+
29+
impl<I: Source> Iterator for Repeat<I> {
3530
type Item = <I as Iterator>::Item;
3631

3732
#[inline]

tests/repeat.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use rodio::Source;
2+
use test_support::{TestSource, TestSpan};
3+
4+
mod test_support;
5+
6+
#[test]
7+
fn frame_boundray_at_start_of_repeat() {
8+
let source = TestSource::new()
9+
.with_span(TestSpan::from_samples((0..10).map(|n| n as f32)).with_sample_count(10))
10+
.with_span(TestSpan::from_samples((10..20).map(|n| n as f32)).with_sample_count(10));
11+
12+
let mut repeating = source.clone().repeat_infinite();
13+
repeating.by_ref().take(source.len()).count();
14+
assert!(repeating.parameters_changed());
15+
16+
assert!(repeating.next().is_some());
17+
assert!(!repeating.parameters_changed());
18+
}
19+
20+
#[test]
21+
fn parameters_identical_on_second_run() {
22+
let source = TestSource::new()
23+
.with_span(TestSpan::from_samples((0..10).map(|n| n as f32)).with_sample_count(10))
24+
.with_span(TestSpan::from_samples((10..20).map(|n| n as f32)).with_sample_count(10));
25+
26+
let mut repeating = source.clone().repeat_infinite();
27+
28+
let mut first_run_params = Vec::new();
29+
let mut second_run_params = Vec::new();
30+
31+
for params in [&mut first_run_params, &mut second_run_params] {
32+
for _ in 0..source.len() {
33+
assert!(repeating.by_ref().next().is_some());
34+
params.push((
35+
repeating.parameters_changed(),
36+
repeating.channels(),
37+
repeating.sample_rate(),
38+
));
39+
}
40+
}
41+
42+
assert_eq!(first_run_params, second_run_params);
43+
}
44+
45+
#[test]
46+
fn same_samples_on_second_run() {
47+
let source = TestSource::new()
48+
.with_span(TestSpan::from_samples((0..10).map(|n| n as f32)).with_sample_count(10))
49+
.with_span(TestSpan::from_samples((10..20).map(|n| n as f32)).with_sample_count(10));
50+
51+
let mut repeating = source.clone().repeat_infinite();
52+
let first_run: Vec<_> = repeating.by_ref().take(source.len()).collect();
53+
let second_run: Vec<_> = repeating.take(source.len()).collect();
54+
55+
assert_eq!(first_run, second_run);
56+
}

0 commit comments

Comments
 (0)