From cfec23f4e91816f0912317975a69b49ed077707b Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Fri, 19 Nov 2021 07:24:16 -1000 Subject: [PATCH] More opcode tests --- test/opcode_test.go | 88 +++++++++++++++++++++++++++++++++++++++++++++ test/util.go | 7 ++++ 2 files changed, 95 insertions(+) diff --git a/test/opcode_test.go b/test/opcode_test.go index 2d51e64..3af78aa 100644 --- a/test/opcode_test.go +++ b/test/opcode_test.go @@ -65,3 +65,91 @@ functions: expectGlobalMemory(t, state, 0, 15) } + +func TestSub(t *testing.T) { + state := assembleAndExecute(t, ` +functions: +- - [sub, g0, 2] + - [sub, g0, -5] + - [sub, f0, 4] + - [sub, g0, f0] +`) + + expectGlobalMemory(t, state, 0, 7) +} + +func TestMul(t *testing.T) { + state := assembleAndExecute(t, ` +functions: +- - [mov, g0, 5] + - [mul, g0, 3] + - [mul, g0, g0] + - [mov, g1, 5] + - [mul, g1, -3] +`) + + expectGlobalMemory(t, state, 0, 225) + expectGlobalMemorySigned(t, state, 1, -15) +} + +func TestDivU(t *testing.T) { + state := assembleAndExecute(t, ` +functions: +- - [mov, g0, 15] + - [divu, g0, 3] + - [mov, g1, 4] + - [divu, g1, g1] +`) + + expectGlobalMemory(t, state, 0, 5) + expectGlobalMemory(t, state, 1, 1) +} + +func TestDivS(t *testing.T) { + state := assembleAndExecute(t, ` +functions: +- - [mov, g0, 15] + - [divs, g0, -3] + - [mov, g1, -15] + - [divs, g1, -3] + - [mov, g2, -4] + - [divs, g2, g2] +`) + + expectGlobalMemorySigned(t, state, 0, -5) + expectGlobalMemorySigned(t, state, 1, 5) + expectGlobalMemorySigned(t, state, 2, 1) +} + +func TestEq(t *testing.T) { + state := assembleAndExecute(t, ` +functions: +- - [mov, g0, 5] + - [eq, g0, 5] + - [jmpt, +2] + - [add, g1, 1] + - [eq, g0, 6] + - [jmpt, +2] + - [add, g1, 2] +`) + + expectGlobalMemory(t, state, 1, 2) +} + +func TestLTU(t *testing.T) { + state := assembleAndExecute(t, ` +functions: +- - [mov, g0, 5] + - [ltu, g0, 4] + - [jmpt, +2] + - [add, g1, 1] + - [ltu, g0, 5] + - [jmpt, +2] + - [add, g1, 2] + - [ltu, g0, 6] + - [jmpt, +2] + - [add, g1, 4] +`) + + expectGlobalMemory(t, state, 1, 3) +} diff --git a/test/util.go b/test/util.go index 5d05b1a..1ff46ba 100644 --- a/test/util.go +++ b/test/util.go @@ -30,3 +30,10 @@ func expectGlobalMemory(t *testing.T, state *vm.State, i uint64, expected uint64 t.Fatalf("Global memory index %d: expected=%d actual=%d\n", i, expected, actual) } } + +func expectGlobalMemorySigned(t *testing.T, state *vm.State, i uint64, expected int64) { + actual := int64(state.GlobalMemory().MustReadUnsigned(i)) + if actual != expected { + t.Fatalf("Global memory index %d: expected=%d actual=%d\n", i, expected, actual) + } +}