prettify code

This commit is contained in:
Wuelle
2021-12-30 21:59:29 +01:00
parent 5e563cddf8
commit 9a010ca0c4
17 changed files with 1312 additions and 1060 deletions

View File

@@ -1,7 +1,6 @@
extern crate serde_json;
use core::fmt::Debug;
use serde_json::{Value, from_str};
use serde_json::{from_str, Value};
use std::fs::File;
use std::io::Read;
@@ -13,43 +12,42 @@ use bwavfile::WaveReader;
// as read by `WaveReader`.
// This is rickety but we're going with it
fn assert_match_stream<T>(stream_key: &str,
other: impl Fn(&mut WaveReader<File>) -> T)
where T: PartialEq + Debug,
T: Into<Value>
{
fn assert_match_stream<T>(stream_key: &str, other: impl Fn(&mut WaveReader<File>) -> T)
where
T: PartialEq + Debug,
T: Into<Value>,
{
let mut json_file = File::open("tests/ffprobe_media_tests.json").unwrap();
let mut s = String::new();
json_file.read_to_string(&mut s).unwrap();
if let Value::Array(v) = from_str(&mut s).unwrap() { /* */
if let Value::Array(v) = from_str(&mut s).unwrap() {
/* */
v.iter()
.filter(|value| {
!value["format"]["filename"].is_null()
})
.filter(|value| !value["format"]["filename"].is_null())
.for_each(|value| {
let filen : &str = value["format"]["filename"].as_str().unwrap();
let json_value : &Value = &value["streams"][0][stream_key];
let filen: &str = value["format"]["filename"].as_str().unwrap();
let json_value: &Value = &value["streams"][0][stream_key];
let mut wavfile = WaveReader::open_unbuffered(filen).unwrap();
let wavfile_value: T = other(&mut wavfile);
println!("asserting {} for {}",stream_key, filen);
println!("asserting {} for {}", stream_key, filen);
assert_eq!(Into::<Value>::into(wavfile_value), *json_value);
})
}
}
}
#[test]
fn test_frame_count() {
assert_match_stream("duration_ts", |w| w.frame_length().unwrap() );
fn test_frame_count() {
assert_match_stream("duration_ts", |w| w.frame_length().unwrap());
}
#[test]
fn test_sample_rate() {
assert_match_stream("sample_rate", |w| format!("{}", w.format().unwrap().sample_rate) );
assert_match_stream("sample_rate", |w| {
format!("{}", w.format().unwrap().sample_rate)
});
}
#[test]
fn test_channel_count() {
assert_match_stream("channels", |w| w.format().unwrap().channel_count );
assert_match_stream("channels", |w| w.format().unwrap().channel_count);
}

View File

@@ -1,17 +1,15 @@
extern crate bwavfile;
use bwavfile::WaveReader;
use bwavfile::ChannelMask;
use bwavfile::Error;
use bwavfile::{ ChannelMask};
use bwavfile::WaveReader;
#[test]
fn test_open() {
let path = "tests/media/ff_silence.wav";
match WaveReader::open(path) {
Ok(_) => {
()
},
Ok(_) => (),
Err(x) => {
assert!(false, "Opened error.wav with unexpected error {:?}", x)
}
@@ -19,7 +17,7 @@ fn test_open() {
}
#[test]
fn test_format_silence() -> Result<(),Error> {
fn test_format_silence() -> Result<(), Error> {
let path = "tests/media/ff_silence.wav";
let mut w = WaveReader::open(path)?;
@@ -29,7 +27,7 @@ fn test_format_silence() -> Result<(),Error> {
assert_eq!(format.sample_rate, 44100);
assert_eq!(format.channel_count, 1);
assert_eq!(format.tag as u16, 1);
Ok( () )
Ok(())
}
#[test]
@@ -44,18 +42,18 @@ fn test_format_error() {
}
#[test]
fn test_frame_count() -> Result<(),Error> {
fn test_frame_count() -> Result<(), Error> {
let path = "tests/media/ff_silence.wav";
let mut w = WaveReader::open(path)?;
let l = w.frame_length()?;
assert_eq!(l, 44100);
Ok( () )
Ok(())
}
#[test]
fn test_minimal_wave() {
fn test_minimal_wave() {
let path = "tests/media/ff_silence.wav";
let mut w = WaveReader::open(path).expect("Failure opening file");
@@ -86,14 +84,12 @@ fn test_read() {
let mut reader = w.audio_frame_reader().unwrap();
assert_eq!(reader.read_integer_frame(&mut buffer).unwrap(), 1);
assert_eq!(buffer[0], -2823_i32);
assert_eq!(reader.read_integer_frame(&mut buffer).unwrap(), 1);
assert_eq!(buffer[0], 2012_i32);
assert_eq!(reader.read_integer_frame(&mut buffer).unwrap(), 1);
assert_eq!(buffer[0], 4524_i32);
assert_eq!(buffer[0], 4524_i32);
}
#[test]
@@ -126,10 +122,10 @@ fn test_channels_stereo() {
let channels = w.channels().unwrap();
assert_eq!(channels.len(), 2);
assert_eq!(channels[0].index,0);
assert_eq!(channels[1].index,1);
assert_eq!(channels[0].speaker,ChannelMask::FrontLeft);
assert_eq!(channels[1].speaker,ChannelMask::FrontRight);
assert_eq!(channels[0].index, 0);
assert_eq!(channels[1].index, 1);
assert_eq!(channels[0].speaker, ChannelMask::FrontLeft);
assert_eq!(channels[1].speaker, ChannelMask::FrontRight);
}
#[test]
@@ -140,8 +136,8 @@ fn test_channels_mono_no_extended() {
let channels = w.channels().unwrap();
assert_eq!(channels.len(), 1);
assert_eq!(channels[0].index,0);
assert_eq!(channels[0].speaker,ChannelMask::FrontCenter);
assert_eq!(channels[0].index, 0);
assert_eq!(channels[0].speaker, ChannelMask::FrontCenter);
}
#[test]
@@ -152,19 +148,21 @@ fn test_channels_stereo_no_fmt_extended() {
let channels = w.channels().unwrap();
assert_eq!(channels.len(), 2);
assert_eq!(channels[0].index,0);
assert_eq!(channels[1].index,1);
assert_eq!(channels[0].speaker,ChannelMask::FrontLeft);
assert_eq!(channels[1].speaker,ChannelMask::FrontRight);
assert_eq!(channels[0].index, 0);
assert_eq!(channels[1].index, 1);
assert_eq!(channels[0].speaker, ChannelMask::FrontLeft);
assert_eq!(channels[1].speaker, ChannelMask::FrontRight);
}
///See issue 6 and 7
#[test]
fn test_frame_reader_consumes_reader() {
// Issue #6
use bwavfile::{WaveFmt, AudioFrameReader};
use bwavfile::{AudioFrameReader, WaveFmt};
use std::fs::File;
fn from_wav_filename(wav_filename: &str) -> Result<(WaveFmt, AudioFrameReader<std::io::BufReader<File>>), ()> {
fn from_wav_filename(
wav_filename: &str,
) -> Result<(WaveFmt, AudioFrameReader<std::io::BufReader<File>>), ()> {
if let Ok(mut r) = WaveReader::open(&wav_filename) {
let format = r.format().unwrap();
let frame_reader = r.audio_frame_reader().unwrap();
@@ -189,34 +187,34 @@ fn test_cue_read_sounddevices() {
assert_eq!(cue_points[0].label, None);
assert_eq!(cue_points[0].note, None);
assert_eq!(cue_points[0].offset, 90112);
assert_eq!(cue_points[1].frame, 0);
assert_eq!(cue_points[1].length, None);
assert_eq!(cue_points[1].label, None);
assert_eq!(cue_points[1].note, None);
assert_eq!(cue_points[1].note, None);
assert_eq!(cue_points[1].offset, 176128);
assert_eq!(cue_points[2].frame, 0);
assert_eq!(cue_points[2].length, None);
assert_eq!(cue_points[2].label, None);
assert_eq!(cue_points[2].note, None);
assert_eq!(cue_points[2].note, None);
assert_eq!(cue_points[2].offset, 237568);
assert_eq!(cue_points[3].frame, 0);
assert_eq!(cue_points[3].length, None);
assert_eq!(cue_points[3].label, None);
assert_eq!(cue_points[3].note, None);
assert_eq!(cue_points[3].note, None);
assert_eq!(cue_points[3].offset, 294912);
assert_eq!(cue_points[4].frame, 0);
assert_eq!(cue_points[4].length, None);
assert_eq!(cue_points[4].label, None);
assert_eq!(cue_points[4].note, None);
assert_eq!(cue_points[4].note, None);
assert_eq!(cue_points[4].offset, 380928);
assert_eq!(cue_points[5].frame, 0);
assert_eq!(cue_points[5].length, None);
assert_eq!(cue_points[5].label, None);
assert_eq!(cue_points[5].note, None);
assert_eq!(cue_points[5].note, None);
assert_eq!(cue_points[5].offset, 385024);
}
}