Added more tests

This commit is contained in:
Eduard Urbach 2024-07-17 19:50:46 +02:00
parent 8b2cdfa841
commit 68e21c96e3
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
5 changed files with 65 additions and 3 deletions

View File

@ -48,7 +48,6 @@ func (f *Function) AddVariable(variable *Variable) {
variable.Scope = scope variable.Scope = scope
scope.variables[variable.Name] = variable scope.variables[variable.Name] = variable
scope.Reserve(variable.Register)
scope.Use(variable.Register) scope.Use(variable.Register)
} }

View File

@ -0,0 +1,13 @@
package cpu_test
import (
"testing"
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/go/assert"
)
func TestRegisterString(t *testing.T) {
register := cpu.Register(1)
assert.Equal(t, "r1", register.String())
}

View File

@ -35,7 +35,7 @@ func (s *State) Use(reg Register) {
// FindFree tries to find a free register in the given slice of registers. // FindFree tries to find a free register in the given slice of registers.
func (s *State) FindFree(registers []Register) (Register, bool) { func (s *State) FindFree(registers []Register) (Register, bool) {
for _, reg := range registers { for _, reg := range registers {
if !s.IsReserved(reg) { if !s.IsReserved(reg) && !s.IsUsed(reg) {
return reg, true return reg, true
} }
} }

View File

@ -0,0 +1,45 @@
package cpu_test
import (
"testing"
"git.akyoto.dev/cli/q/src/build/cpu"
"git.akyoto.dev/go/assert"
)
func TestRegisterState(t *testing.T) {
s := cpu.State{}
assert.False(t, s.IsReserved(0))
assert.False(t, s.IsUsed(0))
s.Reserve(0)
assert.True(t, s.IsReserved(0))
assert.False(t, s.IsUsed(0))
s.Use(0)
assert.True(t, s.IsReserved(0))
assert.True(t, s.IsUsed(0))
s.Free(0)
assert.False(t, s.IsReserved(0))
assert.False(t, s.IsUsed(0))
}
func TestFindFree(t *testing.T) {
s := cpu.State{}
s.Reserve(0)
s.Use(1)
reg, found := s.FindFree([]cpu.Register{0, 1, 2, 3})
assert.True(t, found)
assert.Equal(t, reg, 2)
_, found = s.FindFree([]cpu.Register{0, 1})
assert.False(t, found)
}
func TestMustFindFree(t *testing.T) {
s := cpu.State{}
s.Reserve(0)
s.Use(1)
reg := s.MustFindFree([]cpu.Register{0, 1, 2, 3})
assert.Equal(t, reg, 2)
}

View File

@ -19,7 +19,12 @@ func TestWalk(t *testing.T) {
assert.Contains(t, files, "Walk_test.go") assert.Contains(t, files, "Walk_test.go")
} }
func TestNonExisting(t *testing.T) { func TestWalkFile(t *testing.T) {
err := fs.Walk("Walk.go", func(file string) {})
assert.NotNil(t, err)
}
func TestWalkNonExisting(t *testing.T) {
err := fs.Walk("does-not-exist", func(file string) {}) err := fs.Walk("does-not-exist", func(file string) {})
assert.NotNil(t, err) assert.NotNil(t, err)
} }