From 668971808b22e2a48d340d5ff110630ec24f96ec Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 27 Jun 2024 21:39:04 +0200 Subject: [PATCH] Added more tests --- src/build/arch/x64/Jump_test.go | 31 +++++++++++++++++++++ src/build/arch/x64/{sizeOf.go => SizeOf.go} | 4 +-- src/build/arch/x64/SizeOf_test.go | 21 ++++++++++++++ src/build/arch/x64/regRegNum.go | 2 +- src/build/arch/x64/x64_test.go | 11 ++++---- 5 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 src/build/arch/x64/Jump_test.go rename src/build/arch/x64/{sizeOf.go => SizeOf.go} (74%) create mode 100644 src/build/arch/x64/SizeOf_test.go diff --git a/src/build/arch/x64/Jump_test.go b/src/build/arch/x64/Jump_test.go new file mode 100644 index 0000000..ba48db0 --- /dev/null +++ b/src/build/arch/x64/Jump_test.go @@ -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) + } +} diff --git a/src/build/arch/x64/sizeOf.go b/src/build/arch/x64/SizeOf.go similarity index 74% rename from src/build/arch/x64/sizeOf.go rename to src/build/arch/x64/SizeOf.go index 8e6533b..c4804ae 100644 --- a/src/build/arch/x64/sizeOf.go +++ b/src/build/arch/x64/SizeOf.go @@ -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 diff --git a/src/build/arch/x64/SizeOf_test.go b/src/build/arch/x64/SizeOf_test.go new file mode 100644 index 0000000..eb84a59 --- /dev/null +++ b/src/build/arch/x64/SizeOf_test.go @@ -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) +} diff --git a/src/build/arch/x64/regRegNum.go b/src/build/arch/x64/regRegNum.go index 0fb41d1..3fc6987 100644 --- a/src/build/arch/x64/regRegNum.go +++ b/src/build/arch/x64/regRegNum.go @@ -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)) } diff --git a/src/build/arch/x64/x64_test.go b/src/build/arch/x64/x64_test.go index 8369738..bcbf45b 100644 --- a/src/build/arch/x64/x64_test.go +++ b/src/build/arch/x64/x64_test.go @@ -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}) }