diff options
Diffstat (limited to 'bytecode/src')
| -rw-r--r-- | bytecode/src/encoding.rs | 276 |
1 files changed, 261 insertions, 15 deletions
diff --git a/bytecode/src/encoding.rs b/bytecode/src/encoding.rs index 8f49ab6..7e9ede2 100644 --- a/bytecode/src/encoding.rs +++ b/bytecode/src/encoding.rs @@ -9,7 +9,7 @@ fn read_const<T: Read>(prog: &mut T) -> Result<Value, String> { match prog.read_exact(&mut buf) { Ok(()) => (), Err(e) => { - return Err(format!("error reading input: {}", e)); + return Err(format!("error reading input: {e}")); } }; Ok(Value(u64::from_le_bytes(buf))) @@ -20,7 +20,7 @@ fn read_local<T: Read>(prog: &mut T) -> Result<Local, String> { match prog.read_exact(&mut buf) { Ok(()) => (), Err(e) => { - return Err(format!("error reading input: {}", e)); + return Err(format!("error reading input: {e}")); } }; Ok(Local(buf[0])) @@ -123,48 +123,294 @@ pub fn decode<T: Read>(prog: &mut T) -> Result<Vec<Op>, String> { } } +#[cfg(test)] mod tests { use super::*; use Arg::*; use Op::*; + fn decode_vec(v: Vec<u8>) -> Vec<Op> { + decode(&mut v.as_slice()).expect("decode failed") + } + #[test] fn decode_mov_const() { assert_eq!( - Ok(vec![Mov(Local(0), Const(Value::from_int(10)))]), - decode(&mut vec![0x2, 0, 0x15, 0, 0, 0, 0, 0, 0, 0].as_slice()) + vec![Mov(Local(0), Const(Value::from_int(10)))], + decode_vec(vec![0x2, 0, 0x15, 0, 0, 0, 0, 0, 0, 0]) ); } #[test] fn decode_mov_local() { assert_eq!( - Ok(vec![Mov(Local(0), L(Local(1)))]), - decode(&mut vec![0, 0, 1].as_slice()), + vec![Mov(Local(0), L(Local(1)))], + decode_vec(vec![0, 0, 0x1]), + ); + } + + #[test] + fn decode_jmpif_local() { + assert_eq!( + vec![JmpIf(L(Local(0)), L(Local(1)))], + decode_vec(vec![0x4, 0, 0x1]) + ); + } + + #[test] + fn decode_jmpif_const() { + assert_eq!( + vec![JmpIf( + Const(Value::from_bool(true)), + Const(Value::from_int(10)), + )], + decode_vec(vec![ + 0x7, 0xa, 0, 0, 0, 0, 0, 0, 0, 0x15, 0, 0, 0, 0, 0, 0, 0 + ]) + ); + } + + #[test] + fn decode_jmp_local() { + assert_eq!(vec![Jmp(L(Local(0)))], decode_vec(vec![0x8, 0])); + } + + #[test] + fn decode_jmp_const() { + assert_eq!( + vec![Jmp(Const(Value::from_int(10)))], + decode_vec(vec![0xb, 0x15, 0, 0, 0, 0, 0, 0, 0]) + ); + } + + #[test] + fn decode_alloc_local() { + assert_eq!( + vec![Alloc(Local(0), L(Local(1)))], + decode_vec(vec![0xc, 0, 0x1]) + ); + } + + #[test] + fn decode_alloc_const() { + assert_eq!( + vec![Alloc(Local(0), Const(Value::from_int(10)))], + decode_vec(vec![0xe, 0, 0x15, 0, 0, 0, 0, 0, 0, 0]) ); } #[test] - fn decode_jmpif() { + fn decode_peek_local() { + assert_eq!( + vec![Peek(Local(0), L(Local(1)), L(Local(2)))], + decode_vec(vec![0x10, 0, 0x1, 0x2]) + ); + } + + #[test] + fn decode_peek_const() { + assert_eq!( + vec![Peek(Local(0), L(Local(1)), Const(Value::from_int(10)))], + decode_vec(vec![0x11, 0, 0x1, 0x15, 0, 0, 0, 0, 0, 0, 0]) + ); + } + + #[test] + fn decode_poke_local() { + assert_eq!( + vec![Poke(L(Local(0)), L(Local(1)), L(Local(2)))], + decode_vec(vec![0x14, 0, 0x1, 0x2]) + ); + } + + #[test] + fn decode_poke_const() { + assert_eq!( + vec![Poke( + Const(Value::from_int(10)), + L(Local(0)), + Const(Value::from_int(0)), + )], + decode_vec(vec![ + 0x17, 0x15, 0, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0, 0 + ]) + ); + } + + #[test] + fn decode_add_local() { + assert_eq!( + vec![Add(Local(0), L(Local(1)), L(Local(2)))], + decode_vec(vec![0x18, 0, 1, 2]) + ); + } + + #[test] + fn decode_add_const() { + assert_eq!( + vec![Add( + Local(0), + Const(Value::from_int(13)), + Const(Value::from_int(84)) + )], + decode_vec(vec![ + 0x1b, 0, 0x1b, 0, 0, 0, 0, 0, 0, 0, 0xa9, 0, 0, 0, 0, 0, 0, 0 + ]) + ); + } + + #[test] + fn decode_sub_local() { + assert_eq!( + vec![Sub(Local(0), L(Local(1)), L(Local(2)))], + decode_vec(vec![0x1c, 0, 1, 2]) + ); + } + + #[test] + fn decode_sub_const() { + assert_eq!( + vec![Sub( + Local(0), + Const(Value::from_int(13)), + Const(Value::from_int(84)) + )], + decode_vec(vec![ + 0x1f, 0, 0x1b, 0, 0, 0, 0, 0, 0, 0, 0xa9, 0, 0, 0, 0, 0, 0, 0 + ]) + ); + } + + #[test] + fn decode_mul_local() { + assert_eq!( + vec![Mul(Local(0), L(Local(1)), L(Local(2)))], + decode_vec(vec![0x20, 0, 1, 2]) + ); + } + + #[test] + fn decode_mul_const() { + assert_eq!( + vec![Mul( + Local(0), + Const(Value::from_int(13)), + Const(Value::from_int(84)) + )], + decode_vec(vec![ + 0x23, 0, 0x1b, 0, 0, 0, 0, 0, 0, 0, 0xa9, 0, 0, 0, 0, 0, 0, 0 + ]) + ); + } + + #[test] + fn decode_div_local() { + assert_eq!( + vec![Div(Local(0), L(Local(1)), L(Local(2)))], + decode_vec(vec![0x24, 0, 1, 2]) + ); + } + + #[test] + fn decode_div_const() { + assert_eq!( + vec![Div( + Local(0), + Const(Value::from_int(5)), + Const(Value::from_int(2)) + )], + decode_vec(vec![ + 0x27, 0, 0xb, 0, 0, 0, 0, 0, 0, 0, 0x5, 0, 0, 0, 0, 0, 0, 0 + ]) + ); + } + + #[test] + fn decode_mod_local() { + assert_eq!( + vec![Mod(Local(0), L(Local(1)), L(Local(2)))], + decode_vec(vec![0x28, 0, 1, 2]) + ); + } + + #[test] + fn decode_mod_const() { + assert_eq!( + vec![Mod( + Local(0), + Const(Value::from_int(5)), + Const(Value::from_int(2)) + )], + decode_vec(vec![ + 0x2b, 0, 0xb, 0, 0, 0, 0, 0, 0, 0, 0x5, 0, 0, 0, 0, 0, 0, 0 + ]) + ); + } + + #[test] + fn decode_peekbyte_local() { + assert_eq!( + vec![PeekByte(Local(0), L(Local(1)), L(Local(2)))], + decode_vec(vec![0x2c, 0, 1, 2]) + ); + } + + #[test] + fn decode_peekbyte_const() { + assert_eq!( + vec![PeekByte(Local(0), L(Local(1)), Const(Value::from_int(10)))], + decode_vec(vec![0x2d, 0, 1, 0x15, 0, 0, 0, 0, 0, 0, 0]) + ); + } + + #[test] + fn decode_pokebyte_local() { + assert_eq!( + vec![PokeByte(L(Local(0)), L(Local(1)), L(Local(2)))], + decode_vec(vec![0x30, 0, 1, 2]) + ); + } + + #[test] + fn decode_pokebyte_const() { + assert_eq!( + vec![PokeByte( + Const(Value::from_int(10)), + L(Local(0)), + Const(Value::from_int(0)) + )], + decode_vec(vec![ + 0x33, 0x15, 0, 0, 0, 0, 0, 0, 0, 0, 0x1, 0, 0, 0, 0, 0, 0, 0 + ]) + ); + } + + #[test] + fn decode_exit_local() { + assert_eq!(vec![Exit(L(Local(0)))], decode_vec(vec![0x34, 0])); + } + + #[test] + fn decode_exit_const() { assert_eq!( - Ok(vec![JmpIf(L(Local(0)), L(Local(1)))]), - decode(&mut vec![4, 0, 1].as_slice()) + vec![Exit(Const(Value::from_int(0)))], + decode_vec(vec![0x36, 0x1, 0, 0, 0, 0, 0, 0, 0]) ); } #[test] - fn decode_jmp() { + fn decode_alloc_bytevector_local() { assert_eq!( - Ok(vec![Jmp(L(Local(0)))]), - decode(&mut vec![8, 0].as_slice()) + vec![AllocBytevector(Local(0), L(Local(1)))], + decode_vec(vec![0x38, 0, 1]) ); } #[test] - fn decode_alloc() { + fn decode_alloc_bytevector_const() { assert_eq!( - Ok(vec![Alloc(Local(0), L(Local(1)))]), - decode(&mut vec![12, 0, 1].as_slice()) + vec![AllocBytevector(Local(0), Const(Value::from_int(10)))], + decode_vec(vec![0x3a, 0, 0x15, 0, 0, 0, 0, 0, 0, 0]) ); } } |
