]> Witch of Git - jade-mouse/blob - toolchain/src/inst/test.rs
Add more mid-byte underscores to the ISA spec
[jade-mouse] / toolchain / src / inst / test.rs
1 use super::{Cond, Decode, Encode, Inst, Reg};
2 use proptest::prelude::*;
3 use std::io::Cursor;
4
5 pub(super) fn cond_strategy(op: impl Fn(Reg, Reg) -> Cond) -> impl Strategy<Value = Cond> {
6 (0..4u8, 1..4u8).prop_map(move |(a, b)| op(reg_num(a), reg_num(a + b)))
7 }
8
9 fn reg_num(x: u8) -> Reg {
10 match x & 0x3 {
11 0 => Reg::R0,
12 1 => Reg::R1,
13 2 => Reg::R2,
14 3 => Reg::R3,
15 _ => unreachable!(),
16 }
17 }
18
19 proptest! {
20 #[test]
21 fn encoding_roundtrip(inst: Inst) {
22 let mut buffer = [0, 0];
23 let mut cursor = Cursor::new(&mut buffer[..]);
24 inst.encode(&mut cursor).expect("Should encode instruction");
25 cursor.set_position(0);
26 let out_inst = Inst::decode(&mut cursor)
27 .expect(&format!("Should decode instruction bytes {:?}", cursor.get_ref()));
28 prop_assert_eq!(inst, out_inst, "inst should round-trip the same");
29 }
30 }