@@ -12,8 +12,10 @@ import (
12
12
"os"
13
13
"os/exec"
14
14
"path/filepath"
15
+ "regexp"
15
16
"runtime"
16
17
"sort"
18
+ "strconv"
17
19
"strings"
18
20
"sync"
19
21
"testing"
@@ -22,12 +24,31 @@ import (
22
24
"github.com/tinygo-org/tinygo/builder"
23
25
"github.com/tinygo-org/tinygo/compileopts"
24
26
"github.com/tinygo-org/tinygo/goenv"
27
+ "tinygo.org/x/go-llvm"
25
28
)
26
29
27
30
const TESTDATA = "testdata"
28
31
29
32
var testTarget = flag .String ("target" , "" , "override test target" )
30
33
34
+ // There are a lot of tests that don't yet pass on AVR, often because avr-libc
35
+ // doesn't provide the required functions (for float64 for example).
36
+ var skipOnAVR = map [string ]struct {}{
37
+ "testdata/atomic.go" : {},
38
+ "testdata/cgo/" : {},
39
+ "testdata/channel.go" : {},
40
+ "testdata/coroutines.go" : {},
41
+ "testdata/float.go" : {},
42
+ "testdata/gc.go" : {},
43
+ "testdata/interface.go" : {},
44
+ "testdata/map.go" : {},
45
+ "testdata/math.go" : {},
46
+ "testdata/print.go" : {},
47
+ "testdata/reflect.go" : {},
48
+ "testdata/stdlib.go" : {},
49
+ "testdata/structs.go" : {},
50
+ }
51
+
31
52
func TestCompiler (t * testing.T ) {
32
53
matches , err := filepath .Glob (filepath .Join (TESTDATA , "*.go" ))
33
54
if err != nil {
@@ -105,6 +126,15 @@ func TestCompiler(t *testing.T) {
105
126
t .Run ("WASI" , func (t * testing.T ) {
106
127
runPlatTests ("wasi" , matches , t )
107
128
})
129
+
130
+ llvmMajorVersion , _ := strconv .ParseInt (strings .Split (llvm .Version , "." )[0 ], 10 , 32 )
131
+ if llvmMajorVersion >= 11 {
132
+ // The AVR backend in LLVM 11 has been significantly improved and is
133
+ // able to correctly compile a lot more tests than before.
134
+ t .Run ("AVR" , func (t * testing.T ) {
135
+ runPlatTests ("atmega1284p" , matches , t )
136
+ })
137
+ }
108
138
}
109
139
}
110
140
@@ -114,6 +144,13 @@ func runPlatTests(target string, matches []string, t *testing.T) {
114
144
for _ , path := range matches {
115
145
path := path // redefine to avoid race condition
116
146
147
+ if target == "atmega1284p" {
148
+ if _ , ok := skipOnAVR [path ]; ok {
149
+ // Some tests don't work on AVR yet, so skip them.
150
+ continue
151
+ }
152
+ }
153
+
117
154
t .Run (filepath .Base (path ), func (t * testing.T ) {
118
155
t .Parallel ()
119
156
runTest (path , target , t )
@@ -180,6 +217,7 @@ func runTest(path, target string, t *testing.T) {
180
217
runComplete := make (chan struct {})
181
218
var cmd * exec.Cmd
182
219
ranTooLong := false
220
+ var emulator = ""
183
221
if target == "" {
184
222
cmd = exec .Command (binary )
185
223
} else {
@@ -190,13 +228,19 @@ func runTest(path, target string, t *testing.T) {
190
228
if len (spec .Emulator ) == 0 {
191
229
cmd = exec .Command (binary )
192
230
} else {
231
+ emulator = spec .Emulator [0 ]
193
232
args := append (spec .Emulator [1 :], binary )
194
233
cmd = exec .Command (spec .Emulator [0 ], args ... )
195
234
}
196
235
}
197
236
stdout := & bytes.Buffer {}
198
- cmd .Stdout = stdout
199
- cmd .Stderr = os .Stderr
237
+ if emulator == "simavr" {
238
+ cmd .Stdout = nil
239
+ cmd .Stderr = stdout
240
+ } else {
241
+ cmd .Stdout = stdout
242
+ cmd .Stderr = os .Stderr
243
+ }
200
244
err = cmd .Start ()
201
245
if err != nil {
202
246
t .Fatal ("failed to start:" , err )
@@ -230,6 +274,13 @@ func runTest(path, target string, t *testing.T) {
230
274
actual := bytes .Replace (stdout .Bytes (), []byte {'\r' , '\n' }, []byte {'\n' }, - 1 )
231
275
expected = bytes .Replace (expected , []byte {'\r' , '\n' }, []byte {'\n' }, - 1 ) // for Windows
232
276
277
+ if emulator == "simavr" {
278
+ // Munge the data a bit to remove escape characters and the two dots
279
+ // simavr likes to put at the end of each line.
280
+ actual = regexp .MustCompile ("\x1b .*?m" ).ReplaceAll (actual , nil )
281
+ actual = regexp .MustCompile ("\\ .\\ .\n " ).ReplaceAll (actual , []byte {'\n' })
282
+ }
283
+
233
284
// Check whether the command ran successfully.
234
285
fail := false
235
286
if err != nil {
0 commit comments