From feacc27f495f61783ce138c9382af0cc861dbf6c Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 8 Feb 2025 00:40:41 +0100 Subject: [PATCH] Added more tests --- src/scanner/scanFile.go | 5 +-- src/types/types_test.go | 55 ++++++++++++++++++++++++++++ src/x86/{x64_test.go => x86_test.go} | 2 +- 3 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 src/types/types_test.go rename src/x86/{x64_test.go => x86_test.go} (96%) diff --git a/src/scanner/scanFile.go b/src/scanner/scanFile.go index 79cf791..3b1bff2 100644 --- a/src/scanner/scanFile.go +++ b/src/scanner/scanFile.go @@ -26,9 +26,8 @@ func (s *Scanner) scanFile(path string, pkg string) error { } s.files <- file - i := 0 - for i < len(tokens) { + for i := 0; i < len(tokens); i++ { switch tokens[i].Kind { case token.NewLine: case token.Comment: @@ -49,8 +48,6 @@ func (s *Scanner) scanFile(path string, pkg string) error { if err != nil { return err } - - i++ } return nil diff --git a/src/types/types_test.go b/src/types/types_test.go new file mode 100644 index 0000000..d5b6ba8 --- /dev/null +++ b/src/types/types_test.go @@ -0,0 +1,55 @@ +package types_test + +import ( + "testing" + + "git.akyoto.dev/cli/q/src/types" + "git.akyoto.dev/go/assert" +) + +func TestName(t *testing.T) { + assert.Equal(t, types.Int.Name(), "Int64") + assert.Equal(t, types.PointerAny.Name(), "Pointer") + assert.Equal(t, (&types.Pointer{To: types.Int}).Name(), "Pointer:Int64") +} + +func TestSize(t *testing.T) { + assert.Equal(t, types.Int.Size(), 8) + assert.Equal(t, types.Int8.Size(), 1) + assert.Equal(t, types.Int16.Size(), 2) + assert.Equal(t, types.Int32.Size(), 4) + assert.Equal(t, types.Int64.Size(), 8) + assert.Equal(t, types.PointerAny.Size(), 8) + assert.Equal(t, (&types.Pointer{To: types.Int}).Size(), 8) +} + +func TestStruct(t *testing.T) { + s := types.NewStruct("Test") + assert.Equal(t, s.Name(), "Test") + assert.Equal(t, s.Size(), 0) + field := &types.Field{Name: "TestField", TypeName: "Int8"} + s.AddField(field) + s.Update(map[string]types.Type{"Int8": types.Int8}) + assert.Equal(t, s.Size(), 1) + assert.Equal(t, s.FieldByName("TestField"), field) + assert.Nil(t, s.FieldByName("does-not-exist")) +} + +func TestBasics(t *testing.T) { + assert.True(t, types.Is(types.Int, types.Int)) + assert.True(t, types.Is(types.PointerAny, types.PointerAny)) + assert.False(t, types.Is(types.Int, types.Float)) + assert.False(t, types.Is(&types.Pointer{To: types.Int}, &types.Pointer{To: types.Float})) +} + +func TestSpecialCases(t *testing.T) { + // Case #1: + // For syscalls whose return type is `nil` we currently allow casting them to anything. + assert.True(t, types.Is(nil, types.Int)) + assert.True(t, types.Is(nil, types.Float)) + + // Case #2: + // A pointer pointing to a known type fulfills the requirement of a pointer to anything. + assert.True(t, types.Is(&types.Pointer{To: types.Int}, &types.Pointer{To: nil})) + assert.True(t, types.Is(&types.Pointer{To: types.Float}, &types.Pointer{To: nil})) +} diff --git a/src/x86/x64_test.go b/src/x86/x86_test.go similarity index 96% rename from src/x86/x64_test.go rename to src/x86/x86_test.go index f9e88e4..12efd4d 100644 --- a/src/x86/x64_test.go +++ b/src/x86/x86_test.go @@ -7,7 +7,7 @@ import ( "git.akyoto.dev/go/assert" ) -func TestX64(t *testing.T) { +func TestX86(t *testing.T) { assert.DeepEqual(t, x86.AlignStack(nil), []byte{0x48, 0x83, 0xE4, 0xF0}) assert.DeepEqual(t, x86.Call(nil, 1), []byte{0xE8, 0x01, 0x00, 0x00, 0x00}) assert.DeepEqual(t, x86.CallAtAddress(nil, 1), []byte{0xFF, 0x15, 0x01, 0x00, 0x00, 0x00})