2022-08-28 16:16:43 -06:00
|
|
|
package creature
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
func TestArithmetic(test *testing.T) {
|
|
|
|
machine := Machine {
|
|
|
|
Program: []int {
|
|
|
|
0x0, 2,
|
|
|
|
0x0, 3,
|
|
|
|
0x4,
|
|
|
|
|
|
|
|
0x0, 10,
|
|
|
|
0x0, 4,
|
|
|
|
0x5,
|
|
|
|
|
|
|
|
0x0, 7,
|
|
|
|
0x0, 2,
|
|
|
|
0x6,
|
|
|
|
|
|
|
|
0x0, 12,
|
|
|
|
0x0, 3,
|
|
|
|
0x7,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-08-28 16:20:09 -06:00
|
|
|
err := machine.Execute(0)
|
|
|
|
if err != nil {
|
|
|
|
test.Log("machine exited with error:", err)
|
|
|
|
test.Fail()
|
|
|
|
}
|
|
|
|
|
2022-08-28 16:16:43 -06:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|