Skip to content

Commit

Permalink
Add test for passing u64 values between plugin and host
Browse files Browse the repository at this point in the history
  • Loading branch information
kajacx committed May 10, 2023
1 parent a32fb73 commit 7c29c56
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/api/tests/import_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,48 @@ fn pass_i64_between_host_and_plugin() -> Result<(), String> {
Ok(())
}

#[universal_test]
fn pass_u64_between_host_and_plugin() -> Result<(), String> {
let mut store = Store::default();

let wat = r#"(module
(func $add_one_u64 (import "host" "add_one_u64") (param i64) (result i64))
(func (export "add_three_u64") (param i64) (result i64)
(i64.add (call $add_one_u64 (i64.add (local.get 0) (i64.const 1))) (i64.const 1))
)
)"#;
let module = Module::new(&store, wat).map_err(|e| format!("{e:?}"))?;

let imports = {
imports! {
"host" => {
"add_one_u64" => Function::new_typed(&mut store, |value: u64| value.wrapping_add(1)),
},
}
};

let instance = Instance::new(&mut store, &module, &imports).map_err(|e| format!("{e:?}"))?;
let add_three_u64 = instance
.exports
.get_typed_function::<u64, u64>(&store, "add_three_u64")
.map_err(|e| format!("{e:?}"))?;

let mut numbers = Vec::<u64>::new();
numbers.extend(0..=4);
numbers.extend((u64::MAX / 2 - 4)..=(u64::MAX / 2 + 4));
numbers.extend((u64::MAX - 4)..=u64::MAX);

for number in numbers {
let wasm_result = add_three_u64
.call(&mut store, number)
.map_err(|e| format!("{e:?}"))?;
let compare_result = number.wrapping_add(3);

assert_eq!(wasm_result, compare_result)
}
Ok(())
}

#[universal_test]
fn calling_function_exports() -> Result<()> {
let mut store = Store::default();
Expand Down

0 comments on commit 7c29c56

Please sign in to comment.