diff --git a/elm_test.go b/elm_test.go index cba14e1..2e95868 100644 --- a/elm_test.go +++ b/elm_test.go @@ -404,3 +404,64 @@ func TestJumpIfDontPassIllegalAccess(t * testing.T) { assert.NotNil(t, err, "Should be errored"); } + +func TestCallDontPassIllegallAccess(t *testing.T) { + m := Constructor() + m.Push(Inst{Operation: PUSH, Operand: 6}) + m.Push(Inst{Operation: CALL, Operand: 3}) + m.Push(Inst{Operation: HALT}) + + err := m.Run() + + assert.NotNil(t, err, "Should be errored"); +} + +func TestCallDontPassIllegallAccess2(t *testing.T) { + m := Constructor() + m.Push(Inst{Operation: PUSH, Operand: 6}) + m.Push(Inst{Operation: CALL, Operand: -3}) + m.Push(Inst{Operation: HALT}) + + err := m.Run() + + assert.NotNil(t, err, "Should be errored"); +} + +func TestCallPass(t * testing.T) { + m := Constructor() + m.Push(Inst{Operation: PUSH, Operand: 1}) + m.Push(Inst{Operation: CALL, Operand: 3}) + m.Push(Inst{Operation: PUSH, Operand: 6}) + m.Push(Inst{Operation: PUSH, Operand: 69}) + m.Push(Inst{Operation: HALT}) + + err := m.Run() + + assert.Nil(t, err, "Should be successfull") + assert.Equal(t, int32(4), m.ip, "Instruction pointer should be incremented"); + assert.True(t, m.isHalted, "Virtual Machine should be Halted"); + assert.Equal(t, int32(1), m.stack[0], "Should skip the pushed value") + assert.Equal(t, int32(69), m.stack[1], "Should skip the pushed value") + assert.Equal(t, int32(0), m.stack[2], "Should be zero afterall") +} + +func TestRetPass(t * testing.T) { + m := Constructor() + m.Push(Inst{Operation: PUSH, Operand: 1}) + m.Push(Inst{Operation: CALL, Operand: 4}) + m.Push(Inst{Operation: PUSH, Operand: 6}) + m.Push(Inst{Operation: HALT}) + m.Push(Inst{Operation: PUSH, Operand: 69}) + m.Push(Inst{Operation: RET}) + + + err := m.Run() + + assert.Nil(t, err, "Should be successfull") + assert.Equal(t, int32(3), m.ip, "Instruction pointer should be incremented"); + assert.True(t, m.isHalted, "Virtual Machine should be Halted"); + assert.Equal(t, int32(1), m.stack[0], "Should skip the pushed value") + assert.Equal(t, int32(69), m.stack[1], "Should skip the pushed value") + assert.Equal(t, int32(6), m.stack[2], "Should skip the pushed value") + assert.Equal(t, int32(0), m.stack[3], "Should be zero afterall") +}