mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 15:21:54 +00:00
912005267d
Change-Id: I46be49b2effec3e38a3dc44cd45cfe736f77fa78 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/182680 Commit-Queue: Sigmund Cherem <sigmund@google.com> Reviewed-by: Joshua Litt <joshualitt@google.com> Reviewed-by: Nicholas Shahan <nshahan@google.com> Reviewed-by: Stephen Adams <sra@google.com>
135 lines
2.9 KiB
Dart
135 lines
2.9 KiB
Dart
// Copyright (c) 2015, 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.
|
|
|
|
// @dart = 2.7
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
// Test that optimized codeUnitAt and slow path codeUnitAt produce the same
|
|
// error.
|
|
|
|
@pragma('dart2js:noInline')
|
|
@pragma('dart2js:assumeDynamic')
|
|
confuse(x) => x;
|
|
|
|
void check2(String name, name1, f1, name2, f2) {
|
|
Error trap(part, f) {
|
|
try {
|
|
f();
|
|
} catch (e) {
|
|
return e;
|
|
}
|
|
Expect.fail('should throw: $name.$part');
|
|
}
|
|
|
|
var e1 = trap(name1, f1);
|
|
var e2 = trap(name2, f2);
|
|
var s1 = '$e1';
|
|
var s2 = '$e2';
|
|
Expect.equals(s1, s2, '\n $name.$name1: "$s1"\n $name.$name2: "$s2"\n');
|
|
}
|
|
|
|
void check(String name, f1, f2, [f3, f4]) {
|
|
check2(name, 'f1', f1, 'f2', f2);
|
|
if (f3 != null) check2(name, 'f1', f1, 'f3', f3);
|
|
if (f4 != null) check2(name, 'f1', f1, 'f4', f4);
|
|
}
|
|
|
|
class TooHigh {
|
|
static f1() {
|
|
return confuse('AB').codeUnitAt(3); // dynamic receiver.
|
|
}
|
|
|
|
static f2() {
|
|
var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length.
|
|
var i = confuse(3);
|
|
return a.codeUnitAt(i);
|
|
}
|
|
|
|
static f3() {
|
|
var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length.
|
|
return a.codeUnitAt(3);
|
|
}
|
|
|
|
static test() {
|
|
check('TooHigh', f1, f2, f3);
|
|
}
|
|
}
|
|
|
|
class Negative {
|
|
static f1() {
|
|
return confuse('AB').codeUnitAt(-3); // dynamic receiver.
|
|
}
|
|
|
|
static f2() {
|
|
var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length.
|
|
var i = confuse(-3);
|
|
return a.codeUnitAt(i);
|
|
}
|
|
|
|
static f3() {
|
|
var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length.
|
|
var i = confuse(true) ? -3 : 0;
|
|
return a.codeUnitAt(i);
|
|
}
|
|
|
|
static f4() {
|
|
var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length.
|
|
return a.codeUnitAt(-3);
|
|
}
|
|
|
|
static test() {
|
|
check('Negative', f1, f2, f3, f4);
|
|
}
|
|
}
|
|
|
|
class Empty {
|
|
static f1() {
|
|
return confuse('').codeUnitAt(0); // dynamic receiver.
|
|
}
|
|
|
|
static f2() {
|
|
var a = confuse(true) ? '' : 'ABCDE'; // Empty String with unknown length.
|
|
var i = confuse(true) ? 0 : 1;
|
|
return a.codeUnitAt(i);
|
|
}
|
|
|
|
static f3() {
|
|
var a = confuse(true) ? '' : 'ABCDE'; // Empty String with unknown length.
|
|
return a.codeUnitAt(0);
|
|
}
|
|
|
|
static test() {
|
|
check('Empty', f1, f2, f3);
|
|
}
|
|
}
|
|
|
|
class BadType {
|
|
static f1() {
|
|
return confuse('AB').codeUnitAt('a'); // dynamic receiver.
|
|
}
|
|
|
|
static f2() {
|
|
var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length.
|
|
var i = confuse('a');
|
|
return a.codeUnitAt(i);
|
|
}
|
|
|
|
static f3() {
|
|
var a = confuse(true) ? 'AB' : 'ABCDE'; // String with unknown length.
|
|
return a.codeUnitAt(('a' as dynamic));
|
|
}
|
|
|
|
static test() {
|
|
check('BadType', f1, f2, f3);
|
|
}
|
|
}
|
|
|
|
main() {
|
|
TooHigh.test();
|
|
Negative.test();
|
|
Empty.test();
|
|
BadType.test();
|
|
}
|