Added more tests

This commit is contained in:
Eduard Urbach 2024-06-27 21:39:04 +02:00
parent 77cfe9ff31
commit 668971808b
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
5 changed files with 61 additions and 8 deletions

View File

@ -0,0 +1,31 @@
package x64_test
import (
"testing"
"git.akyoto.dev/cli/q/src/build/arch/x64"
"git.akyoto.dev/go/assert"
)
func TestJump8(t *testing.T) {
usagePatterns := []struct {
Offset int8
Code []byte
}{
{0, []byte{0xEB, 0x00}},
{1, []byte{0xEB, 0x01}},
{2, []byte{0xEB, 0x02}},
{3, []byte{0xEB, 0x03}},
{127, []byte{0xEB, 0x7F}},
{-1, []byte{0xEB, 0xFF}},
{-2, []byte{0xEB, 0xFE}},
{-3, []byte{0xEB, 0xFD}},
{-128, []byte{0xEB, 0x80}},
}
for _, pattern := range usagePatterns {
t.Logf("jmp %x", pattern.Offset)
code := x64.Jump8(nil, pattern.Offset)
assert.DeepEqual(t, code, pattern.Code)
}
}

View File

@ -2,8 +2,8 @@ package x64
import "math"
// sizeOf tells you how many bytes are needed to encode this number.
func sizeOf(number int) int {
// SizeOf tells you how many bytes are needed to encode this number.
func SizeOf(number int) int {
switch {
case number >= math.MinInt8 && number <= math.MaxInt8:
return 1

View File

@ -0,0 +1,21 @@
package x64_test
import (
"math"
"testing"
"git.akyoto.dev/cli/q/src/build/arch/x64"
"git.akyoto.dev/go/assert"
)
func TestSizeOf(t *testing.T) {
assert.Equal(t, x64.SizeOf(0), 1)
assert.Equal(t, x64.SizeOf(math.MinInt8), 1)
assert.Equal(t, x64.SizeOf(math.MaxInt8), 1)
assert.Equal(t, x64.SizeOf(math.MinInt16), 2)
assert.Equal(t, x64.SizeOf(math.MaxInt16), 2)
assert.Equal(t, x64.SizeOf(math.MinInt32), 4)
assert.Equal(t, x64.SizeOf(math.MaxInt32), 4)
assert.Equal(t, x64.SizeOf(math.MinInt64), 8)
assert.Equal(t, x64.SizeOf(math.MaxInt64), 8)
}

View File

@ -4,7 +4,7 @@ import "encoding/binary"
// regRegNum encodes an instruction with up to two registers and a number parameter.
func regRegNum(code []byte, reg byte, rm byte, number int, opCode8 byte, opCode32 byte) []byte {
if sizeOf(number) == 1 {
if SizeOf(number) == 1 {
code = regReg(code, reg, rm, opCode8)
return append(code, byte(number))
}

View File

@ -8,9 +8,10 @@ import (
)
func TestX64(t *testing.T) {
assert.DeepEqual(t, x64.Call([]byte{}, 1), []byte{0xe8, 0x01, 0x00, 0x00, 0x00})
assert.DeepEqual(t, x64.MoveRegisterNumber32([]byte{}, 0, 1), []byte{0xb8, 0x01, 0x00, 0x00, 0x00})
assert.DeepEqual(t, x64.MoveRegisterNumber32([]byte{}, 1, 1), []byte{0xb9, 0x01, 0x00, 0x00, 0x00})
assert.DeepEqual(t, x64.Return([]byte{}), []byte{0xc3})
assert.DeepEqual(t, x64.Syscall([]byte{}), []byte{0x0f, 0x05})
assert.DeepEqual(t, x64.Call(nil, 1), []byte{0xe8, 0x01, 0x00, 0x00, 0x00})
assert.DeepEqual(t, x64.MoveRegisterNumber32(nil, 0, 1), []byte{0xb8, 0x01, 0x00, 0x00, 0x00})
assert.DeepEqual(t, x64.MoveRegisterNumber32(nil, 1, 1), []byte{0xb9, 0x01, 0x00, 0x00, 0x00})
assert.DeepEqual(t, x64.Return(nil), []byte{0xc3})
assert.DeepEqual(t, x64.Syscall(nil), []byte{0x0f, 0x05})
assert.DeepEqual(t, x64.ExtendRAXToRDX(nil), []byte{0x48, 0x99})
}