1
2
3
4
5 package types
6
7 import (
8 "bytes"
9 "fmt"
10 "strings"
11 "testing"
12
13 "cmd/internal/src"
14 )
15
16
17 type testTypeName struct {
18 sym *Sym
19 }
20
21 func (o *testTypeName) Sym() *Sym { return o.sym }
22 func (o *testTypeName) Pos() src.XPos { return src.NoXPos }
23 func (o *testTypeName) Type() *Type { return nil }
24
25
26
27
28
29 func TestFormatInterfaceUnexportedMethodUsesGoModeForSignature(t *testing.T) {
30 oldLocal := LocalPkg
31 oldPtr, oldReg, oldMax := PtrSize, RegSize, MaxWidth
32 t.Cleanup(func() {
33 LocalPkg = oldLocal
34 PtrSize, RegSize, MaxWidth = oldPtr, oldReg, oldMax
35 })
36 LocalPkg = NewPkg("main", "main")
37 PtrSize = 8
38 RegSize = 8
39 MaxWidth = 1 << 50
40
41 lib := NewPkg("example.com/lib", "lib")
42 namedT := NewNamed(&testTypeName{sym: lib.Lookup("T")})
43
44 namedT.SetUnderlying(NewStruct(nil))
45
46 sig := NewSignature(nil, nil, []*Field{NewField(src.NoXPos, nil, namedT)})
47 iface := NewInterface([]*Field{NewField(src.NoXPos, LocalPkg.Lookup("unexported"), sig)})
48 CalcSize(iface)
49
50 var buf bytes.Buffer
51 fmt.Fprintf(&buf, "%v", iface)
52 got := buf.String()
53
54 const want = "lib.T"
55 if !strings.Contains(got, want) {
56 t.Fatalf("formatted type missing %q:\n%s", want, got)
57 }
58
59 if strings.Contains(got, "example.com/lib.T") {
60 t.Fatalf("formatted type should not use import path to qualify T (found example.com/lib.T):\n%s", got)
61 }
62 }
63
64 func TestSSACompare(t *testing.T) {
65 a := []*Type{
66 TypeInvalid,
67 TypeMem,
68 TypeFlags,
69 TypeVoid,
70 TypeInt128,
71 }
72 for _, x := range a {
73 for _, y := range a {
74 c := x.Compare(y)
75 if x == y && c != CMPeq || x != y && c == CMPeq {
76 t.Errorf("%s compare %s == %d\n", x.extra, y.extra, c)
77 }
78 }
79 }
80 }
81
View as plain text