Skip to content

Commit 4c2e9ef

Browse files
committed
Reorganize tests in src/context
1 parent 2ef80c5 commit 4c2e9ef

File tree

3 files changed

+72
-56
lines changed

3 files changed

+72
-56
lines changed

src/context/base.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,59 @@ pub trait BaseAudioContext {
339339
}
340340
}
341341
}
342+
343+
#[cfg(test)]
344+
mod tests {
345+
use super::*;
346+
use crate::context::OfflineAudioContext;
347+
348+
use float_eq::assert_float_eq;
349+
350+
#[test]
351+
fn test_decode_audio_data_sync() {
352+
let context = OfflineAudioContext::new(1, 1, 44100.);
353+
let file = std::fs::File::open("samples/sample.wav").unwrap();
354+
let audio_buffer = context.decode_audio_data_sync(file).unwrap();
355+
356+
assert_eq!(audio_buffer.sample_rate(), 44100.);
357+
assert_eq!(audio_buffer.length(), 142_187);
358+
assert_eq!(audio_buffer.number_of_channels(), 2);
359+
assert_float_eq!(audio_buffer.duration(), 3.224, abs_all <= 0.001);
360+
361+
let left_start = &audio_buffer.get_channel_data(0)[0..100];
362+
let right_start = &audio_buffer.get_channel_data(1)[0..100];
363+
// assert distinct two channel data
364+
assert!(left_start != right_start);
365+
}
366+
367+
// #[test]
368+
// disabled: symphonia cannot handle empty WAV-files
369+
#[allow(dead_code)]
370+
fn test_decode_audio_data_empty() {
371+
let context = OfflineAudioContext::new(1, 1, 44100.);
372+
let file = std::fs::File::open("samples/empty_2c.wav").unwrap();
373+
let audio_buffer = context.decode_audio_data_sync(file).unwrap();
374+
assert_eq!(audio_buffer.length(), 0);
375+
}
376+
377+
#[test]
378+
fn test_decode_audio_data_decoding_error() {
379+
let context = OfflineAudioContext::new(1, 1, 44100.);
380+
let file = std::fs::File::open("samples/corrupt.wav").unwrap();
381+
assert!(context.decode_audio_data_sync(file).is_err());
382+
}
383+
384+
#[test]
385+
fn test_create_buffer() {
386+
let number_of_channels = 3;
387+
let length = 2000;
388+
let sample_rate = 96_000.;
389+
390+
let context = OfflineAudioContext::new(1, 1, 44100.);
391+
let buffer = context.create_buffer(number_of_channels, length, sample_rate);
392+
393+
assert_eq!(buffer.number_of_channels(), 3);
394+
assert_eq!(buffer.length(), 2000);
395+
assert_float_eq!(buffer.sample_rate(), 96000., abs_all <= 0.);
396+
}
397+
}

src/context/mod.rs

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ mod tests {
146146
use super::*;
147147
use crate::node::AudioNode;
148148

149-
use float_eq::assert_float_eq;
150-
151149
fn require_send_sync_static<T: Send + Sync + 'static>(_: T) {}
152150

153151
#[test]
@@ -156,7 +154,7 @@ mod tests {
156154
let registration = context.mock_registration();
157155

158156
// we want to be able to ship AudioNodes to another thread, so the Registration should be
159-
// Send Sync and 'static
157+
// Send, Sync and 'static
160158
require_send_sync_static(registration);
161159
}
162160

@@ -167,62 +165,17 @@ mod tests {
167165
}
168166

169167
#[test]
170-
fn test_sample_rate_length() {
171-
let context = OfflineAudioContext::new(1, 48000, 96000.);
172-
assert_float_eq!(context.sample_rate(), 96000., abs_all <= 0.);
173-
assert_eq!(context.length(), 48000);
174-
}
175-
176-
#[test]
177-
fn test_decode_audio_data() {
178-
let context = OfflineAudioContext::new(1, 1, 44100.);
179-
let file = std::fs::File::open("samples/sample.wav").unwrap();
180-
let audio_buffer = context.decode_audio_data_sync(file).unwrap();
181-
182-
assert_eq!(audio_buffer.sample_rate(), 44100.);
183-
assert_eq!(audio_buffer.length(), 142_187);
184-
assert_eq!(audio_buffer.number_of_channels(), 2);
185-
assert_float_eq!(audio_buffer.duration(), 3.224, abs_all <= 0.001);
186-
187-
let left_start = &audio_buffer.get_channel_data(0)[0..100];
188-
let right_start = &audio_buffer.get_channel_data(1)[0..100];
189-
// assert distinct two channel data
190-
assert!(left_start != right_start);
191-
}
192-
193-
// #[test]
194-
// disabled: symphonia cannot handle empty WAV-files
195-
#[allow(dead_code)]
196-
fn test_decode_audio_data_empty() {
197-
let context = OfflineAudioContext::new(1, 1, 44100.);
198-
let file = std::fs::File::open("samples/empty_2c.wav").unwrap();
199-
let audio_buffer = context.decode_audio_data_sync(file).unwrap();
200-
assert_eq!(audio_buffer.length(), 0);
201-
}
202-
203-
#[test]
204-
fn test_decode_audio_data_decoding_error() {
205-
let context = OfflineAudioContext::new(1, 1, 44100.);
206-
let file = std::fs::File::open("samples/corrupt.wav").unwrap();
207-
assert!(context.decode_audio_data_sync(file).is_err());
208-
}
209-
210-
#[test]
211-
fn test_create_buffer() {
212-
let number_of_channels = 3;
213-
let length = 2000;
214-
let sample_rate = 96_000.;
215-
216-
let context = OfflineAudioContext::new(1, 1, 44100.);
217-
let buffer = context.create_buffer(number_of_channels, length, sample_rate);
218-
219-
assert_eq!(buffer.number_of_channels(), 3);
220-
assert_eq!(buffer.length(), 2000);
221-
assert_float_eq!(buffer.sample_rate(), 96000., abs_all <= 0.);
168+
fn test_online_audio_context_send_sync() {
169+
let options = AudioContextOptions {
170+
sink_id: "none".into(),
171+
..AudioContextOptions::default()
172+
};
173+
let context = AudioContext::new(options);
174+
require_send_sync_static(context);
222175
}
223176

224177
#[test]
225-
fn test_registration() {
178+
fn test_context_equals() {
226179
let context = OfflineAudioContext::new(1, 48000, 96000.);
227180
let dest = context.destination();
228181
assert!(dest.context() == context.base());

src/context/offline.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,13 @@ mod tests {
433433
use crate::node::AudioNode;
434434
use crate::node::AudioScheduledSourceNode;
435435

436+
#[test]
437+
fn test_sample_rate_length() {
438+
let context = OfflineAudioContext::new(1, 48000, 96000.);
439+
assert_float_eq!(context.sample_rate(), 96000., abs_all <= 0.);
440+
assert_eq!(context.length(), 48000);
441+
}
442+
436443
#[test]
437444
fn render_empty_graph() {
438445
let mut context = OfflineAudioContext::new(2, 555, 44_100.);

0 commit comments

Comments
 (0)