From 3067358a336e6a4b0c0b51dda8b6e40b4b1dc026 Mon Sep 17 00:00:00 2001 From: Brian Carroll Date: Sun, 9 Jan 2022 11:11:37 +0000 Subject: [PATCH] Wasm: test for LEB-128 decoder --- compiler/gen_wasm/src/wasm_module/serialize.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/compiler/gen_wasm/src/wasm_module/serialize.rs b/compiler/gen_wasm/src/wasm_module/serialize.rs index e7c8dead3e..0c948fba73 100644 --- a/compiler/gen_wasm/src/wasm_module/serialize.rs +++ b/compiler/gen_wasm/src/wasm_module/serialize.rs @@ -436,4 +436,18 @@ mod tests { &[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7f], ); } + + #[test] + fn test_decode_u32() { + assert_eq!(decode_u32(&[0]), Ok((0, 1))); + assert_eq!(decode_u32(&[64]), Ok((64, 1))); + assert_eq!(decode_u32(&[0x7f]), Ok((0x7f, 1))); + assert_eq!(decode_u32(&[0x80, 0x01]), Ok((0x80, 2))); + assert_eq!(decode_u32(&[0xff, 0x7f]), Ok((0x3fff, 2))); + assert_eq!(decode_u32(&[0x80, 0x80, 0x01]), Ok((0x4000, 3))); + assert_eq!(decode_u32(&[0xff, 0xff, 0xff, 0xff, 0x0f]), Ok((u32::MAX, 5))); + assert!(matches!(decode_u32(&[0x80; 6]), Err(_))); + assert!(matches!(decode_u32(&[0x80; 2]), Err(_))); + assert!(matches!(decode_u32(&[]), Err(_))); + } }