Skip to content

Commit

Permalink
Add e2e tests for calling exported functions with lots of params and …
Browse files Browse the repository at this point in the history
…results (#958)

add e2e tests with lots of params and results
  • Loading branch information
Robbepop authored Mar 19, 2024
1 parent c0505f2 commit 5ad0b30
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
45 changes: 45 additions & 0 deletions crates/wasmi/src/engine/tests/many_inout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use crate::{Engine, Func, Linker, Module, Store, Value};
use std::vec::Vec;

/// Converts the given `.wat` into `.wasm`.
fn wat2wasm(wat: &str) -> Vec<u8> {
wat::parse_str(wat).unwrap()
}

/// Common routine to setup the tests.
fn setup_test(wat: &str) -> (Store<()>, Func) {
let wasm = wat2wasm(wat);
let engine = Engine::default();
let mut store = <Store<()>>::new(&engine, ());
let linker = <Linker<()>>::new(&engine);
let module = Module::new(&engine, &wasm[..]).unwrap();
let instance = linker
.instantiate(&mut store, &module)
.unwrap()
.ensure_no_start(&mut store)
.unwrap();
let func = instance.get_func(&store, "test").unwrap();
(store, func)
}

#[test]
fn many_params() {
let wat = include_str!("wat/many_params.wat");
let (mut store, func) = setup_test(wat);
func.call(&mut store, &[0; 150].map(Value::I32), &mut [])
.unwrap();
}

#[test]
fn many_results() {
let wat = include_str!("wat/many_results.wat");
let (mut store, func) = setup_test(wat);
let mut results = [0; 150].map(Value::I32);
func.call(&mut store, &[], &mut results).unwrap();
for (i, result) in results.iter().enumerate() {
let &Value::I32(result) = result else {
panic!("unexpected result type at index {i}: {result:?}");
};
assert!(result as usize == i % 10);
}
}
1 change: 1 addition & 0 deletions crates/wasmi/src/engine/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod host_calls;
mod many_inout;
23 changes: 23 additions & 0 deletions crates/wasmi/src/engine/tests/wat/many_params.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(module
(func (export "test")
(param
;; Define 150 i32 params
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
)
;; do nothing
)
)
31 changes: 31 additions & 0 deletions crates/wasmi/src/engine/tests/wat/many_results.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(module
(func (export "test")
(result
;; Define 150 i32 results
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
i32 i32 i32 i32 i32 i32 i32 i32 i32 i32
)
(call $return10) (call $return10) (call $return10) (call $return10) (call $return10)
(call $return10) (call $return10) (call $return10) (call $return10) (call $return10)
(call $return10) (call $return10) (call $return10) (call $return10) (call $return10)
)

(func $return10
(result i32 i32 i32 i32 i32 i32 i32 i32 i32 i32)
(i32.const 0) (i32.const 1) (i32.const 2) (i32.const 3) (i32.const 4)
(i32.const 5) (i32.const 6) (i32.const 7) (i32.const 8) (i32.const 9)
)
)

0 comments on commit 5ad0b30

Please sign in to comment.