Skip to content

Commit

Permalink
do-core1: Move instruction unit tests to do-core
Browse files Browse the repository at this point in the history
And define a workspace.

Signed-off-by: Samuel Ortiz <[email protected]>
  • Loading branch information
Samuel Ortiz authored and Samuel Ortiz committed Feb 26, 2022
1 parent aaa36ec commit b25f5e8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 83 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ edition = "2018"
[dependencies]
clap = { version = "3.0.5", features = ["derive"] }
do-core = { path = "do-core" }

[workspace]
members = [
"do-core"
]
84 changes: 84 additions & 0 deletions do-core/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,87 @@ impl Instruction {
self.op1
}
}

#[cfg(test)]
mod tests {
use crate::instruction::{Instruction, OpCode};
use crate::Error;

#[test]
fn test_instruction_disassemble_add_r1_r3() -> Result<(), Error> {
let insn_bytes: u32 = 0x1842;
let insn = Instruction::disassemble(insn_bytes)?;

assert_eq!(insn.opcode, OpCode::ADD);
assert_eq!(insn.op0, 1);
assert_eq!(insn.op1, 3);

Ok(())
}

#[test]
fn test_instruction_disassemble_badop_r9_r1() -> Result<(), Error> {
// Use all 6 bytes for the opcode.
// It should be invalid for a while...
let insn_bytes: u32 = 0x067f;
assert!(Instruction::disassemble(insn_bytes).is_err());

Ok(())
}

#[test]
fn test_instruction_disassemble_add_r0_r10() -> Result<(), Error> {
let insn_bytes: u32 = 0x20a;
assert!(Instruction::disassemble(insn_bytes).is_err());

Ok(())
}

#[test]
fn test_instruction_disassemble_add_r7_r2() -> Result<(), Error> {
let insn_bytes: u32 = 0x11c2;
let insn = Instruction::disassemble(insn_bytes)?;

assert_eq!(insn.opcode, OpCode::ADD);
assert_eq!(insn.op0, 7);
assert_eq!(insn.op1, 2);

Ok(())
}

#[test]
fn test_instruction_disassemble_ldw_r0_r1() -> Result<(), Error> {
let insn_bytes: u32 = 0x0800;
let insn = Instruction::disassemble(insn_bytes)?;

assert_eq!(insn.opcode, OpCode::LDW);
assert_eq!(insn.op0, 0);
assert_eq!(insn.op1, 1);

Ok(())
}

#[test]
fn test_instruction_disassemble_xor_r2_r3() -> Result<(), Error> {
let insn_bytes: u32 = 0x1883;
let insn = Instruction::disassemble(insn_bytes)?;

assert_eq!(insn.opcode, OpCode::XOR);
assert_eq!(insn.op0, 2);
assert_eq!(insn.op1, 3);

Ok(())
}

#[test]
fn test_instruction_disassemble_stw_r5_r0() -> Result<(), Error> {
let insn_bytes: u32 = 0x0141;
let insn = Instruction::disassemble(insn_bytes)?;

assert_eq!(insn.opcode, OpCode::STW);
assert_eq!(insn.op0, 5);
assert_eq!(insn.op1, 0);

Ok(())
}
}
83 changes: 0 additions & 83 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,86 +56,3 @@ fn main() -> Result<(), Error> {

Ok(())
}

#[cfg(test)]
mod tests {
use crate::{Error, Instruction, OpCode};

#[test]
fn test_instruction_disassemble_add_r1_r3() -> Result<(), Error> {
let insn_bytes: u32 = 0x1842;
let insn = Instruction::disassemble(insn_bytes)?;

assert_eq!(insn.opcode, OpCode::ADD);
assert_eq!(insn.op0, 1);
assert_eq!(insn.op1, 3);

Ok(())
}

#[test]
fn test_instruction_disassemble_badop_r9_r1() -> Result<(), Error> {
// Use all 6 bytes for the opcode.
// It should be invalid for a while...
let insn_bytes: u32 = 0x067f;
assert!(Instruction::disassemble(insn_bytes).is_err());

Ok(())
}

#[test]
fn test_instruction_disassemble_add_r0_r10() -> Result<(), Error> {
let insn_bytes: u32 = 0x20a;
assert!(Instruction::disassemble(insn_bytes).is_err());

Ok(())
}

#[test]
fn test_instruction_disassemble_add_r7_r2() -> Result<(), Error> {
let insn_bytes: u32 = 0x11c2;
let insn = Instruction::disassemble(insn_bytes)?;

assert_eq!(insn.opcode, OpCode::ADD);
assert_eq!(insn.op0, 7);
assert_eq!(insn.op1, 2);

Ok(())
}

#[test]
fn test_instruction_disassemble_ldw_r0_r1() -> Result<(), Error> {
let insn_bytes: u32 = 0x0800;
let insn = Instruction::disassemble(insn_bytes)?;

assert_eq!(insn.opcode, OpCode::LDW);
assert_eq!(insn.op0, 0);
assert_eq!(insn.op1, 1);

Ok(())
}

#[test]
fn test_instruction_disassemble_xor_r2_r3() -> Result<(), Error> {
let insn_bytes: u32 = 0x1883;
let insn = Instruction::disassemble(insn_bytes)?;

assert_eq!(insn.opcode, OpCode::XOR);
assert_eq!(insn.op0, 2);
assert_eq!(insn.op1, 3);

Ok(())
}

#[test]
fn test_instruction_disassemble_stw_r5_r0() -> Result<(), Error> {
let insn_bytes: u32 = 0x0141;
let insn = Instruction::disassemble(insn_bytes)?;

assert_eq!(insn.opcode, OpCode::STW);
assert_eq!(insn.op0, 5);
assert_eq!(insn.op1, 0);

Ok(())
}
}

0 comments on commit b25f5e8

Please sign in to comment.