1
2
3
4
5 package sgutil
6
7 import (
8 "fmt"
9 "go/parser"
10 "go/token"
11 "slices"
12 "testing"
13 )
14
15 func TestParseBuildConstraints(t *testing.T) {
16 tests := []struct {
17 filename, build string
18 phase Phase
19 stableArchOnly bool
20 }{
21
22 {"x.go", "", 0, false},
23 {"x.go", "//go:build amd64", 0, true},
24 {"x.go", "//go:build goexperiment.simdstable", PhaseStable, false},
25 {"x.go", "//go:build goexperiment.simd", PhaseExperiment, false},
26 {"x.go", "//go:build !goexperiment.simd", 0, false},
27 {"x.go", "//go:build !goexperiment.simdstable", 0, false},
28 {"x.go", "//go:build goexperiment.simd || goexperiment.simdstable", PhaseStable, false},
29 {"x.go", "//go:build goexperiment.simdstable && !goexperiment.simd", PhaseStable, false},
30 {"x.go", "//go:build goexperiment.simdstable && (goexperiment.simd || amd64)", PhaseStable, true},
31
32
33 {"ops_amd64.go", "//go:build goexperiment.simdstable", PhaseStable, true},
34 {"ops_amd64.go", "", 0, true},
35 {"slicepart_wasm_test.go", "//go:build goexperiment.simdstable", PhaseStable, true},
36 {"ops_sve.go", "//go:build goexperiment.simd && arm64", PhaseExperiment, true},
37 {"maskrange_128.go", "//go:build goexperiment.simdstable && (amd64 || arm64 || wasm)", PhaseStable, true},
38 {"x.go", "//go:build goexperiment.simdstable && (amd64 || mips)", PhaseStable, false},
39 {"x.go", "//go:build goexperiment.simdstable && amd64 && !mips", PhaseStable, true},
40 {"x.go", "//go:build goexperiment.simdstable && (amd64 || arm64) && linux", PhaseStable, true},
41 {"x.go", "//go:build goexperiment.simdstable && !amd64", PhaseStable, false},
42 {"ops_riscv64.go", "//go:build goexperiment.simdstable", PhaseStable, false},
43 {"ops_riscv64.go", "//go:build goexperiment.simd && riscv64", PhaseExperiment, false},
44 {"amd64.go", "//go:build goexperiment.simdstable", PhaseStable, false},
45 {"x_linux_amd64.go", "//go:build goexperiment.simdstable", PhaseStable, true},
46 {"x_arm64_test.go", "//go:build goexperiment.simdstable", PhaseStable, true},
47 {"x_linux.go", "//go:build goexperiment.simdstable", PhaseStable, false},
48 {"cpu.go", "//go:build goexperiment.simdstable", PhaseStable, false},
49 {"tofrom_emulated.go", "//go:build goexperiment.simdstable && !(amd64 || wasm || arm64)", PhaseStable, false},
50
51
52 {"x.go", "//go:build goexperiment.simdstable && (amd64 || !goexperiment.simd)", PhaseStable, false},
53 }
54 for _, tt := range tests {
55 fset := token.NewFileSet()
56 f, err := parser.ParseFile(fset, tt.filename, tt.build+"\n\npackage p\n", parser.ParseComments)
57 if err != nil {
58 t.Fatal(err)
59 }
60 c, err := ParseBuildConstraints(fset, f)
61 if err != nil {
62 t.Errorf("ParseBuildConstraints(%q, %q): %v", tt.filename, tt.build, err)
63 continue
64 }
65 if got := c.Phase(); got != tt.phase {
66 t.Errorf("ParseBuildConstraints(%q, %q).Phase() = %v, want %v", tt.filename, tt.build, int(got), int(tt.phase))
67 }
68 if got := c.StableArchOnly(); got != tt.stableArchOnly {
69 t.Errorf("ParseBuildConstraints(%q, %q).StableArchOnly() = %v, want %v", tt.filename, tt.build, got, tt.stableArchOnly)
70 }
71 }
72
73
74 tooMany := "//go:build goexperiment.simdstable && (t0"
75 for i := 1; i <= maxBuildTags; i++ {
76 tooMany += fmt.Sprintf(" || t%d", i)
77 }
78 tooMany += ")"
79 for _, build := range []string{
80 tooMany,
81 "//go:build goexperiment.simd && !goexperiment.simdstable",
82 "//go:build amd64 && !amd64",
83 "//go:build goexperiment.simdstable && amd64 && !(arm64 || amd64)",
84 } {
85 fset := token.NewFileSet()
86 f, err := parser.ParseFile(fset, "x.go", build+"\n\npackage p\n", parser.ParseComments)
87 if err != nil {
88 t.Fatal(err)
89 }
90 if _, err := ParseBuildConstraints(fset, f); err == nil {
91 t.Errorf("ParseBuildConstraints(%q): want error", build)
92 }
93 }
94 }
95
96
97
98 func TestParseBuildConstraintsFixed(t *testing.T) {
99 tests := []struct {
100 filename, build string
101 free []string
102 }{
103 {"x.go", "//go:build goexperiment.simdstable && amd64", nil},
104 {"ops_amd64.go", "//go:build goexperiment.simdstable", nil},
105 {"x.go", "//go:build goexperiment.simd && arm64 && !mips", nil},
106 {"x.go", "//go:build goexperiment.simdstable && (amd64 || mips)", []string{"amd64", "mips"}},
107 {"x.go", "//go:build goexperiment.simdstable && linux && (amd64 || arm64)", []string{"amd64", "arm64"}},
108 {"x.go", "//go:build goexperiment.simdstable && !mips && (amd64 || mips)", []string{"amd64"}},
109 {"x.go", "//go:build goexperiment.simdstable && !(mips || s390x) && (amd64 || mips || s390x)", []string{"amd64"}},
110 }
111 for _, tt := range tests {
112 fset := token.NewFileSet()
113 f, err := parser.ParseFile(fset, tt.filename, tt.build+"\n\npackage p\n", parser.ParseComments)
114 if err != nil {
115 t.Fatal(err)
116 }
117 c, err := ParseBuildConstraints(fset, f)
118 if err != nil {
119 t.Errorf("ParseBuildConstraints(%q, %q): %v", tt.filename, tt.build, err)
120 continue
121 }
122 slices.Sort(c.free)
123 if !slices.Equal(c.free, tt.free) {
124 t.Errorf("ParseBuildConstraints(%q, %q) tried free tags %q, want %q", tt.filename, tt.build, c.free, tt.free)
125 }
126 }
127 }
128
129 func TestNameGOARCH(t *testing.T) {
130 for name, want := range map[string]string{
131 "x.go": "",
132 "x_amd64.go": "amd64",
133 "x_linux_arm64.go": "arm64",
134 "x_wasm.go": "wasm",
135 "x_wasm_test.go": "wasm",
136 "x_linux.go": "",
137 "x_riscv64.go": "",
138 "amd64.go": "",
139 } {
140 if got := nameGOARCH(name); got != want {
141 t.Errorf("nameGOARCH(%q) = %q, want %q", name, got, want)
142 }
143 }
144 }
145
View as plain text