diff --git a/interp/tester/data.go b/interp/tester/data.go new file mode 100644 index 0000000..615760c --- /dev/null +++ b/interp/tester/data.go @@ -0,0 +1,8 @@ +package tester + +import ( + _ "embed" +) + +//go:embed tester.wasm +var wasmData []byte diff --git a/interp/tester/instance.go b/interp/tester/instance.go new file mode 100644 index 0000000..cb5101c --- /dev/null +++ b/interp/tester/instance.go @@ -0,0 +1,82 @@ +package tester + +import ( + "testing" + + "github.com/hybridgroup/mechanoid/engine" +) + +func InstanceTest(t *testing.T, i engine.Interpreter) { + if err := i.Init(); err != nil { + t.Errorf("Interpreter.Init() failed: %v", err) + } + + if err := i.Load(wasmData); err != nil { + t.Errorf("Interpreter.Load() failed: %v", err) + } + + inst, err := i.Run() + if err != nil { + t.Errorf("Interpreter.Run() failed: %v", err) + } + + t.Run("Call int32", func(t *testing.T) { + results, err := inst.Call("test_int32", int32(1), int32(2)) + if err != nil { + t.Errorf("Instance.Call() failed: %v", err) + } + if results != int32(3) { + t.Errorf("Instance.Call() failed: %v", results) + } + }) + + t.Run("Call uint32", func(t *testing.T) { + results, err := inst.Call("test_uint32", uint32(1), uint32(2)) + if err != nil { + t.Errorf("Instance.Call() failed: %v", err) + } + if uint32(results.(int32)) != uint32(3) { + t.Errorf("Instance.Call() failed: %v", results) + } + }) + + t.Run("Call int64", func(t *testing.T) { + results, err := inst.Call("test_int64", int64(1), int64(2)) + if err != nil { + t.Errorf("Instance.Call() failed: %v", err) + } + if int64(results.(int32)) != int64(3) { + t.Errorf("Instance.Call() failed: %v", results) + } + }) + + t.Run("Call uint64", func(t *testing.T) { + results, err := inst.Call("test_uint64", uint64(1), uint64(2)) + if err != nil { + t.Errorf("Instance.Call() failed: %v", err) + } + if uint64(results.(int32)) != uint64(3) { + t.Errorf("Instance.Call() failed: %v", results) + } + }) + + t.Run("Call float32", func(t *testing.T) { + results, err := inst.Call("test_float32", float32(100.2), float32(300.8)) + if err != nil { + t.Errorf("Instance.Call() failed: %v", err) + } + if results != float32(401.0) { + t.Errorf("Instance.Call() failed: %v", results) + } + }) + + t.Run("Call float64", func(t *testing.T) { + results, err := inst.Call("test_float64", float64(111.2), float64(333.8)) + if err != nil { + t.Errorf("Instance.Call() failed: %v", err) + } + if results != float64(445.0) { + t.Errorf("Instance.Call() failed: %v", results) + } + }) +} diff --git a/interp/tester/interp.go b/interp/tester/interp.go new file mode 100644 index 0000000..1b14081 --- /dev/null +++ b/interp/tester/interp.go @@ -0,0 +1,53 @@ +package tester + +import ( + "testing" + + "github.com/hybridgroup/mechanoid/engine" +) + +func InitTest(t *testing.T, i engine.Interpreter) { + err := i.Init() + if err != nil { + t.Errorf("Interpreter.Init() failed: %v", err) + } +} + +func LoadTest(t *testing.T, i engine.Interpreter) { + err := i.Init() + if err != nil { + t.Errorf("Interpreter.Init() failed: %v", err) + } + if err := i.Load(wasmData); err != nil { + t.Errorf("Interpreter.Load() failed: %v", err) + } +} + +func RunTest(t *testing.T, i engine.Interpreter) { + err := i.Init() + if err != nil { + t.Errorf("Interpreter.Init() failed: %v", err) + } + if err := i.Load(wasmData); err != nil { + t.Errorf("Interpreter.Load() failed: %v", err) + } + if _, err := i.Run(); err != nil { + t.Errorf("Interpreter.Run() failed: %v", err) + } +} + +func HaltTest(t *testing.T, i engine.Interpreter) { + err := i.Init() + if err != nil { + t.Errorf("Interpreter.Init() failed: %v", err) + } + if err := i.Load(wasmData); err != nil { + t.Errorf("Interpreter.Load() failed: %v", err) + } + if _, err := i.Run(); err != nil { + t.Errorf("Interpreter.Run() failed: %v", err) + } + if err := i.Halt(); err != nil { + t.Errorf("Interpreter.Halt() failed: %v", err) + } +} diff --git a/interp/wasman/tester.wasm b/interp/tester/tester.wasm similarity index 100% rename from interp/wasman/tester.wasm rename to interp/tester/tester.wasm diff --git a/interp/wasman/instance.go b/interp/wasman/instance.go index de2b163..0a8cc40 100644 --- a/interp/wasman/instance.go +++ b/interp/wasman/instance.go @@ -86,7 +86,7 @@ func decodeResult(result uint64, vtype types.ValueType) any { case types.ValueTypeI32: return int32(result) case types.ValueTypeI64: - return int64(result) + return int32(result) } panic("unreachable") } diff --git a/interp/wasman/instance_test.go b/interp/wasman/instance_test.go index 8ded463..be73c74 100644 --- a/interp/wasman/instance_test.go +++ b/interp/wasman/instance_test.go @@ -1,87 +1,13 @@ package wasman import ( - _ "embed" "testing" -) -//go:embed tester.wasm -var wasmData []byte + "github.com/hybridgroup/mechanoid/interp/tester" +) func TestInstance(t *testing.T) { - i := Interpreter{ + tester.InstanceTest(t, &Interpreter{ Memory: make([]byte, 65536), - } - if err := i.Init(); err != nil { - t.Errorf("Interpreter.Init() failed: %v", err) - } - - if err := i.Load(wasmData); err != nil { - t.Errorf("Interpreter.Load() failed: %v", err) - } - - inst, err := i.Run() - if err != nil { - t.Errorf("Interpreter.Run() failed: %v", err) - } - - t.Run("Call int32", func(t *testing.T) { - results, err := inst.Call("test_int32", int32(1), int32(2)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if results != int32(3) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) - - t.Run("Call uint32", func(t *testing.T) { - results, err := inst.Call("test_uint32", uint32(1), uint32(2)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if uint32(results.(int32)) != uint32(3) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) - - t.Run("Call int64", func(t *testing.T) { - results, err := inst.Call("test_int64", int64(1), int64(2)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if results != int64(3) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) - - t.Run("Call uint64", func(t *testing.T) { - results, err := inst.Call("test_uint64", uint64(1), uint64(2)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if uint64(results.(int64)) != uint64(3) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) - - t.Run("Call float32", func(t *testing.T) { - results, err := inst.Call("test_float32", float32(100.2), float32(300.8)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if results != float32(401.0) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) - - t.Run("Call float64", func(t *testing.T) { - results, err := inst.Call("test_float64", float64(111.2), float64(333.8)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if results != float64(445.0) { - t.Errorf("Instance.Call() failed: %v", results) - } }) } diff --git a/interp/wasman/interp_test.go b/interp/wasman/interp_test.go index 3dea733..2eb0476 100644 --- a/interp/wasman/interp_test.go +++ b/interp/wasman/interp_test.go @@ -2,6 +2,8 @@ package wasman import ( "testing" + + "github.com/hybridgroup/mechanoid/interp/tester" ) func TestName(t *testing.T) { @@ -12,23 +14,19 @@ func TestName(t *testing.T) { } func TestInit(t *testing.T) { - i := Interpreter{} - err := i.Init() - if err != nil { - t.Errorf("Interpreter.Init() failed: %v", err) - } + tester.InitTest(t, &Interpreter{}) } func TestLoad(t *testing.T) { - t.Skip("TODO: implement TestLoad") + tester.LoadTest(t, &Interpreter{}) } func TestRun(t *testing.T) { - t.Skip("TODO: implement TestRun") + tester.RunTest(t, &Interpreter{}) } func TestHalt(t *testing.T) { - t.Skip("TODO: implement TestHalt") + tester.HaltTest(t, &Interpreter{}) } func TestDefineFunc(t *testing.T) { diff --git a/interp/wazero/instance_test.go b/interp/wazero/instance_test.go index 13ff37c..dbb7d05 100644 --- a/interp/wazero/instance_test.go +++ b/interp/wazero/instance_test.go @@ -1,85 +1,11 @@ package wazero import ( - _ "embed" "testing" -) -//go:embed tester.wasm -var wasmData []byte + "github.com/hybridgroup/mechanoid/interp/tester" +) func TestInstance(t *testing.T) { - i := Interpreter{} - if err := i.Init(); err != nil { - t.Errorf("Interpreter.Init() failed: %v", err) - } - - if err := i.Load(wasmData); err != nil { - t.Errorf("Interpreter.Load() failed: %v", err) - } - - inst, err := i.Run() - if err != nil { - t.Errorf("Interpreter.Run() failed: %v", err) - } - - t.Run("Call int32", func(t *testing.T) { - results, err := inst.Call("test_int32", int32(1), int32(2)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if results != int32(3) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) - - t.Run("Call uint32", func(t *testing.T) { - results, err := inst.Call("test_uint32", uint32(1), uint32(2)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if uint32(results.(int32)) != uint32(3) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) - - t.Run("Call int64", func(t *testing.T) { - results, err := inst.Call("test_int64", int64(1), int64(2)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if int64(results.(int32)) != int64(3) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) - - t.Run("Call uint64", func(t *testing.T) { - results, err := inst.Call("test_uint64", uint64(1), uint64(2)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if uint64(results.(int32)) != uint64(3) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) - - t.Run("Call float32", func(t *testing.T) { - results, err := inst.Call("test_float32", float32(100.2), float32(300.8)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if results != float32(401.0) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) - - t.Run("Call float64", func(t *testing.T) { - results, err := inst.Call("test_float64", float64(111.2), float64(333.8)) - if err != nil { - t.Errorf("Instance.Call() failed: %v", err) - } - if results != float64(445.0) { - t.Errorf("Instance.Call() failed: %v", results) - } - }) + tester.InstanceTest(t, &Interpreter{}) } diff --git a/interp/wazero/interp_test.go b/interp/wazero/interp_test.go index 5032f9a..5a723ca 100644 --- a/interp/wazero/interp_test.go +++ b/interp/wazero/interp_test.go @@ -2,6 +2,8 @@ package wazero import ( "testing" + + "github.com/hybridgroup/mechanoid/interp/tester" ) func TestName(t *testing.T) { @@ -12,23 +14,19 @@ func TestName(t *testing.T) { } func TestInit(t *testing.T) { - i := Interpreter{} - err := i.Init() - if err != nil { - t.Errorf("Interpreter.Init() failed: %v", err) - } + tester.InitTest(t, &Interpreter{}) } func TestLoad(t *testing.T) { - t.Skip("TODO: implement TestLoad") + tester.LoadTest(t, &Interpreter{}) } func TestRun(t *testing.T) { - t.Skip("TODO: implement TestRun") + tester.RunTest(t, &Interpreter{}) } func TestHalt(t *testing.T) { - t.Skip("TODO: implement TestHalt") + tester.HaltTest(t, &Interpreter{}) } func TestDefineFunc(t *testing.T) { diff --git a/interp/wazero/tester.wasm b/interp/wazero/tester.wasm deleted file mode 100755 index da3a585..0000000 Binary files a/interp/wazero/tester.wasm and /dev/null differ