diff --git a/tests/arithmetic/bitwise.rs b/tests/arithmetic/bitwise.rs index 2f338d78..a84a5e10 100644 --- a/tests/arithmetic/bitwise.rs +++ b/tests/arithmetic/bitwise.rs @@ -231,3 +231,52 @@ pub fn i32_bitwise_rotl() { assert_eq!(i32::MAX / 4 + 1, instance.invoke_func(0, (i32::MIN, 30))); assert_eq!(i32::MIN / 4 - 1, instance.invoke_func(0, (i32::MAX, 30))); } + +/// A simple function to test the i32.rotr bitwise operation +#[test_log::test] +pub fn i32_bitwise_rotr() { + use wasm::{validate, RuntimeInstance}; + + let wat = String::from(BASE_WAT).replace("{{0}}", "rotr"); + + let wasm_bytes = wat::parse_str(wat).unwrap(); + + let validation_info = validate(&wasm_bytes).expect("validation failed"); + + let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); + + assert_eq!(814187813, instance.invoke_func(0, (142_103_123, 4))); + assert_eq!(-261857977, instance.invoke_func(0, (391_248_921, 14))); + assert_eq!(601955006, instance.invoke_func(0, (1_203_910_012, 33))); + assert_eq!(-1090889033, instance.invoke_func(0, (2_113_189_231, 33))); + assert_eq!(1, instance.invoke_func(0, (i32::MIN, i32::MAX))); + + // Basic positive number + assert_eq!(4, instance.invoke_func(0, (8, 1))); + + // Rotating by 0 (no shift) + assert_eq!(-1, instance.invoke_func(0, (-1, 0))); + assert_eq!(1, instance.invoke_func(0, (1, 0))); + + // Shifting negative numbers + assert_eq!(i32::MAX - 3, instance.invoke_func(0, (-8, 1))); + assert_eq!(-1, instance.invoke_func(0, (-1, 1))); + + // Rotating by 31 + assert_eq!(-1, instance.invoke_func(0, (-1, 31))); + assert_eq!(1, instance.invoke_func(0, (i32::MIN, 31))); + assert_eq!(-2, instance.invoke_func(0, (i32::MAX, 31))); + + // Rotating by more than 31 + assert_eq!(-1, instance.invoke_func(0, (-1, 32))); + assert_eq!(1, instance.invoke_func(0, (1, 32))); + assert_eq!(-1, instance.invoke_func(0, (-1, 100))); + + // Minimum and maximum 32-bit integers + assert_eq!(i32::MAX / 2 + 1, instance.invoke_func(0, (i32::MIN, 1))); + assert_eq!(i32::MIN / 2 - 1, instance.invoke_func(0, (i32::MAX, 1))); + + // Shifting out all bits except sign + assert_eq!(2, instance.invoke_func(0, (i32::MIN, 30))); + assert_eq!(-3, instance.invoke_func(0, (i32::MAX, 30))); +}