Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interp: DRY up testing and help ensure compatibility in different interpreters #5

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions interp/tester/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tester

import (
_ "embed"
)

//go:embed tester.wasm
var wasmData []byte
82 changes: 82 additions & 0 deletions interp/tester/instance.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
53 changes: 53 additions & 0 deletions interp/tester/interp.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion interp/wasman/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
80 changes: 3 additions & 77 deletions interp/wasman/instance_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
14 changes: 6 additions & 8 deletions interp/wasman/interp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package wasman

import (
"testing"

"github.com/hybridgroup/mechanoid/interp/tester"
)

func TestName(t *testing.T) {
Expand All @@ -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) {
Expand Down
80 changes: 3 additions & 77 deletions interp/wazero/instance_test.go
Original file line number Diff line number Diff line change
@@ -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{})
}
Loading