Initial checkin.
git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@22 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
# 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.
|
||||
|
||||
prefix corelib
|
||||
|
||||
[ $arch == ia32 || $arch == dartium ]
|
||||
UnicodeTest: Fail # Bug 5163868
|
||||
|
||||
|
||||
[ $arch == ia32 ]
|
||||
|
||||
|
||||
[ $arch == dartc || $arch == chromium ]
|
||||
ConstListLiteralTest: Fail # Bug 3341367
|
||||
CoreRuntimeTypesTest: Fail # Bug 5196164
|
||||
StringTest: Fail # Bug 5196164
|
||||
|
||||
|
||||
[ $arch == chromium || $arch == dartium ]
|
||||
# Bug 5293748
|
||||
SortTest: Skip
|
||||
ListSortTest: Skip
|
||||
|
||||
[ $arch == chromium ]
|
||||
# Bug 5275717
|
||||
MapTest: Fail
|
||||
SplayTreeTest: Fail
|
||||
QueueTest: Fail
|
||||
ExceptionImplementationTest: Fail
|
||||
|
||||
|
||||
[ $arch == x64 ]
|
||||
*: Skip
|
||||
|
||||
[ $arch == simarm ]
|
||||
*: Skip
|
||||
|
||||
[ $arch == arm ]
|
||||
*: Skip
|
||||
@@ -0,0 +1,36 @@
|
||||
// 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.
|
||||
|
||||
class CollectionFromTest {
|
||||
static testMain() {
|
||||
var set = new Set<int>();
|
||||
set.add(1);
|
||||
set.add(2);
|
||||
set.add(4);
|
||||
check(set, new List<int>.from(set));
|
||||
check(set, new List.from(set));
|
||||
check(set, new Queue<int>.from(set));
|
||||
check(set, new Queue.from(set));
|
||||
check(set, new Set<int>.from(set));
|
||||
check(set, new Set.from(set));
|
||||
}
|
||||
|
||||
|
||||
static check(Collection initial, Collection other) {
|
||||
Expect.equals(3, initial.length);
|
||||
Expect.equals(initial.length, other.length);
|
||||
|
||||
int initialSum = 0;
|
||||
int otherSum = 0;
|
||||
|
||||
initial.forEach(void f(e) { initialSum += e; });
|
||||
other.forEach(void f(e) { otherSum += e; });
|
||||
Expect.equals(4 + 2 + 1, otherSum);
|
||||
Expect.equals(otherSum, initialSum);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
CollectionFromTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// 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.
|
||||
|
||||
// Test that a final list literal is not expandable nor modifiable.
|
||||
|
||||
class ConstListLiteralTest {
|
||||
|
||||
static void testMain() {
|
||||
var list = const [4, 2, 3];
|
||||
Expect.equals(3, list.length);
|
||||
|
||||
var exception = null;
|
||||
try {
|
||||
list.add(4);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
Expect.equals(3, list.length);
|
||||
exception = null;
|
||||
|
||||
exception = null;
|
||||
try {
|
||||
list.addAll([4, 5]);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
Expect.equals(3, list.length);
|
||||
|
||||
exception = null;
|
||||
try {
|
||||
list[0] = 0;
|
||||
} catch (UnsupportedOperationException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
Expect.equals(3, list.length);
|
||||
|
||||
exception = null;
|
||||
try {
|
||||
list.sort((a, b) => a < b);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
Expect.equals(3, list.length);
|
||||
Expect.equals(4, list[0]);
|
||||
Expect.equals(2, list[1]);
|
||||
Expect.equals(3, list[2]);
|
||||
|
||||
exception = null;
|
||||
try {
|
||||
list.copyFrom([1], 0, 0, 1);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
Expect.equals(3, list.length);
|
||||
Expect.equals(4, list[0]);
|
||||
Expect.equals(2, list[1]);
|
||||
Expect.equals(3, list[2]);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ConstListLiteralTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
// 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.
|
||||
|
||||
/**
|
||||
* A test of simple runtime behavior on numbers, strings and lists with
|
||||
* a focus on both correct behavior and runtime errors.
|
||||
*
|
||||
* This file is written to use minimal type declarations to match a
|
||||
* typical dynamic language coding style.
|
||||
*/
|
||||
class CoreRuntimeTypesTest {
|
||||
static testMain() {
|
||||
testBooleanOperators();
|
||||
testRationalOperators();
|
||||
testIntegerOperators();
|
||||
testOperatorErrors();
|
||||
testRationalMethods();
|
||||
testIntegerMethods();
|
||||
testStringOperators();
|
||||
testStringMethods();
|
||||
testListOperators();
|
||||
testListMethods();
|
||||
testMapOperators();
|
||||
testMapMethods();
|
||||
testLiterals();
|
||||
|
||||
// TODO(ngeoffray): re-enable this test once static const fields
|
||||
// in interfaces are correctly emitted (b/4336711).
|
||||
// testDateMethods();
|
||||
}
|
||||
|
||||
static assertEquals(a, b) {
|
||||
Expect.equals(b, a);
|
||||
}
|
||||
|
||||
static assertListEquals(List a, List b) {
|
||||
Expect.equals(b.length, a.length);
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
Expect.equals(b[i], a[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static assertListContains(List<Comparable> a, List<Comparable> b) {
|
||||
a.sort((x, y) => x.compareTo(y));
|
||||
b.sort((x, y) => x.compareTo(y));
|
||||
assertListEquals(a, b);
|
||||
}
|
||||
|
||||
static assertTypeError(void f()) {
|
||||
try {
|
||||
f();
|
||||
} catch (var exception) {
|
||||
Expect.equals(true, (exception is TypeError) ||
|
||||
(exception is NoSuchMethodException) ||
|
||||
(exception is NullPointerException) ||
|
||||
(exception is IllegalArgumentException));
|
||||
return;
|
||||
}
|
||||
Expect.equals(true, false);
|
||||
}
|
||||
|
||||
static testBooleanOperators() {
|
||||
var x = true, y = false;
|
||||
assertEquals(x, true);
|
||||
assertEquals(y, false);
|
||||
assertEquals(x, !y);
|
||||
assertEquals(!x, y);
|
||||
}
|
||||
|
||||
|
||||
static testRationalOperators() {
|
||||
var x = 10, y = 20;
|
||||
assertEquals(x + y, 30);
|
||||
assertEquals(x - y, -10);
|
||||
assertEquals(x * y, 200);
|
||||
assertEquals(x / y, 0.5);
|
||||
assertEquals(x ~/ y, 0);
|
||||
assertEquals(x % y, 10);
|
||||
}
|
||||
|
||||
static testIntegerOperators() {
|
||||
var x = 18, y = 17;
|
||||
assertEquals(x | y, 19);
|
||||
assertEquals(x & y, 16);
|
||||
assertEquals(x ^ y, 3);
|
||||
assertEquals(2 >> 1, 1);
|
||||
assertEquals(1 << 1, 2);
|
||||
}
|
||||
|
||||
static testOperatorErrors() {
|
||||
var objs = [1, '2', [3], null, true, new Map()];
|
||||
for (var i=0; i < objs.length; i++) {
|
||||
for (var j=i+1; j < objs.length; j++) {
|
||||
testBinaryOperatorErrors(objs[i], objs[j]);
|
||||
testBinaryOperatorErrors(objs[j], objs[i]);
|
||||
}
|
||||
if (objs[i] != 1) {
|
||||
testUnaryOperatorErrors(objs[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static testBinaryOperatorErrors(x, y) {
|
||||
assertTypeError(() { x - y;});
|
||||
assertTypeError(() { x * y;});
|
||||
assertTypeError(() { x / y;});
|
||||
assertTypeError(() { x | y;});
|
||||
assertTypeError(() { x ^ y;});
|
||||
assertTypeError(() { x & y;});
|
||||
assertTypeError(() { x << y;});
|
||||
assertTypeError(() { x >> y;});
|
||||
assertTypeError(() { x >>> y;});
|
||||
assertTypeError(() { x ~/ y;});
|
||||
assertTypeError(() { x % y;});
|
||||
|
||||
testComparisonOperatorErrors(x, y);
|
||||
}
|
||||
|
||||
static testComparisonOperatorErrors(x, y) {
|
||||
assertEquals(x == y, false);
|
||||
assertEquals(x != y, true);
|
||||
assertTypeError(() { x < y; });
|
||||
assertTypeError(() { x <= y; });
|
||||
assertTypeError(() { x > y; });
|
||||
assertTypeError(() { x >= y; });
|
||||
}
|
||||
|
||||
static testUnaryOperatorErrors(x) {
|
||||
// TODO(jimhug): Add guard for 'is num' when 'is' is working
|
||||
assertTypeError(() { ~x;});
|
||||
assertTypeError(() { -x;});
|
||||
// TODO(jimhug): Add check for !x as an error when x is not a bool
|
||||
}
|
||||
|
||||
static testRationalMethods() {
|
||||
var x = 10.6;
|
||||
assertEquals(x.abs(), 10.6);
|
||||
assertEquals((-x).abs(), 10.6);
|
||||
assertEquals(x.round(), 11);
|
||||
assertEquals(x.floor(), 10);
|
||||
assertEquals(x.ceil(), 11);
|
||||
}
|
||||
|
||||
// TODO(jimhug): Determine correct behavior for mixing ints and floats.
|
||||
static testIntegerMethods() {
|
||||
var y = 9;
|
||||
assertEquals(y.isEven(), false);
|
||||
assertEquals(y.isOdd(), true);
|
||||
assertEquals(y.toRadixString(2), '1001');
|
||||
assertEquals(y.toRadixString(3), '100');
|
||||
assertEquals(y.toRadixString(16), '9');
|
||||
assertEquals((0).toRadixString(16), '0');
|
||||
try {
|
||||
y.toRadixString(0);
|
||||
Expect.fail("Illegal radix 0 accepted.");
|
||||
} catch (var e) { }
|
||||
try {
|
||||
y.toRadixString(-1);
|
||||
Expect.fail("Illegal radix -1 accepted.");
|
||||
} catch (var e) { }
|
||||
}
|
||||
|
||||
static testStringOperators() {
|
||||
var s = "abcdef";
|
||||
assertEquals(s, "abcdef");
|
||||
assertEquals(s.charCodeAt(0), 97);
|
||||
assertEquals(s[0], 'a');
|
||||
assertEquals(s.length, 6);
|
||||
assertTypeError(() { s[null]; });
|
||||
assertTypeError(() { s['hello']; });
|
||||
assertTypeError(() { s[0] = 'x'; });
|
||||
}
|
||||
|
||||
// TODO(jimhug): Fill out full set of string methods.
|
||||
static testStringMethods() {
|
||||
var s = "abcdef";
|
||||
assertEquals(s.isEmpty(), false);
|
||||
assertEquals(s.startsWith("abc"), true);
|
||||
assertEquals(s.endsWith("def"), true);
|
||||
assertEquals(s.startsWith("aa"), false);
|
||||
assertEquals(s.endsWith("ff"), false);
|
||||
assertEquals(s.contains('cd', 0), true);
|
||||
assertEquals(s.contains('cd', 2), true);
|
||||
assertEquals(s.contains('cd', 3), false);
|
||||
assertEquals(s.indexOf('cd', 2), 2);
|
||||
assertEquals(s.indexOf('cd', 3), -1);
|
||||
|
||||
assertTypeError(() { s.startsWith(1); });
|
||||
assertTypeError(() { s.endsWith(1); });
|
||||
}
|
||||
|
||||
static testListOperators() {
|
||||
var a = [1,2,3,4];
|
||||
assertEquals(a[0], 1);
|
||||
assertTypeError(() { a['0']; });
|
||||
a[0] = 42;
|
||||
assertEquals(a[0], 42);
|
||||
assertTypeError(() { a['0'] = 99; });
|
||||
assertEquals(a.length, 4);
|
||||
}
|
||||
|
||||
// TODO(jimhug): Fill out full set of list methods.
|
||||
static testListMethods() {
|
||||
var a = [1,2,3,4];
|
||||
assertEquals(a.isEmpty(), false);
|
||||
assertEquals(a.length, 4);
|
||||
var exception = null;
|
||||
a.clear();
|
||||
assertEquals(a.length, 0);
|
||||
}
|
||||
|
||||
static testMapOperators() {
|
||||
var d = new Map();
|
||||
d['a'] = 1;
|
||||
d['b'] = 2;
|
||||
assertEquals(d['a'], 1);
|
||||
assertEquals(d['b'], 2);
|
||||
assertEquals(d['c'], null);
|
||||
}
|
||||
|
||||
static testMapMethods() {
|
||||
var d = new Map();
|
||||
d['a'] = 1;
|
||||
d['b'] = 2;
|
||||
assertEquals(d.containsValue(2), true);
|
||||
assertEquals(d.containsValue(3), false);
|
||||
assertEquals(d.containsKey('a'), true);
|
||||
assertEquals(d.containsKey('c'), false);
|
||||
assertEquals(d.getKeys().length, 2);
|
||||
assertEquals(d.getValues().length, 2);
|
||||
|
||||
assertEquals(d.remove('c'), null);
|
||||
assertEquals(d.remove('b'), 2);
|
||||
assertListEquals(d.getKeys(), ['a']);
|
||||
assertListEquals(d.getValues(), [1]);
|
||||
|
||||
d['c'] = 3;
|
||||
d['f'] = 4;
|
||||
assertEquals(d.getKeys().length, 3);
|
||||
assertEquals(d.getValues().length, 3);
|
||||
assertListContains(d.getKeys(), ['a', 'c', 'f']);
|
||||
assertListContains(d.getValues(), [1, 3, 4]);
|
||||
|
||||
var count = 0;
|
||||
d.forEach((key, value) {
|
||||
count++;
|
||||
assertEquals(value, d[key]);
|
||||
});
|
||||
assertEquals(count, 3);
|
||||
|
||||
d = { 'a': 1, 'b': 2 };
|
||||
assertEquals(d.containsValue(2), true);
|
||||
assertEquals(d.containsValue(3), false);
|
||||
assertEquals(d.containsKey('a'), true);
|
||||
assertEquals(d.containsKey('c'), false);
|
||||
assertEquals(d.getKeys().length, 2);
|
||||
assertEquals(d.getValues().length, 2);
|
||||
|
||||
d['g'] = null;
|
||||
assertEquals(d.containsKey('g'), true);
|
||||
assertEquals(d['g'], null);
|
||||
}
|
||||
|
||||
static testDateMethods() {
|
||||
// TODO(jimhug): Switch to named constructors when available.
|
||||
// Pushing this into Jan 2nd to make the year independent of timezone.
|
||||
// TODO(jimhug): Pursue a better solution to TZ issues.
|
||||
var msec = 115201000;
|
||||
var d = new DateTime(msec);
|
||||
assertEquals(d.getSeconds(), 1);
|
||||
assertEquals(d.getYear(), 1970);
|
||||
|
||||
d = new DateTime();
|
||||
assertEquals(d.getYear() >= 2011, true);
|
||||
}
|
||||
|
||||
static testLiterals() {
|
||||
true.toString();
|
||||
1.0.toString();
|
||||
.5.toString();
|
||||
1.toString();
|
||||
if (false) {
|
||||
// Depends on http://b/4198808.
|
||||
null.toString();
|
||||
}
|
||||
'${null}'.toString();
|
||||
'${true}'.toString();
|
||||
'${false}'.toString();
|
||||
''.toString();
|
||||
''.endsWith('');
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
CoreRuntimeTypesTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for DateTime.
|
||||
|
||||
class DateTimeTest {
|
||||
// Tests if the time moves eventually forward.
|
||||
static void testNow() {
|
||||
var t1 = new DateTime.now();
|
||||
bool timeMovedForward = false;
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
var t2 = new DateTime.now();
|
||||
if (t1.value < t2.value) {
|
||||
timeMovedForward = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Expect.equals(true, timeMovedForward);
|
||||
}
|
||||
|
||||
static void testValue() {
|
||||
var dt1 = new DateTime.now();
|
||||
var value = dt1.value;
|
||||
var dt2 = new DateTime.fromEpoch(value, new TimeZone.local());
|
||||
Expect.equals(value, dt2.value);
|
||||
}
|
||||
|
||||
static void testFarAwayDates() {
|
||||
DateTime dt =
|
||||
new DateTime.fromEpoch(1000000000000001, const TimeZone.utc());
|
||||
Expect.equals(33658, dt.year);
|
||||
Expect.equals(9, dt.month);
|
||||
Expect.equals(27, dt.day);
|
||||
Expect.equals(1, dt.hours);
|
||||
Expect.equals(46, dt.minutes);
|
||||
Expect.equals(40, dt.seconds);
|
||||
Expect.equals(1, dt.milliseconds);
|
||||
Date d = dt.date;
|
||||
Expect.equals(33658, d.year);
|
||||
Expect.equals(9, d.month);
|
||||
Expect.equals(27, d.day);
|
||||
Time t = dt.time;
|
||||
Expect.equals(1, t.hours);
|
||||
Expect.equals(46, t.minutes);
|
||||
Expect.equals(40, t.seconds);
|
||||
Expect.equals(1, t.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-1000000000000001, const TimeZone.utc());
|
||||
Expect.equals(-29719, dt.year);
|
||||
Expect.equals(4, dt.month);
|
||||
Expect.equals(5, dt.day);
|
||||
Expect.equals(22, dt.hours);
|
||||
Expect.equals(13, dt.minutes);
|
||||
Expect.equals(19, dt.seconds);
|
||||
Expect.equals(999, dt.milliseconds);
|
||||
d = dt.date;
|
||||
Expect.equals(-29719, d.year);
|
||||
Expect.equals(4, d.month);
|
||||
Expect.equals(5, d.day);
|
||||
t = dt.time;
|
||||
Expect.equals(22, t.hours);
|
||||
Expect.equals(13, t.minutes);
|
||||
Expect.equals(19, t.seconds);
|
||||
Expect.equals(999, t.milliseconds);
|
||||
// Same with local zone.
|
||||
dt = new DateTime.fromEpoch(1000000000000001, new TimeZone.local());
|
||||
Expect.equals(33658, dt.year);
|
||||
Expect.equals(9, dt.month);
|
||||
Expect.equals(true, dt.day == 27 || dt.day == 26);
|
||||
// Not much we can test for local hours.
|
||||
Expect.equals(true, dt.hours >= 0 && dt.hours < 24);
|
||||
// Timezones can have offsets down to 15 minutes.
|
||||
Expect.equals(true, dt.minutes % 15 == 46 % 15);
|
||||
Expect.equals(40, dt.seconds);
|
||||
Expect.equals(1, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-1000000000000001, new TimeZone.local());
|
||||
Expect.equals(-29719, dt.year);
|
||||
Expect.equals(4, dt.month);
|
||||
Expect.equals(true, 5 == dt.day || 6 == dt.day);
|
||||
// Not much we can test for local hours.
|
||||
Expect.equals(true, dt.hours >= 0 && dt.hours < 24);
|
||||
// Timezones can have offsets down to 15 minutes.
|
||||
Expect.equals(true, dt.minutes % 15 == 13);
|
||||
Expect.equals(19, dt.seconds);
|
||||
Expect.equals(999, dt.milliseconds);
|
||||
}
|
||||
|
||||
static void testEquivalentYears() {
|
||||
// All hardcoded values come from V8. This means that the values are not
|
||||
// necessarily correct (see limitations of Date object in
|
||||
// EcmaScript 15.9.1 and in particular 15.9.1.8/9).
|
||||
DateTime dt = new DateTime.fromEpoch(-31485600000, const TimeZone.utc());
|
||||
Expect.equals(1969, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(14, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-63108000000, const TimeZone.utc());
|
||||
Expect.equals(1968, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(14, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-94644000000, const TimeZone.utc());
|
||||
Expect.equals(1967, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(14, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-126180000000, const TimeZone.utc());
|
||||
Expect.equals(1966, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(14, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-157716000000, const TimeZone.utc());
|
||||
Expect.equals(1965, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(14, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-2177402400000, const TimeZone.utc());
|
||||
Expect.equals(1901, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(14, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-5333076000000, const TimeZone.utc());
|
||||
Expect.equals(1801, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(14, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-8520285600000, const TimeZone.utc());
|
||||
Expect.equals(1700, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(14, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-14831719200000, const TimeZone.utc());
|
||||
Expect.equals(1500, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(14, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-59011408800000, const TimeZone.utc());
|
||||
Expect.equals(100, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(14, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-62011408800000, const TimeZone.utc());
|
||||
Expect.equals(4, dt.year);
|
||||
Expect.equals(12, dt.month);
|
||||
Expect.equals(8, dt.day);
|
||||
Expect.equals(8, dt.hours);
|
||||
Expect.equals(40, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-64011408800000, const TimeZone.utc());
|
||||
Expect.equals(-59, dt.year);
|
||||
Expect.equals(7, dt.month);
|
||||
Expect.equals(24, dt.day);
|
||||
Expect.equals(5, dt.hours);
|
||||
Expect.equals(6, dt.minutes);
|
||||
Expect.equals(40, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
final int SECONDS_YEAR_2035 = 2051222400;
|
||||
dt = new DateTime.fromEpoch(SECONDS_YEAR_2035 * 1000 + 1,
|
||||
const TimeZone.utc());
|
||||
Expect.equals(2035, dt.year);
|
||||
Expect.equals(1, dt.month);
|
||||
Expect.equals(1, dt.day);
|
||||
Expect.equals(0, dt.hours);
|
||||
Expect.equals(0, dt.minutes);
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(1, dt.milliseconds);
|
||||
dt = new DateTime.fromEpoch(SECONDS_YEAR_2035 * 1000 - 1,
|
||||
const TimeZone.utc());
|
||||
Expect.equals(2034, dt.year);
|
||||
Expect.equals(12, dt.month);
|
||||
Expect.equals(31, dt.day);
|
||||
Expect.equals(23, dt.hours);
|
||||
Expect.equals(59, dt.minutes);
|
||||
Expect.equals(59, dt.seconds);
|
||||
Expect.equals(999, dt.milliseconds);
|
||||
dt = new DateTime.withTimeZone(2035, 1, 1, 0, 0, 0, 1,
|
||||
const TimeZone.utc());
|
||||
Expect.equals(SECONDS_YEAR_2035 * 1000 + 1, dt.value);
|
||||
dt = new DateTime.withTimeZone(2034, 12, 31, 23, 59, 59, 999,
|
||||
const TimeZone.utc());
|
||||
Expect.equals(SECONDS_YEAR_2035 * 1000 - 1, dt.value);
|
||||
dt = new DateTime.fromEpoch(SECONDS_YEAR_2035 * 1000 + 1,
|
||||
new TimeZone.local());
|
||||
Expect.equals(true, (2035 == dt.year && 1 == dt.month && 1 == dt.day) ||
|
||||
(2034 == dt.year && 12 == dt.month && 31 == dt.day));
|
||||
Expect.equals(0, dt.seconds);
|
||||
Expect.equals(1, dt.milliseconds);
|
||||
DateTime dt2 = new DateTime.fromDateAndTime(dt.date, dt.time,
|
||||
new TimeZone.local());
|
||||
Expect.equals(dt.value, dt2.value);
|
||||
dt = new DateTime.fromEpoch(SECONDS_YEAR_2035 * 1000 - 1,
|
||||
new TimeZone.local());
|
||||
Expect.equals(true, (2035 == dt.year && 1 == dt.month && 1 == dt.day) ||
|
||||
(2034 == dt.year && 12 == dt.month && 31 == dt.day));
|
||||
Expect.equals(59, dt.seconds);
|
||||
Expect.equals(999, dt.milliseconds);
|
||||
dt2 = new DateTime.fromDateAndTime(dt.date, dt.time, new TimeZone.local());
|
||||
Expect.equals(dt.value, dt2.value);
|
||||
}
|
||||
|
||||
static void testUTCGetters() {
|
||||
var dt = new DateTime.fromEpoch(1305140315000, const TimeZone.utc());
|
||||
Expect.equals(2011, dt.year);
|
||||
Expect.equals(5, dt.month);
|
||||
Expect.equals(11, dt.day);
|
||||
Expect.equals(18, dt.hours);
|
||||
Expect.equals(58, dt.minutes);
|
||||
Expect.equals(35, dt.seconds);
|
||||
Expect.equals(0, dt.milliseconds);
|
||||
Expect.equals(true, const TimeZone.utc() == dt.timeZone);
|
||||
Expect.equals(1305140315000, dt.value);
|
||||
Date d = dt.date;
|
||||
Expect.equals(2011, d.year);
|
||||
Expect.equals(5, d.month);
|
||||
Expect.equals(11, d.day);
|
||||
Time t = dt.time;
|
||||
Expect.equals(0, t.days);
|
||||
Expect.equals(18, t.hours);
|
||||
Expect.equals(58, t.minutes);
|
||||
Expect.equals(35, t.seconds);
|
||||
Expect.equals(0, t.milliseconds);
|
||||
dt = new DateTime.fromEpoch(-9999999, const TimeZone.utc());
|
||||
Expect.equals(1969, dt.year);
|
||||
Expect.equals(12, dt.month);
|
||||
Expect.equals(31, dt.day);
|
||||
Expect.equals(21, dt.hours);
|
||||
Expect.equals(13, dt.minutes);
|
||||
Expect.equals(20, dt.seconds);
|
||||
Expect.equals(1, dt.milliseconds);
|
||||
d = dt.date;
|
||||
Expect.equals(1969, d.year);
|
||||
Expect.equals(12, d.month);
|
||||
Expect.equals(31, d.day);
|
||||
t = dt.time;
|
||||
Expect.equals(21, t.hours);
|
||||
Expect.equals(13, t.minutes);
|
||||
Expect.equals(20, t.seconds);
|
||||
Expect.equals(1, t.milliseconds);
|
||||
}
|
||||
|
||||
static void testLocalGetters() {
|
||||
var dt1 = new DateTime.fromEpoch(1305140315000, new TimeZone.local());
|
||||
var dt2 =
|
||||
new DateTime.withTimeZone(dt1.year, dt1.month, dt1.day,
|
||||
dt1.hours, dt1.minutes, dt1.seconds,
|
||||
dt1.milliseconds,
|
||||
const TimeZone.utc());
|
||||
Time zoneOffset = dt1.difference(dt2);
|
||||
Expect.equals(true, zoneOffset.days == 0);
|
||||
Expect.equals(true, zoneOffset.hours.abs() <= 12);
|
||||
Expect.equals(dt1.year, dt2.year);
|
||||
Expect.equals(dt1.month, dt2.month);
|
||||
Expect.equals(true, (dt1.day - dt2.day).abs() <= 1);
|
||||
Expect.equals(true, dt1.hours < 24);
|
||||
// There are timezones with 0.5 or 0.25 hour offsets.
|
||||
Expect.equals(true,
|
||||
(dt1.minutes == dt2.minutes) ||
|
||||
((dt1.minutes - dt2.minutes).abs() == 30) ||
|
||||
((dt1.minutes - dt2.minutes).abs() == 15));
|
||||
Expect.equals(dt1.seconds, dt2.seconds);
|
||||
Expect.equals(dt1.milliseconds, dt2.milliseconds);
|
||||
}
|
||||
|
||||
static void testConstructors() {
|
||||
var dt1 = new DateTime.fromEpoch(1305140315000, new TimeZone.local());
|
||||
Date d = dt1.date;
|
||||
Time t = dt1.time;
|
||||
var dt3 = new DateTime.fromDateAndTime(d, t, null);
|
||||
Expect.equals(dt1.value, dt3.value);
|
||||
Expect.equals(true, dt1 == dt3);
|
||||
dt3 = new DateTime.fromDateAndTime(d, t, new TimeZone.local());
|
||||
Expect.equals(dt1.value, dt3.value);
|
||||
Expect.equals(true, dt1 == dt3);
|
||||
dt3 = new DateTime.withTimeZone(2011, 5, 11, 18, 58, 35, 0,
|
||||
const TimeZone.utc());
|
||||
Expect.equals(dt1.value, dt3.value);
|
||||
Expect.equals(false, dt1 == dt3);
|
||||
var dt2 = dt1.changeTimeZone(new TimeZone.local());
|
||||
dt3 = new DateTime.withTimeZone(2011, 5, dt1.day,
|
||||
dt1.hours, dt1.minutes, 35, 0,
|
||||
new TimeZone.local());
|
||||
Expect.equals(dt2.value, dt3.value);
|
||||
Expect.equals(true, dt2 == dt3);
|
||||
dt1 = new DateTime.fromEpoch(-9999999, const TimeZone.utc());
|
||||
d = dt1.date;
|
||||
t = dt1.time;
|
||||
dt3 = new DateTime.fromDateAndTime(d, t, const TimeZone.utc());
|
||||
Expect.equals(dt1.value, dt3.value);
|
||||
}
|
||||
|
||||
static void testChangeTimeZone() {
|
||||
var dt1 = new DateTime.fromEpoch(1305140315000, new TimeZone.local());
|
||||
var dt2 = dt1.changeTimeZone(const TimeZone.utc());
|
||||
Expect.equals(dt1.value, dt2.value);
|
||||
var dt3 = new DateTime.fromEpoch(1305140315000, const TimeZone.utc());
|
||||
Expect.equals(dt1.value, dt3.value);
|
||||
Expect.equals(true, dt2.date == dt3.date);
|
||||
Expect.equals(true, dt2.time == dt3.time);
|
||||
var dt4 = dt3.changeTimeZone(new TimeZone.local());
|
||||
Expect.equals(true, dt1.date == dt4.date);
|
||||
Expect.equals(true, dt1.time == dt4.time);
|
||||
}
|
||||
|
||||
static void testSubAdd() {
|
||||
var dt1 = new DateTime.fromEpoch(1305140315000, const TimeZone.utc());
|
||||
var dt2 = dt1.add(const Time.duration(3 * Time.MS_PER_SECOND + 5));
|
||||
Expect.equals(true, dt1.date == dt2.date);
|
||||
Expect.equals(dt1.hours, dt2.hours);
|
||||
Expect.equals(dt1.minutes, dt2.minutes);
|
||||
Expect.equals(dt1.seconds + 3, dt2.seconds);
|
||||
Expect.equals(dt1.milliseconds + 5, dt2.milliseconds);
|
||||
var dt3 = dt2.subtract(const Time.duration(3 * Time.MS_PER_SECOND + 5));
|
||||
Expect.equals(true, dt1 == dt3);
|
||||
Expect.equals(false, dt1 == dt2);
|
||||
}
|
||||
|
||||
static void testDateStrings() {
|
||||
// TODO(floitsch): Clean up the DateTime API that deals with strings.
|
||||
var dt1 = new DateTime.fromString("2011-05-11 18:58:35Z");
|
||||
Expect.equals(1305140315000, dt1.value);
|
||||
var str = dt1.toString();
|
||||
var dt2 = new DateTime.fromString(str);
|
||||
Expect.equals(true, dt1 == dt2);
|
||||
var dt3 = dt1.changeTimeZone(const TimeZone.utc());
|
||||
str = dt3.toString();
|
||||
Expect.equals("2011-05-11 18:58:35.000Z", str);
|
||||
}
|
||||
|
||||
static void testMain() {
|
||||
testNow();
|
||||
testValue();
|
||||
testUTCGetters();
|
||||
testLocalGetters();
|
||||
testConstructors();
|
||||
testChangeTimeZone();
|
||||
testSubAdd();
|
||||
testDateStrings();
|
||||
testEquivalentYears();
|
||||
testFarAwayDates();
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
DateTimeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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.
|
||||
|
||||
// VMOptions=--expose_core_impl
|
||||
|
||||
main() {
|
||||
final msg = 1;
|
||||
try {
|
||||
throw new Exception(msg);
|
||||
Expect.fail("Unreachable");
|
||||
} catch (Exception e) {
|
||||
Expect.isTrue(e is Exception);
|
||||
Expect.isTrue(e is ExceptionImplementation);
|
||||
Expect.equals("Exception: $msg", e.toString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
// 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.
|
||||
|
||||
// Tests basic expressions. Does not attempt to validate the details of arithmetic, coercion, and
|
||||
// so forth.
|
||||
class ExpressionTest {
|
||||
|
||||
ExpressionTest() {}
|
||||
|
||||
int foo;
|
||||
|
||||
static testMain() {
|
||||
var test = new ExpressionTest();
|
||||
test.testBinary();
|
||||
test.testUnary();
|
||||
test.testShifts();
|
||||
test.testBitwise();
|
||||
test.testIncrement();
|
||||
test.testMangling();
|
||||
}
|
||||
|
||||
testBinary() {
|
||||
int x = 4, y = 2;
|
||||
Expect.equals(6, x + y);
|
||||
Expect.equals(2, x - y);
|
||||
Expect.equals(8, x * y);
|
||||
Expect.equals(2, x / y);
|
||||
Expect.equals(0, x % y);
|
||||
}
|
||||
|
||||
testUnary() {
|
||||
int x = 4, y = 2;
|
||||
bool t = true, f = false;
|
||||
Expect.equals(-4, -x);
|
||||
Expect.equals(-5, ~x);
|
||||
Expect.equals(f, !t);
|
||||
}
|
||||
|
||||
testShifts() {
|
||||
int x = 4, y = 2;
|
||||
Expect.equals(y, x >> 1);
|
||||
Expect.equals(x, y << 1);
|
||||
}
|
||||
|
||||
testBitwise() {
|
||||
int x = 4, y = 2;
|
||||
Expect.equals(6, (x | y));
|
||||
Expect.equals(0, (x & y));
|
||||
Expect.equals(6, (x ^ y));
|
||||
}
|
||||
|
||||
operator [](int index) {
|
||||
return foo;
|
||||
}
|
||||
|
||||
operator []=(int index, int value) {
|
||||
foo = value;
|
||||
}
|
||||
|
||||
testIncrement() {
|
||||
int x = 4, a = x++;
|
||||
Expect.equals(4, a);
|
||||
Expect.equals(5, x);
|
||||
Expect.equals(6, ++x);
|
||||
Expect.equals(6, x++);
|
||||
Expect.equals(7, x);
|
||||
Expect.equals(6, --x);
|
||||
Expect.equals(6, x--);
|
||||
Expect.equals(5, x);
|
||||
|
||||
this.foo = 0;
|
||||
Expect.equals(0, this.foo++);
|
||||
Expect.equals(1, this.foo);
|
||||
Expect.equals(2, ++this.foo);
|
||||
Expect.equals(2, this.foo);
|
||||
Expect.equals(2, this.foo--);
|
||||
Expect.equals(1, this.foo);
|
||||
Expect.equals(0, --this.foo);
|
||||
Expect.equals(0, this.foo);
|
||||
|
||||
Expect.equals(0, this[0]++);
|
||||
Expect.equals(1, this[0]);
|
||||
Expect.equals(2, ++this[0]);
|
||||
Expect.equals(2, this[0]);
|
||||
Expect.equals(2, this[0]--);
|
||||
Expect.equals(1, this[0]);
|
||||
Expect.equals(0, --this[0]);
|
||||
Expect.equals(0, this[0]);
|
||||
|
||||
int $0 = 42, $1 = 87, $2 = 117;
|
||||
Expect.equals(42, $0++);
|
||||
Expect.equals(43, $0);
|
||||
Expect.equals(44, ++$0);
|
||||
Expect.equals(88, $0 += $0);
|
||||
Expect.equals(87, $1++);
|
||||
Expect.equals(88, $1);
|
||||
Expect.equals(89, ++$1);
|
||||
Expect.equals(178, ($1 += $1));
|
||||
Expect.equals(117, $2++);
|
||||
Expect.equals(118, $2);
|
||||
Expect.equals(119, ++$2);
|
||||
}
|
||||
|
||||
void testMangling() {
|
||||
int $0 = 42, $1 = 87, $2 = 117;
|
||||
this[0] = 0;
|
||||
Expect.equals(42, (this[0] += $0));
|
||||
Expect.equals(129, (this[0] += $1));
|
||||
Expect.equals(246, (this[0] += $2));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ExpressionTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// 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.
|
||||
|
||||
class ForInTest {
|
||||
static testMain() {
|
||||
testSimple();
|
||||
testBreak();
|
||||
testContinue();
|
||||
testClosure();
|
||||
}
|
||||
|
||||
static Set<int> getSmallSet() {
|
||||
Set<int> set = new Set<int>();
|
||||
set.add(1);
|
||||
set.add(2);
|
||||
set.add(4);
|
||||
return set;
|
||||
}
|
||||
|
||||
static void testSimple() {
|
||||
Set<int> set = getSmallSet();
|
||||
int count = 0;
|
||||
for (final i in set) {
|
||||
count += i;
|
||||
}
|
||||
Expect.equals(7, count);
|
||||
|
||||
count = 0;
|
||||
for (var i in set) {
|
||||
count += i;
|
||||
}
|
||||
Expect.equals(7, count);
|
||||
|
||||
count = 0;
|
||||
for (int i in set) {
|
||||
count += i;
|
||||
}
|
||||
Expect.equals(7, count);
|
||||
|
||||
count = 0;
|
||||
for (final int i in set) {
|
||||
count += i;
|
||||
}
|
||||
Expect.equals(7, count);
|
||||
|
||||
count = 0;
|
||||
int i = 0;
|
||||
Expect.equals(false, set.contains(i)); // Used to test [i] after loop.
|
||||
for (i in set) {
|
||||
count += i;
|
||||
}
|
||||
Expect.equals(7, count);
|
||||
// TODO(ngeoffray): We should really test that [i] is 4 with a set
|
||||
// that preserves order. For now, making sure [i] is in the set
|
||||
// will have to do.
|
||||
Expect.equals(true, set.contains(i));
|
||||
}
|
||||
|
||||
static void testBreak() {
|
||||
Set<int> set = getSmallSet();
|
||||
int count = 0;
|
||||
for (final i in set) {
|
||||
if (i == 4) break;
|
||||
count += i;
|
||||
}
|
||||
Expect.equals(true, count < 4);
|
||||
}
|
||||
|
||||
static void testContinue() {
|
||||
Set<int> set = getSmallSet();
|
||||
int count = 0;
|
||||
for (final i in set) {
|
||||
if (i < 4) continue;
|
||||
count += i;
|
||||
}
|
||||
Expect.equals(4, count);
|
||||
}
|
||||
|
||||
static void testClosure() {
|
||||
Set<int> set = getSmallSet();
|
||||
List<Function> closures = new List(set.length);
|
||||
int index = 0;
|
||||
for (var i in set) {
|
||||
closures[index++] = () => i;
|
||||
}
|
||||
|
||||
Expect.equals(index, set.length);
|
||||
Expect.equals(7, closures[0]() + closures[1]() + closures[2]());
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ForInTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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.
|
||||
|
||||
// Test program for the HashMap class.
|
||||
|
||||
class HashMapTest {
|
||||
|
||||
static testMain() {
|
||||
// TODO(srdjan/ngeoffray): Add more meaningful testing below. For now this
|
||||
// is used to verify that the test script is picking up these tests.
|
||||
var m = new Map();
|
||||
Expect.equals(0, m.length);
|
||||
Expect.equals(true, m.isEmpty());
|
||||
m["one"] = 1;
|
||||
Expect.equals(1, m.length);
|
||||
Expect.equals(false, m.isEmpty());
|
||||
Expect.equals(1, m["one"]);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
HashMapTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// 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.
|
||||
|
||||
// Dart test for testing out of range exceptions on arrays.
|
||||
|
||||
class IndexOutOfRangeExceptionTest {
|
||||
static testRead() {
|
||||
testListRead([], 0);
|
||||
testListRead([], -1);
|
||||
testListRead([], 1);
|
||||
|
||||
var list = [1];
|
||||
testListRead(list, -1);
|
||||
testListRead(list, 1);
|
||||
|
||||
list = new List(1);
|
||||
testListRead(list, -1);
|
||||
testListRead(list, 1);
|
||||
|
||||
list = new List();
|
||||
testListRead(list, -1);
|
||||
testListRead(list, 0);
|
||||
testListRead(list, 1);
|
||||
}
|
||||
|
||||
static testWrite() {
|
||||
testListWrite([], 0);
|
||||
testListWrite([], -1);
|
||||
testListWrite([], 1);
|
||||
|
||||
var list = [1];
|
||||
testListWrite(list, -1);
|
||||
testListWrite(list, 1);
|
||||
|
||||
list = new List(1);
|
||||
testListWrite(list, -1);
|
||||
testListWrite(list, 1);
|
||||
|
||||
list = new List();
|
||||
testListWrite(list, -1);
|
||||
testListWrite(list, 0);
|
||||
testListWrite(list, 1);
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
testRead();
|
||||
testWrite();
|
||||
}
|
||||
|
||||
static testListRead(list, index) {
|
||||
var exception = null;
|
||||
try {
|
||||
var e = list[index];
|
||||
} catch (IndexOutOfRangeException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
}
|
||||
|
||||
static testListWrite(list, index) {
|
||||
var exception = null;
|
||||
try {
|
||||
list[index] = null;
|
||||
} catch (IndexOutOfRangeException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
IndexOutOfRangeExceptionTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
// 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.
|
||||
|
||||
// Dart test for linked hash-maps.
|
||||
|
||||
class LinkedHashMapTest {
|
||||
static void testMain() {
|
||||
Map map = new LinkedHashMap();
|
||||
map["a"] = 1;
|
||||
map["b"] = 2;
|
||||
map["c"] = 3;
|
||||
map["d"] = 4;
|
||||
map["e"] = 5;
|
||||
|
||||
List<String> keys = new List<String>(5);
|
||||
List<int> values = new List<int>(5);
|
||||
|
||||
int index;
|
||||
|
||||
clear() {
|
||||
index = 0;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
keys[i] = null;
|
||||
values[i] = null;
|
||||
}
|
||||
}
|
||||
|
||||
verifyKeys(List<String> correctKeys) {
|
||||
for (int i = 0; i < correctKeys.length; i++) {
|
||||
Expect.equals(correctKeys[i], keys[i]);
|
||||
}
|
||||
}
|
||||
|
||||
verifyValues(List<int> correctValues) {
|
||||
for (int i = 0; i < correctValues.length; i++) {
|
||||
Expect.equals(correctValues[i], values[i]);
|
||||
}
|
||||
}
|
||||
|
||||
testForEachMap(Object key, Object value) {
|
||||
Expect.equals(map[key], value);
|
||||
keys[index] = key;
|
||||
values[index] = value;
|
||||
index++;
|
||||
}
|
||||
|
||||
testForEachValue(Object v) {
|
||||
values[index++] = v;
|
||||
}
|
||||
|
||||
testForEachKey(Object v) {
|
||||
keys[index++] = v;
|
||||
}
|
||||
|
||||
final keysInOrder = const ["a", "b", "c", "d", "e"];
|
||||
final valuesInOrder = const [1, 2, 3, 4, 5];
|
||||
|
||||
clear();
|
||||
map.forEach(testForEachMap);
|
||||
verifyKeys(keysInOrder);
|
||||
verifyValues(valuesInOrder);
|
||||
|
||||
clear();
|
||||
map.getKeys().forEach(testForEachKey);
|
||||
verifyKeys(keysInOrder);
|
||||
|
||||
clear();
|
||||
map.getValues().forEach(testForEachValue);
|
||||
verifyValues(valuesInOrder);
|
||||
|
||||
// Remove and then insert.
|
||||
map.remove("b");
|
||||
map["b"] = 6;
|
||||
final keysAfterBMove = const ["a", "c", "d", "e", "b"];
|
||||
final valuesAfterBMove = const [1, 3, 4, 5, 6];
|
||||
|
||||
|
||||
clear();
|
||||
map.forEach(testForEachMap);
|
||||
verifyKeys(keysAfterBMove);
|
||||
verifyValues(valuesAfterBMove);
|
||||
|
||||
clear();
|
||||
map.getKeys().forEach(testForEachKey);
|
||||
verifyKeys(keysAfterBMove);
|
||||
|
||||
clear();
|
||||
map.getValues().forEach(testForEachValue);
|
||||
verifyValues(valuesAfterBMove);
|
||||
|
||||
// Update.
|
||||
map["a"] = 0;
|
||||
final valuesAfterAUpdate = const [0, 3, 4, 5, 6];
|
||||
|
||||
clear();
|
||||
map.forEach(testForEachMap);
|
||||
verifyKeys(keysAfterBMove);
|
||||
verifyValues(valuesAfterAUpdate);
|
||||
|
||||
clear();
|
||||
map.getKeys().forEach(testForEachKey);
|
||||
verifyKeys(keysAfterBMove);
|
||||
|
||||
clear();
|
||||
map.getValues().forEach(testForEachValue);
|
||||
verifyValues(valuesAfterAUpdate);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
LinkedHashMapTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// 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.
|
||||
|
||||
class ListFromListTest {
|
||||
|
||||
static testMain() {
|
||||
var list = [1, 2, 4];
|
||||
|
||||
var sub = new List.fromList(list, 0, 3);
|
||||
Expect.equals(3, sub.length);
|
||||
Expect.equals(1, sub[0]);
|
||||
Expect.equals(2, sub[1]);
|
||||
Expect.equals(4, sub[2]);
|
||||
|
||||
sub = new List.fromList(list, 1, 3);
|
||||
Expect.equals(2, sub.length);
|
||||
Expect.equals(2, sub[0]);
|
||||
Expect.equals(4, sub[1]);
|
||||
|
||||
sub = new List.fromList(list, 2, 3);
|
||||
Expect.equals(1, sub.length);
|
||||
Expect.equals(4, sub[0]);
|
||||
|
||||
sub = new List.fromList(list, 0, 0);
|
||||
Expect.equals(0, sub.length);
|
||||
|
||||
sub = new List.fromList(list, 3, 3);
|
||||
Expect.equals(0, sub.length);
|
||||
|
||||
sub = new List.fromList(list, 0, 1);
|
||||
Expect.equals(1, sub.length);
|
||||
Expect.equals(1, sub[0]);
|
||||
|
||||
sub = new List.fromList(list, 0, 2);
|
||||
Expect.equals(2, sub.length);
|
||||
Expect.equals(1, sub[0]);
|
||||
Expect.equals(2, sub[1]);
|
||||
|
||||
sub = new List.fromList(list, 1, 2);
|
||||
Expect.equals(1, sub.length);
|
||||
Expect.equals(2, sub[0]);
|
||||
|
||||
sub = new List.fromList(list, -1, 2);
|
||||
Expect.equals(2, sub.length);
|
||||
Expect.equals(1, sub[0]);
|
||||
Expect.equals(2, sub[1]);
|
||||
|
||||
sub = new List.fromList(list, 1, 5);
|
||||
Expect.equals(2, sub.length);
|
||||
Expect.equals(2, sub[0]);
|
||||
Expect.equals(4, sub[1]);
|
||||
|
||||
list = [];
|
||||
sub = new List.fromList(list, 1, 5);
|
||||
Expect.equals(0, sub.length);
|
||||
|
||||
sub = new List.fromList(list, 0, 0);
|
||||
Expect.equals(0, sub.length);
|
||||
|
||||
sub = new List.fromList(list, 0, 1);
|
||||
Expect.equals(0, sub.length);
|
||||
|
||||
// Test that the original list is unchanged after modifications
|
||||
// to the list.
|
||||
list = [1, 2, 4];
|
||||
sub = new List.fromList(list, 0, 3);
|
||||
sub[0] = 42;
|
||||
Expect.equals(1, list[0]);
|
||||
Expect.equals(42, sub[0]);
|
||||
|
||||
sub.add(42);
|
||||
Expect.equals(4, sub.length);
|
||||
Expect.equals(3, list.length);
|
||||
|
||||
list.add(43);
|
||||
Expect.equals(4, sub.length);
|
||||
Expect.equals(4, list.length);
|
||||
|
||||
Expect.equals(42, sub[3]);
|
||||
Expect.equals(43, list[3]);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ListFromListTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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.
|
||||
|
||||
class ListIndexOfTest {
|
||||
static testMain() {
|
||||
test(new List<int>(5));
|
||||
var l = new List<int>();
|
||||
l.length = 5;
|
||||
test(l);
|
||||
}
|
||||
|
||||
static void test(List<int> list) {
|
||||
list[0] = 1;
|
||||
list[1] = 2;
|
||||
list[2] = 3;
|
||||
list[3] = 4;
|
||||
list[4] = 1;
|
||||
|
||||
Expect.equals(3, list.indexOf(4, 0));
|
||||
Expect.equals(0, list.indexOf(1, 0));
|
||||
Expect.equals(4, list.lastIndexOf(1, list.length - 1));
|
||||
|
||||
Expect.equals(4, list.indexOf(1, 1));
|
||||
Expect.equals(-1, list.lastIndexOf(4, 2));
|
||||
|
||||
Expect.equals(3, list.indexOf(4, 2));
|
||||
Expect.equals(3, list.indexOf(4, -5));
|
||||
Expect.equals(-1, list.indexOf(4, 50));
|
||||
|
||||
Expect.equals(-1, list.lastIndexOf(4, 2));
|
||||
Expect.equals(-1, list.lastIndexOf(4, -5));
|
||||
Expect.equals(3, list.lastIndexOf(4, 50));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ListIndexOfTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// 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.
|
||||
|
||||
class ListIteratorsTest {
|
||||
static void checkListIterator(List a) {
|
||||
Iterator it = a.iterator();
|
||||
Expect.equals(false, it.hasNext() == a.isEmpty());
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
Expect.equals(true, it.hasNext());
|
||||
var elem = it.next();
|
||||
}
|
||||
Expect.equals(false, it.hasNext());
|
||||
bool exceptionCaught = false;
|
||||
try {
|
||||
var eleme = it.next();
|
||||
} catch (NoMoreElementsException e) {
|
||||
exceptionCaught = true;
|
||||
}
|
||||
Expect.equals(true, exceptionCaught);
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
checkListIterator([]);
|
||||
checkListIterator([1, 2]);
|
||||
checkListIterator(new List(0));
|
||||
checkListIterator(new List(10));
|
||||
checkListIterator(new List());
|
||||
List g = new List();
|
||||
g.addAll([1, 2]);
|
||||
checkListIterator(g);
|
||||
|
||||
Iterator it = g.iterator();
|
||||
Expect.equals(true, it.hasNext());
|
||||
g.removeLast();
|
||||
Expect.equals(true, it.hasNext());
|
||||
g.removeLast();
|
||||
Expect.equals(false, it.hasNext());
|
||||
|
||||
g.addAll([10, 20]);
|
||||
int sum = 0;
|
||||
for (var elem in g) {
|
||||
sum += elem;
|
||||
// Iterator must realize that g has no more elements.
|
||||
g.removeLast();
|
||||
}
|
||||
Expect.equals(10, sum);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ListIteratorsTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// 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.
|
||||
|
||||
main() {
|
||||
var l = [];
|
||||
l.add(1);
|
||||
Expect.equals(1, l.length);
|
||||
Expect.equals(1, l[0]);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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.
|
||||
|
||||
// Test that a list literal is expandable and modifiable.
|
||||
|
||||
class ListLiteralTest {
|
||||
|
||||
static void testMain() {
|
||||
var list = [1, 2, 3];
|
||||
Expect.equals(3, list.length);
|
||||
list.add(4);
|
||||
Expect.equals(4, list.length);
|
||||
list.addAll([5, 6]);
|
||||
Expect.equals(6, list.length);
|
||||
list[0] = 0;
|
||||
Expect.equals(0, list[0]);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ListLiteralTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// 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.
|
||||
|
||||
#source("SortHelper.dart");
|
||||
|
||||
class ListSortTest {
|
||||
static void testMain() {
|
||||
var compare = (a, b) => a.compareTo(b);
|
||||
var sort = (list) => list.sort(compare);
|
||||
new SortHelper(sort, compare).run();
|
||||
|
||||
compare = (a, b) => -a.compareTo(b);
|
||||
new SortHelper(sort, compare).run();
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ListSortTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// 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.
|
||||
|
||||
class ListTest {
|
||||
|
||||
static testMain() {
|
||||
testList();
|
||||
testExpandableList();
|
||||
}
|
||||
|
||||
static void expectValues(list, val1, val2, val3, val4) {
|
||||
Expect.equals(true, list.length == 4);
|
||||
Expect.equals(true, list.length == 4);
|
||||
Expect.equals(true, !list.isEmpty());
|
||||
Expect.equals(list[0], val1);
|
||||
Expect.equals(list[1], val2);
|
||||
Expect.equals(list[2], val3);
|
||||
Expect.equals(list[3], val4);
|
||||
}
|
||||
|
||||
static void testClosures(List list) {
|
||||
testFilter(val) { return val == 3; }
|
||||
Collection filtered = list.filter(testFilter);
|
||||
Expect.equals(filtered.length, 1);
|
||||
|
||||
testEvery(val) { return val != 11; }
|
||||
bool test = list.every(testEvery);
|
||||
Expect.equals(true, test);
|
||||
|
||||
testSome(val) { return val == 1; }
|
||||
test = list.some(testSome);
|
||||
Expect.equals(true, test);
|
||||
|
||||
testSomeFirst(val) { return val == 0; }
|
||||
test = list.some(testSomeFirst);
|
||||
Expect.equals(true, test);
|
||||
|
||||
testSomeLast(val) { return val == (list.length - 1); }
|
||||
test = list.some(testSomeLast);
|
||||
Expect.equals(true, test);
|
||||
}
|
||||
|
||||
static void testList() {
|
||||
List list = new List(4);
|
||||
Expect.equals(list.length, 4);
|
||||
list[0] = 4;
|
||||
expectValues(list, 4, null, null, null);
|
||||
String val = "fisk";
|
||||
list[1] = val;
|
||||
expectValues(list, 4, val, null, null);
|
||||
double d = 2.0;
|
||||
list[3] = d;
|
||||
expectValues(list, 4, val, null, d);
|
||||
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
list[i] = i;
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Expect.equals(list[i], i);
|
||||
}
|
||||
|
||||
testClosures(list);
|
||||
|
||||
var exception = null;
|
||||
try {
|
||||
list.clear();
|
||||
} catch (UnsupportedOperationException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
}
|
||||
|
||||
static void testExpandableList() {
|
||||
List list = new List();
|
||||
Expect.equals(true, list.isEmpty());
|
||||
Expect.equals(list.length, 0);
|
||||
list.add(4);
|
||||
Expect.equals(1, list.length);
|
||||
Expect.equals(true, !list.isEmpty());
|
||||
Expect.equals(list.length, 1);
|
||||
Expect.equals(list.length, 1);
|
||||
Expect.equals(list.removeLast(), 4);
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
|
||||
Expect.equals(list.length, 10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Expect.equals(list[i], i);
|
||||
}
|
||||
|
||||
testClosures(list);
|
||||
|
||||
Expect.equals(list.removeLast(), 9);
|
||||
list.clear();
|
||||
Expect.equals(list.length, 0);
|
||||
Expect.equals(list.length, 0);
|
||||
Expect.equals(true, list.isEmpty());
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ListTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// 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.
|
||||
|
||||
main() {
|
||||
testWithConstMap();
|
||||
testWithNonConstMap();
|
||||
testWithLinkedMap();
|
||||
}
|
||||
|
||||
testWithConstMap() {
|
||||
var map = const { 'b': 42, 'a': 43 };
|
||||
var otherMap = new Map.from(map);
|
||||
Expect.isTrue(otherMap is Map);
|
||||
Expect.isTrue(otherMap is HashMap);
|
||||
Expect.isTrue(otherMap is !LinkedHashMap);
|
||||
|
||||
Expect.equals(2, otherMap.length);
|
||||
Expect.equals(2, otherMap.getKeys().length);
|
||||
Expect.equals(2, otherMap.getValues().length);
|
||||
|
||||
var count = (map) {
|
||||
int count = 0;
|
||||
map.forEach((a, b) { count += b; });
|
||||
return count;
|
||||
};
|
||||
|
||||
Expect.equals(42 + 43, count(map));
|
||||
Expect.equals(count(map), count(otherMap));
|
||||
}
|
||||
|
||||
testWithNonConstMap() {
|
||||
var map = { 'b': 42, 'a': 43 };
|
||||
var otherMap = new Map.from(map);
|
||||
Expect.isTrue(otherMap is Map);
|
||||
Expect.isTrue(otherMap is HashMap);
|
||||
Expect.isTrue(otherMap is !LinkedHashMap);
|
||||
|
||||
Expect.equals(2, otherMap.length);
|
||||
Expect.equals(2, otherMap.getKeys().length);
|
||||
Expect.equals(2, otherMap.getValues().length);
|
||||
|
||||
int count(map) {
|
||||
int count = 0;
|
||||
map.forEach((a, b) { count += b; });
|
||||
return count;
|
||||
};
|
||||
|
||||
Expect.equals(42 + 43, count(map));
|
||||
Expect.equals(count(map), count(otherMap));
|
||||
|
||||
// Test that adding to the original map does not change otherMap.
|
||||
map['c'] = 44;
|
||||
Expect.equals(3, map.length);
|
||||
Expect.equals(2, otherMap.length);
|
||||
Expect.equals(2, otherMap.getKeys().length);
|
||||
Expect.equals(2, otherMap.getValues().length);
|
||||
|
||||
// Test that adding to otherMap does not change the original map.
|
||||
otherMap['c'] = 44;
|
||||
Expect.equals(3, map.length);
|
||||
Expect.equals(3, otherMap.length);
|
||||
Expect.equals(3, otherMap.getKeys().length);
|
||||
Expect.equals(3, otherMap.getValues().length);
|
||||
}
|
||||
|
||||
testWithLinkedMap() {
|
||||
var map = const { 'b': 1, 'a': 2, 'c': 3 };
|
||||
var otherMap = new LinkedHashMap.from(map);
|
||||
Expect.isTrue(otherMap is Map);
|
||||
Expect.isTrue(otherMap is HashMap);
|
||||
Expect.isTrue(otherMap is LinkedHashMap);
|
||||
var i = 1;
|
||||
for (var val in map.getValues()) {
|
||||
Expect.equals(i++, val);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
// 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.
|
||||
|
||||
// VMOptions=--expose_core_impl
|
||||
|
||||
class MapTest {
|
||||
|
||||
static testMain() {
|
||||
test(new HashMap());
|
||||
test(new LinkedHashMap());
|
||||
test(new SplayTree());
|
||||
testLinkedHashMap();
|
||||
testMapLiteral();
|
||||
testNullValue();
|
||||
}
|
||||
|
||||
static void test(Map map) {
|
||||
testDeletedElement(map);
|
||||
testMap(map, 1, 2, 3, 4, 5, 6, 7, 8);
|
||||
map.clear();
|
||||
testMap(map, "value1", "value2", "value3", "value4", "value5",
|
||||
"value6", "value7", "value8");
|
||||
}
|
||||
|
||||
static void testLinkedHashMap() {
|
||||
LinkedHashMap map = new LinkedHashMap();
|
||||
Expect.equals(false, map.containsKey(1));
|
||||
map[1] = 1;
|
||||
map[1] = 2;
|
||||
Expect.equals(1, map.length);
|
||||
}
|
||||
|
||||
static void testMap(Map map, key1, key2, key3, key4, key5, key6, key7, key8) {
|
||||
int value1 = 10;
|
||||
int value2 = 20;
|
||||
int value3 = 30;
|
||||
int value4 = 40;
|
||||
int value5 = 50;
|
||||
int value6 = 60;
|
||||
int value7 = 70;
|
||||
int value8 = 80;
|
||||
|
||||
Expect.equals(0, map.length);
|
||||
|
||||
map[key1] = value1;
|
||||
Expect.equals(value1, map[key1]);
|
||||
map[key1] = value2;
|
||||
Expect.equals(false, map.containsKey(key2));
|
||||
Expect.equals(1, map.length);
|
||||
|
||||
map[key1] = value1;
|
||||
Expect.equals(value1, map[key1]);
|
||||
// Add enough entries to make sure the table grows.
|
||||
map[key2] = value2;
|
||||
Expect.equals(value2, map[key2]);
|
||||
Expect.equals(2, map.length);
|
||||
map[key3] = value3;
|
||||
Expect.equals(value2, map[key2]);
|
||||
Expect.equals(value3, map[key3]);
|
||||
map[key4] = value4;
|
||||
Expect.equals(value3, map[key3]);
|
||||
Expect.equals(value4, map[key4]);
|
||||
map[key5] = value5;
|
||||
Expect.equals(value4, map[key4]);
|
||||
Expect.equals(value5, map[key5]);
|
||||
map[key6] = value6;
|
||||
Expect.equals(value5, map[key5]);
|
||||
Expect.equals(value6, map[key6]);
|
||||
map[key7] = value7;
|
||||
Expect.equals(value6, map[key6]);
|
||||
Expect.equals(value7, map[key7]);
|
||||
map[key8] = value8;
|
||||
Expect.equals(value1, map[key1]);
|
||||
Expect.equals(value2, map[key2]);
|
||||
Expect.equals(value3, map[key3]);
|
||||
Expect.equals(value4, map[key4]);
|
||||
Expect.equals(value5, map[key5]);
|
||||
Expect.equals(value6, map[key6]);
|
||||
Expect.equals(value7, map[key7]);
|
||||
Expect.equals(value8, map[key8]);
|
||||
Expect.equals(8, map.length);
|
||||
|
||||
map.remove(key4);
|
||||
Expect.equals(false, map.containsKey(key4));
|
||||
Expect.equals(7, map.length);
|
||||
|
||||
// Test clearing the table.
|
||||
map.clear();
|
||||
Expect.equals(0, map.length);
|
||||
Expect.equals(false, map.containsKey(key1));
|
||||
Expect.equals(false, map.containsKey(key2));
|
||||
Expect.equals(false, map.containsKey(key3));
|
||||
Expect.equals(false, map.containsKey(key4));
|
||||
Expect.equals(false, map.containsKey(key5));
|
||||
Expect.equals(false, map.containsKey(key6));
|
||||
Expect.equals(false, map.containsKey(key7));
|
||||
Expect.equals(false, map.containsKey(key8));
|
||||
|
||||
// Test adding and removing again.
|
||||
map[key1] = value1;
|
||||
Expect.equals(value1, map[key1]);
|
||||
Expect.equals(1, map.length);
|
||||
map[key2] = value2;
|
||||
Expect.equals(value2, map[key2]);
|
||||
Expect.equals(2, map.length);
|
||||
map[key3] = value3;
|
||||
Expect.equals(value3, map[key3]);
|
||||
map.remove(key3);
|
||||
Expect.equals(2, map.length);
|
||||
map[key4] = value4;
|
||||
Expect.equals(value4, map[key4]);
|
||||
map.remove(key4);
|
||||
Expect.equals(2, map.length);
|
||||
map[key5] = value5;
|
||||
Expect.equals(value5, map[key5]);
|
||||
map.remove(key5);
|
||||
Expect.equals(2, map.length);
|
||||
map[key6] = value6;
|
||||
Expect.equals(value6, map[key6]);
|
||||
map.remove(key6);
|
||||
Expect.equals(2, map.length);
|
||||
map[key7] = value7;
|
||||
Expect.equals(value7, map[key7]);
|
||||
map.remove(key7);
|
||||
Expect.equals(2, map.length);
|
||||
map[key8] = value8;
|
||||
Expect.equals(value8, map[key8]);
|
||||
map.remove(key8);
|
||||
Expect.equals(2, map.length);
|
||||
|
||||
Expect.equals(true, map.containsKey(key1));
|
||||
Expect.equals(true, map.containsValue(value1));
|
||||
|
||||
// Test Map.forEach.
|
||||
Map other_map = new Map();
|
||||
void testForEachMap(key, value) {
|
||||
other_map[key] = value;
|
||||
}
|
||||
map.forEach(testForEachMap);
|
||||
Expect.equals(true, other_map.containsKey(key1));
|
||||
Expect.equals(true, other_map.containsKey(key2));
|
||||
Expect.equals(true, other_map.containsValue(value1));
|
||||
Expect.equals(true, other_map.containsValue(value2));
|
||||
Expect.equals(2, other_map.length);
|
||||
|
||||
other_map.clear();
|
||||
Expect.equals(0, other_map.length);
|
||||
|
||||
// Test Collection.getKeys.
|
||||
void testForEachCollection(value) {
|
||||
other_map[value] = value;
|
||||
}
|
||||
Collection keys = map.getKeys();
|
||||
keys.forEach(testForEachCollection);
|
||||
Expect.equals(true, other_map.containsKey(key1));
|
||||
Expect.equals(true, other_map.containsKey(key2));
|
||||
Expect.equals(true, other_map.containsValue(key1));
|
||||
Expect.equals(true, other_map.containsValue(key2));
|
||||
Expect.equals(true, !other_map.containsKey(value1));
|
||||
Expect.equals(true, !other_map.containsKey(value2));
|
||||
Expect.equals(true, !other_map.containsValue(value1));
|
||||
Expect.equals(true, !other_map.containsValue(value2));
|
||||
Expect.equals(2, other_map.length);
|
||||
other_map.clear();
|
||||
Expect.equals(0, other_map.length);
|
||||
|
||||
// Test Collection.getValues.
|
||||
Collection values = map.getValues();
|
||||
values.forEach(testForEachCollection);
|
||||
Expect.equals(true, !other_map.containsKey(key1));
|
||||
Expect.equals(true, !other_map.containsKey(key2));
|
||||
Expect.equals(true, !other_map.containsValue(key1));
|
||||
Expect.equals(true, !other_map.containsValue(key2));
|
||||
Expect.equals(true, other_map.containsKey(value1));
|
||||
Expect.equals(true, other_map.containsKey(value2));
|
||||
Expect.equals(true, other_map.containsValue(value1));
|
||||
Expect.equals(true, other_map.containsValue(value2));
|
||||
Expect.equals(2, other_map.length);
|
||||
other_map.clear();
|
||||
Expect.equals(0, other_map.length);
|
||||
|
||||
// Test Map.putIfAbsent.
|
||||
map.clear();
|
||||
Expect.equals(false, map.containsKey(key1));
|
||||
map.putIfAbsent(key1, () => 10);
|
||||
Expect.equals(true, map.containsKey(key1));
|
||||
Expect.equals(10, map[key1]);
|
||||
Expect.equals(10,
|
||||
map.putIfAbsent(key1, () => 11));
|
||||
}
|
||||
|
||||
static void testDeletedElement(Map map) {
|
||||
map.clear();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
map[1] = 2;
|
||||
Expect.equals(1, map.length);
|
||||
map.remove(1);
|
||||
Expect.equals(0, map.length);
|
||||
}
|
||||
Expect.equals(0, map.length);
|
||||
}
|
||||
|
||||
static void testMapLiteral() {
|
||||
Map m = {"a": 1, "b" : 2, "c": 3 };
|
||||
Expect.equals(3, m.length);
|
||||
int sum = 0;
|
||||
m.forEach((a, b) {
|
||||
sum += b;
|
||||
});
|
||||
Expect.equals(6, sum);
|
||||
|
||||
List values = m.getKeys();
|
||||
Expect.equals(3, values.length);
|
||||
String first = values[0];
|
||||
String second = values[1];
|
||||
String third = values[2];
|
||||
String all = "${first}${second}${third}";
|
||||
Expect.equals(3, all.length);
|
||||
Expect.equals(true, all.contains("a", 0));
|
||||
Expect.equals(true, all.contains("b", 0));
|
||||
Expect.equals(true, all.contains("c", 0));
|
||||
}
|
||||
|
||||
static void testNullValue() {
|
||||
Map m = {"a": 1, "b" : null, "c": 3 };
|
||||
|
||||
Expect.equals(null, m["b"]);
|
||||
Expect.equals(true, m.containsKey("b"));
|
||||
Expect.equals(3, m.length);
|
||||
|
||||
m["a"] = null;
|
||||
m["c"] = null;
|
||||
Expect.equals(null, m["a"]);
|
||||
Expect.equals(true, m.containsKey("a"));
|
||||
Expect.equals(null, m["c"]);
|
||||
Expect.equals(true, m.containsKey("c"));
|
||||
Expect.equals(3, m.length);
|
||||
|
||||
m.remove("a");
|
||||
Expect.equals(2, m.length);
|
||||
Expect.equals(null, m["a"]);
|
||||
Expect.equals(false, m.containsKey("a"));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
MapTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,372 @@
|
||||
// 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.
|
||||
|
||||
class MathTest {
|
||||
static void testConstants() {
|
||||
// Source for mathematical constants is Wolfram Alpha.
|
||||
Expect.equals(2.7182818284590452353602874713526624977572470936999595749669,
|
||||
Math.E);
|
||||
Expect.equals(2.3025850929940456840179914546843642076011014886287729760333,
|
||||
Math.LN10);
|
||||
Expect.equals(0.6931471805599453094172321214581765680755001343602552541206,
|
||||
Math.LN2);
|
||||
Expect.equals(1.4426950408889634073599246810018921374266459541529859341354,
|
||||
Math.LOG2E);
|
||||
Expect.equals(0.4342944819032518276511289189166050822943970058036665661144,
|
||||
Math.LOG10E);
|
||||
Expect.equals(3.1415926535897932384626433832795028841971693993751058209749,
|
||||
Math.PI);
|
||||
Expect.equals(0.7071067811865475244008443621048490392848359376884740365883,
|
||||
Math.SQRT1_2);
|
||||
Expect.equals(1.4142135623730950488016887242096980785696718753769480731766,
|
||||
Math.SQRT2);
|
||||
}
|
||||
|
||||
static checkClose(double a, double b, EPSILON) {
|
||||
Expect.equals(true, a - EPSILON <= b);
|
||||
Expect.equals(true, b <= a + EPSILON);
|
||||
}
|
||||
|
||||
static void testSin() {
|
||||
// Given the imprecision of PI we can't expect better results than this.
|
||||
final double EPSILON = 1e-15;
|
||||
checkClose(0.0, Math.sin(0.0), EPSILON);
|
||||
checkClose(0.0, Math.sin(Math.PI), EPSILON);
|
||||
checkClose(0.0, Math.sin(2.0 * Math.PI), EPSILON);
|
||||
checkClose(1.0, Math.sin(Math.PI / 2.0), EPSILON);
|
||||
checkClose(-1.0, Math.sin(Math.PI * (3.0 / 2.0)), EPSILON);
|
||||
}
|
||||
|
||||
static void testCos() {
|
||||
// Given the imprecision of PI we can't expect better results than this.
|
||||
final double EPSILON = 1e-15;
|
||||
checkClose(1.0, Math.cos(0.0), EPSILON);
|
||||
checkClose(-1.0, Math.cos(Math.PI), EPSILON);
|
||||
checkClose(1.0, Math.cos(2.0 * Math.PI), EPSILON);
|
||||
checkClose(0.0, Math.cos(Math.PI / 2.0), EPSILON);
|
||||
checkClose(0.0, Math.cos(Math.PI * (3.0 / 2.0)), EPSILON);
|
||||
}
|
||||
|
||||
static void testTan() {
|
||||
// Given the imprecision of PI we can't expect better results than this.
|
||||
final double EPSILON = 1e-15;
|
||||
checkClose(0.0, Math.tan(0.0), EPSILON);
|
||||
checkClose(0.0, Math.tan(Math.PI), EPSILON);
|
||||
checkClose(0.0, Math.tan(2.0 * Math.PI), EPSILON);
|
||||
checkClose(1.0, Math.tan(Math.PI / 4.0), EPSILON);
|
||||
}
|
||||
|
||||
static void testAsin() {
|
||||
// Given the imprecision of PI we can't expect better results than this.
|
||||
final double EPSILON = 1e-15;
|
||||
checkClose(0.0, Math.asin(0.0), EPSILON);
|
||||
checkClose(Math.PI / 2.0, Math.asin(1.0), EPSILON);
|
||||
checkClose(-Math.PI / 2.0, Math.asin(-1.0), EPSILON);
|
||||
}
|
||||
|
||||
|
||||
static void testAcos() {
|
||||
// Given the imprecision of PI we can't expect better results than this.
|
||||
final double EPSILON = 1e-15;
|
||||
checkClose(0.0, Math.acos(1.0), EPSILON);
|
||||
checkClose(Math.PI, Math.acos(-1.0), EPSILON);
|
||||
checkClose(Math.PI / 2.0, Math.acos(0.0), EPSILON);
|
||||
}
|
||||
|
||||
static void testAtan() {
|
||||
// Given the imprecision of PI we can't expect better results than this.
|
||||
final double EPSILON = 1e-15;
|
||||
checkClose(0.0, Math.atan(0.0), EPSILON);
|
||||
checkClose(Math.PI / 4.0, Math.atan(1.0), EPSILON);
|
||||
checkClose(-Math.PI / 4.0, Math.atan(-1.0), EPSILON);
|
||||
}
|
||||
|
||||
static void testAtan2() {
|
||||
// Given the imprecision of PI we can't expect better results than this.
|
||||
final double EPSILON = 1e-15;
|
||||
checkClose(0.0, Math.atan2(0.0, 5.0), EPSILON);
|
||||
checkClose(Math.PI / 4.0, Math.atan2(2.0, 2.0), EPSILON);
|
||||
checkClose(3 * Math.PI / 4.0, Math.atan2(0.5, -0.5), EPSILON);
|
||||
checkClose(-3 * Math.PI / 4.0, Math.atan2(-2.5, -2.5), EPSILON);
|
||||
}
|
||||
|
||||
static checkVeryClose(double a, double b) {
|
||||
// We find a ulp (unit in the last place) by shifting the original number
|
||||
// to the right. This only works if we are not too close to infinity or if
|
||||
// we work with denormals.
|
||||
// We special case or 0.0, but not for infinity.
|
||||
if (a == 0.0) {
|
||||
final minimalDouble = 4.9406564584124654e-324;
|
||||
Expect.equals(true, b.abs() <= minimalDouble);
|
||||
return;
|
||||
}
|
||||
if (b == 0.0) {
|
||||
// No need to look if they are close. Otherwise the check for 'a' above
|
||||
// whould have triggered.
|
||||
Expect.equals(a, b);
|
||||
}
|
||||
final double shiftRightBy52 = 2.220446049250313080847263336181640625e-16;
|
||||
final double shiftedA = (a * shiftRightBy52).abs();
|
||||
// Compared to 'a', 'shiftedA' is now ~1-2 ulp.
|
||||
|
||||
final double limitLow = a - shiftedA;
|
||||
final double limitHigh = a + shiftedA;
|
||||
Expect.equals(false, a == limitLow);
|
||||
Expect.equals(false, a == limitHigh);
|
||||
Expect.equals(true, limitLow <= b);
|
||||
Expect.equals(true, b <= limitHigh);
|
||||
}
|
||||
|
||||
static void testSqrt() {
|
||||
checkVeryClose(2.0, Math.sqrt(4.0));
|
||||
checkVeryClose(Math.SQRT2, Math.sqrt(2.0));
|
||||
checkVeryClose(Math.SQRT1_2, Math.sqrt(0.5));
|
||||
checkVeryClose(1e50, Math.sqrt(1e100));
|
||||
checkVeryClose(1.1111111061110855443054405046358901279277111935183977e56,
|
||||
Math.sqrt(12345678901234e99));
|
||||
}
|
||||
|
||||
static void testExp() {
|
||||
checkVeryClose(Math.E, Math.exp(1.0));
|
||||
final EPSILON = 1e-15;
|
||||
checkClose(10.0, Math.exp(Math.LN10), EPSILON);
|
||||
checkClose(2.0, Math.exp(Math.LN2), EPSILON);
|
||||
}
|
||||
|
||||
static void testLog() {
|
||||
// Even though E is imprecise, it is good enough to get really close to 1.
|
||||
// We still provide an epsilon.
|
||||
checkClose(1.0, Math.log(Math.E), 1e-16);
|
||||
checkVeryClose(Math.LN10, Math.log(10.0));
|
||||
checkVeryClose(Math.LN2, Math.log(2.0));
|
||||
}
|
||||
|
||||
static void testPow() {
|
||||
checkVeryClose(16.0, Math.pow(4.0, 2.0));
|
||||
checkVeryClose(Math.SQRT2, Math.pow(2.0, 0.5));
|
||||
checkVeryClose(Math.SQRT1_2, Math.pow(0.5, 0.5));
|
||||
}
|
||||
|
||||
static bool parseIntThrowsBadNumberFormatException(str) {
|
||||
try {
|
||||
Math.parseInt(str);
|
||||
return false;
|
||||
} catch (BadNumberFormatException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static void testParseInt() {
|
||||
Expect.equals(499, Math.parseInt("499"));
|
||||
Expect.equals(499, Math.parseInt("+499"));
|
||||
Expect.equals(-499, Math.parseInt("-499"));
|
||||
Expect.equals(499, Math.parseInt(" 499 "));
|
||||
Expect.equals(499, Math.parseInt(" +499 "));
|
||||
Expect.equals(-499, Math.parseInt(" -499 "));
|
||||
Expect.equals(0, Math.parseInt("0"));
|
||||
Expect.equals(0, Math.parseInt("+0"));
|
||||
Expect.equals(0, Math.parseInt("-0"));
|
||||
Expect.equals(0, Math.parseInt(" 0 "));
|
||||
Expect.equals(0, Math.parseInt(" +0 "));
|
||||
Expect.equals(0, Math.parseInt(" -0 "));
|
||||
Expect.equals(0x1234567890, Math.parseInt("0x1234567890"));
|
||||
Expect.equals(0x1234567890, Math.parseInt("+0x1234567890"));
|
||||
Expect.equals(-0x1234567890, Math.parseInt("-0x1234567890"));
|
||||
Expect.equals(0x1234567890, Math.parseInt(" 0x1234567890 "));
|
||||
Expect.equals(0x1234567890, Math.parseInt(" +0x1234567890 "));
|
||||
Expect.equals(-0x1234567890, Math.parseInt(" -0x1234567890 "));
|
||||
Expect.equals(256, Math.parseInt("0x100"));
|
||||
Expect.equals(256, Math.parseInt("+0x100"));
|
||||
Expect.equals(-256, Math.parseInt("-0x100"));
|
||||
Expect.equals(256, Math.parseInt(" 0x100 "));
|
||||
Expect.equals(256, Math.parseInt(" +0x100 "));
|
||||
Expect.equals(-256, Math.parseInt(" -0x100 "));
|
||||
Expect.equals(0xabcdef, Math.parseInt("0xabcdef"));
|
||||
Expect.equals(0xABCDEF, Math.parseInt("0xABCDEF"));
|
||||
Expect.equals(0xabcdef, Math.parseInt("0xabCDEf"));
|
||||
Expect.equals(-0xabcdef, Math.parseInt("-0xabcdef"));
|
||||
Expect.equals(-0xABCDEF, Math.parseInt("-0xABCDEF"));
|
||||
Expect.equals(0xabcdef, Math.parseInt(" 0xabcdef "));
|
||||
Expect.equals(0xABCDEF, Math.parseInt(" 0xABCDEF "));
|
||||
Expect.equals(-0xabcdef, Math.parseInt(" -0xabcdef "));
|
||||
Expect.equals(-0xABCDEF, Math.parseInt(" -0xABCDEF "));
|
||||
Expect.equals(0xabcdef, Math.parseInt("0x00000abcdef"));
|
||||
Expect.equals(0xABCDEF, Math.parseInt("0x00000ABCDEF"));
|
||||
Expect.equals(-0xabcdef, Math.parseInt("-0x00000abcdef"));
|
||||
Expect.equals(-0xABCDEF, Math.parseInt("-0x00000ABCDEF"));
|
||||
Expect.equals(0xabcdef, Math.parseInt(" 0x00000abcdef "));
|
||||
Expect.equals(0xABCDEF, Math.parseInt(" 0x00000ABCDEF "));
|
||||
Expect.equals(-0xabcdef, Math.parseInt(" -0x00000abcdef "));
|
||||
Expect.equals(-0xABCDEF, Math.parseInt(" -0x00000ABCDEF "));
|
||||
Expect.equals(10, Math.parseInt("010"));
|
||||
Expect.equals(-10, Math.parseInt("-010"));
|
||||
Expect.equals(10, Math.parseInt(" 010 "));
|
||||
Expect.equals(-10, Math.parseInt(" -010 "));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("1b"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1b "));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1 b "));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("1e2"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1e2 "));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("00x12"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(" 00x12 "));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("-1b"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1b "));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1 b "));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("-1e2"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1e2 "));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("-00x12"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(" -00x12 "));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(" -00x12 "));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("0x0x12"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("0.1"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("0x3.1"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("5."));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("+-5"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("-+5"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("--5"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("++5"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("+ 5"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException("- 5"));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(""));
|
||||
Expect.equals(true, parseIntThrowsBadNumberFormatException(" "));
|
||||
}
|
||||
|
||||
static bool parseDoubleThrowsBadNumberFormatException(str) {
|
||||
try {
|
||||
Math.parseDouble(str);
|
||||
return false;
|
||||
} catch (BadNumberFormatException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static void testParseDouble() {
|
||||
Expect.equals(499.0, Math.parseDouble("499"));
|
||||
Expect.equals(499.0, Math.parseDouble("499.0"));
|
||||
Expect.equals(499.0, Math.parseDouble("499.0"));
|
||||
Expect.equals(499.0, Math.parseDouble("+499"));
|
||||
Expect.equals(-499.0, Math.parseDouble("-499"));
|
||||
Expect.equals(499.0, Math.parseDouble(" 499 "));
|
||||
Expect.equals(499.0, Math.parseDouble(" +499 "));
|
||||
Expect.equals(-499.0, Math.parseDouble(" -499 "));
|
||||
Expect.equals(0.0, Math.parseDouble("0"));
|
||||
Expect.equals(0.0, Math.parseDouble("+0"));
|
||||
Expect.equals(-0.0, Math.parseDouble("-0"));
|
||||
Expect.equals(true, Math.parseDouble("-0").isNegative());
|
||||
Expect.equals(0.0, Math.parseDouble(" 0 "));
|
||||
Expect.equals(0.0, Math.parseDouble(" +0 "));
|
||||
Expect.equals(-0.0, Math.parseDouble(" -0 "));
|
||||
Expect.equals(true, Math.parseDouble(" -0 ").isNegative());
|
||||
Expect.equals(1.0 * 0x1234567890, Math.parseDouble("0x1234567890"));
|
||||
Expect.equals(1.0 * 0x1234567890, Math.parseDouble("+0x1234567890"));
|
||||
Expect.equals(1.0 * -0x1234567890, Math.parseDouble("-0x1234567890"));
|
||||
Expect.equals(1.0 * 0x1234567890, Math.parseDouble(" 0x1234567890 "));
|
||||
Expect.equals(1.0 * 0x1234567890, Math.parseDouble(" +0x1234567890 "));
|
||||
Expect.equals(1.0 * -0x1234567890, Math.parseDouble(" -0x1234567890 "));
|
||||
Expect.equals(256.0, Math.parseDouble("0x100"));
|
||||
Expect.equals(256.0, Math.parseDouble("+0x100"));
|
||||
Expect.equals(-256.0, Math.parseDouble("-0x100"));
|
||||
Expect.equals(256.0, Math.parseDouble(" 0x100 "));
|
||||
Expect.equals(256.0, Math.parseDouble(" +0x100 "));
|
||||
Expect.equals(-256.0, Math.parseDouble(" -0x100 "));
|
||||
Expect.equals(1.0 * 0xabcdef, Math.parseDouble("0xabcdef"));
|
||||
Expect.equals(1.0 * 0xABCDEF, Math.parseDouble("0xABCDEF"));
|
||||
Expect.equals(1.0 * 0xabcdef, Math.parseDouble("0xabCDEf"));
|
||||
Expect.equals(1.0 * -0xabcdef, Math.parseDouble("-0xabcdef"));
|
||||
Expect.equals(1.0 * -0xABCDEF, Math.parseDouble("-0xABCDEF"));
|
||||
Expect.equals(1.0 * 0xabcdef, Math.parseDouble(" 0xabcdef "));
|
||||
Expect.equals(1.0 * 0xABCDEF, Math.parseDouble(" 0xABCDEF "));
|
||||
Expect.equals(1.0 * -0xabcdef, Math.parseDouble(" -0xabcdef "));
|
||||
Expect.equals(1.0 * -0xABCDEF, Math.parseDouble(" -0xABCDEF "));
|
||||
Expect.equals(1.0 * 0xabcdef, Math.parseDouble("0x00000abcdef"));
|
||||
Expect.equals(1.0 * 0xABCDEF, Math.parseDouble("0x00000ABCDEF"));
|
||||
Expect.equals(1.0 * -0xabcdef, Math.parseDouble("-0x00000abcdef"));
|
||||
Expect.equals(1.0 * -0xABCDEF, Math.parseDouble("-0x00000ABCDEF"));
|
||||
Expect.equals(1.0 * 0xabcdef, Math.parseDouble(" 0x00000abcdef "));
|
||||
Expect.equals(1.0 * 0xABCDEF, Math.parseDouble(" 0x00000ABCDEF "));
|
||||
Expect.equals(1.0 * -0xabcdef, Math.parseDouble(" -0x00000abcdef "));
|
||||
Expect.equals(1.0 * -0xABCDEF, Math.parseDouble(" -0x00000ABCDEF "));
|
||||
Expect.equals(10.0, Math.parseDouble("010"));
|
||||
Expect.equals(-10.0, Math.parseDouble("-010"));
|
||||
Expect.equals(10.0, Math.parseDouble(" 010 "));
|
||||
Expect.equals(-10.0, Math.parseDouble(" -010 "));
|
||||
Expect.equals(0.1, Math.parseDouble("0.1"));
|
||||
Expect.equals(0.1, Math.parseDouble(" 0.1 "));
|
||||
Expect.equals(0.1, Math.parseDouble(" +0.1 "));
|
||||
Expect.equals(-0.1, Math.parseDouble(" -0.1 "));
|
||||
Expect.equals(0.1, Math.parseDouble(".1"));
|
||||
Expect.equals(0.1, Math.parseDouble(" .1 "));
|
||||
Expect.equals(0.1, Math.parseDouble(" +.1 "));
|
||||
Expect.equals(-0.1, Math.parseDouble(" -.1 "));
|
||||
Expect.equals(1234567.89, Math.parseDouble("1234567.89"));
|
||||
Expect.equals(1234567.89, Math.parseDouble(" 1234567.89 "));
|
||||
Expect.equals(1234567.89, Math.parseDouble(" +1234567.89 "));
|
||||
Expect.equals(-1234567.89, Math.parseDouble(" -1234567.89 "));
|
||||
Expect.equals(1234567e89, Math.parseDouble("1234567e89"));
|
||||
Expect.equals(1234567e89, Math.parseDouble(" 1234567e89 "));
|
||||
Expect.equals(1234567e89, Math.parseDouble(" +1234567e89 "));
|
||||
Expect.equals(-1234567e89, Math.parseDouble(" -1234567e89 "));
|
||||
Expect.equals(1234567.89e2, Math.parseDouble("1234567.89e2"));
|
||||
Expect.equals(1234567.89e2, Math.parseDouble(" 1234567.89e2 "));
|
||||
Expect.equals(1234567.89e2, Math.parseDouble(" +1234567.89e2 "));
|
||||
Expect.equals(-1234567.89e2, Math.parseDouble(" -1234567.89e2 "));
|
||||
Expect.equals(1234567.89e2, Math.parseDouble("1234567.89E2"));
|
||||
Expect.equals(1234567.89e2, Math.parseDouble(" 1234567.89E2 "));
|
||||
Expect.equals(1234567.89e2, Math.parseDouble(" +1234567.89E2 "));
|
||||
Expect.equals(-1234567.89e2, Math.parseDouble(" -1234567.89E2 "));
|
||||
Expect.equals(1234567.89e-2, Math.parseDouble("1234567.89e-2"));
|
||||
Expect.equals(1234567.89e-2, Math.parseDouble(" 1234567.89e-2 "));
|
||||
Expect.equals(1234567.89e-2, Math.parseDouble(" +1234567.89e-2 "));
|
||||
Expect.equals(-1234567.89e-2, Math.parseDouble(" -1234567.89e-2 "));
|
||||
// TODO(floitsch): add tests for NaN and Infinity.
|
||||
Expect.equals(false, parseDoubleThrowsBadNumberFormatException("1.5"));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException("1b"));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" 1b "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" 1 b "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" e3 "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" .e3 "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException("00x12"));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" 00x12 "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException("-1b"));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -1b "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -1 b "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException("-00x12"));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -00x12 "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -00x12 "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException("0x0x12"));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException("+ 1.5"));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException("- 1.5"));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(""));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException("5."));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" 5. "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" +5. "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -5. "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException("1234567.e2"));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" 1234567.e2 "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" +1234567.e2 "));
|
||||
Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -1234567.e2 "));
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
testConstants();
|
||||
testSin();
|
||||
testCos();
|
||||
testTan();
|
||||
testAsin();
|
||||
testAcos();
|
||||
testAtan();
|
||||
testAtan2();
|
||||
testSqrt();
|
||||
testLog();
|
||||
testExp();
|
||||
testPow();
|
||||
testParseInt();
|
||||
testParseDouble();
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
MathTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for testing properties of ports.
|
||||
|
||||
class PortTest {
|
||||
|
||||
static void testMain() {
|
||||
testHashCode();
|
||||
testEquals();
|
||||
testMap();
|
||||
}
|
||||
|
||||
static void testHashCode() {
|
||||
ReceivePort rp0 = new ReceivePort();
|
||||
ReceivePort rp1 = new ReceivePort();
|
||||
Expect.equals(rp0.toSendPort().hashCode(), rp0.toSendPort().hashCode());
|
||||
Expect.equals(rp1.toSendPort().hashCode(), rp1.toSendPort().hashCode());
|
||||
rp0.close();
|
||||
rp1.close();
|
||||
}
|
||||
|
||||
static void testEquals() {
|
||||
ReceivePort rp0 = new ReceivePort();
|
||||
ReceivePort rp1 = new ReceivePort();
|
||||
Expect.equals(rp0.toSendPort(), rp0.toSendPort());
|
||||
Expect.equals(rp1.toSendPort(), rp1.toSendPort());
|
||||
Expect.equals(false, (rp0.toSendPort() == rp1.toSendPort()));
|
||||
rp0.close();
|
||||
rp1.close();
|
||||
}
|
||||
|
||||
static void testMap() {
|
||||
ReceivePort rp0 = new ReceivePort();
|
||||
ReceivePort rp1 = new ReceivePort();
|
||||
final map = new Map<SendPort, int>();
|
||||
map[rp0.toSendPort()] = 42;
|
||||
map[rp1.toSendPort()] = 87;
|
||||
Expect.equals(42, map[rp0.toSendPort()]);
|
||||
Expect.equals(87, map[rp1.toSendPort()]);
|
||||
|
||||
map[rp0.toSendPort()] = 99;
|
||||
Expect.equals(99, map[rp0.toSendPort()]);
|
||||
Expect.equals(87, map[rp1.toSendPort()]);
|
||||
|
||||
map.remove(rp0.toSendPort());
|
||||
Expect.equals(false, map.containsKey(rp0.toSendPort()));
|
||||
Expect.equals(87, map[rp1.toSendPort()]);
|
||||
|
||||
map.remove(rp1.toSendPort());
|
||||
Expect.equals(false, map.containsKey(rp0.toSendPort()));
|
||||
Expect.equals(false, map.containsKey(rp1.toSendPort()));
|
||||
|
||||
rp0.close();
|
||||
rp1.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
main() {
|
||||
PortTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,721 @@
|
||||
// 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.
|
||||
|
||||
class PromiseTest {
|
||||
|
||||
static void testMain() {
|
||||
testNormalComplete();
|
||||
testFromValue();
|
||||
testNormalCompleteWithHandler();
|
||||
testNormalCompleteManyHandlers();
|
||||
testError();
|
||||
testErrorWithHandler();
|
||||
testCancel();
|
||||
testCancelWithHandler();
|
||||
testChainComplete();
|
||||
testChainError();
|
||||
testChainCancel();
|
||||
testFlatten();
|
||||
testJoinSelectSecond();
|
||||
testWaitFor1();
|
||||
testWaitForAll();
|
||||
}
|
||||
|
||||
static void testNormalComplete() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
a.complete(3);
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(true, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(3, a.value);
|
||||
Expect.equals(null, a.error);
|
||||
}
|
||||
|
||||
static void testFromValue() {
|
||||
Promise<int> a = new Promise<int>.fromValue(3);
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(true, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(3, a.value);
|
||||
Expect.equals(null, a.error);
|
||||
}
|
||||
|
||||
static void testNormalCompleteWithHandler() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Promise<int> b = new Promise<int>();
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(false, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
readValueThrowsException_(b);
|
||||
readErrorThrowsException_(b);
|
||||
int afterA = null;
|
||||
int afterB = null;
|
||||
|
||||
// value computed after setup is done.
|
||||
a.addCompleteHandler((int v) { afterA = v; });
|
||||
a.complete(3);
|
||||
|
||||
// value computed before setup was done.
|
||||
b.complete(4);
|
||||
b.addCompleteHandler((int v) { afterB = v; });
|
||||
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(true, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(true, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
Expect.equals(null, a.error);
|
||||
Expect.equals(null, b.error);
|
||||
|
||||
Expect.equals(3, a.value);
|
||||
Expect.equals(4, b.value);
|
||||
Expect.equals(3, afterA);
|
||||
Expect.equals(4, afterB);
|
||||
}
|
||||
|
||||
static void testNormalCompleteManyHandlers() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
int afterA1 = null;
|
||||
int afterA2 = null;
|
||||
int afterA3 = null;
|
||||
|
||||
// value computed after setup is done.
|
||||
a.addCompleteHandler((int v) { afterA1 = v; });
|
||||
a.complete(3);
|
||||
a.addCompleteHandler((int v) { afterA2 = v; });
|
||||
a.addCompleteHandler((int v) { afterA3 = v; });
|
||||
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(true, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(null, a.error);
|
||||
Expect.equals(3, a.value);
|
||||
Expect.equals(3, afterA1);
|
||||
Expect.equals(3, afterA2);
|
||||
Expect.equals(3, afterA3);
|
||||
}
|
||||
|
||||
static void testError() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
a.fail("Err");
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(true, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals("Err", a.error);
|
||||
readValueThrowsException_(a, a.error);
|
||||
}
|
||||
|
||||
static void testErrorWithHandler() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Promise<int> b = new Promise<int>();
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(false, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
readValueThrowsException_(b);
|
||||
readErrorThrowsException_(b);
|
||||
String afterA = null;
|
||||
String afterB = null;
|
||||
|
||||
// error after set up is done
|
||||
a.addErrorHandler((v) { afterA = v; });
|
||||
a.fail("ErrA");
|
||||
|
||||
// error before setup is done
|
||||
b.fail("ErrB");
|
||||
b.addErrorHandler((v) { afterB = v; });
|
||||
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(true, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(true, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
|
||||
Expect.equals("ErrA", a.error);
|
||||
Expect.equals("ErrB", b.error);
|
||||
Expect.equals("ErrA", afterA);
|
||||
Expect.equals("ErrB", afterB);
|
||||
|
||||
readValueThrowsException_(a, a.error);
|
||||
readValueThrowsException_(b, b.error);
|
||||
}
|
||||
|
||||
static void testCancel() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
a.cancel();
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(true, a.isCancelled());
|
||||
Expect.equals(null, a.value);
|
||||
Expect.equals(null, a.error);
|
||||
}
|
||||
|
||||
static void testCancelWithHandler() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Promise<int> b = new Promise<int>();
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(false, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
readValueThrowsException_(b);
|
||||
readErrorThrowsException_(b);
|
||||
bool aCancel = false;
|
||||
bool bCancel = false;
|
||||
|
||||
// cancel after setup
|
||||
a.addCancelHandler(() { aCancel = true; });
|
||||
a.cancel();
|
||||
|
||||
// cancel before setup is done
|
||||
b.cancel();
|
||||
b.addCancelHandler(() { bCancel = true; });
|
||||
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(true, a.isCancelled());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(true, b.isCancelled());
|
||||
Expect.equals(null, a.value);
|
||||
Expect.equals(null, b.value);
|
||||
Expect.equals(null, a.error);
|
||||
Expect.equals(null, b.error);
|
||||
Expect.equals(true, aCancel);
|
||||
Expect.equals(true, bCancel);
|
||||
|
||||
Promise<int> c = new Promise<int>();
|
||||
bool cCancel = false;
|
||||
c.cancel();
|
||||
c.complete(3);
|
||||
c.addCancelHandler(() { cCancel = true; });
|
||||
Expect.equals(true, cCancel);
|
||||
Expect.equals(true, c.isDone());
|
||||
Expect.equals(true, c.hasValue());
|
||||
Expect.equals(false, c.hasError());
|
||||
Expect.equals(true, c.isCancelled());
|
||||
|
||||
Promise<int> d = new Promise<int>();
|
||||
bool dCancel = false;
|
||||
d.cancel();
|
||||
d.fail("fail");
|
||||
d.addCancelHandler(() { dCancel = true; });
|
||||
Expect.equals(true, dCancel);
|
||||
Expect.equals(true, d.isDone());
|
||||
Expect.equals(false, d.hasValue());
|
||||
Expect.equals(true, d.hasError());
|
||||
Expect.equals(true, d.isCancelled());
|
||||
}
|
||||
|
||||
static void testChainComplete() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Promise<int> b = a.then((int ares) => ares + 1);
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(false, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
readValueThrowsException_(b);
|
||||
readErrorThrowsException_(b);
|
||||
int resA = null;
|
||||
a.addCompleteHandler((int v) { resA = v; });
|
||||
int resB = null;
|
||||
b.addCompleteHandler((int v) { resB = v; });
|
||||
|
||||
a.complete(3);
|
||||
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(true, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(true, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
|
||||
Expect.equals(3, a.value);
|
||||
Expect.equals(4, b.value);
|
||||
Expect.equals(null, a.error);
|
||||
Expect.equals(null, b.error);
|
||||
Expect.equals(3, resA);
|
||||
Expect.equals(4, resB);
|
||||
}
|
||||
|
||||
static void testChainError() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Promise<int> b = a.then((int ares) => ares + 1);
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(false, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
readValueThrowsException_(b);
|
||||
readErrorThrowsException_(b);
|
||||
String errA = null;
|
||||
a.addErrorHandler((e) { errA = e; });
|
||||
String errB = null;
|
||||
b.addErrorHandler((e) { errB = e; });
|
||||
|
||||
a.fail("err-from-a");
|
||||
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(true, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(true, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
|
||||
Expect.equals("err-from-a", a.error);
|
||||
Expect.equals("err-from-a", b.error);
|
||||
Expect.equals("err-from-a", errA);
|
||||
Expect.equals("err-from-a", errB);
|
||||
|
||||
readValueThrowsException_(a, a.error);
|
||||
readValueThrowsException_(b, b.error);
|
||||
}
|
||||
|
||||
static void testChainCancel() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Promise<int> b = a.then((int ares) => ares + 1);
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(false, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
readValueThrowsException_(b);
|
||||
readErrorThrowsException_(b);
|
||||
bool bCancel = false;
|
||||
b.addCancelHandler(() { bCancel = true; });
|
||||
bool bError = false;
|
||||
b.addErrorHandler((e) { bError = true; });
|
||||
bool aCancel = false;
|
||||
a.addCancelHandler(() { aCancel = true; });
|
||||
|
||||
a.cancel();
|
||||
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(true, a.isCancelled());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(true, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
Expect.equals(null, a.value);
|
||||
Expect.equals(null, a.error);
|
||||
Expect.equals("Source promise was cancelled", b.error);
|
||||
readValueThrowsException_(b, b.error);
|
||||
Expect.equals(false, bCancel);
|
||||
Expect.equals(true, bError);
|
||||
Expect.equals(true, aCancel);
|
||||
}
|
||||
|
||||
static void testFlatten() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Promise<Promise<int>> b = new Promise<Promise<int>>();
|
||||
Promise<Promise<Promise<int>>> c = new Promise<Promise<Promise<int>>>();
|
||||
Promise<Promise<Promise<Promise<int>>>> d =
|
||||
new Promise<Promise<Promise<Promise<int>>>>();
|
||||
Promise<int> flat = d.flatten();
|
||||
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, b.isDone());
|
||||
Expect.equals(false, c.isDone());
|
||||
Expect.equals(false, d.isDone());
|
||||
Expect.equals(false, flat.isDone());
|
||||
readValueThrowsException_(a);
|
||||
readValueThrowsException_(b);
|
||||
readValueThrowsException_(c);
|
||||
readValueThrowsException_(d);
|
||||
readValueThrowsException_(flat);
|
||||
|
||||
b.complete(a);
|
||||
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(false, c.isDone());
|
||||
Expect.equals(false, d.isDone());
|
||||
Expect.equals(false, flat.isDone());
|
||||
readValueThrowsException_(a);
|
||||
Expect.equals(a, b.value);
|
||||
readValueThrowsException_(c);
|
||||
readValueThrowsException_(d);
|
||||
readValueThrowsException_(flat);
|
||||
|
||||
d.complete(c);
|
||||
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(false, c.isDone());
|
||||
Expect.equals(true, d.isDone());
|
||||
Expect.equals(false, flat.isDone());
|
||||
readValueThrowsException_(a);
|
||||
Expect.equals(a, b.value);
|
||||
readValueThrowsException_(c);
|
||||
Expect.equals(c, d.value);
|
||||
readValueThrowsException_(flat);
|
||||
|
||||
a.complete(2);
|
||||
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(false, c.isDone());
|
||||
Expect.equals(true, d.isDone());
|
||||
Expect.equals(false, flat.isDone());
|
||||
Expect.equals(2, a.value);
|
||||
Expect.equals(a, b.value);
|
||||
readValueThrowsException_(c);
|
||||
Expect.equals(c, d.value);
|
||||
readValueThrowsException_(flat);
|
||||
|
||||
c.complete(b);
|
||||
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(true, c.isDone());
|
||||
Expect.equals(true, d.isDone());
|
||||
Expect.equals(true, flat.isDone());
|
||||
Expect.equals(2, a.value);
|
||||
Expect.equals(a, b.value);
|
||||
Expect.equals(b, c.value);
|
||||
Expect.equals(c, d.value);
|
||||
Expect.equals(2, flat.value);
|
||||
}
|
||||
|
||||
static void testJoinSelectSecond() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Promise<int> b = new Promise<int>();
|
||||
Promise<int> c = new Promise<int>();
|
||||
Promise<int> second = new Promise<int>();
|
||||
bool first = true;
|
||||
second.join([a, b, c], (p) {
|
||||
if (first == true) {
|
||||
first = false;
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, b.isDone());
|
||||
Expect.equals(false, c.isDone());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
readValueThrowsException_(b);
|
||||
readErrorThrowsException_(b);
|
||||
readValueThrowsException_(c);
|
||||
readErrorThrowsException_(c);
|
||||
Expect.equals(false, second.isDone());
|
||||
readValueThrowsException_(second);
|
||||
readErrorThrowsException_(second);
|
||||
|
||||
b.complete(2);
|
||||
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(false, c.isDone());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
Expect.equals(2, b.value);
|
||||
Expect.equals(null, b.error);
|
||||
readValueThrowsException_(c);
|
||||
readErrorThrowsException_(c);
|
||||
Expect.equals(false, second.isDone());
|
||||
readValueThrowsException_(second);
|
||||
readErrorThrowsException_(second);
|
||||
|
||||
c.complete(3);
|
||||
|
||||
Expect.equals(true, second.isDone());
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(true, c.isDone());
|
||||
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
Expect.equals(2, b.value);
|
||||
Expect.equals(null, b.error);
|
||||
Expect.equals(3, c.value);
|
||||
Expect.equals(null, c.error);
|
||||
Expect.equals(3, second.value);
|
||||
}
|
||||
|
||||
static void testWaitFor1() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Promise<int> b = new Promise<int>();
|
||||
Promise<int> c = new Promise<int>();
|
||||
Promise<int> first = new Promise<int>();
|
||||
first.waitFor([a, b, c], 1);
|
||||
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(false, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
Expect.equals(false, c.isDone());
|
||||
Expect.equals(false, c.hasValue());
|
||||
Expect.equals(false, c.hasError());
|
||||
Expect.equals(false, c.isCancelled());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
readValueThrowsException_(b);
|
||||
readErrorThrowsException_(b);
|
||||
readValueThrowsException_(c);
|
||||
readErrorThrowsException_(c);
|
||||
Expect.equals(false, first.isDone());
|
||||
|
||||
b.complete(2);
|
||||
|
||||
// a & c got cancelled
|
||||
Expect.equals(true, first.isDone());
|
||||
Expect.equals(true, first.hasValue());
|
||||
Expect.equals(false, first.hasError());
|
||||
Expect.equals(false, first.isCancelled());
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(true, a.isCancelled());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(true, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
Expect.equals(true, c.isDone());
|
||||
Expect.equals(false, c.hasValue());
|
||||
Expect.equals(false, c.hasError());
|
||||
Expect.equals(true, c.isCancelled());
|
||||
|
||||
Expect.equals(null, a.value);
|
||||
Expect.equals(2, b.value);
|
||||
Expect.equals(null, c.value);
|
||||
Expect.equals(null, a.error);
|
||||
Expect.equals(null, b.error);
|
||||
Expect.equals(null, c.error);
|
||||
Expect.equals(2, first.value);
|
||||
}
|
||||
|
||||
static void testWaitForAll() {
|
||||
Promise<int> a = new Promise<int>();
|
||||
Promise<int> b = new Promise<int>();
|
||||
Promise<int> c = new Promise<int>();
|
||||
Promise<int> all = new Promise<int>();
|
||||
all.waitFor([a, b, c], 3);
|
||||
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(false, b.isDone());
|
||||
Expect.equals(false, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
Expect.equals(false, c.isDone());
|
||||
Expect.equals(false, c.hasValue());
|
||||
Expect.equals(false, c.hasError());
|
||||
Expect.equals(false, c.isCancelled());
|
||||
Expect.equals(false, all.isDone());
|
||||
Expect.equals(false, all.hasValue());
|
||||
Expect.equals(false, all.hasError());
|
||||
Expect.equals(false, all.isCancelled());
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
readValueThrowsException_(b);
|
||||
readErrorThrowsException_(b);
|
||||
readValueThrowsException_(c);
|
||||
readErrorThrowsException_(c);
|
||||
readValueThrowsException_(all);
|
||||
readErrorThrowsException_(all);
|
||||
|
||||
b.complete(2);
|
||||
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(true, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
Expect.equals(false, c.isDone());
|
||||
Expect.equals(false, c.hasValue());
|
||||
Expect.equals(false, c.hasError());
|
||||
Expect.equals(false, c.isCancelled());
|
||||
Expect.equals(false, all.isDone());
|
||||
Expect.equals(false, all.hasValue());
|
||||
Expect.equals(false, all.hasError());
|
||||
Expect.equals(false, all.isCancelled());
|
||||
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
Expect.equals(2, b.value);
|
||||
Expect.equals(null, b.error);
|
||||
readValueThrowsException_(c);
|
||||
readErrorThrowsException_(c);
|
||||
readValueThrowsException_(all);
|
||||
readErrorThrowsException_(all);
|
||||
|
||||
c.complete(3);
|
||||
|
||||
Expect.equals(false, a.isDone());
|
||||
Expect.equals(false, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(true, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
Expect.equals(true, c.isDone());
|
||||
Expect.equals(true, c.hasValue());
|
||||
Expect.equals(false, c.hasError());
|
||||
Expect.equals(false, c.isCancelled());
|
||||
Expect.equals(false, all.isDone());
|
||||
Expect.equals(false, all.hasValue());
|
||||
Expect.equals(false, all.hasError());
|
||||
Expect.equals(false, all.isCancelled());
|
||||
|
||||
readValueThrowsException_(a);
|
||||
readErrorThrowsException_(a);
|
||||
Expect.equals(2, b.value);
|
||||
Expect.equals(null, b.error);
|
||||
Expect.equals(3, c.value);
|
||||
Expect.equals(null, c.error);
|
||||
readValueThrowsException_(all);
|
||||
readErrorThrowsException_(all);
|
||||
|
||||
a.complete(1);
|
||||
|
||||
Expect.equals(true, a.isDone());
|
||||
Expect.equals(true, a.hasValue());
|
||||
Expect.equals(false, a.hasError());
|
||||
Expect.equals(false, a.isCancelled());
|
||||
Expect.equals(true, b.isDone());
|
||||
Expect.equals(true, b.hasValue());
|
||||
Expect.equals(false, b.hasError());
|
||||
Expect.equals(false, b.isCancelled());
|
||||
Expect.equals(true, c.isDone());
|
||||
Expect.equals(true, c.hasValue());
|
||||
Expect.equals(false, c.hasError());
|
||||
Expect.equals(false, c.isCancelled());
|
||||
Expect.equals(true, all.isDone());
|
||||
Expect.equals(true, all.hasValue());
|
||||
Expect.equals(false, all.hasError());
|
||||
Expect.equals(false, all.isCancelled());
|
||||
|
||||
Expect.equals(1, a.value);
|
||||
Expect.equals(null, a.error);
|
||||
Expect.equals(2, b.value);
|
||||
Expect.equals(null, b.error);
|
||||
Expect.equals(3, c.value);
|
||||
Expect.equals(null, c.error);
|
||||
Expect.equals(1, all.value);
|
||||
Expect.equals(null, all.error);
|
||||
}
|
||||
|
||||
static void readValueThrowsException_(Promise p, [var error = null]) {
|
||||
bool errorFound = false;
|
||||
try {
|
||||
var x = p.value;
|
||||
} catch (var e) {
|
||||
errorFound = true;
|
||||
if (error !== null) {
|
||||
Expect.equals(true, error === e);
|
||||
}
|
||||
}
|
||||
Expect.equals(true, errorFound);
|
||||
}
|
||||
|
||||
static void readErrorThrowsException_(Promise p) {
|
||||
bool errorFound = false;
|
||||
try {
|
||||
var x = p.error;
|
||||
} catch (var e) {
|
||||
errorFound = true;
|
||||
}
|
||||
Expect.equals(true, errorFound);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
PromiseTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
// 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.
|
||||
|
||||
class QueueIteratorTest {
|
||||
static testMain() {
|
||||
testSmallQueue();
|
||||
testLargeQueue();
|
||||
testEmptyQueue();
|
||||
}
|
||||
|
||||
static void testThrows(Iterator<int> it) {
|
||||
Expect.equals(false, it.hasNext());
|
||||
var exception = null;
|
||||
try {
|
||||
it.next();
|
||||
} catch (NoMoreElementsException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
}
|
||||
|
||||
static int sum(int expected, Iterator<int> it) {
|
||||
int count = 0;
|
||||
while (it.hasNext()) {
|
||||
count += it.next();
|
||||
}
|
||||
Expect.equals(expected, count);
|
||||
}
|
||||
|
||||
static void testSmallQueue() {
|
||||
Queue<int> queue = new Queue<int>();
|
||||
queue.addLast(1);
|
||||
queue.addLast(2);
|
||||
queue.addLast(3);
|
||||
|
||||
Iterator<int> it = queue.iterator();
|
||||
Expect.equals(true, it.hasNext());
|
||||
sum(6, it);
|
||||
testThrows(it);
|
||||
}
|
||||
|
||||
static void testLargeQueue() {
|
||||
Queue<int> queue = new Queue<int>();
|
||||
int count = 0;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
count += i;
|
||||
queue.addLast(i);
|
||||
}
|
||||
Iterator<int> it = queue.iterator();
|
||||
Expect.equals(true, it.hasNext());
|
||||
sum(count, it);
|
||||
testThrows(it);
|
||||
}
|
||||
|
||||
static void testEmptyQueue() {
|
||||
Queue<int> queue = new Queue<int>();
|
||||
Iterator<int> it = queue.iterator();
|
||||
Expect.equals(false, it.hasNext());
|
||||
sum(0, it);
|
||||
testThrows(it);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
QueueIteratorTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
// 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.
|
||||
// VMOptions=--expose_core_impl
|
||||
|
||||
class QueueTest {
|
||||
|
||||
static testMain() {
|
||||
Queue queue = new Queue();
|
||||
checkQueue(queue, 0, 0);
|
||||
|
||||
queue.addFirst(1);
|
||||
checkQueue(queue, 1, 1);
|
||||
|
||||
queue.addLast(10);
|
||||
checkQueue(queue, 2, 11);
|
||||
|
||||
Expect.equals(10, queue.removeLast());
|
||||
checkQueue(queue, 1, 1);
|
||||
|
||||
queue.addLast(10);
|
||||
Expect.equals(1, queue.removeFirst());
|
||||
checkQueue(queue, 1, 10);
|
||||
|
||||
queue.addFirst(1);
|
||||
queue.addLast(100);
|
||||
queue.addLast(1000);
|
||||
Expect.equals(1000, queue.removeLast());
|
||||
queue.addLast(1000);
|
||||
checkQueue(queue, 4, 1111);
|
||||
|
||||
queue.removeFirst();
|
||||
checkQueue(queue, 3, 1110);
|
||||
|
||||
bool is10(int value) {
|
||||
return (value == 10);
|
||||
}
|
||||
|
||||
Queue other = queue.filter(is10);
|
||||
checkQueue(other, 1, 10);
|
||||
|
||||
Expect.equals(true, queue.some(is10));
|
||||
|
||||
bool isInstanceOfInt(int value) {
|
||||
return (value is int);
|
||||
}
|
||||
|
||||
Expect.equals(true, queue.every(isInstanceOfInt));
|
||||
|
||||
Expect.equals(false, queue.every(is10));
|
||||
|
||||
bool is1(int value) {
|
||||
return (value == 1);
|
||||
}
|
||||
Expect.equals(false, queue.some(is1));
|
||||
|
||||
queue.clear();
|
||||
Expect.equals(0, queue.length);
|
||||
|
||||
var exception = null;
|
||||
try {
|
||||
queue.removeFirst();
|
||||
} catch (EmptyQueueException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
Expect.equals(0, queue.length);
|
||||
|
||||
exception = null;
|
||||
try {
|
||||
queue.removeLast();
|
||||
} catch (EmptyQueueException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
Expect.equals(0, queue.length);
|
||||
|
||||
queue.addFirst(1);
|
||||
queue.addFirst(2);
|
||||
Expect.equals(2, queue.first());
|
||||
Expect.equals(1, queue.last());
|
||||
|
||||
queue.addLast(3);
|
||||
Expect.equals(3, queue.last());
|
||||
bool isGreaterThanOne(int value) {
|
||||
return (value > 1);
|
||||
}
|
||||
|
||||
other = queue.filter(isGreaterThanOne);
|
||||
checkQueue(other, 2, 5);
|
||||
|
||||
testAddAll();
|
||||
}
|
||||
|
||||
static void checkQueue(Queue queue, int expectedSize, int expectedSum) {
|
||||
Expect.equals(expectedSize, queue.length);
|
||||
int sum = 0;
|
||||
void sumElements(int value) {
|
||||
sum += value;
|
||||
}
|
||||
queue.forEach(sumElements);
|
||||
Expect.equals(expectedSum, sum);
|
||||
}
|
||||
|
||||
static testAddAll() {
|
||||
Set<int> set = new Set<int>.from([1, 2, 4]);
|
||||
|
||||
Queue<int> queue1 = new Queue<int>.from(set);
|
||||
Queue<int> queue2 = new Queue<int>();
|
||||
Queue<int> queue3 = new Queue<int>();
|
||||
|
||||
queue2.addAll(set);
|
||||
queue3.addAll(queue1);
|
||||
|
||||
Expect.equals(3, set.length);
|
||||
Expect.equals(3, queue1.length);
|
||||
Expect.equals(3, queue2.length);
|
||||
Expect.equals(3, queue3.length);
|
||||
|
||||
int sum = 0;
|
||||
void f(e) { sum += e; };
|
||||
|
||||
set.forEach(f);
|
||||
Expect.equals(7, sum);
|
||||
sum = 0;
|
||||
|
||||
queue1.forEach(f);
|
||||
Expect.equals(7, sum);
|
||||
sum = 0;
|
||||
|
||||
queue2.forEach(f);
|
||||
Expect.equals(7, sum);
|
||||
sum = 0;
|
||||
|
||||
queue3.forEach(f);
|
||||
Expect.equals(7, sum);
|
||||
sum = 0;
|
||||
|
||||
set = new Set<int>.from([]);
|
||||
queue1 = new Queue<int>.from(set);
|
||||
queue2 = new Queue<int>();
|
||||
queue3 = new Queue<int>();
|
||||
|
||||
queue2.addAll(set);
|
||||
queue3.addAll(queue1);
|
||||
|
||||
Expect.equals(0, set.length);
|
||||
Expect.equals(0, queue1.length);
|
||||
Expect.equals(0, queue2.length);
|
||||
Expect.equals(0, queue3.length);
|
||||
|
||||
testQueueElements();
|
||||
}
|
||||
|
||||
static testQueueElements() {
|
||||
Queue<int> queue1 = new DoubleLinkedQueue<int>.from([1, 2, 4]);
|
||||
Queue<int> queue2 = new DoubleLinkedQueue<int>();
|
||||
queue2.addAll(queue1);
|
||||
|
||||
Expect.equals(queue1.length, queue2.length);
|
||||
DoubleLinkedQueueEntry<int> entry1 = queue1.firstEntry();
|
||||
DoubleLinkedQueueEntry<int> entry2 = queue2.firstEntry();
|
||||
while (entry1 != null) {
|
||||
Expect.equals(true, entry1 !== entry2);
|
||||
entry1 = entry1.nextEntry();
|
||||
entry2 = entry2.nextEntry();
|
||||
}
|
||||
Expect.equals(null, entry2);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
QueueTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for RegExp.allMatches.
|
||||
|
||||
class RegExpAllMatchesTest {
|
||||
static testIterator() {
|
||||
var matches = new RegExp("foo", "").allMatches("foo foo");
|
||||
Iterator it = matches.iterator();
|
||||
Expect.equals(true, it.hasNext());
|
||||
Expect.equals('foo', it.next().group(0));
|
||||
Expect.equals(true, it.hasNext());
|
||||
Expect.equals('foo', it.next().group(0));
|
||||
Expect.equals(false, it.hasNext());
|
||||
|
||||
// Run two iterators over the same results.
|
||||
it = matches.iterator();
|
||||
Iterator it2 = matches.iterator();
|
||||
Expect.equals(true, it.hasNext());
|
||||
Expect.equals(true, it2.hasNext());
|
||||
Expect.equals('foo', it.next().group(0));
|
||||
Expect.equals('foo', it2.next().group(0));
|
||||
Expect.equals(true, it.hasNext());
|
||||
Expect.equals(true, it2.hasNext());
|
||||
Expect.equals('foo', it.next().group(0));
|
||||
Expect.equals('foo', it2.next().group(0));
|
||||
Expect.equals(false, it.hasNext());
|
||||
Expect.equals(false, it2.hasNext());
|
||||
}
|
||||
|
||||
static testForEach() {
|
||||
var matches = new RegExp("foo", "").allMatches("foo foo");
|
||||
var str = "";
|
||||
matches.forEach((Match m) {
|
||||
str += m.group(0);
|
||||
});
|
||||
Expect.equals("foofoo", str);
|
||||
}
|
||||
|
||||
static testFilter() {
|
||||
var matches = new RegExp("foo?", "").allMatches("foo fo foo fo");
|
||||
var filtered = matches.filter((Match m) {
|
||||
return m.group(0) == 'foo';
|
||||
});
|
||||
Expect.equals(2, filtered.length);
|
||||
var str = "";
|
||||
for (Match m in filtered) {
|
||||
str += m.group(0);
|
||||
}
|
||||
Expect.equals("foofoo", str);
|
||||
}
|
||||
|
||||
static testEvery() {
|
||||
var matches = new RegExp("foo?", "").allMatches("foo fo foo fo");
|
||||
Expect.equals(true, matches.every((Match m) {
|
||||
return m.group(0).startsWith("fo");
|
||||
}));
|
||||
Expect.equals(false, matches.every((Match m) {
|
||||
return m.group(0).startsWith("foo");
|
||||
}));
|
||||
}
|
||||
|
||||
static testSome() {
|
||||
var matches = new RegExp("foo?", "").allMatches("foo fo foo fo");
|
||||
Expect.equals(true, matches.some((Match m) {
|
||||
return m.group(0).startsWith("fo");
|
||||
}));
|
||||
Expect.equals(true, matches.some((Match m) {
|
||||
return m.group(0).startsWith("foo");
|
||||
}));
|
||||
Expect.equals(false, matches.some((Match m) {
|
||||
return m.group(0).startsWith("fooo");
|
||||
}));
|
||||
}
|
||||
|
||||
static testIsEmpty() {
|
||||
var matches = new RegExp("foo?", "").allMatches("foo fo foo fo");
|
||||
Expect.equals(false, matches.isEmpty());
|
||||
matches = new RegExp("fooo", "").allMatches("foo fo foo fo");
|
||||
Expect.equals(true, matches.isEmpty());
|
||||
}
|
||||
|
||||
static testGetCount() {
|
||||
var matches = new RegExp("foo?", "").allMatches("foo fo foo fo");
|
||||
Expect.equals(4, matches.length);
|
||||
matches = new RegExp("fooo", "").allMatches("foo fo foo fo");
|
||||
Expect.equals(0, matches.length);
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
testIterator();
|
||||
testForEach();
|
||||
testFilter();
|
||||
testEvery();
|
||||
testSome();
|
||||
testIsEmpty();
|
||||
testGetCount();
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
RegExpAllMatchesTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for RegExp.firstMatch.
|
||||
|
||||
class RegExpFirstMatchTest {
|
||||
static testMain() {
|
||||
Expect.equals('cat', new RegExp("(\\w+)", "").firstMatch("cat dog")[0]);
|
||||
Expect.equals(null, new RegExp("foo", "").firstMatch("bar"));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
RegExpFirstMatchTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for RegExp.group.
|
||||
|
||||
class RegExpGroupTest {
|
||||
static testMain() {
|
||||
var match = new RegExp("(a(b)((c|de)+))", "").firstMatch("abcde");
|
||||
Expect.equals('abcde', match.group(0));
|
||||
Expect.equals('abcde', match.group(1));
|
||||
Expect.equals('b', match.group(2));
|
||||
Expect.equals('cde', match[3]);
|
||||
Expect.equals('de', match[4]);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
RegExpGroupTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for RegExp.groups.
|
||||
|
||||
class RegExpGroupsTest {
|
||||
static testMain() {
|
||||
var match = new RegExp("(a(b)((c|de)+))", "").firstMatch("abcde");
|
||||
var groups = match.groups([0, 4, 2, 3]);
|
||||
Expect.equals('abcde', groups[0]);
|
||||
Expect.equals('de', groups[1]);
|
||||
Expect.equals('b', groups[2]);
|
||||
Expect.equals('cde', groups[3]);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
RegExpGroupsTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for RegExp.hasMatch.
|
||||
|
||||
class RegExpHasMatchTest {
|
||||
static testMain() {
|
||||
Expect.equals(false, new RegExp("bar", "").hasMatch("foo"));
|
||||
Expect.equals(true, new RegExp("bar|foo", "").hasMatch("foo"));
|
||||
Expect.equals(true, new RegExp("o+", "").hasMatch("foo"));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
RegExpHasMatchTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// 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.
|
||||
|
||||
main() {
|
||||
var matches =
|
||||
new RegExp("(a(b)((c|de)+))", "").allMatches("abcde abcde abcde");
|
||||
var it = matches.iterator();
|
||||
int start = 0;
|
||||
int end = 5;
|
||||
while (it.hasNext()) {
|
||||
Match match = it.next();
|
||||
Expect.equals(start, match.start());
|
||||
Expect.equals(end, match.end());
|
||||
start += 6;
|
||||
end += 6;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for RegExp.stringMatch.
|
||||
|
||||
class RegExpStringMatchTest {
|
||||
static testMain() {
|
||||
Expect.equals('cat', new RegExp("(\\w+)", "").stringMatch("cat dog"));
|
||||
Expect.equals(null, new RegExp("foo", "").stringMatch("bar"));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
RegExpStringMatchTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
// 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.
|
||||
|
||||
class FixedHashCode implements Hashable {
|
||||
final int _hashCode;
|
||||
const FixedHashCode(this._hashCode);
|
||||
int hashCode() { return _hashCode; }
|
||||
}
|
||||
|
||||
class SetIteratorTest {
|
||||
static testMain() {
|
||||
testSmallSet();
|
||||
testLargeSet();
|
||||
testEmptySet();
|
||||
testSetWithDeletedEntries();
|
||||
testBug5116829();
|
||||
testDifferentSizes();
|
||||
testDifferentHashCodes();
|
||||
}
|
||||
|
||||
static void testThrows(Iterator<int> it) {
|
||||
Expect.equals(false, it.hasNext());
|
||||
var exception = null;
|
||||
try {
|
||||
it.next();
|
||||
} catch (NoMoreElementsException e) {
|
||||
exception = e;
|
||||
}
|
||||
Expect.equals(true, exception != null);
|
||||
}
|
||||
|
||||
static int sum(int expected, Iterator<int> it) {
|
||||
int count = 0;
|
||||
while (it.hasNext()) {
|
||||
count += it.next();
|
||||
}
|
||||
Expect.equals(expected, count);
|
||||
}
|
||||
|
||||
static void testSmallSet() {
|
||||
Set<int> set = new Set<int>();
|
||||
set.add(1);
|
||||
set.add(2);
|
||||
set.add(3);
|
||||
|
||||
Iterator<int> it = set.iterator();
|
||||
Expect.equals(true, it.hasNext());
|
||||
sum(6, it);
|
||||
testThrows(it);
|
||||
}
|
||||
|
||||
static void testLargeSet() {
|
||||
Set<int> set = new Set<int>();
|
||||
int count = 0;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
count += i;
|
||||
set.add(i);
|
||||
}
|
||||
Iterator<int> it = set.iterator();
|
||||
Expect.equals(true, it.hasNext());
|
||||
sum(count, it);
|
||||
testThrows(it);
|
||||
}
|
||||
|
||||
static void testEmptySet() {
|
||||
Set<int> set = new Set<int>();
|
||||
Iterator<int> it = set.iterator();
|
||||
Expect.equals(false, it.hasNext());
|
||||
sum(0, it);
|
||||
testThrows(it);
|
||||
}
|
||||
|
||||
static void testSetWithDeletedEntries() {
|
||||
Set<int> set = new Set<int>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
set.add(i);
|
||||
}
|
||||
for (int i = 0; i < 100; i++) {
|
||||
set.remove(i);
|
||||
}
|
||||
Iterator<int> it = set.iterator();
|
||||
Expect.equals(false, it.hasNext());
|
||||
sum(0, it);
|
||||
testThrows(it);
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
set.add(i);
|
||||
if (i % 2 == 0) set.remove(i);
|
||||
else count += i;
|
||||
}
|
||||
it = set.iterator();
|
||||
Expect.equals(true, it.hasNext());
|
||||
sum(count, it);
|
||||
testThrows(it);
|
||||
}
|
||||
|
||||
static void testBug5116829() {
|
||||
// During iteration we skipped slot 0 of the hashset's key list. "A" was
|
||||
// hashed to slot 0 and therefore triggered the bug.
|
||||
Set<String> mystrs = new Set<String>();
|
||||
mystrs.add("A");
|
||||
int seen = 0;
|
||||
for (String elt in mystrs) {
|
||||
seen++;
|
||||
Expect.equals("A", elt);
|
||||
}
|
||||
Expect.equals(1, seen);
|
||||
}
|
||||
|
||||
static void testDifferentSizes() {
|
||||
for (int i = 1; i < 20; i++) {
|
||||
Set set = new Set();
|
||||
int sum = 0;
|
||||
for (int j = 0; j < i; j++) {
|
||||
set.add(j);
|
||||
sum += j;
|
||||
}
|
||||
int count = 0;
|
||||
int controlSum = 0;
|
||||
for (int x in set) {
|
||||
controlSum += x;
|
||||
count++;
|
||||
}
|
||||
Expect.equals(i, count);
|
||||
Expect.equals(sum, controlSum);
|
||||
}
|
||||
}
|
||||
|
||||
static void testDifferentHashCodes() {
|
||||
for (int i = -20; i < 20; i++) {
|
||||
Set set = new Set();
|
||||
var element = new FixedHashCode(i);
|
||||
set.add(element);
|
||||
Expect.equals(1, set.length);
|
||||
bool foundIt = false;
|
||||
for (var x in set) {
|
||||
foundIt = true;
|
||||
Expect.equals(true, x === element);
|
||||
}
|
||||
Expect.equals(true, foundIt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
SetIteratorTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
// 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.
|
||||
|
||||
class SetTest {
|
||||
|
||||
static testMain() {
|
||||
Set set = new Set();
|
||||
Expect.equals(0, set.length);
|
||||
set.add(1);
|
||||
Expect.equals(1, set.length);
|
||||
Expect.equals(true, set.contains(1));
|
||||
|
||||
set.add(1);
|
||||
Expect.equals(1, set.length);
|
||||
Expect.equals(true, set.contains(1));
|
||||
|
||||
set.remove(1);
|
||||
Expect.equals(0, set.length);
|
||||
Expect.equals(false, set.contains(1));
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
set.add(i);
|
||||
}
|
||||
|
||||
Expect.equals(10, set.length);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Expect.equals(true, set.contains(i));
|
||||
}
|
||||
|
||||
Expect.equals(10, set.length);
|
||||
|
||||
for (int i = 10; i < 20; i++) {
|
||||
Expect.equals(false, set.contains(i));
|
||||
}
|
||||
|
||||
// Test Set.forEach.
|
||||
int sum = 0;
|
||||
testForEach(int val) {
|
||||
sum += (val + 1);
|
||||
}
|
||||
|
||||
set.forEach(testForEach);
|
||||
Expect.equals(10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1, sum);
|
||||
|
||||
Expect.equals(true, set.isSubsetOf(set));
|
||||
Expect.equals(true, set.containsAll(set));
|
||||
|
||||
// Test Set.filter.
|
||||
testFilter(int val) {
|
||||
return val.isEven();
|
||||
}
|
||||
|
||||
Set filtered = set.filter(testFilter);
|
||||
Expect.equals(5, filtered.length);
|
||||
|
||||
Expect.equals(true, set.contains(0));
|
||||
Expect.equals(true, set.contains(2));
|
||||
Expect.equals(true, set.contains(4));
|
||||
Expect.equals(true, set.contains(6));
|
||||
Expect.equals(true, set.contains(8));
|
||||
|
||||
sum = 0;
|
||||
filtered.forEach(testForEach);
|
||||
Expect.equals(1 + 3 + 5 + 7 + 9, sum);
|
||||
|
||||
Expect.equals(true, set.containsAll(filtered));
|
||||
Expect.equals(true, filtered.isSubsetOf(set));
|
||||
|
||||
// Test Set.every.
|
||||
testEvery(int val) {
|
||||
return (val < 10);
|
||||
}
|
||||
|
||||
Expect.equals(true, set.every(testEvery));
|
||||
Expect.equals(true, filtered.every(testEvery));
|
||||
|
||||
filtered.add(10);
|
||||
Expect.equals(false, filtered.every(testEvery));
|
||||
|
||||
// Test Set.some.
|
||||
testSome(int val) {
|
||||
return (val == 4);
|
||||
}
|
||||
|
||||
Expect.equals(true, set.some(testSome));
|
||||
Expect.equals(true, filtered.some(testSome));
|
||||
filtered.remove(4);
|
||||
Expect.equals(false, filtered.some(testSome));
|
||||
|
||||
// Test Set.intersection.
|
||||
Set intersection = set.intersection(filtered);
|
||||
Expect.equals(true, set.contains(0));
|
||||
Expect.equals(true, set.contains(2));
|
||||
Expect.equals(true, set.contains(6));
|
||||
Expect.equals(true, set.contains(8));
|
||||
Expect.equals(false, intersection.contains(1));
|
||||
Expect.equals(false, intersection.contains(3));
|
||||
Expect.equals(false, intersection.contains(4));
|
||||
Expect.equals(false, intersection.contains(5));
|
||||
Expect.equals(false, intersection.contains(7));
|
||||
Expect.equals(false, intersection.contains(9));
|
||||
Expect.equals(false, intersection.contains(10));
|
||||
Expect.equals(4, intersection.length);
|
||||
|
||||
Expect.equals(true, set.containsAll(intersection));
|
||||
Expect.equals(true, filtered.containsAll(intersection));
|
||||
Expect.equals(true, intersection.isSubsetOf(set));
|
||||
Expect.equals(true, intersection.isSubsetOf(filtered));
|
||||
|
||||
// Test Set.addAll.
|
||||
List list = new List(10);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
list[i] = i + 10;
|
||||
}
|
||||
set.addAll(list);
|
||||
Expect.equals(20, set.length);
|
||||
for (int i = 0; i < 20; i++) {
|
||||
Expect.equals(true, set.contains(i));
|
||||
}
|
||||
|
||||
// Test Set.removeAll
|
||||
set.removeAll(list);
|
||||
Expect.equals(10, set.length);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Expect.equals(true, set.contains(i));
|
||||
}
|
||||
for (int i = 10; i < 20; i++) {
|
||||
Expect.equals(false, set.contains(i));
|
||||
}
|
||||
|
||||
// Test Set.clear.
|
||||
set.clear();
|
||||
Expect.equals(0, set.length);
|
||||
set.add(11);
|
||||
Expect.equals(1, set.length);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
SetTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
// 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.
|
||||
|
||||
class SortHelper {
|
||||
|
||||
SortHelper(this.sortFunction, this.compareFunction) {}
|
||||
|
||||
void run() {
|
||||
testSortIntLists();
|
||||
testSortDoubleLists();
|
||||
}
|
||||
|
||||
void printList(List a) {
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (i != 0) buffer.add(",");
|
||||
buffer.add(a[i]);
|
||||
}
|
||||
print("[$buffer]");
|
||||
}
|
||||
|
||||
void isSorted(List a) {
|
||||
for (int i = 1; i < a.length; i++) {
|
||||
if (compareFunction(a[i - 1], a[i]) > 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void testSortIntLists() {
|
||||
List a = new List(40);
|
||||
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = i;
|
||||
}
|
||||
testSort(a);
|
||||
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[a.length - i - 1] = i;
|
||||
}
|
||||
testSort(a);
|
||||
|
||||
for (int i = 0; i < 21; i++) {
|
||||
a[i] = 1;
|
||||
}
|
||||
for (int i = 21; i < a.length; i++) {
|
||||
a[i] = 2;
|
||||
}
|
||||
testSort(a);
|
||||
|
||||
// Same with bad pivot-choices.
|
||||
for (int i = 0; i < 21; i++) {
|
||||
a[i] = 1;
|
||||
}
|
||||
for (int i = 21; i < a.length; i++) {
|
||||
a[i] = 2;
|
||||
}
|
||||
a[6] = 1;
|
||||
a[13] = 1;
|
||||
a[19] = 1;
|
||||
a[25] = 1;
|
||||
a[33] = 2;
|
||||
testSort(a);
|
||||
|
||||
for (int i = 0; i < 21; i++) {
|
||||
a[i] = 2;
|
||||
}
|
||||
for (int i = 21; i < a.length; i++) {
|
||||
a[i] = 1;
|
||||
}
|
||||
testSort(a);
|
||||
|
||||
// Same with bad pivot-choices.
|
||||
for (int i = 0; i < 21; i++) {
|
||||
a[i] = 2;
|
||||
}
|
||||
for (int i = 21; i < a.length; i++) {
|
||||
a[i] = 1;
|
||||
}
|
||||
a[6] = 2;
|
||||
a[13] = 2;
|
||||
a[19] = 2;
|
||||
a[25] = 2;
|
||||
a[33] = 1;
|
||||
testSort(a);
|
||||
|
||||
var a2 = new List(0);
|
||||
testSort(a2);
|
||||
|
||||
var a3 = new List(1);
|
||||
a3[0] = 1;
|
||||
testSort(a3);
|
||||
|
||||
// --------
|
||||
// Test insertion sort.
|
||||
testInsertionSort(0, 1, 2, 3);
|
||||
testInsertionSort(0, 1, 3, 2);
|
||||
testInsertionSort(0, 3, 2, 1);
|
||||
testInsertionSort(0, 3, 1, 2);
|
||||
testInsertionSort(0, 2, 1, 3);
|
||||
testInsertionSort(0, 2, 3, 1);
|
||||
testInsertionSort(1, 0, 2, 3);
|
||||
testInsertionSort(1, 0, 3, 2);
|
||||
testInsertionSort(1, 2, 3, 0);
|
||||
testInsertionSort(1, 2, 0, 3);
|
||||
testInsertionSort(1, 3, 2, 0);
|
||||
testInsertionSort(1, 3, 0, 2);
|
||||
testInsertionSort(2, 0, 1, 3);
|
||||
testInsertionSort(2, 0, 3, 1);
|
||||
testInsertionSort(2, 1, 3, 0);
|
||||
testInsertionSort(2, 1, 0, 3);
|
||||
testInsertionSort(2, 3, 1, 0);
|
||||
testInsertionSort(2, 3, 0, 1);
|
||||
testInsertionSort(3, 0, 1, 2);
|
||||
testInsertionSort(3, 0, 2, 1);
|
||||
testInsertionSort(3, 1, 2, 0);
|
||||
testInsertionSort(3, 1, 0, 2);
|
||||
testInsertionSort(3, 2, 1, 0);
|
||||
testInsertionSort(3, 2, 0, 1);
|
||||
}
|
||||
|
||||
void testSort(List a) {
|
||||
printList(a);
|
||||
sortFunction(a);
|
||||
printList(a);
|
||||
bool sorted = isSorted(a);
|
||||
Expect.equals(true, sorted);
|
||||
print(sorted);
|
||||
}
|
||||
|
||||
void testInsertionSort(int i1, int i2, int i3, int i4) {
|
||||
var a = new List(4);
|
||||
a[0] = i1;
|
||||
a[1] = i2;
|
||||
a[2] = i3;
|
||||
a[3] = i4;
|
||||
testSort(a);
|
||||
}
|
||||
|
||||
void testSortDoubleLists() {
|
||||
List a = new List(40);
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = 1.0 * i + 0.5;
|
||||
}
|
||||
testSort(a);
|
||||
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = 1.0 * (a.length - i) + 0.5;
|
||||
}
|
||||
testSort(a);
|
||||
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = 1.5;
|
||||
}
|
||||
testSort(a);
|
||||
}
|
||||
|
||||
Function sortFunction;
|
||||
Function compareFunction;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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.
|
||||
|
||||
// Dart test for sort routines.
|
||||
// VMOptions=--expose_core_impl
|
||||
#source("SortHelper.dart");
|
||||
|
||||
class SortTest {
|
||||
|
||||
static void testMain() {
|
||||
var compare = (a, b) => a.compareTo(b);
|
||||
var sort = (list) => DualPivotQuicksort.sort(list, compare);
|
||||
new SortHelper(sort, compare).run();
|
||||
|
||||
compare = (a, b) => -a.compareTo(b);
|
||||
new SortHelper(sort, compare).run();
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
SortTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// 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.
|
||||
|
||||
// Dart test for Splaytrees.
|
||||
// VMOptions=--expose_core_impl
|
||||
|
||||
class SplayTreeTest {
|
||||
|
||||
static testMain() {
|
||||
SplayTree tree = new SplayTree();
|
||||
tree[1] = "first";
|
||||
tree[3] = "third";
|
||||
tree[5] = "fifth";
|
||||
tree[2] = "second";
|
||||
tree[4] = "fourth";
|
||||
|
||||
var correctSolution = ["first", "second", "third", "fourth", "fifth"];
|
||||
|
||||
tree.forEach((key, value) {
|
||||
Expect.equals(true, key >= 1);
|
||||
Expect.equals(true, key <= 5);
|
||||
Expect.equals(value, correctSolution[key - 1]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
SplayTreeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for testing stopwatch support.
|
||||
|
||||
class StopWatchTest {
|
||||
static bool checkTicking(StopWatch sw) {
|
||||
sw.start();
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
Math.parseInt(i.toString());
|
||||
if (sw.elapsed() > 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return sw.elapsed() > 0;
|
||||
}
|
||||
|
||||
static bool checkStopping(StopWatch sw) {
|
||||
sw.stop();
|
||||
int v1 = sw.elapsed();
|
||||
Expect.isTrue(v1 > 0); // Expect a non-zero elapsed time.
|
||||
StopWatch sw2 = new StopWatch(); // Used for verification.
|
||||
sw2.start();
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
Math.parseInt(i.toString());
|
||||
int v2 = sw.elapsed();
|
||||
if (v1 != v2) {
|
||||
return false;
|
||||
}
|
||||
v1 = v2;
|
||||
}
|
||||
// The test only makes sense if measureable time elapsed and elapsed time
|
||||
// on the stopped StopWatch did not increase.
|
||||
Expect.isTrue(sw2.elapsed() > 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
static checkRestart() {
|
||||
StopWatch sw = new StopWatch();
|
||||
sw.start();
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
Math.parseInt(i.toString());
|
||||
}
|
||||
sw.stop();
|
||||
int initial = sw.elapsed();
|
||||
sw.start();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
Math.parseInt(i.toString());
|
||||
}
|
||||
sw.stop();
|
||||
Expect.isTrue(sw.elapsed() >= initial);
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
StopWatch sw = new StopWatch();
|
||||
Expect.isTrue(checkTicking(sw));
|
||||
Expect.isTrue(checkStopping(sw));
|
||||
checkRestart();
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
StopWatchTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
// 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.
|
||||
|
||||
// TODO(srdjan): Move StringBuffer to visible names.
|
||||
|
||||
class StringBufferTest {
|
||||
static testConstructor() {
|
||||
StringBuffer bf = new StringBuffer("");
|
||||
Expect.equals(true, bf.isEmpty());
|
||||
|
||||
bf = new StringBuffer("abc");
|
||||
Expect.equals(3, bf.length);
|
||||
Expect.equals("abc", bf.toString());
|
||||
}
|
||||
|
||||
static testAdd() {
|
||||
StringBuffer bf = new StringBuffer("");
|
||||
Expect.equals(true, bf.isEmpty());
|
||||
|
||||
bf.add("a");
|
||||
Expect.equals(1, bf.length);
|
||||
Expect.equals("a", bf.toString());
|
||||
|
||||
bf = new StringBuffer("");
|
||||
bf.add("a");
|
||||
bf.add("b");
|
||||
Expect.equals("ab", bf.toString());
|
||||
|
||||
bf = new StringBuffer("abc");
|
||||
bf.add("d");
|
||||
bf.add("e");
|
||||
bf.add("f");
|
||||
bf.add("g");
|
||||
bf.add("h");
|
||||
bf.add("i");
|
||||
bf.add("j");
|
||||
bf.add("k");
|
||||
bf.add("l");
|
||||
bf.add("m");
|
||||
bf.add("n");
|
||||
bf.add("o");
|
||||
bf.add("p");
|
||||
bf.add("q");
|
||||
bf.add("r");
|
||||
bf.add("s");
|
||||
bf.add("t");
|
||||
bf.add("u");
|
||||
bf.add("v");
|
||||
bf.add("w");
|
||||
bf.add("x");
|
||||
bf.add("y");
|
||||
bf.add("z");
|
||||
bf.add("\n");
|
||||
bf.add("thequickbrownfoxjumpsoverthelazydog");
|
||||
Expect.equals("abcdefghijklmnopqrstuvwxyz\n" +
|
||||
"thequickbrownfoxjumpsoverthelazydog",
|
||||
bf.toString());
|
||||
|
||||
bf = new StringBuffer("");
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
bf.add('');
|
||||
bf.add("");
|
||||
}
|
||||
Expect.equals("", bf.toString());
|
||||
|
||||
Expect.equals(bf, bf.add("foo"));
|
||||
}
|
||||
|
||||
static testLength() {
|
||||
StringBuffer bf = new StringBuffer("");
|
||||
Expect.equals(0, bf.length);
|
||||
bf.add("foo");
|
||||
Expect.equals(3, bf.length);
|
||||
bf.add("bar");
|
||||
Expect.equals(6, bf.length);
|
||||
bf.add("");
|
||||
Expect.equals(6, bf.length);
|
||||
}
|
||||
|
||||
static testIsEmpty() {
|
||||
StringBuffer bf = new StringBuffer("");
|
||||
Expect.equals(true, bf.isEmpty());
|
||||
bf.add("foo");
|
||||
Expect.equals(false, bf.isEmpty());
|
||||
}
|
||||
|
||||
static testAddAll() {
|
||||
StringBuffer bf = new StringBuffer("");
|
||||
bf.addAll(["foo", "bar", "a", "b", "c"]);
|
||||
Expect.equals("foobarabc", bf.toString());
|
||||
|
||||
bf.addAll([]);
|
||||
Expect.equals("foobarabc", bf.toString());
|
||||
|
||||
bf.addAll(["", "", ""]);
|
||||
Expect.equals("foobarabc", bf.toString());
|
||||
|
||||
Expect.equals(bf, bf.addAll(["foo"]));
|
||||
}
|
||||
|
||||
static testClear() {
|
||||
StringBuffer bf = new StringBuffer("");
|
||||
bf.add("foo");
|
||||
bf.clear();
|
||||
Expect.equals("", bf.toString());
|
||||
Expect.equals(0, bf.length);
|
||||
|
||||
bf.add("bar");
|
||||
Expect.equals("bar", bf.toString());
|
||||
Expect.equals(3, bf.length);
|
||||
bf.clear();
|
||||
Expect.equals("", bf.toString());
|
||||
Expect.equals(0, bf.length);
|
||||
|
||||
Expect.equals(bf, bf.clear());
|
||||
}
|
||||
|
||||
static testToString() {
|
||||
StringBuffer bf = new StringBuffer("");
|
||||
Expect.equals("", bf.toString());
|
||||
|
||||
bf = new StringBuffer("foo");
|
||||
Expect.equals("foo", bf.toString());
|
||||
|
||||
bf = new StringBuffer("foo");
|
||||
bf.add("bar");
|
||||
Expect.equals("foobar", bf.toString());
|
||||
}
|
||||
|
||||
static testChaining() {
|
||||
StringBuffer bf = new StringBuffer("");
|
||||
StringBuffer bf2 = new StringBuffer("");
|
||||
bf2.add("bf2");
|
||||
bf.add("foo")
|
||||
.add("bar")
|
||||
.add(bf2)
|
||||
.add(bf2)
|
||||
.add("toto");
|
||||
Expect.equals("foobarbf2bf2toto", bf.toString());
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
testToString();
|
||||
testConstructor();
|
||||
testLength();
|
||||
testIsEmpty();
|
||||
testAdd();
|
||||
testAddAll();
|
||||
testClear();
|
||||
testChaining();
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
StringBufferTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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.
|
||||
|
||||
class StringCaseTest {
|
||||
|
||||
static testMain() {
|
||||
testLowerUpper();
|
||||
}
|
||||
|
||||
static testLowerUpper() {
|
||||
var a = "Stop! Smell the Roses.";
|
||||
var allLower = "stop! smell the roses.";
|
||||
var allUpper = "STOP! SMELL THE ROSES.";
|
||||
Expect.equals(allUpper, a.toUpperCase());
|
||||
Expect.equals(allLower, a.toLowerCase());
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
StringCaseTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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.
|
||||
|
||||
class StringFromListTest {
|
||||
static testMain() {
|
||||
Expect.equals("", new String.fromCharCodes(new List(0)));
|
||||
Expect.equals("", new String.fromCharCodes([]));
|
||||
Expect.equals("", new String.fromCharCodes(const []));
|
||||
Expect.equals("AB", new String.fromCharCodes([65, 66]));
|
||||
Expect.equals("AB", new String.fromCharCodes(const [65, 66]));
|
||||
Expect.equals("", new String.fromCharCodes(new List()));
|
||||
var a = new List();
|
||||
a.add(65);
|
||||
a.add(66);
|
||||
Expect.equals("AB", new String.fromCharCodes(a));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
StringFromListTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
// 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.
|
||||
// Dart test for testing String.allMatches.
|
||||
|
||||
String str = "this is a string with hello here and hello there";
|
||||
|
||||
main() {
|
||||
testNoMatch();
|
||||
testOneMatch();
|
||||
testTwoMatches();
|
||||
testEmptyPattern();
|
||||
testEmptyString();
|
||||
testEmptyPatternAndString();
|
||||
}
|
||||
|
||||
testNoMatch() {
|
||||
// Also tests that RegExp groups don't work.
|
||||
String helloPattern = "with (hello)";
|
||||
Iterable<Match> matches = helloPattern.allMatches(str);
|
||||
Expect.isFalse(matches.iterator().hasNext());
|
||||
}
|
||||
|
||||
testOneMatch() {
|
||||
String helloPattern = "with hello";
|
||||
Iterable<Match> matches = helloPattern.allMatches(str);
|
||||
var iterator = matches.iterator();
|
||||
Match match = iterator.next();
|
||||
Expect.isFalse(iterator.hasNext());
|
||||
Expect.equals(str.indexOf('with', 0), match.start());
|
||||
Expect.equals(str.indexOf('with', 0) + helloPattern.length, match.end());
|
||||
Expect.equals(helloPattern, match.pattern);
|
||||
Expect.equals(str, match.str);
|
||||
Expect.equals(helloPattern, match[0]);
|
||||
Expect.equals(0, match.groupCount());
|
||||
}
|
||||
|
||||
testTwoMatches() {
|
||||
String helloPattern = "hello";
|
||||
Iterable<Match> matches = helloPattern.allMatches(str);
|
||||
|
||||
int count = 0;
|
||||
int start = 0;
|
||||
for (var match in matches) {
|
||||
count++;
|
||||
Expect.equals(str.indexOf('hello', start), match.start());
|
||||
Expect.equals(
|
||||
str.indexOf('hello', start) + helloPattern.length, match.end());
|
||||
Expect.equals(helloPattern, match.pattern);
|
||||
Expect.equals(str, match.str);
|
||||
Expect.equals(helloPattern, match[0]);
|
||||
Expect.equals(0, match.groupCount());
|
||||
start = match.end();
|
||||
}
|
||||
Expect.equals(2, count);
|
||||
}
|
||||
|
||||
testEmptyPattern() {
|
||||
String pattern = "";
|
||||
Iterable<Match> matches = pattern.allMatches(str);
|
||||
Expect.isFalse(matches.iterator().hasNext());
|
||||
}
|
||||
|
||||
testEmptyString() {
|
||||
String pattern = "foo";
|
||||
String str = "";
|
||||
Iterable<Match> matches = pattern.allMatches(str);
|
||||
Expect.isFalse(matches.iterator().hasNext());
|
||||
}
|
||||
|
||||
testEmptyPatternAndString() {
|
||||
String pattern = "";
|
||||
String str = "";
|
||||
Iterable<Match> matches = pattern.allMatches(str);
|
||||
Expect.isFalse(matches.iterator().hasNext());
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// 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.
|
||||
|
||||
class StringReplaceTest {
|
||||
static testMain() {
|
||||
Expect.equals(
|
||||
"AtoBtoCDtoE", "AfromBtoCDtoE".replaceFirst("from", "to"));
|
||||
|
||||
// Test with the replaced string at the begining.
|
||||
Expect.equals(
|
||||
"toABtoCDtoE", "fromABtoCDtoE".replaceFirst("from", "to"));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals(
|
||||
"toABtoCDtoEto", "fromABtoCDtoEto".replaceFirst("from", "to"));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("ABC", "ABC".replaceFirst("from", "to"));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceFirst("from", "to"));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceFirst("from", "to"));
|
||||
|
||||
// Test when the string to change is the replaced string.
|
||||
Expect.equals("to", "from".replaceFirst("from", "to"));
|
||||
|
||||
// Test when the string to change is the replacement string.
|
||||
Expect.equals("to", "to".replaceFirst("from", "to"));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("", "from".replaceFirst("from", ""));
|
||||
Expect.equals("AB", "AfromB".replaceFirst("from", ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("to", "".replaceFirst("", "to"));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("toAtoBtoCto", "AtoBtoCto".replaceFirst("", "to"));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
StringReplaceTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// 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.
|
||||
|
||||
class StringSplitTest {
|
||||
static testMain() {
|
||||
var list = "a b c".split(" ");
|
||||
Expect.equals(3, list.length);
|
||||
Expect.equals("a", list[0]);
|
||||
Expect.equals("b", list[1]);
|
||||
Expect.equals("c", list[2]);
|
||||
|
||||
list = "adbdc".split("d");
|
||||
Expect.equals(3, list.length);
|
||||
Expect.equals("a", list[0]);
|
||||
Expect.equals("b", list[1]);
|
||||
Expect.equals("c", list[2]);
|
||||
|
||||
list = "addbddc".split("dd");
|
||||
Expect.equals(3, list.length);
|
||||
Expect.equals("a", list[0]);
|
||||
Expect.equals("b", list[1]);
|
||||
Expect.equals("c", list[2]);
|
||||
|
||||
list = "abc".split(" ");
|
||||
Expect.equals(1, list.length);
|
||||
Expect.equals("abc", list[0]);
|
||||
|
||||
list = "abc".split("");
|
||||
Expect.equals(3, list.length);
|
||||
Expect.equals("a", list[0]);
|
||||
Expect.equals("b", list[1]);
|
||||
Expect.equals("c", list[2]);
|
||||
|
||||
list = " ".split(" ");
|
||||
Expect.equals(4, list.length);
|
||||
Expect.equals("", list[0]);
|
||||
Expect.equals("", list[1]);
|
||||
Expect.equals("", list[2]);
|
||||
Expect.equals("", list[3]);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
StringSplitTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
// 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.
|
||||
|
||||
// TODO(ngeoffray): test String methods with null arguments.
|
||||
class StringTest {
|
||||
|
||||
static testMain() {
|
||||
testOutOfRange();
|
||||
testIllegalArgument();
|
||||
testConcat();
|
||||
testIndex();
|
||||
testCharCodeAt();
|
||||
testEquals();
|
||||
testEndsWith();
|
||||
testStartsWith();
|
||||
testIndexOf();
|
||||
testLastIndexOf();
|
||||
testContains();
|
||||
testReplaceAll();
|
||||
testCompareTo();
|
||||
testToList();
|
||||
testCharCodes();
|
||||
}
|
||||
|
||||
static void testOutOfRange() {
|
||||
String a = "Hello";
|
||||
bool exception_caught = false;
|
||||
try {
|
||||
var c = a[20]; // Throw exception.
|
||||
} catch (IndexOutOfRangeException e) {
|
||||
exception_caught = true;
|
||||
}
|
||||
Expect.equals(true, exception_caught);
|
||||
}
|
||||
|
||||
static testIllegalArgument() {
|
||||
String a = "Hello";
|
||||
bool exception_caught = false;
|
||||
try {
|
||||
var c = a[2.2]; // Throw exception.
|
||||
Expect.equals(true, false);
|
||||
} catch (IllegalArgumentException e) {
|
||||
exception_caught = true;
|
||||
} catch (TypeError e) { // Thrown in checked mode only.
|
||||
exception_caught = true;
|
||||
}
|
||||
Expect.equals(true, exception_caught);
|
||||
}
|
||||
|
||||
static testIndex() {
|
||||
String str = "string";
|
||||
for (int i = 0; i < str.length; i++) {
|
||||
Expect.equals(true, str[i] is String);
|
||||
Expect.equals(1, str[i].length);
|
||||
}
|
||||
}
|
||||
|
||||
static testCharCodeAt() {
|
||||
String str = "string";
|
||||
for (int i = 0; i < str.length; i++) {
|
||||
Expect.equals(true, str.charCodeAt(i) is int);
|
||||
}
|
||||
}
|
||||
|
||||
static testConcat() {
|
||||
var a = "One";
|
||||
var b = "Four";
|
||||
var c = a.concat(b);
|
||||
Expect.equals(7, c.length);
|
||||
Expect.equals("OneFour", c);
|
||||
}
|
||||
|
||||
static testEquals() {
|
||||
Expect.equals("str", "str");
|
||||
|
||||
Expect.equals("str", "s".concat("t").concat("r"));
|
||||
Expect.equals("s".concat("t").concat("r"), "str");
|
||||
|
||||
Expect.equals(false, "str" == "s");
|
||||
Expect.equals(false, "str" == "r");
|
||||
Expect.equals(false, "str" == "st");
|
||||
Expect.equals(false, "str" == "tr");
|
||||
|
||||
Expect.equals(false, "s" == "str");
|
||||
Expect.equals(false, "r" == "str");
|
||||
Expect.equals(false, "st" == "str");
|
||||
Expect.equals(false, "tr" == "str");
|
||||
|
||||
Expect.equals(false, "" == "s");
|
||||
Expect.equals("", "");
|
||||
}
|
||||
|
||||
static testEndsWith() {
|
||||
Expect.equals(true, "str".endsWith("r"));
|
||||
Expect.equals(true, "str".endsWith("tr"));
|
||||
Expect.equals(true, "str".endsWith("str"));
|
||||
|
||||
Expect.equals(false, "str".endsWith("stri"));
|
||||
Expect.equals(false, "str".endsWith("t"));
|
||||
Expect.equals(false, "str".endsWith("st"));
|
||||
Expect.equals(false, "str".endsWith("s"));
|
||||
|
||||
Expect.equals(true, "".endsWith(""));
|
||||
Expect.equals(false, "".endsWith("s"));
|
||||
}
|
||||
|
||||
static testStartsWith() {
|
||||
Expect.equals(true, "str".startsWith("s"));
|
||||
Expect.equals(true, "str".startsWith("st"));
|
||||
Expect.equals(true, "str".startsWith("str"));
|
||||
|
||||
Expect.equals(false, "str".startsWith("stri"));
|
||||
Expect.equals(false, "str".startsWith("r"));
|
||||
Expect.equals(false, "str".startsWith("tr"));
|
||||
Expect.equals(false, "str".startsWith("t"));
|
||||
|
||||
Expect.equals(true, "".startsWith(""));
|
||||
Expect.equals(false, "".startsWith("s"));
|
||||
}
|
||||
|
||||
static testIndexOf() {
|
||||
Expect.equals(0, "str".indexOf("", 0));
|
||||
Expect.equals(0, "".indexOf("", 0));
|
||||
Expect.equals(-1, "".indexOf("a", 0));
|
||||
|
||||
Expect.equals(1, "str".indexOf("t", 0));
|
||||
Expect.equals(1, "str".indexOf("tr", 0));
|
||||
Expect.equals(0, "str".indexOf("str", 0));
|
||||
Expect.equals(0, "str".indexOf("st", 0));
|
||||
Expect.equals(0, "str".indexOf("s", 0));
|
||||
Expect.equals(2, "str".indexOf("r", 0));
|
||||
Expect.equals(-1, "str".indexOf("string", 0));
|
||||
|
||||
Expect.equals(1, "strstr".indexOf("t", 0));
|
||||
Expect.equals(1, "strstr".indexOf("tr", 0));
|
||||
Expect.equals(0, "strstr".indexOf("str", 0));
|
||||
Expect.equals(0, "strstr".indexOf("st", 0));
|
||||
Expect.equals(0, "strstr".indexOf("s", 0));
|
||||
Expect.equals(2, "strstr".indexOf("r", 0));
|
||||
Expect.equals(-1, "str".indexOf("string", 0));
|
||||
|
||||
Expect.equals(4, "strstr".indexOf("t", 2));
|
||||
Expect.equals(4, "strstr".indexOf("tr", 2));
|
||||
Expect.equals(3, "strstr".indexOf("str", 1));
|
||||
Expect.equals(3, "strstr".indexOf("str", 2));
|
||||
Expect.equals(3, "strstr".indexOf("str", 3));
|
||||
Expect.equals(3, "strstr".indexOf("st", 1));
|
||||
Expect.equals(3, "strstr".indexOf("s", 3));
|
||||
Expect.equals(5, "strstr".indexOf("r", 3));
|
||||
Expect.equals(5, "strstr".indexOf("r", 4));
|
||||
Expect.equals(5, "strstr".indexOf("r", 5));
|
||||
|
||||
String str = "hello";
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int result = str.indexOf("", i);
|
||||
if (i > str.length) {
|
||||
Expect.equals(str.length, result);
|
||||
} else {
|
||||
Expect.equals(i, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static testLastIndexOf() {
|
||||
Expect.equals(2, "str".lastIndexOf("", 2));
|
||||
Expect.equals(0, "".lastIndexOf("", 0));
|
||||
Expect.equals(-1, "".lastIndexOf("a", 0));
|
||||
|
||||
Expect.equals(1, "str".lastIndexOf("t", 2));
|
||||
Expect.equals(1, "str".lastIndexOf("tr", 2));
|
||||
Expect.equals(0, "str".lastIndexOf("str", 2));
|
||||
Expect.equals(0, "str".lastIndexOf("st", 2));
|
||||
Expect.equals(0, "str".lastIndexOf("s", 2));
|
||||
Expect.equals(2, "str".lastIndexOf("r", 2));
|
||||
Expect.equals(-1, "str".lastIndexOf("string", 2));
|
||||
|
||||
Expect.equals(4, "strstr".lastIndexOf("t", 5));
|
||||
Expect.equals(4, "strstr".lastIndexOf("tr", 5));
|
||||
Expect.equals(3, "strstr".lastIndexOf("str", 5));
|
||||
Expect.equals(3, "strstr".lastIndexOf("st", 5));
|
||||
Expect.equals(3, "strstr".lastIndexOf("s", 5));
|
||||
Expect.equals(5, "strstr".lastIndexOf("r", 5));
|
||||
Expect.equals(-1, "str".lastIndexOf("string", 5));
|
||||
|
||||
Expect.equals(4, "strstr".lastIndexOf("t", 5));
|
||||
Expect.equals(4, "strstr".lastIndexOf("tr", 5));
|
||||
Expect.equals(3, "strstr".lastIndexOf("str", 5));
|
||||
Expect.equals(3, "strstr".lastIndexOf("str", 5));
|
||||
Expect.equals(3, "strstr".lastIndexOf("str", 5));
|
||||
Expect.equals(3, "strstr".lastIndexOf("st", 5));
|
||||
Expect.equals(3, "strstr".lastIndexOf("s", 5));
|
||||
Expect.equals(5, "strstr".lastIndexOf("r", 5));
|
||||
Expect.equals(2, "strstr".lastIndexOf("r", 4));
|
||||
Expect.equals(2, "strstr".lastIndexOf("r", 3));
|
||||
|
||||
String str = "hello";
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int result = str.lastIndexOf("", i);
|
||||
if (i > str.length) {
|
||||
Expect.equals(str.length, result);
|
||||
} else {
|
||||
Expect.equals(i, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static testContains() {
|
||||
Expect.equals(true, "str".contains("s", 0));
|
||||
Expect.equals(true, "str".contains("st", 0));
|
||||
Expect.equals(true, "str".contains("str", 0));
|
||||
Expect.equals(true, "str".contains("t", 0));
|
||||
Expect.equals(true, "str".contains("r", 0));
|
||||
Expect.equals(true, "str".contains("tr", 0));
|
||||
|
||||
Expect.equals(false, "str".contains("sr", 0));
|
||||
Expect.equals(false, "str".contains("string", 0));
|
||||
|
||||
Expect.equals(true, "str".contains("", 0));
|
||||
Expect.equals(true, "".contains("", 0));
|
||||
Expect.equals(false, "".contains("s", 0));
|
||||
}
|
||||
|
||||
static testReplaceAll() {
|
||||
Expect.equals(
|
||||
"AtoBtoCDtoE", "AfromBfromCDfromE".replaceAll("from", "to"));
|
||||
|
||||
// Test with the replaced string at the begining.
|
||||
Expect.equals(
|
||||
"toABtoCDtoE", "fromABfromCDfromE".replaceAll("from", "to"));
|
||||
|
||||
// Test with the replaced string at the end.
|
||||
Expect.equals(
|
||||
"toABtoCDtoEto", "fromABfromCDfromEfrom".replaceAll("from", "to"));
|
||||
|
||||
// Test when there are no occurence of the string to replace.
|
||||
Expect.equals("ABC", "ABC".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is the empty string.
|
||||
Expect.equals("", "".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is a substring of the string to
|
||||
// replace.
|
||||
Expect.equals("fro", "fro".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is the replaced string.
|
||||
Expect.equals("to", "from".replaceAll("from", "to"));
|
||||
|
||||
// Test when the string to change is the replacement string.
|
||||
Expect.equals("to", "to".replaceAll("from", "to"));
|
||||
|
||||
// Test replacing by the empty string.
|
||||
Expect.equals("", "from".replaceAll("from", ""));
|
||||
Expect.equals("AB", "AfromB".replaceAll("from", ""));
|
||||
|
||||
// Test changing the empty string.
|
||||
Expect.equals("to", "".replaceAll("", "to"));
|
||||
|
||||
// Test replacing the empty string.
|
||||
Expect.equals("toAtoBtoCto", "ABC".replaceAll("", "to"));
|
||||
}
|
||||
|
||||
static testCompareTo() {
|
||||
Expect.equals(0, "".compareTo(""));
|
||||
Expect.equals(0, "str".compareTo("str"));
|
||||
Expect.equals(-1, "str".compareTo("string"));
|
||||
Expect.equals(1, "string".compareTo("str"));
|
||||
Expect.equals(1, "string".compareTo(""));
|
||||
Expect.equals(-1, "".compareTo("string"));
|
||||
}
|
||||
|
||||
static testToList() {
|
||||
test(str) {
|
||||
var list = str.splitChars();
|
||||
Expect.equals(str.length, list.length);
|
||||
for (int i = 0; i < str.length; i++) {
|
||||
Expect.equals(str[i], list[i]);
|
||||
}
|
||||
}
|
||||
test("abc");
|
||||
test("");
|
||||
test(" ");
|
||||
}
|
||||
|
||||
static testCharCodes() {
|
||||
test(str) {
|
||||
var list = str.charCodes();
|
||||
Expect.equals(str.length, list.length);
|
||||
for (int i = 0; i < str.length; i++) {
|
||||
Expect.equals(str.charCodeAt(i), list[i]);
|
||||
}
|
||||
}
|
||||
test("abc");
|
||||
test("");
|
||||
test(" ");
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
StringTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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.
|
||||
|
||||
class StringTrimTest {
|
||||
static testMain() {
|
||||
Expect.equals("", " ".trim());
|
||||
Expect.equals("", " ".trim());
|
||||
var a = " lots of space on the left";
|
||||
Expect.equals("lots of space on the left", a.trim());
|
||||
a = "lots of space on the right ";
|
||||
Expect.equals("lots of space on the right", a.trim());
|
||||
a = " lots of space ";
|
||||
Expect.equals("lots of space", a.trim());
|
||||
a = " x ";
|
||||
Expect.equals("x", a.trim());
|
||||
Expect.equals("", " \t \n \r ".trim());
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
StringTrimTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for testing class 'Strings'.
|
||||
|
||||
class StringsTest {
|
||||
|
||||
StringsTest() {}
|
||||
|
||||
toString() {
|
||||
return "Strings Tester";
|
||||
}
|
||||
static testCreation() {
|
||||
String s = "Hello";
|
||||
List<int> l = new List(s.length);
|
||||
for (int i = 0; i < l.length; i++) {
|
||||
l[i] = s.charCodeAt(i);
|
||||
}
|
||||
String s2 = new String.fromCharCodes(l);
|
||||
Expect.equals(s, s2);
|
||||
}
|
||||
|
||||
static void testMain() {
|
||||
testCreation();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
main() {
|
||||
StringsTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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.
|
||||
|
||||
class UnicodeTest {
|
||||
|
||||
static testMain() {
|
||||
var lowerStrasse =
|
||||
new String.fromCharCodes([115, 116, 114, 97, 223, 101]);
|
||||
Expect.equals("STRASSE", lowerStrasse.toUpperCase());
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
UnicodeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
# 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 testing
|
||||
|
||||
def GetConfiguration(context, root):
|
||||
return testing.StandardTestConfiguration(context, root)
|
||||
@@ -0,0 +1,51 @@
|
||||
# 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.
|
||||
|
||||
prefix isolate
|
||||
|
||||
[ $arch == dartium ]
|
||||
ConstructorTest: Fail # Bug 5246195
|
||||
MandelIsolateTest: Fail # Bug 5246195
|
||||
SpawnTest: Fail # Bug 5246195
|
||||
MessageTest: Fail # Bug 5246195
|
||||
IsolateComplexMessagesTest: Fail # Bug 5246195
|
||||
CountTest: Fail # Bug 5246195
|
||||
PromiseBasedTest: Fail # Bug 5246195
|
||||
StaticStateTest: Fail # Bug 5246195
|
||||
RequestReplyTest: Crash, Fail # Bug 5395487
|
||||
CrossIsolateMessageTest: Fail # Bug 5246195
|
||||
|
||||
|
||||
[ $arch == ia32 ]
|
||||
Isolate2NegativeTest: Skip # Need to resolve correct behaviour.
|
||||
|
||||
[ $arch == dartium || $arch == ia32 || $arch == x64 || $arch == simarm || $arch == arm ]
|
||||
SerializationTest: Skip # DartC test (uses coreimpl).
|
||||
|
||||
[ $arch == chromium && $arch == release ]
|
||||
ConstructorTest: Fail # Bug 5382463
|
||||
SpawnTest: Fail # Bug 5382463
|
||||
IsolateComplexMessagesTest: Fail # Bug 5382463
|
||||
CountTest: Fail # Bug 5401734
|
||||
PromiseBasedTest: Fail # Bug 5401734
|
||||
MintMakerPromiseTest: Fail # Bug 5401734
|
||||
MintMakerTest: Fail # Bug 5401734
|
||||
RequestReplyTest: Fail # Bug 5401734
|
||||
StaticStateTest: Fail # Bug 5401734
|
||||
|
||||
[ $arch == chromium ]
|
||||
MandelIsolateTest: Skip # Bug 5353937.
|
||||
|
||||
[ $arch == dartc ]
|
||||
MintMakerPromiseTest: Fail # Bug 5283149.
|
||||
MintMakerTest: Fail # Bug 5283149.
|
||||
|
||||
[ $arch == x64 ]
|
||||
*: Skip
|
||||
|
||||
[ $arch == simarm ]
|
||||
*: Skip
|
||||
|
||||
[ $arch == arm ]
|
||||
*: Skip
|
||||
@@ -0,0 +1,33 @@
|
||||
// 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.
|
||||
|
||||
#library("ConstructorTest");
|
||||
#import("TestFramework.dart");
|
||||
|
||||
class ConstructorTest extends Isolate {
|
||||
final int field;
|
||||
ConstructorTest() : super(), field = 499;
|
||||
|
||||
void main() {
|
||||
this.port.receive((ignoredMessage, reply) {
|
||||
reply.send(field, null);
|
||||
this.port.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void test(TestExpectation expect) {
|
||||
ConstructorTest test = new ConstructorTest();
|
||||
expect.completes(test.spawn()).then((SendPort port) {
|
||||
ReceivePort reply = port.call("ignored");
|
||||
reply.receive(expect.runs2((message, replyPort) {
|
||||
Expect.equals(499, message);
|
||||
expect.succeeded();
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
main() {
|
||||
runTests([test]);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// 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.
|
||||
|
||||
#library("CountTest");
|
||||
#import("TestFramework.dart");
|
||||
|
||||
class TestIsolate extends Isolate {
|
||||
|
||||
TestIsolate() : super();
|
||||
|
||||
void main() {
|
||||
int count = 0;
|
||||
this.port.receive((int message, SendPort replyTo) {
|
||||
if (message == -1) {
|
||||
Expect.equals(10, count);
|
||||
replyTo.send(-1, null);
|
||||
this.port.close();
|
||||
return;
|
||||
}
|
||||
|
||||
Expect.equals(count, message);
|
||||
count++;
|
||||
replyTo.send(message * 2, null);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void test(TestExpectation expect) {
|
||||
int count = 0;
|
||||
expect.completes(new TestIsolate().spawn()).then((SendPort remote) {
|
||||
ReceivePort local = new ReceivePort();
|
||||
SendPort reply = local.toSendPort();
|
||||
|
||||
local.receive(expect.runs2((int message, SendPort replyTo) {
|
||||
if (message == -1) {
|
||||
Expect.equals(11, count);
|
||||
local.close();
|
||||
expect.succeeded();
|
||||
return;
|
||||
}
|
||||
|
||||
Expect.equals((count - 1) * 2, message);
|
||||
remote.send(count++, reply);
|
||||
if (count == 10) {
|
||||
remote.send(-1, reply);
|
||||
}
|
||||
}));
|
||||
remote.send(count++, reply);
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
runTests([test]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for testing that isolates can communicate to isolates
|
||||
// other than the main isolate.
|
||||
|
||||
#library('CrossIsolateMessageTest');
|
||||
#import('TestFramework.dart');
|
||||
|
||||
class CrossIsolate1 extends Isolate {
|
||||
CrossIsolate1() : super.heavy();
|
||||
|
||||
void main() {
|
||||
this.port.receive((msg, replyTo) {
|
||||
SendPort otherIsolate = msg;
|
||||
ReceivePort receivePort = new ReceivePort();
|
||||
receivePort.receive((msg, replyTo) {
|
||||
otherIsolate.send(msg + 58, null); // 100.
|
||||
receivePort.close();
|
||||
});
|
||||
replyTo.send('ready', receivePort.toSendPort());
|
||||
this.port.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// CrossIsolate2 is nearly the same as CrossIsolate1, but contains a
|
||||
// different constant.
|
||||
class CrossIsolate2 extends Isolate {
|
||||
CrossIsolate2() : super.heavy();
|
||||
|
||||
void main() {
|
||||
this.port.receive((msg, replyTo) {
|
||||
SendPort mainIsolate = msg;
|
||||
ReceivePort receivePort = new ReceivePort();
|
||||
receivePort.receive((msg, replyTo) {
|
||||
mainIsolate.send(msg + 399, null); // 499.
|
||||
receivePort.close();
|
||||
});
|
||||
replyTo.send('ready', receivePort.toSendPort());
|
||||
this.port.close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
test(TestExpectation expect) {
|
||||
// Create CrossIsolate1 and CrossIsolate2.
|
||||
expect.completes(new CrossIsolate1().spawn()).then((SendPort port1) {
|
||||
expect.completes(new CrossIsolate2().spawn()).then((SendPort port2) {
|
||||
// Create a new receive port and send it to isolate2.
|
||||
ReceivePort myPort = new ReceivePort();
|
||||
port2.call(myPort.toSendPort()).receive(expect.runs2((msg, port2b) {
|
||||
Expect.equals("ready", msg);
|
||||
// Send port of isolate2 to isolate1.
|
||||
port1.call(port2b).receive(expect.runs2((msg, port1b) {
|
||||
Expect.equals("ready", msg);
|
||||
myPort.receive(expect.runs2((msg, replyTo) {
|
||||
Expect.equals(499, msg);
|
||||
expect.succeeded();
|
||||
myPort.close();
|
||||
}));
|
||||
port1b.send(42, null);
|
||||
}));
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
main() {
|
||||
runTests([test]);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for testing that exceptions in other isolates bring down
|
||||
// the program.
|
||||
|
||||
#library('Isolate2NegativeTest');
|
||||
#import('TestFramework.dart');
|
||||
|
||||
class Isolate2NegativeTest extends Isolate {
|
||||
Isolate2NegativeTest() : super();
|
||||
|
||||
void main() {
|
||||
throw "foo";
|
||||
}
|
||||
}
|
||||
|
||||
void test(TestExpectation expect) {
|
||||
// We will never call 'expect.succeeded'. This test fails with a timeout.
|
||||
expect.completes(new Isolate2NegativeTest().spawn());
|
||||
}
|
||||
|
||||
main() {
|
||||
runTests([test]);
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for testing isolate communication with
|
||||
// complex messages.
|
||||
|
||||
#library("IsolateComplexMessagesTest");
|
||||
#import("TestFramework.dart");
|
||||
|
||||
|
||||
void test(TestExpectation expect) {
|
||||
expect.completes(new LogIsolate().spawn()).then((SendPort remote) {
|
||||
|
||||
remote.send(1, null);
|
||||
remote.send("Hello", null);
|
||||
remote.send("World", null);
|
||||
remote.send(const [null, 1, 2, 3, 4], null);
|
||||
remote.send(const [1, 2.0, true, false, 0xffffffffff], null);
|
||||
remote.send(const ["Hello", "World", 0xffffffffff], null);
|
||||
// Shutdown the LogRunner.
|
||||
remote.call(-1).receive(expect.runs2((int message, SendPort replyTo) {
|
||||
Expect.equals(6, message);
|
||||
expect.succeeded();
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
class LogIsolate extends Isolate {
|
||||
LogIsolate() : super() { }
|
||||
|
||||
void main() {
|
||||
int count = 0;
|
||||
|
||||
this.port.receive((var message, SendPort replyTo) {
|
||||
if (message == -1) {
|
||||
this.port.close();
|
||||
replyTo.send(count, null);
|
||||
} else {
|
||||
switch (count) {
|
||||
case 0:
|
||||
Expect.equals(1, message);
|
||||
break;
|
||||
case 1:
|
||||
Expect.equals("Hello", message);
|
||||
break;
|
||||
case 2:
|
||||
Expect.equals("World", message);
|
||||
break;
|
||||
case 3:
|
||||
Expect.equals(5, message.length);
|
||||
Expect.equals(null, message[0]);
|
||||
Expect.equals(1, message[1]);
|
||||
Expect.equals(2, message[2]);
|
||||
Expect.equals(3, message[3]);
|
||||
Expect.equals(4, message[4]);
|
||||
break;
|
||||
case 4:
|
||||
Expect.equals(5, message.length);
|
||||
Expect.equals(1, message[0]);
|
||||
Expect.equals(2.0, message[1]);
|
||||
Expect.equals(true, message[2]);
|
||||
Expect.equals(false, message[3]);
|
||||
Expect.equals(0xffffffffff, message[4]);
|
||||
break;
|
||||
case 5:
|
||||
Expect.equals(3, message.length);
|
||||
Expect.equals("Hello", message[0]);
|
||||
Expect.equals("World", message[1]);
|
||||
Expect.equals(0xffffffffff, message[2]);
|
||||
break;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
runTests([test]);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for testing that isolates are spawned.
|
||||
|
||||
#library('IsolateNegativeTest');
|
||||
#import("TestFramework.dart");
|
||||
|
||||
class IsolateNegativeTest extends Isolate {
|
||||
IsolateNegativeTest() : super();
|
||||
|
||||
void main() {
|
||||
this.port.receive((ignored, replyTo) {
|
||||
replyTo.send("foo", null);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void test(TestExpectation expect) {
|
||||
expect.completes(new IsolateNegativeTest().spawn()).then((SendPort port) {
|
||||
port.call("foo").receive(expect.runs2((message, replyTo) {
|
||||
Expect.equals(true, "Expected fail"); // <=-------- Should fail here.
|
||||
expect.succeeded();
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
main() {
|
||||
runTests([test]);
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
// 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.
|
||||
|
||||
#library("MandelIsolateTest");
|
||||
#import('TestFramework.dart');
|
||||
|
||||
final TERMINATION_MESSAGE = -1;
|
||||
final N = 100;
|
||||
final ISOLATES = 20;
|
||||
|
||||
void test(TestExpectation expect) {
|
||||
final state = new MandelbrotState();
|
||||
expect.completes(state._validated).then((result) {
|
||||
Expect.isTrue(result);
|
||||
expect.succeeded();
|
||||
});
|
||||
for (int i = 0; i < Math.min(ISOLATES, N); i++) state.startClient(i);
|
||||
}
|
||||
|
||||
|
||||
class MandelbrotState {
|
||||
|
||||
MandelbrotState() {
|
||||
_result = new List<List<int>>(N);
|
||||
_lineProcessedBy = new List<LineProcessorClient>(N);
|
||||
_sent = 0;
|
||||
_missing = N;
|
||||
_validated = new Promise<bool>();
|
||||
}
|
||||
|
||||
void startClient(int id) {
|
||||
assert(_sent < N);
|
||||
final client = new LineProcessorClient(this, id);
|
||||
client.processLine(_sent++);
|
||||
}
|
||||
|
||||
void notifyProcessedLine(LineProcessorClient client, int y, List<int> line) {
|
||||
assert(_result[y] === null);
|
||||
_result[y] = line;
|
||||
_lineProcessedBy[y] = client;
|
||||
|
||||
if (_sent != N) {
|
||||
client.processLine(_sent++);
|
||||
} else {
|
||||
client.shutdown();
|
||||
}
|
||||
|
||||
// If all lines have been computed, validate the result.
|
||||
if (--_missing == 0) {
|
||||
_printResult();
|
||||
_validateResult();
|
||||
}
|
||||
}
|
||||
|
||||
void _validateResult() {
|
||||
// TODO(ngeoffray): Implement this.
|
||||
_validated.complete(true);
|
||||
}
|
||||
|
||||
void _printResult() {
|
||||
var output = new StringBuffer();
|
||||
for (int i = 0; i < _result.length; i++) {
|
||||
List<int> line = _result[i];
|
||||
for (int j = 0; j < line.length; j++) {
|
||||
if (line[j] < 10) output.add("0");
|
||||
output.add(line[j]);
|
||||
}
|
||||
output.add("\n");
|
||||
}
|
||||
// print(output);
|
||||
}
|
||||
|
||||
List<List<int>> _result;
|
||||
List<LineProcessorClient> _lineProcessedBy;
|
||||
int _sent;
|
||||
int _missing;
|
||||
Promise<bool> _validated;
|
||||
}
|
||||
|
||||
|
||||
class LineProcessorClient {
|
||||
|
||||
LineProcessorClient(MandelbrotState this._state, int this._id) {
|
||||
_out = new LineProcessor().spawn();
|
||||
}
|
||||
|
||||
void processLine(int y) {
|
||||
_out.then((SendPort p) {
|
||||
p.call(y).receive((List<int> message, SendPort replyTo) {
|
||||
_state.notifyProcessedLine(this, y, message);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void shutdown() {
|
||||
_out.then((SendPort p) {
|
||||
p.send(TERMINATION_MESSAGE, null);
|
||||
});
|
||||
}
|
||||
|
||||
MandelbrotState _state;
|
||||
int _id;
|
||||
Promise<SendPort> _out;
|
||||
|
||||
}
|
||||
|
||||
|
||||
class LineProcessor extends Isolate {
|
||||
|
||||
LineProcessor() : super() { }
|
||||
|
||||
void main() {
|
||||
this.port.receive((message, SendPort replyTo) {
|
||||
if (message == TERMINATION_MESSAGE) {
|
||||
assert(replyTo == null);
|
||||
this.port.close();
|
||||
} else {
|
||||
replyTo.send(_processLine(message), null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static List<int> _processLine(int y) {
|
||||
double inverseN = 2.0 / N;
|
||||
double Civ = y * inverseN - 1.0;
|
||||
List<int> result = new List<int>(N);
|
||||
for (int x = 0; x < N; x++) {
|
||||
double Crv = x * inverseN - 1.5;
|
||||
|
||||
double Zrv = Crv;
|
||||
double Ziv = Civ;
|
||||
|
||||
double Trv = Crv * Crv;
|
||||
double Tiv = Civ * Civ;
|
||||
|
||||
int i = 49;
|
||||
do {
|
||||
Ziv = (Zrv * Ziv) + (Zrv * Ziv) + Civ;
|
||||
Zrv = Trv - Tiv + Crv;
|
||||
|
||||
Trv = Zrv * Zrv;
|
||||
Tiv = Ziv * Ziv;
|
||||
} while (((Trv + Tiv) <= 4.0) && (--i > 0));
|
||||
|
||||
result[x] = i;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
main() {
|
||||
runTests([test]);
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for testing serialization of messages.
|
||||
// VMOptions=--enable_type_checks --enable_asserts
|
||||
|
||||
#library('MessageTest');
|
||||
#import("TestFramework.dart");
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Message passing test.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
class MessageTest {
|
||||
static void test(TestExpectation expect) {
|
||||
PingPongClient.test(expect);
|
||||
}
|
||||
|
||||
static final List list1 = const ["Hello", "World", "Hello", 0xfffffffffff];
|
||||
static final List list2 = const [null, list1, list1, list1, list1];
|
||||
static final List list3 = const [list2, 2.0, true, false, 0xfffffffffff];
|
||||
static final Map map1 = const {
|
||||
"a=1" : 1, "b=2" : 2, "c=3" : 3,
|
||||
};
|
||||
static final Map map2 = const {
|
||||
"list1" : list1, "list2" : list2, "list3" : list3,
|
||||
};
|
||||
static final List list4 = const [map1, map2];
|
||||
static final List elms = const [
|
||||
list1, list2, list3, list4,
|
||||
];
|
||||
|
||||
static void VerifyMap(Map expected, Map actual) {
|
||||
Expect.equals(true, expected is Map);
|
||||
Expect.equals(true, actual is Map);
|
||||
Expect.equals(expected.length, actual.length);
|
||||
testForEachMap(key, value) {
|
||||
if (value is List) {
|
||||
VerifyList(value, actual[key]);
|
||||
} else {
|
||||
Expect.equals(value, actual[key]);
|
||||
}
|
||||
}
|
||||
expected.forEach(testForEachMap);
|
||||
}
|
||||
|
||||
static void VerifyList(List expected, List actual) {
|
||||
for (int i = 0; i < expected.length; i++) {
|
||||
if (expected[i] is List) {
|
||||
VerifyList(expected[i], actual[i]);
|
||||
} else if (expected[i] is Map) {
|
||||
VerifyMap(expected[i], actual[i]);
|
||||
} else {
|
||||
Expect.equals(expected[i], actual[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void VerifyObject(int index, var actual) {
|
||||
var expected = elms[index];
|
||||
Expect.equals(true, expected is List);
|
||||
Expect.equals(true, actual is List);
|
||||
Expect.equals(expected.length, actual.length);
|
||||
VerifyList(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
class PingPongClient {
|
||||
static void test(TestExpectation expect) {
|
||||
expect.completes(new PingPongServer().spawn()).then((SendPort remote) {
|
||||
|
||||
// Send objects and receive them back.
|
||||
for (int i = 0; i < MessageTest.elms.length; i++) {
|
||||
var sentObject = MessageTest.elms[i];
|
||||
// TODO(asiva): remove this local var idx once thew new for-loop
|
||||
// semantics for closures is implemented.
|
||||
var idx = i;
|
||||
remote.call(sentObject).receive(expect.runs2(
|
||||
(var receivedObject, SendPort replyTo) {
|
||||
MessageTest.VerifyObject(idx, receivedObject);
|
||||
}));
|
||||
}
|
||||
|
||||
// Send recursive objects and receive them back.
|
||||
List local_list1 = ["Hello", "World", "Hello", 0xffffffffff];
|
||||
List local_list2 = [null, local_list1, local_list1 ];
|
||||
List local_list3 = [local_list2, 2.0, true, false, 0xffffffffff];
|
||||
List sendObject = new List(5);
|
||||
sendObject[0] = local_list1;
|
||||
sendObject[1] = sendObject;
|
||||
sendObject[2] = local_list2;
|
||||
sendObject[3] = sendObject;
|
||||
sendObject[4] = local_list3;
|
||||
remote.call(sendObject).receive(
|
||||
(var replyObject, SendPort replyTo) {
|
||||
Expect.equals(true, sendObject is List);
|
||||
Expect.equals(true, replyObject is List);
|
||||
Expect.equals(sendObject.length, replyObject.length);
|
||||
Expect.equals(true, replyObject[1] === replyObject);
|
||||
Expect.equals(true, replyObject[3] === replyObject);
|
||||
Expect.equals(true, replyObject[0] === replyObject[2][1]);
|
||||
Expect.equals(true, replyObject[0] === replyObject[2][2]);
|
||||
Expect.equals(true, replyObject[2] === replyObject[4][0]);
|
||||
Expect.equals(true, replyObject[0][0] === replyObject[0][2]);
|
||||
// Bigint literals are not canonicalized so do a == check.
|
||||
Expect.equals(true, replyObject[0][3] == replyObject[4][4]);
|
||||
});
|
||||
|
||||
// Shutdown the MessageServer.
|
||||
remote.call(-1).receive(expect.runs2(
|
||||
(int message, SendPort replyTo) {
|
||||
Expect.equals(MessageTest.elms.length + 1, message);
|
||||
expect.succeeded();
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class PingPongServer extends Isolate {
|
||||
PingPongServer() : super() {}
|
||||
|
||||
void main() {
|
||||
int count = 0;
|
||||
this.port.receive(
|
||||
(var message, SendPort replyTo) {
|
||||
if (message == -1) {
|
||||
this.port.close();
|
||||
replyTo.send(count, null);
|
||||
} else {
|
||||
// Check if the received object is correct.
|
||||
if (count < MessageTest.elms.length) {
|
||||
MessageTest.VerifyObject(count, message);
|
||||
}
|
||||
// Bounce the received object back so that the sender
|
||||
// can make sure that the object matches.
|
||||
replyTo.send(message, null);
|
||||
count++;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
runTests([MessageTest.test]);
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
// 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.
|
||||
|
||||
interface Mint factory MintImpl {
|
||||
|
||||
Mint();
|
||||
|
||||
Purse createPurse(int balance);
|
||||
|
||||
}
|
||||
|
||||
|
||||
class MintImpl implements Mint {
|
||||
|
||||
MintImpl() { }
|
||||
|
||||
Purse createPurse(int balance) {
|
||||
return new PurseImpl(this, balance);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
interface Purse {
|
||||
|
||||
int queryBalance();
|
||||
Purse sproutPurse();
|
||||
void deposit(int amount, Purse$Proxy source);
|
||||
|
||||
}
|
||||
|
||||
|
||||
class PurseImpl implements Purse {
|
||||
|
||||
PurseImpl(this._mint, this._balance) { }
|
||||
|
||||
int queryBalance() {
|
||||
return _balance;
|
||||
}
|
||||
|
||||
Purse sproutPurse() {
|
||||
return _mint.createPurse(0);
|
||||
}
|
||||
|
||||
void deposit(int amount, Purse$Proxy purse) {
|
||||
Purse$ProxyImpl impl = purse.dynamic; // TODO: Get rid of this 'cast'.
|
||||
PurseImpl source = impl.local;
|
||||
if (source._balance < amount) throw "Not enough dough.";
|
||||
_balance += amount;
|
||||
source._balance -= amount;
|
||||
}
|
||||
|
||||
Mint _mint;
|
||||
int _balance;
|
||||
|
||||
}
|
||||
|
||||
|
||||
class MintMakerPromiseTest {
|
||||
|
||||
static void testMain() {
|
||||
Mint$Proxy mint = createMint();
|
||||
Purse$Proxy purse = mint.createPurse(100);
|
||||
expectEquals(100, purse.queryBalance());
|
||||
|
||||
Purse$Proxy sprouted = purse.sproutPurse();
|
||||
expectEquals(0, sprouted.queryBalance());
|
||||
|
||||
sprouted.deposit(5, purse);
|
||||
expectEquals(0 + 5, sprouted.queryBalance());
|
||||
expectEquals(100 - 5, purse.queryBalance());
|
||||
|
||||
sprouted.deposit(42, purse);
|
||||
expectEquals(0 + 5 + 42, sprouted.queryBalance());
|
||||
expectEquals(100 - 5 - 42, purse.queryBalance());
|
||||
|
||||
expectDone(6);
|
||||
}
|
||||
|
||||
static Mint$Proxy createMint() {
|
||||
Proxy isolate = new Proxy.forIsolate(new Mint$Dispatcher$Isolate());
|
||||
return new Mint$ProxyImpl(isolate);
|
||||
}
|
||||
|
||||
|
||||
static List<Promise> results;
|
||||
|
||||
static void expectEquals(int expected, Promise<int> promise) {
|
||||
if (results === null) {
|
||||
results = new List<Promise>();
|
||||
}
|
||||
results.add(promise.then((int actual) {
|
||||
Expect.equals(expected, actual);
|
||||
}));
|
||||
}
|
||||
|
||||
static void expectDone(int n) {
|
||||
if (results === null) {
|
||||
Expect.equals(0, n);
|
||||
} else {
|
||||
Promise done = new Promise();
|
||||
done.waitFor(results, results.length);
|
||||
done.then((ignored) {
|
||||
Expect.equals(n, results.length);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// THE REST OF THIS FILE COULD BE AUTOGENERATED
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
interface Mint$Proxy {
|
||||
|
||||
Purse$Proxy createPurse(int balance); // Promise<int> balance.
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Mint$ProxyImpl extends Proxy implements Mint$Proxy {
|
||||
|
||||
Mint$ProxyImpl(Proxy isolate) : super.forReply(isolate.call([null])) {}
|
||||
|
||||
Purse$Proxy createPurse(int balance) {
|
||||
return new Purse$ProxyImpl(this.call([balance]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Mint$Dispatcher extends Dispatcher<Mint> {
|
||||
|
||||
Mint$Dispatcher(Mint mint) : super(mint) { }
|
||||
|
||||
void process(var message, void reply(var response)) {
|
||||
int balance = message[0];
|
||||
Purse purse = target.createPurse(balance);
|
||||
SendPort port = Dispatcher.serve(new Purse$Dispatcher(purse));
|
||||
reply(port);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Mint$Dispatcher$Isolate extends Isolate {
|
||||
|
||||
Mint$Dispatcher$Isolate() : super() { }
|
||||
|
||||
void main() {
|
||||
this.port.receive((var message, SendPort replyTo) {
|
||||
Mint mint = new Mint();
|
||||
SendPort port = Dispatcher.serve(new Mint$Dispatcher(mint));
|
||||
Proxy proxy = new Proxy.forPort(replyTo);
|
||||
proxy.send([port]);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
interface Purse$Proxy {
|
||||
|
||||
Promise<int> queryBalance();
|
||||
Purse$Proxy sproutPurse();
|
||||
void deposit(int amount, Purse$Proxy source); // Promise<int> amount.
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Purse$ProxyImpl extends Proxy implements Purse$Proxy {
|
||||
|
||||
Purse$ProxyImpl(Promise<SendPort> port) : super.forReply(port) { }
|
||||
|
||||
Promise<int> queryBalance() {
|
||||
return this.call(["balance"]);
|
||||
}
|
||||
|
||||
void deposit(int amount, Purse$Proxy source) {
|
||||
this.send(["deposit", amount, source]);
|
||||
}
|
||||
|
||||
Purse$Proxy sproutPurse() {
|
||||
return new Purse$ProxyImpl(this.call(["sprout"]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class Purse$Dispatcher extends Dispatcher<Purse> {
|
||||
|
||||
Purse$Dispatcher(Purse purse) : super(purse) { }
|
||||
|
||||
void process(var message, void reply(var response)) {
|
||||
String command = message[0];
|
||||
if (command == "balance") {
|
||||
int balance = target.queryBalance();
|
||||
reply(balance);
|
||||
} else if (command == "deposit") {
|
||||
int amount = message[1];
|
||||
Promise<SendPort> port = new Promise<SendPort>.fromValue(message[2]);
|
||||
Purse$Proxy source = new Purse$ProxyImpl(port);
|
||||
target.deposit(amount, source);
|
||||
} else if (command == "sprout") {
|
||||
Purse purse = target.sproutPurse();
|
||||
SendPort port = Dispatcher.serve(new Purse$Dispatcher(purse));
|
||||
reply(port);
|
||||
} else {
|
||||
// TODO: Send an exception back.
|
||||
reply("Exception: Command not understood");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
main() {
|
||||
MintMakerPromiseTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
// 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.
|
||||
|
||||
// Things that should be "auto-generated" are between AUTO START and
|
||||
// AUTO END (or just AUTO if it's a single line).
|
||||
|
||||
|
||||
class Mint {
|
||||
Mint() : registry_ = new Map<SendPort, Purse>() {
|
||||
// AUTO START
|
||||
ReceivePort mintPort = new ReceivePort();
|
||||
port = mintPort.toSendPort();
|
||||
serveMint(mintPort);
|
||||
// AUTO END
|
||||
}
|
||||
|
||||
// AUTO START
|
||||
void serveMint(ReceivePort port) {
|
||||
port.receive((var message, SendPort replyTo) {
|
||||
int balance = message;
|
||||
Purse purse = createPurse(balance);
|
||||
replyTo.send([ purse.port ], null);
|
||||
});
|
||||
}
|
||||
// AUTO END
|
||||
|
||||
Purse createPurse(int balance) {
|
||||
Purse purse = new Purse(this, balance);
|
||||
registry_[purse.port] = purse;
|
||||
return purse;
|
||||
}
|
||||
|
||||
Purse lookupPurse(SendPort port) {
|
||||
return registry_[port];
|
||||
}
|
||||
|
||||
Map<SendPort, Purse> registry_;
|
||||
// AUTO
|
||||
SendPort port;
|
||||
}
|
||||
|
||||
|
||||
// AUTO START
|
||||
class MintWrapper {
|
||||
MintWrapper(SendPort this.mint_) {}
|
||||
|
||||
void createPurse(int balance, handlePurse(PurseWrapper purse)) {
|
||||
mint_.call(balance).receive((var message, SendPort replyTo) {
|
||||
SendPort purse = message[0];
|
||||
handlePurse(new PurseWrapper(purse));
|
||||
});
|
||||
}
|
||||
|
||||
SendPort mint_;
|
||||
}
|
||||
// AUTO END
|
||||
|
||||
|
||||
/*
|
||||
One way this could look without the autogenerated code:
|
||||
|
||||
class Mint {
|
||||
Mint() : registry_ = new Map<SendPort, Purse>() {
|
||||
}
|
||||
|
||||
wrap Purse createPurse(int balance) {
|
||||
Purse purse = new Purse(this, balance);
|
||||
registry_[purse.port] = purse;
|
||||
return purse;
|
||||
}
|
||||
|
||||
Purse lookupPurse(SendPort port) {
|
||||
return registry_[port];
|
||||
}
|
||||
|
||||
Map<SendPort, Purse> registry_;
|
||||
}
|
||||
|
||||
The other end of the port would use Wrapper<Mint> as the wrapper, or
|
||||
Future<Mint> as a future for the wrapper.
|
||||
*/
|
||||
|
||||
|
||||
class Purse {
|
||||
Purse(Mint this.mint, int this.balance) {
|
||||
// AUTO START
|
||||
ReceivePort recipient = new ReceivePort();
|
||||
port = recipient.toSendPort();
|
||||
servePurse(recipient);
|
||||
// AUTO END
|
||||
}
|
||||
|
||||
// AUTO START
|
||||
void servePurse(ReceivePort recipient) {
|
||||
recipient.receive((var message, SendPort replyTo) {
|
||||
String command = message[0];
|
||||
if (command == "balance") {
|
||||
replyTo.send(queryBalance(), null);
|
||||
} else if (command == "deposit") {
|
||||
Purse source = mint.lookupPurse(message[2]);
|
||||
deposit(message[1], source);
|
||||
} else if (command == "sprout") {
|
||||
Purse result = sproutPurse();
|
||||
replyTo.send([ result.port ], null);
|
||||
} else {
|
||||
// TODO: Send an exception back.
|
||||
replyTo.send("Exception: Command not understood", null);
|
||||
}
|
||||
});
|
||||
}
|
||||
// AUTO END
|
||||
|
||||
int queryBalance() { return balance; }
|
||||
|
||||
Purse sproutPurse() { return mint.createPurse(0); }
|
||||
|
||||
void deposit(int amount, Purse source) {
|
||||
// TODO: Throw an exception if the source purse doesn't hold
|
||||
// enough dough.
|
||||
balance += amount;
|
||||
source.balance -= amount;
|
||||
}
|
||||
|
||||
Mint mint;
|
||||
int balance;
|
||||
// AUTO
|
||||
SendPort port;
|
||||
}
|
||||
|
||||
|
||||
// AUTO START
|
||||
class PurseWrapper {
|
||||
PurseWrapper(SendPort this.purse_) {}
|
||||
|
||||
void queryBalance(handleBalance(int balance)) {
|
||||
purse_.call([ "balance" ]).receive((var message, SendPort replyTo) {
|
||||
int balance = message;
|
||||
handleBalance(balance);
|
||||
});
|
||||
}
|
||||
|
||||
void sproutPurse(handleSprouted(PurseWrapper sprouted)) {
|
||||
purse_.call([ "sprout" ]).receive((var message, SendPort replyTo) {
|
||||
SendPort sprouted = message[0];
|
||||
handleSprouted(new PurseWrapper(sprouted));
|
||||
});
|
||||
}
|
||||
|
||||
void deposit(PurseWrapper source, int amount) {
|
||||
purse_.send([ "deposit", amount, source.purse_ ], null);
|
||||
}
|
||||
|
||||
|
||||
SendPort purse_;
|
||||
}
|
||||
// AUTO END
|
||||
|
||||
|
||||
// AUTO STATUS UNCLEAR!
|
||||
|
||||
class MintMakerWrapperIsolate extends Isolate {
|
||||
MintMakerWrapperIsolate() : super() { }
|
||||
void main() {
|
||||
this.port.receive((var message, SendPort replyTo) {
|
||||
Mint mint = new Mint();
|
||||
replyTo.send([ mint.port ], null);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class MintMakerWrapper {
|
||||
MintMakerWrapper() {
|
||||
port_ = new MintMakerWrapperIsolate().spawn();
|
||||
}
|
||||
|
||||
void makeMint(handleMint(MintWrapper mint)) {
|
||||
port_.then((SendPort port) {
|
||||
port.call(null).receive((var message, SendPort replyTo) {
|
||||
SendPort mint = message[0];
|
||||
handleMint(new MintWrapper(mint));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Promise<SendPort> port_;
|
||||
}
|
||||
|
||||
|
||||
class MintMakerTest {
|
||||
static void testMain() {
|
||||
MintMakerWrapper mintMaker = new MintMakerWrapper();
|
||||
mintMaker.makeMint((MintWrapper mint) {
|
||||
mint.createPurse(100, (PurseWrapper purse) {
|
||||
purse.queryBalance((int balance) {
|
||||
Expect.equals(100, balance);
|
||||
});
|
||||
purse.sproutPurse((PurseWrapper sprouted) {
|
||||
sprouted.queryBalance((int balance) {
|
||||
Expect.equals(0, balance);
|
||||
});
|
||||
sprouted.deposit(purse, 5);
|
||||
sprouted.queryBalance((int balance) {
|
||||
Expect.equals(0 + 5, balance);
|
||||
});
|
||||
purse.queryBalance((int balance) {
|
||||
Expect.equals(100 - 5, balance);
|
||||
});
|
||||
sprouted.deposit(purse, 42);
|
||||
sprouted.queryBalance((int balance) {
|
||||
Expect.equals(0 + 5 + 42, balance);
|
||||
});
|
||||
purse.queryBalance((int balance) {
|
||||
Expect.equals(100 - 5 - 42, balance);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/* This is an attempt to show how the above code could look like if we had
|
||||
* better language support for asynchronous messages (deferred/asynccall).
|
||||
* The static helper methods like createPurse and queryBalance would also
|
||||
* have to be marked async.
|
||||
|
||||
void run(port) {
|
||||
MintMakerWrapper mintMaker = spawnMintMaker();
|
||||
deferred {
|
||||
MintWrapper mint = asynccall mintMaker.createMint();
|
||||
PurseWrapper purse = asynccall mint.createPurse(100);
|
||||
Expect.equals(100, asynccall purse.queryBalance());
|
||||
|
||||
PurseWrapper sprouted = asynccall purse.sproutPurse();
|
||||
Expect.equals(0, asynccall sprouted.queryBalance());
|
||||
|
||||
asynccall sprouted.deposit(purse, 5);
|
||||
Expect.equals(0 + 5, asynccall sprouted.queryBalance());
|
||||
Expect.equals(100 - 5, asynccall purse.queryBalance());
|
||||
|
||||
asynccall sprouted.deposit(purse, 42);
|
||||
Expect.equals(0 + 5 + 42, asynccall sprouted.queryBalance());
|
||||
Expect.equals(100 - 5 - 42, asynccall purse.queryBalance());
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
/* And a version using futures and wrappers.
|
||||
|
||||
void run(port) {
|
||||
Wrapper<MintMaker> mintMaker = spawnMintMaker();
|
||||
Future<Mint> mint = mintMaker...createMint();
|
||||
Future<Purse> purse = mint...createPurse(100);
|
||||
Expect.equals(100, purse.queryBalance());
|
||||
|
||||
Future<Purse> sprouted = purse...sproutPurse();
|
||||
Expect.equals(0, sprouted.queryBalance());
|
||||
|
||||
sprouted...deposit(purse, 5);
|
||||
Expect.equals(0 + 5, sprouted.queryBalance());
|
||||
Expect.equals(100 - 5, purse.queryBalance());
|
||||
|
||||
sprouted...deposit(purse, 42);
|
||||
Expect.equals(0 + 5 + 42, sprouted.queryBalance());
|
||||
Expect.equals(100 - 5 - 42, purse.queryBalance());
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
main() {
|
||||
MintMakerTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// 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.
|
||||
|
||||
#library("PromiseBasedTest");
|
||||
#import("TestFramework.dart");
|
||||
|
||||
class TestIsolate extends Isolate {
|
||||
|
||||
TestIsolate() : super();
|
||||
|
||||
void main() {
|
||||
int seed = 0;
|
||||
this.port.receive((var message, SendPort replyTo) {
|
||||
if (seed == 0) {
|
||||
seed = message[0];
|
||||
} else {
|
||||
Promise<int> response = new Promise<int>();
|
||||
var proxy = new Proxy.forPort(replyTo);
|
||||
proxy.send([response]);
|
||||
response.complete(seed + message[0]);
|
||||
this.port.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void test(TestExpectation expect) {
|
||||
Proxy proxy = new Proxy.forIsolate(new TestIsolate());
|
||||
proxy.send([42]); // Seed the isolate.
|
||||
Promise promise = expect.completes(proxy.call([87])).then((int value) {
|
||||
Expect.equals(42 + 87, value);
|
||||
return 99;
|
||||
});
|
||||
expect.completes(promise).then((int value) {
|
||||
Expect.equals(99, value);
|
||||
expect.succeeded();
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
runTests([test]);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// 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.
|
||||
|
||||
#library("RequestReplyTest");
|
||||
#import("TestFramework.dart");
|
||||
|
||||
class TestIsolate extends Isolate {
|
||||
|
||||
TestIsolate() : super();
|
||||
|
||||
void main() {
|
||||
this.port.receive((message, SendPort replyTo) {
|
||||
replyTo.send(message + 87, null);
|
||||
this.port.close();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void testCall(TestExpectation expect) {
|
||||
expect.completes(new TestIsolate().spawn()).then((SendPort port) {
|
||||
port.call(42).receive(expect.runs2((message, replyTo) {
|
||||
Expect.equals(42 + 87, message);
|
||||
expect.succeeded();
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
void testSend(TestExpectation expect) {
|
||||
expect.completes(new TestIsolate().spawn()).then((SendPort port) {
|
||||
ReceivePort reply = new ReceivePort();
|
||||
port.send(99, reply.toSendPort());
|
||||
reply.receive(expect.runs2((message, replyTo) {
|
||||
Expect.equals(99 + 87, message);
|
||||
reply.close();
|
||||
expect.succeeded();
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
void testSendSingleShot(TestExpectation expect) {
|
||||
expect.completes(new TestIsolate().spawn()).then((SendPort port) {
|
||||
ReceivePort reply = new ReceivePort.singleShot();
|
||||
port.send(99, reply.toSendPort());
|
||||
reply.receive(expect.runs2((message, replyTo) {
|
||||
Expect.equals(99 + 87, message);
|
||||
expect.succeeded();
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
runTests([testCall, testSend, testSendSingleShot]);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for testing serialization of messages without spawning
|
||||
// isolates.
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Serialization test.
|
||||
// ---------------------------------------------------------------------------
|
||||
#library('SerializationTest');
|
||||
#import("dart:coreimpl");
|
||||
|
||||
main() {
|
||||
testAllTypes(copy);
|
||||
testAllTypes(serialize);
|
||||
}
|
||||
|
||||
copy(x) {
|
||||
return new Copier().traverse(x);
|
||||
}
|
||||
|
||||
serialize(x) {
|
||||
Serializer serializer = new Serializer();
|
||||
Deserializer deserializer = new Deserializer();
|
||||
return deserializer.deserialize(serializer.traverse(x));
|
||||
}
|
||||
|
||||
void testAllTypes(Function f) {
|
||||
copyAndVerify(0, f);
|
||||
copyAndVerify(499, f);
|
||||
copyAndVerify(true, f);
|
||||
copyAndVerify(false, f);
|
||||
copyAndVerify("", f);
|
||||
copyAndVerify("foo", f);
|
||||
copyAndVerify([], f);
|
||||
copyAndVerify([1, 2], f);
|
||||
copyAndVerify([[]], f);
|
||||
copyAndVerify([1, []], f);
|
||||
copyAndVerify({}, f);
|
||||
copyAndVerify({ 'a': 3 }, f);
|
||||
copyAndVerify({ 'a': 3, 'b': 5, 'c': 8 }, f);
|
||||
copyAndVerify({ 'a': [1, 2] }, f);
|
||||
copyAndVerify({ 'b': { 'c' : 99 } }, f);
|
||||
copyAndVerify([ { 'a': 499 }, { 'b': 42 } ], f);
|
||||
|
||||
var port = new ReceivePort();
|
||||
var transformed = f(port);
|
||||
Expect.equals(port.toSendPort(), transformed);
|
||||
port.close();
|
||||
|
||||
port = new ReceivePort.singleShot();
|
||||
transformed = f(port);
|
||||
Expect.equals(port.toSendPort(), transformed);
|
||||
port.close();
|
||||
|
||||
var a = [ 1, 3, 5 ];
|
||||
var b = { 'b': 49 };
|
||||
var c = [ a, b, a, b, a ];
|
||||
var copied = f(c);
|
||||
verify(c, copied);
|
||||
Expect.isFalse(c === copied);
|
||||
Expect.isTrue(copied[0] === copied[2]);
|
||||
Expect.isTrue(copied[0] === copied[4]);
|
||||
Expect.isTrue(copied[1] === copied[3]);
|
||||
}
|
||||
|
||||
void copyAndVerify(o, Function f) {
|
||||
var copy = f(o);
|
||||
verify(o, copy);
|
||||
}
|
||||
|
||||
void verify(o, copy) {
|
||||
if ((o is bool) || (o is num) || (o is String)) {
|
||||
Expect.equals(o, copy);
|
||||
} else if (o is List) {
|
||||
Expect.isTrue(copy is List);
|
||||
Expect.equals(o.length, copy.length);
|
||||
for (int i = 0; i < o.length; i++) {
|
||||
verify(o[i], copy[i]);
|
||||
}
|
||||
} else if (o is Map) {
|
||||
Expect.isTrue(copy is Map);
|
||||
Expect.equals(o.length, copy.length);
|
||||
o.forEach((key, value) {
|
||||
Expect.isTrue(copy.containsKey(key));
|
||||
verify(value, copy[key]);
|
||||
});
|
||||
} else {
|
||||
Expect.fail("Unexpected object encountered");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// 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.
|
||||
|
||||
#library("SpawnTest");
|
||||
#import("TestFramework.dart");
|
||||
|
||||
void test(TestExpectation expect) {
|
||||
SpawnedIsolate isolate = new SpawnedIsolate();
|
||||
expect.completes(isolate.spawn()).then((SendPort port) {
|
||||
port.call(42).receive(expect.runs2((message, replyTo) {
|
||||
Expect.equals(42, message);
|
||||
expect.succeeded();
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
class SpawnedIsolate extends Isolate {
|
||||
|
||||
SpawnedIsolate() : super() { }
|
||||
|
||||
void main() {
|
||||
this.port.receive((message, SendPort replyTo) {
|
||||
Expect.equals(42, message);
|
||||
replyTo.send(42, null);
|
||||
this.port.close();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
main() {
|
||||
runTests([test]);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
// 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.
|
||||
|
||||
#library("StaticStateTest");
|
||||
#import("TestFramework.dart");
|
||||
|
||||
class TestIsolate extends Isolate {
|
||||
|
||||
TestIsolate() : super();
|
||||
|
||||
void main() {
|
||||
Expect.equals(null, state);
|
||||
this.port.receive((var message, SendPort replyTo) {
|
||||
String old = state;
|
||||
state = message;
|
||||
replyTo.send(old, null);
|
||||
if (message == "exit") {
|
||||
this.port.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static String state;
|
||||
|
||||
}
|
||||
|
||||
void test(TestExpectation expect) {
|
||||
Expect.equals(null, TestIsolate.state);
|
||||
TestIsolate.state = "foo";
|
||||
Expect.equals("foo", TestIsolate.state);
|
||||
|
||||
expect.completes(new TestIsolate().spawn()).then((SendPort remote) {
|
||||
remote.call("bar").receive(expect.runs2((reply, replyTo) {
|
||||
Expect.equals("foo", TestIsolate.state);
|
||||
Expect.equals(null, reply);
|
||||
|
||||
TestIsolate.state = "baz";
|
||||
remote.call("exit").receive(expect.runs2((reply, replyTo) {
|
||||
Expect.equals("baz", TestIsolate.state);
|
||||
Expect.equals("bar", reply);
|
||||
expect.succeeded();
|
||||
}));
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
void main() {
|
||||
runTests([test]);
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
// 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.
|
||||
|
||||
#library("TestFramework");
|
||||
#import("dart:coreimpl");
|
||||
|
||||
|
||||
typedef void AsynchronousTestFunction(TestExpectation check);
|
||||
|
||||
|
||||
void runTests(List tests) {
|
||||
TestRunner runner = new TestRunner(new TestSuite(tests));
|
||||
TestResult result = new TestResult(runner);
|
||||
runner.run(result);
|
||||
}
|
||||
|
||||
|
||||
class TestSuite {
|
||||
|
||||
TestSuite([List tests = const []]) : testCases = <TestCase>[] {
|
||||
for (var test in tests) {
|
||||
addTest(test);
|
||||
}
|
||||
}
|
||||
|
||||
void addTest(var test) {
|
||||
if (test is Function) {
|
||||
addAsynchronousTestCase(test);
|
||||
} else {
|
||||
test.addToTestSuite(this);
|
||||
}
|
||||
}
|
||||
|
||||
void addTestCase(TestCase test) {
|
||||
testCases.add(test);
|
||||
}
|
||||
|
||||
void addAsynchronousTestCase(AsynchronousTestFunction test) {
|
||||
addTestCase(new AsynchronousTestCase(test));
|
||||
}
|
||||
|
||||
void run(TestResult result) {
|
||||
for (TestCase test in testCases) {
|
||||
test.run(result);
|
||||
}
|
||||
}
|
||||
|
||||
final List<TestCase> testCases;
|
||||
|
||||
}
|
||||
|
||||
|
||||
class TestCase {
|
||||
|
||||
TestCase();
|
||||
|
||||
void setUp() { }
|
||||
abstract void performTest();
|
||||
void tearDown() { }
|
||||
|
||||
void run(TestResult result) {
|
||||
setUp();
|
||||
result.runGuarded(this, () {
|
||||
performTest();
|
||||
tearDown();
|
||||
});
|
||||
}
|
||||
|
||||
void addToTestSuite(TestSuite suite) {
|
||||
suite.addTestCase(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class TestResult {
|
||||
|
||||
TestResult(this.runner) : errors = [], failures = [];
|
||||
|
||||
void error(String message, TestCase testCase) {
|
||||
errors.add([message, testCase]);
|
||||
}
|
||||
|
||||
void failure(String message, TestCase testCase) {
|
||||
failures.add([message, testCase]);
|
||||
}
|
||||
|
||||
runGuarded(TestCase testCase, Function fn) {
|
||||
var result = null;
|
||||
try {
|
||||
result = fn();
|
||||
} catch (ExpectException exception) {
|
||||
failure(exception.toString(), testCase);
|
||||
testCase.tearDown();
|
||||
} catch (var exception) {
|
||||
error(exception.toString(), testCase);
|
||||
testCase.tearDown();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool hasDefects() {
|
||||
return !(errors.isEmpty() && failures.isEmpty());
|
||||
}
|
||||
|
||||
final TestRunner runner;
|
||||
final List errors;
|
||||
final List failures;
|
||||
|
||||
}
|
||||
|
||||
|
||||
class TestRunner {
|
||||
|
||||
TestRunner(this.suite);
|
||||
|
||||
void run(TestResult result) {
|
||||
if (waitForDoneCallback !== null) {
|
||||
waitForDoneCallback();
|
||||
}
|
||||
suite.run(result);
|
||||
if (AsynchronousTestCase.running == 0) {
|
||||
done(result);
|
||||
}
|
||||
}
|
||||
|
||||
void done(TestResult result) {
|
||||
if (result.hasDefects()) {
|
||||
printDefects(result);
|
||||
Expect.fail("Test suite failed.");
|
||||
}
|
||||
if (doneCallback !== null) {
|
||||
doneCallback();
|
||||
}
|
||||
}
|
||||
|
||||
void printDefects(TestResult result) {
|
||||
printDefectList("Errors", result.errors);
|
||||
printDefectList("Failures", result.failures);
|
||||
}
|
||||
|
||||
static void printDefectList(String type, List defects) {
|
||||
if (!defects.isEmpty()) {
|
||||
print("$type #${defects.length}:");
|
||||
for (List defect in defects) {
|
||||
print(" - ${defect[0]}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final TestSuite suite;
|
||||
static Function waitForDoneCallback;
|
||||
static Function doneCallback;
|
||||
|
||||
}
|
||||
|
||||
|
||||
class TestExpectation {
|
||||
|
||||
TestExpectation(this.testCase, this.result);
|
||||
|
||||
void succeeded() {
|
||||
Expect.equals(0, pendingCallbacks);
|
||||
hasSucceeded = true;
|
||||
testCase.tearDown();
|
||||
}
|
||||
|
||||
void failed() {
|
||||
testCase.tearDown();
|
||||
}
|
||||
|
||||
Promise completes(Promise promise) {
|
||||
Promise result = new TestPromise(this);
|
||||
promise.then((value) { result.complete(value); });
|
||||
return result;
|
||||
}
|
||||
|
||||
Function runs0(Function fn) {
|
||||
bool ran = false; // We only check that the function is executed once.
|
||||
pendingCallbacks++;
|
||||
return () {
|
||||
if (!ran) pendingCallbacks--;
|
||||
ran = true;
|
||||
return result.runGuarded(testCase, () => fn());
|
||||
};
|
||||
}
|
||||
|
||||
Function runs1(Function fn) {
|
||||
bool ran = false; // We only check that the function is executed once.
|
||||
pendingCallbacks++;
|
||||
return (a0) {
|
||||
if (!ran) pendingCallbacks--;
|
||||
ran = true;
|
||||
return result.runGuarded(testCase, () => fn(a0));
|
||||
};
|
||||
}
|
||||
|
||||
Function runs2(Function fn) {
|
||||
bool ran = false; // We only check that the function is executed once.
|
||||
pendingCallbacks++;
|
||||
return (a0, a1) {
|
||||
if (!ran) pendingCallbacks--;
|
||||
ran = true;
|
||||
return result.runGuarded(testCase, () => fn(a0, a1));
|
||||
};
|
||||
}
|
||||
|
||||
bool hasPendingCallbacks() {
|
||||
return pendingCallbacks > 0;
|
||||
}
|
||||
|
||||
final AsynchronousTestCase testCase;
|
||||
final TestResult result;
|
||||
|
||||
int pendingCallbacks = 0;
|
||||
bool hasSucceeded = false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
class AsynchronousTestCase extends TestCase {
|
||||
|
||||
AsynchronousTestCase(this.test) : super();
|
||||
|
||||
void run(TestResult result) {
|
||||
setUp();
|
||||
result.runGuarded(this, () {
|
||||
addRunning(result);
|
||||
TestExpectation expect = new TestExpectation(this, result);
|
||||
test(expect);
|
||||
if (!expect.hasPendingCallbacks()) {
|
||||
Expect.isTrue(expect.hasSucceeded);
|
||||
tearDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void tearDown() {
|
||||
removeRunning();
|
||||
}
|
||||
|
||||
void addRunning(TestResult result) {
|
||||
if (running++ == 0) {
|
||||
keepalive = new ReceivePort.singleShot();
|
||||
keepalive.receive((message, replyTo) {
|
||||
result.runner.done(result);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void removeRunning() {
|
||||
if (--running == 0) {
|
||||
keepalive.toSendPort().send(null, null);
|
||||
keepalive = null;
|
||||
}
|
||||
}
|
||||
|
||||
AsynchronousTestFunction test;
|
||||
|
||||
static int running = 0;
|
||||
static ReceivePort keepalive = null;
|
||||
|
||||
}
|
||||
|
||||
|
||||
class TestPromise<T> extends PromiseImpl<T> {
|
||||
|
||||
TestPromise(this.expect) : super();
|
||||
|
||||
void addCompleteHandler(void completeHandler(T result)) {
|
||||
super.addCompleteHandler(expect.runs1((T result) {
|
||||
completeHandler(result);
|
||||
}));
|
||||
}
|
||||
|
||||
final TestExpectation expect;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
# 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 testing
|
||||
|
||||
def GetConfiguration(context, root):
|
||||
return testing.StandardTestConfiguration(context, root)
|
||||
@@ -0,0 +1,265 @@
|
||||
# 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.
|
||||
|
||||
# This directory contains tests that are intended to show the
|
||||
# current state of the language.
|
||||
|
||||
# In order to maintain maximum test coverage for all builds,
|
||||
# please use the following procedure to mark a test
|
||||
# failed on an architecture other than the one you are working on.
|
||||
#
|
||||
# 1) Copy the old version of the test to [runtime|compiler]/tests/dart/src
|
||||
# 2) Rename the file with a 'Legacy' prefix.
|
||||
# 3) File a bug for the failure due to the language change.
|
||||
# 4) Update the dart.status and language.status files appropriately.
|
||||
# 5) Update the language/src directory with the updated test.
|
||||
|
||||
prefix language
|
||||
|
||||
|
||||
[ $arch == ia32 || $arch == dartium ]
|
||||
ClassTest: Fail # Bug 4504458 (pseudo keyword)
|
||||
NamingTest: Fail # Bug 4504458 (pseudo keyword)
|
||||
SuperTest: Fail # Bug 4995181
|
||||
TypeVariableBoundsTest/none: Fail # Bug 5257789
|
||||
TypeVariableBoundsTest/02: Fail # Bug 5257789
|
||||
TypeVariableBoundsTest/03: Fail # Bug 5257789
|
||||
TypeVariableBoundsTest/04: Fail # Bug 5257789
|
||||
TypeVariableBoundsTest/07: Fail # Bug 5257789
|
||||
TypeVariableScopeTest/03: Fail # Bug 5349550, was 5316513
|
||||
TypeVariableScopeTest/04: Fail # Bug 5349550, was 5316513
|
||||
FauxverrideTest/01: Fail # Bug 5328413
|
||||
FauxverrideTest/02: Fail # Bug 5328413
|
||||
FauxverrideTest/03: Fail # Bug 5328413
|
||||
ImpliedInterfaceTest: Fail # Bug 5349944
|
||||
OverrideMethodWithFieldTest: Fail # Bug 5384453
|
||||
FieldOverrideTest/none: Fail # Bug 5384222
|
||||
FieldOverrideTest/01: Fail # Bug 5384222
|
||||
CallThroughNullGetterTest: Fail # Bug 4968741
|
||||
|
||||
# These bugs refer currently ongoing language discussions.
|
||||
ExampleConstructorTest: Fail # Bug 4995181
|
||||
|
||||
|
||||
# Regular bugs which should be fixed.
|
||||
SuperNegativeTest: Fail # Bug 4400118 - Language still in flux.
|
||||
ManyEchoServerTest: Skip # Bug 5103754
|
||||
CanonicalConstTest: Fail # Bug 5270133
|
||||
OverrideFieldTest/none: Fail # Bug 5384222
|
||||
GenericParameterizedExtendsTest: Skip # Bug 5391643
|
||||
|
||||
# Problems specific to dartc optimized mode
|
||||
[ ($arch == dartc || $arch == chromium) && $mode == release ]
|
||||
MethodInvocationTest: Fail # Bug 5392266
|
||||
Label2NegativeTest: Crash # Bug 5318228
|
||||
NullPointerExceptionTest: Fail # Bug 5391976
|
||||
CallThroughNullGetterTest: Fail # Bug 5391976
|
||||
Private3Test: Fail # Bug 5391976
|
||||
Switch3NegativeTest: Crash # Bug 5318228
|
||||
|
||||
# Tests that pass in release mode but fail in debug mode
|
||||
[ ($arch == dartc || $arch == chromium) && $mode == debug ]
|
||||
|
||||
|
||||
[ $arch == dartc || $arch == chromium ]
|
||||
Prefix1NegativeTest: Skip # Bug 5406175
|
||||
Prefix2NegativeTest: Skip # Bug 5406175
|
||||
Prefix3NegativeTest: Skip # Bug 5406175
|
||||
Prefix4NegativeTest: Skip # Bug 5406175
|
||||
Prefix5NegativeTest: Skip # Bug 5406175
|
||||
Prefix6NegativeTest: Skip # Bug 5406175
|
||||
Prefix7NegativeTest: Skip # Bug 5406175
|
||||
Prefix8NegativeTest: Skip # Bug 5406175
|
||||
Prefix9NegativeTest: Skip # Bug 5406175
|
||||
Prefix10NegativeTest: Skip # Bug 5406175
|
||||
Prefix11NegativeTest: Skip # Bug 5406175
|
||||
Prefix12NegativeTest: Skip # Bug 5406175
|
||||
PrefixTest: Skip # Bug 5406175
|
||||
Prefix10Test: Skip # Bug 5406175
|
||||
Prefix11Test: Skip # Bug 5406175
|
||||
Prefix12Test: Skip # Bug 5406175
|
||||
LibraryPrefixesTest: Skip # Bug 5406175
|
||||
TopLevelNonPrefixedLibraryTest: Skip # Bug 5406175
|
||||
DefaultFactoryTest: Fail # Bug 5009110
|
||||
FunctionTypeParameterNegativeTest: Fail # Bug 4568007
|
||||
ImplicitScopeTest: FAIL # Nested statements can be declarations
|
||||
ResolveTest: FAIL # 4254120 (implicit constructors)
|
||||
ConstConstructor1NegativeTest: FAIL # 5142545
|
||||
ConstConstructor2NegativeTest: FAIL # 5142545
|
||||
MathTest: FAIL # 5165080
|
||||
StringConcatTest: FAIL # 5196164
|
||||
NamedParametersTest: Fail # Implementation in progress.
|
||||
NamedParametersTypeTest: Fail # Implementation in progress.
|
||||
NamedParametersWithConversionsTest: Fail # Implementation in progress.
|
||||
BadNamedParameters2Test: Fail # Implementation in progress.
|
||||
NamedParametersNegativeTest: Skip # Implementation in progress.
|
||||
NamedParameters2NegativeTest: Skip # Implementation in progress.
|
||||
NamedParameters3NegativeTest: Skip # Implementation in progress.
|
||||
NamedParameters4NegativeTest: Skip # Implementation in progress.
|
||||
NamedParameters5NegativeTest: Skip # Implementation in progress.
|
||||
NamedParameters6NegativeTest: Skip # Implementation in progress.
|
||||
NamedParameters7NegativeTest: Skip # Implementation in progress.
|
||||
NamedParameters8NegativeTest: Skip # Implementation in progress.
|
||||
ScopeVariableTest: Fail # 5244704
|
||||
Field1NegativeTest: Fail # 5253031
|
||||
InstFieldInitializerTest: Fail # Cannot deal with static final values in const expression.
|
||||
RegExp3Test: Fail # 5299683
|
||||
InterfaceFactory3NegativeTest: Fail # 5387405
|
||||
GenericParameterizedExtendsTest: Skip # Bug 5392297
|
||||
InterfaceFactoryMultiTest: Fail # Bug 5399939
|
||||
|
||||
# Crashes in dartc.
|
||||
FunctionTypeAliasTest: Crash # Bug 4519208.
|
||||
|
||||
# Other bugs (or unimplemented features) in dartc.
|
||||
GenericTest: Fail # Bug 5393302 (missing call to super constructor)
|
||||
GenericInheritanceTest: Fail # Bug 4562150 (implicit constructors).
|
||||
Throw7NegativeTest: Fail # Bug 4208459.
|
||||
Throw3Test: Fail # Bug 4205624.
|
||||
SwitchLabelTest: Fail # Bug 4208467.
|
||||
Switch7NegativeTest: Fail # Bug 4208467.
|
||||
StackOverflowTest: Fail # Bug 4591172.
|
||||
ScopeNegativeTest: Fail # Bug 4207538.
|
||||
PseudoKWNegativeTest: Fail # Bug 4979760.
|
||||
OverriddenNoSuchMethodTest: Fail # Bug 4202974.
|
||||
ManyOverriddenNoSuchMethodTest: Fail # Bug 4202974.
|
||||
NoSuchMethodTest: Fail # Bug 4202974.
|
||||
BadNamedParametersTest: Fail # Bug 4202974.
|
||||
NumbersTest: Fail # Fails because numbers are mapped to doubles.
|
||||
LocalFunctionTest: Fail # Bug in test. Bug 4202989 (shadowing).
|
||||
LocalFunction3Test: Fail # Bug 4202974.
|
||||
|
||||
FieldNegativeTest: Fail # Bug 4207626.
|
||||
FunctionTypeAliasNegativeTest: Fail # Bug 5231617.
|
||||
ExampleConstructorTest: Fail # Bug 4205742.
|
||||
CTConstTest: Fail # Bug 4510015.
|
||||
Constructor2NegativeTest: Fail # Bug 4208594.
|
||||
ClassOverrideNegativeTest: Fail # Bug 4205768.
|
||||
BitOperationsTest: Fail # Uses bignums.
|
||||
ListLiteral3Test: Fail # Bug 4510015.
|
||||
ListTest: Fail # Bug 5146975.
|
||||
StackTraceTest: Fail # Bug 4971920.
|
||||
ExpectTest: Fail # Missing extensions to class Expect.
|
||||
DivByZeroTest: Fail # Bug 5184183
|
||||
OverrideFieldMethod1NegativeTest: Fail # Bug 5215249
|
||||
OverrideFieldMethod2NegativeTest: Fail # Bug 5215249
|
||||
OverrideFieldMethod3NegativeTest: Fail # Bug 5215249
|
||||
OverrideFieldMethod4NegativeTest: Fail # Bug 5215249
|
||||
OverrideFieldMethod5NegativeTest: Fail # Bug 5215249
|
||||
OverrideFieldMethod6NegativeTest: Fail # Bug 5215249
|
||||
DeoptimizationTest: Fail # Bug 4254120
|
||||
CharEscapeTest: Fail
|
||||
FunctionTypeParameter2NegativeTest: Fail # Bug 4568007
|
||||
|
||||
# The following tests use missing error classes. Bug 4385894.
|
||||
TypeTest: Fail # Uses TypeError class.
|
||||
AssertTest: Fail # Uses AssertError class.
|
||||
|
||||
ThirdTest: Fail # Bug 5339586
|
||||
|
||||
|
||||
|
||||
[ $arch == chromium ]
|
||||
ApplicationTest: Fail # Bug 5145731
|
||||
TopLevelMultipleFilesTest: Fail # Bug 5145731
|
||||
TopLevelNonPrefixedLibraryTest: Fail # Bug 5145731
|
||||
TopLevelPrefixedLibraryTest: Fail # Bug 5145731
|
||||
TopLevelEntryTest: Fail # Bug 5145731
|
||||
PrivateTest: Fail # Bug 5145731
|
||||
Private3Test: Fail # Bug 5145731
|
||||
|
||||
[ $arch == chromium && $mode == release ]
|
||||
Instanceof2Test: Fail, Pass # Bug 5275232
|
||||
|
||||
|
||||
[ $arch == ia32 ]
|
||||
|
||||
[ $arch == dartium ]
|
||||
Prefix1NegativeTest: Skip # Bug 5072252
|
||||
Prefix2NegativeTest: Skip # Bug 5072252
|
||||
Prefix3NegativeTest: Skip # Bug 5072252
|
||||
Prefix4NegativeTest: Skip # Bug 5072252
|
||||
Prefix5NegativeTest: Skip # Bug 5072252
|
||||
Prefix6NegativeTest: Skip # Bug 5072252
|
||||
Prefix7NegativeTest: Skip # Bug 5072252
|
||||
Prefix8NegativeTest: Skip # Bug 5072252
|
||||
Prefix9NegativeTest: Skip # Bug 5072252
|
||||
Prefix10NegativeTest: Skip # Bug 5072252
|
||||
Prefix11NegativeTest: Skip # Bug 5072252
|
||||
Prefix12NegativeTest: Skip # Bug 5072252
|
||||
PrefixTest: Skip # Bug 5072252
|
||||
Prefix10Test: Skip # Bug 5072252
|
||||
Prefix11Test: Skip # Bug 5072252
|
||||
Prefix12Test: Skip # Bug 5072252
|
||||
LibraryPrefixesTest: Skip # Bug 5072252
|
||||
TopLevelNonPrefixedLibraryTest: Skip # Bug 5072252
|
||||
ApplicationTest: Fail # Bug 5072252
|
||||
TopLevelEntryTest: Fail # Bug 5072252
|
||||
TopLevelMultipleFilesTest: Fail # Bug 5072252
|
||||
AbstractStaticNegativeTest: Skip # Bug 5408067
|
||||
|
||||
# print and println are not implemented yet
|
||||
Throw5Test: Skip
|
||||
ExceptionTest: Skip
|
||||
MathTest: Skip
|
||||
ExpectTest: Skip
|
||||
StringInterpolateTest: Skip
|
||||
ExecuteFinally7Test: Skip
|
||||
SavannahTest: Skip
|
||||
Throw2Test: Skip
|
||||
Throw3Test: Skip
|
||||
HelloDartTest: Skip
|
||||
ThrowTest: Skip
|
||||
TryCatch3Test: Skip
|
||||
StackTraceTest: Skip
|
||||
Throw1Test: Skip
|
||||
RegExp2Test: Skip
|
||||
FannkuchTest: Skip
|
||||
RichardsTest: Skip
|
||||
Throw6Test: Skip
|
||||
RegEx2Test: Skip
|
||||
DivByZeroTest: Skip
|
||||
UnboundGetterTest: Skip
|
||||
NativeTest: Skip
|
||||
Throw4Test: Skip
|
||||
ImplicitClosure1Test: Skip
|
||||
Private2Test: Skip
|
||||
|
||||
TypedMessageTest: Skip # Bug 5246195
|
||||
|
||||
# Expect is not available
|
||||
Private3Test: Skip
|
||||
Library1Test :Skip
|
||||
|
||||
[ $arch == dartium || $arch == chromium ]
|
||||
PrivateTest: Fail # Bug 5382463
|
||||
HelloScriptTest: Fail # Bug 5072252
|
||||
ImportCoreImplNoPrefixTest: Fail # Bug 5382463
|
||||
ImportCoreNoPrefixTest: Fail # Bug 5382463
|
||||
|
||||
# Bug 5293748
|
||||
GenericInstanceofTest: Skip
|
||||
GenericInheritanceTest: Skip
|
||||
GenericParameterizedExtendsTest: Skip
|
||||
MultiPassTest: Skip
|
||||
OverriddenNoSuchMethodTest: Skip
|
||||
BinaryTreesTest: Skip
|
||||
NBodyTest: Skip
|
||||
Library1Test: Skip
|
||||
ManyGenericInstanceofTest: Skip
|
||||
DeltaBlueTest: Skip
|
||||
MandelbrotTest: Skip
|
||||
DeltaBlueClosureTest: Skip
|
||||
ManyOverriddenNoSuchMethodTest: Skip
|
||||
MultiPass2Test: Skip
|
||||
RichardsTest: Skip
|
||||
|
||||
[ $arch == x64 ]
|
||||
*: Skip
|
||||
|
||||
[ $arch == simarm ]
|
||||
*: Skip
|
||||
|
||||
[ $arch == arm ]
|
||||
*: Skip
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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.
|
||||
|
||||
class A {
|
||||
abstract static foo(); // Illegal.
|
||||
}
|
||||
|
||||
main() {
|
||||
A.foo();
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// 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.
|
||||
// Dart version of two-argument Ackermann-Peter function.
|
||||
|
||||
class AckermannTest {
|
||||
static ack(m, n) {
|
||||
return m == 0 ?
|
||||
n + 1 : ((n == 0) ?
|
||||
ack(m - 1, 1) : ack(m - 1, ack(m, n - 1)));
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
Expect.equals(253, ack(3, 5));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
AckermannTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// 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.
|
||||
|
||||
class A {
|
||||
int a;
|
||||
double d1;
|
||||
double d2;
|
||||
double d3;
|
||||
double d4;
|
||||
double d5;
|
||||
double d6;
|
||||
double d7;
|
||||
double d8;
|
||||
double d9;
|
||||
double d10;
|
||||
double d11;
|
||||
double d12;
|
||||
double d13;
|
||||
double d14;
|
||||
static var s;
|
||||
|
||||
static foo() {
|
||||
return s;
|
||||
}
|
||||
|
||||
A(this.a) { }
|
||||
|
||||
value() {
|
||||
return a + foo();
|
||||
}
|
||||
}
|
||||
|
||||
class AllocateLargeObject {
|
||||
static testMain() {
|
||||
var a = new A(1);
|
||||
A.s = 4;
|
||||
Expect.equals(5, a.value());
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
AllocateLargeObject.testMain();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// 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.
|
||||
class MyAllocate {
|
||||
const MyAllocate([int value = 0]) : value_ = value;
|
||||
int getValue() { return value_; }
|
||||
final int value_;
|
||||
}
|
||||
|
||||
class AllocateTest {
|
||||
static testMain() {
|
||||
Expect.equals(900, (new MyAllocate(900)).getValue());
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
AllocateTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// 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.
|
||||
|
||||
#source('FailingMain.dart');
|
||||
@@ -0,0 +1,5 @@
|
||||
// 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.
|
||||
|
||||
#source('EmptyMain.dart');
|
||||
@@ -0,0 +1,395 @@
|
||||
// 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.
|
||||
// Dart test program to test arithmetic operations.
|
||||
|
||||
class ArithmeticTest {
|
||||
|
||||
static bool exceptionCaughtParseInt(String s) {
|
||||
try {
|
||||
Math.parseInt(s);
|
||||
return false;
|
||||
} catch (BadNumberFormatException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static bool exceptionCaughtParseDouble(String s) {
|
||||
try {
|
||||
Math.parseDouble(s);
|
||||
return false;
|
||||
} catch (BadNumberFormatException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static bool toIntThrowsBadNumberFormatException(String str) {
|
||||
// No exception allowed for parse double.
|
||||
double d = Math.parseDouble(str);
|
||||
try {
|
||||
var a = d.toInt();
|
||||
return false;
|
||||
} catch (BadNumberFormatException e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
var a = 22;
|
||||
var b = 4;
|
||||
// Smi & smi.
|
||||
Expect.equals(26, a + b);
|
||||
Expect.equals(18, a - b);
|
||||
Expect.equals(88, a * b);
|
||||
Expect.equals(5, a ~/ b);
|
||||
Expect.equals(5.5, a / b);
|
||||
Expect.equals(2.0, 10 / 5);
|
||||
Expect.equals(2, a % b);
|
||||
Expect.equals(2, a.remainder(b));
|
||||
a = 22;
|
||||
b = 4.0;
|
||||
// Smi & double.
|
||||
Expect.equals(26.0, a + b);
|
||||
Expect.equals(18.0, a - b);
|
||||
Expect.equals(88.0, a * b);
|
||||
Expect.equals(5.0, a ~/ b);
|
||||
Expect.equals(5.5, a / b);
|
||||
Expect.equals(2.0, a % b);
|
||||
Expect.equals(2.0, a.remainder(b));
|
||||
a = 22.0;
|
||||
b = 4;
|
||||
// Double & smi.
|
||||
Expect.equals(26.0, a + b);
|
||||
Expect.equals(18.0, a - b);
|
||||
Expect.equals(88.0, a * b);
|
||||
Expect.equals(5.0, a ~/ b);
|
||||
Expect.equals(5.5, a / b);
|
||||
Expect.equals(2.0, a % b);
|
||||
Expect.equals(2.0, a.remainder(b));
|
||||
a = 22.0;
|
||||
b = 4.0;
|
||||
// Double & double.
|
||||
Expect.equals(26.0, a + b);
|
||||
Expect.equals(18.0, a - b);
|
||||
Expect.equals(88.0, a * b);
|
||||
Expect.equals(5.0, a ~/ b);
|
||||
Expect.equals(5.5, a / b);
|
||||
Expect.equals(2.0, a % b);
|
||||
Expect.equals(2.0, a.remainder(b));
|
||||
|
||||
// Special int operations.
|
||||
Expect.equals(2, (2).floor());
|
||||
Expect.equals(2, (2).ceil());
|
||||
Expect.equals(2, (2).round());
|
||||
Expect.equals(2, (2).truncate());
|
||||
|
||||
Expect.equals(-2, (-2).floor());
|
||||
Expect.equals(-2, (-2).ceil());
|
||||
Expect.equals(-2, (-2).round());
|
||||
Expect.equals(-2, (-2).truncate());
|
||||
|
||||
// Note that this number fits into 53 bits of a double.
|
||||
int big = 123456789012345;
|
||||
|
||||
Expect.equals(big, big.floor());
|
||||
Expect.equals(big, big.ceil());
|
||||
Expect.equals(big, big.round());
|
||||
Expect.equals(big, big.truncate());
|
||||
big = -big;
|
||||
Expect.equals(big, big.floor());
|
||||
Expect.equals(big, big.ceil());
|
||||
Expect.equals(big, big.round());
|
||||
Expect.equals(big, big.truncate());
|
||||
|
||||
// Test if double is contagious. The assignment will check the type.
|
||||
{ double d = 1 + 1.0; }
|
||||
{ double d = 1.0 + 1; }
|
||||
{ double d = 1 * 1.0; }
|
||||
{ double d = 0 * 1.0; }
|
||||
{ double d = 1.0 * 0; }
|
||||
{ double d = 1 / 1.0; }
|
||||
{ double d = 1.0 / 0; }
|
||||
{ double d = 1 - 1.0; }
|
||||
{ double d = 1.0 - 1; }
|
||||
{ double d = big * 1.0; }
|
||||
{ double d = 1.0 * big; }
|
||||
|
||||
// Reset big to positive value.
|
||||
big = 123456789012345;
|
||||
// -- isNegative --.
|
||||
// Smi.
|
||||
Expect.equals(false, (0).isNegative());
|
||||
Expect.equals(false, (1).isNegative());
|
||||
Expect.equals(true, (-1).isNegative());
|
||||
// Big.
|
||||
Expect.equals(false, big.isNegative());
|
||||
Expect.equals(true, (-big).isNegative());
|
||||
// Double.
|
||||
// TODO(srdjan): enable the following test once isNegative works.
|
||||
// Expect.equals(true, (-0.0).isNegative());
|
||||
Expect.equals(false, (0.0).isNegative());
|
||||
Expect.equals(false, (2.0).isNegative());
|
||||
Expect.equals(true, (-2.0).isNegative());
|
||||
|
||||
// Constants.
|
||||
final nan = 0.0/0.0;
|
||||
final infinity = 1.0/0.0;
|
||||
|
||||
// -- isInfinite --.
|
||||
// Smi.
|
||||
Expect.equals(false, (0).isInfinite());
|
||||
Expect.equals(false, (1).isInfinite());
|
||||
Expect.equals(false, (-1).isInfinite());
|
||||
// Big.
|
||||
Expect.equals(false, big.isInfinite());
|
||||
Expect.equals(false, (-big).isInfinite());
|
||||
// Double.
|
||||
Expect.equals(false, (0.0).isInfinite());
|
||||
Expect.equals(true, infinity.isInfinite());
|
||||
Expect.equals(true, (-infinity).isInfinite());
|
||||
Expect.equals(false, (12.0).isInfinite());
|
||||
Expect.equals(false, (-12.0).isInfinite());
|
||||
Expect.equals(false, nan.isInfinite());
|
||||
|
||||
// -- isNaN --.
|
||||
// Smi.
|
||||
Expect.equals(false, (0).isNaN());
|
||||
Expect.equals(false, (1).isNaN());
|
||||
Expect.equals(false, (-1).isNaN());
|
||||
// Big.
|
||||
Expect.equals(false, big.isNaN());
|
||||
Expect.equals(false, (-big).isNaN());
|
||||
// Double.
|
||||
Expect.equals(true, nan.isNaN());
|
||||
Expect.equals(false, (12.0).isNaN());
|
||||
Expect.equals(false, infinity.isNaN());
|
||||
|
||||
// -- abs --.
|
||||
// Smi.
|
||||
Expect.equals(0, (0).abs());
|
||||
Expect.equals(2, (2).abs());
|
||||
Expect.equals(2, (-2).abs());
|
||||
// Big.
|
||||
Expect.equals(big, big.abs());
|
||||
Expect.equals(big, (-big).abs());
|
||||
// Double.
|
||||
Expect.equals(false, (0.0).abs().isNegative());
|
||||
Expect.equals(false, (-0.0).abs().isNegative());
|
||||
Expect.equals(2.0, (2.0).abs());
|
||||
Expect.equals(2.0, (-2.0).abs());
|
||||
|
||||
// -- ceil --.
|
||||
// Smi.
|
||||
Expect.equals(0, (0).ceil());
|
||||
Expect.equals(1, (1).ceil());
|
||||
Expect.equals(-1, (-1).ceil());
|
||||
// Big.
|
||||
Expect.equals(big, big.ceil());
|
||||
Expect.equals(-big, (-big).ceil());
|
||||
// Double.
|
||||
Expect.equals(0.0, (0.0).ceil());
|
||||
Expect.equals(false, (0.0).ceil().isNegative());
|
||||
Expect.equals(1.0, (0.1).ceil());
|
||||
Expect.equals(-0.0, (-0.0).ceil());
|
||||
Expect.equals(-0.0, (-0.3).ceil());
|
||||
// TODO(srdjan): enable the following tests once isNegative works.
|
||||
// Expect.equals(true, (-0.0).ceil().isNegative());
|
||||
// Expect.equals(true, (-0.3).ceil().isNegative());
|
||||
Expect.equals(3.0, (2.1).ceil());
|
||||
Expect.equals(-2.0, (-2.1).ceil());
|
||||
|
||||
// -- floor --.
|
||||
// Smi.
|
||||
Expect.equals(0, (0).floor());
|
||||
Expect.equals(1, (1).floor());
|
||||
Expect.equals(-1, (-1).floor());
|
||||
// Big.
|
||||
Expect.equals(big, big.floor());
|
||||
Expect.equals(-big, (-big).floor());
|
||||
// Double.
|
||||
Expect.equals(0.0, (0.0).floor());
|
||||
Expect.equals(0.0, (0.1).floor());
|
||||
Expect.equals(false, (0.0).floor().isNegative());
|
||||
Expect.equals(false, (0.1).floor().isNegative());
|
||||
Expect.equals(-0.0, (-0.0).floor());
|
||||
// TODO(srdjan): enable the following tests once isNegative works.
|
||||
// Expect.equals(true, (-0.0).floor().isNegative());
|
||||
Expect.equals(-1.0, (-0.1).floor());
|
||||
Expect.equals(2.0, (2.1).floor());
|
||||
Expect.equals(-3.0, (-2.1).floor());
|
||||
|
||||
// -- truncate --.
|
||||
// Smi.
|
||||
Expect.equals(0, (0).truncate());
|
||||
Expect.equals(1, (1).truncate());
|
||||
Expect.equals(-1, (-1).truncate());
|
||||
// Big.
|
||||
Expect.equals(big, big.truncate());
|
||||
Expect.equals(-big, (-big).truncate());
|
||||
// Double.
|
||||
Expect.equals(0.0, (0.0).truncate());
|
||||
Expect.equals(0.0, (0.1).truncate());
|
||||
Expect.equals(false, (0.0).truncate().isNegative());
|
||||
Expect.equals(false, (0.1).truncate().isNegative());
|
||||
Expect.equals(-0.0, (-0.0).truncate());
|
||||
Expect.equals(-0.0, (-0.3).truncate());
|
||||
// TODO(srdjan): enable the following tests once isNegative works.
|
||||
// Expect.equals(true, (-0.0).truncate().isNegative());
|
||||
// Expect.equals(true, (-0.3).truncate().isNegative());
|
||||
Expect.equals(2.0, (2.1).truncate());
|
||||
Expect.equals(-2.0, (-2.1).truncate());
|
||||
|
||||
double b1 = (1234567890123.0).truncate();
|
||||
double b2 = (1234567890124.0).truncate();
|
||||
Expect.equals(b2, b1 + 1.0);
|
||||
|
||||
// -- round --.
|
||||
// Smi.
|
||||
Expect.equals(0, (0).round());
|
||||
Expect.equals(1, (1).round());
|
||||
Expect.equals(-1, (-1).round());
|
||||
// Big.
|
||||
Expect.equals(big, big.round());
|
||||
Expect.equals(-big, (-big).round());
|
||||
// Double.
|
||||
Expect.equals(3.0, (2.6).round());
|
||||
Expect.equals(-3.0, (-2.6).round());
|
||||
Expect.equals(0.0, (0.0).round());
|
||||
Expect.equals(0.0, (0.1).round());
|
||||
Expect.equals(false, (0.0).round().isNegative());
|
||||
Expect.equals(false, (0.1).round().isNegative());
|
||||
Expect.equals(-0.0, (-0.0).round());
|
||||
Expect.equals(-0.0, (-0.3).round());
|
||||
Expect.equals(2.0, (2.1).round());
|
||||
Expect.equals(-2.0, (-2.1).round());
|
||||
Expect.equals(1.0, (0.5).round());
|
||||
// TODO(floitsch): enable or adapt test, once we reached conclusion on
|
||||
// b/4539188.
|
||||
// Expect.equals(-0.0, (-0.5).round());
|
||||
// TODO(srdjan): enable the following tests once isNegative works.
|
||||
// Expect.equals(true, (-0.0).round().isNegative());
|
||||
// Expect.equals(true, (-0.3).round().isNegative());
|
||||
// Expect.equals(true, (-0.5).round().isNegative());
|
||||
Expect.equals(2.0, (1.5).round());
|
||||
// TODO(floitsch): enable or adapt test, once we reached conclusion on
|
||||
// b/4539188.
|
||||
// Expect.equals(-1.0, (-1.5).round());
|
||||
Expect.equals(1.0, (0.99).round());
|
||||
|
||||
// -- toInt --.
|
||||
// Smi.
|
||||
Expect.equals(0, (0).toInt());
|
||||
Expect.equals(1, (1).toInt());
|
||||
Expect.equals(-1, (-1).toInt());
|
||||
// Type checks.
|
||||
{ int i = (0).toInt(); }
|
||||
{ int i = (1).toInt(); }
|
||||
{ int i = (-1).toInt(); }
|
||||
// Big.
|
||||
Expect.equals(big, big.toInt());
|
||||
Expect.equals(-big, (-big).toInt());
|
||||
{ int i = big.toInt(); }
|
||||
{ int i = (-big).toInt(); }
|
||||
// Double.
|
||||
Expect.equals(1234567890123, (1234567890123.0).toInt());
|
||||
Expect.equals(-1234567890123, (-1234567890123.0).toInt());
|
||||
{ int i = (1234567890123.0).toInt(); }
|
||||
{ int i = (-1234567890123.0).toInt(); }
|
||||
// 32bit Smi border cases.
|
||||
Expect.equals(-1073741824, (-1073741824.0).toInt());
|
||||
Expect.equals(-1073741825, (-1073741825.0).toInt());
|
||||
Expect.equals(1073741823, (1073741823.0).toInt());
|
||||
Expect.equals(1073741824, (1073741824.0).toInt());
|
||||
{ int i = (-1073741824.0).toInt(); }
|
||||
{ int i = (-1073741825.0).toInt(); }
|
||||
{ int i = (1073741823.0).toInt(); }
|
||||
{ int i = (1073741824.0).toInt(); }
|
||||
|
||||
// -- toDouble --.
|
||||
// Smi.
|
||||
Expect.equals(0.0, (0).toDouble());
|
||||
Expect.equals(1.0, (1).toDouble());
|
||||
Expect.equals(-1.0, (-1).toDouble());
|
||||
// Type checks.
|
||||
{ double d = (0).toDouble(); }
|
||||
{ double d = (1).toDouble(); }
|
||||
{ double d = (-1).toDouble(); }
|
||||
// Big.
|
||||
Expect.equals(big, big.toInt());
|
||||
Expect.equals(-big, (-big).toInt());
|
||||
{ int i = big.toInt(); }
|
||||
{ int i = (-big).toInt(); }
|
||||
|
||||
// Math functions.
|
||||
Expect.equals(2.0, Math.sqrt(4.0));
|
||||
Expect.equals(1.0, Math.sin(3.14159265 / 2.0));
|
||||
Expect.equals(-1.0, Math.cos(3.14159265));
|
||||
|
||||
Expect.equals(12, Math.parseInt("12"));
|
||||
Expect.equals(-12, Math.parseInt("-12"));
|
||||
Expect.equals(12345678901234567890,
|
||||
Math.parseInt("12345678901234567890"));
|
||||
Expect.equals(-12345678901234567890,
|
||||
Math.parseInt("-12345678901234567890"));
|
||||
// Type checks.
|
||||
{ int i = Math.parseInt("12"); }
|
||||
{ int i = Math.parseInt("-12"); }
|
||||
{ int i = Math.parseInt("12345678901234567890"); }
|
||||
{ int i = Math.parseInt("-12345678901234567890"); }
|
||||
|
||||
Expect.equals(1.2, Math.parseDouble("1.2"));
|
||||
Expect.equals(-1.2, Math.parseDouble("-1.2"));
|
||||
// Type checks.
|
||||
{ double d = Math.parseDouble("1.2"); }
|
||||
{ double d = Math.parseDouble("-1.2"); }
|
||||
{ double d = Math.parseDouble("0"); }
|
||||
|
||||
// Random
|
||||
{ double d = Math.random(); }
|
||||
|
||||
Expect.equals(false, exceptionCaughtParseInt("22"));
|
||||
Expect.equals(true, exceptionCaughtParseInt("alpha"));
|
||||
Expect.equals(true, exceptionCaughtParseInt("-alpha"));
|
||||
Expect.equals(false, exceptionCaughtParseDouble("22.2"));
|
||||
Expect.equals(true, exceptionCaughtParseDouble("alpha"));
|
||||
Expect.equals(true, exceptionCaughtParseDouble("-alpha"));
|
||||
|
||||
Expect.equals(false, Math.parseDouble("1.2").isNaN());
|
||||
Expect.equals(false, Math.parseDouble("1.2").isInfinite());
|
||||
|
||||
Expect.equals(true, Math.parseDouble("NaN").isNaN());
|
||||
Expect.equals(true, Math.parseDouble("Infinity").isInfinite());
|
||||
Expect.equals(true, Math.parseDouble("-Infinity").isInfinite());
|
||||
|
||||
Expect.equals(false, Math.parseDouble("NaN").isNegative());
|
||||
Expect.equals(false, Math.parseDouble("Infinity").isNegative());
|
||||
Expect.equals(true, Math.parseDouble("-Infinity").isNegative());
|
||||
|
||||
Expect.equals("NaN", Math.parseDouble("NaN").toString());
|
||||
Expect.equals("Infinity", Math.parseDouble("Infinity").toString());
|
||||
Expect.equals("-Infinity", Math.parseDouble("-Infinity").toString());
|
||||
|
||||
Expect.equals(false, toIntThrowsBadNumberFormatException("1.2"));
|
||||
Expect.equals(true, toIntThrowsBadNumberFormatException("Infinity"));
|
||||
Expect.equals(true, toIntThrowsBadNumberFormatException("-Infinity"));
|
||||
Expect.equals(true, toIntThrowsBadNumberFormatException("NaN"));
|
||||
|
||||
// Min/max
|
||||
Expect.equals(1, Math.min(1, 12));
|
||||
Expect.equals(12, Math.max(1, 12));
|
||||
Expect.equals(1.0, Math.min(1.0, 12.0));
|
||||
Expect.equals(12.0, Math.max(1.0, 12.0));
|
||||
Expect.equals(false, 1.0 < Math.min(1.0, 12.0));
|
||||
Expect.equals(true, 1.0 < Math.max(1.0, 12.0));
|
||||
|
||||
// Hashcode
|
||||
Expect.equals(false, (3.4).hashCode() == (1.2).hashCode());
|
||||
Expect.equals(true, (1.2).hashCode() == (1.2).hashCode());
|
||||
Expect.equals(false, (3).hashCode() == (1).hashCode());
|
||||
Expect.equals(true, (10).hashCode() == (10).hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ArithmeticTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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.
|
||||
|
||||
class AssertKeywordNegativeTest {
|
||||
|
||||
static void testMain() {
|
||||
assert(true);
|
||||
"assert"(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
main() {
|
||||
AssertKeywordNegativeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// 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.
|
||||
// VMOptions=--enable_asserts
|
||||
//
|
||||
// Dart test program testing assert statements.
|
||||
|
||||
class AssertTest {
|
||||
static test() {
|
||||
int i = 0;
|
||||
try {
|
||||
assert(false);
|
||||
} catch (AssertError error) {
|
||||
i = 1;
|
||||
Expect.equals("false", error.failedAssertion);
|
||||
int pos = error.url.lastIndexOf("/", error.url.length);
|
||||
if (pos == -1) {
|
||||
pos = error.url.lastIndexOf("\\", error.url.length);
|
||||
}
|
||||
String subs = error.url.substring(pos + 1, error.url.length);
|
||||
Expect.equals("AssertTest.dart", subs);
|
||||
Expect.equals(12, error.line);
|
||||
Expect.equals(14, error.column);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
static testClosure() {
|
||||
int i = 0;
|
||||
try {
|
||||
assert(() => false);
|
||||
} catch (AssertError error) {
|
||||
i = 1;
|
||||
Expect.equals("() => false", error.failedAssertion);
|
||||
int pos = error.url.lastIndexOf("/", error.url.length);
|
||||
if (pos == -1) {
|
||||
pos = error.url.lastIndexOf("\\", error.url.length);
|
||||
}
|
||||
String subs = error.url.substring(pos + 1, error.url.length);
|
||||
Expect.equals("AssertTest.dart", subs);
|
||||
Expect.equals(30, error.line);
|
||||
Expect.equals(14, error.column);
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
Expect.equals(1, test());
|
||||
Expect.equals(1, testClosure());
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
AssertTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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.
|
||||
|
||||
class A {
|
||||
A() {}
|
||||
imethod() { return 0; }
|
||||
}
|
||||
|
||||
class AssignInstanceMethodNegativeTest {
|
||||
|
||||
static testMain() {
|
||||
var a = new A();
|
||||
// Illegal, can't change a member method
|
||||
a.imethod = () { return 1; };
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
AssignInstanceMethodNegativeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
// 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.
|
||||
// Dart test program for testing assign operators.
|
||||
|
||||
|
||||
class AssignOpTest {
|
||||
AssignOpTest() {}
|
||||
|
||||
static testMain() {
|
||||
var b = 0;
|
||||
b += 1;
|
||||
Expect.equals(1, b);
|
||||
b *= 5;
|
||||
Expect.equals(5, b);
|
||||
b -= 1;
|
||||
Expect.equals(4, b);
|
||||
b ~/= 2;
|
||||
Expect.equals(2, b);
|
||||
|
||||
f = 0;
|
||||
f += 1;
|
||||
Expect.equals(1, f);
|
||||
f *= 5;
|
||||
Expect.equals(5, f);
|
||||
f -= 1;
|
||||
Expect.equals(4, f);
|
||||
f ~/= 2;
|
||||
Expect.equals(2, f);
|
||||
f /= 4;
|
||||
Expect.equals(.5, f);
|
||||
|
||||
AssignOpTest.f = 0;
|
||||
AssignOpTest.f += 1;
|
||||
Expect.equals(1, AssignOpTest.f);
|
||||
AssignOpTest.f *= 5;
|
||||
Expect.equals(5, AssignOpTest.f);
|
||||
AssignOpTest.f -= 1;
|
||||
Expect.equals(4, AssignOpTest.f);
|
||||
AssignOpTest.f ~/= 2;
|
||||
Expect.equals(2, AssignOpTest.f);
|
||||
AssignOpTest.f /= 4;
|
||||
Expect.equals(.5, f);
|
||||
|
||||
var o = new AssignOpTest();
|
||||
o.instf = 0;
|
||||
o.instf += 1;
|
||||
Expect.equals(1, o.instf);
|
||||
o.instf *= 5;
|
||||
Expect.equals(5, o.instf);
|
||||
o.instf -= 1;
|
||||
Expect.equals(4, o.instf);
|
||||
o.instf ~/= 2;
|
||||
Expect.equals(2, o.instf);
|
||||
o.instf /= 4;
|
||||
Expect.equals(.5, o.instf);
|
||||
|
||||
var x = 0xFF;
|
||||
x >>= 3;
|
||||
Expect.equals(0x1F, x);
|
||||
x <<= 3;
|
||||
Expect.equals(0xF8, x);
|
||||
x |= 0xF00;
|
||||
Expect.equals(0xFF8, x);
|
||||
x &=0xF0;
|
||||
Expect.equals(0xF0, x);
|
||||
x ^=0x11;
|
||||
Expect.equals(0xE1, x);
|
||||
}
|
||||
|
||||
static var f;
|
||||
var instf;
|
||||
}
|
||||
main() {
|
||||
AssignOpTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// 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.
|
||||
// Variable initializer must not reference the initialized variable.
|
||||
|
||||
class BadInitializer1NegativeTest {
|
||||
static testMain() {
|
||||
final List elems = const [
|
||||
const [1, 2.0, true, false, 0xffffffffff, elems], "a", "b"];
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
BadInitializer1NegativeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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.
|
||||
// Variable initializer must not reference the initialized variable.
|
||||
|
||||
class BadInitializer2NegativeTest {
|
||||
static testMain() {
|
||||
var foo = (int n) {
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1 + foo(n - 1); // <-- self-reference to closure foo.
|
||||
}
|
||||
};
|
||||
Expect.equals(4, foo(4));
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
BadInitializer2NegativeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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.
|
||||
|
||||
class BadNamedConstructorNegativeTest {
|
||||
A.foo() {}
|
||||
}
|
||||
|
||||
main() {
|
||||
BadNamedConstructorNegativeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// 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.
|
||||
// Dart test program for testing bad named parameters.
|
||||
|
||||
|
||||
class BadNamedParameters2Test {
|
||||
|
||||
int foo(int a) {
|
||||
// Although no optional named parameters are declared, we must check that
|
||||
// no named arguments are passed in, either here or in the resolving stub.
|
||||
return a;
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
BadNamedParameters2Test np = new BadNamedParameters2Test();
|
||||
|
||||
// Verify that NoSuchMethod is called after an error is detected.
|
||||
bool caught;
|
||||
try {
|
||||
caught = false;
|
||||
np.foo(b:25); // No formal parameter named b.
|
||||
} catch (NoSuchMethodException e) {
|
||||
caught = true;
|
||||
}
|
||||
Expect.equals(true, caught);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
BadNamedParameters2Test.testMain();
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
// 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.
|
||||
// Dart test program for testing bad named parameters.
|
||||
|
||||
|
||||
class BadNamedParametersTest {
|
||||
|
||||
int f42(int a, [int b = 20, int c = 30]) {
|
||||
return 100*(100*a + b) + c;
|
||||
}
|
||||
|
||||
int f52(int a, [int b = 20, int c, int d = 40]) {
|
||||
return 100*(100*(100*a + b) + (c == null ? 0 : c)) + d;
|
||||
}
|
||||
|
||||
static testMain() {
|
||||
BadNamedParametersTest np = new BadNamedParametersTest();
|
||||
|
||||
// Verify that NoSuchMethod is called after an error is detected.
|
||||
bool caught;
|
||||
try {
|
||||
caught = false;
|
||||
np.f42(10, 25, b:25); // Parameter b passed twice.
|
||||
} catch (NoSuchMethodException e) {
|
||||
caught = true;
|
||||
}
|
||||
Expect.equals(true, caught);
|
||||
try {
|
||||
caught = false;
|
||||
np.f42(10, 25, x:99); // Parameter x does not exist.
|
||||
} catch (NoSuchMethodException e) {
|
||||
caught = true;
|
||||
}
|
||||
Expect.equals(true, caught);
|
||||
try {
|
||||
caught = false;
|
||||
np.f52(10, b:25, b1:99, c:35); // Parameter b1 does not exist.
|
||||
} catch (NoSuchMethodException e) {
|
||||
caught = true;
|
||||
}
|
||||
Expect.equals(true, caught);
|
||||
try {
|
||||
caught = false;
|
||||
np.f42(10, 20, 30, 40); // Too many parameters.
|
||||
} catch (NoSuchMethodException e) {
|
||||
caught = true;
|
||||
}
|
||||
Expect.equals(true, caught);
|
||||
try {
|
||||
caught = false;
|
||||
np.f42(b:25); // Too few parameters.
|
||||
} catch (NoSuchMethodException e) {
|
||||
caught = true;
|
||||
}
|
||||
Expect.equals(true, caught);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
BadNamedParametersTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
// 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.
|
||||
// Dart test for testing bitwise operations.
|
||||
|
||||
class BitOperationsTest {
|
||||
static testMain() {
|
||||
Expect.equals(3, (3 & 7));
|
||||
Expect.equals(7, (3 | 7));
|
||||
Expect.equals(4, (3 ^ 7));
|
||||
Expect.equals(25, (100 >> 2));
|
||||
Expect.equals(400, (100 << 2));
|
||||
Expect.equals(-25, (-100 >> 2));
|
||||
Expect.equals(-101, ~100);
|
||||
Expect.equals(0x10000000000000000, 1 << 64);
|
||||
Expect.equals(-0x10000000000000000, -1 << 64);
|
||||
Expect.equals(0, ~-1);
|
||||
Expect.equals(-1, ~0);
|
||||
|
||||
Expect.equals(0, 1 >> 160);
|
||||
Expect.equals(-1, -1 >> 160);
|
||||
|
||||
Expect.equals(0x100000000000000001,
|
||||
0x100000000000000001 & 0x100000100F00000001);
|
||||
Expect.equals(0x1, 0x1 & 0x100000100F00000001);
|
||||
Expect.equals(0x1, 0x100000100F00000001 & 0x1);
|
||||
|
||||
Expect.equals(0x100000100F00000001,
|
||||
0x100000000000000001 | 0x100000100F00000001);
|
||||
Expect.equals(0x100000100F00000011, 0x11 | 0x100000100F00000001);
|
||||
Expect.equals(0x100000100F00000011, 0x100000100F00000001 | 0x11);
|
||||
|
||||
Expect.equals(0x0F000F00000000000000,
|
||||
0x0F00F00000000000001 ^ 0xFF00000000000000001);
|
||||
Expect.equals(0x31, 0xF00F00000000000001 ^ 0xF00F00000000000030);
|
||||
Expect.equals(0xF00F00000000000031, 0xF00F00000000000001 ^ 0x30);
|
||||
Expect.equals(0xF00F00000000000031, 0x30 ^ 0xF00F00000000000001);
|
||||
|
||||
Expect.equals(0xF0000000000000000F, 0xF0000000000000000F7 >> 4);
|
||||
Expect.equals(15, 0xF00000000 >> 32);
|
||||
Expect.equals(1030792151040, 16492674416655 >> 4);
|
||||
|
||||
Expect.equals(0xF0000000000000000F0, 0xF0000000000000000F << 4);
|
||||
Expect.equals(0xF00000000, 15 << 32);
|
||||
|
||||
TestNegativeValueShifts();
|
||||
TestPositiveValueShifts();
|
||||
TestNoMaskingOfShiftCount();
|
||||
}
|
||||
|
||||
static void TestNegativeValueShifts() {
|
||||
for (int value = 0; value > -100; value--) {
|
||||
for (int i = 0; i < 300; i++) {
|
||||
int b = (value << i) >> i;
|
||||
Expect.equals(value, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void TestPositiveValueShifts() {
|
||||
for (int value = 0; value < 100; value++) {
|
||||
for (int i = 0; i < 300; i++) {
|
||||
int b = (value << i) >> i;
|
||||
Expect.equals(value, b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void TestNoMaskingOfShiftCount() {
|
||||
// Shifts which would behave differently if shift count was masked into a
|
||||
// range.
|
||||
Expect.equals(0, 0 >> 256);
|
||||
Expect.equals(0, 1 >> 256);
|
||||
Expect.equals(0, 2 >> 256);
|
||||
Expect.equals(0, ShiftRight(0, 256));
|
||||
Expect.equals(0, ShiftRight(1, 256));
|
||||
Expect.equals(0, ShiftRight(2, 256));
|
||||
|
||||
for (int shift = 1; shift <= 256; shift++) {
|
||||
Expect.equals(0, ShiftRight(1, shift));
|
||||
Expect.equals(-1, ShiftRight(-1, shift));
|
||||
Expect.equals(true, ShiftLeft(1, shift) > ShiftLeft(1, shift - 1));
|
||||
}
|
||||
}
|
||||
|
||||
static int ShiftLeft(int a, int b) { return a << b; }
|
||||
static int ShiftRight(int a, int b) { return a >> b; }
|
||||
}
|
||||
|
||||
main() {
|
||||
BitOperationsTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
// 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.
|
||||
|
||||
// Dart test program for testing basic boolean properties.
|
||||
|
||||
class BoolTest {
|
||||
static void testEquality() {
|
||||
Expect.equals(true, true);
|
||||
Expect.equals(false, false);
|
||||
Expect.equals(true, true === true);
|
||||
Expect.equals(false, true === false);
|
||||
Expect.equals(true, false === false);
|
||||
Expect.equals(false, false === true);
|
||||
Expect.equals(false, true !== true);
|
||||
Expect.equals(true, true !== false);
|
||||
Expect.equals(false, false !== false);
|
||||
Expect.equals(true, false !== true);
|
||||
Expect.equals(true, true == true);
|
||||
Expect.equals(false, true == false);
|
||||
Expect.equals(true, false == false);
|
||||
Expect.equals(false, false == true);
|
||||
Expect.equals(false, true != true);
|
||||
Expect.equals(true, true != false);
|
||||
Expect.equals(false, false != false);
|
||||
Expect.equals(true, false != true);
|
||||
Expect.equals(true, true === (true == true));
|
||||
Expect.equals(true, false === (true == false));
|
||||
Expect.equals(true, true === (false == false));
|
||||
Expect.equals(true, false === (false == true));
|
||||
Expect.equals(false, true !== (true == true));
|
||||
Expect.equals(false, false !== (true == false));
|
||||
Expect.equals(false, true !== (false == false));
|
||||
Expect.equals(false, false !== (false == true));
|
||||
Expect.equals(false, false === (true == true));
|
||||
Expect.equals(false, true === (true == false));
|
||||
Expect.equals(false, false === (false == false));
|
||||
Expect.equals(false, true === (false == true));
|
||||
Expect.equals(true, false !== (true == true));
|
||||
Expect.equals(true, true !== (true == false));
|
||||
Expect.equals(true, false !== (false == false));
|
||||
Expect.equals(true, true !== (false == true));
|
||||
// Expect.equals could rely on a broken boolean equality.
|
||||
if (true == false) {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (false == true) {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (true === false) {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (false === true) {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (true == true) {
|
||||
} else {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (false == false) {
|
||||
} else {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (true === true) {
|
||||
} else {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (false === false) {
|
||||
} else {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (true != false) {
|
||||
} else {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (false != true) {
|
||||
} else {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (true !== false) {
|
||||
} else {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (false !== true) {
|
||||
} else {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (true != true) {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (false != false) {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (true !== true) {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
if (false !== false) {
|
||||
throw "Expect.equals broken";
|
||||
}
|
||||
}
|
||||
|
||||
static void testToString() {
|
||||
Expect.equals("true", true.toString());
|
||||
Expect.equals("false", false.toString());
|
||||
}
|
||||
|
||||
static void testMain() {
|
||||
testEquality();
|
||||
testToString();
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
BoolTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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.
|
||||
// Dart test for testing binary operations.
|
||||
// VMOptions=--verify_implements
|
||||
|
||||
class BootstrapTest {
|
||||
|
||||
static testMain() {
|
||||
var obj = new Object();
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
main() {
|
||||
BootstrapTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
// 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.
|
||||
// Dart test for branches. Make sure that shortcuts work, even if they have
|
||||
// to jump over several expressions.
|
||||
|
||||
class BranchesTest {
|
||||
static bool f() {
|
||||
Expect.equals("Never reached", 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void testMain() {
|
||||
int checkPointCounter = 1;
|
||||
int checkPoint1 = 0;
|
||||
int checkPoint2 = 0;
|
||||
int checkPoint3 = 0;
|
||||
int checkPoint4 = 0;
|
||||
int checkPoint5 = 0;
|
||||
int checkPoint6 = 0;
|
||||
int i = 0;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (i == 0) {
|
||||
checkPoint1 += checkPointCounter++;
|
||||
if (true || // Test branch-if-true.
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f() ||
|
||||
f()) {
|
||||
checkPoint2 += checkPointCounter++;
|
||||
}
|
||||
} else { // Test branch (jumping over the else branch).
|
||||
checkPoint3 += checkPointCounter++;
|
||||
if (false) {
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
checkPoint4 = checkPointCounter++; // Never reached.
|
||||
}
|
||||
}
|
||||
checkPoint5 += checkPointCounter++;
|
||||
}
|
||||
checkPoint6 += checkPointCounter++;
|
||||
Expect.equals(1, checkPoint1);
|
||||
Expect.equals(2, checkPoint2);
|
||||
Expect.equals(4, checkPoint3);
|
||||
Expect.equals(0, checkPoint4);
|
||||
Expect.equals(8, checkPoint5);
|
||||
Expect.equals(6, checkPoint6);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
BranchesTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// 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.
|
||||
// Dart test for breaks in for, do/while and while loops.
|
||||
|
||||
class BreakTest {
|
||||
static testMain() {
|
||||
int i;
|
||||
int forCounter = 0;
|
||||
for (i = 0; i < 10; i++) {
|
||||
forCounter++;
|
||||
if (i > 3) break;
|
||||
}
|
||||
Expect.equals(5, forCounter);
|
||||
Expect.equals(4, i);
|
||||
|
||||
i = 0;
|
||||
int doWhileCounter = 0;
|
||||
do {
|
||||
i++;
|
||||
doWhileCounter++;
|
||||
if (i > 3) break;
|
||||
} while (i < 10);
|
||||
Expect.equals(4, doWhileCounter);
|
||||
Expect.equals(4, i);
|
||||
|
||||
i = 0;
|
||||
int whileCounter = 0;
|
||||
while (i < 10) {
|
||||
i++;
|
||||
whileCounter++;
|
||||
if (i > 3) break;
|
||||
}
|
||||
Expect.equals(4, whileCounter);
|
||||
Expect.equals(4, i);
|
||||
|
||||
// Use a label to break to the outer loop.
|
||||
i = 0;
|
||||
L: while (i < 10) {
|
||||
i++;
|
||||
while (i > 5) {
|
||||
break L;
|
||||
}
|
||||
}
|
||||
Expect.equals(6, i);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
BreakTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
// 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.
|
||||
// All things regarding compile time constant expressions.
|
||||
|
||||
interface Roman {
|
||||
static final I = 1;
|
||||
static final II = 2;
|
||||
static final III = 3;
|
||||
static final IV = 4;
|
||||
static final V = 5;
|
||||
|
||||
static final VivaItalia = const {"green": 1, "red": 3, "white": 2};
|
||||
}
|
||||
|
||||
|
||||
class Point {
|
||||
static final int zero = 0;
|
||||
|
||||
static final origin = const Point(0, 0);
|
||||
static final origin2 = const Point(zero, Roman.IV - 4);
|
||||
|
||||
const Point(x, y) : x_= x, y_ = y;
|
||||
const Point.X(x) : x_ = x, y_ = Roman.V - Roman.II - 3;
|
||||
|
||||
bool operator ==(final Point other) {
|
||||
return (this.x_ == other.x_) && (this.y_ == other.y_);
|
||||
}
|
||||
|
||||
final int x_, y_;
|
||||
}
|
||||
|
||||
|
||||
class Line {
|
||||
const Line(Point begin, Point end) : beg_ = begin, end_ = end;
|
||||
final Point beg_;
|
||||
final Point end_;
|
||||
}
|
||||
|
||||
|
||||
class CTConstTest {
|
||||
static int getZero() { return 0; }
|
||||
static final naught = null;
|
||||
|
||||
static testMain() {
|
||||
Expect.equals(0, Point.zero);
|
||||
Expect.equals(0, Point.origin.x_);
|
||||
Expect.equals(true, Point.origin === Point.origin2);
|
||||
var p1 = const Point(0, 0);
|
||||
Expect.equals(true, Point.origin === p1);
|
||||
|
||||
Expect.equals(false, Point.origin == const Point(1, 1));
|
||||
Expect.equals(false, Point.origin === const Point(1, 1));
|
||||
|
||||
var p2 = new Point(0, getZero());
|
||||
Expect.equals(true, Point.origin == p2); // Point.operator==
|
||||
|
||||
Expect.equals(true, const Point.X(5) === const Point(5, 0));
|
||||
|
||||
Line l1 = const Line(Point.origin, const Point(1, 1));
|
||||
Line l2 = const Line(const Point(0, 0), const Point(1, 1));
|
||||
Line l3 = new Line(const Point(0, 0), const Point(1, 1));
|
||||
Expect.equals(true, l1 === l2);
|
||||
|
||||
final evenNumbers = const <int>[2, 2*2, 2*3, 2*4, 2*5];
|
||||
Expect.equals(true, evenNumbers === const [2, 4, 6, 8, 10]);
|
||||
|
||||
final c11dGermany1 = const {"black": 1, "red": 2, "yellow": 3};
|
||||
Expect.equals(true,
|
||||
c11dGermany1 === const {"black": 1, "red": 2, "yellow": 3});
|
||||
|
||||
final c11dGermany2 = const {"black": 1, "red": 2, "yellow": 3};
|
||||
Expect.equals(true, c11dGermany1 === c11dGermany2);
|
||||
|
||||
final c11dBelgium = const {"black": 1, "yellow": 2, "red": 3};
|
||||
Expect.equals(false, c11dGermany1 == c11dBelgium);
|
||||
Expect.equals(false, c11dGermany1 === c11dBelgium);
|
||||
|
||||
final c11dItaly = const {"green": 1, "red": 3, "white": 2};
|
||||
Expect.equals(true,
|
||||
c11dItaly === const {"green": 1, "red": 3, "white": 2});
|
||||
Expect.equals(true, c11dItaly === Roman.VivaItalia);
|
||||
|
||||
Expect.equals(3, c11dItaly.length);
|
||||
Expect.equals(3, c11dItaly.getKeys().length);
|
||||
Expect.equals(true, c11dItaly.containsKey("white"));
|
||||
Expect.equals(false, c11dItaly.containsKey("black"));
|
||||
|
||||
// Make sure the map object is immutable.
|
||||
bool caughtException = false;
|
||||
try {
|
||||
c11dItaly["green"] = 0;
|
||||
} catch (IllegalAccessException e) {
|
||||
caughtException = true;
|
||||
}
|
||||
Expect.equals(true, caughtException);
|
||||
Expect.equals(1, c11dItaly["green"]);
|
||||
|
||||
caughtException = false;
|
||||
try {
|
||||
c11dItaly.clear();
|
||||
} catch (IllegalAccessException e) {
|
||||
caughtException = true;
|
||||
}
|
||||
Expect.equals(true, caughtException);
|
||||
Expect.equals(1, c11dItaly["green"]);
|
||||
|
||||
caughtException = false;
|
||||
try {
|
||||
c11dItaly.remove("orange");
|
||||
} catch (IllegalAccessException e) {
|
||||
caughtException = true;
|
||||
}
|
||||
Expect.equals(true, caughtException);
|
||||
Expect.equals(1, c11dItaly["green"]);
|
||||
|
||||
Expect.equals(true, null === naught);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
CTConstTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
// 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.
|
||||
|
||||
// Tests that we can call functions through getters.
|
||||
|
||||
final TOP_LEVEL_CONST = 1;
|
||||
final TOP_LEVEL_CONST_REF = TOP_LEVEL_CONST;
|
||||
final TOP_LEVEL_NULL = null;
|
||||
|
||||
var topLevel;
|
||||
|
||||
class CallThroughGetterTest {
|
||||
|
||||
static void testMain() {
|
||||
testTopLevel();
|
||||
testField();
|
||||
testGetter();
|
||||
testMethod();
|
||||
testEvaluationOrder();
|
||||
}
|
||||
|
||||
static void testTopLevel() {
|
||||
topLevel = function() {
|
||||
return 2;
|
||||
};
|
||||
Expect.equals(1, TOP_LEVEL_CONST);
|
||||
Expect.equals(1, TOP_LEVEL_CONST_REF);
|
||||
Expect.equals(2, topLevel());
|
||||
|
||||
expectThrowsNotClosure(() { TOP_LEVEL_CONST(); });
|
||||
expectThrowsNotClosure(() { (TOP_LEVEL_CONST)(); });
|
||||
}
|
||||
|
||||
static void testField() {
|
||||
A a = new A();
|
||||
a.field = () => 42;
|
||||
Expect.equals(42, a.field());
|
||||
Expect.equals(42, (a.field)());
|
||||
|
||||
a.field = () => 87;
|
||||
Expect.equals(87, a.field());
|
||||
Expect.equals(87, (a.field)());
|
||||
|
||||
a.field = 99;
|
||||
expectThrowsNotClosure(() { a.field(); });
|
||||
expectThrowsNotClosure(() { (a.field)(); });
|
||||
}
|
||||
|
||||
static void testGetter() {
|
||||
A a = new A();
|
||||
a.field = () => 42;
|
||||
Expect.equals(42, a.getter());
|
||||
Expect.equals(42, (a.getter)());
|
||||
|
||||
a.field = () => 87;
|
||||
Expect.equals(87, a.getter());
|
||||
Expect.equals(87, (a.getter)());
|
||||
|
||||
a.field = 99;
|
||||
expectThrowsNotClosure(() { a.getter(); });
|
||||
expectThrowsNotClosure(() { (a.getter)(); });
|
||||
}
|
||||
|
||||
static void testMethod() {
|
||||
A a = new A();
|
||||
a.field = () => 42;
|
||||
Expect.equals(true, a.method() is Function);
|
||||
Expect.equals(42, a.method()());
|
||||
|
||||
a.field = () => 87;
|
||||
Expect.equals(true, a.method() is Function);
|
||||
Expect.equals(87, a.method()());
|
||||
|
||||
a.field = null;
|
||||
Expect.equals(null, a.method());
|
||||
}
|
||||
|
||||
static void testEvaluationOrder() {
|
||||
B b = new B();
|
||||
Expect.equals("gf", b.g0());
|
||||
b = new B();
|
||||
Expect.equals("gf", (b.g0)());
|
||||
|
||||
b = new B();
|
||||
Expect.equals("xgf", b.g1(b.x));
|
||||
b = new B();
|
||||
Expect.equals("gxf", (b.g1)(b.x));
|
||||
|
||||
b = new B();
|
||||
Expect.equals("xygf", b.g2(b.x, b.y));
|
||||
b = new B();
|
||||
Expect.equals("gxyf", (b.g2)(b.x, b.y));
|
||||
|
||||
b = new B();
|
||||
Expect.equals("xyzgf", b.g3(b.x, b.y, b.z));
|
||||
b = new B();
|
||||
Expect.equals("gxyzf", (b.g3)(b.x, b.y, b.z));
|
||||
|
||||
b = new B();
|
||||
Expect.equals("yzxgf", b.g3(b.y, b.z, b.x));
|
||||
b = new B();
|
||||
Expect.equals("gyzxf", (b.g3)(b.y, b.z, b.x));
|
||||
}
|
||||
|
||||
static void expectThrowsNotClosure(fn) {
|
||||
var exception = catchException(fn);
|
||||
if (!(exception is ObjectNotClosureException)) {
|
||||
Expect.fail("Wrong exception. Expected: ObjectNotClosureException"
|
||||
+ " got: ${exception}");
|
||||
}
|
||||
}
|
||||
|
||||
static catchException(fn) {
|
||||
bool caught = false;
|
||||
var result = null;
|
||||
try {
|
||||
fn();
|
||||
Expect.equals(true, false); // Shouldn't reach this.
|
||||
} catch (var e) {
|
||||
caught = true;
|
||||
result = e;
|
||||
}
|
||||
Expect.equals(true, caught);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class A {
|
||||
|
||||
A() { }
|
||||
var field;
|
||||
get getter() { return field; }
|
||||
method() { return field; }
|
||||
|
||||
}
|
||||
|
||||
|
||||
class B {
|
||||
|
||||
B() : _order = "" { }
|
||||
|
||||
get g0() { _mark('g'); return f0() { return _mark('f'); }; }
|
||||
get g1() { _mark('g'); return f1(x) { return _mark('f'); }; }
|
||||
get g2() { _mark('g'); return f2(x, y) { return _mark('f'); }; }
|
||||
get g3() { _mark('g'); return f3(x, y, z) { return _mark('f'); }; }
|
||||
|
||||
get x() { _mark('x'); return 0; }
|
||||
get y() { _mark('y'); return 1; }
|
||||
get z() { _mark('z'); return 2; }
|
||||
|
||||
_mark(m) { _order += m; return _order; }
|
||||
String _order;
|
||||
|
||||
}
|
||||
|
||||
main() {
|
||||
CallThroughGetterTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
// 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.
|
||||
|
||||
// Tests that we can call functions through getters which return null.
|
||||
|
||||
final TOP_LEVEL_NULL = null;
|
||||
|
||||
var topLevel;
|
||||
|
||||
class CallThroughNullGetterTest {
|
||||
|
||||
static void testMain() {
|
||||
testTopLevel();
|
||||
testField();
|
||||
testGetter();
|
||||
testMethod();
|
||||
}
|
||||
|
||||
static void testTopLevel() {
|
||||
topLevel = null;
|
||||
expectThrowsNullPointerException(() { topLevel(); });
|
||||
expectThrowsNullPointerException(() { (topLevel)(); });
|
||||
expectThrowsNullPointerException(() { TOP_LEVEL_NULL(); });
|
||||
expectThrowsNullPointerException(() { (TOP_LEVEL_NULL)(); });
|
||||
}
|
||||
|
||||
static void testField() {
|
||||
A a = new A();
|
||||
|
||||
a.field = null;
|
||||
expectThrowsNullPointerException(() { a.field(); });
|
||||
expectThrowsNullPointerException(() { (a.field)(); });
|
||||
}
|
||||
|
||||
static void testGetter() {
|
||||
A a = new A();
|
||||
|
||||
a.field = null;
|
||||
expectThrowsNullPointerException(() { a.getter(); });
|
||||
expectThrowsNullPointerException(() { (a.getter)(); });
|
||||
}
|
||||
|
||||
static void testMethod() {
|
||||
A a = new A();
|
||||
|
||||
a.field = null;
|
||||
expectThrowsNullPointerException(() { a.method()(); });
|
||||
}
|
||||
|
||||
static void expectThrowsNullPointerException(fn) {
|
||||
var exception = catchException(fn);
|
||||
if (!(exception is NullPointerException)) {
|
||||
Expect.fail("Wrong exception. Expected: NullPointerException"
|
||||
+ " got: ${exception}");
|
||||
}
|
||||
}
|
||||
|
||||
static catchException(fn) {
|
||||
bool caught = false;
|
||||
var result = null;
|
||||
try {
|
||||
fn();
|
||||
Expect.equals(true, false); // Shouldn't reach this.
|
||||
} catch (var e) {
|
||||
caught = true;
|
||||
result = e;
|
||||
}
|
||||
Expect.equals(true, caught);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
class A {
|
||||
|
||||
A() { }
|
||||
var field;
|
||||
get getter() { return field; }
|
||||
method() { return field; }
|
||||
|
||||
}
|
||||
|
||||
main() {
|
||||
CallThroughNullGetterTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// 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.
|
||||
// Check that initializers of static const fields are compile time constants.
|
||||
|
||||
class CanonicalConstTest {
|
||||
static final A = const C1();
|
||||
static final B = const C2();
|
||||
|
||||
static testMain() {
|
||||
Expect.isTrue(null===null);
|
||||
Expect.isTrue(null!==0);
|
||||
Expect.isTrue(1===1);
|
||||
Expect.isTrue(1!==2);
|
||||
Expect.isTrue(true===true);
|
||||
Expect.isTrue("so"==="so");
|
||||
Expect.isTrue(const Object()===const Object());
|
||||
Expect.isTrue(const Object()!==const C1());
|
||||
Expect.isTrue(const C1()===const C1());
|
||||
Expect.isTrue(A===const C1());
|
||||
Expect.isTrue(const C1()!==const C2());
|
||||
Expect.isTrue(B===const C2());
|
||||
// TODO(johnlenz): these two values don't currently have the same type
|
||||
// Expect.isTrue(const [1,2] === const List[1,2]);
|
||||
Expect.isTrue(const [2,1] !== const[1,2]);
|
||||
Expect.isTrue(const <int>[1,2] === const <int>[1,2]);
|
||||
Expect.isTrue(const <Object>[1,2] === const <Object>[1,2]);
|
||||
Expect.isTrue(const <int>[1,2] !== const <double>[1,2]);
|
||||
Expect.isTrue(const {"a":1, "b":2} === const {"a":1, "b":2});
|
||||
Expect.isTrue(const {"a":1, "b":2} !== const {"a":2, "b":2});
|
||||
}
|
||||
}
|
||||
|
||||
class C1 {
|
||||
const C1();
|
||||
}
|
||||
|
||||
class C2 extends C1 {
|
||||
const C2() : super();
|
||||
}
|
||||
|
||||
main() {
|
||||
CanonicalConstTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,531 @@
|
||||
// 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.
|
||||
// Dart test for reading escape sequences in string literals
|
||||
|
||||
class CharEscapeTest {
|
||||
static testMain() {
|
||||
var x00 = "\x00";
|
||||
var u0000 = "\u0000";
|
||||
var v0 = "\u{0}";
|
||||
var v00 = "\u{00}";
|
||||
var v000 = "\u{000}";
|
||||
var v0000 = "\u{0000}";
|
||||
var v00000 = "\u{00000}";
|
||||
var v000000 = "\u{000000}";
|
||||
Expect.equals(1, x00.length);
|
||||
Expect.equals(1, u0000.length);
|
||||
Expect.equals(1, v0.length);
|
||||
Expect.equals(1, v00.length);
|
||||
Expect.equals(1, v000.length);
|
||||
Expect.equals(1, v0000.length);
|
||||
Expect.equals(1, v00000.length);
|
||||
Expect.equals(1, v000000.length);
|
||||
Expect.equals(0, x00.charCodeAt(0));
|
||||
Expect.equals(0, u0000.charCodeAt(0));
|
||||
Expect.equals(0, v0.charCodeAt(0));
|
||||
Expect.equals(0, v00.charCodeAt(0));
|
||||
Expect.equals(0, v000.charCodeAt(0));
|
||||
Expect.equals(0, v0000.charCodeAt(0));
|
||||
Expect.equals(0, v00000.charCodeAt(0));
|
||||
Expect.equals(0, v000000.charCodeAt(0));
|
||||
Expect.equals("\x00", new String.fromCharCodes([0]));
|
||||
Expect.equals("\u0000", new String.fromCharCodes([0]));
|
||||
Expect.equals("\u{0}", new String.fromCharCodes([0]));
|
||||
Expect.equals("\u{00}", new String.fromCharCodes([0]));
|
||||
Expect.equals("\u{000}", new String.fromCharCodes([0]));
|
||||
Expect.equals("\u{0000}", new String.fromCharCodes([0]));
|
||||
Expect.equals("\u{00000}", new String.fromCharCodes([0]));
|
||||
Expect.equals("\u{000000}", new String.fromCharCodes([0]));
|
||||
|
||||
var x01 = "\x01";
|
||||
var u0001 = "\u0001";
|
||||
var v1 = "\u{1}";
|
||||
var v01 = "\u{01}";
|
||||
var v001 = "\u{001}";
|
||||
var v0001 = "\u{0001}";
|
||||
var v00001 = "\u{00001}";
|
||||
var v000001 = "\u{000001}";
|
||||
Expect.equals(1, x01.length);
|
||||
Expect.equals(1, u0001.length);
|
||||
Expect.equals(1, v1.length);
|
||||
Expect.equals(1, v01.length);
|
||||
Expect.equals(1, v001.length);
|
||||
Expect.equals(1, v0001.length);
|
||||
Expect.equals(1, v00001.length);
|
||||
Expect.equals(1, v000001.length);
|
||||
Expect.equals(1, x01.charCodeAt(0));
|
||||
Expect.equals(1, u0001.charCodeAt(0));
|
||||
Expect.equals(1, v1.charCodeAt(0));
|
||||
Expect.equals(1, v01.charCodeAt(0));
|
||||
Expect.equals(1, v001.charCodeAt(0));
|
||||
Expect.equals(1, v0001.charCodeAt(0));
|
||||
Expect.equals(1, v00001.charCodeAt(0));
|
||||
Expect.equals(1, v000001.charCodeAt(0));
|
||||
Expect.equals("\x01", new String.fromCharCodes([1]));
|
||||
Expect.equals("\u0001", new String.fromCharCodes([1]));
|
||||
Expect.equals("\u{1}", new String.fromCharCodes([1]));
|
||||
Expect.equals("\u{01}", new String.fromCharCodes([1]));
|
||||
Expect.equals("\u{001}", new String.fromCharCodes([1]));
|
||||
Expect.equals("\u{0001}", new String.fromCharCodes([1]));
|
||||
Expect.equals("\u{00001}", new String.fromCharCodes([1]));
|
||||
Expect.equals("\u{000001}", new String.fromCharCodes([1]));
|
||||
|
||||
var x55 = "\x55";
|
||||
var u0055 = "\u0055";
|
||||
var v55 = "\u{55}";
|
||||
var v055 = "\u{055}";
|
||||
var v0055 = "\u{0055}";
|
||||
var v00055 = "\u{00055}";
|
||||
var v000055 = "\u{000055}";
|
||||
Expect.equals(1, x55.length);
|
||||
Expect.equals(1, u0055.length);
|
||||
Expect.equals(1, v55.length);
|
||||
Expect.equals(1, v055.length);
|
||||
Expect.equals(1, v0055.length);
|
||||
Expect.equals(1, v00055.length);
|
||||
Expect.equals(1, v000055.length);
|
||||
Expect.equals(0x55, x55.charCodeAt(0));
|
||||
Expect.equals(0x55, u0055.charCodeAt(0));
|
||||
Expect.equals(0x55, v55.charCodeAt(0));
|
||||
Expect.equals(0x55, v055.charCodeAt(0));
|
||||
Expect.equals(0x55, v0055.charCodeAt(0));
|
||||
Expect.equals(0x55, v00055.charCodeAt(0));
|
||||
Expect.equals(0x55, v000055.charCodeAt(0));
|
||||
Expect.equals("\x55", new String.fromCharCodes([0x55]));
|
||||
Expect.equals("\u0055", new String.fromCharCodes([0x55]));
|
||||
Expect.equals("\u{55}", new String.fromCharCodes([0x55]));
|
||||
Expect.equals("\u{055}", new String.fromCharCodes([0x55]));
|
||||
Expect.equals("\u{0055}", new String.fromCharCodes([0x55]));
|
||||
Expect.equals("\u{00055}", new String.fromCharCodes([0x55]));
|
||||
Expect.equals("\u{000055}", new String.fromCharCodes([0x55]));
|
||||
|
||||
var x7F = "\x7F";
|
||||
var u007F = "\u007F";
|
||||
var v7F = "\u{7F}";
|
||||
var v07F = "\u{07F}";
|
||||
var v007F = "\u{007F}";
|
||||
var v0007F = "\u{0007F}";
|
||||
var v00007F = "\u{00007F}";
|
||||
Expect.equals(1, x7F.length);
|
||||
Expect.equals(1, u007F.length);
|
||||
Expect.equals(1, v7F.length);
|
||||
Expect.equals(1, v07F.length);
|
||||
Expect.equals(1, v007F.length);
|
||||
Expect.equals(1, v0007F.length);
|
||||
Expect.equals(1, v00007F.length);
|
||||
Expect.equals(0x7F, x7F.charCodeAt(0));
|
||||
Expect.equals(0x7F, u007F.charCodeAt(0));
|
||||
Expect.equals(0x7F, v7F.charCodeAt(0));
|
||||
Expect.equals(0x7F, v07F.charCodeAt(0));
|
||||
Expect.equals(0x7F, v007F.charCodeAt(0));
|
||||
Expect.equals(0x7F, v0007F.charCodeAt(0));
|
||||
Expect.equals(0x7F, v00007F.charCodeAt(0));
|
||||
Expect.equals("\x7F", new String.fromCharCodes([0x7F]));
|
||||
Expect.equals("\u007F", new String.fromCharCodes([0x7F]));
|
||||
Expect.equals("\u{7F}", new String.fromCharCodes([0x7F]));
|
||||
Expect.equals("\u{07F}", new String.fromCharCodes([0x7F]));
|
||||
Expect.equals("\u{007F}", new String.fromCharCodes([0x7F]));
|
||||
Expect.equals("\u{0007F}", new String.fromCharCodes([0x7F]));
|
||||
Expect.equals("\u{00007F}", new String.fromCharCodes([0x7F]));
|
||||
|
||||
var x80 = "\x80";
|
||||
var u0080 = "\u0080";
|
||||
var v80 = "\u{80}";
|
||||
var v080 = "\u{080}";
|
||||
var v0080 = "\u{0080}";
|
||||
var v00080 = "\u{00080}";
|
||||
var v000080 = "\u{000080}";
|
||||
Expect.equals(1, x80.length);
|
||||
Expect.equals(1, u0080.length);
|
||||
Expect.equals(1, v80.length);
|
||||
Expect.equals(1, v080.length);
|
||||
Expect.equals(1, v0080.length);
|
||||
Expect.equals(1, v00080.length);
|
||||
Expect.equals(1, v000080.length);
|
||||
Expect.equals(0x80, x80.charCodeAt(0));
|
||||
Expect.equals(0x80, u0080.charCodeAt(0));
|
||||
Expect.equals(0x80, v80.charCodeAt(0));
|
||||
Expect.equals(0x80, v080.charCodeAt(0));
|
||||
Expect.equals(0x80, v0080.charCodeAt(0));
|
||||
Expect.equals(0x80, v00080.charCodeAt(0));
|
||||
Expect.equals(0x80, v000080.charCodeAt(0));
|
||||
Expect.equals("\x80", new String.fromCharCodes([0x80]));
|
||||
Expect.equals("\u0080", new String.fromCharCodes([0x80]));
|
||||
Expect.equals("\u{80}", new String.fromCharCodes([0x80]));
|
||||
Expect.equals("\u{080}", new String.fromCharCodes([0x80]));
|
||||
Expect.equals("\u{0080}", new String.fromCharCodes([0x80]));
|
||||
Expect.equals("\u{00080}", new String.fromCharCodes([0x80]));
|
||||
Expect.equals("\u{000080}", new String.fromCharCodes([0x80]));
|
||||
|
||||
var xAA = "\xAA";
|
||||
var u00AA = "\u00AA";
|
||||
var vAA = "\u{AA}";
|
||||
var v0AA = "\u{0AA}";
|
||||
var v00AA = "\u{00AA}";
|
||||
var v000AA = "\u{000AA}";
|
||||
var v0000AA = "\u{0000AA}";
|
||||
Expect.equals(1, xAA.length);
|
||||
Expect.equals(1, u00AA.length);
|
||||
Expect.equals(1, vAA.length);
|
||||
Expect.equals(1, v0AA.length);
|
||||
Expect.equals(1, v00AA.length);
|
||||
Expect.equals(1, v000AA.length);
|
||||
Expect.equals(1, v0000AA.length);
|
||||
Expect.equals(0xAA, xAA.charCodeAt(0));
|
||||
Expect.equals(0xAA, u00AA.charCodeAt(0));
|
||||
Expect.equals(0xAA, vAA.charCodeAt(0));
|
||||
Expect.equals(0xAA, v0AA.charCodeAt(0));
|
||||
Expect.equals(0xAA, v00AA.charCodeAt(0));
|
||||
Expect.equals(0xAA, v000AA.charCodeAt(0));
|
||||
Expect.equals(0xAA, v0000AA.charCodeAt(0));
|
||||
Expect.equals("\xAA", new String.fromCharCodes([0xAA]));
|
||||
Expect.equals("\u00AA", new String.fromCharCodes([0xAA]));
|
||||
Expect.equals("\u{AA}", new String.fromCharCodes([0xAA]));
|
||||
Expect.equals("\u{0AA}", new String.fromCharCodes([0xAA]));
|
||||
Expect.equals("\u{00AA}", new String.fromCharCodes([0xAA]));
|
||||
Expect.equals("\u{000AA}", new String.fromCharCodes([0xAA]));
|
||||
Expect.equals("\u{0000AA}", new String.fromCharCodes([0xAA]));
|
||||
|
||||
var xFE = "\xFE";
|
||||
var u00FE = "\u00FE";
|
||||
var vFE = "\u{FE}";
|
||||
var v0FE = "\u{0FE}";
|
||||
var v00FE = "\u{00FE}";
|
||||
var v000FE = "\u{000FE}";
|
||||
var v0000FE = "\u{0000FE}";
|
||||
Expect.equals(1, xFE.length);
|
||||
Expect.equals(1, u00FE.length);
|
||||
Expect.equals(1, vFE.length);
|
||||
Expect.equals(1, v0FE.length);
|
||||
Expect.equals(1, v00FE.length);
|
||||
Expect.equals(1, v000FE.length);
|
||||
Expect.equals(1, v0000FE.length);
|
||||
Expect.equals(0xFE, xFE.charCodeAt(0));
|
||||
Expect.equals(0xFE, u00FE.charCodeAt(0));
|
||||
Expect.equals(0xFE, vFE.charCodeAt(0));
|
||||
Expect.equals(0xFE, v0FE.charCodeAt(0));
|
||||
Expect.equals(0xFE, v00FE.charCodeAt(0));
|
||||
Expect.equals(0xFE, v000FE.charCodeAt(0));
|
||||
Expect.equals(0xFE, v0000FE.charCodeAt(0));
|
||||
Expect.equals("\xFE", new String.fromCharCodes([0xFE]));
|
||||
Expect.equals("\u00FE", new String.fromCharCodes([0xFE]));
|
||||
Expect.equals("\u{FE}", new String.fromCharCodes([0xFE]));
|
||||
Expect.equals("\u{0FE}", new String.fromCharCodes([0xFE]));
|
||||
Expect.equals("\u{00FE}", new String.fromCharCodes([0xFE]));
|
||||
Expect.equals("\u{000FE}", new String.fromCharCodes([0xFE]));
|
||||
Expect.equals("\u{0000FE}", new String.fromCharCodes([0xFE]));
|
||||
|
||||
var xFF = "\xFF";
|
||||
var u00FF = "\u00FF";
|
||||
var vFF = "\u{FF}";
|
||||
var v0FF = "\u{0FF}";
|
||||
var v00FF = "\u{00FF}";
|
||||
var v000FF = "\u{000FF}";
|
||||
var v0000FF = "\u{0000FF}";
|
||||
Expect.equals(1, xFF.length);
|
||||
Expect.equals(1, u00FF.length);
|
||||
Expect.equals(1, vFF.length);
|
||||
Expect.equals(1, v0FF.length);
|
||||
Expect.equals(1, v00FF.length);
|
||||
Expect.equals(1, v000FF.length);
|
||||
Expect.equals(1, v0000FF.length);
|
||||
Expect.equals(0xFF, xFF.charCodeAt(0));
|
||||
Expect.equals(0xFF, u00FF.charCodeAt(0));
|
||||
Expect.equals(0xFF, vFF.charCodeAt(0));
|
||||
Expect.equals(0xFF, v0FF.charCodeAt(0));
|
||||
Expect.equals(0xFF, v00FF.charCodeAt(0));
|
||||
Expect.equals(0xFF, v000FF.charCodeAt(0));
|
||||
Expect.equals(0xFF, v0000FF.charCodeAt(0));
|
||||
Expect.equals("\xFF", new String.fromCharCodes([0xFF]));
|
||||
Expect.equals("\u00FF", new String.fromCharCodes([0xFF]));
|
||||
Expect.equals("\u{FF}", new String.fromCharCodes([0xFF]));
|
||||
Expect.equals("\u{0FF}", new String.fromCharCodes([0xFF]));
|
||||
Expect.equals("\u{00FF}", new String.fromCharCodes([0xFF]));
|
||||
Expect.equals("\u{000FF}", new String.fromCharCodes([0xFF]));
|
||||
Expect.equals("\u{0000FF}", new String.fromCharCodes([0xFF]));
|
||||
|
||||
var u1000 = "\u1000";
|
||||
var v1000 = "\u{1000}";
|
||||
var v01000 = "\u{01000}";
|
||||
var v001000 = "\u{001000}";
|
||||
Expect.equals(1, u1000.length);
|
||||
Expect.equals(1, v1000.length);
|
||||
Expect.equals(1, v01000.length);
|
||||
Expect.equals(1, v001000.length);
|
||||
Expect.equals(0x1000, u1000.charCodeAt(0));
|
||||
Expect.equals(0x1000, v1000.charCodeAt(0));
|
||||
Expect.equals(0x1000, v01000.charCodeAt(0));
|
||||
Expect.equals(0x1000, v001000.charCodeAt(0));
|
||||
Expect.equals("\u1000", new String.fromCharCodes([0x1000]));
|
||||
Expect.equals("\u{1000}", new String.fromCharCodes([0x1000]));
|
||||
Expect.equals("\u{01000}", new String.fromCharCodes([0x1000]));
|
||||
Expect.equals("\u{001000}", new String.fromCharCodes([0x1000]));
|
||||
|
||||
var u5555 = "\u5555";
|
||||
var v5555 = "\u{5555}";
|
||||
var v05555 = "\u{05555}";
|
||||
var v005555 = "\u{005555}";
|
||||
Expect.equals(1, u5555.length);
|
||||
Expect.equals(1, v5555.length);
|
||||
Expect.equals(1, v05555.length);
|
||||
Expect.equals(1, v005555.length);
|
||||
Expect.equals(0x5555, u5555.charCodeAt(0));
|
||||
Expect.equals(0x5555, v5555.charCodeAt(0));
|
||||
Expect.equals(0x5555, v05555.charCodeAt(0));
|
||||
Expect.equals(0x5555, v005555.charCodeAt(0));
|
||||
Expect.equals("\u5555", new String.fromCharCodes([0x5555]));
|
||||
Expect.equals("\u{5555}", new String.fromCharCodes([0x5555]));
|
||||
Expect.equals("\u{05555}", new String.fromCharCodes([0x5555]));
|
||||
Expect.equals("\u{005555}", new String.fromCharCodes([0x5555]));
|
||||
|
||||
var u7FFF = "\u7FFF";
|
||||
var v7FFF = "\u{7FFF}";
|
||||
var v07FFF = "\u{07FFF}";
|
||||
var v007FFF = "\u{007FFF}";
|
||||
Expect.equals(1, u7FFF.length);
|
||||
Expect.equals(1, v7FFF.length);
|
||||
Expect.equals(1, v07FFF.length);
|
||||
Expect.equals(1, v007FFF.length);
|
||||
Expect.equals(0x7FFF, u7FFF.charCodeAt(0));
|
||||
Expect.equals(0x7FFF, v7FFF.charCodeAt(0));
|
||||
Expect.equals(0x7FFF, v07FFF.charCodeAt(0));
|
||||
Expect.equals(0x7FFF, v007FFF.charCodeAt(0));
|
||||
Expect.equals("\u7FFF", new String.fromCharCodes([0x7FFF]));
|
||||
Expect.equals("\u{7FFF}", new String.fromCharCodes([0x7FFF]));
|
||||
Expect.equals("\u{07FFF}", new String.fromCharCodes([0x7FFF]));
|
||||
Expect.equals("\u{007FFF}", new String.fromCharCodes([0x7FFF]));
|
||||
|
||||
var u8000 = "\u8000";
|
||||
var v8000 = "\u{8000}";
|
||||
var v08000 = "\u{08000}";
|
||||
var v008000 = "\u{008000}";
|
||||
Expect.equals(1, u8000.length);
|
||||
Expect.equals(1, v8000.length);
|
||||
Expect.equals(1, v08000.length);
|
||||
Expect.equals(1, v008000.length);
|
||||
Expect.equals(0x8000, u8000.charCodeAt(0));
|
||||
Expect.equals(0x8000, v8000.charCodeAt(0));
|
||||
Expect.equals(0x8000, v08000.charCodeAt(0));
|
||||
Expect.equals(0x8000, v008000.charCodeAt(0));
|
||||
Expect.equals("\u8000", new String.fromCharCodes([0x8000]));
|
||||
Expect.equals("\u{8000}", new String.fromCharCodes([0x8000]));
|
||||
Expect.equals("\u{08000}", new String.fromCharCodes([0x8000]));
|
||||
Expect.equals("\u{008000}", new String.fromCharCodes([0x8000]));
|
||||
|
||||
var uAAAA = "\uAAAA";
|
||||
var vAAAA = "\u{AAAA}";
|
||||
var v0AAAA = "\u{0AAAA}";
|
||||
var v00AAAA = "\u{00AAAA}";
|
||||
Expect.equals(1, uAAAA.length);
|
||||
Expect.equals(1, vAAAA.length);
|
||||
Expect.equals(1, v0AAAA.length);
|
||||
Expect.equals(1, v00AAAA.length);
|
||||
Expect.equals(0xAAAA, uAAAA.charCodeAt(0));
|
||||
Expect.equals(0xAAAA, vAAAA.charCodeAt(0));
|
||||
Expect.equals(0xAAAA, v0AAAA.charCodeAt(0));
|
||||
Expect.equals(0xAAAA, v00AAAA.charCodeAt(0));
|
||||
Expect.equals("\uAAAA", new String.fromCharCodes([0xAAAA]));
|
||||
Expect.equals("\u{AAAA}", new String.fromCharCodes([0xAAAA]));
|
||||
Expect.equals("\u{0AAAA}", new String.fromCharCodes([0xAAAA]));
|
||||
Expect.equals("\u{00AAAA}", new String.fromCharCodes([0xAAAA]));
|
||||
|
||||
var uFFFE = "\uFFFE";
|
||||
var vFFFE = "\u{FFFE}";
|
||||
var v0FFFE = "\u{0FFFE}";
|
||||
var v00FFFE = "\u{00FFFE}";
|
||||
Expect.equals(1, uFFFE.length);
|
||||
Expect.equals(1, vFFFE.length);
|
||||
Expect.equals(1, v0FFFE.length);
|
||||
Expect.equals(1, v00FFFE.length);
|
||||
Expect.equals(0xFFFE, uFFFE.charCodeAt(0));
|
||||
Expect.equals(0xFFFE, vFFFE.charCodeAt(0));
|
||||
Expect.equals(0xFFFE, v0FFFE.charCodeAt(0));
|
||||
Expect.equals(0xFFFE, v00FFFE.charCodeAt(0));
|
||||
Expect.equals("\uFFFE", new String.fromCharCodes([0xFFFE]));
|
||||
Expect.equals("\u{FFFE}", new String.fromCharCodes([0xFFFE]));
|
||||
Expect.equals("\u{0FFFE}", new String.fromCharCodes([0xFFFE]));
|
||||
Expect.equals("\u{00FFFE}", new String.fromCharCodes([0xFFFE]));
|
||||
|
||||
var uFFFF = "\uFFFF";
|
||||
var vFFFF = "\u{FFFF}";
|
||||
var v0FFFF = "\u{0FFFF}";
|
||||
var v00FFFF = "\u{00FFFF}";
|
||||
Expect.equals(1, uFFFF.length);
|
||||
Expect.equals(1, vFFFF.length);
|
||||
Expect.equals(1, v0FFFF.length);
|
||||
Expect.equals(1, v00FFFF.length);
|
||||
Expect.equals(0xFFFF, uFFFF.charCodeAt(0));
|
||||
Expect.equals(0xFFFF, vFFFF.charCodeAt(0));
|
||||
Expect.equals(0xFFFF, v0FFFF.charCodeAt(0));
|
||||
Expect.equals(0xFFFF, v00FFFF.charCodeAt(0));
|
||||
Expect.equals("\uFFFF", new String.fromCharCodes([0xFFFF]));
|
||||
Expect.equals("\u{FFFF}", new String.fromCharCodes([0xFFFF]));
|
||||
Expect.equals("\u{0FFFF}", new String.fromCharCodes([0xFFFF]));
|
||||
Expect.equals("\u{00FFFF}", new String.fromCharCodes([0xFFFF]));
|
||||
|
||||
var v10000 = "\u{10000}";
|
||||
var v010000 = "\u{010000}";
|
||||
Expect.equals(1, v10000.length);
|
||||
Expect.equals(1, v010000.length);
|
||||
Expect.equals("\u{10000}", new String.fromCharCodes([0x10000]));
|
||||
Expect.equals("\u{010000}", new String.fromCharCodes([0x10000]));
|
||||
|
||||
var v1FFFF = "\u{1FFFF}";
|
||||
var v01FFFF = "\u{01FFFF}";
|
||||
Expect.equals(1, v1FFFF.length);
|
||||
Expect.equals(1, v01FFFF.length);
|
||||
Expect.equals("\u{1FFFF}", new String.fromCharCodes([0x1FFFF]));
|
||||
Expect.equals("\u{01FFFF}", new String.fromCharCodes([0x1FFFF]));
|
||||
|
||||
var v105555 = "\u{105555}";
|
||||
Expect.equals(1, v105555.length);
|
||||
Expect.equals("\u{105555}", new String.fromCharCodes([0x105555]));
|
||||
|
||||
var v10FFFF = "\u{10FFFF}";
|
||||
Expect.equals(1, v10FFFF.length);
|
||||
Expect.equals("\u{10FFFF}", new String.fromCharCodes([0x10FFFF]));
|
||||
|
||||
var bs = "\b";
|
||||
Expect.isTrue(bs != "b");
|
||||
Expect.equals(1, bs.length);
|
||||
Expect.equals(0x08, bs.charCodeAt(0));
|
||||
Expect.equals(bs, new String.fromCharCodes([0x08]));
|
||||
Expect.equals("\x08", bs);
|
||||
Expect.equals("\u0008", bs);
|
||||
Expect.equals("\u{8}", bs);
|
||||
Expect.equals("\u{08}", bs);
|
||||
Expect.equals("\u{008}", bs);
|
||||
Expect.equals("\u{0008}", bs);
|
||||
Expect.equals("\u{00008}", bs);
|
||||
Expect.equals("\u{000008}", bs);
|
||||
|
||||
var ht = "\t";
|
||||
Expect.isTrue(ht != "t");
|
||||
Expect.equals(1, ht.length);
|
||||
Expect.equals(0x09, ht.charCodeAt(0));
|
||||
Expect.equals(ht, new String.fromCharCodes([0x09]));
|
||||
Expect.equals("\x09", ht);
|
||||
Expect.equals("\u0009", ht);
|
||||
Expect.equals("\u{9}", ht);
|
||||
Expect.equals("\u{09}", ht);
|
||||
Expect.equals("\u{009}", ht);
|
||||
Expect.equals("\u{0009}", ht);
|
||||
Expect.equals("\u{00009}", ht);
|
||||
Expect.equals("\u{000009}", ht);
|
||||
|
||||
var lf = "\n";
|
||||
Expect.isTrue(lf != "n");
|
||||
Expect.equals(1, lf.length);
|
||||
Expect.equals(0x0A, lf.charCodeAt(0));
|
||||
Expect.equals(lf, new String.fromCharCodes([0x0A]));
|
||||
Expect.equals("\x0A", lf);
|
||||
Expect.equals("\u000A", lf);
|
||||
Expect.equals("\u{A}", lf);
|
||||
Expect.equals("\u{0A}", lf);
|
||||
Expect.equals("\u{00A}", lf);
|
||||
Expect.equals("\u{000A}", lf);
|
||||
Expect.equals("\u{0000A}", lf);
|
||||
Expect.equals("\u{00000A}", lf);
|
||||
|
||||
var vt = "\v";
|
||||
Expect.isTrue(vt != "v");
|
||||
Expect.equals(1, vt.length);
|
||||
Expect.equals(0x0B, vt.charCodeAt(0));
|
||||
Expect.equals(vt, new String.fromCharCodes([0x0B]));
|
||||
Expect.equals("\x0B", vt);
|
||||
Expect.equals("\u000B", vt);
|
||||
Expect.equals("\u{B}", vt);
|
||||
Expect.equals("\u{0B}", vt);
|
||||
Expect.equals("\u{00B}", vt);
|
||||
Expect.equals("\u{000B}", vt);
|
||||
Expect.equals("\u{0000B}", vt);
|
||||
Expect.equals("\u{00000B}", vt);
|
||||
|
||||
var ff = "\f";
|
||||
Expect.isTrue(ff != "f");
|
||||
Expect.equals(1, ff.length);
|
||||
Expect.equals(0x0C, ff.charCodeAt(0));
|
||||
Expect.equals(ff, new String.fromCharCodes([0x0C]));
|
||||
Expect.equals("\x0C", ff);
|
||||
Expect.equals("\u000C", ff);
|
||||
Expect.equals("\u{C}", ff);
|
||||
Expect.equals("\u{0C}", ff);
|
||||
Expect.equals("\u{00C}", ff);
|
||||
Expect.equals("\u{000C}", ff);
|
||||
Expect.equals("\u{0000C}", ff);
|
||||
Expect.equals("\u{00000C}", ff);
|
||||
|
||||
var cr = "\r";
|
||||
Expect.isTrue(cr != "r");
|
||||
Expect.equals(1, cr.length);
|
||||
Expect.equals(0x0D, cr.charCodeAt(0));
|
||||
Expect.equals(cr, new String.fromCharCodes([0x0D]));
|
||||
Expect.equals("\x0D", cr);
|
||||
Expect.equals("\u000D", cr);
|
||||
Expect.equals("\u{D}", cr);
|
||||
Expect.equals("\u{0D}", cr);
|
||||
Expect.equals("\u{00D}", cr);
|
||||
Expect.equals("\u{000D}", cr);
|
||||
Expect.equals("\u{0000D}", cr);
|
||||
Expect.equals("\u{00000D}", cr);
|
||||
|
||||
Expect.equals("\a", "a");
|
||||
// \b U+0006 BS
|
||||
Expect.equals("\c", "c");
|
||||
Expect.equals("\d", "d");
|
||||
Expect.equals("\e", "e");
|
||||
// \f U+000C FF
|
||||
Expect.equals("\g", "g");
|
||||
Expect.equals("\h", "h");
|
||||
Expect.equals("\i", "i");
|
||||
Expect.equals("\j", "j");
|
||||
Expect.equals("\k", "k");
|
||||
Expect.equals("\l", "l");
|
||||
Expect.equals("\m", "m");
|
||||
// \n U+000A LF
|
||||
Expect.equals("\o", "o");
|
||||
Expect.equals("\p", "p");
|
||||
Expect.equals("\q", "q");
|
||||
// \r U+000D CR
|
||||
Expect.equals("\s", "s");
|
||||
// \t U+0009 HT
|
||||
// \u code point escape
|
||||
// \v U+000B VT
|
||||
Expect.equals("\w", "w");
|
||||
// \x code point escape
|
||||
Expect.equals("\y", "y");
|
||||
Expect.equals("\z", "z");
|
||||
|
||||
Expect.equals("\A", "A");
|
||||
Expect.equals("\B", "B");
|
||||
Expect.equals("\C", "C");
|
||||
Expect.equals("\D", "D");
|
||||
Expect.equals("\E", "E");
|
||||
Expect.equals("\F", "F");
|
||||
Expect.equals("\G", "G");
|
||||
Expect.equals("\H", "H");
|
||||
Expect.equals("\I", "I");
|
||||
Expect.equals("\J", "J");
|
||||
Expect.equals("\K", "K");
|
||||
Expect.equals("\L", "L");
|
||||
Expect.equals("\M", "M");
|
||||
Expect.equals("\N", "N");
|
||||
Expect.equals("\O", "O");
|
||||
Expect.equals("\P", "P");
|
||||
Expect.equals("\Q", "Q");
|
||||
Expect.equals("\R", "R");
|
||||
Expect.equals("\S", "S");
|
||||
Expect.equals("\T", "T");
|
||||
Expect.equals("\U", "U");
|
||||
Expect.equals("\V", "V");
|
||||
Expect.equals("\W", "W");
|
||||
Expect.equals("\X", "X");
|
||||
Expect.equals("\Y", "Y");
|
||||
Expect.equals("\Z", "Z");
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
CharEscapeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// 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.
|
||||
// Check fail because of cycles in super class relationship.
|
||||
|
||||
class C extends B {
|
||||
|
||||
}
|
||||
|
||||
class A extends B {
|
||||
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
|
||||
}
|
||||
|
||||
class ClassCycleNegativeTest {
|
||||
static testMain() {
|
||||
}
|
||||
}
|
||||
main() {
|
||||
ClassCycleNegativeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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.
|
||||
// Check fails because class extends from interface.
|
||||
|
||||
interface InterfaceA {
|
||||
|
||||
}
|
||||
|
||||
class ClassA extends InterfaceA {
|
||||
|
||||
}
|
||||
|
||||
class ClassExtendsNegativeTest {
|
||||
static testMain() {
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ClassExtendsNegativeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// 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.
|
||||
|
||||
// Test that classes cannot be used as expressions.
|
||||
|
||||
class Class {
|
||||
static fisk() => 42;
|
||||
}
|
||||
|
||||
foo(x) {}
|
||||
|
||||
main() {
|
||||
if (false) {
|
||||
Class; /// 01: compile-time error
|
||||
Class(); /// 02: compile-time error
|
||||
Class.method(); /// 03: compile-time error
|
||||
Class.field; /// 04: compile-time error
|
||||
Class[0]; /// 05: compile-time error
|
||||
var x = Class; /// 06: compile-time error
|
||||
var x = Class(); /// 07: compile-time error
|
||||
var x = Class.method(); /// 08: compile-time error
|
||||
var x = Class.field; /// 09: compile-time error
|
||||
var x = Class[0]; /// 10: compile-time error
|
||||
var x = Class[0].field; /// 11: compile-time error
|
||||
var x = Class[0].method(); /// 12: compile-time error
|
||||
foo(Class); /// 13: compile-time error
|
||||
foo(Class()); /// 14: compile-time error
|
||||
foo(Class.method()); /// 15: compile-time error
|
||||
foo(Class.field); /// 16: compile-time error
|
||||
foo(Class[0]); /// 17: compile-time error
|
||||
foo(Class[0].field); /// 18: compile-time error
|
||||
foo(Class[0].method()); /// 19: compile-time error
|
||||
Class === null; /// 20: compile-time error
|
||||
null === Class; /// 21: compile-time error
|
||||
Class[0] = 91; /// 22: compile-time error
|
||||
Class++; /// 23: compile-time error
|
||||
++Class; /// 24: compile-time error
|
||||
Class / 3; /// 25: compile-time error
|
||||
Class += 3; /// 26: compile-time error
|
||||
Class[0] += 3; /// 27: compile-time error
|
||||
++Class[0]; /// 28: compile-time error
|
||||
Class[0]++; /// 29: compile-time error
|
||||
}
|
||||
Expect.equals(42, Class.fisk());
|
||||
Expect.equals(null, foo(Class.fisk()));
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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.
|
||||
// Check fail because incompatible overriding method
|
||||
|
||||
class A {
|
||||
foo() {}
|
||||
}
|
||||
|
||||
class B extends A {
|
||||
foo(a) { }
|
||||
}
|
||||
|
||||
class ClassOverrideNegativeTest {
|
||||
static testMain() {
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ClassOverrideNegativeTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
// 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.
|
||||
|
||||
// Tests basic classes and methods.
|
||||
class ClassTest {
|
||||
ClassTest() {}
|
||||
|
||||
static testMain() {
|
||||
var test = new ClassTest();
|
||||
test.testSuperCalls();
|
||||
test.testVirtualCalls();
|
||||
test.testStaticCalls();
|
||||
test.testInheritedField();
|
||||
test.testMemberRefInClosure();
|
||||
test.testFactory();
|
||||
test.testNamedConstructors();
|
||||
test.testDefaultImplementation();
|
||||
test.testFunctionParameter((int a) { return a;});
|
||||
}
|
||||
|
||||
testFunctionParameter(int func(int a)) {
|
||||
Expect.equals(1, func(1));
|
||||
}
|
||||
|
||||
testSuperCalls() {
|
||||
var sub = new Sub();
|
||||
Expect.equals(43, sub.methodX());
|
||||
Expect.equals(84, sub.methodK());
|
||||
}
|
||||
|
||||
testVirtualCalls() {
|
||||
var sub = new Sub();
|
||||
Expect.equals(41, sub.method2());
|
||||
Expect.equals(41, sub.method3());
|
||||
}
|
||||
|
||||
testStaticCalls() {
|
||||
var sub = new Sub();
|
||||
Expect.equals(-42, Sub.method4());
|
||||
Expect.equals(-41, sub.method5());
|
||||
}
|
||||
|
||||
testInheritedField() {
|
||||
var sub = new Sub();
|
||||
Expect.equals(42, sub.method6());
|
||||
}
|
||||
|
||||
testMemberRefInClosure() {
|
||||
var sub = new Sub();
|
||||
Expect.equals(1, sub.closureRef());
|
||||
Expect.equals(2, sub.closureRef());
|
||||
// Make sure it is actually on the object, not the global 'this'.
|
||||
sub = new Sub();
|
||||
Expect.equals(1, sub.closureRef());
|
||||
Expect.equals(2, sub.closureRef());
|
||||
}
|
||||
|
||||
testFactory() {
|
||||
var sup = new Sup.named();
|
||||
Expect.equals(43, sup.methodX());
|
||||
Expect.equals(84, sup.methodK());
|
||||
}
|
||||
|
||||
testNamedConstructors() {
|
||||
var sup = new Sup.fromInt(4);
|
||||
Expect.equals(4, sup.methodX());
|
||||
Expect.equals(0, sup.methodK());
|
||||
}
|
||||
|
||||
testDefaultImplementation() {
|
||||
var x = new Inter(4);
|
||||
Expect.equals(4, x.methodX());
|
||||
Expect.equals(8, x.methodK());
|
||||
|
||||
x = new Inter.fromInt(4);
|
||||
Expect.equals(4, x.methodX());
|
||||
Expect.equals(0, x.methodK());
|
||||
|
||||
x = new Inter.named();
|
||||
Expect.equals(43, x.methodX());
|
||||
Expect.equals(84, x.methodK());
|
||||
|
||||
x = new Inter.factory();
|
||||
Expect.equals(43, x.methodX());
|
||||
Expect.equals(84, x.methodK());
|
||||
}
|
||||
}
|
||||
|
||||
interface Inter factory Sup {
|
||||
Inter.named();
|
||||
Inter.fromInt(int x);
|
||||
Inter(int x);
|
||||
Inter.factory();
|
||||
int methodX();
|
||||
int methodK();
|
||||
int x_;
|
||||
}
|
||||
|
||||
class Sup implements Inter {
|
||||
int x_;
|
||||
int k_;
|
||||
|
||||
factory Sup.named() {
|
||||
return new Sub();
|
||||
}
|
||||
|
||||
factory Inter.factory() {
|
||||
return new Sub();
|
||||
}
|
||||
|
||||
Sup.fromInt(int x) {
|
||||
x_ = x;
|
||||
k_ = 0;
|
||||
}
|
||||
|
||||
int methodX() {
|
||||
return x_;
|
||||
}
|
||||
|
||||
int methodK() {
|
||||
return k_;
|
||||
}
|
||||
|
||||
Sup(int x) : this.x_ = x {
|
||||
k_ = x * 2;
|
||||
}
|
||||
|
||||
int method2() {
|
||||
return x_ - 1;
|
||||
}
|
||||
}
|
||||
|
||||
class Sub extends Sup {
|
||||
int y_;
|
||||
|
||||
// Override
|
||||
int methodX() {
|
||||
return super.methodX() + 1;
|
||||
}
|
||||
|
||||
int method3() {
|
||||
return method2();
|
||||
}
|
||||
|
||||
static int method4() {
|
||||
return -42;
|
||||
}
|
||||
|
||||
int method5() {
|
||||
return method4() + 1;
|
||||
}
|
||||
|
||||
int method6() {
|
||||
return x_ + y_;
|
||||
}
|
||||
|
||||
int closureRef() {
|
||||
var f = () {y_ += 1; return y_;};
|
||||
return f();
|
||||
}
|
||||
|
||||
Sub() : super(42) {
|
||||
y_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ClassTest.testMain();
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// 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.
|
||||
// Dart test for closures.
|
||||
|
||||
class ClosureBreak1 {
|
||||
ClosureBreak1(this.field);
|
||||
int field;
|
||||
}
|
||||
|
||||
class ClosureBreak1Test {
|
||||
static testMain() {
|
||||
var o1 = new ClosureBreak1(3);
|
||||
String newstr = "abcdefgh";
|
||||
foo() {
|
||||
o1.field++;
|
||||
Expect.equals(8, newstr.length);
|
||||
}
|
||||
bool loop = true;
|
||||
L:
|
||||
while (loop) {
|
||||
String newstr1 = "abcd";
|
||||
var o2 = new ClosureBreak1(3);
|
||||
foo1() {
|
||||
o2.field++;
|
||||
Expect.equals(4, newstr1.length);
|
||||
}
|
||||
Expect.equals(4, newstr1.length);
|
||||
while (loop) {
|
||||
int newint = 0;
|
||||
var o3 = new ClosureBreak1(3);
|
||||
foo2() {
|
||||
o3.field++;
|
||||
Expect.equals(0, newint);
|
||||
}
|
||||
foo2();
|
||||
break L;
|
||||
}
|
||||
}
|
||||
foo();
|
||||
Expect.equals(4, o1.field);
|
||||
}
|
||||
}
|
||||
|
||||
main() {
|
||||
ClosureBreak1Test.testMain();
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user