hydro2_mock/
mock_network.rs

1// ---------------- [ File: src/mock_network.rs ]
2crate::ix!();
3
4pub fn single_noop_operator_i32_network() -> Arc<AsyncMutex<Network::<TestWireIO<i32>>>> 
5{
6
7    // Build a "fake" network with 1 node if `node_idx`=0
8    // Or build `node_idx+1` nodes if you want each index to be valid.
9    let mut net: Network<TestWireIO<i32>> = network!{
10        vec![node![0 => NoOpOperator::default()]],
11        Vec::new()
12    };
13
14    // The network you want for your tests
15    let network = Arc::new(AsyncMutex::new(net));
16
17    network
18}
19
20pub fn triple_noop_operator_usize_network() -> Arc<AsyncMutex<Network::<TestWireIO<i32>>>> {
21
22    // Build a "fake" network with 1 node if `node_idx`=0
23    // Or build `node_idx+1` nodes if you want each index to be valid.
24    let mut net = network!{
25        vec![
26            node![0 => NoOpOperator::default()],
27            node![1 => NoOpOperator::default()],
28            node![2 => NoOpOperator::default()],
29        ],
30        Vec::new()
31    };
32
33    // The network you want for your tests
34    let network = Arc::new(AsyncMutex::new(net));
35
36    network
37}
38
39pub fn empty_i32_network() -> Arc<AsyncMutex<Network::<TestWireIO<i32>>>> 
40{
41    // Build a "fake" network with 1 node if `node_idx`=0
42    // Or build `node_idx+1` nodes if you want each index to be valid.
43    let mut net = network!{Vec::new(),Vec::new()};
44
45    // The network you want for your tests
46    let network = Arc::new(AsyncMutex::new(net));
47
48    network
49}
50
51pub fn empty_usize_network() -> Arc<AsyncMutex<Network::<usize>>> 
52{
53    // Build a "fake" network with 1 node if `node_idx`=0
54    // Or build `node_idx+1` nodes if you want each index to be valid.
55    let mut net = network!{
56        vec![ ],
57        Vec::new()
58    };
59
60    // The network you want for your tests
61    let network = Arc::new(AsyncMutex::new(net));
62
63    network
64}
65
66/// Helper function to build a minimal test network with a single node using
67/// the `IncrementOperator`.
68pub fn build_single_node_network() -> Network<TestWireIO<i32>> {
69
70    network!([ node!(0 => IncrementOperator::default()) ], [])
71}
72
73/// Helper to build a multi-node network with each node using `IncrementOperator`.
74/// Edges form a chain 0->1->2->3... up to `n - 1`.
75pub fn build_chain_network(n: usize, initial_value: i32) -> Network<TestWireIO<i32>> {
76    let mut nodes = Vec::with_capacity(n);
77    for idx in 0..n {
78        let in_buf = if idx == 0 {
79            // the first node starts with an initial input
80            vec![arc![initial_value]]
81        } else {
82            // subsequent nodes start empty
83            vec![arc![0]]
84        };
85
86        let node_built: NetworkNode<TestWireIO<i32>> = node!(idx => IncrementOperator::default());
87        nodes.push(node_built);
88    }
89
90    // build chain edges
91    let mut edges = Vec::with_capacity(n.saturating_sub(1));
92    for idx in 0..(n.saturating_sub(1)) {
93        // node idx -> node idx+1
94        let e = edge!((idx,0) -> ((idx+1),0));
95        edges.push(e);
96    }
97
98    network!(nodes, edges)
99}
100
101/// Helper to build a branching network with concurrency potential.
102///        0
103///       / \
104///      1   2
105///       \ /
106///        3
107pub fn build_branching_network() -> Network<TestWireIO<i32>> {
108    network!(
109        vec![
110            // node 0 => single input=5 => single output
111            node!(0 => IncrementOperator::default()),
112            // node 1 => single input, single output
113            node!(1 => IncrementOperator::default()),
114            // node 2 => single input, single output
115            node!(2 => IncrementOperator::default()),
116            // node 3 => two inputs => so we must have two outputs if using IncrementOperator
117            node!(3 => IncrementOperator::default()),
118        ],
119        vec![
120            edge!(0:0 -> 1:0),
121            edge!(0:0 -> 2:0),
122            // combine 1->3:0 and 2->3:1
123            edge!(1:0 -> 3:0),
124            edge!(2:0 -> 3:1),
125        ]
126    )
127}