Skip to content

Commit

Permalink
test: add tests for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
heypoom committed Oct 5, 2023
1 parent 53d95ca commit f28d54b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/instructions/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Load for Memory {
// TODO: this is very repetitive!
// Can we detect the number of arguments and do this automatically?
match ins {
I::LoadString(v) | I::Store(v) | I::Load(v) | I::JumpNotZero(v) | I::JumpZero(v) | I::Jump(v) | I::Push(v) | I::EndLoop(v) => write(v),
I::LoadString(v) | I::Store(v) | I::Load(v) | I::JumpNotZero(v) | I::JumpZero(v) | I::Jump(v) | I::Push(v) => write(v),
_ => {}
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ pub enum Instruction {
Mul,
Div,

StartLoop,
EndLoop(u16),

/// Jump to the address.
Jump(u16),

Expand Down Expand Up @@ -93,7 +90,6 @@ impl From<Instruction> for u16 {
fn from(ins: Instruction) -> Self {
let v = match ins {
I::Push(_) => I::Push(0),
I::EndLoop(_) => I::EndLoop(0),
I::Jump(_) => I::Jump(0),
I::JumpZero(_) => I::JumpZero(0),
I::JumpNotZero(_) => I::JumpNotZero(0),
Expand Down
1 change: 0 additions & 1 deletion src/machine/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ impl Decode for Machine {
// Can we detect the number of arguments and do this automatically?
match i {
I::Push(_) => I::Push(self.arg()),
I::EndLoop(_) => I::EndLoop(self.arg()),
I::Jump(_) => I::Jump(self.arg()),
I::JumpZero(_) => I::JumpZero(self.arg()),
I::JumpNotZero(_) => I::JumpNotZero(self.arg()),
Expand Down
3 changes: 0 additions & 3 deletions src/machine/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ impl Execute for Machine {
I::GreaterThan => s.apply_two(|a, b| (a > b).into()),
I::GreaterThanOrEqual => s.apply_two(|a, b| (a >= b).into()),

I::StartLoop => {}

I::EndLoop(start) => self.reg.set(PC, start),
I::Jump(addr) => self.reg.set(PC, addr),

I::JumpZero(addr) => {
Expand Down
4 changes: 2 additions & 2 deletions tests/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod tests {
extern crate opcodes_to_algorithms as O;

use mockall::{automock, predicate::*};
use O::{Machine as M, Execute, Instruction as I, Load, WithStringManager};
use O::{Machine, Execute, Instruction as I, Load, WithStringManager};

#[cfg_attr(test, automock)]
trait Printer {
Expand All @@ -12,7 +12,7 @@ mod tests {

#[test]
fn test_print_hello_world() {
let mut m = M::new();
let mut m = Machine::new();

let mut ms = m.mem.string();
let h_addr = ms.add_str("hello, ");
Expand Down
29 changes: 26 additions & 3 deletions tests/loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,31 @@
mod loop_tests {
extern crate opcodes_to_algorithms as O;

// use O::{Machine as M, Execute, Instruction as I};
use O::{Machine, Execute, Load, Instruction as I};

#[test]
fn test_loop() {}
}
fn test_loop() {
let mut m = Machine::new();

let loop_start = 1;

m.mem.load_code(vec![
I::Push(0), // i = 0

// [loop_start]
I::Push(2),
I::Add, // i += 2
I::Dup, // B = i
I::Push(20), // A = 20

// [A = 20, B = i; A < B; ? 1 : 0]
I::LessThan,
I::JumpZero(loop_start), // 20 , jump to loop_start
I::Push(0xFF),
]);

m.run();

assert_eq!(m.mem.read_stack(2), [22, 255]);
}
}
4 changes: 2 additions & 2 deletions tests/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
mod tests {
extern crate opcodes_to_algorithms as O;

use O::{Machine as M, Execute, Instruction as I, Load, WithStringManager};
use O::{Machine, Execute, Instruction as I, Load, WithStringManager};

#[test]
fn test_load_str() {
let mut m = M::new();
let mut m = Machine::new();
let mut ms = m.mem.string();

let s = "hello";
Expand Down

0 comments on commit f28d54b

Please sign in to comment.