Files
subcoding/test/opcode_test.go
2021-11-19 16:43:59 -10:00

440 lines
6.7 KiB
Go

package test
import "testing"
func TestNop(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [nop]
`)
expectGlobalMemory(t, state, 0, 0)
}
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)
}
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 TestNot(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 8]
- [not, g0]
- [and, g0, 15]
`)
expectGlobalMemory(t, state, 0, 7)
}
func TestAnd(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 7]
- [and, g0, 18]
`)
expectGlobalMemory(t, state, 0, 2)
}
func TestOr(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 7]
- [or, g0, 18]
`)
expectGlobalMemory(t, state, 0, 23)
}
func TestXor(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 7]
- [xor, g0, 18]
`)
expectGlobalMemory(t, state, 0, 21)
}
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)
}
func TestLTS(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 0]
- [lts, g0, -1]
- [jmpt, +2]
- [add, g1, 1]
- [lts, g0, 0]
- [jmpt, +2]
- [add, g1, 2]
- [lts, g0, 1]
- [jmpt, +2]
- [add, g1, 4]
`)
expectGlobalMemory(t, state, 1, 3)
}
func TestGTU(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 5]
- [gtu, g0, 4]
- [jmpt, +2]
- [add, g1, 1]
- [gtu, g0, 5]
- [jmpt, +2]
- [add, g1, 2]
- [gtu, g0, 6]
- [jmpt, +2]
- [add, g1, 4]
`)
expectGlobalMemory(t, state, 1, 6)
}
func TestGTS(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 0]
- [gts, g0, -1]
- [jmpt, +2]
- [add, g1, 1]
- [gts, g0, 0]
- [jmpt, +2]
- [add, g1, 2]
- [gts, g0, 1]
- [jmpt, +2]
- [add, g1, 4]
`)
expectGlobalMemory(t, state, 1, 6)
}
func TestLTEU(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 5]
- [lteu, g0, 4]
- [jmpt, +2]
- [add, g1, 1]
- [lteu, g0, 5]
- [jmpt, +2]
- [add, g1, 2]
- [lteu, g0, 6]
- [jmpt, +2]
- [add, g1, 4]
`)
expectGlobalMemory(t, state, 1, 1)
}
func TestLTES(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 0]
- [ltes, g0, -1]
- [jmpt, +2]
- [add, g1, 1]
- [ltes, g0, 0]
- [jmpt, +2]
- [add, g1, 2]
- [ltes, g0, 1]
- [jmpt, +2]
- [add, g1, 4]
`)
expectGlobalMemory(t, state, 1, 1)
}
func TestGTEU(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 5]
- [gteu, g0, 4]
- [jmpt, +2]
- [add, g1, 1]
- [gteu, g0, 5]
- [jmpt, +2]
- [add, g1, 2]
- [gteu, g0, 6]
- [jmpt, +2]
- [add, g1, 4]
`)
expectGlobalMemory(t, state, 1, 4)
}
func TestGTES(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [mov, g0, 0]
- [gtes, g0, -1]
- [jmpt, +2]
- [add, g1, 1]
- [gtes, g0, 0]
- [jmpt, +2]
- [add, g1, 2]
- [gtes, g0, 1]
- [jmpt, +2]
- [add, g1, 4]
`)
expectGlobalMemory(t, state, 1, 4)
}
func TestJmp(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [jmp, +2]
- [add, g0, 1]
- [add, g0, 2]
- [jmp, +1]
- [add, g0, 4]
`)
expectGlobalMemory(t, state, 0, 6)
}
func TestJmpT(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [eq, 0, 0]
- [jmpt, +2]
- [add, g0, 1]
- [eq, 0, 1]
- [jmpt, +2]
- [add, g0, 2]
- [add, g0, 4]
`)
expectGlobalMemory(t, state, 0, 6)
}
func TestJmpF(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [eq, 0, 0]
- [jmpf, +2]
- [add, g0, 1]
- [eq, 0, 1]
- [jmpf, +2]
- [add, g0, 2]
- [add, g0, 4]
`)
expectGlobalMemory(t, state, 0, 5)
}
func TestCal(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [cal, +1]
- [add, g0, 1]
- [cal, +2] # Function doesn't exist, immediate implicit return
- [add, g0, 2]
- - [mov, g0, 5]
`)
expectGlobalMemory(t, state, 0, 8)
}
func TestCalT(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [eq, 0, 0]
- [calt, +1]
- [add, g0, 1]
- [eq, 0, 1]
- [calt, +2]
- [add, g0, 2]
- - [add, g0, 5]
- - [add, g0, 13]
`)
expectGlobalMemory(t, state, 0, 8)
}
func TestCalF(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [eq, 0, 0]
- [calf, +1]
- [add, g0, 1]
- [eq, 0, 1]
- [calf, +2]
- [add, g0, 2]
- - [add, g0, 5]
- - [add, g0, 13]
`)
expectGlobalMemory(t, state, 0, 16)
}
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 TestRetT(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [cal, +1]
- [add, g0, 1]
- - [add, g0, 2]
- [eq, 0, 0]
- [rett]
- [add, g0, 4]
- [eq, 0, 1]
- [rett]
- [add, g0, 8]
`)
expectGlobalMemory(t, state, 0, 3)
}
func TestRetF(t *testing.T) {
state := assembleAndExecute(t, `
functions:
- - [cal, +1]
- [add, g0, 1]
- - [add, g0, 2]
- [eq, 0, 0]
- [retf]
- [add, g0, 4]
- [eq, 0, 1]
- [retf]
- [add, g0, 8]
`)
expectGlobalMemory(t, state, 0, 7)
}