1
2
3
4
5
6
7
8 package types
9
10 import (
11 "cmd/internal/src"
12 "testing"
13 )
14
15 type test struct {
16 typ *Type
17 structuralType *Type
18 }
19
20 func TestStructuralType(t *testing.T) {
21
22
23
24 PtrSize = 8
25 RegSize = 8
26 MaxWidth = 1 << 50
27
28
29 intType := newType(TINT)
30
31 structf := NewStruct(nil, []*Field{
32 NewField(src.NoXPos, LocalPkg.Lookup("f"), intType),
33 })
34
35
36 Sf := newType(TFORW)
37 Sf.sym = LocalPkg.Lookup("Sf")
38 Sf.SetUnderlying(structf)
39
40
41 A := newType(TFORW)
42 A.sym = LocalPkg.Lookup("A")
43 A.SetUnderlying(intType)
44
45
46 B := newType(TFORW)
47 B.sym = LocalPkg.Lookup("B")
48 B.SetUnderlying(intType)
49
50 emptyInterface := NewInterface(BuiltinPkg, []*Field{}, false)
51 any := newType(TFORW)
52 any.sym = LocalPkg.Lookup("any")
53 any.SetUnderlying(emptyInterface)
54
55
56
57 tests := []*test{
58 {
59
60 embed(structf),
61 structf,
62 },
63 {
64
65 embed(structf, any),
66 structf,
67 },
68 {
69
70 embed(Sf),
71 structf,
72 },
73 {
74
75 embed(any, Sf),
76 structf,
77 },
78 {
79
80 embed(structf, Sf),
81 nil,
82 },
83 {
84
85 embed(NewUnion([]*Type{structf, structf}, []bool{false, true})),
86 structf,
87 },
88 {
89
90 embed(NewUnion([]*Type{structf}, []bool{true}), Sf),
91 structf,
92 },
93 {
94
95 embed(NewUnion([]*Type{structf}, []bool{false}), Sf),
96 nil,
97 },
98 {
99
100 embed(NewUnion([]*Type{Sf, A}, []bool{false, false}),
101 NewUnion([]*Type{B, Sf}, []bool{false, false})),
102 structf,
103 },
104 {
105
106 embed(NewUnion([]*Type{Sf, A}, []bool{false, false}),
107 NewUnion([]*Type{A, Sf}, []bool{false, false})),
108 nil,
109 },
110 {
111
112 embed(NewUnion([]*Type{Sf, any}, []bool{false, false})),
113 nil,
114 },
115 {
116
117 embed(NewUnion([]*Type{Sf, any}, []bool{false, false}), Sf),
118 structf,
119 },
120 }
121 for _, tst := range tests {
122 if got, want := tst.typ.StructuralType(), tst.structuralType; got != want {
123 t.Errorf("StructuralType(%v) = %v, wanted %v",
124 tst.typ, got, want)
125 }
126 }
127 }
128
129 func embed(types ...*Type) *Type {
130 fields := make([]*Field, len(types))
131 for i, t := range types {
132 fields[i] = NewField(src.NoXPos, nil, t)
133 }
134 return NewInterface(LocalPkg, fields, false)
135 }
136
View as plain text