Skip to content

Commit

Permalink
added UpdatePc tests (#61)
Browse files Browse the repository at this point in the history
* added UpdatePc tests and fixed a bug in UpdatePc

* Fixed accessing field value without Read()

* fixed failing tests

* small refactor for TestUpdatePcJump
  • Loading branch information
mmk-1 authored and jkktom committed Oct 2, 2023
1 parent c5d96d1 commit 08d37bf
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions pkg/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 08d37bf

Please sign in to comment.