test asmtest with instruction list

This commit is contained in:
Michael McLoughlin
2018-11-24 14:20:04 -08:00
parent bec73ca7a1
commit 898d66c585
6 changed files with 37 additions and 23 deletions

View File

@@ -20,10 +20,10 @@ func NewAsmTest(cfg Config) Interface {
return &asmtest{cfg: cfg} return &asmtest{cfg: cfg}
} }
func (a *asmtest) Generate(is []*inst.Instruction) ([]byte, error) { func (a *asmtest) Generate(is []inst.Instruction) ([]byte, error) {
p := &printer{} p := &printer{}
p.Printf("# %s\n\n", a.cfg.GeneratedWarning()) p.Printf("// %s\n\n", a.cfg.GeneratedWarning())
a.sym = "\u00b7loadertest(SB)" a.sym = "\u00b7loadertest(SB)"
p.Printf("TEXT %s, 0, $0\n", a.sym) p.Printf("TEXT %s, 0, $0\n", a.sym)

View File

@@ -10,12 +10,12 @@ import (
) )
type Interface interface { type Interface interface {
Generate([]*inst.Instruction) ([]byte, error) Generate([]inst.Instruction) ([]byte, error)
} }
type Func func([]*inst.Instruction) ([]byte, error) type Func func([]inst.Instruction) ([]byte, error)
func (f Func) Generate(is []*inst.Instruction) ([]byte, error) { func (f Func) Generate(is []inst.Instruction) ([]byte, error) {
return f(is) return f(is)
} }
@@ -39,7 +39,7 @@ type Builder func(Config) Interface
// GoFmt formats Go code produced from the given generator. // GoFmt formats Go code produced from the given generator.
func GoFmt(i Interface) Interface { func GoFmt(i Interface) Interface {
return Func(func(is []*inst.Instruction) ([]byte, error) { return Func(func(is []inst.Instruction) ([]byte, error) {
b, err := i.Generate(is) b, err := i.Generate(is)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -12,7 +12,7 @@ func NewGoData(cfg Config) Interface {
return GoFmt(godata{cfg: cfg}) return GoFmt(godata{cfg: cfg})
} }
func (g godata) Generate(is []*inst.Instruction) ([]byte, error) { func (g godata) Generate(is []inst.Instruction) ([]byte, error) {
p := &printer{} p := &printer{}
p.Printf("// %s\n\n", g.cfg.GeneratedWarning()) p.Printf("// %s\n\n", g.cfg.GeneratedWarning())

View File

@@ -1,9 +1,15 @@
package inst package inst_test
import "testing" import (
"testing"
"github.com/mmcloughlin/avo/internal/gen"
"github.com/mmcloughlin/avo/internal/inst"
"github.com/mmcloughlin/avo/internal/test"
)
func TestHaveInstructions(t *testing.T) { func TestHaveInstructions(t *testing.T) {
n := len(Instructions) n := len(inst.Instructions)
t.Logf("number of instructions = %d", n) t.Logf("number of instructions = %d", n)
if n == 0 { if n == 0 {
t.Fatalf("no instructions") t.Fatalf("no instructions")
@@ -12,7 +18,7 @@ func TestHaveInstructions(t *testing.T) {
func TestOpcodeDupes(t *testing.T) { func TestOpcodeDupes(t *testing.T) {
count := map[string]int{} count := map[string]int{}
for _, i := range Instructions { for _, i := range inst.Instructions {
count[i.Opcode]++ count[i.Opcode]++
} }
@@ -24,7 +30,7 @@ func TestOpcodeDupes(t *testing.T) {
} }
func TestInstructionProperties(t *testing.T) { func TestInstructionProperties(t *testing.T) {
for _, i := range Instructions { for _, i := range inst.Instructions {
if len(i.Opcode) == 0 { if len(i.Opcode) == 0 {
t.Errorf("empty opcode") t.Errorf("empty opcode")
} }
@@ -34,3 +40,12 @@ func TestInstructionProperties(t *testing.T) {
} }
} }
func TestAssembles(t *testing.T) {
g := gen.NewAsmTest(gen.Config{})
b, err := g.Generate(inst.Instructions)
if err != nil {
t.Fatal(err)
}
test.Assembles(t, b)
}

View File

@@ -31,7 +31,7 @@ func NewLoaderFromDataDir(dir string) *Loader {
} }
} }
func (l *Loader) Load() ([]*inst.Instruction, error) { func (l *Loader) Load() ([]inst.Instruction, error) {
if err := l.init(); err != nil { if err := l.init(); err != nil {
return nil, err return nil, err
} }
@@ -63,9 +63,9 @@ func (l *Loader) Load() ([]*inst.Instruction, error) {
} }
// Convert to a slice, sorted by opcode. // Convert to a slice, sorted by opcode.
is := make([]*inst.Instruction, 0, len(im)) is := make([]inst.Instruction, 0, len(im))
for _, i := range im { for _, i := range im {
is = append(is, i) is = append(is, *i)
} }
sort.Slice(is, func(i, j int) bool { sort.Slice(is, func(i, j int) bool {

View File

@@ -1,7 +1,6 @@
package load_test package load_test
import ( import (
"bytes"
"testing" "testing"
"github.com/mmcloughlin/avo/internal/gen" "github.com/mmcloughlin/avo/internal/gen"
@@ -10,7 +9,7 @@ import (
"github.com/mmcloughlin/avo/internal/test" "github.com/mmcloughlin/avo/internal/test"
) )
func Load(t *testing.T) []*inst.Instruction { func Load(t *testing.T) []inst.Instruction {
t.Helper() t.Helper()
l := load.NewLoaderFromDataDir("testdata") l := load.NewLoaderFromDataDir("testdata")
is, err := l.Load() is, err := l.Load()
@@ -22,10 +21,10 @@ func Load(t *testing.T) []*inst.Instruction {
func TestAssembles(t *testing.T) { func TestAssembles(t *testing.T) {
is := Load(t) is := Load(t)
g := gen.NewAsmTest(gen.Config{})
g := &gen.LoaderTest{} b, err := g.Generate(is)
var buf bytes.Buffer if err != nil {
g.Generate(&buf, is) t.Fatal(err)
}
test.Assembles(t, buf.Bytes()) test.Assembles(t, b)
} }