Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: tests/compiler/dart2js/class_set_test.dart

Issue 1627333002: Optimize subclass/subtype queries (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Use strictSubtypeCount Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // Test for iterators on for [SubclassNode]. 5 // Test for iterators on for [SubclassNode].
6 6
7 library class_set_test; 7 library class_set_test;
8 8
9 import 'package:expect/expect.dart'; 9 import 'package:expect/expect.dart';
10 import 'package:async_helper/async_helper.dart'; 10 import 'package:async_helper/async_helper.dart';
11 import 'type_test_helper.dart'; 11 import 'type_test_helper.dart';
12 import 'package:compiler/src/elements/elements.dart' 12 import 'package:compiler/src/elements/elements.dart'
13 show Element, ClassElement; 13 show Element, ClassElement;
14 import 'package:compiler/src/universe/class_set.dart'; 14 import 'package:compiler/src/universe/class_set.dart';
15 import 'package:compiler/src/util/enumset.dart'; 15 import 'package:compiler/src/util/enumset.dart';
16 import 'package:compiler/src/util/util.dart'; 16 import 'package:compiler/src/util/util.dart';
17 import 'package:compiler/src/world.dart'; 17 import 'package:compiler/src/world.dart';
18 18
19 void main() { 19 void main() {
20 asyncTest(() => TypeEnvironment.create(r""" 20 asyncTest(() async {
21 await testIterators();
22 await testForEach();
23 });
24 }
25
26 testIterators() async {
27 var env = await TypeEnvironment.create(r"""
21 /// A 28 /// A
22 /// / \ 29 /// / \
23 /// B C 30 /// B C
24 /// / /|\ 31 /// / /|\
25 /// D E F G 32 /// D E F G
26 /// 33 ///
27 class A {} 34 class A {}
28 class B extends A {} 35 class B extends A {}
29 class C extends A {} 36 class C extends A {}
30 class D extends B {} 37 class D extends B {}
31 class E extends C {} 38 class E extends C {}
32 class F extends C {} 39 class F extends C {}
33 class G extends C {} 40 class G extends C {}
34 """, 41 """,
35 mainSource: r""" 42 mainSource: r"""
36 main() { 43 main() {
37 new A(); 44 new A();
38 new C(); 45 new C();
39 new D(); 46 new D();
40 new E(); 47 new E();
41 new F(); 48 new F();
42 new G(); 49 new G();
43 } 50 }
44 """, 51 """,
45 useMockCompiler: false).then((env) { 52 useMockCompiler: false);
46 World world = env.compiler.world; 53 World world = env.compiler.world;
47 54
48 ClassElement A = env.getElement("A"); 55 ClassElement A = env.getElement("A");
49 ClassElement B = env.getElement("B"); 56 ClassElement B = env.getElement("B");
50 ClassElement C = env.getElement("C"); 57 ClassElement C = env.getElement("C");
51 ClassElement D = env.getElement("D"); 58 ClassElement D = env.getElement("D");
52 ClassElement E = env.getElement("E"); 59 ClassElement E = env.getElement("E");
53 ClassElement F = env.getElement("F"); 60 ClassElement F = env.getElement("F");
54 ClassElement G = env.getElement("G"); 61 ClassElement G = env.getElement("G");
55 62
56 void checkClass(ClassElement cls, 63 void checkClass(ClassElement cls,
57 {bool directlyInstantiated: false, 64 {bool directlyInstantiated: false,
58 bool indirectlyInstantiated: false}) { 65 bool indirectlyInstantiated: false}) {
59 ClassHierarchyNode node = world.getClassHierarchyNode(cls); 66 ClassHierarchyNode node = world.getClassHierarchyNode(cls);
60 Expect.isNotNull(node, "Expected ClassHierarchyNode for $cls."); 67 Expect.isNotNull(node, "Expected ClassHierarchyNode for $cls.");
61 Expect.equals( 68 Expect.equals(
62 directlyInstantiated || indirectlyInstantiated, 69 directlyInstantiated || indirectlyInstantiated,
63 node.isInstantiated, 70 node.isInstantiated,
64 "Unexpected `isInstantiated` on ClassHierarchyNode for $cls."); 71 "Unexpected `isInstantiated` on ClassHierarchyNode for $cls.");
65 Expect.equals( 72 Expect.equals(
66 directlyInstantiated, 73 directlyInstantiated,
67 node.isDirectlyInstantiated, 74 node.isDirectlyInstantiated,
68 "Unexpected `isDirectlyInstantiated` on ClassHierarchyNode for " 75 "Unexpected `isDirectlyInstantiated` on ClassHierarchyNode for "
69 "$cls."); 76 "$cls.");
70 Expect.equals( 77 Expect.equals(
71 indirectlyInstantiated, 78 indirectlyInstantiated,
72 node.isIndirectlyInstantiated, 79 node.isIndirectlyInstantiated,
73 "Unexpected `isIndirectlyInstantiated` on ClassHierarchyNode for " 80 "Unexpected `isIndirectlyInstantiated` on ClassHierarchyNode for "
74 "$cls."); 81 "$cls.");
75 } 82 }
76 83
77 84
78 checkClass(A, directlyInstantiated: true, indirectlyInstantiated: true); 85 checkClass(A, directlyInstantiated: true, indirectlyInstantiated: true);
79 checkClass(B, indirectlyInstantiated: true); 86 checkClass(B, indirectlyInstantiated: true);
80 checkClass(C, directlyInstantiated: true, indirectlyInstantiated: true); 87 checkClass(C, directlyInstantiated: true, indirectlyInstantiated: true);
81 checkClass(D, directlyInstantiated: true); 88 checkClass(D, directlyInstantiated: true);
82 checkClass(E, directlyInstantiated: true); 89 checkClass(E, directlyInstantiated: true);
83 checkClass(F, directlyInstantiated: true); 90 checkClass(F, directlyInstantiated: true);
84 checkClass(G, directlyInstantiated: true); 91 checkClass(G, directlyInstantiated: true);
85 92
86 ClassHierarchyNodeIterator iterator; 93 ClassHierarchyNodeIterator iterator;
87 94
88 void checkState( 95 void checkState(
89 ClassElement root, 96 ClassElement root,
90 {ClassElement currentNode, 97 {ClassElement currentNode,
91 List<List<ClassElement>> stack}) { 98 List<List<ClassElement>> stack}) {
92 99
93 ClassElement classOf(ClassHierarchyNode node) { 100 ClassElement classOf(ClassHierarchyNode node) {
94 return node != null ? node.cls : null; 101 return node != null ? node.cls : null;
102 }
103
104 List<ClassElement> classesOf(Link<ClassHierarchyNode> link) {
105 if (link == null) return null;
106 return link.map(classOf).toList();
107 }
108
109 ClassElement foundRoot = iterator.root.cls;
110 ClassElement foundCurrentNode = classOf(iterator.currentNode);
111 List<ClassElement> foundStack = classesOf(iterator.stack);
112
113 StringBuffer sb = new StringBuffer();
114 sb.write('{\n root: $foundRoot');
115 sb.write('\n currentNode: $foundCurrentNode');
116 sb.write('\n stack: $foundStack\n}');
117
118 Expect.equals(root, foundRoot,
119 "Expected root $root in $sb.");
120 if (currentNode == null) {
121 Expect.isNull(iterator.currentNode,
122 "Unexpected non-null currentNode in $sb.");
123 } else {
124 Expect.isNotNull(foundCurrentNode,
125 "Expected non-null currentNode ${currentNode} in $sb.");
126 Expect.equals(currentNode, foundCurrentNode,
127 "Expected currentNode $currentNode in $sb.");
128 }
129 if (stack == null) {
130 Expect.isNull(foundStack,
131 "Unexpected non-null stack in $sb.");
132 } else {
133 Expect.isNotNull(foundStack,
134 "Expected non-null stack ${stack} in $sb.");
135 Expect.listEquals(stack, foundStack,
136 "Expected stack ${stack}, "
137 "found ${foundStack} in $sb.");
138 }
139 }
140
141 iterator = new ClassHierarchyNodeIterable(
142 world.getClassHierarchyNode(G),
143 ClassHierarchyNode.ALL).iterator;
144 checkState(G, currentNode: null, stack: null);
145 Expect.isNull(iterator.current);
146 Expect.isTrue(iterator.moveNext());
147 checkState(G, currentNode: G, stack: []);
148 Expect.equals(G, iterator.current);
149 Expect.isFalse(iterator.moveNext());
150 checkState(G, currentNode: null, stack: []);
151 Expect.isNull(iterator.current);
152
153 iterator = new ClassHierarchyNodeIterable(
154 world.getClassHierarchyNode(G),
155 ClassHierarchyNode.ALL,
156 includeRoot: false).iterator;
157 checkState(G, currentNode: null, stack: null);
158 Expect.isNull(iterator.current);
159 Expect.isFalse(iterator.moveNext());
160 checkState(G, currentNode: null, stack: []);
161 Expect.isNull(iterator.current);
162
163 iterator = new ClassHierarchyNodeIterable(
164 world.getClassHierarchyNode(C),
165 ClassHierarchyNode.ALL).iterator;
166 checkState(C, currentNode: null, stack: null);
167 Expect.isNull(iterator.current);
168 Expect.isTrue(iterator.moveNext());
169 checkState(C, currentNode: C, stack: [E, F, G]);
170 Expect.equals(C, iterator.current);
171 Expect.isTrue(iterator.moveNext());
172 checkState(C, currentNode: E, stack: [F, G]);
173 Expect.equals(E, iterator.current);
174 Expect.isTrue(iterator.moveNext());
175 checkState(C, currentNode: F, stack: [G]);
176 Expect.equals(F, iterator.current);
177 Expect.isTrue(iterator.moveNext());
178 checkState(C, currentNode: G, stack: []);
179 Expect.equals(G, iterator.current);
180 Expect.isFalse(iterator.moveNext());
181 checkState(C, currentNode: null, stack: []);
182 Expect.isNull(iterator.current);
183
184 iterator = new ClassHierarchyNodeIterable(
185 world.getClassHierarchyNode(D),
186 ClassHierarchyNode.ALL).iterator;
187 checkState(D, currentNode: null, stack: null);
188 Expect.isNull(iterator.current);
189 Expect.isTrue(iterator.moveNext());
190 checkState(D, currentNode: D, stack: []);
191 Expect.equals(D, iterator.current);
192 Expect.isFalse(iterator.moveNext());
193 checkState(D, currentNode: null, stack: []);
194 Expect.isNull(iterator.current);
195
196 iterator = new ClassHierarchyNodeIterable(
197 world.getClassHierarchyNode(B),
198 ClassHierarchyNode.ALL).iterator;
199 checkState(B, currentNode: null, stack: null);
200 Expect.isNull(iterator.current);
201 Expect.isTrue(iterator.moveNext());
202 checkState(B, currentNode: B, stack: [D]);
203 Expect.equals(B, iterator.current);
204 Expect.isTrue(iterator.moveNext());
205 checkState(B, currentNode: D, stack: []);
206 Expect.equals(D, iterator.current);
207 Expect.isFalse(iterator.moveNext());
208 checkState(B, currentNode: null, stack: []);
209 Expect.isNull(iterator.current);
210
211 iterator = new ClassHierarchyNodeIterable(
212 world.getClassHierarchyNode(B),
213 ClassHierarchyNode.ALL,
214 includeRoot: false).iterator;
215 checkState(B, currentNode: null, stack: null);
216 Expect.isNull(iterator.current);
217 Expect.isTrue(iterator.moveNext());
218 checkState(B, currentNode: D, stack: []);
219 Expect.equals(D, iterator.current);
220 Expect.isFalse(iterator.moveNext());
221 checkState(B, currentNode: null, stack: []);
222 Expect.isNull(iterator.current);
223
224 iterator = new ClassHierarchyNodeIterable(
225 world.getClassHierarchyNode(B),
226 new EnumSet<Instantiation>.fromValues(<Instantiation>[
227 Instantiation.DIRECTLY_INSTANTIATED,
228 Instantiation.UNINSTANTIATED])).iterator;
229 checkState(B, currentNode: null, stack: null);
230 Expect.isNull(iterator.current);
231 Expect.isTrue(iterator.moveNext());
232 checkState(B, currentNode: D, stack: []);
233 Expect.equals(D, iterator.current);
234 Expect.isFalse(iterator.moveNext());
235 checkState(B, currentNode: null, stack: []);
236 Expect.isNull(iterator.current);
237
238 iterator = new ClassHierarchyNodeIterable(
239 world.getClassHierarchyNode(A),
240 ClassHierarchyNode.ALL).iterator;
241 checkState(A, currentNode: null, stack: null);
242 Expect.isNull(iterator.current);
243 Expect.isTrue(iterator.moveNext());
244 checkState(A, currentNode: A, stack: [C, B]);
245 Expect.equals(A, iterator.current);
246 Expect.isTrue(iterator.moveNext());
247 checkState(A, currentNode: C, stack: [E, F, G, B]);
248 Expect.equals(C, iterator.current);
249 Expect.isTrue(iterator.moveNext());
250 checkState(A, currentNode: E, stack: [F, G, B]);
251 Expect.equals(E, iterator.current);
252 Expect.isTrue(iterator.moveNext());
253 checkState(A, currentNode: F, stack: [G, B]);
254 Expect.equals(F, iterator.current);
255 Expect.isTrue(iterator.moveNext());
256 checkState(A, currentNode: G, stack: [B]);
257 Expect.equals(G, iterator.current);
258 Expect.isTrue(iterator.moveNext());
259 checkState(A, currentNode: B, stack: [D]);
260 Expect.equals(B, iterator.current);
261 Expect.isTrue(iterator.moveNext());
262 checkState(A, currentNode: D, stack: []);
263 Expect.equals(D, iterator.current);
264 Expect.isFalse(iterator.moveNext());
265 checkState(A, currentNode: null, stack: []);
266 Expect.isNull(iterator.current);
267
268 iterator = new ClassHierarchyNodeIterable(
269 world.getClassHierarchyNode(A),
270 ClassHierarchyNode.ALL,
271 includeRoot: false).iterator;
272 checkState(A, currentNode: null, stack: null);
273 Expect.isNull(iterator.current);
274 Expect.isTrue(iterator.moveNext());
275 checkState(A, currentNode: C, stack: [E, F, G, B]);
276 Expect.equals(C, iterator.current);
277 Expect.isTrue(iterator.moveNext());
278 checkState(A, currentNode: E, stack: [F, G, B]);
279 Expect.equals(E, iterator.current);
280 Expect.isTrue(iterator.moveNext());
281 checkState(A, currentNode: F, stack: [G, B]);
282 Expect.equals(F, iterator.current);
283 Expect.isTrue(iterator.moveNext());
284 checkState(A, currentNode: G, stack: [B]);
285 Expect.equals(G, iterator.current);
286 Expect.isTrue(iterator.moveNext());
287 checkState(A, currentNode: B, stack: [D]);
288 Expect.equals(B, iterator.current);
289 Expect.isTrue(iterator.moveNext());
290 checkState(A, currentNode: D, stack: []);
291 Expect.equals(D, iterator.current);
292 Expect.isFalse(iterator.moveNext());
293 checkState(A, currentNode: null, stack: []);
294 Expect.isNull(iterator.current);
295
296 iterator = new ClassHierarchyNodeIterable(
297 world.getClassHierarchyNode(A),
298 new EnumSet<Instantiation>.fromValues(<Instantiation>[
299 Instantiation.DIRECTLY_INSTANTIATED,
300 Instantiation.UNINSTANTIATED])).iterator;
301 checkState(A, currentNode: null, stack: null);
302 Expect.isNull(iterator.current);
303 Expect.isTrue(iterator.moveNext());
304 checkState(A, currentNode: A, stack: [C, B]);
305 Expect.equals(A, iterator.current);
306 Expect.isTrue(iterator.moveNext());
307 checkState(A, currentNode: C, stack: [E, F, G, B]);
308 Expect.equals(C, iterator.current);
309 Expect.isTrue(iterator.moveNext());
310 checkState(A, currentNode: E, stack: [F, G, B]);
311 Expect.equals(E, iterator.current);
312 Expect.isTrue(iterator.moveNext());
313 checkState(A, currentNode: F, stack: [G, B]);
314 Expect.equals(F, iterator.current);
315 Expect.isTrue(iterator.moveNext());
316 checkState(A, currentNode: G, stack: [B]);
317 Expect.equals(G, iterator.current);
318 Expect.isTrue(iterator.moveNext());
319 checkState(A, currentNode: D, stack: []);
320 Expect.equals(D, iterator.current);
321 Expect.isFalse(iterator.moveNext());
322 checkState(A, currentNode: null, stack: []);
323 Expect.isNull(iterator.current);
324
325 iterator = new ClassHierarchyNodeIterable(
326 world.getClassHierarchyNode(A),
327 new EnumSet<Instantiation>.fromValues(<Instantiation>[
328 Instantiation.DIRECTLY_INSTANTIATED,
329 Instantiation.UNINSTANTIATED]),
330 includeRoot: false).iterator;
331 checkState(A, currentNode: null, stack: null);
332 Expect.isNull(iterator.current);
333 Expect.isTrue(iterator.moveNext());
334 checkState(A, currentNode: C, stack: [E, F, G, B]);
335 Expect.equals(C, iterator.current);
336 Expect.isTrue(iterator.moveNext());
337 checkState(A, currentNode: E, stack: [F, G, B]);
338 Expect.equals(E, iterator.current);
339 Expect.isTrue(iterator.moveNext());
340 checkState(A, currentNode: F, stack: [G, B]);
341 Expect.equals(F, iterator.current);
342 Expect.isTrue(iterator.moveNext());
343 checkState(A, currentNode: G, stack: [B]);
344 Expect.equals(G, iterator.current);
345 Expect.isTrue(iterator.moveNext());
346 checkState(A, currentNode: D, stack: []);
347 Expect.equals(D, iterator.current);
348 Expect.isFalse(iterator.moveNext());
349 checkState(A, currentNode: null, stack: []);
350 Expect.isNull(iterator.current);
351 }
352
353 testForEach() async {
354 var env = await TypeEnvironment.create(r"""
355 /// A
356 /// / \
357 /// B C
358 /// / /|\
359 /// D E F G
360 /// / \
361 /// H I
362 ///
363 class A implements X {}
364 class B extends A {}
365 class C extends A {}
366 class D extends B {}
367 class E extends C {}
368 class F extends C implements B {}
369 class G extends C implements D {}
370 class H extends F {}
371 class I extends F {}
372 class X {}
373 """,
374 mainSource: r"""
375 main() {
376 new A();
377 new C();
378 new D();
379 new E();
380 new F();
381 new G();
382 new H();
383 new I();
95 } 384 }
96 385 """,
97 List<ClassElement> classesOf(Link<ClassHierarchyNode> link) { 386 useMockCompiler: false);
98 if (link == null) return null; 387 World world = env.compiler.world;
99 return link.map(classOf).toList(); 388
389 ClassElement A = env.getElement("A");
390 ClassElement B = env.getElement("B");
391 ClassElement C = env.getElement("C");
392 ClassElement D = env.getElement("D");
393 ClassElement E = env.getElement("E");
394 ClassElement F = env.getElement("F");
395 ClassElement G = env.getElement("G");
396 ClassElement H = env.getElement("H");
397 ClassElement I = env.getElement("I");
398 ClassElement X = env.getElement("X");
399
400 void checkForEachSubclass(ClassElement cls, List<ClassElement> expected) {
401 ClassSet classSet = world.getClassSet(cls);
402 List<ClassElement> visited = <ClassElement>[];
403 classSet.forEachSubclass((ClassElement cls) {
404 visited.add(cls);
405 }, ClassHierarchyNode.ALL);
406
407 Expect.listEquals(expected, visited,
408 "Unexpected classes on $cls.forEachSubclass:\n"
409 "Actual: $visited, expected: $expected\n$classSet");
410
411 visited = <ClassElement>[];
412 classSet.forEachSubclass((ClassElement cls) {
413 visited.add(cls);
414 return ForEach.CONTINUE;
415 }, ClassHierarchyNode.ALL);
416
417 Expect.listEquals(expected, visited,
418 "Unexpected classes on $cls.forEachSubclass:\n"
419 "Actual: $visited, expected: $expected\n$classSet");
420 }
421
422 checkForEachSubclass(A, [A, B, D, C, G, F, I, H, E]);
423 checkForEachSubclass(B, [B, D]);
424 checkForEachSubclass(C, [C, G, F, I, H, E]);
425 checkForEachSubclass(D, [D]);
426 checkForEachSubclass(E, [E]);
427 checkForEachSubclass(F, [F, I, H]);
428 checkForEachSubclass(G, [G]);
429 checkForEachSubclass(H, [H]);
430 checkForEachSubclass(I, [I]);
431 checkForEachSubclass(X, [X]);
432
433 void checkForEachSubtype(ClassElement cls, List<ClassElement> expected) {
434 ClassSet classSet = world.getClassSet(cls);
435 List<ClassElement> visited = <ClassElement>[];
436 classSet.forEachSubtype((ClassElement cls) {
437 visited.add(cls);
438 }, ClassHierarchyNode.ALL);
439
440 Expect.listEquals(expected, visited,
441 "Unexpected classes on $cls.forEachSubtype:\n"
442 "Actual: $visited, expected: $expected\n$classSet");
443
444 visited = <ClassElement>[];
445 classSet.forEachSubtype((ClassElement cls) {
446 visited.add(cls);
447 return ForEach.CONTINUE;
448 }, ClassHierarchyNode.ALL);
449
450 Expect.listEquals(expected, visited,
451 "Unexpected classes on $cls.forEachSubtype:\n"
452 "Actual: $visited, expected: $expected\n$classSet");
453 }
454
455 checkForEachSubtype(A, [A, B, D, C, G, F, I, H, E]);
456 checkForEachSubtype(B, [B, D, F, I, H, G]);
457 checkForEachSubtype(C, [C, G, F, I, H, E]);
458 checkForEachSubtype(D, [D, G]);
459 checkForEachSubtype(E, [E]);
460 checkForEachSubtype(F, [F, I, H]);
461 checkForEachSubtype(G, [G]);
462 checkForEachSubtype(H, [H]);
463 checkForEachSubtype(I, [I]);
464 checkForEachSubtype(X, [X, A, B, D, C, G, F, I, H, E]);
465
466 void checkForEach(
467 ClassElement cls,
468 List<ClassElement> expected,
469 {ClassElement stop,
470 List<ClassElement> skipSubclasses: const <ClassElement>[],
471 bool forEachSubtype: false,
472 EnumSet<Instantiation> mask}) {
473
474 if (mask == null) {
475 mask = ClassHierarchyNode.ALL;
476 }
477
478 ClassSet classSet = world.getClassSet(cls);
479 List<ClassElement> visited = <ClassElement>[];
480
481 ForEach visit(ClassElement cls) {
482 visited.add(cls);
483 if (cls == stop) {
484 return ForEach.STOP;
485 } else if (skipSubclasses.contains(cls)) {
486 return ForEach.SKIP_SUBCLASSES;
100 } 487 }
101 488 return ForEach.CONTINUE;
102 ClassElement foundRoot = iterator.root.cls; 489 }
103 ClassElement foundCurrentNode = classOf(iterator.currentNode); 490
104 List<ClassElement> foundStack = classesOf(iterator.stack); 491 if (forEachSubtype) {
105 492 classSet.forEachSubtype(visit, mask);
106 StringBuffer sb = new StringBuffer(); 493 } else {
107 sb.write('{\n root: $foundRoot'); 494 classSet.forEachSubclass(visit, mask);
108 sb.write('\n currentNode: $foundCurrentNode'); 495 }
109 sb.write('\n stack: $foundStack\n}'); 496
110 497 Expect.listEquals(expected, visited,
111 Expect.equals(root, foundRoot, 498 "Unexpected classes on $cls."
112 "Expected root $root in $sb."); 499 "forEach${forEachSubtype ? 'Subtype' : 'Subclass'} "
113 if (currentNode == null) { 500 "(stop:$stop, skipSubclasses:$skipSubclasses):\n"
114 Expect.isNull(iterator.currentNode, 501 "Actual: $visited, expected: $expected\n$classSet");
115 "Unexpected non-null currentNode in $sb."); 502 }
116 } else { 503
117 Expect.isNotNull(foundCurrentNode, 504 checkForEach(A, [A, B, D, C, G, F, I, H, E]);
118 "Expected non-null currentNode ${currentNode} in $sb."); 505 checkForEach(A, [A], stop: A);
119 Expect.equals(currentNode, foundCurrentNode, 506 checkForEach(A, [A, B, C, G, F, I, H, E], skipSubclasses: [B]);
120 "Expected currentNode $currentNode in $sb."); 507 checkForEach(A, [A, B, C], skipSubclasses: [B, C]);
121 } 508 checkForEach(A, [A, B, C, G], stop: G, skipSubclasses: [B]);
122 if (stack == null) { 509
123 Expect.isNull(foundStack, 510 checkForEach(B, [B, D, F, I, H, G], forEachSubtype: true);
124 "Unexpected non-null stack in $sb."); 511 checkForEach(B, [B, D], stop: D, forEachSubtype: true);
125 } else { 512 checkForEach(B, [B, D, F, G], skipSubclasses: [F], forEachSubtype: true);
126 Expect.isNotNull(foundStack, 513 checkForEach(B, [B, F, I, H, G], skipSubclasses: [B], forEachSubtype: true);
127 "Expected non-null stack ${stack} in $sb."); 514 checkForEach(B, [B, D, F, I, H, G], skipSubclasses: [D], forEachSubtype: true) ;
128 Expect.listEquals(stack, foundStack, 515
129 "Expected stack ${stack}, " 516 checkForEach(X, [X, A, B, D, C, G, F, I, H, E], forEachSubtype: true);
130 "found ${foundStack} in $sb."); 517 checkForEach(X, [X, A, B, D], stop: D, forEachSubtype: true);
131 } 518 checkForEach(X, [X, A, B, D, C, G, F, E],
132 } 519 skipSubclasses: [F], forEachSubtype: true);
133 520 checkForEach(X, [X, A, B, D, C, G, F, I, H, E],
134 iterator = new ClassHierarchyNodeIterable( 521 skipSubclasses: [X], forEachSubtype: true);
135 world.getClassHierarchyNode(G), 522 checkForEach(X, [X, A, B, D, C, G, F, I, H, E],
136 ClassHierarchyNode.ALL).iterator; 523 skipSubclasses: [D], forEachSubtype: true);
137 checkState(G, currentNode: null, stack: null); 524 checkForEach(X, [A, D, C, G, F, I, H, E],
138 Expect.isNull(iterator.current); 525 forEachSubtype: true,
139 Expect.isTrue(iterator.moveNext()); 526 mask: ClassHierarchyNode.DIRECTLY_INSTANTIATED);
140 checkState(G, currentNode: G, stack: []); 527 checkForEach(X, [A, B, D, C, G, F, I, H, E],
141 Expect.equals(G, iterator.current); 528 forEachSubtype: true,
142 Expect.isFalse(iterator.moveNext()); 529 mask: ClassHierarchyNode.INSTANTIATED);
143 checkState(G, currentNode: null, stack: []); 530
144 Expect.isNull(iterator.current); 531 void checkAny(
145 532 ClassElement cls,
146 iterator = new ClassHierarchyNodeIterable( 533 List<ClassElement> expected,
147 world.getClassHierarchyNode(G), 534 {ClassElement find,
148 ClassHierarchyNode.ALL, 535 bool expectedResult,
149 includeRoot: false).iterator; 536 bool anySubtype: false}) {
150 checkState(G, currentNode: null, stack: null); 537 ClassSet classSet = world.getClassSet(cls);
151 Expect.isNull(iterator.current); 538 List<ClassElement> visited = <ClassElement>[];
152 Expect.isFalse(iterator.moveNext()); 539
153 checkState(G, currentNode: null, stack: []); 540 bool visit(ClassElement cls) {
154 Expect.isNull(iterator.current); 541 visited.add(cls);
155 542 return cls == find;
156 iterator = new ClassHierarchyNodeIterable( 543 }
157 world.getClassHierarchyNode(C), 544
158 ClassHierarchyNode.ALL).iterator; 545 bool result;
159 checkState(C, currentNode: null, stack: null); 546 if (anySubtype) {
160 Expect.isNull(iterator.current); 547 result = classSet.anySubtype(visit, ClassHierarchyNode.ALL);
161 Expect.isTrue(iterator.moveNext()); 548 } else {
162 checkState(C, currentNode: C, stack: [E, F, G]); 549 result = classSet.anySubclass(visit, ClassHierarchyNode.ALL);
163 Expect.equals(C, iterator.current); 550 }
164 Expect.isTrue(iterator.moveNext()); 551
165 checkState(C, currentNode: E, stack: [F, G]); 552 Expect.equals(expectedResult, result,
166 Expect.equals(E, iterator.current); 553 "Unexpected result on $cls."
167 Expect.isTrue(iterator.moveNext()); 554 "any${anySubtype ? 'Subtype' : 'Subclass'} "
168 checkState(C, currentNode: F, stack: [G]); 555 "(find:$find).");
169 Expect.equals(F, iterator.current); 556
170 Expect.isTrue(iterator.moveNext()); 557 Expect.listEquals(expected, visited,
171 checkState(C, currentNode: G, stack: []); 558 "Unexpected classes on $cls."
172 Expect.equals(G, iterator.current); 559 "any${anySubtype ? 'Subtype' : 'Subclass'} "
173 Expect.isFalse(iterator.moveNext()); 560 "(find:$find):\n"
174 checkState(C, currentNode: null, stack: []); 561 "Actual: $visited, expected: $expected\n$classSet");
175 Expect.isNull(iterator.current); 562 }
176 563
177 iterator = new ClassHierarchyNodeIterable( 564 checkAny(A, [A, B, D, C, G, F, I, H, E], expectedResult: false);
178 world.getClassHierarchyNode(D), 565 checkAny(A, [A], find: A, expectedResult: true);
179 ClassHierarchyNode.ALL).iterator; 566 checkAny(A, [A, B, D, C, G, F, I], find: I, expectedResult: true);
180 checkState(D, currentNode: null, stack: null); 567
181 Expect.isNull(iterator.current); 568 checkAny(B, [B, D, F, I, H, G], anySubtype: true, expectedResult: false);
182 Expect.isTrue(iterator.moveNext()); 569 checkAny(B, [B, D, F, I, H, G],
183 checkState(D, currentNode: D, stack: []); 570 find: A, anySubtype: true, expectedResult: false);
184 Expect.equals(D, iterator.current); 571 checkAny(B, [B, D],
185 Expect.isFalse(iterator.moveNext()); 572 find: D, anySubtype: true, expectedResult: true);
186 checkState(D, currentNode: null, stack: []); 573 checkAny(B, [B, D, F, I],
187 Expect.isNull(iterator.current); 574 find: I, anySubtype: true, expectedResult: true);
188 575
189 iterator = new ClassHierarchyNodeIterable( 576 checkAny(X, [X, A, B, D, C, G, F, I, H, E],
190 world.getClassHierarchyNode(B), 577 anySubtype: true, expectedResult: false);
191 ClassHierarchyNode.ALL).iterator; 578 checkAny(X, [X, A],
192 checkState(B, currentNode: null, stack: null); 579 find: A, anySubtype: true, expectedResult: true);
193 Expect.isNull(iterator.current); 580 checkAny(X, [X, A, B, D],
194 Expect.isTrue(iterator.moveNext()); 581 find: D, anySubtype: true, expectedResult: true);
195 checkState(B, currentNode: B, stack: [D]); 582 checkAny(X, [X, A, B, D, C, G, F, I],
196 Expect.equals(B, iterator.current); 583 find: I, anySubtype: true, expectedResult: true);
197 Expect.isTrue(iterator.moveNext());
198 checkState(B, currentNode: D, stack: []);
199 Expect.equals(D, iterator.current);
200 Expect.isFalse(iterator.moveNext());
201 checkState(B, currentNode: null, stack: []);
202 Expect.isNull(iterator.current);
203
204 iterator = new ClassHierarchyNodeIterable(
205 world.getClassHierarchyNode(B),
206 ClassHierarchyNode.ALL,
207 includeRoot: false).iterator;
208 checkState(B, currentNode: null, stack: null);
209 Expect.isNull(iterator.current);
210 Expect.isTrue(iterator.moveNext());
211 checkState(B, currentNode: D, stack: []);
212 Expect.equals(D, iterator.current);
213 Expect.isFalse(iterator.moveNext());
214 checkState(B, currentNode: null, stack: []);
215 Expect.isNull(iterator.current);
216
217 iterator = new ClassHierarchyNodeIterable(
218 world.getClassHierarchyNode(B),
219 new EnumSet<Instantiation>.fromValues(<Instantiation>[
220 Instantiation.DIRECTLY_INSTANTIATED,
221 Instantiation.UNINSTANTIATED])).iterator;
222 checkState(B, currentNode: null, stack: null);
223 Expect.isNull(iterator.current);
224 Expect.isTrue(iterator.moveNext());
225 checkState(B, currentNode: D, stack: []);
226 Expect.equals(D, iterator.current);
227 Expect.isFalse(iterator.moveNext());
228 checkState(B, currentNode: null, stack: []);
229 Expect.isNull(iterator.current);
230
231 iterator = new ClassHierarchyNodeIterable(
232 world.getClassHierarchyNode(A),
233 ClassHierarchyNode.ALL).iterator;
234 checkState(A, currentNode: null, stack: null);
235 Expect.isNull(iterator.current);
236 Expect.isTrue(iterator.moveNext());
237 checkState(A, currentNode: A, stack: [C, B]);
238 Expect.equals(A, iterator.current);
239 Expect.isTrue(iterator.moveNext());
240 checkState(A, currentNode: C, stack: [E, F, G, B]);
241 Expect.equals(C, iterator.current);
242 Expect.isTrue(iterator.moveNext());
243 checkState(A, currentNode: E, stack: [F, G, B]);
244 Expect.equals(E, iterator.current);
245 Expect.isTrue(iterator.moveNext());
246 checkState(A, currentNode: F, stack: [G, B]);
247 Expect.equals(F, iterator.current);
248 Expect.isTrue(iterator.moveNext());
249 checkState(A, currentNode: G, stack: [B]);
250 Expect.equals(G, iterator.current);
251 Expect.isTrue(iterator.moveNext());
252 checkState(A, currentNode: B, stack: [D]);
253 Expect.equals(B, iterator.current);
254 Expect.isTrue(iterator.moveNext());
255 checkState(A, currentNode: D, stack: []);
256 Expect.equals(D, iterator.current);
257 Expect.isFalse(iterator.moveNext());
258 checkState(A, currentNode: null, stack: []);
259 Expect.isNull(iterator.current);
260
261 iterator = new ClassHierarchyNodeIterable(
262 world.getClassHierarchyNode(A),
263 ClassHierarchyNode.ALL,
264 includeRoot: false).iterator;
265 checkState(A, currentNode: null, stack: null);
266 Expect.isNull(iterator.current);
267 Expect.isTrue(iterator.moveNext());
268 checkState(A, currentNode: C, stack: [E, F, G, B]);
269 Expect.equals(C, iterator.current);
270 Expect.isTrue(iterator.moveNext());
271 checkState(A, currentNode: E, stack: [F, G, B]);
272 Expect.equals(E, iterator.current);
273 Expect.isTrue(iterator.moveNext());
274 checkState(A, currentNode: F, stack: [G, B]);
275 Expect.equals(F, iterator.current);
276 Expect.isTrue(iterator.moveNext());
277 checkState(A, currentNode: G, stack: [B]);
278 Expect.equals(G, iterator.current);
279 Expect.isTrue(iterator.moveNext());
280 checkState(A, currentNode: B, stack: [D]);
281 Expect.equals(B, iterator.current);
282 Expect.isTrue(iterator.moveNext());
283 checkState(A, currentNode: D, stack: []);
284 Expect.equals(D, iterator.current);
285 Expect.isFalse(iterator.moveNext());
286 checkState(A, currentNode: null, stack: []);
287 Expect.isNull(iterator.current);
288
289 iterator = new ClassHierarchyNodeIterable(
290 world.getClassHierarchyNode(A),
291 new EnumSet<Instantiation>.fromValues(<Instantiation>[
292 Instantiation.DIRECTLY_INSTANTIATED,
293 Instantiation.UNINSTANTIATED])).iterator;
294 checkState(A, currentNode: null, stack: null);
295 Expect.isNull(iterator.current);
296 Expect.isTrue(iterator.moveNext());
297 checkState(A, currentNode: A, stack: [C, B]);
298 Expect.equals(A, iterator.current);
299 Expect.isTrue(iterator.moveNext());
300 checkState(A, currentNode: C, stack: [E, F, G, B]);
301 Expect.equals(C, iterator.current);
302 Expect.isTrue(iterator.moveNext());
303 checkState(A, currentNode: E, stack: [F, G, B]);
304 Expect.equals(E, iterator.current);
305 Expect.isTrue(iterator.moveNext());
306 checkState(A, currentNode: F, stack: [G, B]);
307 Expect.equals(F, iterator.current);
308 Expect.isTrue(iterator.moveNext());
309 checkState(A, currentNode: G, stack: [B]);
310 Expect.equals(G, iterator.current);
311 Expect.isTrue(iterator.moveNext());
312 checkState(A, currentNode: D, stack: []);
313 Expect.equals(D, iterator.current);
314 Expect.isFalse(iterator.moveNext());
315 checkState(A, currentNode: null, stack: []);
316 Expect.isNull(iterator.current);
317
318 iterator = new ClassHierarchyNodeIterable(
319 world.getClassHierarchyNode(A),
320 new EnumSet<Instantiation>.fromValues(<Instantiation>[
321 Instantiation.DIRECTLY_INSTANTIATED,
322 Instantiation.UNINSTANTIATED]),
323 includeRoot: false).iterator;
324 checkState(A, currentNode: null, stack: null);
325 Expect.isNull(iterator.current);
326 Expect.isTrue(iterator.moveNext());
327 checkState(A, currentNode: C, stack: [E, F, G, B]);
328 Expect.equals(C, iterator.current);
329 Expect.isTrue(iterator.moveNext());
330 checkState(A, currentNode: E, stack: [F, G, B]);
331 Expect.equals(E, iterator.current);
332 Expect.isTrue(iterator.moveNext());
333 checkState(A, currentNode: F, stack: [G, B]);
334 Expect.equals(F, iterator.current);
335 Expect.isTrue(iterator.moveNext());
336 checkState(A, currentNode: G, stack: [B]);
337 Expect.equals(G, iterator.current);
338 Expect.isTrue(iterator.moveNext());
339 checkState(A, currentNode: D, stack: []);
340 Expect.equals(D, iterator.current);
341 Expect.isFalse(iterator.moveNext());
342 checkState(A, currentNode: null, stack: []);
343 Expect.isNull(iterator.current);
344 }));
345 } 584 }
346
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698