From 095353df1781bd8aae606d54d58ff5bb28003271 Mon Sep 17 00:00:00 2001 From: nerodesu017 <46645625+nerodesu017@users.noreply.github.com> Date: Mon, 1 Jul 2024 14:45:00 +0300 Subject: [PATCH] test: fixed tests --- examples/stuff/main.rs | 8 ++++---- src/lib.rs | 2 +- tests/add_one.rs | 6 +++--- tests/arithmetic/division.rs | 23 +++++++++++++++++------ tests/arithmetic/multiply.rs | 10 +++++----- tests/basic_memory.rs | 4 ++-- tests/globals.rs | 4 ++-- tests/start_function.rs | 2 +- 8 files changed, 35 insertions(+), 24 deletions(-) diff --git a/examples/stuff/main.rs b/examples/stuff/main.rs index edda53f7..149a2998 100644 --- a/examples/stuff/main.rs +++ b/examples/stuff/main.rs @@ -53,15 +53,15 @@ fn main() -> ExitCode { } }; - let twelve: i32 = instance.invoke_func(1, (5, 7)); + let twelve: i32 = instance.invoke_func(1, (5, 7)).unwrap(); assert_eq!(twelve, 12); - let twelve_plus_one: i32 = instance.invoke_func(0, twelve); + let twelve_plus_one: i32 = instance.invoke_func(0, twelve).unwrap(); assert_eq!(twelve_plus_one, 13); - instance.invoke_func::<_, ()>(2, 42_i32); + instance.invoke_func::<_, ()>(2, 42_i32).unwrap(); - assert_eq!(instance.invoke_func::<(), i32>(3, ()), 42_i32); + assert_eq!(instance.invoke_func::<(), i32>(3, ()).unwrap(), 42_i32); ExitCode::SUCCESS } diff --git a/src/lib.rs b/src/lib.rs index bfa31cd1..e3700955 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ extern crate alloc; #[macro_use] extern crate log; -pub use core::error::{Error, Result}; +pub use core::error::{Error, Result, RuntimeError}; pub use execution::*; pub use validation::*; diff --git a/tests/add_one.rs b/tests/add_one.rs index d8e766bd..b5108593 100644 --- a/tests/add_one.rs +++ b/tests/add_one.rs @@ -16,7 +16,7 @@ fn add_one() { let validation_info = validate(&wasm_bytes).expect("validation failed"); let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); - assert_eq!(12, instance.invoke_func(0, 11)); - assert_eq!(1, instance.invoke_func(0, 0)); - assert_eq!(-5, instance.invoke_func(0, -6)); + assert_eq!(12, instance.invoke_func(0, 11).unwrap()); + assert_eq!(1, instance.invoke_func(0, 0).unwrap()); + assert_eq!(-5, instance.invoke_func(0, -6).unwrap()); } diff --git a/tests/arithmetic/division.rs b/tests/arithmetic/division.rs index 35daf056..1a2e899a 100644 --- a/tests/arithmetic/division.rs +++ b/tests/arithmetic/division.rs @@ -18,13 +18,15 @@ pub fn division_signed_simple() { let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); - assert_eq!(10, instance.invoke_func(0, (20, 2))); - assert_eq!(9_001, instance.invoke_func(0, (81_018_001, 9_001))); + assert_eq!(10, instance.invoke_func(0, (20, 2)).unwrap()); + assert_eq!(9_001, instance.invoke_func(0, (81_018_001, 9_001)).unwrap()); + assert_eq!(-10, instance.invoke_func(0, (20, -2)).unwrap()); + assert_eq!(10, instance.invoke_func(0, (-20, -2)).unwrap()); + assert_eq!(-10, instance.invoke_func(0, (-20, 2)).unwrap()); } /// A simple function to test signed division's RuntimeError when dividing by 0 #[test_log::test] -#[should_panic(expected = "RuntimeError: divide by zero")] pub fn division_signed_panic_dividend_0() { use wasm::{validate, RuntimeInstance}; @@ -43,12 +45,16 @@ pub fn division_signed_panic_dividend_0() { let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); - instance.invoke_func::<(i32, i32), i32>(0, (222, 0)); + let result = instance.invoke_func::<(i32, i32), i32>(0, (222, 0)); + + assert_eq!( + result.unwrap_err(), + wasm::Error::RuntimeError(wasm::RuntimeError::DivideBy0) + ); } /// A simple function to test signed division's RuntimeError when we are dividing the i32 minimum by -1 (which gives an unrepresentable result - overflow) #[test_log::test] -#[should_panic(expected = "RuntimeError: divide result unrepresentable")] pub fn division_signed_panic_result_unrepresentable() { use wasm::{validate, RuntimeInstance}; @@ -67,5 +73,10 @@ pub fn division_signed_panic_result_unrepresentable() { let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); - instance.invoke_func::<(i32, i32), i32>(0, (-(2_i32.pow(31)), -1)); + let result = instance.invoke_func::<(i32, i32), i32>(0, (i32::MIN, -1)); + + assert_eq!( + result.unwrap_err(), + wasm::Error::RuntimeError(wasm::RuntimeError::UnrepresentableResult) + ); } diff --git a/tests/arithmetic/multiply.rs b/tests/arithmetic/multiply.rs index 0c9859e6..e2baf140 100644 --- a/tests/arithmetic/multiply.rs +++ b/tests/arithmetic/multiply.rs @@ -18,10 +18,10 @@ pub fn multiply() { let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); - assert_eq!(33, instance.invoke_func(0, 11)); - assert_eq!(0, instance.invoke_func(0, 0)); - assert_eq!(-30, instance.invoke_func(0, -10)); + assert_eq!(33, instance.invoke_func(0, 11).unwrap()); + assert_eq!(0, instance.invoke_func(0, 0).unwrap()); + assert_eq!(-30, instance.invoke_func(0, -10).unwrap()); - assert_eq!(i32::MAX - 5, instance.invoke_func(0, i32::MAX-1)); - assert_eq!(i32::MIN + 3, instance.invoke_func(0, i32::MIN+1)); + assert_eq!(i32::MAX - 5, instance.invoke_func(0, i32::MAX - 1).unwrap()); + assert_eq!(i32::MIN + 3, instance.invoke_func(0, i32::MIN + 1).unwrap()); } diff --git a/tests/basic_memory.rs b/tests/basic_memory.rs index a7ae37ba..5d84fbb6 100644 --- a/tests/basic_memory.rs +++ b/tests/basic_memory.rs @@ -20,6 +20,6 @@ fn basic_memory() { let validation_info = validate(&wasm_bytes).expect("validation failed"); let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); - let () = instance.invoke_func(0, 42); - assert_eq!(42, instance.invoke_func(1, ())); + let _ = instance.invoke_func::(0, 42); + assert_eq!(42, instance.invoke_func(1, ()).unwrap()); } diff --git a/tests/globals.rs b/tests/globals.rs index 181676df..7616d19f 100644 --- a/tests/globals.rs +++ b/tests/globals.rs @@ -28,8 +28,8 @@ fn globals() { let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); // Set global to 17. 3 is returned as previous (default) value. - assert_eq!(3, instance.invoke_func(0, 17)); + assert_eq!(3, instance.invoke_func(0, 17).unwrap()); // Now 17 will be returned when getting the global - assert_eq!(17, instance.invoke_func(1, ())); + assert_eq!(17, instance.invoke_func(1, ()).unwrap()); } diff --git a/tests/start_function.rs b/tests/start_function.rs index 50c3ab4e..d1a3e6bb 100644 --- a/tests/start_function.rs +++ b/tests/start_function.rs @@ -25,5 +25,5 @@ fn start_function() { let validation_info = validate(&wasm_bytes).expect("validation failed"); let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed"); - assert_eq!(42, instance.invoke_func(1, ())); + assert_eq!(42, instance.invoke_func(1, ()).unwrap()); }