]> Witch of Git - jade-rose/blob - toolchain/src/inst/test.rs
Add an instruction parser
[jade-rose] / toolchain / src / inst / test.rs
1 use super::{parse, encode::Encode};
2 use std::io::Cursor;
3
4 fn display(buf: &[u8]) -> String {
5 println!("{}", buf.len());
6 match buf.len() {
7 1 => format!("{:04b}_{:04b}", buf[0] >> 4, buf[0] & 0xf),
8 2 => format!("{:04b}_{:04b} {:04b}_{:04b}", buf[0] >> 4, buf[0] & 0xf, buf[1] >> 4, buf[1] & 0xf),
9 n => unreachable!("Shouldn't be instructions of length {}", n),
10 }
11 }
12
13 macro_rules! test_parse {
14 ($($txt:expr => $res:expr),* $(,)?) => {
15 $(
16 let mut buf = [0, 0];
17 let mut cursor = Cursor::new(&mut buf[..]);
18 let parsed = parse::parse_inst($txt).expect(&format!("parsed {:?}", $txt));
19 parsed.encode(&mut cursor).expect(&format!("encoded {:x?}", parsed));
20 let pos = cursor.position() as usize;
21 assert_eq!($res, &display(&buf[..pos]));
22 )*
23 }
24 }
25
26 #[test]
27 fn trap() {
28 test_parse! {
29 "TRAP" => "0000_0000",
30 "trap" => "0000_0000",
31 }
32 }
33
34 #[test]
35 fn jump_call() {
36 test_parse! {
37 "JABS" => "0000_0100",
38 "CABS" => "0000_0101",
39 "JOFF" => "0000_0110",
40 "COFF" => "0000_0111",
41 }
42 }
43