Files
subcoding/test/opcode_test.go

522 lines
8.4 KiB
Go
Raw Normal View History

2021-11-18 20:12:35 -10:00
package test
import "testing"
func TestNop(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-18 20:12:35 -10:00
functions:
- - [nop]
`)
expectGlobalMemory(t, state, 0, 0)
}
func TestMov(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-18 20:12:35 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-18 20:12:35 -10:00
functions:
- - [add, g0, 5]
- [add, g0, 2]
- [add, g0, g0]
- [add, f0, 3]
- [add, f0, -2]
- [add, g0, f0]
`)
expectGlobalMemory(t, state, 0, 15)
}
2021-11-19 07:24:16 -10:00
func TestSub(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 07:24:16 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 07:24:16 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 07:24:16 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 07:24:16 -10:00
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)
}
2021-11-19 16:43:59 -10:00
func TestNot(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:43:59 -10:00
functions:
- - [mov, g0, 8]
- [not, g0]
- [and, g0, 15]
`)
expectGlobalMemory(t, state, 0, 7)
}
func TestAnd(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:43:59 -10:00
functions:
- - [mov, g0, 7]
- [and, g0, 18]
`)
expectGlobalMemory(t, state, 0, 2)
}
func TestOr(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:43:59 -10:00
functions:
- - [mov, g0, 7]
- [or, g0, 18]
`)
expectGlobalMemory(t, state, 0, 23)
}
func TestXor(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:43:59 -10:00
functions:
- - [mov, g0, 7]
- [xor, g0, 18]
`)
expectGlobalMemory(t, state, 0, 21)
}
2021-11-19 17:04:11 -10:00
func TestShR(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 17:04:11 -10:00
functions:
- - [mov, g0, 53]
- [shr, g0, 2]
`)
expectGlobalMemory(t, state, 0, 13)
}
func TestShL(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 17:04:11 -10:00
functions:
- - [mov, g0, 53]
- [shl, g0, 2]
`)
expectGlobalMemory(t, state, 0, 212)
}
2021-11-19 07:24:16 -10:00
func TestEq(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 07:24:16 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 07:24:16 -10:00
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)
}
2021-11-19 15:32:49 -10:00
2021-11-19 15:38:28 -10:00
func TestLTS(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 15:38:28 -10:00
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)
}
2021-11-19 15:32:49 -10:00
func TestGTU(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 15:32:49 -10:00
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)
}
2021-11-19 15:38:28 -10:00
func TestGTS(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 15:38:28 -10:00
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)
}
2021-11-19 15:32:49 -10:00
func TestLTEU(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 15:32:49 -10:00
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)
}
2021-11-19 15:38:28 -10:00
func TestLTES(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 15:38:28 -10:00
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)
}
2021-11-19 15:32:49 -10:00
func TestGTEU(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 15:32:49 -10:00
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)
}
2021-11-19 15:38:28 -10:00
func TestGTES(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 15:38:28 -10:00
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)
}
2021-11-19 15:42:27 -10:00
func TestJmp(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 15:42:27 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 15:42:27 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 15:42:27 -10:00
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)
}
2021-11-19 16:24:04 -10:00
func TestCal(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:24:04 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:24:04 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:24:04 -10:00
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)
}
2021-11-19 16:30:11 -10:00
func TestRet(t *testing.T) {
state := assembleAndExecute(t, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:30:11 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:30:11 -10:00
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, `
2021-11-20 18:27:06 -10:00
global_memory_size: 4
function_memory_size: 4
2021-11-19 16:30:11 -10:00
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)
}