From c6ea3a0cb322b888b0b8e6fd14e6d9942eeab576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Thu, 23 Mar 2023 17:42:36 +0100 Subject: [PATCH] :recycle: Refacted machine tests with macros --- src/simulator/machine.rs | 104 ++++++++++++--------------------------- 1 file changed, 32 insertions(+), 72 deletions(-) diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index e54bd87..1a1057f 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -728,6 +728,29 @@ mod test { 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] fn test_init_machine() { let _ = Machine::init_machine(); @@ -756,126 +779,63 @@ mod test { #[test] fn test_comp() { - let mut m = Machine::init_machine(); - 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(); - + let (m, memory_after, expected_trace) = init_test!("Comp"); assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m)); assert!(expected_trace.contains(m.registers_trace.as_str())); } #[test] fn test_add() { - let mut m = Machine::init_machine(); - 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(); - + let (m, memory_after, expected_trace) = init_test!("Add"); assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m)); assert!(expected_trace.contains(m.registers_trace.as_str())); } #[test] fn test_div() { - let mut m = Machine::init_machine(); - 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(); - + let (m, memory_after, expected_trace) = init_test!("Div"); assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m)); assert!(expected_trace.contains(m.registers_trace.as_str())); } #[test] fn test_if() { - let mut m = Machine::init_machine(); - 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(); - + let (m, memory_after, expected_trace) = init_test!("If"); assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m)); assert!(expected_trace.contains(m.registers_trace.as_str())); } #[test] fn test_jump() { - let mut m = Machine::init_machine(); - 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(); - + let (m, memory_after, expected_trace) = init_test!("Jump"); assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m)); assert!(expected_trace.contains(m.registers_trace.as_str())); } #[test] fn test_mul() { - let mut m = Machine::init_machine(); - 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(); - + let (m, memory_after, expected_trace) = init_test!("Mult"); assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m)); assert!(expected_trace.contains(m.registers_trace.as_str())); } #[test] fn test_ret() { - let mut m = Machine::init_machine(); - 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(); - + let (m, memory_after, expected_trace) = init_test!("Ret"); assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m)); assert!(expected_trace.contains(m.registers_trace.as_str())); } #[test] fn test_sub() { - let mut m = Machine::init_machine(); - 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(); - + let (m, memory_after, expected_trace) = init_test!("Sub"); assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m)); assert!(expected_trace.contains(m.registers_trace.as_str())); } #[test] fn test_switch() { - let mut m = Machine::init_machine(); - 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(); - + let (m, memory_after, expected_trace) = init_test!("Switch"); assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m)); assert!(expected_trace.contains(m.registers_trace.as_str())); }