hydro2_basic_operators/
quad_to_quad_op.rs

1// ---------------- [ File: src/quad_to_quad_op.rs ]
2crate::ix!();
3
4// --------------------------------------
5// QuadToQuadOp
6// --------------------------------------
7#[derive(NamedItem, Operator, Debug)]
8#[operator(
9    execute="quad_math",
10    opcode="BasicOpCode::QuadToQuadOp",
11    input0="i32",
12    input1="i32",
13    input2="i32",
14    input3="i32",
15    output0="i32",
16    output1="i32",
17    output2="i32",
18    output3="i32"
19)]
20pub struct QuadToQuadOp {
21    name: String,
22}
23
24impl QuadToQuadOp {
25    pub fn new() -> Self {
26        Self { name: "QuadToQuadOp".into() }
27    }
28
29    async fn quad_math(&self, a: &i32, b: &i32, c: &i32, d: &i32) -> NetResult<(i32,i32,i32,i32)> {
30        let sum_all = *a + *b + *c + *d;
31        let sum_ab  = *a + *b;
32        let sum_cd  = *c + *d;
33        let product = *a * *b * *c * *d;
34        info!("QuadToQuadOp => a={}, b={}, c={}, d={} => sums/products", a,b,c,d);
35        Ok((sum_all, sum_ab, sum_cd, product))
36    }
37}
38
39#[cfg(test)]
40mod quad_to_quad_op_tests {
41    use super::*;
42
43    #[tokio::test]
44    async fn test_quad_to_quad_op_basic() -> Result<(), NetworkError> {
45        let op = QuadToQuadOp::new();
46        let i0 = QuadToQuadOpIO::Input0(1);
47        let i1 = QuadToQuadOpIO::Input1(2);
48        let i2 = QuadToQuadOpIO::Input2(3);
49        let i3 = QuadToQuadOpIO::Input3(4);
50
51        let input = [Some(&i0), Some(&i1), Some(&i2), Some(&i3)];
52        let mut out = [None,None,None,None];
53        op.execute(input, &mut out).await?;
54        // sum_all=10, sum_ab=3, sum_cd=7, product=24
55        assert_eq!(out[0], Some(QuadToQuadOpIO::Output0(10)));
56        assert_eq!(out[1], Some(QuadToQuadOpIO::Output1(3)));
57        assert_eq!(out[2], Some(QuadToQuadOpIO::Output2(7)));
58        assert_eq!(out[3], Some(QuadToQuadOpIO::Output3(24)));
59        Ok(())
60    }
61}