Source file
src/go/types/typeterm_test.go
1
2
3
4
5
6
7
8 package types
9
10 import (
11 "strings"
12 "testing"
13 )
14
15 var myInt = func() Type {
16 tname := NewTypeName(nopos, nil, "myInt", nil)
17 return NewNamed(tname, Typ[Int], nil)
18 }()
19
20 var testTerms = map[string]*term{
21 "β
": nil,
22 "π€": {},
23 "int": {false, Typ[Int]},
24 "~int": {true, Typ[Int]},
25 "string": {false, Typ[String]},
26 "~string": {true, Typ[String]},
27 "myInt": {false, myInt},
28 }
29
30 func TestTermString(t *testing.T) {
31 for want, x := range testTerms {
32 if got := x.String(); got != want {
33 t.Errorf("%v.String() == %v; want %v", x, got, want)
34 }
35 }
36 }
37
38 func split(s string, n int) []string {
39 r := strings.Split(s, " ")
40 if len(r) != n {
41 panic("invalid test case: " + s)
42 }
43 return r
44 }
45
46 func testTerm(name string) *term {
47 r, ok := testTerms[name]
48 if !ok {
49 panic("invalid test argument: " + name)
50 }
51 return r
52 }
53
54 func TestTermEqual(t *testing.T) {
55 for _, test := range []string{
56 "β
β
T",
57 "π€ π€ T",
58 "int int T",
59 "~int ~int T",
60 "myInt myInt T",
61 "β
π€ F",
62 "β
int F",
63 "β
~int F",
64 "π€ int F",
65 "π€ ~int F",
66 "π€ myInt F",
67 "int ~int F",
68 "int myInt F",
69 "~int myInt F",
70 } {
71 args := split(test, 3)
72 x := testTerm(args[0])
73 y := testTerm(args[1])
74 want := args[2] == "T"
75 if got := x.equal(y); got != want {
76 t.Errorf("%v.equal(%v) = %v; want %v", x, y, got, want)
77 }
78
79 x, y = y, x
80 if got := x.equal(y); got != want {
81 t.Errorf("%v.equal(%v) = %v; want %v", x, y, got, want)
82 }
83 }
84 }
85
86 func TestTermUnion(t *testing.T) {
87 for _, test := range []string{
88 "β
β
β
β
",
89 "β
π€ π€ β
",
90 "β
int int β
",
91 "β
~int ~int β
",
92 "β
myInt myInt β
",
93 "π€ π€ π€ β
",
94 "π€ int π€ β
",
95 "π€ ~int π€ β
",
96 "π€ myInt π€ β
",
97 "int int int β
",
98 "int ~int ~int β
",
99 "int string int string",
100 "int ~string int ~string",
101 "int myInt int myInt",
102 "~int ~string ~int ~string",
103 "~int myInt ~int β
",
104
105
106 "π€ β
π€ β
",
107 "int β
int β
",
108 "~int β
~int β
",
109 "myInt β
myInt β
",
110 "int π€ π€ β
",
111 "~int π€ π€ β
",
112 "myInt π€ π€ β
",
113 "~int int ~int β
",
114 "string int string int",
115 "~string int ~string int",
116 "myInt int myInt int",
117 "~string ~int ~string ~int",
118 "myInt ~int ~int β
",
119 } {
120 args := split(test, 4)
121 x := testTerm(args[0])
122 y := testTerm(args[1])
123 want1 := testTerm(args[2])
124 want2 := testTerm(args[3])
125 if got1, got2 := x.union(y); !got1.equal(want1) || !got2.equal(want2) {
126 t.Errorf("%v.union(%v) = %v, %v; want %v, %v", x, y, got1, got2, want1, want2)
127 }
128 }
129 }
130
131 func TestTermIntersection(t *testing.T) {
132 for _, test := range []string{
133 "β
β
β
",
134 "β
π€ β
",
135 "β
int β
",
136 "β
~int β
",
137 "β
myInt β
",
138 "π€ π€ π€",
139 "π€ int int",
140 "π€ ~int ~int",
141 "π€ myInt myInt",
142 "int int int",
143 "int ~int int",
144 "int string β
",
145 "int ~string β
",
146 "int string β
",
147 "~int ~string β
",
148 "~int myInt myInt",
149 } {
150 args := split(test, 3)
151 x := testTerm(args[0])
152 y := testTerm(args[1])
153 want := testTerm(args[2])
154 if got := x.intersect(y); !got.equal(want) {
155 t.Errorf("%v.intersect(%v) = %v; want %v", x, y, got, want)
156 }
157
158 x, y = y, x
159 if got := x.intersect(y); !got.equal(want) {
160 t.Errorf("%v.intersect(%v) = %v; want %v", x, y, got, want)
161 }
162 }
163 }
164
165 func TestTermIncludes(t *testing.T) {
166 for _, test := range []string{
167 "β
int F",
168 "π€ int T",
169 "int int T",
170 "~int int T",
171 "~int myInt T",
172 "string int F",
173 "~string int F",
174 "myInt int F",
175 } {
176 args := split(test, 3)
177 x := testTerm(args[0])
178 y := testTerm(args[1]).typ
179 want := args[2] == "T"
180 if got := x.includes(y); got != want {
181 t.Errorf("%v.includes(%v) = %v; want %v", x, y, got, want)
182 }
183 }
184 }
185
186 func TestTermSubsetOf(t *testing.T) {
187 for _, test := range []string{
188 "β
β
T",
189 "π€ π€ T",
190 "int int T",
191 "~int ~int T",
192 "myInt myInt T",
193 "β
π€ T",
194 "β
int T",
195 "β
~int T",
196 "β
myInt T",
197 "π€ int F",
198 "π€ ~int F",
199 "π€ myInt F",
200 "int ~int T",
201 "int myInt F",
202 "~int myInt F",
203 "myInt int F",
204 "myInt ~int T",
205 } {
206 args := split(test, 3)
207 x := testTerm(args[0])
208 y := testTerm(args[1])
209 want := args[2] == "T"
210 if got := x.subsetOf(y); got != want {
211 t.Errorf("%v.subsetOf(%v) = %v; want %v", x, y, got, want)
212 }
213 }
214 }
215
216 func TestTermDisjoint(t *testing.T) {
217 for _, test := range []string{
218 "int int F",
219 "~int ~int F",
220 "int ~int F",
221 "int string T",
222 "int ~string T",
223 "int myInt T",
224 "~int ~string T",
225 "~int myInt F",
226 "string myInt T",
227 "~string myInt T",
228 } {
229 args := split(test, 3)
230 x := testTerm(args[0])
231 y := testTerm(args[1])
232 want := args[2] == "T"
233 if got := x.disjoint(y); got != want {
234 t.Errorf("%v.disjoint(%v) = %v; want %v", x, y, got, want)
235 }
236
237 x, y = y, x
238 if got := x.disjoint(y); got != want {
239 t.Errorf("%v.disjoint(%v) = %v; want %v", x, y, got, want)
240 }
241 }
242 }
243
View as plain text