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