| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
| 6 | 6 |
| 7 // Test that optimized codeUnitAt and slow path codeUnitAt produce the same | 7 // Test that optimized codeUnitAt and slow path codeUnitAt produce the same |
| 8 // error. | 8 // error. |
| 9 | 9 |
| 10 @NoInline() | 10 @NoInline() |
| 11 @AssumeDynamic() | 11 @AssumeDynamic() |
| 12 confuse(x) => x; | 12 confuse(x) => x; |
| 13 | 13 |
| 14 void check2(String name, name1, f1, name2, f2) { | 14 void check2(String name, name1, f1, name2, f2) { |
| 15 Error trap(part, f) { | 15 Error trap(part, f) { |
| 16 try { | 16 try { |
| 17 f(); | 17 f(); |
| 18 } catch (e) { | 18 } catch (e) { |
| 19 return e; | 19 return e; |
| 20 } | 20 } |
| 21 Expect.fail('should throw: $name.$part'); | 21 Expect.fail('should throw: $name.$part'); |
| 22 } | 22 } |
| 23 |
| 23 var e1 = trap(name1, f1); | 24 var e1 = trap(name1, f1); |
| 24 var e2 = trap(name2, f2); | 25 var e2 = trap(name2, f2); |
| 25 var s1 = '$e1'; | 26 var s1 = '$e1'; |
| 26 var s2 = '$e2'; | 27 var s2 = '$e2'; |
| 27 Expect.equals(s1, s2, '\n $name.$name1: "$s1"\n $name.$name2: "$s2"\n'); | 28 Expect.equals(s1, s2, '\n $name.$name1: "$s1"\n $name.$name2: "$s2"\n'); |
| 28 } | 29 } |
| 29 | 30 |
| 30 void check(String name, f1, f2, [f3, f4]) { | 31 void check(String name, f1, f2, [f3, f4]) { |
| 31 check2(name, 'f1', f1, 'f2', f2); | 32 check2(name, 'f1', f1, 'f2', f2); |
| 32 if (f3 != null) check2(name, 'f1', f1, 'f3', f3); | 33 if (f3 != null) check2(name, 'f1', f1, 'f3', f3); |
| 33 if (f4 != null) check2(name, 'f1', f1, 'f4', f4); | 34 if (f4 != null) check2(name, 'f1', f1, 'f4', f4); |
| 34 } | 35 } |
| 35 | 36 |
| 36 | |
| 37 class TooHigh { | 37 class TooHigh { |
| 38 static f1() { | 38 static f1() { |
| 39 return confuse('AB').codeUnitAt(3); // dynamic receiver. | 39 return confuse('AB').codeUnitAt(3); // dynamic receiver. |
| 40 } | 40 } |
| 41 | 41 |
| 42 static f2() { | 42 static f2() { |
| 43 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. | 43 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. |
| 44 var i = confuse(3); | 44 var i = confuse(3); |
| 45 return a.codeUnitAt(i); | 45 return a.codeUnitAt(i); |
| 46 } | 46 } |
| 47 | 47 |
| 48 static f3() { | 48 static f3() { |
| 49 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. | 49 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. |
| 50 return a.codeUnitAt(3); | 50 return a.codeUnitAt(3); |
| 51 } | 51 } |
| 52 | 52 |
| 53 static test() { | 53 static test() { |
| 54 check('TooHigh', f1, f2, f3); | 54 check('TooHigh', f1, f2, f3); |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 | 57 |
| 58 class Negative { | 58 class Negative { |
| 59 static f1() { | 59 static f1() { |
| 60 return confuse('AB').codeUnitAt(-3); // dynamic receiver. | 60 return confuse('AB').codeUnitAt(-3); // dynamic receiver. |
| 61 } | 61 } |
| 62 | 62 |
| 63 static f2() { | 63 static f2() { |
| 64 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. | 64 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. |
| 65 var i = confuse(-3); | 65 var i = confuse(-3); |
| 66 return a.codeUnitAt(i); | 66 return a.codeUnitAt(i); |
| 67 } | 67 } |
| 68 | 68 |
| 69 static f3() { | 69 static f3() { |
| 70 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. | 70 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. |
| 71 var i = confuse(true) ? -3 : 0; | 71 var i = confuse(true) ? -3 : 0; |
| 72 return a.codeUnitAt(i); | 72 return a.codeUnitAt(i); |
| 73 } | 73 } |
| 74 | 74 |
| 75 static f4() { | 75 static f4() { |
| 76 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. | 76 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. |
| 77 return a.codeUnitAt(-3); | 77 return a.codeUnitAt(-3); |
| 78 } | 78 } |
| 79 | 79 |
| 80 static test() { | 80 static test() { |
| 81 check('Negative', f1, f2, f3, f4); | 81 check('Negative', f1, f2, f3, f4); |
| 82 } | 82 } |
| 83 } | 83 } |
| 84 | 84 |
| 85 class Empty { | 85 class Empty { |
| 86 static f1() { | 86 static f1() { |
| 87 return confuse('').codeUnitAt(0); // dynamic receiver. | 87 return confuse('').codeUnitAt(0); // dynamic receiver. |
| 88 } | 88 } |
| 89 | 89 |
| 90 static f2() { | 90 static f2() { |
| 91 var a = confuse(true) ? '' : 'ABCDE'; // Empty String with unknown length. | 91 var a = confuse(true) ? '' : 'ABCDE'; // Empty String with unknown length. |
| 92 var i = confuse(true) ? 0 : 1; | 92 var i = confuse(true) ? 0 : 1; |
| 93 return a.codeUnitAt(i); | 93 return a.codeUnitAt(i); |
| 94 } | 94 } |
| 95 | 95 |
| 96 static f3() { | 96 static f3() { |
| 97 var a = confuse(true) ? '' : 'ABCDE'; // Empty String with unknown length. | 97 var a = confuse(true) ? '' : 'ABCDE'; // Empty String with unknown length. |
| 98 return a.codeUnitAt(0); | 98 return a.codeUnitAt(0); |
| 99 } | 99 } |
| 100 | 100 |
| 101 static test() { | 101 static test() { |
| 102 check('Empty', f1, f2, f3); | 102 check('Empty', f1, f2, f3); |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 | 105 |
| 106 class BadType { | 106 class BadType { |
| 107 static f1() { | 107 static f1() { |
| 108 return confuse('AB').codeUnitAt('a'); // dynamic receiver. | 108 return confuse('AB').codeUnitAt('a'); // dynamic receiver. |
| 109 } | 109 } |
| 110 | 110 |
| 111 static f2() { | 111 static f2() { |
| 112 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. | 112 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. |
| 113 var i = confuse('a'); | 113 var i = confuse('a'); |
| 114 return a.codeUnitAt(i); | 114 return a.codeUnitAt(i); |
| 115 } | 115 } |
| 116 | 116 |
| 117 static f3() { | 117 static f3() { |
| 118 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. | 118 var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length. |
| 119 return a.codeUnitAt('a'); | 119 return a.codeUnitAt('a'); |
| 120 } | 120 } |
| 121 | 121 |
| 122 static test() { | 122 static test() { |
| 123 check('BadType', f1, f2, f3); | 123 check('BadType', f1, f2, f3); |
| 124 } | 124 } |
| 125 } | 125 } |
| 126 | 126 |
| 127 main() { | 127 main() { |
| 128 TooHigh.test(); | 128 TooHigh.test(); |
| 129 Negative.test(); | 129 Negative.test(); |
| 130 Empty.test(); | 130 Empty.test(); |
| 131 BadType.test(); | 131 BadType.test(); |
| 132 } | 132 } |
| OLD | NEW |