From b0ee50030442f01e90e09d598c726d16898adf6a Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Thu, 18 Nov 2021 20:12:35 -1000 Subject: [PATCH] Split test utils, add opcode tests --- test/integration_test.go | 22 ++----------- test/opcode_test.go | 67 ++++++++++++++++++++++++++++++++++++++++ test/util.go | 32 +++++++++++++++++++ 3 files changed, 101 insertions(+), 20 deletions(-) create mode 100644 test/opcode_test.go create mode 100644 test/util.go diff --git a/test/integration_test.go b/test/integration_test.go index 79eeb1f..c4d68f8 100644 --- a/test/integration_test.go +++ b/test/integration_test.go @@ -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) } diff --git a/test/opcode_test.go b/test/opcode_test.go new file mode 100644 index 0000000..2d51e64 --- /dev/null +++ b/test/opcode_test.go @@ -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) +} diff --git a/test/util.go b/test/util.go new file mode 100644 index 0000000..5d05b1a --- /dev/null +++ b/test/util.go @@ -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) + } +}