Skip to content

Commit

Permalink
refactor: extract unit tests to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
heypoom committed Sep 25, 2023
1 parent 5c4a2eb commit bb17688
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 147 deletions.
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "cpu-from-scratch"
name = "opcodes_to_algorithms"
version = "0.1.0"
edition = "2021"

Expand Down
3 changes: 0 additions & 3 deletions src/instructions/load.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::instructions::Instruction;
use crate::instructions::Instruction as I;
use crate::mem::Memory;

Expand Down Expand Up @@ -46,8 +45,6 @@ impl Load for Memory {
mod tests {
use super::*;

type I = Instruction;

#[test]
fn test_load_code() {
let mut m = Memory::new();
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub mod instructions;
pub mod machine;
pub mod mem;
pub mod register;

pub use instructions::*;
pub use machine::*;
pub use mem::*;
pub use register::*;


115 changes: 1 addition & 114 deletions src/machine/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod decode;
mod execute;

use crate::instructions::{Instruction as I, Instruction, Load};
use crate::instructions::{Instruction, Load};
use crate::mem::{Memory, StackManager};
use crate::register::Registers;

Expand Down Expand Up @@ -34,116 +34,3 @@ impl From<Vec<Instruction>> for Machine {
}
}

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

type M = Machine;

#[test]
fn test_add() {
let mut m: M = vec![I::Push(5), I::Push(10), I::Add, I::Push(3), I::Sub].into();

m.tick();
m.tick();
assert_eq!(m.mem.read_stack(3), [0, 10, 5]);

m.tick();
assert_eq!(m.mem.read_stack(3), [0, 0, 15]);

m.tick();
assert_eq!(m.stack().peek(), 3);

m.tick();
assert_eq!(m.mem.read_stack(3), [0, 0, 12]);
}

#[test]
fn test_run() {
let mut m: M = vec![I::Push(10), I::Push(3), I::Sub].into();
m.run();
assert_eq!(m.mem.read_stack(2), [0, 7]);
}

#[test]
fn test_eq() {
let mut m: M = vec![I::Push(10), I::Push(10), I::Equal].into();
m.run();
assert_eq!(m.stack().peek(), 1);

let mut m: M = vec![I::Push(5), I::Push(2), I::Equal].into();
m.run();
assert_eq!(m.stack().peek(), 0);
}

#[test]
fn test_le_ge() {
let mut m: M = vec![I::Push(5), I::Push(2), I::LessThan].into();
m.run();
assert_eq!(m.stack().peek(), 1);

let mut m: M = vec![I::Push(2), I::Push(5), I::GreaterThan].into();
m.run();
assert_eq!(m.stack().peek(), 1);
}

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

let s = "hello";
let h_addr = ms.add_str(s);

let mut ins: Vec<I> = vec![];

for i in h_addr..h_addr + s.len() as u16 {
ins.push(I::Load(i));
}

m.mem.load_code(ins);
assert_eq!(m.mem.read_stack(5), [0, 0, 0, 0, 0]);

m.run();
assert_eq!(m.mem.read_stack(5), [111, 108, 108, 101, 104]);
}

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

let mut ms = m.mem.string();
let h_addr = ms.add_str("hello, ");
let w_addr = ms.add_str("world!");

m.mem.load_code(vec![I::Push(h_addr), I::Print, I::Push(w_addr), I::Print]);
m.run();

assert_eq!(m.mem.read_stack(2), [0, 0]);
}

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

let mut ms = m.mem.string();
let s_addr = ms.add_str("poom");

m.mem.load_code(vec![
// Push the string from the data section onto the stack.
I::LoadString(s_addr),

// TODO: manipulate the stack to reverse the string.
I::StartLoop,
I::Pop,
I::Push(0),
I::Equal,
I::JumpNotZero(0x09),
I::EndLoop(0x01),
]);

m.tick();
assert_eq!(m.mem.read_stack(5), [0, 109, 111, 111, 112]);
}
}
19 changes: 1 addition & 18 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1 @@
pub mod instructions;
pub mod machine;
pub mod mem;
pub mod register;

// use machine::Machine as M;
// use instructions::Instruction as I;
// use crate::machine::Execute;

fn main() {
// let mut m: M = vec![
// I::Push(1),
// I::Inc,
// ].into();
//
// m.run();
}

fn main() {}
2 changes: 2 additions & 0 deletions src/mem/string.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate snafu;

use snafu::{Whatever, whatever};
use crate::mem::{DATA_END, DATA_START, Memory};

Expand Down
28 changes: 28 additions & 0 deletions tests/comparison.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[cfg(test)]
mod tests {
extern crate opcodes_to_algorithms as O;

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

#[test]
fn test_eq() {
let mut m: M = vec![I::Push(10), I::Push(10), I::Equal].into();
m.run();
assert_eq!(m.stack().peek(), 1);

let mut m: M = vec![I::Push(5), I::Push(2), I::Equal].into();
m.run();
assert_eq!(m.stack().peek(), 0);
}

#[test]
fn test_le_ge() {
let mut m: M = vec![I::Push(5), I::Push(2), I::LessThan].into();
m.run();
assert_eq!(m.stack().peek(), 1);

let mut m: M = vec![I::Push(2), I::Push(5), I::GreaterThan].into();
m.run();
assert_eq!(m.stack().peek(), 1);
}
}
14 changes: 14 additions & 0 deletions tests/machine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#[cfg(test)]
mod tests {
extern crate opcodes_to_algorithms as O;

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

#[test]
fn test_run_machine() {
let mut m: M = vec![I::Push(10), I::Push(3), I::Sub].into();
m.run();

assert_eq!(m.mem.read_stack(2), [0, 7]);
}
}
24 changes: 24 additions & 0 deletions tests/math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#[cfg(test)]
mod tests {
extern crate opcodes_to_algorithms as O;

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

#[test]
fn test_add() {
let mut m: M = vec![I::Push(5), I::Push(10), I::Add, I::Push(3), I::Sub].into();

m.tick();
m.tick();
assert_eq!(m.mem.read_stack(3), [0, 10, 5]);

m.tick();
assert_eq!(m.mem.read_stack(3), [0, 0, 15]);

m.tick();
assert_eq!(m.stack().peek(), 3);

m.tick();
assert_eq!(m.mem.read_stack(3), [0, 0, 12]);
}
}
55 changes: 55 additions & 0 deletions tests/strings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#[cfg(test)]
mod tests {
extern crate opcodes_to_algorithms as O;

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

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

let s = "hello";
let h_addr = ms.add_str(s);

let mut ins: Vec<I> = vec![];

for i in h_addr..h_addr + s.len() as u16 {
ins.push(I::Load(i));
}

m.mem.load_code(ins);
assert_eq!(m.mem.read_stack(5), [0, 0, 0, 0, 0]);

m.run();
assert_eq!(m.mem.read_stack(5), [111, 108, 108, 101, 104]);
}

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

let mut ms = m.mem.string();
let h_addr = ms.add_str("hello, ");
let w_addr = ms.add_str("world!");

// TODO: assert that "Hello World" is printed.
// requires callbacks to the host.
m.mem.load_code(vec![I::Push(h_addr), I::Print, I::Push(w_addr), I::Print]);
m.run();

assert_eq!(m.mem.read_stack(2), [0, 0]);
}

/// TODO: manipulate the stack to reverse the string.
#[test]
fn reverse_string() {
let mut m = M::new();

let mut ms = m.mem.string();
let s_addr = ms.add_str("poom");
m.mem.load_code(vec![I::LoadString(s_addr)]);
m.tick();
assert_eq!(m.mem.read_stack(5), [0, 109, 111, 111, 112]);
}
}

0 comments on commit bb17688

Please sign in to comment.