Skip to content

Commit

Permalink
test: i32.rem_u operation
Browse files Browse the repository at this point in the history
  • Loading branch information
nerodesu017 committed Jul 1, 2024
1 parent 96a62be commit af9d42d
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions tests/arithmetic/remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ const REM_S_WAT: &'static str = r#"
)
"#;

const REM_U_WAT: &'static str = r#"
(module
(func (export "rem_u") (param $divisor i32) (param $dividend i32) (result i32)
local.get $divisor
local.get $dividend
i32.rem_u)
)
"#;

/// A simple function to test signed remainder
#[test_log::test]
pub fn remainder_signed_simple() {
Expand Down Expand Up @@ -44,3 +53,49 @@ pub fn remainder_signed_panic_dividend_0() {
wasm::Error::RuntimeError(wasm::RuntimeError::DivideBy0)
);
}


/// A simple function to test unsigned remainder
#[test_log::test]
pub fn remainder_unsigned_simple() {
use wasm::{validate, RuntimeInstance};

let wat = REM_U_WAT;
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!(0, instance.invoke_func(0, (i32::MIN, 2)).unwrap());
assert_eq!(i32::MIN, instance.invoke_func(0, (i32::MIN, -2)).unwrap());
assert_eq!((i32::MIN + 2)*(-1), instance.invoke_func(0, (-2, i32::MIN)).unwrap());
assert_eq!(2, instance.invoke_func(0, (2, i32::MIN)).unwrap());
assert_eq!(i32::MAX, instance.invoke_func(0, (i32::MAX, i32::MIN)).unwrap());


assert_eq!(0, instance.invoke_func(0, (20, 2)).unwrap());
assert_eq!(999, instance.invoke_func(0, (10_000, 9_001)).unwrap());
assert_eq!(2, instance.invoke_func(0, (-20, 3)).unwrap());
assert_eq!(-20, instance.invoke_func(0, (-20, -3)).unwrap());
assert_eq!(20, instance.invoke_func(0, (20, -3)).unwrap());
assert_eq!(2, instance.invoke_func(0, (20, 3)).unwrap());
assert_eq!(i32::MIN, instance.invoke_func(0, (i32::MIN, -1)).unwrap());
assert_eq!(0, instance.invoke_func(0, (i32::MIN, 2)).unwrap());
}

/// A simple function to test signed remainder's RuntimeError when dividing by 0
#[test_log::test]
pub fn remainder_unsigned_panic_dividend_0() {
use wasm::{validate, RuntimeInstance};

let wat = REM_U_WAT;
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");

let result = instance.invoke_func::<(i32, i32), i32>(0, (222, 0));

assert_eq!(
result.unwrap_err(),
wasm::Error::RuntimeError(wasm::RuntimeError::DivideBy0)
);
}

0 comments on commit af9d42d

Please sign in to comment.