Skip to content

Commit

Permalink
test: add validation and execution tests for typed/polymorphic select
Browse files Browse the repository at this point in the history
Signed-off-by: Cem Onem <[email protected]>
  • Loading branch information
Cem Onem committed Dec 18, 2024
1 parent 369203c commit 10f23c6
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
69 changes: 69 additions & 0 deletions tests/select.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use wasm::{validate, RuntimeInstance};

const SELECT_TEST: &str = r#"
(module
(func $select_test (param $num i32) (result i32)
(if (result i32)
(i32.le_s
(local.get $num)
(i32.const 1)
)
(then
(select {{TYPE_1}}
(i32.const 8)
(i32.const 4)
(local.get $num)
)
)
(else
(i32.wrap_i64
(select {{TYPE_2}}
(i64.const 16)
(i64.const 15)
(i32.sub (local.get $num) (i32.const 2))
)
)
)
)
)
(export "select_test" (func $select_test))
)"#;

#[test_log::test]
fn polymorphic_select_test() {
let wat = String::from(SELECT_TEST)
.replace("{{TYPE_1}}", "")
.replace("{{TYPE_2}}", "");
let wasm_bytes = wat::parse_str(wat).unwrap();
validate(&wasm_bytes).expect("validation failed");

let validation_info = validate(&wasm_bytes).expect("validation failed");
let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed");

let select_test_fn = instance.get_function_by_index(0, 0).unwrap();

assert_eq!(4, instance.invoke(&select_test_fn, 0).unwrap());
assert_eq!(8, instance.invoke(&select_test_fn, 1).unwrap());
assert_eq!(15, instance.invoke(&select_test_fn, 2).unwrap());
assert_eq!(16, instance.invoke(&select_test_fn, 3).unwrap());
}

#[test_log::test]
fn typed_select_test() {
let wat = String::from(SELECT_TEST)
.replace("{{TYPE_1}}", "(result i32)")
.replace("{{TYPE_2}}", "(result i64)");
let wasm_bytes = wat::parse_str(wat).unwrap();
validate(&wasm_bytes).expect("validation failed");

let validation_info = validate(&wasm_bytes).expect("validation failed");
let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed");

let select_test_fn = instance.get_function_by_index(0, 0).unwrap();

assert_eq!(4, instance.invoke(&select_test_fn, 0).unwrap());
assert_eq!(8, instance.invoke(&select_test_fn, 1).unwrap());
assert_eq!(15, instance.invoke(&select_test_fn, 2).unwrap());
assert_eq!(16, instance.invoke(&select_test_fn, 3).unwrap());
}
36 changes: 36 additions & 0 deletions tests/structured_control_flow/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,39 @@ fn switch_case() {
assert_eq!(9, instance.invoke(&switch_case_fn, 4).unwrap());
assert_eq!(9, instance.invoke(&switch_case_fn, 7).unwrap());
}

const POLYMORPHIC_SELECT_VALIDATION: &str = r#"
(module
(func $polymorphic_select_validation
return
select
select
{{NUM_OR_VEC_INSTR1}}
drop
{{NUM_OR_VEC_INSTR2}}
drop
drop
drop
drop
drop
drop
)
)"#;

#[test_log::test]
fn polymorphic_select_validation1() {
let wat = String::from(POLYMORPHIC_SELECT_VALIDATION)
.replace("{{NUM_OR_VEC_INSTR1}}", "i32.eqz")
.replace("{{NUM_OR_VEC_INSTR2}}", "i64.eqz");
let wasm_bytes = wat::parse_str(wat).unwrap();
validate(&wasm_bytes).expect("validation failed");
}

#[test_log::test]
fn polymorphic_select_validation2() {
let wat = String::from(POLYMORPHIC_SELECT_VALIDATION)
.replace("{{NUM_OR_VEC_INSTR1}}", "i64.eqz")
.replace("{{NUM_OR_VEC_INSTR2}}", "i32.eqz");
let wasm_bytes = wat::parse_str(wat).unwrap();
validate(&wasm_bytes).expect("validation failed");
}

0 comments on commit 10f23c6

Please sign in to comment.