Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
feat(test): add utility to assert the equivalence of programs
Browse files Browse the repository at this point in the history
  • Loading branch information
lm98 authored and mergify[bot] committed Oct 9, 2023
1 parent 1396037 commit 3cd824e
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions src/core/lang/test/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::any::Any;
use std::collections::HashMap;
use std::fmt::Debug;
use crate::core::context::context::Context;
use crate::core::export::export::Export;
use crate::core::lang::execution::round;
use crate::core::path::path::path::Path;
use crate::core::sensor_id::sensor_id::SensorId;
use crate::core::vm::round_vm::round_vm::RoundVM;

pub fn init_vm() -> RoundVM {
Expand Down Expand Up @@ -28,10 +33,45 @@ pub fn vm(self_id: i32, local_sensor: HashMap<SensorId, Box<dyn Any>>, nbr_senso

pub fn assert_equivalence<A,F, G>(exec_order: Vec<i32>, nbrs: HashMap<i32, Vec<i32>>, program_1: F, program_2: G) -> bool
where
F: Fn(RoundVM) -> (RoundVM, A),
A: Eq + Copy + 'static,
F: Fn(RoundVM) -> (RoundVM, A) + Copy,
G: Fn(RoundVM) -> (RoundVM, A) + Copy,
A: Eq + Copy + 'static + Debug,
{
let (_vm_1, res_1) = round(init_vm(), program_1);
let (_vm_2, res_2) = round(init_vm(), program_2);
res_1 == res_2
let states: HashMap<i32, (RoundVM, RoundVM)> = nbrs.iter().map(|(curr, neighbors)|{
let ex_1: HashMap<i32, Export> = neighbors.iter().map(|nbr| (nbr.clone(), Export::new())).collect();
let ex_2: HashMap<i32, Export> = neighbors.iter().map(|nbr| (nbr.clone(), Export::new())).collect();
(
curr.clone(),
(vm(curr.clone(), Default::default(), Default::default(), ex_1),
vm(curr.clone(), Default::default(), Default::default(), ex_2))
)
}).collect();
assert_equivalence_rec(exec_order, states, program_1, program_2)
}

fn assert_equivalence_rec<A, F, G>(mut exec_order: Vec<i32>, states: HashMap<i32, (RoundVM, RoundVM)>, program_1: F, program_2: G) -> bool
where
F: Fn(RoundVM) -> (RoundVM, A) + Copy,
G: Fn(RoundVM) -> (RoundVM, A) + Copy,
A: Eq + Copy + 'static + Debug,
{
if exec_order.is_empty() {
return true;
}

let curr = exec_order.pop().unwrap();

let new_states: HashMap<i32, (RoundVM, RoundVM)> = states.into_iter().map(|(id, (vm_1, vm_2))| {
if id == curr {
let (vm_1_, res_1) = round(vm_1, program_1);
let (vm_2_, res_2) = round(vm_2, program_2);
if res_1 != res_2 {
panic!("Programs are not equivalent: {:?} != {:?}", res_1, res_2);
}
(id, (vm_1_, vm_2_))
} else {
(id, (vm_1, vm_2))
}
}).collect();
assert_equivalence_rec(exec_order, new_states, program_1, program_2)
}

0 comments on commit 3cd824e

Please sign in to comment.