logic_mesh/blocks/logic/
lt.rs

1// Copyright (c) 2022-2023, Radu Racariu.
2
3use uuid::Uuid;
4
5use crate::base::{
6    block::{Block, BlockDesc, BlockProps, BlockState},
7    input::{Input, InputProps},
8    output::Output,
9};
10
11use libhaystack::val::kind::HaystackKind;
12
13use crate::{blocks::InputImpl, blocks::OutputImpl};
14
15use super::util::execute_impl;
16
17/// Outputs true if value of the in1 is less.
18#[block]
19#[derive(BlockProps, Debug)]
20#[dis = "LessThan"]
21#[category = "logic"]
22pub struct LessThan {
23    #[input(name = "in1", kind = "Null")]
24    pub input1: InputImpl,
25    #[input(name = "in2", kind = "Null")]
26    pub input2: InputImpl,
27    #[output(kind = "Bool")]
28    pub out: OutputImpl,
29}
30
31impl Block for LessThan {
32    async fn execute(&mut self) {
33        execute_impl(self, |in1, in2| in1 < in2).await;
34    }
35}
36
37#[cfg(test)]
38mod test {
39
40    use crate::{
41        base::block::test_utils::write_block_inputs,
42        base::{block::Block, input::input_reader::InputReader},
43        blocks::logic::LessThan,
44    };
45
46    #[tokio::test]
47    async fn test_lt_block() {
48        let mut block = LessThan::new();
49
50        for _ in write_block_inputs(&mut [
51            (&mut block.input1, 3.into()),
52            (&mut block.input2, 41.into()),
53        ])
54        .await
55        {
56            block.read_inputs().await;
57        }
58
59        block.execute().await;
60        assert_eq!(block.out.value, true.into());
61    }
62}