Skip to content

Commit

Permalink
derive PartialEq and Eq for ProgramMemory and MutableState (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Nov 7, 2024
1 parent 9cdadc5 commit 2acd221
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions crates/brainfuck_vm/src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ impl MachineBuilder {
}
}

#[derive(Debug, Default, PartialEq, Eq)]
pub struct ProgramMemory {
code: Vec<BaseField>,
}

#[derive(Debug, Default, PartialEq, Eq)]
pub struct MutableState {
ram: Vec<BaseField>,
registers: Registers,
Expand Down Expand Up @@ -237,9 +239,14 @@ mod tests {
let code = vec![BaseField::from(43)];
let (machine, _) = create_test_machine(&code, &[]);

assert_eq!(machine.program.code, code);
assert_eq!(machine.state.ram.len(), Machine::DEFAULT_RAM_SIZE);
assert!(machine.state.ram.iter().all(|&x| x == BaseField::zero()));
assert_eq!(machine.program, ProgramMemory { code });
assert_eq!(
machine.state,
MutableState {
ram: vec![BaseField::zero(); Machine::DEFAULT_RAM_SIZE],
..Default::default()
}
);
}

#[test]
Expand All @@ -250,9 +257,11 @@ mod tests {
let ram_size = 55000;
let machine = Machine::new_with_config(&code, input, output, ram_size);

assert_eq!(machine.program.code, code);
assert_eq!(machine.state.ram.len(), ram_size);
assert!(machine.state.ram.iter().all(|&x| x == BaseField::zero()));
assert_eq!(machine.program, ProgramMemory { code });
assert_eq!(
machine.state,
MutableState { ram: vec![BaseField::zero(); ram_size], ..Default::default() }
);
}

#[test]
Expand All @@ -262,6 +271,7 @@ mod tests {
let (mut machine, _) = create_test_machine(&code, &[]);
machine.execute()?;

assert_eq!(machine.program, ProgramMemory { code });
assert_eq!(machine.state.registers.mp, BaseField::from(2));
Ok(())
}
Expand Down Expand Up @@ -394,11 +404,7 @@ mod tests {
mvi: BaseField::from(2).inverse(),
};

assert_eq!(trace.len(), 3);
assert_eq!(trace[0], initial_state);
assert_eq!(trace[1], intermediate_state);
assert_eq!(trace[2], final_state);

assert_eq!(trace, vec![initial_state, intermediate_state, final_state]);
Ok(())
}

Expand Down

0 comments on commit 2acd221

Please sign in to comment.