-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract unit tests to separate files
- Loading branch information
Showing
11 changed files
with
148 additions
and
147 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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::*; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} |