Split test utils, add opcode tests
This commit is contained in:
@@ -2,11 +2,8 @@ package test
|
||||
|
||||
import "testing"
|
||||
|
||||
import "github.com/firestuff/subcoding/asm"
|
||||
import "github.com/firestuff/subcoding/vm"
|
||||
|
||||
func TestLoop(t *testing.T) {
|
||||
prog, err := asm.AssembleString(`
|
||||
state := assembleAndExecute(t, `
|
||||
functions:
|
||||
- - [add, f0, 1]
|
||||
- [call, +1]
|
||||
@@ -15,21 +12,6 @@ functions:
|
||||
|
||||
- - [add, g0, 1]
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
state, err := vm.NewState(prog)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = state.Execute()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if state.GlobalMemory().MustReadUnsigned(0) != 3 {
|
||||
t.Fatalf("Expected 3, found %d\n", state.GlobalMemory().MustReadUnsigned(0))
|
||||
}
|
||||
expectGlobalMemory(t, state, 0, 3)
|
||||
}
|
||||
|
||||
67
test/opcode_test.go
Normal file
67
test/opcode_test.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package test
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNop(t *testing.T) {
|
||||
state := assembleAndExecute(t, `
|
||||
functions:
|
||||
- - [nop]
|
||||
`)
|
||||
|
||||
expectGlobalMemory(t, state, 0, 0)
|
||||
}
|
||||
|
||||
func TestCal(t *testing.T) {
|
||||
state := assembleAndExecute(t, `
|
||||
functions:
|
||||
- - [cal, +1]
|
||||
- [add, g0, 1]
|
||||
|
||||
- - [mov, g0, 5]
|
||||
`)
|
||||
|
||||
expectGlobalMemory(t, state, 0, 6)
|
||||
}
|
||||
|
||||
func TestRet(t *testing.T) {
|
||||
state := assembleAndExecute(t, `
|
||||
functions:
|
||||
- - [cal, +1]
|
||||
- [add, g0, 1]
|
||||
- [ret]
|
||||
|
||||
- - [mov, g0, 5]
|
||||
- [ret]
|
||||
- [add, g0, 2]
|
||||
`)
|
||||
|
||||
expectGlobalMemory(t, state, 0, 6)
|
||||
}
|
||||
|
||||
func TestMov(t *testing.T) {
|
||||
state := assembleAndExecute(t, `
|
||||
functions:
|
||||
- - [mov, g0, 1]
|
||||
- [mov, g1, g0]
|
||||
- [mov, f0, 5]
|
||||
- [mov, g2, f0]
|
||||
`)
|
||||
|
||||
expectGlobalMemory(t, state, 0, 1)
|
||||
expectGlobalMemory(t, state, 1, 1)
|
||||
expectGlobalMemory(t, state, 2, 5)
|
||||
}
|
||||
|
||||
func TestAdd(t *testing.T) {
|
||||
state := assembleAndExecute(t, `
|
||||
functions:
|
||||
- - [add, g0, 5]
|
||||
- [add, g0, 2]
|
||||
- [add, g0, g0]
|
||||
- [add, f0, 3]
|
||||
- [add, f0, -2]
|
||||
- [add, g0, f0]
|
||||
`)
|
||||
|
||||
expectGlobalMemory(t, state, 0, 15)
|
||||
}
|
||||
32
test/util.go
Normal file
32
test/util.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package test
|
||||
|
||||
import "testing"
|
||||
|
||||
import "github.com/firestuff/subcoding/asm"
|
||||
import "github.com/firestuff/subcoding/vm"
|
||||
|
||||
func assembleAndExecute(t *testing.T, src string) *vm.State {
|
||||
prog, err := asm.AssembleString(src)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
state, err := vm.NewState(prog)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = state.Execute()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return state
|
||||
}
|
||||
|
||||
func expectGlobalMemory(t *testing.T, state *vm.State, i uint64, expected uint64) {
|
||||
actual := state.GlobalMemory().MustReadUnsigned(i)
|
||||
if actual != expected {
|
||||
t.Fatalf("Global memory index %d: expected=%d actual=%d\n", i, expected, actual)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user