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

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

Issue 1604333002: Implement TypeMask.isDisjoint instead of using intersection. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: 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
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
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.
4
5 import 'package:async_helper/async_helper.dart';
6 import 'package:expect/expect.dart';
7 import 'package:compiler/src/types/types.dart';
8
9 import 'compiler_helper.dart';
10
11 const String CODE = """
12 class A {}
13 class B extends A {}
14 class C extends A {}
15
16 class D implements A {}
17
18 class E {}
19 class F extends E {}
20 class G implements E {}
21
22 class H {}
23 class I implements H {}
24 class J extends D implements I {}
25
sra1 2016/01/20 21:53:48 Test mixins too (another kind of 'implements')
Siggi Cherem (dart-lang) 2016/01/20 22:14:57 Good idea. Done.
26 main() {
27 print([new A(), new B(), new C(), new D(), new E(), new F(), new G(),
28 new H(), new I(), new J()]);
29 }
30 """;
31
32 Uri uri = new Uri(scheme: 'source');
33 var compiler = compilerFor(CODE, uri);
34 var world = compiler.world;
35
36 main() {
37 asyncTest(() => compiler.run(uri).then((_) {
38
39 // Empty
40 check(' ! ', ' ! '); // both non-null
41 check(' ! ', ' '); // one non-null
42 check(' ', ' ! '); // one non-null
43 check(' ', ' ', areDisjoint: false); // null is common
44
45 // Exact
46 check('A!=', 'A!=', areDisjoint: false);
47 check('A!=', 'B!=');
48 check('A!=', 'E!=');
49 check('A =', 'E =', areDisjoint: false); // null is common
50
51 // Exact with subclass
52 check('A!=', 'A!<', areDisjoint: false);
53 check('B!=', 'A!<', areDisjoint: false);
54 check('A!=', 'B!<');
55 check('A!=', 'E!<');
56 check('A =', 'E!<');
57 check('A =', 'E <', areDisjoint: false);
58
59 // Exact with subtype
60 check('A!=', 'A!*', areDisjoint: false);
61 check('B!=', 'A!*', areDisjoint: false);
62 check('A!=', 'B!*');
63 check('A!=', 'E!*');
64 check('A!=', 'I!*');
65 check('J!=', 'H!*', areDisjoint: false);
66
67 // Subclass with subclass
68 check('A!<', 'A!<', areDisjoint: false);
69 check('A!<', 'B!<', areDisjoint: false);
70 check('A!<', 'E!<');
71 check('A!<', 'H!<');
72 check('D!<', 'I!<');
73
74 // Subclass with subtype
75 check('A!<', 'A!*', areDisjoint: false);
76 check('A!<', 'B!*', areDisjoint: false);
77 check('A!<', 'E!*');
78 check('A!<', 'H!*');
79 check('D!<', 'I!*', areDisjoint: false);
80
81 // Subtype with subtype
82 check('A!*', 'A!*', areDisjoint: false);
83 check('A!*', 'B!*', areDisjoint: false);
84 check('A!*', 'E!*');
85 check('A!*', 'H!*', areDisjoint: false);
86 check('D!*', 'I!*', areDisjoint: false);
87
88 // Unions!
89 checkUnions(['B!=', 'C!='], ['A!=']);
90 checkUnions(['B!=', 'C!='], ['A =']);
91 checkUnions(['B!=', 'C ='], ['A ='], areDisjoint: false);
92
93 checkUnions(['B!=', 'C!='], ['A!<'], areDisjoint: false);
94 checkUnions(['B!=', 'C!='], ['B!='], areDisjoint: false);
95 checkUnions(['A!<', 'E!<'], ['C!='], areDisjoint: false);
96 checkUnions(['A!<', 'E!<'], ['F!='], areDisjoint: false);
97
98 checkUnions(['A!=', 'E!='], ['C!=', 'F!=']);
99 checkUnions(['A!=', 'E!='], ['A!=', 'F!='], areDisjoint: false);
100 checkUnions(['B!=', 'E!='], ['A!<', 'F!='], areDisjoint: false);
101 checkUnions(['A!<', 'E!<'], ['C!=', 'F!='], areDisjoint: false);
102 checkUnions(['A!=', 'E!='], ['C!=', 'F!=']);
103 }));
104 }
105
106 /// Checks the expectation of `isDisjoint` for two mask. Also checks that the
107 /// result is consistent with an equivalent (but slower) implementation based on
108 /// intersection.
109 checkMask(TypeMask m1, TypeMask m2, {areDisjoint: false}) {
110 print('masks: $m1 $m2');
111 Expect.equals(areDisjoint, m1.isDisjoint(m2, world));
112 Expect.equals(areDisjoint, m2.isDisjoint(m1, world));
113 var i1 = m1.intersection(m2, world);
114 Expect.equals(areDisjoint, i1.isEmpty && !i1.isNullable);
115 var i2 = m2.intersection(m1, world);
116 Expect.equals(areDisjoint, i2.isEmpty && !i2.isNullable);
117 }
118
119 /// Checks the expectation of `isDisjoint` for two mask descriptors (see
120 /// [maskOf] for details).
121 check(String typeMaskDescriptor1, String typeMaskDescriptor2,
122 {areDisjoint: true}) {
123 print('[$typeMaskDescriptor1] & [$typeMaskDescriptor2]');
124 checkMask(maskOf(typeMaskDescriptor1), maskOf(typeMaskDescriptor2),
125 areDisjoint: areDisjoint);
126 }
127
128
129 checkUnions(List descriptors1, List descriptors2, {areDisjoint: true}) {
130 print('[$descriptors1] & [$descriptors2]');
131 var m1 = new TypeMask.unionOf(descriptors1.map(maskOf).toList(), world);
132 var m2 = new TypeMask.unionOf(descriptors2.map(maskOf).toList(), world);
133 checkMask(m1, m2, areDisjoint: areDisjoint);
134 }
135
136 Map _maskCache = {};
137 Map _elementCache = {};
138
139 /// Parses a descriptor of a flat mask. A descriptor is of the form "AXY" where:
140 /// A: either a type T or " " (base class or empty)
141 /// X: can be either ! or " " (nullable/nonnullable)
142 /// Y: can be either " " (no flag), = (exact), < (subclass), * (subtype)
143 ///
144 /// Examples:
145 /// "-! " - empty, non-null
146 /// "- " - null
147 /// "Type!=" - non-null exact Type
148 /// "Type =" - nullable exact Type
149 /// "Type!<" - non-null subclass of Type
150 /// "Type!*" - non-null subtype of Type
151 TypeMask maskOf(String descriptor) =>
152 _maskCache.putIfAbsent(descriptor, () {
153 Expect.isTrue(descriptor.length >= 3);
154 var type = descriptor.substring(0, descriptor.length - 2);
155 bool isNullable = descriptor[descriptor.length - 2] != '!';
156 bool isExact = descriptor[descriptor.length - 1] == '=';
157 bool isSubclass = descriptor[descriptor.length - 1] == '<';
158 bool isSubtype = descriptor[descriptor.length - 1] == '*';
159
160 if (type == " ") {
161 Expect.isFalse(isExact || isSubclass || isSubtype);
162 return isNullable ? new TypeMask.empty() : new TypeMask.nonNullEmpty();
163 }
164
165 Expect.isTrue(isExact || isSubclass || isSubtype);
166 var element = _elementCache.putIfAbsent(type,
167 () => type == " " ? null : findElement(compiler, type));
168
169 var mask = isExact
170 ? new TypeMask.nonNullExact(element, world)
171 : (isSubclass
172 ? new TypeMask.nonNullSubclass(element, world)
173 : new TypeMask.nonNullSubtype(element, world));
174 return isNullable ? mask.nullable() : mask;
175 });
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/types/union_type_mask.dart ('k') | tests/compiler/dart2js_extra/dart2js_extra.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698