Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
835f6f86cc | ||
|
|
a5192366ed |
No files matched your search
@@ -24,6 +24,7 @@ if not exist "%LIB%" mkdir "%LIB%"
|
||||
-Fu"%SRC%\wasm\test" ^
|
||||
-Fu"%SRC%\wasm\test\opcodes" ^
|
||||
-Fu"%SRC%\wasm\test\parsers" ^
|
||||
-Fu"%SRC%\wasm\test\binaries" ^
|
||||
-Fu"%SRC%\wasm\test\infra" ^
|
||||
-Fu"%SRC%\wasm\types" ^
|
||||
-Fu"%SRC%\wasm\vm" ^
|
||||
|
||||
@@ -9,7 +9,7 @@ uses
|
||||
|
||||
wasm.types,
|
||||
wasm.parser,
|
||||
wasm.vm, wasm.test.binary,
|
||||
wasm.vm, wasm.test.binary.return42,
|
||||
wasm.types.stack
|
||||
{$IFDEF RUN_TESTS}
|
||||
, wasm.test
|
||||
@@ -30,7 +30,7 @@ begin
|
||||
wasm.vm.init();
|
||||
|
||||
writestringln('[main] Parsing WASM Binary');
|
||||
Context:= wasm.parser.parse(@wasm.test.binary.TEST_BINARY_1[0], puint8(@wasm.test.binary.TEST_BINARY_1[0] + $26));
|
||||
Context:= wasm.parser.parse(@wasm.test.binary.return42.BINARY[0], puint8(@wasm.test.binary.return42.BINARY[0] + wasm.test.binary.return42.BINARY_SIZE));
|
||||
|
||||
writestringln('[main] Running...');
|
||||
Context^.ExecutionState.Running:= true;
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
{ E2E test: i32.const 10 + i32.const 32 = 42
|
||||
Equivalent WAT:
|
||||
(module
|
||||
(func (export "main") (result i32)
|
||||
i32.const 10
|
||||
i32.const 32
|
||||
i32.add
|
||||
return
|
||||
)
|
||||
)
|
||||
}
|
||||
unit wasm.test.binary.addnums;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
types, console, wasm.types, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
const
|
||||
BINARY_SIZE = 41;
|
||||
BINARY : Array[$00..$28] of uint8 = (
|
||||
$00, $61, $73, $6D, { magic }
|
||||
$01, $00, $00, $00, { version 1 }
|
||||
$01, $05, $01, $60, { type section: 1 func type }
|
||||
$00, $01, $7F, { () -> (i32) }
|
||||
$03, $02, $01, $00, { function section: 1 func, type 0 }
|
||||
$07, $08, $01, $04, { export section: 1 export, name len 4 }
|
||||
$6D, $61, $69, $6E, { "main" }
|
||||
$00, $00, { export type=func, index=0 }
|
||||
$0A, $0A, $01, $08, { code section: len=10, count=1, body=8 }
|
||||
$00, { 0 locals }
|
||||
$41, $0A, { i32.const 10 }
|
||||
$41, $20, { i32.const 32 }
|
||||
$6A, { i32.add }
|
||||
$0F, $0B { return, end }
|
||||
);
|
||||
|
||||
procedure run;
|
||||
var
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('binary.addnums');
|
||||
|
||||
ctx := wasm.parser.parse(@BINARY[0], puint8(@BINARY[0] + BINARY_SIZE));
|
||||
assert_true('valid binary', ctx^.ValidBinary);
|
||||
|
||||
ctx^.ExecutionState.Running := true;
|
||||
while wasm.vm.tick(ctx) do;
|
||||
|
||||
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
|
||||
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
|
||||
assert_i32('10+32=42', popi32(ctx^.ExecutionState.Operand_Stack), 42);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,66 @@
|
||||
{ E2E test: (2 + 3) * 4 = 20
|
||||
Equivalent WAT:
|
||||
(module
|
||||
(func (export "main") (result i32)
|
||||
i32.const 2
|
||||
i32.const 3
|
||||
i32.add
|
||||
i32.const 4
|
||||
i32.mul
|
||||
return
|
||||
)
|
||||
)
|
||||
}
|
||||
unit wasm.test.binary.arithmetic;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
types, console, wasm.types, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
const
|
||||
BINARY_SIZE = 44;
|
||||
BINARY : Array[$00..$2B] of uint8 = (
|
||||
$00, $61, $73, $6D, { magic }
|
||||
$01, $00, $00, $00, { version 1 }
|
||||
$01, $05, $01, $60, { type section: 1 func type }
|
||||
$00, $01, $7F, { () -> (i32) }
|
||||
$03, $02, $01, $00, { function section: 1 func, type 0 }
|
||||
$07, $08, $01, $04, { export section: 1 export, name len 4 }
|
||||
$6D, $61, $69, $6E, { "main" }
|
||||
$00, $00, { export type=func, index=0 }
|
||||
$0A, $0D, $01, $0B, { code section: len=13, count=1, body=11 }
|
||||
$00, { 0 locals }
|
||||
$41, $02, { i32.const 2 }
|
||||
$41, $03, { i32.const 3 }
|
||||
$6A, { i32.add }
|
||||
$41, $04, { i32.const 4 }
|
||||
$6C, { i32.mul }
|
||||
$0F, $0B { return, end }
|
||||
);
|
||||
|
||||
procedure run;
|
||||
var
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('binary.arithmetic');
|
||||
|
||||
ctx := wasm.parser.parse(@BINARY[0], puint8(@BINARY[0] + BINARY_SIZE));
|
||||
assert_true('valid binary', ctx^.ValidBinary);
|
||||
|
||||
ctx^.ExecutionState.Running := true;
|
||||
while wasm.vm.tick(ctx) do;
|
||||
|
||||
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
|
||||
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
|
||||
assert_i32('(2+3)*4=20', popi32(ctx^.ExecutionState.Operand_Stack), 20);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,74 @@
|
||||
{ E2E test: use local variables, local.set 0 := 10, local.set 1 := 20,
|
||||
return local.get 0 + local.get 1 = 30
|
||||
Equivalent WAT:
|
||||
(module
|
||||
(func (export "main") (result i32) (local i32 i32)
|
||||
i32.const 10
|
||||
local.set 0
|
||||
i32.const 20
|
||||
local.set 1
|
||||
local.get 0
|
||||
local.get 1
|
||||
i32.add
|
||||
return
|
||||
)
|
||||
)
|
||||
}
|
||||
unit wasm.test.binary.locals;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
types, console, wasm.types, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
const
|
||||
BINARY_SIZE = 51;
|
||||
BINARY : Array[$00..$32] of uint8 = (
|
||||
$00, $61, $73, $6D, { magic }
|
||||
$01, $00, $00, $00, { version 1 }
|
||||
$01, $05, $01, $60, { type section: 1 func type }
|
||||
$00, $01, $7F, { () -> (i32) }
|
||||
$03, $02, $01, $00, { function section: 1 func, type 0 }
|
||||
$07, $08, $01, $04, { export section: 1 export, name len 4 }
|
||||
$6D, $61, $69, $6E, { "main" }
|
||||
$00, $00, { export type=func, index=0 }
|
||||
$0A, $14, $01, $12, { code section: len=20, count=1, body=18 }
|
||||
$01, $02, $7F, { 1 local type entry: 2 x i32 }
|
||||
$41, $0A, { i32.const 10 }
|
||||
$21, $00, { local.set 0 }
|
||||
$41, $14, { i32.const 20 }
|
||||
$21, $01, { local.set 1 }
|
||||
$20, $00, { local.get 0 }
|
||||
$20, $01, { local.get 1 }
|
||||
$6A, { i32.add }
|
||||
$0F, $0B { return, end }
|
||||
);
|
||||
|
||||
procedure run;
|
||||
var
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('binary.locals');
|
||||
|
||||
ctx := wasm.parser.parse(@BINARY[0], puint8(@BINARY[0] + BINARY_SIZE));
|
||||
assert_true('valid binary', ctx^.ValidBinary);
|
||||
|
||||
{ Set up locals from the parsed code entry for the single function }
|
||||
ctx^.ExecutionState.Locals := @ctx^.Sections.CodeSection^.Entries[0].Locals;
|
||||
|
||||
ctx^.ExecutionState.Running := true;
|
||||
while wasm.vm.tick(ctx) do;
|
||||
|
||||
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
|
||||
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
|
||||
assert_i32('10+20=30', popi32(ctx^.ExecutionState.Operand_Stack), 30);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,68 @@
|
||||
{ E2E test: store 99 to linear memory, load it back = 99
|
||||
Equivalent WAT:
|
||||
(module
|
||||
(memory 1)
|
||||
(func (export "main") (result i32)
|
||||
i32.const 0
|
||||
i32.const 99
|
||||
i32.store offset=0 align=0
|
||||
i32.const 0
|
||||
i32.load offset=0 align=0
|
||||
return
|
||||
)
|
||||
)
|
||||
}
|
||||
unit wasm.test.binary.memroundtrip;
|
||||
|
||||
interface
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
types, console, wasm.types, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
const
|
||||
BINARY_SIZE = 53;
|
||||
BINARY : Array[$00..$34] of uint8 = (
|
||||
$00, $61, $73, $6D, { magic }
|
||||
$01, $00, $00, $00, { version 1 }
|
||||
$01, $05, $01, $60, { type section: 1 func type }
|
||||
$00, $01, $7F, { () -> (i32) }
|
||||
$03, $02, $01, $00, { function section: 1 func, type 0 }
|
||||
$05, $03, $01, $00, $01, { memory section: 1 memory, no max, 1 page }
|
||||
$07, $08, $01, $04, { export section: 1 export, name len 4 }
|
||||
$6D, $61, $69, $6E, { "main" }
|
||||
$00, $00, { export type=func, index=0 }
|
||||
$0A, $11, $01, $0F, { code section: len=17, count=1, body=15 }
|
||||
$00, { 0 locals }
|
||||
$41, $00, { i32.const 0 (address) }
|
||||
$41, $63, { i32.const 99 (value) }
|
||||
$36, $00, $00, { i32.store align=0 offset=0 }
|
||||
$41, $00, { i32.const 0 (address) }
|
||||
$28, $00, $00, { i32.load align=0 offset=0 }
|
||||
$0F, $0B { return, end }
|
||||
);
|
||||
|
||||
procedure run;
|
||||
var
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('binary.memroundtrip');
|
||||
|
||||
ctx := wasm.parser.parse(@BINARY[0], puint8(@BINARY[0] + BINARY_SIZE));
|
||||
assert_true('valid binary', ctx^.ValidBinary);
|
||||
|
||||
ctx^.ExecutionState.Running := true;
|
||||
while wasm.vm.tick(ctx) do;
|
||||
|
||||
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
|
||||
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
|
||||
assert_i32('store+load=99', popi32(ctx^.ExecutionState.Operand_Stack), 99);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -0,0 +1,58 @@
|
||||
{ E2E test: () -> i32, returns 42
|
||||
Equivalent WAT:
|
||||
(module
|
||||
(func (export "main") (result i32)
|
||||
i32.const 42
|
||||
return
|
||||
)
|
||||
)
|
||||
}
|
||||
unit wasm.test.binary.return42;
|
||||
|
||||
interface
|
||||
|
||||
const
|
||||
BINARY_SIZE = $26;
|
||||
BINARY : Array[$0..$25] of uint8 = (
|
||||
$00, $61, $73, $6D, { magic }
|
||||
$01, $00, $00, $00, { version 1 }
|
||||
$01, $05, $01, $60, { type section: 1 func type }
|
||||
$00, $01, $7F, { () -> (i32) }
|
||||
$03, $02, $01, $00, { function section: 1 func, type 0 }
|
||||
$07, $08, $01, $04, { export section: 1 export, name len 4 }
|
||||
$6D, $61, $69, $6E, { "main" }
|
||||
$00, $00, { export type=func, index=0 }
|
||||
$0A, $07, $01, { code section: len=7, count=1 }
|
||||
$05, $00, { body_size=5, 0 locals }
|
||||
$41, $2A, $0F, $0B { i32.const 42, return, end }
|
||||
);
|
||||
|
||||
procedure run;
|
||||
|
||||
implementation
|
||||
|
||||
uses
|
||||
types, console, wasm.types, wasm.types.stack,
|
||||
wasm.parser, wasm.vm, wasm.test.framework;
|
||||
|
||||
procedure run;
|
||||
var
|
||||
ctx : PWASMProcessContext;
|
||||
begin
|
||||
test_begin('binary.return42');
|
||||
|
||||
ctx := wasm.parser.parse(@BINARY[0], puint8(@BINARY[0] + BINARY_SIZE));
|
||||
assert_true('valid binary', ctx^.ValidBinary);
|
||||
assert_u32('version=1', ctx^.Version, 1);
|
||||
|
||||
ctx^.ExecutionState.Running := true;
|
||||
while wasm.vm.tick(ctx) do;
|
||||
|
||||
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
|
||||
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
|
||||
assert_i32('result=42', popi32(ctx^.ExecutionState.Operand_Stack), 42);
|
||||
|
||||
test_end;
|
||||
end;
|
||||
|
||||
end.
|
||||
@@ -1,22 +0,0 @@
|
||||
unit wasm.test.binary;
|
||||
|
||||
interface
|
||||
|
||||
const
|
||||
TEST_BINARY_1 : Array[$0..$25] of uint8 = (
|
||||
$00, $61, $73, $6D,
|
||||
$01, $00, $00, $00,
|
||||
$01, $05, $01, $60,
|
||||
$00, $01, $7F, $03,
|
||||
$02, $01, $00, $07,
|
||||
$08, $01, $04, $6D,
|
||||
$61, $69, $6E, $00,
|
||||
$00, $0A, $07, $01,
|
||||
$05, $00, $41, $2A,
|
||||
$0F, $0B
|
||||
);
|
||||
|
||||
implementation
|
||||
|
||||
end.
|
||||
|
||||
@@ -142,7 +142,13 @@ uses
|
||||
wasm.test.opcode.i64store32,
|
||||
{ Memory size/grow tests }
|
||||
wasm.test.opcode.memorysize,
|
||||
wasm.test.opcode.memorygrow;
|
||||
wasm.test.opcode.memorygrow,
|
||||
{ End-to-end binary tests }
|
||||
wasm.test.binary.return42,
|
||||
wasm.test.binary.addnums,
|
||||
wasm.test.binary.arithmetic,
|
||||
wasm.test.binary.locals,
|
||||
wasm.test.binary.memroundtrip;
|
||||
|
||||
procedure run_all_tests;
|
||||
begin
|
||||
@@ -302,6 +308,13 @@ begin
|
||||
wasm.test.opcode.memorysize.run;
|
||||
wasm.test.opcode.memorygrow.run;
|
||||
|
||||
{ End-to-end binary tests }
|
||||
wasm.test.binary.return42.run;
|
||||
wasm.test.binary.addnums.run;
|
||||
wasm.test.binary.arithmetic.run;
|
||||
wasm.test.binary.locals.run;
|
||||
wasm.test.binary.memroundtrip.run;
|
||||
|
||||
{ Summary }
|
||||
writestringln('');
|
||||
writestringln('========================================');
|
||||
|
||||
Reference in new issue
Block a user