Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
koute committed Jan 11, 2024
1 parent 170d1bf commit fe4f77a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
14 changes: 13 additions & 1 deletion crates/polkavm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,13 @@ impl TestInstance {

let engine = Engine::new(config).unwrap();
let module = Module::from_blob(&engine, &Default::default(), &blob).unwrap();
let linker = Linker::new(&engine);
let mut linker = Linker::new(&engine);
linker
.func_wrap("multiply_by_2", |_caller: Caller<()>, value: u32| -> Result<u32, Trap> {
Ok(value * 2)
})
.unwrap();

let instance_pre = linker.instantiate_pre(&module).unwrap();
let instance = instance_pre.instantiate().unwrap();

Expand Down Expand Up @@ -532,6 +538,11 @@ fn test_blob_atomic_fetch_minmax(config: Config) {
}
}

fn test_blob_hostcall(config: Config) {
let i = TestInstance::new(&config);
assert_eq!(i.call::<(u32,), u32>("test_multiply_by_6", (10,)).unwrap(), 60);
}

fn basic_gas_metering(config: Config, gas_metering_kind: GasMeteringKind) {
let _ = env_logger::try_init();

Expand Down Expand Up @@ -678,6 +689,7 @@ run_tests! {
test_blob_atomic_fetch_add
test_blob_atomic_fetch_swap
test_blob_atomic_fetch_minmax
test_blob_hostcall

basic_gas_metering_sync
basic_gas_metering_async
Expand Down
26 changes: 26 additions & 0 deletions guest-programs/test-blob/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,29 @@ extern "C" fn atomic_fetch_min_unsigned(value: u32) -> u32 {
output
}
}

// Test that an unused import will be stripped.
#[polkavm_derive::polkavm_import]
extern "C" {
fn unused_import(value: u32) -> u32;
}

// Test duplicate imports.
mod a {
#[polkavm_derive::polkavm_import]
extern "C" {
pub fn multiply_by_2(value: u32) -> u32;
}
}

mod b {
#[polkavm_derive::polkavm_import]
extern "C" {
pub fn multiply_by_2(value: u32) -> u32;
}
}

#[polkavm_derive::polkavm_export]
extern "C" fn test_multiply_by_6(value: u32) -> u32 {
unsafe { a::multiply_by_2(value * 3) }
}
Binary file modified test-data/test-blob.elf.zst
Binary file not shown.

0 comments on commit fe4f77a

Please sign in to comment.