Debian Codegraph @

test/com/google/javascript/jscomp/LinkedFlowScopeTest.java

raw symbols SCIP
1 /*
2 * Copyright 2008 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.google.javascript.jscomp;
18
19 import com.google.common.collect.Lists;
20 import com.google.javascript.jscomp.type.FlowScope;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.Token;
23 import com.google.javascript.rhino.jstype.JSType;
24
25 /**
26 * Tests for LinkedFlowScope.
27 * @author [email protected] (Nick Santos)
28 */
29 public class LinkedFlowScopeTest extends CompilerTypeTestCase {
30
31 private final Node blockNode = new Node(Token.BLOCK);
32 private final Node functionNode = new Node(Token.FUNCTION);
33 private final int LONG_CHAIN_LENGTH = 1050;
34
35 private Scope globalScope;
36 private Scope localScope;
37 @SuppressWarnings("unused")
38 private FlowScope globalEntry;
39 private FlowScope localEntry;
40
41 @Override
42 public void setUp() throws Exception {
43 super.setUp();
44
45 globalScope = Scope.createGlobalScope(blockNode);
46 globalScope.declare("globalA", null, null, null);
47 globalScope.declare("globalB", null, null, null);
48
49 localScope = new Scope(globalScope, functionNode);
50 localScope.declare("localA", null, null, null);
51 localScope.declare("localB", null, null, null);
52
53 globalEntry = LinkedFlowScope.createEntryLattice(globalScope);
54 localEntry = LinkedFlowScope.createEntryLattice(localScope);
55 }
56
57 public void testOptimize() {
58 assertEquals(localEntry, localEntry.optimize());
59
60 FlowScope child = localEntry.createChildFlowScope();
61 assertEquals(localEntry, child.optimize());
62
63 child.inferSlotType("localB", NUMBER_TYPE);
64 assertEquals(child, child.optimize());
65 }
66
67 public void testJoin1() {
68 FlowScope childA = localEntry.createChildFlowScope();
69 childA.inferSlotType("localB", NUMBER_TYPE);
70
71 FlowScope childAB = childA.createChildFlowScope();
72 childAB.inferSlotType("localB", STRING_TYPE);
73
74 FlowScope childB = localEntry.createChildFlowScope();
75 childB.inferSlotType("localB", BOOLEAN_TYPE);
76
77 assertTypeEquals(STRING_TYPE, childAB.getSlot("localB").getType());
78 assertTypeEquals(BOOLEAN_TYPE, childB.getSlot("localB").getType());
79 assertNull(childB.getSlot("localA").getType());
80
81 FlowScope joined = join(childB, childAB);
82 assertTypeEquals(createUnionType(STRING_TYPE, BOOLEAN_TYPE),
83 joined.getSlot("localB").getType());
84 assertNull(joined.getSlot("localA").getType());
85
86 joined = join(childAB, childB);
87 assertTypeEquals(createUnionType(STRING_TYPE, BOOLEAN_TYPE),
88 joined.getSlot("localB").getType());
89 assertNull(joined.getSlot("localA").getType());
90
91 assertEquals("Join should be symmetric",
92 join(childB, childAB), join(childAB, childB));
93 }
94
95 public void testJoin2() {
96 FlowScope childA = localEntry.createChildFlowScope();
97 childA.inferSlotType("localA", STRING_TYPE);
98
99 FlowScope childB = localEntry.createChildFlowScope();
100 childB.inferSlotType("globalB", BOOLEAN_TYPE);
101
102 assertTypeEquals(STRING_TYPE, childA.getSlot("localA").getType());
103 assertTypeEquals(BOOLEAN_TYPE, childB.getSlot("globalB").getType());
104 assertNull(childB.getSlot("localB").getType());
105
106 FlowScope joined = join(childB, childA);
107 assertTypeEquals(STRING_TYPE, joined.getSlot("localA").getType());
108 assertTypeEquals(BOOLEAN_TYPE, joined.getSlot("globalB").getType());
109
110 joined = join(childA, childB);
111 assertTypeEquals(STRING_TYPE, joined.getSlot("localA").getType());
112 assertTypeEquals(BOOLEAN_TYPE, joined.getSlot("globalB").getType());
113
114 assertEquals("Join should be symmetric",
115 join(childB, childA), join(childA, childB));
116 }
117
118 public void testJoin3() {
119 localScope.declare("localC", null, STRING_TYPE, null);
120 localScope.declare("localD", null, STRING_TYPE, null);
121
122 FlowScope childA = localEntry.createChildFlowScope();
123 childA.inferSlotType("localC", NUMBER_TYPE);
124
125 FlowScope childB = localEntry.createChildFlowScope();
126 childA.inferSlotType("localD", BOOLEAN_TYPE);
127
128 FlowScope joined = join(childB, childA);
129 assertTypeEquals(createUnionType(STRING_TYPE, NUMBER_TYPE),
130 joined.getSlot("localC").getType());
131 assertTypeEquals(createUnionType(STRING_TYPE, BOOLEAN_TYPE),
132 joined.getSlot("localD").getType());
133
134 joined = join(childA, childB);
135 assertTypeEquals(createUnionType(STRING_TYPE, NUMBER_TYPE),
136 joined.getSlot("localC").getType());
137 assertTypeEquals(createUnionType(STRING_TYPE, BOOLEAN_TYPE),
138 joined.getSlot("localD").getType());
139
140 assertEquals("Join should be symmetric",
141 join(childB, childA), join(childA, childB));
142 }
143
144 /**
145 * Create a long chain of flow scopes where each link in the chain
146 * contains one slot.
147 */
148 public void testLongChain1() {
149 FlowScope chainA = localEntry.createChildFlowScope();
150 FlowScope chainB = localEntry.createChildFlowScope();
151 for (int i = 0; i < LONG_CHAIN_LENGTH; i++) {
152 localScope.declare("local" + i, null, null, null);
153 chainA.inferSlotType("local" + i,
154 i % 2 == 0 ? NUMBER_TYPE : BOOLEAN_TYPE);
155 chainB.inferSlotType("local" + i,
156 i % 3 == 0 ? STRING_TYPE : BOOLEAN_TYPE);
157
158 chainA = chainA.createChildFlowScope();
159 chainB = chainB.createChildFlowScope();
160 }
161
162 verifyLongChains(chainA, chainB);
163 }
164
165 /**
166 * Create a long chain of flow scopes where each link in the chain
167 * contains 7 slots.
168 */
169 public void testLongChain2() {
170 FlowScope chainA = localEntry.createChildFlowScope();
171 FlowScope chainB = localEntry.createChildFlowScope();
172 for (int i = 0; i < LONG_CHAIN_LENGTH * 7; i++) {
173 localScope.declare("local" + i, null, null, null);
174 chainA.inferSlotType("local" + i,
175 i % 2 == 0 ? NUMBER_TYPE : BOOLEAN_TYPE);
176 chainB.inferSlotType("local" + i,
177 i % 3 == 0 ? STRING_TYPE : BOOLEAN_TYPE);
178
179 if (i % 7 == 0) {
180 chainA = chainA.createChildFlowScope();
181 chainB = chainB.createChildFlowScope();
182 }
183 }
184
185 verifyLongChains(chainA, chainB);
186 }
187
188 /**
189 * Create a long chain of flow scopes where every 4 links in the chain
190 * contain a slot.
191 */
192 public void testLongChain3() {
193 FlowScope chainA = localEntry.createChildFlowScope();
194 FlowScope chainB = localEntry.createChildFlowScope();
195 for (int i = 0; i < LONG_CHAIN_LENGTH * 7; i++) {
196 if (i % 7 == 0) {
197 int j = i / 7;
198 localScope.declare("local" + j, null, null, null);
199 chainA.inferSlotType("local" + j,
200 j % 2 == 0 ? NUMBER_TYPE : BOOLEAN_TYPE);
201 chainB.inferSlotType("local" + j,
202 j % 3 == 0 ? STRING_TYPE : BOOLEAN_TYPE);
203 }
204
205 chainA = chainA.createChildFlowScope();
206 chainB = chainB.createChildFlowScope();
207 }
208
209 verifyLongChains(chainA, chainB);
210 }
211
212 // Common chain verification for testLongChainN for all N.
213 private void verifyLongChains(FlowScope chainA, FlowScope chainB) {
214 FlowScope joined = join(chainA, chainB);
215 for (int i = 0; i < LONG_CHAIN_LENGTH; i++) {
216 assertTypeEquals(
217 i % 2 == 0 ? NUMBER_TYPE : BOOLEAN_TYPE,
218 chainA.getSlot("local" + i).getType());
219 assertTypeEquals(
220 i % 3 == 0 ? STRING_TYPE : BOOLEAN_TYPE,
221 chainB.getSlot("local" + i).getType());
222
223 JSType joinedSlotType = joined.getSlot("local" + i).getType();
224 if (i % 6 == 0) {
225 assertTypeEquals(createUnionType(STRING_TYPE, NUMBER_TYPE), joinedSlotType);
226 } else if (i % 2 == 0) {
227 assertTypeEquals(createUnionType(NUMBER_TYPE, BOOLEAN_TYPE),
228 joinedSlotType);
229 } else if (i % 3 == 0) {
230 assertTypeEquals(createUnionType(STRING_TYPE, BOOLEAN_TYPE),
231 joinedSlotType);
232 } else {
233 assertTypeEquals(BOOLEAN_TYPE, joinedSlotType);
234 }
235 }
236
237 assertScopesDiffer(chainA, chainB);
238 assertScopesDiffer(chainA, joined);
239 assertScopesDiffer(chainB, joined);
240 }
241
242 public void testFindUniqueSlot() {
243 FlowScope childA = localEntry.createChildFlowScope();
244 childA.inferSlotType("localB", NUMBER_TYPE);
245
246 FlowScope childAB = childA.createChildFlowScope();
247 childAB.inferSlotType("localB", STRING_TYPE);
248
249 FlowScope childABC = childAB.createChildFlowScope();
250 childABC.inferSlotType("localA", BOOLEAN_TYPE);
251
252 assertNull(childABC.findUniqueRefinedSlot(childABC));
253 assertTypeEquals(BOOLEAN_TYPE,
254 childABC.findUniqueRefinedSlot(childAB).getType());
255 assertNull(childABC.findUniqueRefinedSlot(childA));
256 assertNull(childABC.findUniqueRefinedSlot(localEntry));
257
258 assertTypeEquals(STRING_TYPE,
259 childAB.findUniqueRefinedSlot(childA).getType());
260 assertTypeEquals(STRING_TYPE,
261 childAB.findUniqueRefinedSlot(localEntry).getType());
262
263 assertTypeEquals(NUMBER_TYPE,
264 childA.findUniqueRefinedSlot(localEntry).getType());
265 }
266
267 public void testDiffer1() {
268 FlowScope childA = localEntry.createChildFlowScope();
269 childA.inferSlotType("localB", NUMBER_TYPE);
270
271 FlowScope childAB = childA.createChildFlowScope();
272 childAB.inferSlotType("localB", STRING_TYPE);
273
274 FlowScope childABC = childAB.createChildFlowScope();
275 childABC.inferSlotType("localA", BOOLEAN_TYPE);
276
277 FlowScope childB = childAB.createChildFlowScope();
278 childB.inferSlotType("localB", STRING_TYPE);
279
280 FlowScope childBC = childB.createChildFlowScope();
281 childBC.inferSlotType("localA", NO_TYPE);
282
283 assertScopesSame(childAB, childB);
284 assertScopesDiffer(childABC, childBC);
285
286 assertScopesDiffer(childABC, childB);
287 assertScopesDiffer(childAB, childBC);
288
289 assertScopesDiffer(childA, childAB);
290 assertScopesDiffer(childA, childABC);
291 assertScopesDiffer(childA, childB);
292 assertScopesDiffer(childA, childBC);
293 }
294
295 public void testDiffer2() {
296 FlowScope childA = localEntry.createChildFlowScope();
297 childA.inferSlotType("localA", NUMBER_TYPE);
298
299 FlowScope childB = localEntry.createChildFlowScope();
300 childB.inferSlotType("localA", NO_TYPE);
301
302 assertScopesDiffer(childA, childB);
303 }
304
305 private void assertScopesDiffer(FlowScope a, FlowScope b) {
306 assertFalse(a.equals(b));
307 assertFalse(b.equals(a));
308 assertEquals(a, a);
309 assertEquals(b, b);
310 }
311
312 private void assertScopesSame(FlowScope a, FlowScope b) {
313 assertEquals(a, b);
314 assertEquals(b, a);
315 assertEquals(a, a);
316 assertEquals(b, b);
317 }
318
319 @SuppressWarnings("unchecked")
320 private FlowScope join(FlowScope a, FlowScope b) {
321 return (new LinkedFlowScope.FlowScopeJoinOp()).apply(
322 Lists.newArrayList(a, b));
323 }
324 }