hydro2_basic_operators/
double_out_op.rs

1// ---------------- [ File: src/double_out_op.rs ]
2crate::ix!();
3
4// --------------------------------------
5// DoubleOutOp
6// --------------------------------------
7#[derive(NamedItem, Debug, Operator)]
8#[operator(
9    execute="double_out", 
10    opcode="BasicOpCode::DoubleOutOp", 
11    input0="i32", 
12    output0="i32", 
13    output1="i32"
14)]
15pub struct DoubleOutOp {
16    name: String,
17}
18
19impl Default for DoubleOutOp {
20    fn default() -> Self {
21        Self {
22            name: format!("DoubleOutOp.default"),
23        }
24    }
25}
26
27impl DoubleOutOp {
28    async fn double_out(&self, input0: &i32) -> NetResult<(i32, i32)> {
29        let val = *input0;
30        let out0 = val;
31        let out1 = val + 100;
32        info!("DoubleOutOp => in={}, out0={}, out1={}", val, out0, out1);
33        Ok((out0, out1))
34    }
35}
36
37#[cfg(test)]
38mod double_out_op_tests {
39    use super::*;
40
41    #[tokio::test]
42    async fn test_double_out_simple() -> Result<(), NetworkError> {
43        let op = DoubleOutOp { name: "DoubleOut".to_string() };
44        let i0 = DoubleOutOpIO::Input0(5);
45        let input_arr = [Some(&i0), None, None, None];
46        let mut out_arr = [None, None, None, None];
47
48        op.execute(input_arr, &mut out_arr).await?;
49        assert_eq!(out_arr[0], Some(DoubleOutOpIO::Output0(5)));
50        assert_eq!(out_arr[1], Some(DoubleOutOpIO::Output1(105)));
51        Ok(())
52    }
53
54    #[tokio::test]
55    async fn test_double_out_negative() -> Result<(), NetworkError> {
56        let op = DoubleOutOp { name: "DoubleOut".to_string() };
57        let input = [Some(&DoubleOutOpIO::Input0(-10)), None, None, None];
58        let mut out = [None,None,None,None];
59        op.execute(input, &mut out).await?;
60        assert_eq!(out[0], Some(DoubleOutOpIO::Output0(-10)));
61        assert_eq!(out[1], Some(DoubleOutOpIO::Output1(90)));
62        Ok(())
63    }
64
65    #[tokio::test]
66    async fn test_double_out_multiple_calls() -> Result<(), NetworkError> {
67        let op = DoubleOutOp { name: "DoubleOut".to_string() };
68        for &val in &[0, 1, 100] {
69            let i0 = DoubleOutOpIO::Input0(val);
70            let input = [Some(&i0), None, None, None];
71            let mut out = [None,None,None,None];
72            op.execute(input, &mut out).await?;
73            assert_eq!(out[0], Some(DoubleOutOpIO::Output0(val)));
74            assert_eq!(out[1], Some(DoubleOutOpIO::Output1(val+100)));
75        }
76        Ok(())
77    }
78}