Reworked unit tests for machine and renamed test_programs directory

This commit is contained in:
François Autin 2023-03-12 00:22:16 +01:00
parent 177abfe846
commit c75e2995e7
No known key found for this signature in database
GPG Key ID: 343F5D382E1DD77C
29 changed files with 24 additions and 48 deletions

View File

@ -729,15 +729,12 @@ mod test {
#[test] #[test]
fn test_comp() { fn test_comp() {
let mut m = Machine::init_machine(); let mut m = Machine::init_machine();
let path_before = "memoryComp.txt".to_string(); let memory_before = mem_cmp::MemChecker::from("test/machine/memoryComp.txt").unwrap();
let path_after = "memoryCompEnd.txt".to_string(); let memory_after = mem_cmp::MemChecker::from("test/machine/memoryCompEnd.txt").unwrap();
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
Machine::run(&mut m); Machine::run(&mut m);
let path_trace = "memoryCompTrace.txt".to_string(); let expected_trace = fs::read_to_string("test/machine/memoryCompTrace.txt").unwrap();
let expected_trace = fs::read_to_string(path_trace).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()));
@ -746,15 +743,12 @@ mod test {
#[test] #[test]
fn test_div() { fn test_div() {
let mut m = Machine::init_machine(); let mut m = Machine::init_machine();
let path_before = "memoryDiv.txt"; let memory_before = mem_cmp::MemChecker::from("test/machine/memoryDiv.txt").unwrap();
let path_after = "memoryDivEnd.txt"; let memory_after = mem_cmp::MemChecker::from("test/machine/memoryDivEnd.txt").unwrap();
let memory_before = mem_cmp::MemChecker::from(path_before).unwrap();
let memory_after = mem_cmp::MemChecker::from(path_after).unwrap();
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
Machine::run(&mut m); Machine::run(&mut m);
let path_trace = "memoryDivTrace.txt".to_string(); let expected_trace = fs::read_to_string("test/machine/memoryDivTrace.txt").unwrap();
let expected_trace = fs::read_to_string(path_trace).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()));
@ -763,15 +757,12 @@ mod test {
#[test] #[test]
fn test_if() { fn test_if() {
let mut m = Machine::init_machine(); let mut m = Machine::init_machine();
let path_before = "memoryIf.txt".to_string(); let memory_before = mem_cmp::MemChecker::from("test/machine/memoryIf.txt").unwrap();
let path_after = "memoryIfEnd.txt".to_string(); let memory_after = mem_cmp::MemChecker::from("test/machine/memoryIfEnd.txt").unwrap();
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
Machine::run(&mut m); Machine::run(&mut m);
let path_trace = "memoryIfTrace.txt".to_string(); let expected_trace = fs::read_to_string("test/machine/memoryIfTrace.txt").unwrap();
let expected_trace = fs::read_to_string(path_trace).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()));
@ -780,15 +771,12 @@ mod test {
#[test] #[test]
fn test_jump() { fn test_jump() {
let mut m = Machine::init_machine(); let mut m = Machine::init_machine();
let path_before = "memoryJump.txt".to_string(); let memory_before = mem_cmp::MemChecker::from("test/machine/memoryJump.txt").unwrap();
let path_after = "memoryJumpEnd.txt".to_string(); let memory_after = mem_cmp::MemChecker::from("test/machine/memoryJumpEnd.txt").unwrap();
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
Machine::run(&mut m); Machine::run(&mut m);
let path_trace = "memoryJumpTrace.txt".to_string(); let expected_trace = fs::read_to_string("test/machine/memoryJumpTrace.txt").unwrap();
let expected_trace = fs::read_to_string(path_trace).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()));
@ -797,15 +785,12 @@ mod test {
#[test] #[test]
fn test_mul() { fn test_mul() {
let mut m = Machine::init_machine(); let mut m = Machine::init_machine();
let path_before = "memoryMul.txt".to_string(); let memory_before = mem_cmp::MemChecker::from("test/machine/memoryMul.txt").unwrap();
let path_after = "memoryMulEnd.txt".to_string(); let memory_after = mem_cmp::MemChecker::from("test/machine/memoryMulEnd.txt").unwrap();
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
Machine::run(&mut m); Machine::run(&mut m);
let path_trace = "memoryMulTrace.txt".to_string(); let expected_trace = fs::read_to_string("test/machine/memoryMulTrace.txt").unwrap();
let expected_trace = fs::read_to_string(path_trace).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()));
@ -814,15 +799,12 @@ mod test {
#[test] #[test]
fn test_ret() { fn test_ret() {
let mut m = Machine::init_machine(); let mut m = Machine::init_machine();
let path_before = "memoryRet.txt".to_string(); let memory_before = mem_cmp::MemChecker::from("test/machine/memoryRet.txt").unwrap();
let path_after = "memoryRetEnd.txt".to_string(); let memory_after = mem_cmp::MemChecker::from("test/machine/memoryRetEnd.txt").unwrap();
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
Machine::run(&mut m); Machine::run(&mut m);
let path_trace = "memoryRetTrace.txt".to_string(); let expected_trace = fs::read_to_string("test/machine/memoryRetTrace.txt").unwrap();
let expected_trace = fs::read_to_string(path_trace).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()));
@ -831,15 +813,12 @@ mod test {
#[test] #[test]
fn test_sub() { fn test_sub() {
let mut m = Machine::init_machine(); let mut m = Machine::init_machine();
let path_before = "memorySub.txt".to_string(); let memory_before = mem_cmp::MemChecker::from("test/machine/memorySub.txt").unwrap();
let path_after = "memorySubEnd.txt".to_string(); let memory_after = mem_cmp::MemChecker::from("test/machine/memorySubEnd.txt").unwrap();
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
Machine::run(&mut m); Machine::run(&mut m);
let path_trace = "memorySubTrace.txt".to_string(); let expected_trace = fs::read_to_string("test/machine/memorySubTrace.txt").unwrap();
let expected_trace = fs::read_to_string(path_trace).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()));
@ -848,15 +827,12 @@ mod test {
#[test] #[test]
fn test_switch() { fn test_switch() {
let mut m = Machine::init_machine(); let mut m = Machine::init_machine();
let path_before = "memorySwitch.txt".to_string(); let memory_before = mem_cmp::MemChecker::from("test/machine/memorySwitch.txt").unwrap();
let path_after = "memorySwitchEnd.txt".to_string(); let memory_after = mem_cmp::MemChecker::from("test/machine/memorySwitchEnd.txt").unwrap();
let memory_before = mem_cmp::MemChecker::from(&path_before).unwrap();
let memory_after = mem_cmp::MemChecker::from(&path_after).unwrap();
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m); mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
Machine::run(&mut m); Machine::run(&mut m);
let path_trace = "memorySwitchTrace.txt".to_string(); let expected_trace = fs::read_to_string("test/machine/memorySwitchTrace.txt").unwrap();
let expected_trace = fs::read_to_string(path_trace).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()));

Binary file not shown.

Binary file not shown.