Skip to content

Commit

Permalink
Add go test
Browse files Browse the repository at this point in the history
  • Loading branch information
DQNEO committed Jul 9, 2023
1 parent e12f109 commit 9aaf114
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ goas: $(GO_SOURCES)

.PHONY: test
# Check /etc/os-release to prevent non-linux from running this
test: test1 test2 test3
test: gotest test1 test2 test3

.PHONY: gotest
gotest:
go test .

T1_SOURCES = $(wildcard t1/*.s)
T1_GNU_OBJS = $(T1_SOURCES:t1/%.s=out/g/1/%.o)
Expand Down
13 changes: 13 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,19 @@ func showVersion() {
fmt.Println("goas assembler version " + Version)
}

// For unit tests
func EncodeString(source string) ([]byte, []byte) {
sc := &symbolCollection{
symbolsAppeared: make(map[string]bool),
}
stmts, symbolsInLexicalOrder := ParseString(source, sc)
textStmts, dataStmts, labeledSymbols := analyzeStatements(stmts)
allText := encodeAllText(textStmts, labeledSymbols)
allData := encodeAllData(dataStmts, labeledSymbols)
_ = symbolsInLexicalOrder
return allText, allData
}

func main() {
flag.Parse()

Expand Down
30 changes: 30 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"reflect"
"testing"
)

func TestEncodeStringAsText(t *testing.T) {
tests := []struct {
name string
source string
encoded []byte
}{
// Simple instructions
{"ret", "ret", []byte{0xc3}},
{"nop", "nop", []byte{0x90}},
{"syscall", "syscall", []byte{0x0f, 0x05}},
{"leave", "leave", []byte{0xc9}},
{"movq 64", "movq $1, %rax", []byte{0x48, 0xc7, 0xc0, 0x01, 0x00, 0x00, 0x00}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
encoded, data := EncodeString(tt.source)
_ = data
if !reflect.DeepEqual(encoded, tt.encoded) {
t.Errorf("EncodeString() got = %v, want %v", encoded, tt.encoded)
}
})
}
}
16 changes: 15 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (p *parser) readCh() byte {

func (p *parser) peekCh() byte {
if p.idx == len(p.source) {
panic("Sudden EOF")
panic(fmt.Sprintf("Sudden EOF at idx=%d", p.idx))
}
return p.source[p.idx]
}
Expand Down Expand Up @@ -580,3 +580,17 @@ func ParseFile(path string, sc *symbolCollection) []*Stmt {
stmts := p.parse()
return stmts
}

// For unit tests
func ParseString(src string, sc *symbolCollection) ([]*Stmt, []string) {
p := &parser{
path: "string",
lineno: 1,
source: []byte(src + "\n"),
idx: 0,
sc: sc,
}
stmts := p.parse()
return stmts, sc.symbolsInLexicalOrder

}
2 changes: 1 addition & 1 deletion t1/05.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ nop
syscall
leave
ret

movq $1, %rax
movq %rcx,0x8(%rsi)
movq %rax, 0(%rsi)
movq 8(%rax), %rdx
Expand Down

0 comments on commit 9aaf114

Please sign in to comment.