Skip to content

Commit

Permalink
add tests on parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
chanmix51 committed Apr 4, 2024
1 parent eabc4f7 commit 8ac21b5
Show file tree
Hide file tree
Showing 8 changed files with 390 additions and 114 deletions.
36 changes: 27 additions & 9 deletions soft65c02_lib/src/memory/ram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,9 @@ impl AddressableIO for RAM {
if location + data.len() > self.ram.len() {
Err(MemoryError::WriteOverflow(data.len(), location))
} else {
self.ram
.iter_mut()
.enumerate()
.skip(location)
.for_each(|(index, value)| *value = data[index]);
// for offset in 0..data.len() {
// self.ram[usize::from(location) + offset] = data[offset];
// }

for (offset, value) in data.into_iter().enumerate() {
self.ram[location + offset] = *value;
}
Ok(())
}
}
Expand All @@ -44,3 +38,27 @@ impl AddressableIO for RAM {
}

impl DebugIO for RAM {}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn check_write_ram() {
let mut memory = RAM::default();
memory.write(1000, &[0x01, 0x02, 0x03]).unwrap();

assert_eq!(1, memory.ram[1000]);
assert_eq!(2, memory.ram[1001]);
assert_eq!(3, memory.ram[1002]);
assert_eq!(0, memory.ram[1003]);
}

#[test]
fn check_read_ram() {
let mut memory = RAM::default();
memory.ram[1000] = 0xff;

assert_eq!(vec![0x00, 0xff, 0x00], memory.read(999, 3).unwrap());
}
}
9 changes: 0 additions & 9 deletions soft65c02_tester/rules.pest
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ sentence = _{ SOI ~ instruction | COMMENT ~ EOI }
instruction = { registers_instruction |
memory_instruction |
run_instruction |
help_instruction |
disassemble_instruction |
assert_instruction |
reset_instruction }
Expand Down Expand Up @@ -40,14 +39,6 @@ disassemble_instruction = {

assert_instruction = { ^"assert" ~ boolean_condition ~ "$$" ~ description ~ "$$"}

help_instruction = { help_registers | help_memory | help_run | help_disassemble | help_assert | help_global }
help_registers = { ^"help registers" }
help_memory = { ^"help memory" }
help_run = { ^"help run" }
help_disassemble = { ^"help disassemble" }
help_assert = { ^"help assert" }
help_global = { ^"help" }

boolean_condition = { boolean | operation }

boolean = { ^"true" | ^"false" }
Expand Down
85 changes: 79 additions & 6 deletions soft65c02_tester/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,92 @@
use crate::until_condition::BooleanExpression;
use anyhow::anyhow;
use soft65c02_lib::{Memory, Registers};

use crate::{until_condition::BooleanExpression, AppResult};

pub trait Command {
fn execute(&self, registers: &mut Registers, memory: &mut Memory) -> AppResult<String>;
}

#[derive(Debug)]
pub enum CliCommand {
Help(HelpCommand),
Run(RunCommand),
Assert(AssertCommand),
None,
}

impl Command for CliCommand {
fn execute(&self, registers: &mut Registers, memory: &mut Memory) -> AppResult<String> {
match self {
Self::Run(command) => command.execute(registers, memory),
Self::Assert(command) => command.execute(registers, memory),
Self::None => Ok(String::new()),
}
}
}

#[derive(Debug)]
pub enum HelpCommand {
Global,
Registers,
Run,
pub struct AssertCommand {
pub condition: BooleanExpression,
pub comment: String,
}

impl Command for AssertCommand {
fn execute(&self, registers: &mut Registers, memory: &mut Memory) -> AppResult<String> {
if self.condition.solve(registers, memory) {
Ok(self.comment.clone())
} else {
Err(anyhow!(self.comment.clone()))
}
}
}

#[derive(Debug)]
pub struct RunCommand {
pub stop_condition: BooleanExpression,
pub start_address: Option<usize>,
}

impl Command for RunCommand {
fn execute(&self, registers: &mut Registers, memory: &mut Memory) -> AppResult<String> {
todo!()
}
}

#[cfg(test)]
mod assert_command_tests {
use super::*;

#[test]
fn test_assert_command_ok() {
let command = AssertCommand {
condition: BooleanExpression::Value(true),
comment: "nice comment".to_string(),
};
let mut registers = Registers::new(0x0000);
let mut memory = Memory::new_with_ram();

match command.execute(&mut registers, &mut memory) {
Ok(s) => assert_eq!("nice comment", s),
Err(_) => panic!("This condition must be valid."),
};
}

#[test]
fn test_assert_command_fails() {
let command = AssertCommand {
condition: BooleanExpression::Value(false),
comment: "nice comment".to_string(),
};
let mut registers = Registers::new(0x0000);
let mut memory = Memory::new_with_ram();

command
.execute(&mut registers, &mut memory)
.expect_err("This condition must fail.");
}
}

#[cfg(test)]
mod run_command_tests {
use super::*;
}
8 changes: 8 additions & 0 deletions soft65c02_tester/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod commands;
mod pest_parser;
mod until_condition;

pub use commands::*;
pub use pest_parser::CliCommandParser;

pub type AppResult<T> = anyhow::Result<T>;
6 changes: 0 additions & 6 deletions soft65c02_tester/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
mod commands;
mod pest_parser;
mod until_condition;

use anyhow::Result;

pub type AppResult<T> = anyhow::Result<T>;

fn main() -> Result<()> {
println!("Hello, world!");

Expand Down
Loading

0 comments on commit 8ac21b5

Please sign in to comment.