use super::create::{
create_cea2034_combined_traces, create_cea2034_traces, create_cea2034_with_eq_combined_traces,
create_cea2034_with_eq_traces,
};
use super::misc::shorten_curve_name;
use crate::ref_lines::make_ref_lines;
use ndarray::Array1;
use serde_json::json;
use serde_json::to_value as to_json;
use std::collections::HashMap;
#[test]
fn test_create_cea2034_traces() {
let mut curves = HashMap::new();
let freq = Array1::from(vec![20.0, 100.0, 1000.0, 10000.0, 20000.0]);
let spl = Array1::from(vec![80.0, 85.0, 90.0, 85.0, 80.0]);
curves.insert(
"On Axis".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Listening Window".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Early Reflections".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Sound Power".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl.clone(),
phase: None,
..Default::default()
},
);
let traces = create_cea2034_traces(&curves);
assert_eq!(traces.len(), 4);
let eq_response = Array1::from(vec![1.0, 1.0, 1.0, 1.0, 1.0]);
let eq_traces = create_cea2034_with_eq_traces(&curves, &eq_response);
assert_eq!(eq_traces.len(), 4);
}
#[test]
fn test_make_ref_lines_values() {
let lines = make_ref_lines("x3", "y3");
assert_eq!(lines.len(), 2);
let v0 = to_json(&lines[0]).unwrap();
let v1 = to_json(&lines[1]).unwrap();
assert_eq!(v0["x"], json!([100.0, 10000.0]));
assert_eq!(v1["x"], json!([100.0, 10000.0]));
assert_eq!(v0["y"], json!([1.0, 1.0]));
assert_eq!(v1["y"], json!([-1.0, -1.0]));
}
#[test]
fn test_create_cea2034_combined_traces_counts_and_axes() {
let mut curves = HashMap::new();
let freq = Array1::from(vec![100.0, 1000.0, 10000.0]);
let spl_primary = Array1::from(vec![80.0, 85.0, 82.0]);
let spl_di = Array1::from(vec![5.0, 6.0, 7.0]);
curves.insert(
"On Axis".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_primary.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Listening Window".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_primary.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Early Reflections".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_primary.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Sound Power".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_primary.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Early Reflections DI".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_di.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Sound Power DI".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_di.clone(),
phase: None,
..Default::default()
},
);
let traces = create_cea2034_combined_traces(&curves, "x7", "y7", "y7");
assert_eq!(traces.len(), 6);
let v = to_json(&traces).unwrap();
let names: Vec<String> = v
.as_array()
.unwrap()
.iter()
.map(|t| t["name"].as_str().unwrap().to_string())
.collect();
assert!(names.contains(&"ERDI".to_string()));
assert!(names.contains(&"SPDI".to_string()));
for t in v.as_array().unwrap() {
let n = t["name"].as_str().unwrap();
if n.ends_with(" DI") {
assert_eq!(t["yaxis"], json!("y7"));
}
}
}
#[test]
fn test_create_cea2034_with_eq_combined_traces_counts_and_names() {
let mut curves = HashMap::new();
let freq = Array1::from(vec![100.0, 1000.0, 10000.0]);
let spl_primary = Array1::from(vec![80.0, 85.0, 82.0]);
let spl_di = Array1::from(vec![5.0, 6.0, 7.0]);
curves.insert(
"On Axis".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_primary.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Listening Window".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_primary.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Early Reflections".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_primary.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Sound Power".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_primary.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Early Reflections DI".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_di.clone(),
phase: None,
..Default::default()
},
);
curves.insert(
"Sound Power DI".to_string(),
crate::Curve {
freq: freq.clone(),
spl: spl_di.clone(),
phase: None,
..Default::default()
},
);
let eq = Array1::from(vec![1.0, -1.0, 0.5]);
let traces = create_cea2034_with_eq_combined_traces(&curves, &eq, "x8", "y8", "y8");
assert_eq!(traces.len(), 6);
let v = to_json(&traces).unwrap();
let names: Vec<String> = v
.as_array()
.unwrap()
.iter()
.map(|t| t["name"].as_str().unwrap().to_string())
.collect();
assert!(names.iter().any(|n| n == "ON w/EQ"));
assert!(names.iter().any(|n| n == "LW w/EQ"));
assert!(names.iter().any(|n| n == "ER w/EQ"));
assert!(names.iter().any(|n| n == "SP w/EQ"));
assert!(names.iter().any(|n| n == "ERDI"));
assert!(names.iter().any(|n| n == "SPDI"));
for t in v.as_array().unwrap() {
let n = t["name"].as_str().unwrap();
if n.ends_with(" DI") {
assert_eq!(t["yaxis"], json!("y8"));
}
}
}
#[test]
fn test_shorten_curve_name() {
assert_eq!(shorten_curve_name("On Axis"), "ON");
assert_eq!(shorten_curve_name("Listening Window"), "LW");
assert_eq!(shorten_curve_name("Early Reflections"), "ER");
assert_eq!(shorten_curve_name("Sound Power"), "SP");
assert_eq!(shorten_curve_name("Estimated In-Room Response"), "PIR");
assert_eq!(shorten_curve_name("Early Reflections DI"), "ERDI");
assert_eq!(shorten_curve_name("Sound Power DI"), "SPDI");
assert_eq!(shorten_curve_name("Unknown Curve"), "Unknown Curve");
assert_eq!(shorten_curve_name(""), "");
assert_eq!(shorten_curve_name("on axis"), "on axis");
assert_eq!(shorten_curve_name("ON AXIS"), "ON AXIS");
}
#[test]
fn test_shorten_curve_name_edge_cases() {
let long_name = "A".repeat(200);
assert_eq!(shorten_curve_name(&long_name), long_name.as_str());
assert_eq!(shorten_curve_name(" "), " ");
assert_eq!(shorten_curve_name("On Axis @ 0°"), "On Axis @ 0°");
assert_eq!(shorten_curve_name("On Axis DI"), "On Axis DI");
assert_eq!(shorten_curve_name("Sound Power X"), "Sound Power X");
}