Added and passed comparison test
This commit is contained in:
parent
884f00d048
commit
0f1285bbb9
@ -144,7 +144,7 @@ func (machine *Machine) Execute(offset int) (err error) {
|
|||||||
// checks if the last word on the stack is less than the
|
// checks if the last word on the stack is less than the
|
||||||
// second to last word on the stack
|
// second to last word on the stack
|
||||||
less := 0
|
less := 0
|
||||||
if machine.Pop() > machine.Pop() {
|
if machine.Pop() < machine.Pop() {
|
||||||
less = 1
|
less = 1
|
||||||
}
|
}
|
||||||
machine.Push(less)
|
machine.Push(less)
|
||||||
|
@ -45,8 +45,13 @@ func TestArithmetic(test *testing.T) {
|
|||||||
PUSH, 3,
|
PUSH, 3,
|
||||||
PUSH, 12,
|
PUSH, 12,
|
||||||
DIV,
|
DIV,
|
||||||
|
|
||||||
|
PUSH, 6,
|
||||||
|
PUSH, 8,
|
||||||
|
MOD,
|
||||||
}, test)
|
}, test)
|
||||||
|
|
||||||
|
modResult := machine.Pop()
|
||||||
divResult := machine.Pop()
|
divResult := machine.Pop()
|
||||||
mulResult := machine.Pop()
|
mulResult := machine.Pop()
|
||||||
subResult := machine.Pop()
|
subResult := machine.Pop()
|
||||||
@ -56,6 +61,7 @@ func TestArithmetic(test *testing.T) {
|
|||||||
test.Log("sub:", subResult)
|
test.Log("sub:", subResult)
|
||||||
test.Log("mul:", mulResult)
|
test.Log("mul:", mulResult)
|
||||||
test.Log("div:", divResult)
|
test.Log("div:", divResult)
|
||||||
|
test.Log("mod:", modResult)
|
||||||
|
|
||||||
if addResult != 5 {
|
if addResult != 5 {
|
||||||
test.Log("add result should be", 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.Log("div result should be", 4)
|
||||||
test.Fail()
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user