80 lines
1.2 KiB
Go
80 lines
1.2 KiB
Go
package creature
|
|
|
|
import "testing"
|
|
|
|
func runMachineTest(program []int, test *testing.T) (machine *Machine) {
|
|
machine = &Machine { Program: program }
|
|
|
|
err := machine.Execute(0)
|
|
if err != nil {
|
|
test.Log("machine exited with error:", err)
|
|
test.Fail()
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func TestPush(test *testing.T) {
|
|
machine := runMachineTest ([]int {
|
|
PUSH, 654,
|
|
}, test)
|
|
|
|
result := machine.Pop()
|
|
test.Log("popped:", result)
|
|
|
|
if result != 654 {
|
|
test.Log("result should be", 654)
|
|
test.Fail()
|
|
}
|
|
}
|
|
|
|
func TestArithmetic(test *testing.T) {
|
|
machine := runMachineTest([]int {
|
|
PUSH, 2,
|
|
PUSH, 3,
|
|
ADD,
|
|
|
|
PUSH, 10,
|
|
PUSH, 4,
|
|
SUB,
|
|
|
|
PUSH, 7,
|
|
PUSH, 2,
|
|
MUL,
|
|
|
|
PUSH, 12,
|
|
PUSH, 3,
|
|
DIV,
|
|
}, test)
|
|
|
|
addResult := machine.Pop()
|
|
subResult := machine.Pop()
|
|
mulResult := machine.Pop()
|
|
divResult := machine.Pop()
|
|
|
|
test.Log("add:", addResult)
|
|
test.Log("sub:", subResult)
|
|
test.Log("mul:", mulResult)
|
|
test.Log("div:", divResult)
|
|
|
|
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()
|
|
}
|
|
}
|