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

Commit

Permalink
test: alignment and foldhood are now tested
Browse files Browse the repository at this point in the history
  • Loading branch information
PaoloPenazzi authored and lm98 committed Jul 27, 2023
1 parent b811758 commit 63fafa2
Showing 1 changed file with 123 additions and 2 deletions.
125 changes: 123 additions & 2 deletions src/core/lang/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ where

#[cfg(test)]
mod test {
use std::any::Any;
use std::collections::HashMap;
use crate::core::context::context::Context;
use crate::core::export::export::Export;
use crate::core::lang::execution::round;
use crate::core::lang::lang::{nbr, rep};
use crate::core::lang::lang::{foldhood, nbr, rep};
use crate::core::path::path::path::Path;
use crate::core::path::slot::slot::Slot::Rep;
use crate::core::path::slot::slot::Slot::{FoldHood, Nbr, Rep};
use crate::core::vm::round_vm::round_vm::RoundVM;

fn init_vm() -> RoundVM {
Expand Down Expand Up @@ -77,4 +79,123 @@ mod test {
let (_vm__, res_) = round(init_with_ctx(ctx_), program);
assert_eq!(2, res_);
}

#[test]
fn test_local_value() {
let context = Context::new(0,
Default::default(),
Default::default(),
Default::default());
let result = round(init_with_ctx(context), |vm| (vm, 10));
assert_eq!(10, result.1);
}

#[test]
fn test_alignment() {
// No neighbor is aligned
let context = Context::new(0, Default::default(),
Default::default(), Default::default());
let result = round(init_with_ctx(context),
|vm1| rep(vm1, || 0, |vm2, _| {
foldhood(vm2, || 0,
| a, b | (a + b), || 1 )}));
assert_eq!(1, result.1);

// One neighbor is aligned
let mut exports: HashMap<i32, Export> = HashMap::new();
exports.insert(1, create_export());

let context = Context::new(0, Default::default(),
Default::default(), exports);

let result = round(init_with_ctx(context),
|vm1| rep(vm1, || 0, |vm2, _| {
foldhood(vm2, || 0,
| a, b| (a + b), || 1 )}));
assert_eq!(2, result.1);
}

fn create_export() -> Export {
let mut map: HashMap<Path, Box<dyn Any>> = HashMap::new();
map.insert(Path::from(vec![Rep(0)]), Box::new(1));
map.insert(Path::from(vec![Rep(0),FoldHood(0)]), Box::new(1));
Export::from(map)
}

fn create_context_foldhood_test() -> Context {
// Export of device 2: Export(/ -> "1", FoldHood(0) -> "1")
let mut exports_device_2: HashMap<Path, Box<dyn Any>> = HashMap::new();
exports_device_2.insert(Path::from(vec![FoldHood(0)]), Box::new(1));
exports_device_2.insert(Path::from(vec![]), Box::new(1));
// Export of device 4: Export(/ -> "3", FoldHood(0) -> "3")
let mut exports_device_4: HashMap<Path, Box<dyn Any>> = HashMap::new();
exports_device_4.insert(Path::from(vec![FoldHood(0)]), Box::new(3));
exports_device_4.insert(Path::from(vec![]), Box::new(3));
// Exports of the context: Map(2 -> Export(/ -> "a", FoldHood(0) -> "a"), 4 -> Export(/ -> "b", FoldHood(0) -> "b"))
let mut exports: HashMap<i32, Export> = HashMap::new();
exports.insert(2, Export::from(exports_device_2));
exports.insert(4, Export::from(exports_device_4));
// Context of the device
let context = Context::new(0, Default::default(), Default::default(), exports);
return context;
}

#[test]
fn test_foldhood() {
// Context of the device
let context = create_context_foldhood_test();
// Aggregate program in Scala: foldhood("1")(_ + _)("2")
let result = round(init_with_ctx(context),
|vm| foldhood(vm, || 1,
| a, b| (a + b), || 2 ));
assert_eq!(7, result.1);

// TODO
//let context = create_context_foldhood_test();
// Aggregate program in Scala: foldhood("-5")(_ + _)(if (nbr(false)) {0} else {1})
//let result = round(init_with_ctx(context),
//|vm| foldhood(vm, || -5,
// | a, b| (a + b),
//|vm1| if nbr(vm1, || false).1 {0} else {1} ));
//assert_eq!(-14, result.1);
}

#[test]
fn test_nbr() {
// 1 - NBR needs not to be nested into fold
let context = Context::new(0, Default::default(), Default::default(), Default::default());
let result = round(init_with_ctx(context), |vm| nbr(vm, || 7));
assert_eq!(7, result.1);

// 2 - NBR should support interaction between aligned devices
let context = create_context_nbr_test2();
// The following program is run: foldhood(0)(_ + _)(if (nbr(mid()) == mid()) 0 else 1)
// TODO
//let program = |vm| foldhood(vm,
// || 0,
// | a, b| (a + b),
// |vm1| if nbr(vm1, |vm1| vm1.self_id(vm1)) == mid() {0} else {1} );

}

fn create_context_nbr_test2() -> Context {
// Create this export: Map(
// 1 -> Export(/ -> "any", FoldHood(0) -> 1, FoldHood(0) / Nbr(0) -> 1),
// 2 -> Export(/ -> "any", FoldHood(0) -> 2, FoldHood(0) / Nbr(0) -> 2)
// )
let mut export_dev_1: HashMap<Path, Box<dyn Any>> = HashMap::new();
export_dev_1.insert(Path::from(vec![]), Box::new("any"));
export_dev_1.insert(Path::from(vec![FoldHood(0)]), Box::new(1));
export_dev_1.insert(Path::from(vec![FoldHood(0), Nbr(0)]), Box::new(1));
let mut export_dev_2: HashMap<Path, Box<dyn Any>> = HashMap::new();
export_dev_2.insert(Path::from(vec![]), Box::new("any"));
export_dev_2.insert(Path::from(vec![FoldHood(0)]), Box::new(2));
export_dev_2.insert(Path::from(vec![FoldHood(0), Nbr(0)]), Box::new(2));
let mut exports: HashMap<i32, Export> = HashMap::new();
exports.insert(1, Export::from(export_dev_1));
exports.insert(2, Export::from(export_dev_2));
// Context of the device
let context = Context::new(0, Default::default(), Default::default(), exports);
context
}
}

0 comments on commit 63fafa2

Please sign in to comment.