@@ -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 {
@@ -77,6 +98,19 @@ func TestCompiler(t *testing.T) {
77
98
})
78
99
}
79
100
101
+ if runtime .GOOS == "darwin" {
102
+ // Running AVR tests only on Darwin as it has an easily installed
103
+ // homebrew simavr package.
104
+ llvmMajorVersion , _ := strconv .ParseInt (strings .Split (llvm .Version , "." )[0 ], 10 , 32 )
105
+ if llvmMajorVersion >= 11 {
106
+ // The AVR backend in LLVM 11 has been significantly improved and is
107
+ // able to correctly compile a lot more tests than before.
108
+ t .Run ("AVR" , func (t * testing.T ) {
109
+ runPlatTests ("atmega1284p" , matches , t )
110
+ })
111
+ }
112
+ }
113
+
80
114
if runtime .GOOS == "linux" {
81
115
t .Run ("X86Linux" , func (t * testing.T ) {
82
116
runPlatTests ("i386--linux-gnu" , matches , t )
@@ -114,6 +148,13 @@ func runPlatTests(target string, matches []string, t *testing.T) {
114
148
for _ , path := range matches {
115
149
path := path // redefine to avoid race condition
116
150
151
+ if target == "atmega1284p" {
152
+ if _ , ok := skipOnAVR [path ]; ok {
153
+ // Some tests don't work on AVR yet, so skip them.
154
+ continue
155
+ }
156
+ }
157
+
117
158
t .Run (filepath .Base (path ), func (t * testing.T ) {
118
159
t .Parallel ()
119
160
runTest (path , target , t )
@@ -180,6 +221,7 @@ func runTest(path, target string, t *testing.T) {
180
221
runComplete := make (chan struct {})
181
222
var cmd * exec.Cmd
182
223
ranTooLong := false
224
+ var emulator = ""
183
225
if target == "" {
184
226
cmd = exec .Command (binary )
185
227
} else {
@@ -190,13 +232,19 @@ func runTest(path, target string, t *testing.T) {
190
232
if len (spec .Emulator ) == 0 {
191
233
cmd = exec .Command (binary )
192
234
} else {
235
+ emulator = spec .Emulator [0 ]
193
236
args := append (spec .Emulator [1 :], binary )
194
237
cmd = exec .Command (spec .Emulator [0 ], args ... )
195
238
}
196
239
}
197
240
stdout := & bytes.Buffer {}
198
- cmd .Stdout = stdout
199
- cmd .Stderr = os .Stderr
241
+ if emulator == "simavr" {
242
+ cmd .Stdout = nil
243
+ cmd .Stderr = stdout
244
+ } else {
245
+ cmd .Stdout = stdout
246
+ cmd .Stderr = os .Stderr
247
+ }
200
248
err = cmd .Start ()
201
249
if err != nil {
202
250
t .Fatal ("failed to start:" , err )
@@ -230,6 +278,13 @@ func runTest(path, target string, t *testing.T) {
230
278
actual := bytes .Replace (stdout .Bytes (), []byte {'\r' , '\n' }, []byte {'\n' }, - 1 )
231
279
expected = bytes .Replace (expected , []byte {'\r' , '\n' }, []byte {'\n' }, - 1 ) // for Windows
232
280
281
+ if emulator == "simavr" {
282
+ // Munge the data a bit to remove escape characters and the two dots
283
+ // simavr likes to put at the end of each line.
284
+ actual = regexp .MustCompile ("\x1b .*?m" ).ReplaceAll (actual , nil )
285
+ actual = regexp .MustCompile ("\\ .\\ .\n " ).ReplaceAll (actual , []byte {'\n' })
286
+ }
287
+
233
288
// Check whether the command ran successfully.
234
289
fail := false
235
290
if err != nil {
0 commit comments