Skip to content

Commit

Permalink
test: i32.shr_u test case
Browse files Browse the repository at this point in the history
  • Loading branch information
nerodesu017 committed Jul 2, 2024
1 parent 72c0b65 commit 7c819c2
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion tests/arithmetic/bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,56 @@ pub fn i32_bitwise_shr_s() {
// Shifting out all bits except sign
assert_eq!(-2, instance.invoke_func(0, (i32::MIN, 30)));
assert_eq!(1, instance.invoke_func(0, (i32::MAX, 30)));
// assert_eq!(, instance.invoke_func(0, (,)));
}

/// A simple function to test the i32.shr_u bitwise operation
#[test_log::test]
pub fn i32_bitwise_shr_u() {
use wasm::{validate, RuntimeInstance};

let wat = String::from(BASE_WAT).replace("{{0}}", "shr_u");

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!(8881445, instance.invoke_func(0, (142_103_123, 4)));
assert_eq!(23879, instance.invoke_func(0, (391_248_921, 14)));
assert_eq!(601955006, instance.invoke_func(0, (1_203_910_012, 33)));
assert_eq!(1056594615, 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)));

// Shifting 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!(i32::MAX, instance.invoke_func(0, (-1, 1)));

// Shifting by 31 (maximum shift for 32-bit int)
assert_eq!(1, instance.invoke_func(0, (-1, 31)));
assert_eq!(1, instance.invoke_func(0, (i32::MIN, 31)));
assert_eq!(0, instance.invoke_func(0, (i32::MAX, 31)));

// Shifting by more than 31
assert_eq!(-1, instance.invoke_func(0, (-1, 32)));
assert_eq!(1, instance.invoke_func(0, (1, 32)));
assert_eq!(268435455, instance.invoke_func(0, (-1, 100)));

// Minimum and maximum 32-bit integers
assert_eq!(
(i32::MIN / 2) * (-1),
instance.invoke_func(0, (i32::MIN, 1))
);
assert_eq!(i32::MAX / 2, 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!(1, instance.invoke_func(0, (i32::MAX, 30)));
}

0 comments on commit 7c819c2

Please sign in to comment.