Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wasm-forge committed Oct 7, 2023
1 parent f594448 commit 9964203
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn get_replacement_module_id(module: &walrus::Module, import_item: &walrus::Impo
log::debug!("Function replacement found: {:?} -> {:?}.",
module.funcs.get(fn_id).name,
module.funcs.get(fun.id()).name);

assert_eq!(module.funcs.get(fn_id).ty(), module.funcs.get(fun.id()).ty());

return Some(fun.id());
Expand Down Expand Up @@ -77,7 +78,6 @@ fn gather_replacement_ids(m: &walrus::Module) -> HashMap<FunctionId, FunctionId>
},

walrus::ImportKind::Table(_) => todo!(),

walrus::ImportKind::Memory(_) => {},
walrus::ImportKind::Global(_) => {},
}
Expand Down
59 changes: 59 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,62 @@ fn test_remove_start_export() {
assert!(None == export_found);

}



#[test]
fn test_gather_replacement_ids() {

let wat = r#"
(module
(type (;0;) (func))
(type (;1;) (func (param i32)))
(type (;2;) (func (param i32 i32)))
(type (;3;) (func (param i32 i32) (result i32)))
(type (;4;) (func (param i32 i32 i32)))
(type (;5;) (func (param i32 i32 i32 i32) (result i32)))
(import "ic0" "debug_print" (func $_dprint (;0;) (type 2)))
(import "ic0" "msg_reply" (func $_msg_reply (;1;) (type 0)))
(import "wasi_snapshot_preview1" "fd_write" (func $_wasi_snapshot_preview_fd_write (;2;) (type 5)))
(import "wasi_snapshot_preview1" "random_get" (func $_wasi_snapshot_preview_random_get (;3;) (type 3)))
(import "wasi_snapshot_preview1" "environ_get" (func $__imported_wasi_snapshot_preview1_environ_get (;4;) (type 3)))
(import "wasi_snapshot_preview1" "proc_exit" (func $__imported_wasi_snapshot_preview1_proc_exit (;5;) (type 1)))
(func $_start (;6;) (type 0)
i32.const 1
i32.const 2
call $__ic_custom_random_get
i32.const 1
i32.const 2
call $_wasi_snapshot_preview_random_get
i32.const 4
i32.const 5
call $__ic_custom_fd_write
drop
)
(func $__ic_custom_random_get (;7;) (type 3) (param i32 i32) (result i32)
call $_msg_reply
i32.const 421
)
(func $__ic_custom_fd_write (;8;) (type 5) (param i32 i32 i32 i32) (result i32)
i32.const 0
i32.const 0
call $_dprint
i32.const 42
)
(export "_start" (func $_start))
)
"#;

let binary = wat::parse_str(wat).unwrap();
let module = walrus::Module::from_buffer(&binary).unwrap();

let id_reps: HashMap<usize, usize> = gather_replacement_ids(&module).iter().map(|(x, y)| (x.index(), y.index())).collect();

assert!(id_reps[&2] == 8);
assert!(id_reps[&3] == 7);

}

0 comments on commit 9964203

Please sign in to comment.