Skip to content
Snippets Groups Projects
Commit 1e92574b authored by Udo Eisenbarth's avatar Udo Eisenbarth :speech_balloon:
Browse files

Add unit test for IdealFIlter::analyze

Add name parameter to IdealFIlter::new
parent dc943620
No related branches found
No related tags found
1 merge request!28Resolve "IdealFilter: Add analysis unit test"
......@@ -26,7 +26,10 @@ fn main() -> Result<(), OpossumError> {
));
let i_bs = scenery.add_node(BeamSplitter::new(0.5).unwrap());
let filter_spectrum = Spectrum::from_csv("NE03B.csv")?;
let i_f = scenery.add_node(IdealFilter::new(FilterType::Spectrum(filter_spectrum))?);
let i_f = scenery.add_node(IdealFilter::new(
"filter",
FilterType::Spectrum(filter_spectrum),
)?);
let i_d1 = scenery.add_node(Detector::default()); // Detector 1
scenery.connect_nodes(i_s1, "out1", i_bs, "input1")?;
......
......@@ -20,7 +20,10 @@ fn main() -> Result<(), OpossumError> {
));
let i_bs = scenery.add_node(BeamSplitter::new(0.6).unwrap());
let filter_spectrum = Spectrum::from_csv("NE03B.csv")?;
let i_f = scenery.add_node(IdealFilter::new(FilterType::Spectrum(filter_spectrum))?);
let i_f = scenery.add_node(IdealFilter::new(
"filter",
FilterType::Spectrum(filter_spectrum),
)?);
let i_d1 = scenery.add_node(EnergyMeter::new(
"Energy meter 1",
opossum::nodes::Metertype::IdealEnergyMeter,
......
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
version="1.1"
id="svg870"
width="504.50769"
height="467.74179"
viewBox="0 0 504.50769 467.74179"
sodipodi:docname="opossum.svg"
inkscape:version="1.2.2 (732a01da63, 2022-12-09)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg">
<defs
id="defs874" />
<sodipodi:namedview
id="namedview872"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
showgrid="true"
fit-margin-top="0"
fit-margin-left="0"
fit-margin-right="0"
fit-margin-bottom="0"
inkscape:zoom="1.1734241"
inkscape:cx="243.30504"
inkscape:cy="281.22824"
inkscape:window-width="1680"
inkscape:window-height="997"
inkscape:window-x="-8"
inkscape:window-y="-8"
inkscape:window-maximized="1"
inkscape:current-layer="g876"
inkscape:showpageshadow="2"
inkscape:deskcolor="#d1d1d1">
<inkscape:grid
type="xygrid"
id="grid239"
originx="2.2538492"
originy="2.7415204" />
</sodipodi:namedview>
<g
inkscape:groupmode="layer"
inkscape:label="Image"
id="g876"
transform="translate(-131.90553,-144.21967)">
<path
style="fill:#000000"
d="m 167.51624,354.83902 c -5.94301,-9.07019 -19.08994,-40.97754 -25.29746,-61.39646 -7.81214,-25.69715 -10.94269,-46.70372 -10.20962,-68.50849 0.99679,-29.64929 7.44289,-47.50095 22.73154,-62.95218 8.4165,-8.506 19.50898,-14.83948 29.69259,-16.95359 7.87892,-1.63566 24.88078,-0.68931 34.72609,1.9329 21.69182,5.77743 47.0272,23.17135 63.81051,43.8089 7.22649,8.88604 22.86865,32.33854 22.24352,33.35002 -73.30788,14.90823 -118.40348,72.82551 -135.25856,132.7108 -0.51165,0.17055 -2.43861,-1.9919 -2.43861,-1.9919 z"
id="path568"
sodipodi:nodetypes="cssssssccc" />
<path
id="path564"
style="fill:#000000"
d="m 189.77852,502.18385 c -0.14894,-0.002 -0.25357,0.006 -0.31055,0.0254 -1.98509,0.66169 -1.35574,3.20964 1.04883,4.24609 1.2375,0.5334 10.57501,2.79258 20.75,5.01953 20.43475,4.47245 64.64204,15.72913 67,17.06055 3.38236,1.90983 7,0.24367 7,-3.22266 0,-1.93455 -1.38124,-2.40076 -26.10742,-8.83398 -17.14906,-4.46181 -64.76385,-14.24069 -69.38086,-14.29493 z m 7.79883,28.26758 c -9.91151,-0.009 -12.83381,0.86737 -10.54102,3.16016 0.54703,0.54703 12.41075,1.61801 26.36328,2.38086 13.95254,0.76285 30.3172,1.86607 36.36719,2.45117 13.58547,1.31388 48.84489,6.00894 49.41602,6.58008 0.95764,0.95764 9.24414,4.38085 9.24414,-0.61914 0,-5 -10.00078,-5.00113 -13.91016,-5.45117 -3.90938,-0.45004 -69.04433,-8.47716 -96.93945,-8.50196 z m -11.0918,28.05274 c -3.12447,0.0362 -3.21875,0.53121 -3.21875,1.82617 0,1.16135 0.58028,2.11328 1.28906,2.11328 0.70879,0 9.1463,0.64875 18.75,1.44336 20.6222,1.70628 67.25905,4.49672 81.46094,4.87305 5.49999,0.14574 11.37817,0.35754 13.06055,0.47265 3.80396,0.26027 6.77761,-1.79843 6.28906,-4.35547 -0.42704,-2.23509 -2.66216,-2.39228 -61.34961,-4.27929 -18.69998,-0.60128 -39.73751,-1.38076 -46.75,-1.73438 -4.69156,-0.23658 -7.65657,-0.38106 -9.53125,-0.35937 z" />
<path
style="fill:#000000"
d="m 384.15938,611.9612 c -12.6902,-0.043 -21.0961,-4.40156 -30,-10 -8.9039,-5.59844 -25,-20 -25,-25 15,5 25,15 35,5 5,-5 5,-5 -5,-10 -10,-5 -19.82509,-9.58142 -25,-20 -5.17491,-10.41859 -5,-20 0,-30 5,-10 25,-30 50,-30 25,0 45,20 50,30 5,10 5,19.99999 0,30 -5,10 -15,15 -25,20 -10,5 -10,5 -5,10 10,10 20,-10e-6 35,-5 0,5 -15,20 -25,25 -10,5 -17.3098,10.04302 -30,10 z"
id="path1035"
sodipodi:nodetypes="zzcczzzzzzzcczz" />
<circle
style="fill:#000000;stroke:none;stroke-width:4.7811"
id="path293"
cx="254.15938"
cy="396.96121"
r="35" />
<circle
style="fill:#000000;stroke:none;stroke-width:4.7811"
id="path293-5"
cx="504.15939"
cy="396.96121"
r="35" />
<path
style="fill:#000000"
d="m 600.80252,354.83902 c 5.94301,-9.07019 19.08994,-40.97754 25.29746,-61.39646 7.81214,-25.69715 10.94269,-46.70372 10.20962,-68.50849 -0.99679,-29.64929 -7.44289,-47.50095 -22.73154,-62.95218 -8.4165,-8.506 -19.50898,-14.83948 -29.69259,-16.95359 -7.87892,-1.63566 -24.88078,-0.68931 -34.72609,1.9329 -21.69182,5.77743 -47.0272,23.17135 -63.81051,43.8089 -7.22649,8.88604 -22.86865,32.33854 -22.24352,33.35002 73.30788,14.90823 118.40348,72.82551 135.25856,132.7108 0.51165,0.17055 2.43861,-1.9919 2.43861,-1.9919 z"
id="path568-9"
sodipodi:nodetypes="cssssssccc" />
<path
id="path564-0"
style="fill:#000000"
d="m 578.14766,502.18385 c 0.14894,-0.002 0.25357,0.006 0.31055,0.0254 1.98509,0.66169 1.35574,3.20964 -1.04883,4.24609 -1.2375,0.5334 -10.57501,2.79258 -20.75,5.01953 -20.43475,4.47245 -64.64204,15.72913 -67,17.06055 -3.38236,1.90983 -7,0.24367 -7,-3.22266 0,-1.93455 1.38124,-2.40076 26.10742,-8.83398 17.14906,-4.46181 64.76385,-14.24069 69.38086,-14.29493 z m -7.79883,28.26758 c 9.91151,-0.009 12.83381,0.86737 10.54102,3.16016 -0.54703,0.54703 -12.41075,1.61801 -26.36328,2.38086 -13.95254,0.76285 -30.3172,1.86607 -36.36719,2.45117 -13.58547,1.31388 -48.84489,6.00894 -49.41602,6.58008 -0.95764,0.95764 -9.24414,4.38085 -9.24414,-0.61914 0,-5 10.00078,-5.00113 13.91016,-5.45117 3.90938,-0.45004 69.04433,-8.47716 96.93945,-8.50196 z m 11.0918,28.05274 c 3.12447,0.0362 3.21875,0.53121 3.21875,1.82617 0,1.16135 -0.58028,2.11328 -1.28906,2.11328 -0.70879,0 -9.1463,0.64875 -18.75,1.44336 -20.6222,1.70628 -67.25905,4.49672 -81.46094,4.87305 -5.49999,0.14574 -11.37817,0.35754 -13.06055,0.47265 -3.80396,0.26027 -6.77761,-1.79843 -6.28906,-4.35547 0.42704,-2.23509 2.66216,-2.39228 61.34961,-4.27929 18.69998,-0.60128 39.73751,-1.38076 46.75,-1.73438 4.69156,-0.23658 7.65657,-0.38106 9.53125,-0.35937 z" />
</g>
</svg>
......@@ -43,8 +43,8 @@ fn create_default_props() -> Properties {
props.set("filter type", FilterType::Constant(1.0).into());
props
}
impl Default for IdealFilter {
/// Create an ideal filter node with a transmission of 100%.
fn default() -> Self {
Self {
props: create_default_props(),
......@@ -58,7 +58,7 @@ impl IdealFilter {
///
/// This function will return an [`OpossumError::Other`] if the filter type is
/// [`FilterType::Constant`] and the transmission factor is outside the interval [0.0; 1.0].
pub fn new(filter_type: FilterType) -> OpmResult<Self> {
pub fn new(name: &str, filter_type: FilterType) -> OpmResult<Self> {
if let FilterType::Constant(transmission) = filter_type {
if !(0.0..=1.0).contains(&transmission) {
return Err(OpossumError::Other(
......@@ -68,6 +68,7 @@ impl IdealFilter {
}
let mut props = create_default_props();
props.set("filter type", filter_type.into());
props.set("name", name.into());
Ok(Self { props })
}
/// Returns the filter type of this [`IdealFilter`].
......@@ -206,6 +207,8 @@ impl Dottable for IdealFilter {
}
#[cfg(test)]
mod test {
use crate::spectrum::create_he_ne_spectrum;
use super::*;
#[test]
fn default() {
......@@ -220,7 +223,8 @@ mod test {
}
#[test]
fn new() {
let node = IdealFilter::new(FilterType::Constant(0.8)).unwrap();
let node = IdealFilter::new("test", FilterType::Constant(0.8)).unwrap();
assert_eq!(node.name(), "test");
assert_eq!(node.filter_type(), FilterType::Constant(0.8));
}
#[test]
......@@ -235,4 +239,58 @@ mod test {
assert_eq!(node.ports().inputs(), vec!["front"]);
assert_eq!(node.ports().outputs(), vec!["rear"]);
}
#[test]
fn analyze_ok() {
let mut node = IdealFilter::new("test", FilterType::Constant(0.5)).unwrap();
let mut input = LightResult::default();
let input_light = LightData::Energy(DataEnergy {
spectrum: create_he_ne_spectrum(1.0),
});
input.insert("front".into(), Some(input_light.clone()));
let output = node.analyze(input, &AnalyzerType::Energy);
assert!(output.is_ok());
let output = output.unwrap();
assert!(output.contains_key("rear".into()));
assert_eq!(output.len(), 1);
let output = output.get("rear".into()).unwrap();
assert!(output.is_some());
let output = output.clone().unwrap();
let expected_output_light = LightData::Energy(DataEnergy {
spectrum: create_he_ne_spectrum(0.5),
});
assert_eq!(output, expected_output_light);
}
#[test]
fn analyze_wrong() {
let mut node = IdealFilter::default();
let mut input = LightResult::default();
let input_light = LightData::Energy(DataEnergy {
spectrum: create_he_ne_spectrum(1.0),
});
input.insert("rear".into(), Some(input_light.clone()));
let output = node.analyze(input, &AnalyzerType::Energy);
assert!(output.is_err());
}
#[test]
fn analyze_inverse() {
let mut node = IdealFilter::new("test", FilterType::Constant(0.5)).unwrap();
node.set_property("inverted", true.into()).unwrap();
let mut input = LightResult::default();
let input_light = LightData::Energy(DataEnergy {
spectrum: create_he_ne_spectrum(1.0),
});
input.insert("rear".into(), Some(input_light.clone()));
let output = node.analyze(input, &AnalyzerType::Energy);
assert!(output.is_ok());
let output = output.unwrap();
assert!(output.contains_key("front".into()));
assert_eq!(output.len(), 1);
let output = output.get("front".into()).unwrap();
assert!(output.is_some());
let output = output.clone().unwrap();
let expected_output_light = LightData::Energy(DataEnergy {
spectrum: create_he_ne_spectrum(0.5),
});
assert_eq!(output, expected_output_light);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment