]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/exp/types/staging/gcimporter_test.go
libgo: Update to current sources.
[thirdparty/gcc.git] / libgo / go / exp / types / staging / gcimporter_test.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package types
6
7 import (
8 "go/ast"
9 "go/build"
10 "io/ioutil"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "runtime"
15 "strings"
16 "testing"
17 "time"
18 )
19
20 var gcPath string // Go compiler path
21
22 func init() {
23 // determine compiler
24 var gc string
25 switch runtime.GOARCH {
26 case "386":
27 gc = "8g"
28 case "amd64":
29 gc = "6g"
30 case "arm":
31 gc = "5g"
32 default:
33 gcPath = "unknown-GOARCH-compiler"
34 return
35 }
36 gcPath = filepath.Join(build.ToolDir, gc)
37 }
38
39 func compile(t *testing.T, dirname, filename string) string {
40 cmd := exec.Command(gcPath, filename)
41 cmd.Dir = dirname
42 out, err := cmd.CombinedOutput()
43 if err != nil {
44 t.Logf("%s", out)
45 t.Fatalf("%s %s failed: %s", gcPath, filename, err)
46 }
47 archCh, _ := build.ArchChar(runtime.GOARCH)
48 // filename should end with ".go"
49 return filepath.Join(dirname, filename[:len(filename)-2]+archCh)
50 }
51
52 // Use the same global imports map for all tests. The effect is
53 // as if all tested packages were imported into a single package.
54 var imports = make(map[string]*ast.Object)
55
56 func testPath(t *testing.T, path string) bool {
57 _, err := GcImport(imports, path)
58 if err != nil {
59 t.Errorf("testPath(%s): %s", path, err)
60 return false
61 }
62 return true
63 }
64
65 const maxTime = 3 * time.Second
66
67 func testDir(t *testing.T, dir string, endTime time.Time) (nimports int) {
68 dirname := filepath.Join(runtime.GOROOT(), "pkg", runtime.GOOS+"_"+runtime.GOARCH, dir)
69 list, err := ioutil.ReadDir(dirname)
70 if err != nil {
71 t.Errorf("testDir(%s): %s", dirname, err)
72 }
73 for _, f := range list {
74 if time.Now().After(endTime) {
75 t.Log("testing time used up")
76 return
77 }
78 switch {
79 case !f.IsDir():
80 // try extensions
81 for _, ext := range pkgExts {
82 if strings.HasSuffix(f.Name(), ext) {
83 name := f.Name()[0 : len(f.Name())-len(ext)] // remove extension
84 if testPath(t, filepath.Join(dir, name)) {
85 nimports++
86 }
87 }
88 }
89 case f.IsDir():
90 nimports += testDir(t, filepath.Join(dir, f.Name()), endTime)
91 }
92 }
93 return
94 }
95
96 func TestGcImport(t *testing.T) {
97 // On cross-compile builds, the path will not exist.
98 // Need to use GOHOSTOS, which is not available.
99 if _, err := os.Stat(gcPath); err != nil {
100 t.Logf("skipping test: %v", err)
101 return
102 }
103
104 if outFn := compile(t, "testdata", "exports.go"); outFn != "" {
105 defer os.Remove(outFn)
106 }
107
108 nimports := 0
109 if testPath(t, "./testdata/exports") {
110 nimports++
111 }
112 nimports += testDir(t, "", time.Now().Add(maxTime)) // installed packages
113 t.Logf("tested %d imports", nimports)
114 }
115
116 var importedObjectTests = []struct {
117 name string
118 kind ast.ObjKind
119 typ string
120 }{
121 {"unsafe.Pointer", ast.Typ, "Pointer"},
122 {"math.Pi", ast.Con, "untyped float"},
123 {"io.Reader", ast.Typ, "interface{Read(p []byte) (n int, err error)}"},
124 {"io.ReadWriter", ast.Typ, "interface{Read(p []byte) (n int, err error); Write(p []byte) (n int, err error)}"},
125 {"math.Sin", ast.Fun, "func(x float64) (_ float64)"},
126 // TODO(gri) add more tests
127 }
128
129 func TestGcImportedTypes(t *testing.T) {
130 for _, test := range importedObjectTests {
131 s := strings.Split(test.name, ".")
132 if len(s) != 2 {
133 t.Fatal("inconsistent test data")
134 }
135 importPath := s[0]
136 objName := s[1]
137
138 pkg, err := GcImport(imports, importPath)
139 if err != nil {
140 t.Error(err)
141 continue
142 }
143
144 obj := pkg.Data.(*ast.Scope).Lookup(objName)
145 if obj.Kind != test.kind {
146 t.Errorf("%s: got kind = %q; want %q", test.name, obj.Kind, test.kind)
147 }
148 typ := typeString(underlying(obj.Type.(Type)))
149 if typ != test.typ {
150 t.Errorf("%s: got type = %q; want %q", test.name, typ, test.typ)
151 }
152 }
153 }