| // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| #import("compiler_helper.dart"); |
| |
| const String TEST_ONE = r""" |
| sum(param0, param1) { |
| var sum = 0; |
| for (var i = param0; i < param1; i += 1) sum = sum + i; |
| return sum; |
| } |
| """; |
| |
| const String TEST_TWO = r""" |
| foo(int param0) { |
| return -param0; |
| } |
| """; |
| |
| const String TEST_TWO_WITH_BAILOUT = r""" |
| foo(int param0) { |
| for (int i = 0; i < 1; i++) { |
| param0 = -param0; |
| } |
| return param0; |
| } |
| """; |
| |
| const String TEST_THREE = r""" |
| foo(c) { |
| for (int i = 0; i < 10; i++) print(c[i]); |
| } |
| """; |
| |
| const String TEST_FOUR = r""" |
| foo(String c) { |
| print(c[0]); // Force a type guard. |
| while (true) print(c.length); |
| } |
| """; |
| |
| const String TEST_FIVE = r""" |
| foo(a) { |
| a[0] = 1; |
| print(a[1]); |
| } |
| """; |
| |
| const String TEST_FIVE_WITH_BAILOUT = r""" |
| foo(a) { |
| for (int i = 0; i < 1; i++) { |
| a[0] = 1; |
| print(a[1]); |
| } |
| } |
| """; |
| |
| const String TEST_SIX = r""" |
| foo(a) { |
| print(a[0]); |
| while (true) { |
| a[0] = a[1]; |
| } |
| } |
| """; |
| |
| main() { |
| String generated = compile(TEST_ONE, 'sum'); |
| Expect.isTrue(generated.contains('sum += i')); |
| Expect.isTrue(generated.contains("typeof param1 !== 'number'")); |
| |
| generated = compile(TEST_TWO, 'foo'); |
| RegExp regexp = new RegExp(getNumberTypeCheck('param0')); |
| Expect.isTrue(!regexp.hasMatch(generated)); |
| |
| regexp = const RegExp('-param0'); |
| Expect.isTrue(!regexp.hasMatch(generated)); |
| |
| generated = compile(TEST_TWO_WITH_BAILOUT, 'foo'); |
| regexp = new RegExp(getNumberTypeCheck('param0')); |
| Expect.isTrue(regexp.hasMatch(generated)); |
| |
| regexp = const RegExp('-param0'); |
| Expect.isTrue(regexp.hasMatch(generated)); |
| |
| generated = compile(TEST_THREE, 'foo'); |
| regexp = new RegExp("c[$anyIdentifier]"); |
| Expect.isTrue(regexp.hasMatch(generated)); |
| |
| generated = compile(TEST_FOUR, 'foo'); |
| regexp = new RegExp("c.length"); |
| Expect.isTrue(regexp.hasMatch(generated)); |
| |
| generated = compile(TEST_FIVE, 'foo'); |
| regexp = const RegExp('a.constructor !== Array'); |
| Expect.isTrue(!regexp.hasMatch(generated)); |
| Expect.isTrue(generated.contains('index')); |
| Expect.isTrue(generated.contains('indexSet')); |
| |
| generated = compile(TEST_FIVE_WITH_BAILOUT, 'foo'); |
| regexp = const RegExp('a.constructor !== Array'); |
| Expect.isTrue(regexp.hasMatch(generated)); |
| Expect.isTrue(!generated.contains('index')); |
| Expect.isTrue(!generated.contains('indexSet')); |
| |
| generated = compile(TEST_SIX, 'foo'); |
| regexp = const RegExp('a.constructor !== Array'); |
| Expect.isTrue(regexp.hasMatch(generated)); |
| Expect.isTrue(!generated.contains('index')); |
| Expect.isTrue(!generated.contains('indexSet')); |
| } |