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

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

Issue 1571953002: cps_ir: add refinement on "success" arguments for a set of whitelisted methods (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) 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
3 // BSD-style license that can be found in the LICENSE file.
4
5 /// Test that the cps ir can refine the type of the arguments of certain
6 /// whitelisted operators and methods in the core libraries.
7 library argument_refinement_test;
8
9 import 'js_backend_cps_ir.dart';
10
11 List<TestEntry> tests = [
12 numArgTest('print(x - y);', r'P.print(J.$sub$n(x, y));'),
13 numArgTest('print(x / y);', r'P.print(J.$div$n(x, y));'),
14 numArgTest('print(x % y);', r'P.print(J.$mod$n(x, y));'),
15 numArgTest('print(x ~/ y);', r'P.print(J.$tdiv$n(x, y));'),
16 numArgTest('print(x >> y);', r'P.print(J.$shr$n(x, y));'),
17 numArgTest('print(x << y);', r'P.print(J.$shl$n(x, y));'),
18 numArgTest('print(x & y);', r'P.print(J.$and$n(x, y));'),
19 numArgTest('print(x | y);', r'P.print(J.$or$n(x, y));'),
20 numArgTest('print(x ^ y);', r'P.print(J.$xor$n(x, y));'),
21 numArgTest('print(x > y);', r'P.print(J.$gt$n(x, y));'),
22 numArgTest('print(x < y);', r'P.print(J.$lt$n(x, y));'),
23 numArgTest('print(x >= y);', r'P.print(J.$ge$n(x, y));'),
24 numArgTest('print(x <= y);', r'P.print(J.$le$n(x, y));'),
25 numArgTest('print(x.remainder(y));', r'P.print(J.remainder$1$n(x, y));'),
26 num2ArgTest('print(x.clamp(y, z));', r'P.print(J.clamp$2$n(x, y, z));'),
27 noRefinementNumTest('print(x + y);', r'P.print(J.$add$ns(x, y));'),
28 noRefinementNumTest('print(x * y);', r'P.print(J.$mul$ns(x, y));'),
29 noRefinementNumTest('print(x.compareTo(y));', r'P.print(J.compareTo$1$ns(x, y) );'),
30
31 // TODO(sigmund): would be nice if we can disable inlining on the following
32 // tests...
33 notStringNumTest('print(x + y);',
34 'if (typeof y !== "number")\n '
35 ' throw H.wrapException(H.argumentErrorValue(y));\n '
36 'P.print(x + y);'),
37 notStringNumTest('print(x * y);',
38 'if (typeof y !== "number")\n '
39 ' throw H.wrapException(H.argumentErrorValue(y));\n '
40 'P.print(x * y);'),
41 notStringNumTest('print(x.compareTo(y));',
42 'if (typeof y !== "number")\n '
43 ' throw H.wrapException(H.argumentErrorValue(y));\n '
44 'P.print(x < y ? -1 : x > y ? 1 : x === y ? x === 0 ? '
45 '(y === 0 ? 1 / y < 0 : y < 0) === (x === 0 ? 1 / x < 0 : x < 0) ? 0 : '
46 '(x === 0 ? 1 / x < 0 : x < 0) ? -1 : 1 : 0 : isNaN(x) ? isNaN(y) '
47 '? 0 : 1 : -1);'),
48
49 intArgTest('print(x.toSigned(y));', r'P.print(J.toSigned$1$i(x, y));'),
50 intArgTest('print(x.toUnsigned(y));', r'P.print(J.toUnsigned$1$i(x, y));'),
51 intArgTest('print(x.modInverse(y));', r'P.print(J.modInverse$1$i(x, y));'),
52 intArgTest('print(x.gcd(y));', r'P.print(J.gcd$1$i(x, y));'),
53
54 int2ArgTest('print(x.modPow(y, z));', r'P.print(J.modPow$2$i(x, y, z));'),
55
56 codeUnitAtTest,
57 mathStaticTest,
58 ];
59
60 void main() {
61 runTests(tests);
62 }
63
64 /// Creates a test for 1-arg methods on num that immediately identify the
65 /// receiver and the argument as num values.
66 numArgTest(String sourceSend, String compiledSend) {
67 return new TestEntry("""
68 main() {
69 var x = int.parse('1233');
70 var y = int.parse('1234');
71 print(x is num);
72 print(y is num);
73 $sourceSend
74 print(x is num);
75 print(y is num); // will be compiled to `true` if we know the type of `y`.
76 }""", """
77 function() {
78 var x = P.int_parse("1233", null, null), y = P.int_parse("1234", null, null);
79 P.print(typeof x === "number");
80 P.print(typeof y === "number");
81 $compiledSend
82 P.print(true);
83 P.print(true);
84 }""");
85 }
86
87 /// Creates a test for 2-arg methods on num that immediately identify the
88 /// receiver and both arguments as num values.
89 num2ArgTest(String sourceSend, String compiledSend) {
90 return new TestEntry("""
91 main() {
92 var x = int.parse('1233');
93 var y = int.parse('1234');
94 var z = int.parse('1235');
95 print(x is num);
96 print(y is num);
97 print(z is num);
98 $sourceSend
99 print(x is num);
100 print(y is num);
101 print(z is num);
102 }""", """
103 function() {
104 var x = P.int_parse("1233", null, null), y = P.int_parse("1234", null, null), z = P.int_parse("1235", null, null);
105 P.print(typeof x === "number");
106 P.print(typeof y === "number");
107 P.print(typeof z === "number");
108 $compiledSend
109 P.print(true);
110 P.print(true);
111 P.print(true);
112 }""");
113 }
114
115 /// Creates a test for 1-arg methods on num that are ambiguous and could be
116 /// methods on other types, and hence we cannot refine the arguments in this
117 /// case.
118 noRefinementNumTest(String sourceSend, String compiledSend) {
119 return new TestEntry("""
120 main() {
121 var x = int.parse('1233');
122 var y = int.parse('1234');
123 print(x is num);
124 print(y is num);
125 $sourceSend
126 print(x is num);
127 print(y is num);
128 }""", """
129 function() {
130 var x = P.int_parse("1233", null, null), y = P.int_parse("1234", null, null);
131 P.print(typeof x === "number");
132 P.print(typeof y === "number");
133 $compiledSend
134 P.print(typeof x === "number");
135 P.print(typeof y === "number");
136 }""");
137 }
138
139
140 /// For operators that are common to String and num, we create tests that do not
141 /// refine the receiver, but that once the receiver type is known, the argument
142 /// type is also known.
143 notStringNumTest(String sourceSend, String compiledSend) {
144 return new TestEntry("""
145 main() {
146 var x = int.parse('1233');
147 var y = int.parse('1234');
148 print(x / 2);
149 print(x is num);
150 print(y is num);
151 $sourceSend
152 print(y is num);
153 }""", """
154 function() {
155 var x = P.int_parse("1233", null, null), y = P.int_parse("1234", null, null);
156 P.print(J.\$div\$n(x, 2));
157 P.print(true);
158 P.print(typeof y === "number");
159 $compiledSend
160 P.print(true);
161 }""");
162 }
163
164 /// Creates a test for 1-arg methods on int that immediately identify the
165 /// receiver and the argument as int values.
166 intArgTest(String sourceSend, String compiledSend) {
167 return new TestEntry("""
168 main() {
169 var x = int.parse('1233');
170 var y = int.parse('1234');
171 print(x is int);
172 print(y is int);
173 $sourceSend
174 print(x is int);
175 print(y is int);
176 }""", """
177 function() {
178 var x = P.int_parse("1233", null, null), y = P.int_parse("1234", null, null);
179 P.print(typeof x === "number" && Math.floor(x) === x);
180 P.print(typeof y === "number" && Math.floor(y) === y);
181 $compiledSend
182 P.print(true);
183 P.print(true);
184 }""");
185 }
186
187 /// Creates a test for 2-arg methods on num that immediately identify the
188 /// receiver and both arguments as num values.
189 int2ArgTest(String sourceSend, String compiledSend) {
190 return new TestEntry("""
191 main() {
192 var x = int.parse('1233');
193 var y = int.parse('1234');
194 var z = int.parse('1235');
195 print(x is int);
196 print(y is int);
197 print(z is int);
198 $sourceSend
199 print(x is int);
200 print(y is int);
201 print(z is int);
202 }""", """
203 function() {
204 var x = P.int_parse("1233", null, null), y = P.int_parse("1234", null, null), z = P.int_parse("1235", null, null);
205 P.print(typeof x === "number" && Math.floor(x) === x);
206 P.print(typeof y === "number" && Math.floor(y) === y);
207 P.print(typeof z === "number" && Math.floor(z) === z);
208 $compiledSend
209 P.print(true);
210 P.print(true);
211 P.print(true);
212 }""");
213 }
214
215 const codeUnitAtTest = const TestEntry(r"""
216 main() {
217 var x = int.parse('3');
218 var y = int.parse('a', onError: (e) => 'abcde');
219 print(x is int);
220 print(y is String);
221 print(y.codeUnitAt(x));
222 print(x is int);
223 print(y is String);
224 }""", r"""
225 function() {
226 var x = P.int_parse("3", null, null), y = P.int_parse("a", new V.main_closure( ), null);
227 P.print(typeof x === "number" && Math.floor(x) === x);
228 P.print(typeof y === "string");
229 P.print(J.codeUnitAt$1$s(y, x));
230 P.print(true);
231 P.print(true);
232 }""");
233
234 const mathStaticTest = const TestEntry(r"""
235 import 'dart:math';
236 main() {
237 var x = int.parse('3');
238 var y = int.parse('1234');
239 var z = int.parse('1236');
240 var w = int.parse('2');
241 print(x is num);
242 print(sin(x));
243 print(x is num);
244
245 print(y is num);
246 print(log(y));
247 print(y is num);
248
249 print(z is num);
250 print(w is num);
251 print(pow(z, w));
252 print(z is num);
253 print(w is num);
254 }""", r"""
255 function() {
256 var x = P.int_parse("3", null, null), y = P.int_parse("1234", null, null), z = P.int_parse("1236", null, null), w = P.int_parse("2", null, null);
257 P.print(typeof x === "number");
258 if (typeof x !== "number")
259 throw H.wrapException(H.argumentErrorValue(x));
260 P.print(Math.sin(x));
261 P.print(true);
262 P.print(typeof y === "number");
263 if (typeof y !== "number")
264 throw H.wrapException(H.argumentErrorValue(y));
265 P.print(Math.log(y));
266 P.print(true);
267 P.print(typeof z === "number");
268 P.print(typeof w === "number");
269 if (typeof z !== "number")
270 throw H.wrapException(H.argumentErrorValue(z));
271 if (typeof w !== "number")
272 throw H.wrapException(H.argumentErrorValue(w));
273 P.print(Math.pow(z, w));
274 P.print(true);
275 P.print(true);
276 }""");
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698