OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'package:expect/expect.dart'; | 6 import 'package:expect/expect.dart'; |
7 import 'package:async_helper/async_helper.dart'; | 7 import 'package:async_helper/async_helper.dart'; |
8 import 'compiler_helper.dart'; | 8 import 'compiler_helper.dart'; |
9 | 9 |
10 const int REMOVED = 0; | 10 const int REMOVED = 0; |
11 const int ABOVE_ZERO = 1; | 11 const int ABOVE_ZERO = 1; |
12 const int BELOW_LENGTH = 2; | 12 const int BELOW_LENGTH = 2; |
13 const int KEPT = 3; | 13 const int KEPT = 3; |
14 const int ONE_CHECK = 4; | 14 const int ONE_CHECK = 4; |
15 const int ONE_ZERO_CHECK = 5; | 15 const int ONE_ZERO_CHECK = 5; |
16 const int BELOW_ZERO_CHECK = 6; | 16 const int BELOW_ZERO_CHECK = 6; |
17 | 17 |
18 final List TESTS = [ | 18 final List TESTS = [ |
19 """ | 19 """ |
20 main() { | 20 main() { |
21 var a = new List(); | 21 var a = new List(); |
22 var sum = 0; | 22 var sum = 0; |
23 for (int i = 0; i < a.length; i++) { | 23 for (int i = 0; i < a.length; i++) { |
24 sum += a[i]; | 24 sum += a[i]; |
25 } | 25 } |
26 return sum; | 26 return sum; |
27 } | 27 } |
28 """, | 28 """, |
29 REMOVED, | 29 REMOVED, |
30 | 30 """ |
31 """ | |
32 main(value) { | 31 main(value) { |
33 var a = new List(); | 32 var a = new List(); |
34 var sum = 0; | 33 var sum = 0; |
35 for (int i = 0; i < value; i++) { | 34 for (int i = 0; i < value; i++) { |
36 sum += a[i]; | 35 sum += a[i]; |
37 } | 36 } |
38 return sum; | 37 return sum; |
39 } | 38 } |
40 """, | 39 """, |
41 ABOVE_ZERO, | 40 ABOVE_ZERO, |
42 | 41 """ |
43 """ | |
44 main(check) { | 42 main(check) { |
45 // Make sure value is an int. | 43 // Make sure value is an int. |
46 var value = check ? 42 : 54; | 44 var value = check ? 42 : 54; |
47 var a = new List(value); | 45 var a = new List(value); |
48 var sum = 0; | 46 var sum = 0; |
49 for (int i = 0; i < value; i++) { | 47 for (int i = 0; i < value; i++) { |
50 sum += a[i]; | 48 sum += a[i]; |
51 } | 49 } |
52 return sum; | 50 return sum; |
53 } | 51 } |
54 """, | 52 """, |
55 REMOVED, | 53 REMOVED, |
56 | 54 """ |
57 """ | |
58 main() { | 55 main() { |
59 var a = new List(); | 56 var a = new List(); |
60 return a[0]; | 57 return a[0]; |
61 } | 58 } |
62 """, | 59 """, |
63 KEPT, | 60 KEPT, |
64 | 61 """ |
65 """ | |
66 main() { | 62 main() { |
67 var a = new List(); | 63 var a = new List(); |
68 return a.removeLast(); | 64 return a.removeLast(); |
69 } | 65 } |
70 """, | 66 """, |
71 KEPT, | 67 KEPT, |
72 | 68 """ |
73 """ | |
74 main() { | 69 main() { |
75 var a = new List(4); | 70 var a = new List(4); |
76 return a[0]; | 71 return a[0]; |
77 } | 72 } |
78 """, | 73 """, |
79 REMOVED, | 74 REMOVED, |
80 | 75 """ |
81 """ | |
82 main() { | 76 main() { |
83 var a = new List(4); | 77 var a = new List(4); |
84 return a.removeLast(); | 78 return a.removeLast(); |
85 } | 79 } |
86 """, | 80 """, |
87 REMOVED, | 81 REMOVED, |
88 | 82 """ |
89 """ | |
90 main(value) { | 83 main(value) { |
91 var a = new List(value); | 84 var a = new List(value); |
92 return a[value]; | 85 return a[value]; |
93 } | 86 } |
94 """, | 87 """, |
95 KEPT, | 88 KEPT, |
96 | 89 """ |
97 """ | |
98 main(value) { | 90 main(value) { |
99 var a = new List(1024); | 91 var a = new List(1024); |
100 return a[1023 & value]; | 92 return a[1023 & value]; |
101 } | 93 } |
102 """, | 94 """, |
103 REMOVED, | 95 REMOVED, |
104 | 96 """ |
105 """ | |
106 main(value) { | 97 main(value) { |
107 var a = new List(1024); | 98 var a = new List(1024); |
108 return a[1024 & value]; | 99 return a[1024 & value]; |
109 } | 100 } |
110 """, | 101 """, |
111 ABOVE_ZERO, | 102 ABOVE_ZERO, |
112 | 103 """ |
113 """ | |
114 main(value) { | 104 main(value) { |
115 var a = new List(); | 105 var a = new List(); |
116 return a[1]; | 106 return a[1]; |
117 } | 107 } |
118 """, | 108 """, |
119 ABOVE_ZERO, | 109 ABOVE_ZERO, |
120 | 110 """ |
121 """ | |
122 main(value, call) { | 111 main(value, call) { |
123 var a = new List(); | 112 var a = new List(); |
124 return a[value] + call() + a[value]; | 113 return a[value] + call() + a[value]; |
125 } | 114 } |
126 """, | 115 """, |
127 ONE_ZERO_CHECK, | 116 ONE_ZERO_CHECK, |
128 | 117 """ |
129 """ | |
130 main(value) { | 118 main(value) { |
131 var a = new List(); | 119 var a = new List(); |
132 return a[1] + a[0]; | 120 return a[1] + a[0]; |
133 } | 121 } |
134 """, | 122 """, |
135 ONE_CHECK, | 123 ONE_CHECK, |
136 | 124 """ |
137 """ | |
138 main() { | 125 main() { |
139 var a = new List(); | 126 var a = new List(); |
140 var sum = 0; | 127 var sum = 0; |
141 for (int i = 0; i <= a.length - 1; i++) { | 128 for (int i = 0; i <= a.length - 1; i++) { |
142 sum += a[i]; | 129 sum += a[i]; |
143 } | 130 } |
144 return sum; | 131 return sum; |
145 } | 132 } |
146 """, | 133 """, |
147 REMOVED, | 134 REMOVED, |
148 | 135 """ |
149 """ | |
150 main() { | 136 main() { |
151 var a = new List(); | 137 var a = new List(); |
152 var sum = 0; | 138 var sum = 0; |
153 for (int i = a.length - 1; i >=0; i--) { | 139 for (int i = a.length - 1; i >=0; i--) { |
154 sum += a[i]; | 140 sum += a[i]; |
155 } | 141 } |
156 return sum; | 142 return sum; |
157 } | 143 } |
158 """, | 144 """, |
159 REMOVED, | 145 REMOVED, |
160 | 146 """ |
161 """ | |
162 main(value) { | 147 main(value) { |
163 value = value is int ? value as int : 42; | 148 value = value is int ? value as int : 42; |
164 int sum = ~value; | 149 int sum = ~value; |
165 for (int i = 0; i < 42; i++) sum += (value & 4); | 150 for (int i = 0; i < 42; i++) sum += (value & 4); |
166 var a = new List(); | 151 var a = new List(); |
167 if (value > a.length - 1) return; | 152 if (value > a.length - 1) return; |
168 if (value < 0) return; | 153 if (value < 0) return; |
169 return a[value]; | 154 return a[value]; |
170 } | 155 } |
171 """, | 156 """, |
172 REMOVED, | 157 REMOVED, |
173 | 158 """ |
174 """ | |
175 main(value) { | 159 main(value) { |
176 value = value is int ? value as int : 42; | 160 value = value is int ? value as int : 42; |
177 int sum = ~value; | 161 int sum = ~value; |
178 for (int i = 0; i < 42; i++) sum += (value & 4); | 162 for (int i = 0; i < 42; i++) sum += (value & 4); |
179 var a = new List(); | 163 var a = new List(); |
180 if (value <= a.length - 1) { | 164 if (value <= a.length - 1) { |
181 if (value >= 0) { | 165 if (value >= 0) { |
182 return a[value]; | 166 return a[value]; |
183 } | 167 } |
184 } | 168 } |
185 } | 169 } |
186 """, | 170 """, |
187 REMOVED, | 171 REMOVED, |
188 """ | 172 """ |
189 main(value) { | 173 main(value) { |
190 value = value is int ? value as int : 42; | 174 value = value is int ? value as int : 42; |
191 int sum = ~value; | 175 int sum = ~value; |
192 for (int i = 0; i < 42; i++) sum += (value & 4); | 176 for (int i = 0; i < 42; i++) sum += (value & 4); |
193 var a = new List(); | 177 var a = new List(); |
194 if (value >= a.length) return; | 178 if (value >= a.length) return; |
195 if (value <= -1) return; | 179 if (value <= -1) return; |
196 return a[value]; | 180 return a[value]; |
197 } | 181 } |
198 """, | 182 """, |
199 REMOVED, | 183 REMOVED, |
200 """ | 184 """ |
201 main(value) { | 185 main(value) { |
202 var a = new List(4); | 186 var a = new List(4); |
203 var sum = 0; | 187 var sum = 0; |
204 for (int i = 0; i < a.length; i++) { | 188 for (int i = 0; i < a.length; i++) { |
205 sum += a[i]; | 189 sum += a[i]; |
206 if (sum == 0) i++; | 190 if (sum == 0) i++; |
207 } | 191 } |
208 return sum; | 192 return sum; |
209 } | 193 } |
210 """, | 194 """, |
211 REMOVED, | 195 REMOVED, |
212 """ | 196 """ |
213 main(value) { | 197 main(value) { |
214 var a = new List(5); | 198 var a = new List(5); |
215 var sum = 0; | 199 var sum = 0; |
216 for (int i = a.length - 1; i >= 0; i--) { | 200 for (int i = a.length - 1; i >= 0; i--) { |
217 sum += a[i]; | 201 sum += a[i]; |
218 if (sum == 0) i--; | 202 if (sum == 0) i--; |
219 } | 203 } |
220 return sum; | 204 return sum; |
221 } | 205 } |
222 """, | 206 """, |
223 REMOVED, | 207 REMOVED, |
224 """ | 208 """ |
225 main(value) { | 209 main(value) { |
226 var a = new List(6); | 210 var a = new List(6); |
227 var sum = 0; | 211 var sum = 0; |
228 for (int i = 0; i < a.length; i++) { | 212 for (int i = 0; i < a.length; i++) { |
229 sum += a[i]; | 213 sum += a[i]; |
230 if (sum == 0) i--; | 214 if (sum == 0) i--; |
231 } | 215 } |
232 return sum; | 216 return sum; |
233 } | 217 } |
234 """, | 218 """, |
235 BELOW_ZERO_CHECK, | 219 BELOW_ZERO_CHECK, |
236 """ | 220 """ |
237 main(value) { | 221 main(value) { |
238 var a = new List(7); | 222 var a = new List(7); |
239 var sum = 0; | 223 var sum = 0; |
240 for (int i = 0; i < a.length;) { | 224 for (int i = 0; i < a.length;) { |
241 sum += a[i]; | 225 sum += a[i]; |
242 sum == 0 ? i-- : i++; | 226 sum == 0 ? i-- : i++; |
243 } | 227 } |
244 return sum; | 228 return sum; |
245 } | 229 } |
246 """, | 230 """, |
247 BELOW_ZERO_CHECK, | 231 BELOW_ZERO_CHECK, |
248 """ | 232 """ |
249 main(value) { | 233 main(value) { |
250 var a = new List(7); | 234 var a = new List(7); |
251 var sum = 0; | 235 var sum = 0; |
252 for (int i = -2; i < a.length; i = 0) { | 236 for (int i = -2; i < a.length; i = 0) { |
253 sum += a[i]; | 237 sum += a[i]; |
254 } | 238 } |
255 return sum; | 239 return sum; |
256 } | 240 } |
257 """, | 241 """, |
258 BELOW_ZERO_CHECK, | 242 BELOW_ZERO_CHECK, |
259 ]; | 243 ]; |
260 | 244 |
261 // TODO(ahe): It would probably be better if this test used the real | 245 // TODO(ahe): It would probably be better if this test used the real |
262 // core library sources, as its purpose is to detect failure to | 246 // core library sources, as its purpose is to detect failure to |
263 // optimize fixed-sized arrays. | 247 // optimize fixed-sized arrays. |
264 Future expect(String code, int kind) { | 248 Future expect(String code, int kind) { |
265 return compile( | 249 return compile(code, check: (String generated) { |
266 code, | |
267 check: (String generated) { | |
268 switch (kind) { | 250 switch (kind) { |
269 case REMOVED: | 251 case REMOVED: |
270 Expect.isTrue(!generated.contains('ioore')); | 252 Expect.isTrue(!generated.contains('ioore')); |
271 break; | 253 break; |
272 | 254 |
273 case ABOVE_ZERO: | 255 case ABOVE_ZERO: |
274 Expect.isTrue(!generated.contains('< 0')); | 256 Expect.isTrue(!generated.contains('< 0')); |
275 Expect.isTrue(generated.contains('ioore')); | 257 Expect.isTrue(generated.contains('ioore')); |
276 break; | 258 break; |
277 | 259 |
(...skipping 20 matching lines...) Expand all Loading... |
298 | 280 |
299 case ONE_ZERO_CHECK: | 281 case ONE_ZERO_CHECK: |
300 RegExp regexp = new RegExp('< 0|>>> 0 !=='); | 282 RegExp regexp = new RegExp('< 0|>>> 0 !=='); |
301 Iterator matches = regexp.allMatches(generated).iterator; | 283 Iterator matches = regexp.allMatches(generated).iterator; |
302 checkNumberOfMatches(matches, 1); | 284 checkNumberOfMatches(matches, 1); |
303 break; | 285 break; |
304 } | 286 } |
305 }); | 287 }); |
306 } | 288 } |
307 | 289 |
308 | |
309 main() { | 290 main() { |
310 int i = 0; | 291 int i = 0; |
311 Future testNext() { | 292 Future testNext() { |
312 return expect(TESTS[i], TESTS[i + 1]).then((_) { | 293 return expect(TESTS[i], TESTS[i + 1]).then((_) { |
313 i += 2; | 294 i += 2; |
314 if (i < TESTS.length) return testNext(); | 295 if (i < TESTS.length) return testNext(); |
315 }); | 296 }); |
316 } | 297 } |
317 | 298 |
318 asyncTest(() => testNext()); | 299 asyncTest(() => testNext()); |
319 } | 300 } |
OLD | NEW |