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