From c00024039f3f95992df7c81f5c5ab9b4164fa928 Mon Sep 17 00:00:00 2001 From: Mahdi Khosravi Date: Wed, 20 Sep 2023 13:12:57 +0300 Subject: [PATCH] added getCellDst test with negative offset --- pkg/vm/vm_test.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/pkg/vm/vm_test.go b/pkg/vm/vm_test.go index 8442305f..350de9cc 100644 --- a/pkg/vm/vm_test.go +++ b/pkg/vm/vm_test.go @@ -3,9 +3,9 @@ package vm import ( "testing" - "github.com/stretchr/testify/require" f "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" mem "github.com/NethermindEth/cairo-vm-go/pkg/vm/memory" ) @@ -80,6 +80,51 @@ func TestGetCellFpDst(t *testing.T) { assert.Equal(t, mem.MemoryValueFromInt(123), cell.Read()) } +func TestGetCellDstApNegativeOffset(t *testing.T) { + vm := defaultVirtualMachine() + + const ( + offDest = -2 + ap = 12 + ) + vm.Context.Ap = ap + + writeToDataSegment(vm, ap+offDest, mem.MemoryValueFromInt(100)) + + instruction := Instruction{ + OffDest: offDest, + DstRegister: Ap, + } + + cell, err := vm.getCellDst(&instruction) + + require.NoError(t, err) + assert.True(t, cell.Accessed) + assert.Equal(t, mem.MemoryValueFromInt(100), cell.Read()) +} + +func TestGetCellDstFpNegativeOffset(t *testing.T) { + vm := defaultVirtualMachine() + + const ( + offDest = -19 + fp = 33 + ) + vm.Context.Fp = fp + + writeToDataSegment(vm, fp+offDest, mem.MemoryValueFromInt(100)) + + instruction := Instruction{ + OffDest: offDest, + DstRegister: Fp, + } + + cell, err := vm.getCellDst(&instruction) + require.NoError(t, err) + assert.True(t, cell.Accessed) + assert.Equal(t, mem.MemoryValueFromInt(100), cell.Read()) +} + func TestGetApCellOp0(t *testing.T) { vm := defaultVirtualMachine()