♻️ Refacted machine tests with macros
This commit is contained in:
parent
5963759141
commit
c6ea3a0cb3
@ -728,6 +728,29 @@ mod test {
|
|||||||
|
|
||||||
use crate::simulator::{machine::Machine, mem_cmp};
|
use crate::simulator::{machine::Machine, mem_cmp};
|
||||||
|
|
||||||
|
macro_rules! get_full_path {
|
||||||
|
($prefix: expr, $test_name:expr) => {{
|
||||||
|
let mut s = String::from("test/machine/");
|
||||||
|
s.push_str($prefix);
|
||||||
|
s.push_str($test_name);
|
||||||
|
s.push_str(".txt");
|
||||||
|
&s.to_owned()
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! init_test {
|
||||||
|
($a:expr) => {{
|
||||||
|
let mut m = Machine::init_machine();
|
||||||
|
let end_file_name = { let mut s = String::from($a); s.push_str("End"); s };
|
||||||
|
let memory_before = mem_cmp::MemChecker::from(get_full_path!("memory", $a)).unwrap();
|
||||||
|
let memory_after = mem_cmp::MemChecker::from(get_full_path!("memory", &end_file_name)).unwrap();
|
||||||
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
||||||
|
Machine::run(&mut m);
|
||||||
|
let expected_trace = fs::read_to_string(get_full_path!("reg_trace", $a)).unwrap();
|
||||||
|
(m, memory_after, expected_trace)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_init_machine() {
|
fn test_init_machine() {
|
||||||
let _ = Machine::init_machine();
|
let _ = Machine::init_machine();
|
||||||
@ -756,126 +779,63 @@ mod test {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_comp() {
|
fn test_comp() {
|
||||||
let mut m = Machine::init_machine();
|
let (m, memory_after, expected_trace) = init_test!("Comp");
|
||||||
let memory_before = mem_cmp::MemChecker::from("test/machine/memoryComp.txt").unwrap();
|
|
||||||
let memory_after = mem_cmp::MemChecker::from("test/machine/memoryCompEnd.txt").unwrap();
|
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
||||||
Machine::run(&mut m);
|
|
||||||
|
|
||||||
let expected_trace = fs::read_to_string("test/machine/reg_traceComp.txt").unwrap();
|
|
||||||
|
|
||||||
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
||||||
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add() {
|
fn test_add() {
|
||||||
let mut m = Machine::init_machine();
|
let (m, memory_after, expected_trace) = init_test!("Add");
|
||||||
let memory_before = mem_cmp::MemChecker::from("test/machine/memoryAdd.txt").unwrap();
|
|
||||||
let memory_after = mem_cmp::MemChecker::from("test/machine/memoryAddEnd.txt").unwrap();
|
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
||||||
Machine::run(&mut m);
|
|
||||||
|
|
||||||
let expected_trace = fs::read_to_string("test/machine/reg_traceAdd.txt").unwrap();
|
|
||||||
|
|
||||||
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
||||||
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_div() {
|
fn test_div() {
|
||||||
let mut m = Machine::init_machine();
|
let (m, memory_after, expected_trace) = init_test!("Div");
|
||||||
let memory_before = mem_cmp::MemChecker::from("test/machine/memoryDiv.txt").unwrap();
|
|
||||||
let memory_after = mem_cmp::MemChecker::from("test/machine/memoryDivEnd.txt").unwrap();
|
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
||||||
Machine::run(&mut m);
|
|
||||||
|
|
||||||
let expected_trace = fs::read_to_string("test/machine/reg_traceDiv.txt").unwrap();
|
|
||||||
|
|
||||||
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
||||||
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_if() {
|
fn test_if() {
|
||||||
let mut m = Machine::init_machine();
|
let (m, memory_after, expected_trace) = init_test!("If");
|
||||||
let memory_before = mem_cmp::MemChecker::from("test/machine/memoryIf.txt").unwrap();
|
|
||||||
let memory_after = mem_cmp::MemChecker::from("test/machine/memoryIfEnd.txt").unwrap();
|
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
||||||
Machine::run(&mut m);
|
|
||||||
|
|
||||||
let expected_trace = fs::read_to_string("test/machine/reg_traceIf.txt").unwrap();
|
|
||||||
|
|
||||||
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
||||||
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_jump() {
|
fn test_jump() {
|
||||||
let mut m = Machine::init_machine();
|
let (m, memory_after, expected_trace) = init_test!("Jump");
|
||||||
let memory_before = mem_cmp::MemChecker::from("test/machine/memoryJump.txt").unwrap();
|
|
||||||
let memory_after = mem_cmp::MemChecker::from("test/machine/memoryJumpEnd.txt").unwrap();
|
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
||||||
Machine::run(&mut m);
|
|
||||||
|
|
||||||
let expected_trace = fs::read_to_string("test/machine/reg_traceJump.txt").unwrap();
|
|
||||||
|
|
||||||
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
||||||
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mul() {
|
fn test_mul() {
|
||||||
let mut m = Machine::init_machine();
|
let (m, memory_after, expected_trace) = init_test!("Mult");
|
||||||
let memory_before = mem_cmp::MemChecker::from("test/machine/memoryMult.txt").unwrap();
|
|
||||||
let memory_after = mem_cmp::MemChecker::from("test/machine/memoryMultEnd.txt").unwrap();
|
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
||||||
Machine::run(&mut m);
|
|
||||||
|
|
||||||
let expected_trace = fs::read_to_string("test/machine/reg_traceMult.txt").unwrap();
|
|
||||||
|
|
||||||
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
||||||
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ret() {
|
fn test_ret() {
|
||||||
let mut m = Machine::init_machine();
|
let (m, memory_after, expected_trace) = init_test!("Ret");
|
||||||
let memory_before = mem_cmp::MemChecker::from("test/machine/memoryRet.txt").unwrap();
|
|
||||||
let memory_after = mem_cmp::MemChecker::from("test/machine/memoryRetEnd.txt").unwrap();
|
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
||||||
Machine::run(&mut m);
|
|
||||||
|
|
||||||
let expected_trace = fs::read_to_string("test/machine/reg_traceRet.txt").unwrap();
|
|
||||||
|
|
||||||
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
||||||
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sub() {
|
fn test_sub() {
|
||||||
let mut m = Machine::init_machine();
|
let (m, memory_after, expected_trace) = init_test!("Sub");
|
||||||
let memory_before = mem_cmp::MemChecker::from("test/machine/memorySub.txt").unwrap();
|
|
||||||
let memory_after = mem_cmp::MemChecker::from("test/machine/memorySubEnd.txt").unwrap();
|
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
||||||
Machine::run(&mut m);
|
|
||||||
|
|
||||||
let expected_trace = fs::read_to_string("test/machine/reg_traceSub.txt").unwrap();
|
|
||||||
|
|
||||||
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
||||||
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_switch() {
|
fn test_switch() {
|
||||||
let mut m = Machine::init_machine();
|
let (m, memory_after, expected_trace) = init_test!("Switch");
|
||||||
let memory_before = mem_cmp::MemChecker::from("test/machine/memorySwitch.txt").unwrap();
|
|
||||||
let memory_after = mem_cmp::MemChecker::from("test/machine/memorySwitchEnd.txt").unwrap();
|
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
|
||||||
Machine::run(&mut m);
|
|
||||||
|
|
||||||
let expected_trace = fs::read_to_string("test/machine/reg_traceSwitch.txt").unwrap();
|
|
||||||
|
|
||||||
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
||||||
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user