| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, 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 'dart:async'; |
| 6 import 'package:expect/expect.dart'; |
| 7 import 'package:async_helper/async_helper.dart'; |
| 8 import 'compiler_helper.dart'; |
| 9 |
| 10 const String MOD1 = r""" |
| 11 foo(param) { |
| 12 var a = param ? 0xFFFFFFFF : 1; |
| 13 return a % 2; |
| 14 // Unoptimized. |
| 15 // present: '$mod' |
| 16 // absent: '%' |
| 17 } |
| 18 """; |
| 19 |
| 20 const String MOD2 = r""" |
| 21 foo(param) { |
| 22 var a = param ? 0xFFFFFFFF : 1; |
| 23 return (a + 1) % 2; |
| 24 // 'a + 1' cannot be -0.0, so we can optimize. |
| 25 // present: ' % 2' |
| 26 // absent: '$mod' |
| 27 } |
| 28 """; |
| 29 |
| 30 const String REM1 = r""" |
| 31 foo(param) { |
| 32 var a = param ? 0xFFFFFFFF : 1; |
| 33 return a.remainder(2); |
| 34 // Above can be compiled to '%'. |
| 35 // present: ' % 2' |
| 36 // absent: 'remainder' |
| 37 } |
| 38 """; |
| 39 |
| 40 const String REM2 = r""" |
| 41 foo(param) { |
| 42 var a = param ? 123.4 : -1; |
| 43 return a.remainder(3); |
| 44 // Above can be compiled to '%'. |
| 45 // present: ' % 3' |
| 46 // absent: 'remainder' |
| 47 } |
| 48 """; |
| 49 |
| 50 const String REM3 = r""" |
| 51 foo(param) { |
| 52 var a = param ? 123 : null; |
| 53 return 100.remainder(a); |
| 54 // No specialization for possibly null inputs. |
| 55 // present: 'remainder' |
| 56 // absent: '%' |
| 57 } |
| 58 """; |
| 59 |
| 60 main() { |
| 61 RegExp directivePattern = new RegExp( |
| 62 // \1 \2 \3 |
| 63 r'''// *(present|absent): (?:"([^"]*)"|'([^'']*)')''', |
| 64 multiLine: true); |
| 65 |
| 66 Future check(String test) { |
| 67 return compile(test, entry: 'foo', check: (String generated) { |
| 68 for (Match match in directivePattern.allMatches(test)) { |
| 69 String directive = match.group(1); |
| 70 String pattern = match.groups([2, 3]).where((s) => s != null).single; |
| 71 if (directive == 'present') { |
| 72 Expect.isTrue(generated.contains(pattern), |
| 73 "Cannot find '$pattern' in:\n$generated"); |
| 74 } else { |
| 75 assert(directive == 'absent'); |
| 76 Expect.isFalse(generated.contains(pattern), |
| 77 "Must not find '$pattern' in:\n$generated"); |
| 78 } |
| 79 } |
| 80 }); |
| 81 } |
| 82 |
| 83 asyncTest(() => Future.wait([ |
| 84 check(MOD1), |
| 85 check(MOD2), |
| 86 check(REM1), |
| 87 check(REM2), |
| 88 check(REM3), |
| 89 ])); |
| 90 } |
| OLD | NEW |