From a78c1dfcf1ca0e97e2aad3ce9f5ecca7bf89d444 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 28 Aug 2022 21:11:06 -0400 Subject: [PATCH] Created and passed peek/poke test --- creature_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/creature_test.go b/creature_test.go index ad1db3f..4ac4f94 100644 --- a/creature_test.go +++ b/creature_test.go @@ -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() + } +}