diff --git a/src/runtime2/tests/mod.rs b/src/runtime2/tests/mod.rs index 794f42274ffbd045070e49049e068e1c613ba26f..04c61793c086384b81c5694d4a90c18e7a4912dc 100644 --- a/src/runtime2/tests/mod.rs +++ b/src/runtime2/tests/mod.rs @@ -5,52 +5,69 @@ use crate::{PortId, ProtocolDescription}; use crate::common::Id; use crate::protocol::eval::*; -fn runtime_for(num_threads: u32, pdl: &str) -> Runtime { +const NUM_THREADS: u32 = 4; // number of threads in runtime +const NUM_INSTANCES: u32 = 10; // number of test instances constructed +const NUM_LOOPS: u32 = 1; // number of loops within a single test (not used by all tests) + +fn create_runtime(pdl: &str) -> Runtime { let protocol = ProtocolDescription::parse(pdl.as_bytes()).expect("parse pdl"); - let runtime = Runtime::new(num_threads, protocol); + let runtime = Runtime::new(NUM_THREADS, protocol); return runtime; } +fn run_test_in_runtime(pdl: &str, constructor: F) { + let protocol = ProtocolDescription::parse(pdl.as_bytes()) + .expect("parse PDL"); + let runtime = Runtime::new(NUM_THREADS, protocol); + + let mut api = runtime.create_interface(); + for _ in 0..NUM_INSTANCES { + constructor(&mut api); + } + + // Wait until done :) +} + #[test] fn test_put_and_get() { - let rt = runtime_for(4, " -primitive putter(out sender, u32 loops) { - u32 index = 0; - while (index < loops) { - synchronous { - print(\"putting!\"); - put(sender, true); + const CODE: &'static str = " + primitive putter(out sender, u32 loops) { + u32 index = 0; + while (index < loops) { + synchronous { + print(\"putting!\"); + put(sender, true); + } + index += 1; } - index += 1; } -} -primitive getter(in receiver, u32 loops) { - u32 index = 0; - while (index < loops) { - synchronous { - print(\"getting!\"); - auto result = get(receiver); - assert(result); + primitive getter(in receiver, u32 loops) { + u32 index = 0; + while (index < loops) { + synchronous { + print(\"getting!\"); + auto result = get(receiver); + assert(result); + } + index += 1; } - index += 1; } -} - "); + "; - let mut api = rt.create_interface(); - let channel = api.create_channel(); - let num_loops = 1; + run_test_in_runtime(CODE, |api| { + let channel = api.create_channel(); - api.create_connector("", "putter", ValueGroup::new_stack(vec![ - Value::Output(PortId(Id{ connector_id: 0, u32_suffix: channel.putter_id.index })), - Value::UInt32(num_loops) - ])).expect("create putter"); + api.create_connector("", "putter", ValueGroup::new_stack(vec![ + Value::Output(PortId(Id{ connector_id: 0, u32_suffix: channel.putter_id.index })), + Value::UInt32(NUM_LOOPS) + ])).expect("create putter"); - api.create_connector("", "getter", ValueGroup::new_stack(vec![ - Value::Input(PortId(Id{ connector_id: 0, u32_suffix: channel.getter_id.index })), - Value::UInt32(num_loops) - ])).expect("create getter"); + api.create_connector("", "getter", ValueGroup::new_stack(vec![ + Value::Input(PortId(Id{ connector_id: 0, u32_suffix: channel.getter_id.index })), + Value::UInt32(NUM_LOOPS) + ])).expect("create getter"); + }); } \ No newline at end of file