Skip to content

Commit

Permalink
test: fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nerodesu017 committed Jul 1, 2024
1 parent e729f90 commit 095353d
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 24 deletions.
8 changes: 4 additions & 4 deletions examples/stuff/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
6 changes: 3 additions & 3 deletions tests/add_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
23 changes: 17 additions & 6 deletions tests/arithmetic/division.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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};

Expand All @@ -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)
);
}
10 changes: 5 additions & 5 deletions tests/arithmetic/multiply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
4 changes: 2 additions & 2 deletions tests/basic_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<i32, ()>(0, 42);
assert_eq!(42, instance.invoke_func(1, ()).unwrap());
}
4 changes: 2 additions & 2 deletions tests/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
2 changes: 1 addition & 1 deletion tests/start_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

0 comments on commit 095353d

Please sign in to comment.