1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package test
import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"internal/testenv"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
// runGenTest runs a test-generator, then runs the generated test.
// Generated test can either fail in compilation or execution.
// The environment variable parameter(s) is passed to the run
// of the generated test.
func runGenTest(t *testing.T, filename, tmpname string, ev ...string) {
testenv.MustHaveGoRun(t)
gotool := testenv.GoToolPath(t)
var stdout, stderr bytes.Buffer
cmd := testenv.Command(t, gotool, "run", filepath.Join("testdata", filename))
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
t.Fatalf("Failed: %v:\nOut: %s\nStderr: %s\n", err, &stdout, &stderr)
}
// Write stdout into a temporary file
rungo := filepath.Join(t.TempDir(), "run.go")
ok := os.WriteFile(rungo, stdout.Bytes(), 0600)
if ok != nil {
t.Fatalf("Failed to create temporary file " + rungo)
}
stdout.Reset()
stderr.Reset()
cmd = testenv.Command(t, gotool, "run", "-gcflags=-d=ssa/check/on", rungo)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Env = append(cmd.Env, ev...)
err := cmd.Run()
if err != nil {
t.Fatalf("Failed: %v:\nOut: %s\nStderr: %s\n", err, &stdout, &stderr)
}
if s := stderr.String(); s != "" {
t.Errorf("Stderr = %s\nWant empty", s)
}
if s := stdout.String(); s != "" {
t.Errorf("Stdout = %s\nWant empty", s)
}
}
func TestGenFlowGraph(t *testing.T) {
if testing.Short() {
t.Skip("not run in short mode.")
}
runGenTest(t, "flowgraph_generator1.go", "ssa_fg_tmp1")
}
// TestCode runs all the tests in the testdata directory as subtests.
// These tests are special because we want to run them with different
// compiler flags set (and thus they can't just be _test.go files in
// this directory).
func TestCode(t *testing.T) {
testenv.MustHaveGoBuild(t)
gotool := testenv.GoToolPath(t)
// Make a temporary directory to work in.
tmpdir := t.TempDir()
// Find all the test functions (and the files containing them).
var srcs []string // files containing Test functions
type test struct {
name string // TestFoo
usesFloat bool // might use float operations
}
var tests []test
files, err := os.ReadDir("testdata")
if err != nil {
t.Fatalf("can't read testdata directory: %v", err)
}
for _, f := range files {
if !strings.HasSuffix(f.Name(), "_test.go") {
continue
}
text, err := os.ReadFile(filepath.Join("testdata", f.Name()))
if err != nil {
t.Fatalf("can't read testdata/%s: %v", f.Name(), err)
}
fset := token.NewFileSet()
code, err := parser.ParseFile(fset, f.Name(), text, 0)
if err != nil {
t.Fatalf("can't parse testdata/%s: %v", f.Name(), err)
}
srcs = append(srcs, filepath.Join("testdata", f.Name()))
foundTest := false
for _, d := range code.Decls {
fd, ok := d.(*ast.FuncDecl)
if !ok {
continue
}
if !strings.HasPrefix(fd.Name.Name, "Test") {
continue
}
if fd.Recv != nil {
continue
}
if fd.Type.Results != nil {
continue
}
if len(fd.Type.Params.List) != 1 {
continue
}
p := fd.Type.Params.List[0]
if len(p.Names) != 1 {
continue
}
s, ok := p.Type.(*ast.StarExpr)
if !ok {
continue
}
sel, ok := s.X.(*ast.SelectorExpr)
if !ok {
continue
}
base, ok := sel.X.(*ast.Ident)
if !ok {
continue
}
if base.Name != "testing" {
continue
}
if sel.Sel.Name != "T" {
continue
}
// Found a testing function.
tests = append(tests, test{name: fd.Name.Name, usesFloat: bytes.Contains(text, []byte("float"))})
foundTest = true
}
if !foundTest {
t.Fatalf("test file testdata/%s has no tests in it", f.Name())
}
}
flags := []string{""}
if runtime.GOARCH == "arm" || runtime.GOARCH == "mips" || runtime.GOARCH == "mips64" || runtime.GOARCH == "386" {
flags = append(flags, ",softfloat")
}
for _, flag := range flags {
args := []string{"test", "-c", "-gcflags=-d=ssa/check/on" + flag, "-o", filepath.Join(tmpdir, "code.test")}
args = append(args, srcs...)
out, err := testenv.Command(t, gotool, args...).CombinedOutput()
if err != nil || len(out) != 0 {
t.Fatalf("Build failed: %v\n%s\n", err, out)
}
// Now we have a test binary. Run it with all the tests as subtests of this one.
for _, test := range tests {
test := test
if flag == ",softfloat" && !test.usesFloat {
// No point in running the soft float version if the test doesn't use floats.
continue
}
t.Run(fmt.Sprintf("%s%s", test.name[4:], flag), func(t *testing.T) {
out, err := testenv.Command(t, filepath.Join(tmpdir, "code.test"), "-test.run=^"+test.name+"$").CombinedOutput()
if err != nil || string(out) != "PASS\n" {
t.Errorf("Failed:\n%s\n", out)
}
})
}
}
}
|