From 898d66c5856560e0ccaa3f7d23bf8c5c31258f31 Mon Sep 17 00:00:00 2001 From: Michael McLoughlin Date: Sat, 24 Nov 2018 14:20:04 -0800 Subject: [PATCH] test asmtest with instruction list --- internal/gen/asmtest.go | 4 ++-- internal/gen/gen.go | 8 ++++---- internal/gen/godata.go | 2 +- internal/inst/inst_test.go | 25 ++++++++++++++++++++----- internal/load/load.go | 6 +++--- internal/load/load_test.go | 15 +++++++-------- 6 files changed, 37 insertions(+), 23 deletions(-) diff --git a/internal/gen/asmtest.go b/internal/gen/asmtest.go index 9b81a99..87ce5b9 100644 --- a/internal/gen/asmtest.go +++ b/internal/gen/asmtest.go @@ -20,10 +20,10 @@ func NewAsmTest(cfg Config) Interface { 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.Printf("# %s\n\n", a.cfg.GeneratedWarning()) + p.Printf("// %s\n\n", a.cfg.GeneratedWarning()) a.sym = "\u00b7loadertest(SB)" p.Printf("TEXT %s, 0, $0\n", a.sym) diff --git a/internal/gen/gen.go b/internal/gen/gen.go index 2d939b0..39ebf94 100644 --- a/internal/gen/gen.go +++ b/internal/gen/gen.go @@ -10,12 +10,12 @@ import ( ) 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) } @@ -39,7 +39,7 @@ type Builder func(Config) Interface // GoFmt formats Go code produced from the given generator. 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) if err != nil { return nil, err diff --git a/internal/gen/godata.go b/internal/gen/godata.go index 117db4f..b2f522a 100644 --- a/internal/gen/godata.go +++ b/internal/gen/godata.go @@ -12,7 +12,7 @@ func NewGoData(cfg Config) Interface { 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.Printf("// %s\n\n", g.cfg.GeneratedWarning()) diff --git a/internal/inst/inst_test.go b/internal/inst/inst_test.go index 75a8f00..e9d1a35 100644 --- a/internal/inst/inst_test.go +++ b/internal/inst/inst_test.go @@ -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) { - n := len(Instructions) + n := len(inst.Instructions) t.Logf("number of instructions = %d", n) if n == 0 { t.Fatalf("no instructions") @@ -12,7 +18,7 @@ func TestHaveInstructions(t *testing.T) { func TestOpcodeDupes(t *testing.T) { count := map[string]int{} - for _, i := range Instructions { + for _, i := range inst.Instructions { count[i.Opcode]++ } @@ -24,7 +30,7 @@ func TestOpcodeDupes(t *testing.T) { } func TestInstructionProperties(t *testing.T) { - for _, i := range Instructions { + for _, i := range inst.Instructions { if len(i.Opcode) == 0 { 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) +} diff --git a/internal/load/load.go b/internal/load/load.go index 30bf1c4..39426cb 100644 --- a/internal/load/load.go +++ b/internal/load/load.go @@ -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 { return nil, err } @@ -63,9 +63,9 @@ func (l *Loader) Load() ([]*inst.Instruction, error) { } // 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 { - is = append(is, i) + is = append(is, *i) } sort.Slice(is, func(i, j int) bool { diff --git a/internal/load/load_test.go b/internal/load/load_test.go index 3cfcef8..6c93177 100644 --- a/internal/load/load_test.go +++ b/internal/load/load_test.go @@ -1,7 +1,6 @@ package load_test import ( - "bytes" "testing" "github.com/mmcloughlin/avo/internal/gen" @@ -10,7 +9,7 @@ import ( "github.com/mmcloughlin/avo/internal/test" ) -func Load(t *testing.T) []*inst.Instruction { +func Load(t *testing.T) []inst.Instruction { t.Helper() l := load.NewLoaderFromDataDir("testdata") is, err := l.Load() @@ -22,10 +21,10 @@ func Load(t *testing.T) []*inst.Instruction { func TestAssembles(t *testing.T) { is := Load(t) - - g := &gen.LoaderTest{} - var buf bytes.Buffer - g.Generate(&buf, is) - - test.Assembles(t, buf.Bytes()) + g := gen.NewAsmTest(gen.Config{}) + b, err := g.Generate(is) + if err != nil { + t.Fatal(err) + } + test.Assembles(t, b) }