Skip to content

Commit b25f5e8

Browse files
Samuel OrtizSamuel Ortiz
Samuel Ortiz
authored and
Samuel Ortiz
committedFeb 26, 2022
do-core1: Move instruction unit tests to do-core
And define a workspace. Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
1 parent aaa36ec commit b25f5e8

File tree

3 files changed

+89
-83
lines changed

3 files changed

+89
-83
lines changed
 

‎Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,8 @@ edition = "2018"
99
[dependencies]
1010
clap = { version = "3.0.5", features = ["derive"] }
1111
do-core = { path = "do-core" }
12+
13+
[workspace]
14+
members = [
15+
"do-core"
16+
]

‎do-core/src/instruction.rs

+84
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,87 @@ impl Instruction {
6363
self.op1
6464
}
6565
}
66+
67+
#[cfg(test)]
68+
mod tests {
69+
use crate::instruction::{Instruction, OpCode};
70+
use crate::Error;
71+
72+
#[test]
73+
fn test_instruction_disassemble_add_r1_r3() -> Result<(), Error> {
74+
let insn_bytes: u32 = 0x1842;
75+
let insn = Instruction::disassemble(insn_bytes)?;
76+
77+
assert_eq!(insn.opcode, OpCode::ADD);
78+
assert_eq!(insn.op0, 1);
79+
assert_eq!(insn.op1, 3);
80+
81+
Ok(())
82+
}
83+
84+
#[test]
85+
fn test_instruction_disassemble_badop_r9_r1() -> Result<(), Error> {
86+
// Use all 6 bytes for the opcode.
87+
// It should be invalid for a while...
88+
let insn_bytes: u32 = 0x067f;
89+
assert!(Instruction::disassemble(insn_bytes).is_err());
90+
91+
Ok(())
92+
}
93+
94+
#[test]
95+
fn test_instruction_disassemble_add_r0_r10() -> Result<(), Error> {
96+
let insn_bytes: u32 = 0x20a;
97+
assert!(Instruction::disassemble(insn_bytes).is_err());
98+
99+
Ok(())
100+
}
101+
102+
#[test]
103+
fn test_instruction_disassemble_add_r7_r2() -> Result<(), Error> {
104+
let insn_bytes: u32 = 0x11c2;
105+
let insn = Instruction::disassemble(insn_bytes)?;
106+
107+
assert_eq!(insn.opcode, OpCode::ADD);
108+
assert_eq!(insn.op0, 7);
109+
assert_eq!(insn.op1, 2);
110+
111+
Ok(())
112+
}
113+
114+
#[test]
115+
fn test_instruction_disassemble_ldw_r0_r1() -> Result<(), Error> {
116+
let insn_bytes: u32 = 0x0800;
117+
let insn = Instruction::disassemble(insn_bytes)?;
118+
119+
assert_eq!(insn.opcode, OpCode::LDW);
120+
assert_eq!(insn.op0, 0);
121+
assert_eq!(insn.op1, 1);
122+
123+
Ok(())
124+
}
125+
126+
#[test]
127+
fn test_instruction_disassemble_xor_r2_r3() -> Result<(), Error> {
128+
let insn_bytes: u32 = 0x1883;
129+
let insn = Instruction::disassemble(insn_bytes)?;
130+
131+
assert_eq!(insn.opcode, OpCode::XOR);
132+
assert_eq!(insn.op0, 2);
133+
assert_eq!(insn.op1, 3);
134+
135+
Ok(())
136+
}
137+
138+
#[test]
139+
fn test_instruction_disassemble_stw_r5_r0() -> Result<(), Error> {
140+
let insn_bytes: u32 = 0x0141;
141+
let insn = Instruction::disassemble(insn_bytes)?;
142+
143+
assert_eq!(insn.opcode, OpCode::STW);
144+
assert_eq!(insn.op0, 5);
145+
assert_eq!(insn.op1, 0);
146+
147+
Ok(())
148+
}
149+
}

‎src/main.rs

-83
Original file line numberDiff line numberDiff line change
@@ -56,86 +56,3 @@ fn main() -> Result<(), Error> {
5656

5757
Ok(())
5858
}
59-
60-
#[cfg(test)]
61-
mod tests {
62-
use crate::{Error, Instruction, OpCode};
63-
64-
#[test]
65-
fn test_instruction_disassemble_add_r1_r3() -> Result<(), Error> {
66-
let insn_bytes: u32 = 0x1842;
67-
let insn = Instruction::disassemble(insn_bytes)?;
68-
69-
assert_eq!(insn.opcode, OpCode::ADD);
70-
assert_eq!(insn.op0, 1);
71-
assert_eq!(insn.op1, 3);
72-
73-
Ok(())
74-
}
75-
76-
#[test]
77-
fn test_instruction_disassemble_badop_r9_r1() -> Result<(), Error> {
78-
// Use all 6 bytes for the opcode.
79-
// It should be invalid for a while...
80-
let insn_bytes: u32 = 0x067f;
81-
assert!(Instruction::disassemble(insn_bytes).is_err());
82-
83-
Ok(())
84-
}
85-
86-
#[test]
87-
fn test_instruction_disassemble_add_r0_r10() -> Result<(), Error> {
88-
let insn_bytes: u32 = 0x20a;
89-
assert!(Instruction::disassemble(insn_bytes).is_err());
90-
91-
Ok(())
92-
}
93-
94-
#[test]
95-
fn test_instruction_disassemble_add_r7_r2() -> Result<(), Error> {
96-
let insn_bytes: u32 = 0x11c2;
97-
let insn = Instruction::disassemble(insn_bytes)?;
98-
99-
assert_eq!(insn.opcode, OpCode::ADD);
100-
assert_eq!(insn.op0, 7);
101-
assert_eq!(insn.op1, 2);
102-
103-
Ok(())
104-
}
105-
106-
#[test]
107-
fn test_instruction_disassemble_ldw_r0_r1() -> Result<(), Error> {
108-
let insn_bytes: u32 = 0x0800;
109-
let insn = Instruction::disassemble(insn_bytes)?;
110-
111-
assert_eq!(insn.opcode, OpCode::LDW);
112-
assert_eq!(insn.op0, 0);
113-
assert_eq!(insn.op1, 1);
114-
115-
Ok(())
116-
}
117-
118-
#[test]
119-
fn test_instruction_disassemble_xor_r2_r3() -> Result<(), Error> {
120-
let insn_bytes: u32 = 0x1883;
121-
let insn = Instruction::disassemble(insn_bytes)?;
122-
123-
assert_eq!(insn.opcode, OpCode::XOR);
124-
assert_eq!(insn.op0, 2);
125-
assert_eq!(insn.op1, 3);
126-
127-
Ok(())
128-
}
129-
130-
#[test]
131-
fn test_instruction_disassemble_stw_r5_r0() -> Result<(), Error> {
132-
let insn_bytes: u32 = 0x0141;
133-
let insn = Instruction::disassemble(insn_bytes)?;
134-
135-
assert_eq!(insn.opcode, OpCode::STW);
136-
assert_eq!(insn.op0, 5);
137-
assert_eq!(insn.op1, 0);
138-
139-
Ok(())
140-
}
141-
}

0 commit comments

Comments
 (0)