logic_mesh/blocks/bitwise/
bitwise_xor.rs

1// Copyright (c) 2022-2024, 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::utils::execute_impl;
16
17/// Outputs bitwise XOR operation.
18#[block]
19#[derive(BlockProps, Debug)]
20#[category = "bitwise"]
21pub struct BitwiseXor {
22    #[input(kind = "Number")]
23    pub in1: InputImpl,
24    #[input(kind = "Number")]
25    pub in2: InputImpl,
26    #[output(kind = "Bool")]
27    pub out: OutputImpl,
28}
29
30impl Block for BitwiseXor {
31    async fn execute(&mut self) {
32        execute_impl(self, |in1, in2| in1 ^ in2).await;
33    }
34}
35
36#[cfg(test)]
37mod test {
38    use crate::{
39        base::block::Block,
40        base::{block::test_utils::write_block_inputs, input::input_reader::InputReader},
41        blocks::bitwise::BitwiseXor,
42    };
43
44    #[tokio::test]
45    async fn test_and_op() {
46        let mut block = BitwiseXor::new();
47
48        for _ in
49            write_block_inputs(&mut [(&mut block.in1, 2.into()), (&mut block.in2, 2.into())]).await
50        {
51            block.read_inputs().await;
52        }
53
54        block.execute().await;
55
56        assert_eq!(block.out.value, (0).into());
57
58        for _ in
59            write_block_inputs(&mut [(&mut block.in1, 4.into()), (&mut block.in2, 2.into())]).await
60        {
61            block.read_inputs().await;
62        }
63
64        block.execute().await;
65
66        assert_eq!(block.out.value, (6).into());
67    }
68}