diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index e7e8b635b..7a46febca 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -557,6 +557,101 @@ func TestUpdatePcNextInstrImm(t *testing.T) { assert.Equal(t, mem.NewMemoryAddress(0, 5), nextPc) } +func TestUpdatePcJump(t *testing.T) { + vm := defaultVirtualMachine() + + vm.Context.Pc = mem.NewMemoryAddress(0, 3) + jumpAddr := uint64(10) + res := mem.MemoryValueFromSegmentAndOffset(0, jumpAddr) + + instruction := Instruction{ + PcUpdate: Jump, + } + nextPc, err := vm.updatePc(&instruction, nil, nil, res) + + require.NoError(t, err) + assert.Equal(t, mem.NewMemoryAddress(0, jumpAddr), nextPc) +} + +func TestUpdatePcJumpRel(t *testing.T) { + vm := defaultVirtualMachine() + + vm.Context.Pc = mem.NewMemoryAddress(0, 3) + relAddr := uint64(10) + res := mem.MemoryValueFromInt(relAddr) + + instruction := Instruction{ + PcUpdate: JumpRel, + } + nextPc, err := vm.updatePc(&instruction, nil, nil, res) + + require.NoError(t, err) + assert.Equal(t, mem.NewMemoryAddress(0, 3+relAddr), nextPc) +} + +func TestUpdatePcJnz(t *testing.T) { + vm := defaultVirtualMachine() + + vm.Context.Pc = mem.NewMemoryAddress(0, 11) + relAddr := uint64(10) + + res := mem.MemoryValueFromInt(10) + dstCell := &mem.Cell{ + Accessed: true, + Value: mem.MemoryValueFromInt(10), + } + op1Cell := &mem.Cell{ + Accessed: true, + Value: mem.MemoryValueFromInt(relAddr), + } + instruction := Instruction{ + PcUpdate: Jnz, + Op1Source: Op0, + } + nextPc, err := vm.updatePc(&instruction, dstCell, op1Cell, res) + + require.NoError(t, err) + assert.Equal(t, mem.NewMemoryAddress(0, 11+relAddr), nextPc) +} + +func TestUpdatePcJnzDstZero(t *testing.T) { + vm := defaultVirtualMachine() + + vm.Context.Pc = mem.NewMemoryAddress(0, 11) + + dstCell := &mem.Cell{ + Accessed: true, + Value: mem.MemoryValueFromInt(0), + } + instruction := Instruction{ + PcUpdate: Jnz, + Op1Source: Op0, + } + nextPc, err := vm.updatePc(&instruction, dstCell, nil, nil) + + require.NoError(t, err) + assert.Equal(t, mem.NewMemoryAddress(0, 11+1), nextPc) +} + +func TestUpdatePcJnzDstZeroImm(t *testing.T) { + vm := defaultVirtualMachine() + + vm.Context.Pc = mem.NewMemoryAddress(0, 9) + + dstCell := &mem.Cell{ + Accessed: true, + Value: mem.MemoryValueFromInt(0), + } + instruction := Instruction{ + PcUpdate: Jnz, + Op1Source: Imm, + } + nextPc, err := vm.updatePc(&instruction, dstCell, nil, nil) + + require.NoError(t, err) + assert.Equal(t, mem.NewMemoryAddress(0, 9+2), nextPc) +} + func TestUpdateApAddOne(t *testing.T) { vm := defaultVirtualMachine()