mirror of
https://github.com/fluencelabs/parity-wasm
synced 2025-06-26 13:12:04 +00:00
add utest 'interpreter_accumulate_u8' iterating u8-array of length N
This commit is contained in:
BIN
res/cases/v1/accumulate_u8.wasm
Normal file
BIN
res/cases/v1/accumulate_u8.wasm
Normal file
Binary file not shown.
94
res/cases/v1/accumulate_u8.wast
Normal file
94
res/cases/v1/accumulate_u8.wast
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
;; /// @file accumulate_u8.cpp
|
||||||
|
;; #include <emscripten.h> // macro EMSCRIPTEN_KEEPALIVE
|
||||||
|
;; #include <stdint.h>
|
||||||
|
;; #include <vector>
|
||||||
|
;; #include <numeric>
|
||||||
|
;; extern "C" {
|
||||||
|
;; int32_t EMSCRIPTEN_KEEPALIVE accumulate_u8(const int32_t arlen, const uint8_t *ar) {
|
||||||
|
;; int32_t arsum = 0;
|
||||||
|
;; for (int32_t i=0; i<arlen; ++i)
|
||||||
|
;; arsum += (int32_t) ar[i];
|
||||||
|
;; return arsum;
|
||||||
|
;; }
|
||||||
|
;; } // extern "C"
|
||||||
|
(module
|
||||||
|
(type $0 (func (param i32 i32) (result i32)))
|
||||||
|
(type $1 (func))
|
||||||
|
(import "env" "memoryBase" (global $import$0 i32))
|
||||||
|
(import "env" "memory" (memory $0 256))
|
||||||
|
(import "env" "table" (table 0 anyfunc))
|
||||||
|
(import "env" "tableBase" (global $import$3 i32))
|
||||||
|
(global $global$0 (mut i32) (i32.const 0))
|
||||||
|
(global $global$1 (mut i32) (i32.const 0))
|
||||||
|
(export "__post_instantiate" (func $2))
|
||||||
|
(export "_accumulate_u8" (func $0))
|
||||||
|
(func $0 (type $0) (param $var$0 i32) (param $var$1 i32) (result i32)
|
||||||
|
(local $var$2 i32)
|
||||||
|
(local $var$3 i32)
|
||||||
|
(block $label$0 (result i32)
|
||||||
|
(if
|
||||||
|
(i32.gt_s
|
||||||
|
(get_local $var$0)
|
||||||
|
(i32.const 0)
|
||||||
|
)
|
||||||
|
(block $label$1
|
||||||
|
(set_local $var$2
|
||||||
|
(i32.const 0)
|
||||||
|
)
|
||||||
|
(set_local $var$3
|
||||||
|
(i32.const 0)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(block $label$2
|
||||||
|
(return
|
||||||
|
(i32.const 0)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(loop $label$3
|
||||||
|
(set_local $var$3
|
||||||
|
(i32.add
|
||||||
|
(i32.load8_u
|
||||||
|
(i32.add
|
||||||
|
(get_local $var$1)
|
||||||
|
(get_local $var$2)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(get_local $var$3)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(br_if $label$3
|
||||||
|
(i32.ne
|
||||||
|
(tee_local $var$2
|
||||||
|
(i32.add
|
||||||
|
(get_local $var$2)
|
||||||
|
(i32.const 1)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(get_local $var$0)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(get_local $var$3)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(func $1 (type $1)
|
||||||
|
(nop)
|
||||||
|
)
|
||||||
|
(func $2 (type $1)
|
||||||
|
(block $label$0
|
||||||
|
(set_global $global$0
|
||||||
|
(get_global $import$0)
|
||||||
|
)
|
||||||
|
(set_global $global$1
|
||||||
|
(i32.add
|
||||||
|
(get_global $global$0)
|
||||||
|
(i32.const 5242880)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
(call $1)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
;; custom section "dylink", size 5
|
||||||
|
)
|
||||||
|
|
@ -1,18 +1,18 @@
|
|||||||
use elements::deserialize_file;
|
use elements::deserialize_file;
|
||||||
use elements::Module;
|
use elements::Module;
|
||||||
use interpreter::{EnvParams, ExecutionParams, DefaultProgramInstance};
|
use interpreter::{EnvParams, ExecutionParams, DefaultProgramInstance};
|
||||||
use interpreter::module::ModuleInstanceInterface;
|
|
||||||
use interpreter::value::RuntimeValue;
|
use interpreter::value::RuntimeValue;
|
||||||
|
use interpreter::module::{ModuleInstanceInterface, ItemIndex};
|
||||||
// Name of function contained in WASM file (note the leading underline)
|
|
||||||
const FUNCTION_NAME: &'static str = "_inc_i32";
|
|
||||||
|
|
||||||
// The WASM file containing the module and function
|
|
||||||
const WASM_FILE: &str = &"res/cases/v1/inc_i32.wasm";
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn interpreter_inc_i32() {
|
fn interpreter_inc_i32() {
|
||||||
let program = DefaultProgramInstance::with_env_params(EnvParams {
|
// Name of function contained in WASM file (note the leading underline)
|
||||||
|
const FUNCTION_NAME: &'static str = "_inc_i32";
|
||||||
|
// The WASM file containing the module and function
|
||||||
|
const WASM_FILE: &str = &"res/cases/v1/inc_i32.wasm";
|
||||||
|
|
||||||
|
let program = DefaultProgramInstance::with_env_params(
|
||||||
|
EnvParams {
|
||||||
total_stack: 128 * 1024,
|
total_stack: 128 * 1024,
|
||||||
total_memory: 2 * 1024 * 1024,
|
total_memory: 2 * 1024 * 1024,
|
||||||
allow_memory_growth: false,
|
allow_memory_growth: false,
|
||||||
@ -25,11 +25,62 @@ fn interpreter_inc_i32() {
|
|||||||
let args = vec![RuntimeValue::I32(i32_val)];
|
let args = vec![RuntimeValue::I32(i32_val)];
|
||||||
let exp_retval = Some(RuntimeValue::I32(i32_val + 1));
|
let exp_retval = Some(RuntimeValue::I32(i32_val + 1));
|
||||||
let execution_params = ExecutionParams::from(args);
|
let execution_params = ExecutionParams::from(args);
|
||||||
let module = program
|
|
||||||
.add_module("main", module, None)
|
let module_result = program
|
||||||
|
.add_module("main", module, None);
|
||||||
|
|
||||||
|
let module = module_result
|
||||||
.expect("Failed to initialize module");
|
.expect("Failed to initialize module");
|
||||||
|
|
||||||
let retval = module
|
let retval = module
|
||||||
.execute_export(FUNCTION_NAME, execution_params)
|
.execute_export(FUNCTION_NAME, execution_params)
|
||||||
.expect("");
|
.expect("");
|
||||||
assert_eq!(exp_retval, retval);
|
assert_eq!(exp_retval, retval);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn interpreter_accumulate_u8() {
|
||||||
|
// Name of function contained in WASM file (note the leading underline)
|
||||||
|
const FUNCTION_NAME: &'static str = "_accumulate_u8";
|
||||||
|
// The WASM file containing the module and function
|
||||||
|
const WASM_FILE: &str = &"res/cases/v1/accumulate_u8.wasm";
|
||||||
|
// The octet sequence being accumulated
|
||||||
|
const BUF : &[u8] = & [9,8,7,6,5,4,3,2,1];
|
||||||
|
|
||||||
|
// Declare the memory limits of the runtime-environment
|
||||||
|
let program = DefaultProgramInstance::with_env_params(EnvParams {
|
||||||
|
total_stack: 128 * 1024,
|
||||||
|
total_memory: 2 * 1024 * 1024,
|
||||||
|
allow_memory_growth: false,
|
||||||
|
}).expect("Failed to instanciate program");
|
||||||
|
|
||||||
|
// Load the module-structure from wasm-file and add to program
|
||||||
|
let module: Module =
|
||||||
|
deserialize_file(WASM_FILE).expect("Failed to deserialize module from buffer");
|
||||||
|
let module = program
|
||||||
|
.add_module("main", module, None)
|
||||||
|
.expect("Failed to initialize module");
|
||||||
|
|
||||||
|
// => env module is created
|
||||||
|
let env_instance = program.module("env").unwrap();
|
||||||
|
// => linear memory is created
|
||||||
|
let env_memory = env_instance.memory(ItemIndex::Internal(0)).unwrap();
|
||||||
|
|
||||||
|
// Place the octet-sequence at index 0 in linear memory
|
||||||
|
let offset : u32 = 0;
|
||||||
|
let _ = env_memory.set(offset, BUF);
|
||||||
|
|
||||||
|
// Set up the function argument list and invoke the function
|
||||||
|
let args = vec![RuntimeValue::I32(BUF.len() as i32), RuntimeValue::I32(offset as i32)];
|
||||||
|
let execution_params = ExecutionParams::from(args);
|
||||||
|
let retval = module
|
||||||
|
.execute_export(FUNCTION_NAME, execution_params)
|
||||||
|
.expect("");
|
||||||
|
|
||||||
|
// For verification, repeat accumulation using native code
|
||||||
|
let accu = BUF.into_iter().fold(0 as i32, |a, b| a as i32 + *b as i32);
|
||||||
|
let exp_retval: Option<RuntimeValue> = Some(RuntimeValue::I32(accu));
|
||||||
|
|
||||||
|
// Verify calculation from WebAssembly runtime is identical to expected result
|
||||||
|
assert_eq!(exp_retval, retval);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user