Skip to content

Commit

Permalink
test for jump instructions abs and rel
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomi-3-0 committed Oct 25, 2023
1 parent 846d32b commit 7c93f16
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,48 @@ func TestAssertEqualInstruction(t *testing.T) {

}

// add test for each different form of jump instructions
func TestJumpInstructions(t *testing.T) {

// relative jumps
t.Run("jmp rel 5", func(t *testing.T){
vm := defaultVirtualMachine()

vm.Context.Pc = mem.MemoryAddress{SegmentIndex: 1, Offset: 4}
relAddr := uint64(5)
res := mem.MemoryValueFromInt(relAddr)

instruction := a.Instruction{
PcUpdate: a.PcUpdateJumpRel,
}
nextPc, err := vm.updatePc(&instruction, nil, nil, &res)

require.NoError(t, err)
assert.Equal(t, mem.MemoryAddress{SegmentIndex: 1, Offset: 9}, nextPc)

})

// absolute jumps
t.Run("jmp abs 123", func(t *testing.T) {
vm := defaultVirtualMachine()

vm.Context.Pc = mem.MemoryAddress{SegmentIndex: 0, Offset: 3}
jmpAddr := uint64(123)
res := mem.MemoryValueFromSegmentAndOffset(0, jmpAddr)

instruction := a.Instruction{
PcUpdate: a.PcUpdateJump,
}
nextPc, err := vm.updatePc(&instruction, nil, nil, &res)

require.NoError(t, err)
assert.Equal(t, mem.MemoryAddress{SegmentIndex: 0, Offset: jmpAddr}, nextPc)

})

}


// ======================
// Test Memory Relocation
// ======================
Expand Down

0 comments on commit 7c93f16

Please sign in to comment.