use crate::protocol::*;
use crate::protocol::eval::*;
use crate::runtime2::runtime::*;
use crate::runtime2::component::{CompCtx, CompPDL};
fn create_component(rt: &Runtime, module_name: &str, routine_name: &str, args: ValueGroup) {
let prompt = rt.inner.protocol.new_component(
module_name.as_bytes(), routine_name.as_bytes(), args
).expect("create prompt");
let reserved = rt.inner.start_create_pdl_component();
let ctx = CompCtx::new(&reserved);
let (key, _) = rt.inner.finish_create_pdl_component(reserved, CompPDL::new(prompt, 0), ctx, false);
rt.inner.enqueue_work(key);
}
fn no_args() -> ValueGroup { ValueGroup::new_stack(Vec::new()) }
#[test]
fn test_component_creation() {
let pd = ProtocolDescription::parse(b"
primitive nothing_at_all() {
s32 a = 5;
auto b = 5 + a;
}
").expect("compilation");
let rt = Runtime::new(1, pd);
for i in 0..20 {
create_component(&rt, "", "nothing_at_all", no_args());
}
}
#[test]
fn test_component_communication_a() {
let pd = ProtocolDescription::parse(b"
primitive sender(out<u32> o) {
sync put(o, 1);
}
primitive receiver(in<u32> i) {
sync auto a = get(i);
}
composite constructor() {
channel o -> i;
new sender(o);
new receiver(i);
}
").expect("compilation");
let rt = Runtime::new(1, pd);
create_component(&rt, "", "constructor", no_args());
}
#[test]
fn test_component_communication_b() {
let pd = ProtocolDescription::parse(b"
primitive sender(out<u32> o, u32 rounds) {
u32 index = 0;
sync while (index < rounds) {
put(o, index);
index += 1;
}
}
primitive receiver(in<u32> i, u32 rounds) {
u32 index = 0;
sync while (index < rounds) {
auto val = get(i);
while (val != index) {} // infinite loop if incorrect value is received
index += 1;
}
}
composite constructor() {
channel o -> i;
new sender(o, 5);
new receiver(i, 5);
}").expect("compilation");
let rt = Runtime::new(1, pd);
create_component(&rt, "", "constructor", no_args());
}