Added more tests
This commit is contained in:
parent
8b2cdfa841
commit
68e21c96e3
@ -48,7 +48,6 @@ func (f *Function) AddVariable(variable *Variable) {
|
||||
variable.Scope = scope
|
||||
|
||||
scope.variables[variable.Name] = variable
|
||||
scope.Reserve(variable.Register)
|
||||
scope.Use(variable.Register)
|
||||
}
|
||||
|
||||
|
13
src/build/cpu/Register_test.go
Normal file
13
src/build/cpu/Register_test.go
Normal 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())
|
||||
}
|
@ -35,7 +35,7 @@ func (s *State) Use(reg Register) {
|
||||
// FindFree tries to find a free register in the given slice of registers.
|
||||
func (s *State) FindFree(registers []Register) (Register, bool) {
|
||||
for _, reg := range registers {
|
||||
if !s.IsReserved(reg) {
|
||||
if !s.IsReserved(reg) && !s.IsUsed(reg) {
|
||||
return reg, true
|
||||
}
|
||||
}
|
||||
|
45
src/build/cpu/State_test.go
Normal file
45
src/build/cpu/State_test.go
Normal 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)
|
||||
}
|
@ -19,7 +19,12 @@ func TestWalk(t *testing.T) {
|
||||
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) {})
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user