Created and passed peek/poke test
This commit is contained in:
parent
302a4e5806
commit
a78c1dfcf1
@ -2,8 +2,17 @@ package creature
|
||||
|
||||
import "testing"
|
||||
|
||||
func runMachineTest(program []int, test *testing.T) (machine *Machine) {
|
||||
func runMachineTest (
|
||||
program []int,
|
||||
memory []int,
|
||||
test *testing.T,
|
||||
) (
|
||||
machine *Machine,
|
||||
) {
|
||||
machine = &Machine { Program: program }
|
||||
if memory != nil {
|
||||
machine.LoadMemory(memory)
|
||||
}
|
||||
|
||||
err := machine.Execute(0)
|
||||
if err != nil {
|
||||
@ -19,7 +28,7 @@ func TestPush(test *testing.T) {
|
||||
PUSH, 3,
|
||||
POP,
|
||||
PUSH, 654,
|
||||
}, test)
|
||||
}, nil, test)
|
||||
|
||||
result := machine.Pop()
|
||||
test.Log("popped:", result)
|
||||
@ -51,7 +60,7 @@ func TestArithmetic(test *testing.T) {
|
||||
PUSH, 6,
|
||||
PUSH, 8,
|
||||
MOD,
|
||||
}, test)
|
||||
}, nil, test)
|
||||
|
||||
modResult := machine.Pop()
|
||||
divResult := machine.Pop()
|
||||
@ -109,7 +118,7 @@ func TestComparison(test *testing.T) {
|
||||
PUSH, 54,
|
||||
NEQ,
|
||||
|
||||
}, test)
|
||||
}, nil, test)
|
||||
|
||||
neqResult := machine.Pop()
|
||||
ltResult := machine.Pop()
|
||||
@ -141,3 +150,36 @@ func TestComparison(test *testing.T) {
|
||||
test.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeekPoke(test *testing.T) {
|
||||
machine := runMachineTest ([]int {
|
||||
PUSH, 0,
|
||||
PEEK,
|
||||
|
||||
PUSH, 29,
|
||||
PUSH, 1,
|
||||
POKE,
|
||||
|
||||
PUSH, 1,
|
||||
PEEK,
|
||||
}, []int {
|
||||
632,
|
||||
29,
|
||||
}, test)
|
||||
|
||||
secondResult := machine.Pop()
|
||||
firstResult := machine.Pop()
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user