]> Witch of Git - jade-mouse/blob - toolchain/src/inst/test.rs
efe1b5ed3c172e13dc0976a8527dd54296552d85
[jade-mouse] / toolchain / src / inst / test.rs
1 use super::{Cond, Decode, Encode, Inst, Reg};
2 use proptest::prelude::*;
3 use std::{fmt::Debug, io::Cursor};
4
5 pub(super) fn regpair<T: Debug>(op: impl Fn(Reg, Reg) -> T) -> impl Strategy<Value = T> {
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 let len = cursor.position();
26 cursor.set_position(0);
27 let out_inst = Inst::decode(&mut cursor)
28 .expect(&format!("Should decode instruction bytes {:?}", cursor.get_ref()));
29 let buffer = &buffer[..len as usize];
30 prop_assert_eq!(inst, out_inst, "inst should round-trip the same. Encoded as {:x?}", buffer);
31 }
32 }