From a2e15c6f559a111042c9bf7bd04c310a17df91c7 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Mon, 29 Jun 2020 12:17:42 +0200 Subject: [PATCH] Add test for cmp_default_heuristics --- src/lib.rs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 1c3e2c8..2614b20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -590,6 +590,70 @@ impl SupportedStreamConfigRange { } } +#[test] +fn test_cmp_default_heuristics() { + let mut formats = vec![ + SupportedStreamConfigRange { + channels: 2, + min_sample_rate: SampleRate(1), + max_sample_rate: SampleRate(96000), + sample_format: SampleFormat::F32, + }, + SupportedStreamConfigRange { + channels: 1, + min_sample_rate: SampleRate(1), + max_sample_rate: SampleRate(96000), + sample_format: SampleFormat::F32, + }, + SupportedStreamConfigRange { + channels: 2, + min_sample_rate: SampleRate(1), + max_sample_rate: SampleRate(96000), + sample_format: SampleFormat::I16, + }, + SupportedStreamConfigRange { + channels: 2, + min_sample_rate: SampleRate(1), + max_sample_rate: SampleRate(96000), + sample_format: SampleFormat::U16, + }, + SupportedStreamConfigRange { + channels: 2, + min_sample_rate: SampleRate(1), + max_sample_rate: SampleRate(22050), + sample_format: SampleFormat::F32, + }, + ]; + + formats.sort_by(|a, b| a.cmp_default_heuristics(b)); + + // lowest-priority first: + assert_eq!(formats[0].sample_format(), SampleFormat::F32); + assert_eq!(formats[0].min_sample_rate(), SampleRate(1)); + assert_eq!(formats[0].max_sample_rate(), SampleRate(96000)); + assert_eq!(formats[0].channels(), 1); + + assert_eq!(formats[1].sample_format(), SampleFormat::U16); + assert_eq!(formats[1].min_sample_rate(), SampleRate(1)); + assert_eq!(formats[1].max_sample_rate(), SampleRate(96000)); + assert_eq!(formats[1].channels(), 2); + + assert_eq!(formats[2].sample_format(), SampleFormat::I16); + assert_eq!(formats[2].min_sample_rate(), SampleRate(1)); + assert_eq!(formats[2].max_sample_rate(), SampleRate(96000)); + assert_eq!(formats[2].channels(), 2); + + assert_eq!(formats[3].sample_format(), SampleFormat::F32); + assert_eq!(formats[3].min_sample_rate(), SampleRate(1)); + assert_eq!(formats[3].max_sample_rate(), SampleRate(22050)); + assert_eq!(formats[3].channels(), 2); + + assert_eq!(formats[4].sample_format(), SampleFormat::F32); + assert_eq!(formats[4].min_sample_rate(), SampleRate(1)); + assert_eq!(formats[4].max_sample_rate(), SampleRate(96000)); + assert_eq!(formats[4].channels(), 2); +} + impl From for StreamConfig { fn from(conf: SupportedStreamConfig) -> Self { conf.config()