From 0f1285bbb983a6b9eda2159fde5b5eccf3e99c4e Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Sun, 28 Aug 2022 20:13:55 -0400 Subject: [PATCH] Added and passed comparison test --- creature.go | 2 +- creature_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/creature.go b/creature.go index a40039c..1a338e3 100644 --- a/creature.go +++ b/creature.go @@ -144,7 +144,7 @@ func (machine *Machine) Execute(offset int) (err error) { // checks if the last word on the stack is less than the // second to last word on the stack less := 0 - if machine.Pop() > machine.Pop() { + if machine.Pop() < machine.Pop() { less = 1 } machine.Push(less) diff --git a/creature_test.go b/creature_test.go index 39bbd57..6db50e9 100644 --- a/creature_test.go +++ b/creature_test.go @@ -45,8 +45,13 @@ func TestArithmetic(test *testing.T) { PUSH, 3, PUSH, 12, DIV, + + PUSH, 6, + PUSH, 8, + MOD, }, test) + modResult := machine.Pop() divResult := machine.Pop() mulResult := machine.Pop() subResult := machine.Pop() @@ -56,6 +61,7 @@ func TestArithmetic(test *testing.T) { test.Log("sub:", subResult) test.Log("mul:", mulResult) test.Log("div:", divResult) + test.Log("mod:", modResult) if addResult != 5 { test.Log("add result should be", 5) @@ -76,4 +82,60 @@ func TestArithmetic(test *testing.T) { test.Log("div result should be", 4) test.Fail() } + + if modResult != 2 { + test.Log("mod result should be", 2) + test.Fail() + } +} + +func TestComparison(test *testing.T) { + machine := runMachineTest([]int { + PUSH, 6, + PUSH, 6, + EQ, + + PUSH, 4, + PUSH, 324, + GT, + + PUSH, 324, + PUSH, 4, + LT, + + PUSH, 6, + PUSH, 54, + NEQ, + + }, test) + + 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() + } }