Skip to content

Commit

Permalink
test: i32.rotr test case
Browse files Browse the repository at this point in the history
  • Loading branch information
nerodesu017 committed Jul 2, 2024
1 parent b980739 commit ef6c237
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions tests/arithmetic/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}

0 comments on commit ef6c237

Please sign in to comment.