creature/creature_test.go

280 lines
4.0 KiB
Go
Raw Normal View History

2022-08-28 22:16:43 +00:00
package creature
import "testing"
2022-09-08 18:43:19 +00:00
func runMachineTest(
2022-08-29 01:11:06 +00:00
program []int,
2022-09-08 18:43:19 +00:00
memory []int,
2022-08-29 01:11:06 +00:00
test *testing.T,
) (
2022-08-29 14:48:45 +00:00
machine *Machine[int],
2022-08-29 01:11:06 +00:00
) {
2022-09-08 18:43:19 +00:00
machine = &Machine[int]{}
machine.LoadProgram(program)
2022-08-29 01:11:06 +00:00
if memory != nil {
machine.LoadMemory(memory)
}
2022-08-28 22:26:44 +00:00
err := machine.Execute(0)
if err != nil {
test.Log("machine exited with error:", err)
test.Fail()
}
2022-08-28 22:29:10 +00:00
return
}
func TestPush(test *testing.T) {
2022-09-08 18:43:19 +00:00
machine := runMachineTest([]int{
PUSH, 3,
POP,
2022-08-28 22:34:23 +00:00
PUSH, 654,
2022-08-29 01:11:06 +00:00
}, nil, test)
2022-09-08 18:43:19 +00:00
2022-08-28 22:26:44 +00:00
result := machine.Pop()
test.Log("popped:", result)
if result != 654 {
test.Log("result should be", 654)
test.Fail()
}
}
2022-08-28 22:16:43 +00:00
func TestArithmetic(test *testing.T) {
2022-09-08 18:43:19 +00:00
machine := runMachineTest([]int{
2022-08-28 22:34:23 +00:00
PUSH, 3,
PUSH, 2,
2022-08-28 22:34:23 +00:00
ADD,
2022-09-08 18:43:19 +00:00
2022-08-28 23:59:00 +00:00
PUSH, 10,
PUSH, 4,
2022-08-28 22:34:23 +00:00
SUB,
2022-09-08 18:43:19 +00:00
2022-08-28 22:34:23 +00:00
PUSH, 2,
PUSH, 7,
2022-08-28 22:34:23 +00:00
MUL,
2022-09-08 18:43:19 +00:00
2022-08-28 23:59:00 +00:00
PUSH, 12,
PUSH, 3,
2022-08-28 22:34:23 +00:00
DIV,
2022-09-08 18:43:19 +00:00
2022-08-29 00:13:55 +00:00
PUSH, 8,
PUSH, 6,
2022-08-29 00:13:55 +00:00
MOD,
2022-08-29 01:11:06 +00:00
}, nil, test)
2022-08-28 22:20:09 +00:00
2022-08-29 00:13:55 +00:00
modResult := machine.Pop()
2022-08-28 22:16:43 +00:00
divResult := machine.Pop()
2022-08-28 23:59:00 +00:00
mulResult := machine.Pop()
subResult := machine.Pop()
addResult := machine.Pop()
2022-08-28 22:16:43 +00:00
test.Log("add:", addResult)
test.Log("sub:", subResult)
test.Log("mul:", mulResult)
test.Log("div:", divResult)
2022-08-29 00:13:55 +00:00
test.Log("mod:", modResult)
2022-08-28 22:16:43 +00:00
if addResult != 5 {
test.Log("add result should be", 5)
test.Fail()
}
if subResult != 6 {
test.Log("sub result should be", 6)
test.Fail()
}
if mulResult != 14 {
test.Log("mul result should be", 14)
test.Fail()
}
if divResult != 4 {
test.Log("div result should be", 4)
test.Fail()
}
2022-08-29 00:13:55 +00:00
if modResult != 2 {
test.Log("mod result should be", 2)
test.Fail()
}
}
func TestComparison(test *testing.T) {
2022-09-08 18:43:19 +00:00
machine := runMachineTest([]int{
2022-08-29 00:13:55 +00:00
PUSH, 6,
PUSH, 6,
EQ,
2022-09-08 18:43:19 +00:00
2022-08-29 00:13:55 +00:00
PUSH, 324,
PUSH, 4,
2022-08-29 00:13:55 +00:00
GT,
2022-09-08 18:43:19 +00:00
2022-08-29 00:13:55 +00:00
PUSH, 4,
PUSH, 324,
2022-08-29 00:13:55 +00:00
LT,
2022-09-08 18:43:19 +00:00
2022-08-29 00:13:55 +00:00
PUSH, 54,
PUSH, 6,
2022-08-29 00:13:55 +00:00
NEQ,
2022-08-29 01:11:06 +00:00
}, nil, test)
2022-08-29 00:13:55 +00:00
neqResult := machine.Pop()
ltResult := machine.Pop()
gtResult := machine.Pop()
eqResult := machine.Pop()
test.Log("eq:", eqResult)
test.Log("gt:", gtResult)
test.Log("lt:", ltResult)
test.Log("neq:", neqResult)
if eqResult != 1 {
test.Log("eq result should be", 1)
test.Fail()
}
if gtResult != 1 {
test.Log("gt result should be", 1)
test.Fail()
}
if ltResult != 1 {
test.Log("lt result should be", 1)
test.Fail()
}
if neqResult != 1 {
test.Log("neq result should be", 1)
test.Fail()
}
2022-08-28 22:16:43 +00:00
}
2022-08-29 01:11:06 +00:00
func TestPeekPoke(test *testing.T) {
2022-09-08 18:43:19 +00:00
machine := runMachineTest([]int{
2022-08-29 01:11:06 +00:00
PUSH, 0,
PEEK,
PUSH, 29,
PUSH, 1,
POKE,
PUSH, 1,
PEEK,
2022-09-08 18:43:19 +00:00
}, []int{
2022-08-29 01:11:06 +00:00
632,
2022-08-29 04:43:13 +00:00
13,
2022-08-29 01:11:06 +00:00
}, test)
2022-09-08 18:43:19 +00:00
2022-08-29 01:11:06 +00:00
secondResult := machine.Pop()
firstResult := machine.Pop()
2022-09-08 18:43:19 +00:00
2022-08-29 01:11:06 +00:00
test.Log("first:", firstResult)
test.Log("second:", secondResult)
if firstResult != 632 {
test.Log("first result should be", 632)
test.Fail()
}
if secondResult != 29 {
test.Log("second result should be", 29)
test.Fail()
}
}
2022-08-29 01:33:26 +00:00
func TestHalt(test *testing.T) {
2022-09-08 18:43:19 +00:00
machine := runMachineTest([]int{
2022-08-29 01:33:26 +00:00
PUSH, 32,
HALT,
PUSH, 3,
}, nil, test)
2022-09-08 18:43:19 +00:00
2022-08-29 01:33:26 +00:00
result := machine.Pop()
test.Log("popped:", result)
if result != 32 {
test.Log("result should be", 32)
test.Fail()
}
}
2022-08-29 02:45:46 +00:00
func TestJump(test *testing.T) {
2022-09-08 18:43:19 +00:00
machine := runMachineTest([]int{
2022-08-29 02:45:46 +00:00
PUSH, 1,
PUSH, 8,
JMP,
2022-09-08 18:43:19 +00:00
2022-08-29 02:45:46 +00:00
PUSH, 3,
HALT,
PUSH, 0,
PUSH, 16,
JMP,
PUSH, 4,
HALT,
PUSH, 5,
}, nil, test)
2022-09-08 18:43:19 +00:00
2022-08-29 02:45:46 +00:00
result := machine.Pop()
test.Log("popped:", result)
if result != 4 {
test.Log("result should be", 4)
test.Fail()
}
}
func TestRegister(test *testing.T) {
output := ""
2022-09-08 18:43:19 +00:00
machine := &Machine[int]{}
machine.LoadProgram([]int{
PUSH, int('h'),
PUSH, 4,
CAL,
PUSH, int('e'),
PUSH, 4,
CAL,
PUSH, int('l'),
PUSH, 4,
CAL,
PUSH, int('l'),
PUSH, 4,
CAL,
PUSH, int('o'),
PUSH, 4,
CAL,
PUSH, int('r'),
PUSH, 4,
CAL,
PUSH, int('l'),
PUSH, 4,
CAL,
PUSH, int('d'),
PUSH, 4,
CAL,
PUSH, int('!'),
PUSH, 4,
CAL,
})
2022-08-29 14:48:45 +00:00
machine.Register(4, func(machine *Machine[int]) (stop bool) {
output += string(rune(machine.Pop()))
return
})
err := machine.Execute(0)
if err != nil {
test.Log("machine exited with error:", err)
test.Fail()
}
test.Log("printed:", output)
if output != "hellorld!" {
test.Log("result should be", "hellorld!")
test.Fail()
}
}