Migrate corelib/ tests starting with b-d to NNBD.

Note that cast_test.dart isn't migrated yet due to #39517.

First patch set is just copying the tests over. The second patchset has
the actual meaningful changes.

Change-Id: I89233f20187b4305a776f865cde09a984423fa4f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/125920
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
This commit is contained in:
Robert Nystrom 2019-12-02 23:13:10 +00:00 committed by commit-bot@chromium.org
parent 18a9b29687
commit a4d799c402
62 changed files with 9338 additions and 0 deletions

View file

@ -0,0 +1,81 @@
// Copyright (c) 2018, 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.
// Testing Bigints with and without intrinsics.
// VMOptions=--intrinsify --no-enable-asserts
// VMOptions=--intrinsify --enable-asserts
// VMOptions=--no-intrinsify --enable-asserts
// VMOptions=--optimization-counter-threshold=5 --no-background-compilation
import "package:expect/expect.dart";
import 'dart:math' show pow;
main() {
// Test integers.
testInt(0);
for (int i = 0; i < 63; i++) {
var n = pow(2, i) as int;
testInt(-n - 1);
testInt(-n);
testInt(-n + 1);
testInt(n - 1);
testInt(n);
testInt(n + 1);
}
testInt(-0x8000000000000000);
double dPow(num x, num exponent) => pow(x, exponent) as double;
// Test doubles.
testDouble(0.0);
testDouble(-0.0, 0.0);
for (double d in [
1.0,
2.0,
dPow(2.0, 30) - 1,
dPow(2.0, 30),
dPow(2.0, 31) - 1,
dPow(2.0, 31),
dPow(2.0, 31) + 1,
dPow(2.0, 32) - 1,
dPow(2.0, 32),
dPow(2.0, 32) + 1,
dPow(2.0, 52) - 1,
dPow(2.0, 52),
dPow(2.0, 52) + 1,
dPow(2.0, 53) - 1,
dPow(2.0, 53),
]) {
for (int p = 0; p < 1024; p++) {
var value = (d * pow(2.0, p)); // Valid integer value.
if (!value.isFinite) break;
testDouble(-value);
testDouble(value);
}
}
Expect.equals(BigInt.zero, new BigInt.from(0.5));
Expect.equals(BigInt.one, new BigInt.from(1.5));
Expect.throws(() => new BigInt.from(double.infinity));
Expect.throws(() => new BigInt.from(-double.infinity));
Expect.throws(() => new BigInt.from(double.nan));
}
testInt(int n) {
var bigint = new BigInt.from(n);
Expect.equals(n, bigint.toInt());
// If the integers are inexact (e.g. > 2^53 when represented by doubles as
// when compiled to JavaScript numbers) then the 'toString' might be rounded
// to the nearest equivalent 'nicer looking' number.
if (n == n + 1 || n == n - 1) return;
Expect.equals("$n", "$bigint");
}
testDouble(double input, [double? expectation]) {
var bigint = new BigInt.from(input);
Expect.equals(expectation ?? input, bigint.toDouble());
}

View file

@ -0,0 +1,46 @@
// Copyright (c) 2018, 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.
// Testing Bigints with and without intrinsics.
// VMOptions=--intrinsify --no-enable-asserts
// VMOptions=--intrinsify --enable-asserts
// VMOptions=--no-intrinsify --enable-asserts
// VMOptions=--optimization-counter-threshold=5 --no-background-compilation
// Test for JavaScript specific BigInt behaviour. Any JavaScript number (double)
// that is an integral value is a Dart 'int' value, so any BigInt that has a
// value that is exactly a double integral value should return `true` for
// [BigInt.isValidInt].
import "package:expect/expect.dart";
int intPow(int a, int p) {
int result = 1;
for (int i = 0; i < p; i++) result *= a;
return result;
}
int pow2_53 = intPow(2, 53);
test(int n1, int n2, int shift, [bool expectedIsValidInt = true]) {
var n = (new BigInt.from(n1) * new BigInt.from(n2)) << shift;
Expect.equals(expectedIsValidInt, n.isValidInt, '${n}.isValidInt');
if (n >= new BigInt.from(pow2_53)) {
var nplus1 = n + BigInt.one;
Expect.isFalse(nplus1.isValidInt, '${nplus1}.isValidInt');
}
}
main() {
test(13, 19, 1);
test(19997, 19993, 100);
test(19997, pow2_53 ~/ 19997, 0);
test(19997, pow2_53 ~/ 19997, 1);
test(19997, pow2_53 ~/ 19997, 100);
test(1, 1, 100);
test(1, 1, 10000, false);
// More than 53 bits in product,
test(pow2_53 ~/ 3, pow2_53 ~/ 5, 0, false);
}

View file

@ -0,0 +1,140 @@
// Copyright (c) 2017, 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.
// Testing Bigints with and without intrinsics.
// VMOptions=--intrinsify --no-enable-asserts
// VMOptions=--intrinsify --enable-asserts
// VMOptions=--no-intrinsify --enable-asserts
// VMOptions=--optimization-counter-threshold=5 --no-background-compilation
import "package:expect/expect.dart";
import 'dart:math' show pow;
void testParseRadix() {
bool checkedMode = false;
assert((checkedMode = true));
const String oneByteWhiteSpace = "\x09\x0a\x0b\x0c\x0d\x20"
// "\x85" // Might make troubles on some systems. Was marked as OK test.
"\xa0";
const String whiteSpace = "$oneByteWhiteSpace\u1680"
"\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a"
"\u2028\u2029\u202f\u205f\u3000\ufeff";
var digits = "0123456789abcdefghijklmnopqrstuvwxyz";
var zeros = "0" * 64;
for (int i = 0; i < whiteSpace.length; i++) {
var ws = whiteSpace[i];
Expect.equals(BigInt.zero, BigInt.parse("${ws}0${ws}", radix: 2));
}
void testParse(BigInt result, String radixString, int radix) {
var m = "$radixString/$radix->$result";
Expect.equals(
result, BigInt.parse(radixString.toLowerCase(), radix: radix), m);
Expect.equals(
result, BigInt.parse(radixString.toUpperCase(), radix: radix), m);
Expect.equals(result, BigInt.parse(" $radixString", radix: radix), m);
Expect.equals(result, BigInt.parse("$radixString ", radix: radix), m);
Expect.equals(result, BigInt.parse(" $radixString ", radix: radix), m);
Expect.equals(result, BigInt.parse("+$radixString", radix: radix), m);
Expect.equals(result, BigInt.parse(" +$radixString", radix: radix), m);
Expect.equals(result, BigInt.parse("+$radixString ", radix: radix), m);
Expect.equals(result, BigInt.parse(" +$radixString ", radix: radix), m);
Expect.equals(-result, BigInt.parse("-$radixString", radix: radix), m);
Expect.equals(-result, BigInt.parse(" -$radixString", radix: radix), m);
Expect.equals(-result, BigInt.parse("-$radixString ", radix: radix), m);
Expect.equals(-result, BigInt.parse(" -$radixString ", radix: radix), m);
Expect.equals(
result,
BigInt.parse("$oneByteWhiteSpace$radixString$oneByteWhiteSpace",
radix: radix),
m);
Expect.equals(
-result,
BigInt.parse("$oneByteWhiteSpace-$radixString$oneByteWhiteSpace",
radix: radix),
m);
Expect.equals(result,
BigInt.parse("$whiteSpace$radixString$whiteSpace", radix: radix), m);
Expect.equals(-result,
BigInt.parse("$whiteSpace-$radixString$whiteSpace", radix: radix), m);
Expect.equals(result, BigInt.parse("$zeros$radixString", radix: radix), m);
Expect.equals(result, BigInt.parse("+$zeros$radixString", radix: radix), m);
Expect.equals(
-result, BigInt.parse("-$zeros$radixString", radix: radix), m);
}
for (int r = 2; r <= 36; r++) {
for (var i = BigInt.zero; i <= new BigInt.from(r * r); i += BigInt.one) {
String radixString = i.toRadixString(r);
testParse(i, radixString, r);
}
}
for (int i = 2; i <= 36; i++) {
var digit = digits[i - 1];
testParse(new BigInt.from(i).pow(64) - BigInt.one, digit * 64, i);
testParse(BigInt.zero, zeros, i);
}
// Allow both upper- and lower-case letters.
Expect.equals(new BigInt.from(0xABCD), BigInt.parse("ABCD", radix: 16));
Expect.equals(new BigInt.from(0xABCD), BigInt.parse("abcd", radix: 16));
Expect.equals(new BigInt.from(15628859), BigInt.parse("09azAZ", radix: 36));
Expect.equals(
(new BigInt.from(0x12345678) << 96) +
(new BigInt.from(0x12345678) << 64) +
(new BigInt.from(0x12345678) << 32) +
new BigInt.from(0x12345678),
BigInt.parse("0x12345678123456781234567812345678"));
// Allow whitespace before and after the number.
Expect.equals(BigInt.one, BigInt.parse(" 1", radix: 2));
Expect.equals(BigInt.one, BigInt.parse("1 ", radix: 2));
Expect.equals(BigInt.one, BigInt.parse(" 1 ", radix: 2));
Expect.equals(BigInt.one, BigInt.parse("\n1", radix: 2));
Expect.equals(BigInt.one, BigInt.parse("1\n", radix: 2));
Expect.equals(BigInt.one, BigInt.parse("\n1\n", radix: 2));
Expect.equals(BigInt.one, BigInt.parse("+1", radix: 2));
void testFails(String source, int? radix) {
Expect.throws(() {
BigInt.parse(source, radix: radix);
}, (e) => e is FormatException, "$source/$radix");
}
for (int i = 2; i < 36; i++) {
var char = i.toRadixString(36);
testFails(char.toLowerCase(), i);
testFails(char.toUpperCase(), i);
}
testFails("", 2);
testFails("+ 1", 2); // No space between sign and digits.
testFails("- 1", 2); // No space between sign and digits.
testFails("0x", null);
for (int i = 2; i <= 33; i++) {
// No 0x specially allowed.
// At radix 34 and above, "x" is a valid digit.
testFails("0x10", i);
}
testBadArguments(String source, int radix) {
// If the types match, it should be an ArgumentError of some sort.
Expect.throws(
() => BigInt.parse(source, radix: radix), (e) => e is ArgumentError);
}
testBadArguments("0", -1);
testBadArguments("0", 0);
testBadArguments("0", 1);
testBadArguments("0", 37);
}
main() {
testParseRadix();
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,184 @@
// Copyright (c) 2013, 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.
// Testing int.bitLength, int.toUnsigned and int.toSigned.
library bit_twiddling_test;
import "package:expect/expect.dart";
testBitLength() {
check(int i, width) {
Expect.equals(width, i.bitLength, '$i.bitLength == $width');
// (~i) written as (-i-1) to avoid issues with limited range of dart2js ops.
Expect.equals(width, (-i - 1).bitLength, '(~$i).bitLength == $width');
}
check(0, 0);
check(1, 1);
check(2, 2);
check(3, 2);
check(4, 3);
check(5, 3);
check(6, 3);
check(7, 3);
check(8, 4);
check(127, 7);
check(128, 8);
check(129, 8);
check(2147483646, 31);
check(2147483647, 31);
check(2147483648, 32);
check(2147483649, 32);
check(4294967295, 32);
check(4294967296, 33);
check(0xffffffffff, 40);
check(0xfffffffffff, 44);
check(0xffffffffffff, 48);
check(0x1000000000000, 49);
check(0x1000000000001, 49);
check(0x1ffffffffffff, 49);
check(0x2000000000000, 50);
check(0x2000000000001, 50);
check(0xffffffffffffff, 56); // //# int64: ok
check(0x7fffffffffffffff, 63); // //# int64: continued
check(0xffffffffffffffff, 0); // //# int64: continued
}
testToUnsigned() {
checkU(src, width, expected) {
Expect.equals(expected, src.toUnsigned(width));
}
checkU(1, 8, 1);
checkU(0xff, 8, 0xff);
checkU(0xffff, 8, 0xff);
checkU(-1, 8, 0xff);
checkU(0xffffffff, 32, 0xffffffff);
checkU(0x7fffffff, 30, 0x3fffffff);
checkU(0x7fffffff, 31, 0x7fffffff);
checkU(0x7fffffff, 32, 0x7fffffff);
checkU(0x80000000, 30, 0);
checkU(0x80000000, 31, 0);
checkU(0x80000000, 32, 0x80000000);
checkU(0xffffffff, 30, 0x3fffffff);
checkU(0xffffffff, 31, 0x7fffffff);
checkU(0xffffffff, 32, 0xffffffff);
checkU(0x100000000, 30, 0);
checkU(0x100000000, 31, 0);
checkU(0x100000000, 32, 0);
checkU(0x1ffffffff, 30, 0x3fffffff);
checkU(0x1ffffffff, 31, 0x7fffffff);
checkU(0x1ffffffff, 32, 0xffffffff);
checkU(-1, 0, 0);
checkU(0, 0, 0);
checkU(1, 0, 0);
checkU(2, 0, 0);
checkU(3, 0, 0);
checkU(-1, 1, 1);
checkU(0, 1, 0);
checkU(1, 1, 1);
checkU(2, 1, 0);
checkU(3, 1, 1);
checkU(4, 1, 0);
checkU(-1, 2, 3);
checkU(0, 2, 0);
checkU(1, 2, 1);
checkU(2, 2, 2);
checkU(3, 2, 3);
checkU(4, 2, 0);
checkU(-1, 3, 7);
checkU(0, 3, 0);
checkU(1, 3, 1);
checkU(2, 3, 2);
checkU(3, 3, 3);
checkU(4, 3, 4);
checkU(0x0100000000000001, 2, 1); // //# int64: continued
checkU(0x0200000000000001, 60, 0x200000000000001); // //# int64: continued
checkU(0x0200000000000001, 59, 0x200000000000001); // //# int64: continued
checkU(0x0200000000000001, 58, 0x200000000000001); // //# int64: continued
checkU(0x0200000000000001, 57, 1); // //# int64: continued
checkU(0x8100000000000001, 2, 1); // //# int64: continued
checkU(0x8200000000000001, 60, 0x200000000000001); // //# int64: continued
checkU(0x8200000000000001, 59, 0x200000000000001); // //# int64: continued
checkU(0x8200000000000001, 58, 0x200000000000001); // //# int64: continued
checkU(0x8200000000000001, 57, 1); // //# int64: continued
}
testToSigned() {
checkS(src, width, expected) {
Expect.equals(
expected, src.toSigned(width), '$src.toSigned($width) == $expected');
}
checkS(1, 8, 1);
checkS(0xff, 8, -1);
checkS(0xffff, 8, -1);
checkS(-1, 8, -1);
checkS(128, 8, -128);
checkS(0xffffffff, 32, -1);
checkS(0x7fffffff, 30, -1);
checkS(0x7fffffff, 31, -1);
checkS(0x7fffffff, 32, 0x7fffffff);
checkS(0x80000000, 30, 0);
checkS(0x80000000, 31, 0);
checkS(0x80000000, 32, -2147483648);
checkS(0xffffffff, 30, -1);
checkS(0xffffffff, 31, -1);
checkS(0xffffffff, 32, -1);
checkS(0x100000000, 30, 0);
checkS(0x100000000, 31, 0);
checkS(0x100000000, 32, 0);
checkS(0x1ffffffff, 30, -1);
checkS(0x1ffffffff, 31, -1);
checkS(0x1ffffffff, 32, -1);
checkS(-1, 1, -1);
checkS(0, 1, 0);
checkS(1, 1, -1); // The only bit is the sign bit.
checkS(2, 1, 0);
checkS(3, 1, -1);
checkS(4, 1, 0);
checkS(-1, 2, -1);
checkS(0, 2, 0);
checkS(1, 2, 1);
checkS(2, 2, -2);
checkS(3, 2, -1);
checkS(4, 2, 0);
checkS(-1, 3, -1);
checkS(0, 3, 0);
checkS(1, 3, 1);
checkS(2, 3, 2);
checkS(3, 3, 3);
checkS(4, 3, -4);
checkS(0x0100000000000001, 2, 1); // //# int64: continued
checkS(0x0200000000000001, 60, 0x200000000000001); // //# int64: continued
checkS(0x0200000000000001, 59, 0x200000000000001); // //# int64: continued
checkS(0x0200000000000001, 58, -0x200000000000000 + 1); // //# int64: continued
checkS(0x0200000000000001, 57, 1); // //# int64: continued
checkS(0x8100000000000001, 2, 1); // //# int64: continued
checkS(0x8200000000000001, 60, 0x200000000000001); // //# int64: continued
checkS(0x8200000000000001, 59, 0x200000000000001); // //# int64: continued
checkS(0x8200000000000001, 58, -0x200000000000000 + 1); // //# int64: continued
checkS(0x8200000000000001, 57, 1); // //# int64: continued
}
main() {
testBitLength();
testToUnsigned();
testToSigned();
}

View file

@ -0,0 +1,11 @@
// Copyright (c) 2013, 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() {
const bool.fromEnvironment('NOT_FOUND', defaultValue: ''); // //# 01: compile-time error
const bool.fromEnvironment('NOT_FOUND', defaultValue: 1); // //# 02: compile-time error
const bool.fromEnvironment(null); // //# 03: compile-time error
const bool.fromEnvironment(1); // //# 04: compile-time error
const bool.fromEnvironment([]); // //# 05: compile-time error
}

View file

@ -0,0 +1,15 @@
// Copyright (c) 2013, 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.
// SharedOptions=-Da=true -Db=false -Dc=NOTBOOL -Dd=True
import "package:expect/expect.dart";
main() {
Expect.isTrue(const bool.fromEnvironment('a'));
Expect.isFalse(const bool.fromEnvironment('b'));
Expect.isTrue(const bool.fromEnvironment('c', defaultValue: true));
Expect.isFalse(const bool.fromEnvironment('c', defaultValue: false));
Expect.isFalse(const bool.fromEnvironment('d', defaultValue: false));
Expect.equals(const bool.fromEnvironment('dart.isVM'), !identical(1.0, 1));
}

View file

@ -0,0 +1,15 @@
// Copyright (c) 2012, 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 "package:expect/expect.dart";
class BoolHashCodeTest {
static testMain() {
Expect.notEquals(true.hashCode, false.hashCode);
}
}
main() {
BoolHashCodeTest.testMain();
}

View file

@ -0,0 +1,46 @@
// Copyright (c) 2017, 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 "package:expect/expect.dart";
main() {
void test(bool b1, bool b2) {
var and1 = b1 && b2;
var and2 = b1 & b2;
var and3 = b1 ? b2 ? true : false : false;
var or1 = b1 || b2;
var or2 = b1 | b2;
var or3 = b1 ? true : b2 ? true : false;
var xor1 = b1 != b2;
var xor2 = b1 ^ b2;
var xor3 = b1 ? b2 ? false : true : b2 ? true : false;
var nb1 = !b1;
var nb2 = !b2;
Expect.equals(and3, and1);
Expect.equals(and3, and2);
Expect.equals(or3, or1);
Expect.equals(or3, or2);
Expect.equals(xor3, xor1);
Expect.equals(xor3, xor2);
Expect.notEquals(nb1, b1);
Expect.notEquals(nb2, b2);
}
test(true, false);
test(true, true);
test(false, true);
test(false, false);
Expect.isTrue(true || (throw "unreachable"));
Expect.throws(() => false || (throw "unreachable"));
Expect.isFalse(false && (throw "unreachable"));
Expect.throws(() => true && (throw "unreachable"));
Expect.throws(() => true | (throw "unreachable"));
Expect.throws(() => false | (throw "unreachable"));
Expect.throws(() => true & (throw "unreachable"));
Expect.throws(() => false & (throw "unreachable"));
}

View file

@ -0,0 +1,189 @@
// 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 "dart:collection";
import "dart:typed_data";
import "package:expect/expect.dart";
// TODO(rnystrom): Not migrated to NNBD yet.
// See https://github.com/dart-lang/sdk/issues/39517.
void main() {
testIterable();
testList();
}
final elements = <C>[c, d, e, f, null];
void testIterable() {
var iterable = new Iterable<C>.generate(elements.length, (n) => elements[n]);
// Down-cast
{
// An iterable that (likely) can do direct access.
var dIterable = Iterable.castFrom<C, D>(iterable);
Expect.throws(() => dIterable.first, null, "1.first");
Expect.equals(d, dIterable.elementAt(1));
Expect.throws(() => dIterable.elementAt(2), null, "1.2"); // E is not D.
Expect.equals(f, dIterable.skip(3).first); // Skip does not access element.
Expect.equals(null, dIterable.skip(3).elementAt(1));
Expect.throws(() => dIterable.toList(), null, "1.toList");
}
{
// An iterable that cannot do direct access.
var dIterable2 = Iterable.castFrom<C, D>(iterable.where((_) => true));
Expect.throws(() => dIterable2.first, null, "2.first");
Expect.equals(d, dIterable2.elementAt(1));
Expect.throws(() => dIterable2.elementAt(2), null, "2.2"); // E is not D.
Expect.equals(f, dIterable2.skip(3).first); // Skip does not access element.
Expect.equals(null, dIterable2.skip(3).elementAt(1));
Expect.throws(() => dIterable2.toList(), null, "2.toList");
}
{
// Iterable that definitely won't survive accessing element 2.
var iterable3 = new Iterable<C>.generate(
elements.length, (n) => n == 3 ? throw "untouchable" : elements[n]);
var dIterable3 = Iterable.castFrom<C, D>(iterable3);
Expect.throws(() => dIterable3.first, null, "3.first");
Expect.equals(d, dIterable3.elementAt(1));
Expect.throws(() => dIterable3.elementAt(3), null, "3.3");
// Skip does not access element.
Expect.equals(null, dIterable3.skip(4).first);
Expect.equals(null, dIterable3.skip(3).elementAt(1));
Expect.throws(() => dIterable3.toList(), null, "3.toList");
}
// Up-cast.
{
var oIterable4 = Iterable.castFrom<C, Object>(iterable);
Expect.listEquals(elements, oIterable4.toList());
}
}
void testList() {
// Down-cast.
var list = new List<C>.from(elements);
var dList = List.castFrom<C, D>(list);
Expect.throws(() => dList.first); // C is not D.
Expect.equals(d, dList[1]);
Expect.throws(() => dList[2]); // E is not D.
Expect.equals(f, dList[3]);
Expect.equals(null, dList.last);
Expect.throws(() => dList.toList());
dList[2] = d;
Expect.equals(d, dList[2]); // Setting works.
// Up-cast.
var list2 = new List<C>.from(elements);
var dList2 = List.castFrom<C, Object>(list2);
Expect.listEquals(elements, dList2);
Expect.throws(() => dList2[2] = new Object()); // Cannot set non-C.
Expect.listEquals(elements, dList2);
// Regression test.
var list3 = <num>[4, 3, 2, 1];
var dList3 = list3.cast<int>();
dList3.sort(null);
Expect.listEquals([1, 2, 3, 4], list3);
}
void testSet() {
var set = new Set<C>.from(elements); // Linked HashSet.
Expect.listEquals(elements, set.toList()); // Preserves order.
var dSet = Set.castFrom<C, D>(set);
// Preserves order.
Expect.throws(() => dSet.first); // C is not D.
Expect.equals(d, dSet.elementAt(1));
Expect.throws(() => dSet.elementAt(2)); // E is not D.
Expect.throws(() => dSet.toList());
// Contains is not typed.
var newC = new C();
Expect.isFalse(dSet.contains(newC));
dSet.add(newC);
Expect.isTrue(dSet.contains(newC));
Expect.equals(5, dSet.length);
dSet.remove(newC);
Expect.equals(5, dSet.length);
dSet.remove(c); // Success, no type checks.
Expect.equals(4, dSet.length);
// Up-cast
var set2 = new Set<C>.from(elements);
var dSet2 = Set.castFrom<C, Object>(set2);
var newObject = new Object();
Expect.throws(() => dSet2.add(newObject));
Expect.isFalse(dSet.contains(newObject));
var toSet2 = dSet2.toSet();
Expect.isTrue(toSet2 is LinkedHashSet<Object>);
Expect.isTrue(toSet2 is! LinkedHashSet<C>);
// Custom emptySet.
var set3 = new Set<C>.from(elements);
var dSet3 = Set.castFrom<C, Object>(set3, newSet: <T>() => new HashSet<T>());
var toSet3 = dSet3.toSet();
Expect.isTrue(toSet3 is HashSet<Object>);
Expect.isTrue(toSet3 is HashSet<C>);
Expect.isTrue(toSet3 is! LinkedHashSet<Object>);
}
void testMap() {
var map = new Map.fromIterables(elements, elements);
var dMap = Map.castFrom<C, C, D, D>(map);
Expect.isTrue(dMap is Map<D, D>);
Expect.equals(null, dMap[new C()]);
Expect.throws(() => dMap[c]);
Expect.isTrue(dMap.containsKey(c));
Expect.equals(d, dMap[d]);
Expect.throws(() => dMap[e]);
Expect.equals(null, dMap[null]);
Expect.equals(5, dMap.length);
dMap.remove(c); // Success, no type checks along the way.
Expect.equals(4, dMap.length);
Expect.equals(null, dMap[c]);
Expect.throws(() => dMap[c] = d);
Expect.throws(() => dMap[d] = c);
Expect.equals(4, dMap.length);
Expect.isTrue(dMap.keys is Iterable<D>);
Expect.isTrue(dMap.values is Iterable<D>);
Expect.throws(() => dMap.keys.toList());
Expect.throws(() => dMap.values.toList());
}
class C {}
class D extends C {}
class E extends C {}
class F implements D, E {}
final c = new C();
final d = new D();
final e = new E();
final f = new F();

View file

@ -0,0 +1,93 @@
// 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 collection.from.test;
import "package:expect/expect.dart";
import 'dart:collection';
main() {
for (Iterable<num> elements in [
new Set<num>(),
<num>[],
const <num>[],
const <num, int>{}.keys,
const <int, num>{}.values,
new Iterable<num>.generate(0),
new Set<num>()..add(1)..add(2)..add(4),
<num>[1, 2, 4],
new Iterable<num>.generate(3, (i) => [1, 2, 4][i]),
const <num>[1, 2, 4],
const <num, int>{1: 0, 2: 0, 4: 0}.keys,
const <int, num>{1: 1, 2: 2, 4: 4}.values,
]) {
int elementCount = elements.length;
check(elements, new List<num>.from(elements));
check(elements, new List<int>.from(elements));
check(elements, new List<Object>.from(elements));
check(elements, new List<num>.from(elements, growable: true));
check(elements, new List<int>.from(elements, growable: true));
check(elements, new List<Object>.from(elements, growable: true));
check(elements, new List<num>.from(elements, growable: false));
check(elements, new List<int>.from(elements, growable: false));
check(elements, new List<Object>.from(elements, growable: false));
check(elements, new Queue<num>.from(elements));
check(elements, new Queue<int>.from(elements));
check(elements, new Queue<Object>.from(elements));
check(elements, new ListQueue<num>.from(elements));
check(elements, new ListQueue<int>.from(elements));
check(elements, new ListQueue<Object>.from(elements));
check(elements, new DoubleLinkedQueue<num>.from(elements));
check(elements, new DoubleLinkedQueue<int>.from(elements));
check(elements, new DoubleLinkedQueue<Object>.from(elements));
check(elements, new Set<num>.from(elements));
check(elements, new Set<int>.from(elements));
check(elements, new Set<Object>.from(elements));
check(elements, new HashSet<num>.from(elements));
check(elements, new HashSet<int>.from(elements));
check(elements, new HashSet<Object>.from(elements));
check(elements, new LinkedHashSet<num>.from(elements));
check(elements, new LinkedHashSet<int>.from(elements));
check(elements, new LinkedHashSet<Object>.from(elements));
check(elements, new SplayTreeSet<num>.from(elements));
check(elements, new SplayTreeSet<int>.from(elements));
check(elements, new SplayTreeSet<Object>.from(elements));
// Sanity check that elements didn't change.
Expect.equals(elementCount, elements.length);
// Lists may be growable or not growable.
{
var list = new List<num>.from(elements, growable: true);
Expect.equals(elementCount, list.length);
list.add(42);
Expect.equals(elementCount + 1, list.length);
}
{
var list = new List<num>.from(elements);
Expect.equals(elementCount, list.length);
list.add(42);
Expect.equals(elementCount + 1, list.length);
}
{
var list = new List<num>.from(elements, growable: false);
Expect.equals(elementCount, list.length);
Expect.throwsUnsupportedError(() {
list.add(42);
});
Expect.equals(elementCount, list.length);
}
}
}
void check(Iterable<num> initial, Iterable other) {
Expect.equals(initial.length, other.length);
for (var element in other) {
initial.contains(element);
}
for (var element in initial) {
other.contains(element);
}
}

View file

@ -0,0 +1,76 @@
// Copyright (c) 2013, 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 map_test;
import 'dart:collection';
// Test that length/isEmpty opertions are constant time on
// maps, strings and collections.
void testString(int n) {
String s = "x";
String string = "";
int length = n;
while (true) {
if ((length & 1) == 1) {
string += s;
}
length >>= 1;
if (length == 0) break;
s += s;
}
testLength(string, n);
testLength(string.codeUnits, n);
}
void testMap(Map map, int n) {
for (int i = 0; i < n; i++) {
map[i] = i;
}
testLength(map, n);
testLength(map.keys, n);
testLength(map.values, n);
}
void testCollection(var collection, n) {
for (int i = 0; i < n; i++) {
collection.add(i);
}
testLength(collection, n);
}
void testList(List list, n) {
// Works even if list is fixed-length.
for (int i = 0; i < n; i++) {
list[i] = i;
}
testLength(list, n);
}
void testLength(dynamic lengthable, int size) {
print(lengthable.runtimeType); // Show what hangs the test.
int length = 0;
// If length, isEmpty or isNotEmpty is not a constant-time (or very fast)
// operation, this will timeout.
for (int i = 0; i < 100000; i++) {
if (!lengthable.isEmpty) length += lengthable.length as int;
if (lengthable.isNotEmpty) length += lengthable.length as int;
}
if (length != size * 200000) throw "Bad length: $length / size: $size";
}
main() {
const int N = 100000;
testMap(new HashMap(), N);
testMap(new LinkedHashMap(), N);
testMap(new SplayTreeMap(), N);
testCollection(new HashSet(), N);
testCollection(new LinkedHashSet(), N);
testCollection(new ListQueue(), N);
testCollection(new DoubleLinkedQueue(), N);
testList(new List()..length = N, N);
testList(new List(N), N);
testString(N);
}

View file

@ -0,0 +1,76 @@
// Copyright (c) 2018, 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 collection.from.test;
import "package:expect/expect.dart";
import 'dart:collection';
main() {
for (Iterable<num> elements in [
new Set<num>(),
<num>[],
const <num>[],
const <num, int>{}.keys,
const <int, num>{}.values,
new Iterable<num>.generate(0),
new Set<num>()..add(1)..add(2)..add(4),
<num>[1, 2, 4],
new Iterable<num>.generate(3, (i) => [1, 2, 4][i]),
const <num>[1, 2, 4],
const <num, int>{1: 0, 2: 0, 4: 0}.keys,
const <int, num>{1: 1, 2: 2, 4: 4}.values,
]) {
String sourceType = elements.runtimeType.toString();
check(sourceType, elements, new List<num>.of(elements));
check(sourceType, elements, new List<Object>.of(elements));
check(sourceType, elements, new Queue<num>.of(elements));
check(sourceType, elements, new Queue<Object>.of(elements));
check(sourceType, elements, new ListQueue<num>.of(elements));
check(sourceType, elements, new ListQueue<Object>.of(elements));
check(sourceType, elements, new DoubleLinkedQueue<num>.of(elements));
check(sourceType, elements, new DoubleLinkedQueue<Object>.of(elements));
check(sourceType, elements, new Set<num>.of(elements));
check(sourceType, elements, new Set<Object>.of(elements));
check(sourceType, elements, new HashSet<num>.of(elements));
check(sourceType, elements, new HashSet<Object>.of(elements));
check(sourceType, elements, new LinkedHashSet<num>.of(elements));
check(sourceType, elements, new LinkedHashSet<Object>.of(elements));
check(sourceType, elements, new SplayTreeSet<num>.of(elements));
check(sourceType, elements, new SplayTreeSet<Object>.of(elements));
// Inference applies to the `of` constructor, unlike the `from` constructor.
Expect.isTrue(new List.of(elements) is Iterable<num>);
Expect.isTrue(new Queue.of(elements) is Iterable<num>);
Expect.isTrue(new ListQueue.of(elements) is Iterable<num>);
Expect.isTrue(new DoubleLinkedQueue.of(elements) is Iterable<num>);
Expect.isTrue(new Set.of(elements) is Iterable<num>);
Expect.isTrue(new HashSet.of(elements) is Iterable<num>);
Expect.isTrue(new LinkedHashSet.of(elements) is Iterable<num>);
Expect.isTrue(new SplayTreeSet.of(elements) is Iterable<num>);
Expect.isTrue(new List.of(elements) is! Iterable<int>);
Expect.isTrue(new Queue.of(elements) is! Iterable<int>);
Expect.isTrue(new ListQueue.of(elements) is! Iterable<int>);
Expect.isTrue(new DoubleLinkedQueue.of(elements) is! Iterable<int>);
Expect.isTrue(new Set.of(elements) is! Iterable<int>);
Expect.isTrue(new HashSet.of(elements) is! Iterable<int>);
Expect.isTrue(new LinkedHashSet.of(elements) is! Iterable<int>);
Expect.isTrue(new SplayTreeSet.of(elements) is! Iterable<int>);
}
}
void check(String sourceType, Iterable<num> source, Iterable target) {
String targetType = target.runtimeType.toString();
String name = "$sourceType->$targetType";
Expect.equals(source.length, target.length, "$name.length");
for (var element in target) {
Expect.isTrue(source.contains(element), "$name:$element in source");
}
for (var element in source) {
Expect.isTrue(target.contains(element), "$name:$element in target");
}
}

View file

@ -0,0 +1,130 @@
// 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 'dart:collection';
import "package:expect/expect.dart";
testRemove(base) {
int length = base.length;
for (int i = 0; i < length; i++) {
Expect.isFalse(base.isEmpty);
base.remove(base.first);
}
Expect.isTrue(base.isEmpty);
}
testRemoveAll(base, Iterable removes) {
Set retained = new Set();
for (var element in base) {
if (!removes.contains(element)) {
retained.add(element);
}
}
String name = "$base.removeAll($removes) -> $retained";
base.removeAll(removes);
for (var value in base) {
Expect.isFalse(removes.contains(value), "$name: Found $value");
}
for (var value in retained) {
Expect.isTrue(base.contains(value), "$name: Found $value");
}
}
testRetainAll(base, Iterable retains) {
Set retained = new Set();
for (var element in base) {
if (retains.contains(element)) {
retained.add(element);
}
}
String name = "$base.retainAll($retains) -> $retained";
base.retainAll(retains);
for (var value in base) {
Expect.isTrue(retains.contains(value), "$name: Found $value");
}
for (var value in retained) {
Expect.isTrue(base.contains(value), "$name: Found $value");
}
}
testRemoveWhere(base, bool test(value)) {
Set retained = new Set();
for (var element in base) {
if (!test(element)) {
retained.add(element);
}
}
String name = "$base.removeWhere(...) -> $retained";
base.removeWhere(test);
for (var value in base) {
Expect.isFalse(test(value), "$name: Found $value");
}
for (var value in retained) {
Expect.isTrue(base.contains(value), "$name: Found $value");
}
}
testRetainWhere(base, bool test(value)) {
Set retained = new Set();
for (var element in base) {
if (test(element)) {
retained.add(element);
}
}
String name = "$base.retainWhere(...) -> $retained";
base.retainWhere(test);
for (var value in base) {
Expect.isTrue(test(value), "$name: Found $value");
}
for (var value in retained) {
Expect.isTrue(base.contains(value), "$name: Found $value");
}
}
void main() {
var collections = [
[],
[1],
[2],
[1, 2],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[1, 3, 5, 7, 9],
[2, 4, 6, 8, 10]
];
for (var base in collections) {
for (var delta in collections) {
testRemove(base.toList());
testRemove(base.toSet());
var deltaSet = delta.toSet();
testRemoveWhere(base.toList(), deltaSet.contains);
testRetainWhere(base.toList(), (e) => !deltaSet.contains(e));
testRemoveAll(base.toSet(), delta);
testRemoveAll(base.toSet(), deltaSet);
testRetainAll(base.toSet(), delta);
testRetainAll(base.toSet(), deltaSet);
testRemoveWhere(base.toSet(), deltaSet.contains);
testRetainWhere(base.toSet(), (e) => !deltaSet.contains(e));
// Test the ListBase class's List implementation.
testRemoveWhere(new MyList(base.toList()), deltaSet.contains);
testRetainWhere(new MyList(base.toList()), (e) => !deltaSet.contains(e));
}
}
}
class MyList<E> extends ListBase<E> {
List<E> _source;
MyList(this._source);
int get length => _source.length;
void set length(int length) {
_source.length = length;
}
E operator [](int index) => _source[index];
void operator []=(int index, E value) {
_source[index] = value;
}
}

View file

@ -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.
library collection_test;
import "package:expect/expect.dart";
import 'dart:collection' show Queue;
class CollectionTest {
CollectionTest(Iterable<int> iterable) {
testFold(iterable);
}
void testFold(Iterable<int> iterable) {
Expect.equals(
28, iterable.fold<int>(0, (prev, element) => prev + element));
Expect.equals(
3024, iterable.fold<int>(1, (prev, element) => prev * element));
}
}
main() {
final TEST_ELEMENTS = const [4, 2, 6, 7, 9];
// Const list.
new CollectionTest(TEST_ELEMENTS);
// Fixed size list.
var fixedList = new List<int>.of(TEST_ELEMENTS, growable: false);
new CollectionTest(fixedList);
// Growable list.
new CollectionTest(new List.from(TEST_ELEMENTS));
// Set.
new CollectionTest(new Set.from(TEST_ELEMENTS));
// Queue.
new CollectionTest(new Queue.from(TEST_ELEMENTS));
}

View file

@ -0,0 +1,386 @@
// Copyright (c) 2012, 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 for the toString methods on collections and maps.
*/
library collection_to_string;
import "package:expect/expect.dart";
import 'dart:collection' show Queue, LinkedHashMap;
import 'dart:math' as Math;
// TODO(jjb): seed random number generator when API allows it
const int NUM_TESTS = 300;
const int MAX_COLLECTION_SIZE = 7;
final rand = Math.Random();
main() {
smokeTest();
exactTest();
inexactTest();
}
/**
* Test a few simple examples.
*/
void smokeTest() {
// Non-const lists
Expect.equals([].toString(), '[]');
Expect.equals([1].toString(), '[1]');
Expect.equals(['Elvis'].toString(), '[Elvis]');
Expect.equals([null].toString(), '[null]');
Expect.equals([1, 2].toString(), '[1, 2]');
Expect.equals(['I', 'II'].toString(), '[I, II]');
Expect.equals(
[
[1, 2],
[3, 4],
[5, 6]
].toString(),
'[[1, 2], [3, 4], [5, 6]]');
// Const lists
Expect.equals((const []).toString(), '[]');
Expect.equals((const [1]).toString(), '[1]');
Expect.equals((const ['Elvis']).toString(), '[Elvis]');
Expect.equals((const [null]).toString(), '[null]');
Expect.equals((const [1, 2]).toString(), '[1, 2]');
Expect.equals((const ['I', 'II']).toString(), '[I, II]');
Expect.equals(
(const [
const [1, 2],
const [3, 4],
const [5, 6]
])
.toString(),
'[[1, 2], [3, 4], [5, 6]]');
// Non-const maps - Note that all keys are strings; the spec currently demands this
Expect.equals({}.toString(), '{}');
Expect.equals({'Elvis': 'King'}.toString(), '{Elvis: King}');
Expect.equals({'Elvis': null}.toString(), '{Elvis: null}');
Expect.equals({'I': 1, 'II': 2}.toString(), '{I: 1, II: 2}');
Expect.equals(
{
'X': {'I': 1, 'II': 2},
'Y': {'III': 3, 'IV': 4},
'Z': {'V': 5, 'VI': 6}
}.toString(),
'{X: {I: 1, II: 2}, Y: {III: 3, IV: 4}, Z: {V: 5, VI: 6}}');
// Const maps
Expect.equals(const {}.toString(), '{}');
Expect.equals(const {'Elvis': 'King'}.toString(), '{Elvis: King}');
Expect.equals({'Elvis': null}.toString(), '{Elvis: null}');
Expect.equals(const {'I': 1, 'II': 2}.toString(), '{I: 1, II: 2}');
Expect.equals(
const {
'X': const {'I': 1, 'II': 2},
'Y': const {'III': 3, 'IV': 4},
'Z': const {'V': 5, 'VI': 6}
}.toString(),
'{X: {I: 1, II: 2}, Y: {III: 3, IV: 4}, Z: {V: 5, VI: 6}}');
}
// SERIOUS "BASHER" TESTS
/**
* Generate a bunch of random collections (including Maps), and test that
* there string form is as expected. The collections include collections
* as elements, keys, and values, and include recursive references.
*
* This test restricts itself to collections with well-defined iteration
* orders (i.e., no HashSet, HashMap).
*/
void exactTest() {
for (int i = 0; i < NUM_TESTS; i++) {
// Choose a size from 0 to MAX_COLLECTION_SIZE, favoring larger sizes
int size =
Math.sqrt(random(MAX_COLLECTION_SIZE * MAX_COLLECTION_SIZE)).toInt();
StringBuffer stringRep = new StringBuffer();
Object o = randomCollection(size, stringRep, exact: true);
print(stringRep);
print(o);
Expect.equals(o.toString(), stringRep.toString());
}
}
/**
* Generate a bunch of random collections (including Maps), and test that
* there string form is as expected. The collections include collections
* as elements, keys, and values, and include recursive references.
*
* This test includes collections with ill-defined iteration orders (i.e.,
* HashSet, HashMap). As a consequence, it can't use equality tests on the
* string form. Instead, it performs equality tests on their "alphagrams."
* This might allow false positives, but it does give a fair amount of
* confidence.
*/
void inexactTest() {
for (int i = 0; i < NUM_TESTS; i++) {
// Choose a size from 0 to MAX_COLLECTION_SIZE, favoring larger sizes
int size =
Math.sqrt(random(MAX_COLLECTION_SIZE * MAX_COLLECTION_SIZE)).toInt();
StringBuffer stringRep = new StringBuffer();
Object o = randomCollection(size, stringRep, exact: false);
print(stringRep);
print(o);
Expect.equals(alphagram(o.toString()), alphagram(stringRep.toString()));
}
}
/**
* Return a random collection (or Map) of the specified size, placing its
* string representation into the given string buffer.
*
* If exact is true, the returned collections will not be, and will not contain
* a collection with ill-defined iteration order (i.e., a HashSet or HashMap).
*/
Object randomCollection(int size, StringBuffer stringRep, {bool exact = false}) {
return randomCollectionHelper(size, exact, stringRep, []);
}
/**
* Return a random collection (or map) of the specified size, placing its
* string representation into the given string buffer. The beingMade
* parameter is a list of collections currently under construction, i.e.,
* candidates for recursive references.
*
* If exact is true, the returned collections will not be, and will not contain
* a collection with ill-defined iteration order (i.e., a HashSet or HashMap).
*/
Object randomCollectionHelper(
int size, bool exact, StringBuffer stringRep, List beingMade) {
double interfaceFrac = rand.nextDouble();
if (exact) {
if (interfaceFrac < 1 / 3) {
return randomList(size, exact, stringRep, beingMade);
} else if (interfaceFrac < 2 / 3) {
return randomQueue(size, exact, stringRep, beingMade);
} else {
return randomMap(size, exact, stringRep, beingMade);
}
} else {
if (interfaceFrac < 1 / 4) {
return randomList(size, exact, stringRep, beingMade);
} else if (interfaceFrac < 2 / 4) {
return randomQueue(size, exact, stringRep, beingMade);
} else if (interfaceFrac < 3 / 4) {
return randomSet(size, exact, stringRep, beingMade);
} else {
return randomMap(size, exact, stringRep, beingMade);
}
}
}
/**
* Return a random List of the specified size, placing its string
* representation into the given string buffer. The beingMade
* parameter is a list of collections currently under construction, i.e.,
* candidates for recursive references.
*
* If exact is true, the returned collections will not be, and will not contain
* a collection with ill-defined iteration order (i.e., a HashSet or HashMap).
*/
List randomList(int size, bool exact, StringBuffer stringRep, List beingMade) {
return populateRandomCollection(size, exact, stringRep, beingMade, [], "[]");
}
/**
* Like randomList, but returns a queue.
*/
Queue randomQueue(
int size, bool exact, StringBuffer stringRep, List beingMade) {
return populateRandomCollection(
size, exact, stringRep, beingMade, new Queue(), "{}");
}
/**
* Like randomList, but returns a Set.
*/
Set randomSet(int size, bool exact, StringBuffer stringRep, List beingMade) {
// Until we have LinkedHashSet, method will only be called with exact==true
return populateRandomSet(size, exact, stringRep, beingMade, new Set());
}
/**
* Like randomList, but returns a map.
*/
Map randomMap(int size, bool exact, StringBuffer stringRep, List beingMade) {
if (exact) {
return populateRandomMap(
size, exact, stringRep, beingMade, new LinkedHashMap());
} else {
return populateRandomMap(size, exact, stringRep, beingMade,
randomBool() ? new Map() : new LinkedHashMap());
}
}
/**
* Populates the given empty collection with elements, emitting the string
* representation of the collection to stringRep. The beingMade parameter is
* a list of collections currently under construction, i.e., candidates for
* recursive references.
*
* If exact is true, the elements of the returned collections will not be,
* and will not contain a collection with ill-defined iteration order
* (i.e., a HashSet or HashMap).
*/
populateRandomCollection(int size, bool exact, StringBuffer stringRep,
List beingMade, var coll, String delimiters) {
beingMade.add(coll);
int start = stringRep.length;
stringRep.write(delimiters[0]);
List<int> indices = [];
for (int i = 0; i < size; i++) {
indices.add(stringRep.length);
if (i != 0) stringRep.write(', ');
coll.add(randomElement(random(size), exact, stringRep, beingMade));
}
if (size > 5 && delimiters == "()") {
const int MAX_LENGTH = 80;
const int MIN_COUNT = 3;
const int MAX_COUNT = 100;
// It's an iterable, it may omit some elements.
int end = stringRep.length;
if (size > MAX_COUNT) {
// Last two elements are also omitted, just find the first three or
// first 60 characters.
for (int i = MIN_COUNT; i < size; i++) {
int startIndex = indices[i];
if (startIndex - start > MAX_LENGTH - 6) {
// Limit - ", ...)".length.
String prefix = stringRep.toString().substring(0, startIndex);
stringRep.clear();
stringRep.write(prefix);
stringRep.write(", ...");
}
}
} else if (stringRep.length - start > MAX_LENGTH - 1) {
// 80 - ")".length.
// Last two elements are always included. Middle ones may be omitted.
int lastTwoLength = end - indices[indices.length - 2];
// Try to find first element to omit.
for (int i = 3; i <= size - 3; i++) {
int elementEnd = indices[i + 1];
int lengthAfter = elementEnd - start;
int ellipsisSize = 5; // ", ...".length
if (i == size - 3) ellipsisSize = 0; // No ellipsis if we hit the end.
if (lengthAfter + ellipsisSize + lastTwoLength > MAX_LENGTH - 1) {
// Omit this element and everything up to the last two.
int elementStart = indices[i];
// Rewrite string buffer by copying it out, clearing, and putting
// the parts back in.
String buffer = stringRep.toString();
String prefix = buffer.substring(0, elementStart);
String suffix = buffer.substring(end - lastTwoLength, end);
stringRep.clear();
stringRep.write(prefix);
stringRep.write(", ...");
stringRep.write(suffix);
break;
}
}
}
}
stringRep.write(delimiters[1]);
beingMade.removeLast();
return coll;
}
/** Like populateRandomCollection, but for sets (elements must be hashable) */
Set populateRandomSet(
int size, bool exact, StringBuffer stringRep, List beingMade, Set set) {
stringRep.write('{');
for (int i = 0; i < size; i++) {
if (i != 0) stringRep.write(', ');
set.add(i);
stringRep.write(i);
}
stringRep.write('}');
return set;
}
/** Like populateRandomCollection, but for maps. */
Map populateRandomMap(
int size, bool exact, StringBuffer stringRep, List beingMade, Map map) {
beingMade.add(map);
stringRep.write('{');
for (int i = 0; i < size; i++) {
if (i != 0) stringRep.write(', ');
int key = i; // Ensures no duplicates
stringRep.write(key);
stringRep.write(': ');
Object val = randomElement(random(size), exact, stringRep, beingMade);
map[key] = val;
}
stringRep.write('}');
beingMade.removeLast();
return map;
}
/**
* Generates a random element which can be an int, a collection, or a map,
* and emits it to StringRep. The beingMade parameter is a list of collections
* currently under construction, i.e., candidates for recursive references.
*
* If exact is true, the returned element will not be, and will not contain
* a collection with ill-defined iteration order (i.e., a HashSet or HashMap).
*/
Object randomElement(
int size, bool exact, StringBuffer stringRep, List beingMade) {
Object result;
double elementTypeFrac = rand.nextDouble();
if (elementTypeFrac < 1 / 3) {
result = random(1000);
stringRep.write(result);
} else if (elementTypeFrac < 2 / 3) {
// Element is a random (new) collection
result = randomCollectionHelper(size, exact, stringRep, beingMade);
} else {
// Element is a random recursive ref
result = beingMade[random(beingMade.length)];
if (result is List) {
stringRep.write('[...]');
} else if (result is Set || result is Map || result is Queue) {
stringRep.write('{...}');
} else {
stringRep.write('(...)');
}
}
return result;
}
/** Returns a random int on [0, max) */
int random(int max) {
return rand.nextInt(max);
}
/** Returns a random boolean value. */
bool randomBool() {
return rand.nextBool();
}
/** Returns the alphabetized characters in a string. */
String alphagram(String s) {
// Calling [toList] to convert unmodifiable list to normal list.
List<int> chars = s.codeUnits.toList();
chars.sort((int a, int b) => a - b);
return new String.fromCharCodes(chars);
}

View file

@ -0,0 +1,96 @@
// Copyright (c) 2012, 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 Math.min and Math.max.
import "package:expect/expect.dart";
negate(x) => -x;
main() {
// Test matrix:
var minNonZero = 5e-324;
var maxDenormal = 2.225073858507201e-308;
var minNormal = 2.2250738585072014e-308;
var maxFraction = 0.9999999999999999;
var minAbove1 = 1.0000000000000002;
var maxNonInt = 4503599627370495.5;
var maxNonIntFloorAsInt = maxNonInt.floor();
var maxNonIntFloorAsDouble = maxNonIntFloorAsInt.toDouble();
var maxExactIntAsDouble = 9007199254740992.0;
var maxExactIntAsInt = 9007199254740992;
var two53 = 1 << 53; // Same as maxExactIntAsInt.
var two53p1 = two53 + 1;
var maxFiniteAsDouble = 1.7976931348623157e+308;
var maxFiniteAsInt = maxFiniteAsDouble.truncate();
var inf = double.infinity;
var nan = double.nan;
var mnan = negate(nan);
var minInt64 = -0x8000000000000000;
var minInt64AsDouble = minInt64.toDouble();
var maxInt64 = 0x7fffffffffffffff;
var maxInt64AsDouble = maxInt64.toDouble(); // 1 << 63
var matrix = [
-inf,
-maxFiniteAsDouble,
[minInt64, minInt64AsDouble],
[-maxInt64, -maxFiniteAsInt],
-two53p1,
[-two53, -maxExactIntAsInt, -maxExactIntAsDouble],
-maxNonInt,
[-maxNonIntFloorAsDouble, -maxNonIntFloorAsInt],
[-499.0, -499],
-minAbove1,
[-1.0, -1],
-maxFraction,
-minNormal,
-maxDenormal,
-minNonZero,
-0.0,
[0, 0, 0],
minNonZero,
maxDenormal,
minNormal,
maxFraction,
[1.0, 1],
minAbove1,
[499.0, 499],
[maxNonIntFloorAsDouble, maxNonIntFloorAsInt],
maxNonInt,
[two53, maxExactIntAsInt, maxExactIntAsDouble],
two53p1,
[maxInt64, maxFiniteAsInt],
maxInt64AsDouble,
maxFiniteAsDouble,
inf,
[nan, mnan],
];
check(left, right, expectedResult) {
if (left is List) {
for (var x in left) check(x, right, expectedResult);
return;
}
if (right is List) {
for (var x in right) check(left, x, expectedResult);
return;
}
int actual = left.compareTo(right);
Expect.equals(
expectedResult,
actual,
"($left).compareTo($right) failed "
"(should have been $expectedResult, was $actual");
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
var left = matrix[i];
var right = matrix[j];
if (left is List) {
check(left, left, 0);
}
check(left, right, i == j ? 0 : (i < j ? -1 : 1));
}
}
}

View file

@ -0,0 +1,136 @@
// Copyright (c) 2012, 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 Math.min and Math.max.
import "package:expect/expect.dart";
negate(x) => -x;
main() {
// Test matrix:
// -inf < -499.0 == -499 < -0.0 < 0.0 == 0 < 499.0 == 499 < +inf < -NaN, NaN.
var inf = double.infinity;
var nan = double.nan;
var mnan = negate(nan);
Expect.equals(0, (-inf).compareTo(-inf));
Expect.equals(-1, (-inf).compareTo(-499.0));
Expect.equals(-1, (-inf).compareTo(-499));
Expect.equals(-1, (-inf).compareTo(-0.0));
Expect.equals(-1, (-inf).compareTo(0));
Expect.equals(-1, (-inf).compareTo(0.0));
Expect.equals(-1, (-inf).compareTo(499.0));
Expect.equals(-1, (-inf).compareTo(499));
Expect.equals(-1, (-inf).compareTo(inf));
Expect.equals(-1, (-inf).compareTo(nan));
Expect.equals(-1, (-inf).compareTo(mnan));
Expect.equals(1, (-499.0).compareTo(-inf));
Expect.equals(0, (-499.0).compareTo(-499.0));
Expect.equals(0, (-499.0).compareTo(-499));
Expect.equals(-1, (-499.0).compareTo(-0.0));
Expect.equals(-1, (-499.0).compareTo(0));
Expect.equals(-1, (-499.0).compareTo(0.0));
Expect.equals(-1, (-499.0).compareTo(499.0));
Expect.equals(-1, (-499.0).compareTo(499));
Expect.equals(-1, (-499.0).compareTo(inf));
Expect.equals(-1, (-499.0).compareTo(nan));
Expect.equals(-1, (-499.0).compareTo(mnan));
Expect.equals(1, (-499).compareTo(-inf));
Expect.equals(0, (-499).compareTo(-499.0));
Expect.equals(0, (-499).compareTo(-499));
Expect.equals(-1, (-499).compareTo(-0.0));
Expect.equals(-1, (-499).compareTo(0));
Expect.equals(-1, (-499).compareTo(0.0));
Expect.equals(-1, (-499).compareTo(499.0));
Expect.equals(-1, (-499).compareTo(499));
Expect.equals(-1, (-499).compareTo(inf));
Expect.equals(-1, (-499).compareTo(nan));
Expect.equals(-1, (-499).compareTo(mnan));
Expect.equals(1, (-0.0).compareTo(-inf));
Expect.equals(1, (-0.0).compareTo(-499.0));
Expect.equals(1, (-0.0).compareTo(-499));
Expect.equals(0, (-0.0).compareTo(-0.0));
Expect.equals(-1, (-0.0).compareTo(0));
Expect.equals(-1, (-0.0).compareTo(0.0));
Expect.equals(-1, (-0.0).compareTo(499.0));
Expect.equals(-1, (-0.0).compareTo(499));
Expect.equals(-1, (-0.0).compareTo(inf));
Expect.equals(-1, (-0.0).compareTo(nan));
Expect.equals(-1, (-0.0).compareTo(mnan));
Expect.equals(1, (0).compareTo(-inf));
Expect.equals(1, (0).compareTo(-499.0));
Expect.equals(1, (0).compareTo(-499));
Expect.equals(1, (0).compareTo(-0.0));
Expect.equals(0, (0).compareTo(0));
Expect.equals(0, (0).compareTo(0.0));
Expect.equals(-1, (0).compareTo(499.0));
Expect.equals(-1, (0).compareTo(499));
Expect.equals(-1, (0).compareTo(inf));
Expect.equals(-1, (0).compareTo(nan));
Expect.equals(-1, (0).compareTo(mnan));
Expect.equals(1, (0.0).compareTo(-inf));
Expect.equals(1, (0.0).compareTo(-499.0));
Expect.equals(1, (0.0).compareTo(-499));
Expect.equals(1, (0.0).compareTo(-0.0));
Expect.equals(0, (0.0).compareTo(0));
Expect.equals(0, (0.0).compareTo(0.0));
Expect.equals(-1, (0.0).compareTo(499.0));
Expect.equals(-1, (0.0).compareTo(499));
Expect.equals(-1, (0.0).compareTo(inf));
Expect.equals(-1, (0.0).compareTo(nan));
Expect.equals(-1, (0.0).compareTo(mnan));
Expect.equals(1, (499.0).compareTo(-inf));
Expect.equals(1, (499.0).compareTo(-499.0));
Expect.equals(1, (499.0).compareTo(-499));
Expect.equals(1, (499.0).compareTo(-0.0));
Expect.equals(1, (499.0).compareTo(0));
Expect.equals(1, (499.0).compareTo(0.0));
Expect.equals(0, (499.0).compareTo(499.0));
Expect.equals(0, (499.0).compareTo(499));
Expect.equals(-1, (499.0).compareTo(inf));
Expect.equals(-1, (499.0).compareTo(nan));
Expect.equals(-1, (499.0).compareTo(mnan));
Expect.equals(1, (499).compareTo(-inf));
Expect.equals(1, (499).compareTo(-499.0));
Expect.equals(1, (499).compareTo(-499));
Expect.equals(1, (499).compareTo(-0.0));
Expect.equals(1, (499).compareTo(0));
Expect.equals(1, (499).compareTo(0.0));
Expect.equals(0, (499).compareTo(499.0));
Expect.equals(0, (499).compareTo(499));
Expect.equals(-1, (499).compareTo(inf));
Expect.equals(-1, (499).compareTo(nan));
Expect.equals(-1, (499).compareTo(mnan));
Expect.equals(1, inf.compareTo(-inf));
Expect.equals(1, inf.compareTo(-499.0));
Expect.equals(1, inf.compareTo(-499));
Expect.equals(1, inf.compareTo(-0.0));
Expect.equals(1, inf.compareTo(0));
Expect.equals(1, inf.compareTo(0.0));
Expect.equals(1, inf.compareTo(499.0));
Expect.equals(1, inf.compareTo(499));
Expect.equals(0, inf.compareTo(inf));
Expect.equals(-1, inf.compareTo(nan));
Expect.equals(-1, inf.compareTo(mnan));
Expect.equals(1, nan.compareTo(-inf));
Expect.equals(1, nan.compareTo(-499.0));
Expect.equals(1, nan.compareTo(-499));
Expect.equals(1, nan.compareTo(-0.0));
Expect.equals(1, nan.compareTo(0));
Expect.equals(1, nan.compareTo(0.0));
Expect.equals(1, nan.compareTo(499.0));
Expect.equals(1, nan.compareTo(499));
Expect.equals(1, nan.compareTo(inf));
Expect.equals(0, nan.compareTo(nan));
Expect.equals(0, nan.compareTo(mnan));
}

View file

@ -0,0 +1,80 @@
// 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 "package:expect/expect.dart";
// 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);
} on UnsupportedError catch (e) {
exception = e;
}
Expect.equals(true, exception != null);
Expect.equals(3, list.length);
exception = null;
exception = null;
try {
list.addAll([4, 5]);
} on UnsupportedError catch (e) {
exception = e;
}
Expect.equals(true, exception != null);
Expect.equals(3, list.length);
exception = null;
try {
list[0] = 0;
} on UnsupportedError catch (e) {
exception = e;
}
Expect.equals(true, exception != null);
Expect.equals(3, list.length);
exception = null;
try {
list.sort((a, b) => a - b);
} on UnsupportedError catch (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.setRange(0, 1, [1], 0);
} on UnsupportedError catch (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]);
// Note: the next check is a regression test for dart2js. The immutable list
// overrides the 'length' property of List, but relies on using the native
// 'forEach' construct in Array. This test ensures that our strategy works
// correctly.
int x = 0;
list.forEach((e) {
x += e;
});
Expect.equals(9, x);
}
}
main() {
ConstListLiteralTest.testMain();
}

View file

@ -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.
import "package:expect/expect.dart";
main() {
testImmutable(const []);
testImmutable(const [1]);
testImmutable(const [1, 2]);
}
testImmutable(var list) {
Expect.throwsUnsupportedError(() => list.removeRange(0, 0));
Expect.throwsUnsupportedError(() => list.removeRange(0, 1));
Expect.throwsUnsupportedError(() => list.removeRange(-1, 1));
}

View file

@ -0,0 +1,25 @@
// 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 "package:expect/expect.dart";
main() {
testImmutable(const []);
testImmutable(const [1]);
testImmutable(const [1, 2]);
}
testImmutable(List list) {
Expect.throwsUnsupportedError(() => list.setRange(0, 0, const []));
Expect.throwsUnsupportedError(() => list.setRange(0, 1, const [], 1));
Expect.throwsUnsupportedError(() => list.setRange(0, 1, const []));
Expect.throwsUnsupportedError(() => list.setRange(0, 0, []));
Expect.throwsUnsupportedError(() => list.setRange(0, 1, [], 1));
Expect.throwsUnsupportedError(() => list.setRange(0, 1, []));
Expect.throwsUnsupportedError(() => list.setRange(0, 0, const [1]));
Expect.throwsUnsupportedError(() => list.setRange(0, 1, const [1]));
Expect.throwsUnsupportedError(() => list.setRange(0, 0, [1]));
Expect.throwsUnsupportedError(() => list.setRange(0, 1, [1]));
Expect.throwsUnsupportedError(() => list.setRange(0, 1, [1], 1));
}

View file

@ -0,0 +1,38 @@
// 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 "package:expect/expect.dart";
/**
* Verify static compilation errors on strings and lists.
*/
class CoreStaticTypesTest {
static testMain() {
testStringOperators();
testStringMethods();
testListOperators();
}
static testStringOperators() {
var q = "abcdef";
/*@compile-error=unspecified*/ q['hello'];
/*@compile-error=unspecified*/ q[0] = 'x';
}
static testStringMethods() {
var s = "abcdef";
/*@compile-error=unspecified*/ s.startsWith(1);
/*@compile-error=unspecified*/ s.endsWith(1);
}
static testListOperators() {
var a = [1, 2, 3, 4];
/*@compile-error=unspecified*/ a['0'];
/*@compile-error=unspecified*/ a['0'] = 99;
}
}
main() {
CoreStaticTypesTest.testMain();
}

View file

@ -0,0 +1,333 @@
// 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 "package:expect/expect.dart";
/**
* 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();
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 a, List b) {
a.sort((x, y) => x.compareTo(y));
b.sort((x, y) => x.compareTo(y));
assertListEquals(a, b);
}
static assertTypeError(void f(), [String? message]) {
Expect.throws<Error>(
f,
(exception) =>
(exception is TypeError) ||
(exception is CastError) ||
(exception is AssertionError) ||
(exception is NoSuchMethodError) ||
(exception is ArgumentError),
message ?? "");
}
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]);
}
testUnaryOperatorErrors(objs[i]);
}
}
static testBinaryOperatorErrors(dynamic x, dynamic y) {
assertTypeError(() {
x + y;
}, "$x+$y");
assertTypeError(() {
x - y;
}, "$x-$y");
// String.* is the only non-same-type binary operator we have.
if (x is! String && y is! int) {
assertTypeError(() {
x * y;
}, "$x*$y");
}
assertTypeError(() {
x / y;
}, "$x/$y");
assertTypeError(() {
x | y;
}, "$x|$y");
assertTypeError(() {
x ^ y;
}, "$x^$y");
assertTypeError(() {
x & y;
}, "$x&$y");
assertTypeError(() {
x << y;
}, "$x<<$y");
assertTypeError(() {
x >> y;
}, "$x>>$y");
assertTypeError(() {
x ~/ y;
}, "$x~/$y");
assertTypeError(() {
x % y;
}, "$x%$y");
testComparisonOperatorErrors(x, y);
}
static testComparisonOperatorErrors(x, y) {
assertEquals(x == y, false);
assertEquals(x != y, true);
assertTypeError(() {
x < y;
}, "$x<$y");
assertTypeError(() {
x <= y;
}, "$x<=$y");
assertTypeError(() {
x > y;
}, "$x>$y");
assertTypeError(() {
x >= y;
}, "$x>=$y");
}
static testUnaryOperatorErrors(x) {
if (x is! int) {
assertTypeError(() {
~x;
}, "~$x");
}
if (x is! num) {
assertTypeError(() {
-x;
}, "-$x");
}
if (x is! bool) {
assertTypeError(() {
!x;
}, "!$x");
}
}
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 (e) {}
try {
y.toRadixString(-1);
Expect.fail("Illegal radix -1 accepted.");
} catch (e) {}
}
static testStringOperators() {
dynamic s = "abcdef";
assertEquals(s, "abcdef");
assertEquals(s.codeUnitAt(0), 97);
assertEquals(s[0], 'a');
assertEquals(s.length, 6);
}
// TODO(jimhug): Fill out full set of string methods.
static testStringMethods() {
var s = "abcdef";
assertEquals(s.isEmpty, false);
assertEquals(s.isNotEmpty, true);
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);
}
static testListOperators() {
var a = [1, 2, 3, 4];
assertEquals(a[0], 1);
a[0] = 42;
assertEquals(a[0], 42);
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.keys.length, 2);
assertEquals(d.values.length, 2);
assertEquals(d.remove('c'), null);
assertEquals(d.remove('b'), 2);
assertEquals(d.keys.single, 'a');
assertEquals(d.values.single, 1);
d['c'] = 3;
d['f'] = 4;
assertEquals(d.keys.length, 3);
assertEquals(d.values.length, 3);
assertListContains(d.keys.toList(), ['a', 'c', 'f']);
assertListContains(d.values.toList(), [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.keys.length, 2);
assertEquals(d.values.length, 2);
d['g'] = null;
assertEquals(d.containsKey('g'), true);
assertEquals(d['g'], null);
}
static testDateMethods() {
var msec = 115201000;
var d = new DateTime.fromMillisecondsSinceEpoch(msec, isUtc: true);
assertEquals(d.second, 1);
assertEquals(d.year, 1970);
d = new DateTime.now();
assertEquals(d.year >= 1970, 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();
}

View file

@ -0,0 +1,301 @@
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
import "dart:convert";
import "dart:typed_data";
main() {
testMediaType();
testRoundTrip("");
testRoundTrip("a");
testRoundTrip("ab");
testRoundTrip("abc");
testRoundTrip("abcd");
testRoundTrip("Content with special%25 characters: # ? = % # ? = %");
testRoundTrip("blåbærgrød", utf8);
testRoundTrip("blåbærgrød", latin1);
testUriEquals("data:,abc?d");
testUriEquals("DATA:,ABC?D");
testUriEquals("data:,a%20bc?d");
testUriEquals("DATA:,A%20BC?D");
testUriEquals("data:,abc?d%23e"); // # must and will be is escaped.
// Test that UriData.uri normalizes path and query.
testUtf8Encoding("\u1000\uffff");
testBytes();
testInvalidCharacters();
testNormalization();
testErrors();
}
void testMediaType() {
for (var mimeType in ["", "text/plain", "text/javascript"]) {
for (var charset in ["", ";charset=US-ASCII", ";charset=UTF-8"]) {
for (var base64 in ["", ";base64"]) {
bool isBase64 = base64.isNotEmpty;
var text = "data:$mimeType$charset$base64,";
var uri = UriData.parse(text);
String expectedCharset =
charset.isEmpty ? "US-ASCII" : charset.substring(9);
String expectedMimeType = mimeType.isEmpty ? "text/plain" : mimeType;
Expect.equals(text, "$uri");
Expect.equals(expectedMimeType, uri.mimeType);
Expect.equals(expectedCharset, uri.charset);
Expect.equals(isBase64, uri.isBase64);
}
}
}
}
void testRoundTrip(String content, [Encoding? encoding]) {
UriData dataUri = new UriData.fromString(content, encoding: encoding);
Expect.isFalse(dataUri.isBase64);
Uri uri = dataUri.uri;
expectUriEquals(new Uri.dataFromString(content, encoding: encoding), uri);
if (encoding != null) {
UriData dataUriParams =
new UriData.fromString(content, parameters: {"charset": encoding.name});
Expect.equals("$dataUri", "$dataUriParams");
}
Expect.equals(encoding ?? ascii, Encoding.getByName(dataUri.charset));
Expect.equals(content, dataUri.contentAsString(encoding: encoding));
Expect.equals(content, dataUri.contentAsString());
Expect.equals(content, (encoding ?? ascii).decode(dataUri.contentAsBytes()));
uri = dataUri.uri;
Expect.equals(uri.toString(), dataUri.toString());
Expect.equals(dataUri.toString(), new UriData.fromUri(uri).toString());
dataUri = new UriData.fromBytes(content.codeUnits);
Expect.listEquals(content.codeUnits, dataUri.contentAsBytes());
Expect.equals(content, dataUri.contentAsString(encoding: latin1));
uri = dataUri.uri;
Expect.equals(uri.toString(), dataUri.toString());
Expect.equals(dataUri.toString(), new UriData.fromUri(uri).toString());
// Check that the URI is properly normalized.
expectUriEquals(uri, Uri.parse("$uri"));
}
void testUtf8Encoding(String content) {
UriData uri = new UriData.fromString(content, encoding: utf8);
Expect.equals(content, uri.contentAsString(encoding: utf8));
Expect.listEquals(utf8.encode(content), uri.contentAsBytes());
}
void testInvalidCharacters() {
// SPACE, CTL and tspecial, plus '%' and '#' (URI gen-delim)
// This contains all ASCII character that are not valid in attribute/value
// parts.
var invalid =
'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x7f'
' ()<>@,;:"/[]?=%#\x80\u{1000}\u{10000}';
var invalidNoSlash = invalid.replaceAll('/', '');
var dataUri = new UriData.fromString(invalid,
encoding: utf8,
mimeType: "$invalidNoSlash/$invalidNoSlash",
parameters: {invalid: invalid});
Expect.equals(invalid, dataUri.contentAsString());
Expect.equals("$invalidNoSlash/$invalidNoSlash", dataUri.mimeType);
Expect.equals(invalid, dataUri.parameters[invalid]);
var uri = dataUri.uri;
Expect.equals("$uri", "$dataUri");
expectUriEquals(uri, Uri.parse("$uri")); // Check that it's canonicalized.
Expect.equals("$dataUri", new UriData.fromUri(uri).toString());
}
void testBytes() {
void testList(List<int> list) {
var dataUri = new UriData.fromBytes(list);
Expect.equals("application/octet-stream", dataUri.mimeType);
Expect.isTrue(dataUri.isBase64);
Expect.listEquals(list, dataUri.contentAsBytes());
dataUri = new UriData.fromBytes(list, percentEncoded: true);
Expect.equals("application/octet-stream", dataUri.mimeType);
Expect.isFalse(dataUri.isBase64);
Expect.listEquals(list, dataUri.contentAsBytes());
var string = new String.fromCharCodes(list);
dataUri = new UriData.fromString(string, encoding: latin1);
Expect.equals("text/plain", dataUri.mimeType);
Expect.isFalse(dataUri.isBase64);
Expect.listEquals(list, dataUri.contentAsBytes());
dataUri = new UriData.fromString(string, encoding: latin1, base64: true);
Expect.equals("text/plain", dataUri.mimeType);
Expect.isTrue(dataUri.isBase64);
Expect.listEquals(list, dataUri.contentAsBytes());
}
void testLists(List<int> list) {
testList(list);
for (int i = 0; i < 27; i++) {
testList(list.sublist(i, i + i)); // All lengths from 0 to 27.
}
}
var bytes = new Uint8List(512);
for (int i = 0; i < bytes.length; i++) {
bytes[i] = i;
}
testLists(bytes);
testLists(new List.from(bytes));
testLists(new List.unmodifiable(bytes));
}
void testNormalization() {
// Base-64 normalization.
// Normalized URI-alphabet characters.
Expect.equals(
"data:;base64,AA/+", UriData.parse("data:;base64,AA_-").toString());
// Normalized escapes.
Expect.equals(
"data:;base64,AB==", UriData.parse("data:;base64,A%42=%3D").toString());
Expect.equals("data:;base64,/+/+",
UriData.parse("data:;base64,%5F%2D%2F%2B").toString());
// Normalized padded data.
Expect.equals(
"data:;base64,AA==", UriData.parse("data:;base64,AA%3D%3D").toString());
Expect.equals(
"data:;base64,AAA=", UriData.parse("data:;base64,AAA%3D").toString());
// Normalized unpadded data.
Expect.equals(
"data:;base64,AA==", UriData.parse("data:;base64,AA").toString());
Expect.equals(
"data:;base64,AAA=", UriData.parse("data:;base64,AAA").toString());
// "URI normalization" of non-base64 content.
var uri = UriData.parse("data:,\x20\xa0");
Expect.equals("data:,%20%C2%A0", uri.toString());
uri = UriData.parse("data:,x://x@y:[z]:42/p/./?q=x&y=z#?#\u1234\u{12345}");
Expect.equals(
"data:,x://x@y:%5Bz%5D:42/p/./?q=x&y=z%23?%23%E1%88%B4%F0%92%8D%85",
uri.toString());
}
void testErrors() {
// Invalid constructor parameters.
Expect.throwsArgumentError(
() => new UriData.fromBytes([], mimeType: "noslash"));
Expect.throwsArgumentError(() => new UriData.fromBytes([257]));
Expect.throwsArgumentError(() => new UriData.fromBytes([-1]));
Expect.throwsArgumentError(() => new UriData.fromBytes([0x10000000]));
Expect.throwsArgumentError(
() => new UriData.fromString("", mimeType: "noslash"));
Expect.throwsArgumentError(
() => new Uri.dataFromBytes([], mimeType: "noslash"));
Expect.throwsArgumentError(() => new Uri.dataFromBytes([257]));
Expect.throwsArgumentError(() => new Uri.dataFromBytes([-1]));
Expect.throwsArgumentError(() => new Uri.dataFromBytes([0x10000000]));
Expect.throwsArgumentError(
() => new Uri.dataFromString("", mimeType: "noslash"));
// Empty parameters allowed, not an error.
var uri = new UriData.fromString("", mimeType: "", parameters: {});
Expect.equals("data:,", "$uri");
// Empty parameter key or value is an error.
Expect.throwsArgumentError(
() => new UriData.fromString("", parameters: {"": "X"}));
Expect.throwsArgumentError(
() => new UriData.fromString("", parameters: {"X": ""}));
// Not recognizing charset is an error.
uri = UriData.parse("data:;charset=arglebargle,X");
Expect.throws(() {
uri.contentAsString();
});
// Doesn't throw if we specify the encoding.
Expect.equals("X", uri.contentAsString(encoding: ascii));
// Parse format.
Expect.throwsFormatException(() => UriData.parse("notdata:,"));
Expect.throwsFormatException(() => UriData.parse("text/plain,noscheme"));
Expect.throwsFormatException(() => UriData.parse("data:noseparator"));
Expect.throwsFormatException(() => UriData.parse("data:noslash,text"));
Expect.throwsFormatException(
() => UriData.parse("data:type/sub;noequals,text"));
Expect.throwsFormatException(() => UriData.parse("data:type/sub;knocomma="));
Expect.throwsFormatException(
() => UriData.parse("data:type/sub;k=v;nocomma"));
Expect.throwsFormatException(() => UriData.parse("data:type/sub;k=nocomma"));
Expect.throwsFormatException(() => UriData.parse("data:type/sub;k=v;base64"));
void formatError(String input) {
Expect.throwsFormatException(() => UriData.parse("data:;base64,$input"),
input);
}
// Invalid base64 format (detected when parsed).
for (var a = 0; a <= 4; a++) {
for (var p = 0; p <= 4; p++) {
// Base-64 encoding must have length divisible by four and no more
// than two padding characters at the end.
if (p < 3 && (a + p) % 4 == 0) continue;
if (p == 0 && a > 1) continue;
formatError("A" * a + "=" * p);
formatError("A" * a + "%3D" * p);
}
}
// Invalid base64 encoding: padding not at end.
formatError("AA=A");
formatError("A=AA");
formatError("=AAA");
formatError("A==A");
formatError("==AA");
formatError("===A");
formatError("AAA%3D=");
formatError("A%3D==");
// Invalid unpadded data.
formatError("A");
formatError("AAAAA");
// Invalid characters.
formatError("AAA*");
formatError("AAA\x00");
formatError("AAA\\");
formatError("AAA,");
// Invalid escapes.
formatError("AAA%25");
formatError("AAA%7F");
formatError("AAA%7F");
}
/// Checks that two [Uri]s are exactly the same.
expectUriEquals(Uri expect, Uri actual) {
Expect.equals(expect.scheme, actual.scheme, "scheme");
Expect.equals(expect.hasAuthority, actual.hasAuthority, "hasAuthority");
Expect.equals(expect.userInfo, actual.userInfo, "userInfo");
Expect.equals(expect.host, actual.host, "host");
Expect.equals(expect.hasPort, actual.hasPort, "hasPort");
Expect.equals(expect.port, actual.port, "port");
Expect.equals(expect.port, actual.port, "port");
Expect.equals(expect.hasQuery, actual.hasQuery, "hasQuery");
Expect.equals(expect.query, actual.query, "query");
Expect.equals(expect.hasFragment, actual.hasFragment, "hasFragment");
Expect.equals(expect.fragment, actual.fragment, "fragment");
}
void testUriEquals(String uriText) {
var data = UriData.parse(uriText);
var uri = Uri.parse(uriText);
Expect.equals(data.uri, uri);
Expect.equals(data.toString(), uri.data.toString());
Expect.equals(data.toString(), uri.toString());
}

View file

@ -0,0 +1,54 @@
// Copyright (c) 2012, 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 "package:expect/expect.dart";
// Make sure the years in the range of single digits are handled correctly with
// month roll-over. (This tests an edge condition when delegating to
// JavaScript's Date constructor.)
void check(String expected, DateTime actual) {
Expect.equals(expected, actual.toString());
}
testUtc() {
check("0099-01-01 00:00:00.000Z", new DateTime.utc(99, 1));
check("0100-01-01 00:00:00.000Z", new DateTime.utc(99, 1 + 12));
check("0000-01-01 00:00:00.000Z", new DateTime.utc(0, 1));
check("-0001-01-01 00:00:00.000Z", new DateTime.utc(0, 1 - 12));
check("0099-03-02 00:00:00.000Z", new DateTime.utc(99, 2, 30));
check("0100-03-02 00:00:00.000Z", new DateTime.utc(99, 2 + 12, 30));
check("0004-03-01 00:00:00.000Z", new DateTime.utc(3, 2 + 12, 30));
check("0004-03-01 00:00:00.000Z", new DateTime.utc(4, 2, 30));
check("0004-03-01 00:00:00.000Z", new DateTime.utc(5, 2 - 12, 30));
check("0005-03-02 00:00:00.000Z", new DateTime.utc(4, 2 + 12, 30));
check("0005-03-02 00:00:00.000Z", new DateTime.utc(5, 2, 30));
check("0005-03-02 00:00:00.000Z", new DateTime.utc(6, 2 - 12, 30));
}
testLocal() {
check("0099-01-01 00:00:00.000", new DateTime(99, 1));
check("0100-01-01 00:00:00.000", new DateTime(99, 1 + 12));
check("0000-01-01 00:00:00.000", new DateTime(0, 1));
check("-0001-01-01 00:00:00.000", new DateTime(0, 1 - 12));
check("0099-03-02 00:00:00.000", new DateTime(99, 2, 30));
check("0100-03-02 00:00:00.000", new DateTime(99, 2 + 12, 30));
check("0004-03-01 00:00:00.000", new DateTime(3, 2 + 12, 30));
check("0004-03-01 00:00:00.000", new DateTime(4, 2, 30));
check("0004-03-01 00:00:00.000", new DateTime(5, 2 - 12, 30));
check("0005-03-02 00:00:00.000", new DateTime(4, 2 + 12, 30));
check("0005-03-02 00:00:00.000", new DateTime(5, 2, 30));
check("0005-03-02 00:00:00.000", new DateTime(6, 2 - 12, 30));
}
main() {
testUtc();
testLocal();
}

View file

@ -0,0 +1,138 @@
// Copyright (c) 2017, 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 "package:expect/expect.dart";
// Make sure that date-times close to daylight savings work correctly.
// See http://dartbug.com/30550
/// A list of (decomposed) date-times where daylight saving changes
/// happen.
///
/// This list covers multiple timezones to increase test coverage on
/// different machines.
final daylightSavingChanges = [
// TZ environment, y, m, d, h, change.
["Europe/Paris", 2017, 03, 26, 02, 60],
["Europe/Paris", 2017, 10, 29, 03, -60],
["Antarctica/Troll", 2017, 03, 19, 01, 120],
["Antarctica/Troll", 2017, 10, 29, 03, -120],
["Australia/Canberra", 2017, 04, 02, 03, -60],
["Australia/Canberra", 2017, 10, 01, 02, 60],
["Australia/Lord_Howe", 2017, 04, 02, 02, -30],
["Australia/Lord_Howe", 2017, 10, 01, 02, 30],
["Atlantic/Bermuda", 2017, 03, 12, 02, 60], // US and Canada.
["Atlantic/Bermuda", 2017, 11, 05, 02, -60],
["America/Campo_Grande", 2017, 02, 19, 00, -60], // Brazil
["America/Campo_Grande", 2017, 10, 15, 00, 60],
["America/Santiago", 2017, 05, 14, 00, -60],
["America/Santiago", 2017, 08, 13, 00, 60],
["Chile/EasterIsland", 2017, 05, 13, 22, -60],
["Chile/EasterIsland", 2017, 08, 12, 22, 60],
["Pacific/Fiji", 2017, 01, 15, 03, -60],
["Pacific/Fiji", 2017, 11, 05, 02, 60],
["America/Scoresbysund", 2017, 03, 26, 00, 60], // Ittoqqortoormiit.
["America/Scoresbysund", 2017, 10, 29, 01, -60],
["Asia/Tehran", 2017, 03, 22, 00, 60],
["Asia/Tehran", 2017, 09, 22, 00, -60],
["Israel", 2017, 03, 24, 02, 60],
["Israel", 2017, 10, 29, 02, -60],
["Asia/Amman", 2017, 03, 31, 00, 60],
["Asia/Amman", 2017, 10, 27, 01, -60],
["Mexico/General", 2017, 04, 02, 02, 60],
["Mexico/General", 2017, 10, 29, 02, -60],
];
void runTests() {
// Makes sure we don't go into the wrong direction during a
// daylight-savings change (as happened in #30550).
for (var test in daylightSavingChanges) {
for (int i = 0; i < 2; i++) {
var year = test[1] as int;
var month = test[2] as int;
var day = test[3] as int;
var hour = test[4] as int;
var minute = i == 0 ? 0 : test[5] as int;
// Rather adjust the hours than keeping the minutes.
hour += minute ~/ 60;
minute = minute.remainder(60) as int;
if (hour < 0) {
hour += 24;
day--;
}
{
// Check that microseconds are taken into account.
var dtMillisecond = new DateTime(year, month, day, hour, minute, 0, 1);
var dtSecond = new DateTime(year, month, day, hour, minute, 1);
Expect.equals(const Duration(milliseconds: 999),
dtSecond.difference(dtMillisecond));
dtMillisecond = new DateTime(year, month, day, hour, minute, 0, -1);
dtSecond = new DateTime(year, month, day, hour, minute, -1);
Expect.equals(const Duration(milliseconds: 999),
dtMillisecond.difference(dtSecond));
}
var dt1 = new DateTime(year, month, day, hour);
var dt2 = new DateTime(year, month, day, hour, 1);
// Earlier:
int earlierDay = day;
int earlierHour = hour - 1;
if (earlierHour < 0) {
earlierHour = 23;
earlierDay--;
}
var dt3 = new DateTime(year, month, earlierDay, earlierHour, 59);
var diff1 = dt2.difference(dt1).inMinutes;
var diff2 = dt1.difference(dt3).inMinutes;
if (diff1 == 1 && diff2 == 1 && dt1.hour == hour && dt1.minute == 0) {
// Regular date-time.
continue;
}
// At most one is at a distance of more than a minute.
Expect.isTrue(diff1 == 1 || diff2 == 1);
if (diff2 < 0) {
// This happens, when we ask for invalid times.
// Suppose daylight-saving is at 2:00 and switches to 3:00. If we
// ask for 2:59, we get 3:59 (59 minutes after 2:00).
Expect.isFalse(dt3.day == earlierDay && dt3.hour == earlierHour);
// If that's the case, then removing one minute from dt1 should
// not yield a date-time with the earlier values, and it should
// be far away from dt3.
var dt4 = dt1.add(const Duration(minutes: -1));
Expect.isFalse(dt4.day == earlierDay && dt4.hour == earlierHour);
Expect.isTrue(dt4.isBefore(dt1));
Expect.isTrue(dt4.day < earlierDay ||
(dt4.day == earlierDay && dt4.hour < earlierHour));
continue;
}
// They must be in the right order.
Expect.isTrue(dt1.isBefore(dt2));
Expect.isTrue(dt3.isBefore(dt1));
}
}
}
void main(List<String> args) {
// The following code constructs a String with all timezones that are
// relevant for this test.
// This can be helpful for running tests in multiple timezones.
// Typically, one would write something like:
// for tz in <contents_of_string>; do TZ=$tz tools/test.py ...; done
var result = new StringBuffer();
for (int i = 0; i < daylightSavingChanges.length; i += 2) {
if (i != 0) result.write(" ");
result.write(daylightSavingChanges[i][0]);
}
runTests();
}

View file

@ -0,0 +1,16 @@
// Copyright (c) 2012, 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 "package:expect/expect.dart";
// Dart test program for DateTime's hashCode.
main() {
var d = DateTime.parse("2000-01-01T00:00:00Z");
var d2 = DateTime.parse("2000-01-01T00:00:01Z");
// There is no guarantee that the hashcode for these two dates is different,
// but in the worst case we will have to fix this test.
// The important test here is, that DateTime .
Expect.isFalse(d.hashCode == d2.hashCode);
}

View file

@ -0,0 +1,14 @@
// Copyright (c) 2012, 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 "package:expect/expect.dart";
// At some point dart was emitting a bad padding 0 for Dates where the ms were
// ending with 10.
main() {
String s = "2012-01-30 08:30:00.010";
DateTime d = DateTime.parse(s);
Expect.equals(s, d.toString());
}

View file

@ -0,0 +1,104 @@
// Copyright (c) 2012, 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 "package:expect/expect.dart";
// Test fromString with 6 digits after the decimal point.
bool get supportsMicroseconds =>
new DateTime.fromMicrosecondsSinceEpoch(1).microsecondsSinceEpoch == 1;
main() {
if (supportsMicroseconds) {
testMicrosecondPrecision();
} else {
testMillisecondPrecision();
}
}
void testMillisecondPrecision() {
// We only support milliseconds. If the user supplies more data (the "51"
// here), we round.
DateTime dt1 = DateTime.parse("1999-01-02 23:59:59.999519");
Expect.equals(1999, dt1.year);
Expect.equals(1, dt1.month);
Expect.equals(3, dt1.day);
Expect.equals(0, dt1.hour);
Expect.equals(0, dt1.minute);
Expect.equals(0, dt1.second);
Expect.equals(0, dt1.millisecond);
Expect.equals(false, dt1.isUtc);
dt1 = DateTime.parse("1999-01-02 23:58:59.999519Z");
Expect.equals(1999, dt1.year);
Expect.equals(1, dt1.month);
Expect.equals(2, dt1.day);
Expect.equals(23, dt1.hour);
Expect.equals(59, dt1.minute);
Expect.equals(0, dt1.second);
Expect.equals(0, dt1.millisecond);
Expect.equals(true, dt1.isUtc);
dt1 = DateTime.parse("0009-09-09 09:09:09.009411Z");
Expect.equals(9, dt1.year);
Expect.equals(9, dt1.month);
Expect.equals(9, dt1.day);
Expect.equals(9, dt1.hour);
Expect.equals(9, dt1.minute);
Expect.equals(9, dt1.second);
Expect.equals(9, dt1.millisecond);
Expect.equals(true, dt1.isUtc);
String svnDate = "2012-03-30T04:28:13.752341Z";
dt1 = DateTime.parse(svnDate);
Expect.equals(2012, dt1.year);
Expect.equals(3, dt1.month);
Expect.equals(30, dt1.day);
Expect.equals(4, dt1.hour);
Expect.equals(28, dt1.minute);
Expect.equals(13, dt1.second);
Expect.equals(752, dt1.millisecond);
Expect.equals(true, dt1.isUtc);
}
void testMicrosecondPrecision() {
DateTime dt1 = DateTime.parse("1999-01-02 23:59:59.999519");
Expect.equals(1999, dt1.year);
Expect.equals(1, dt1.month);
Expect.equals(2, dt1.day);
Expect.equals(23, dt1.hour);
Expect.equals(59, dt1.minute);
Expect.equals(59, dt1.second);
Expect.equals(999, dt1.millisecond);
Expect.equals(519, dt1.microsecond);
Expect.equals(false, dt1.isUtc);
dt1 = DateTime.parse("1999-01-02 23:58:59.999519Z");
Expect.equals(1999, dt1.year);
Expect.equals(1, dt1.month);
Expect.equals(2, dt1.day);
Expect.equals(23, dt1.hour);
Expect.equals(58, dt1.minute);
Expect.equals(59, dt1.second);
Expect.equals(999, dt1.millisecond);
Expect.equals(519, dt1.microsecond);
Expect.equals(true, dt1.isUtc);
dt1 = DateTime.parse("0009-09-09 09:09:09.009411Z");
Expect.equals(9, dt1.year);
Expect.equals(9, dt1.month);
Expect.equals(9, dt1.day);
Expect.equals(9, dt1.hour);
Expect.equals(9, dt1.minute);
Expect.equals(9, dt1.second);
Expect.equals(9, dt1.millisecond);
Expect.equals(411, dt1.microsecond);
Expect.equals(true, dt1.isUtc);
String svnDate = "2012-03-30T04:28:13.752341Z";
dt1 = DateTime.parse(svnDate);
Expect.equals(2012, dt1.year);
Expect.equals(3, dt1.month);
Expect.equals(30, dt1.day);
Expect.equals(4, dt1.hour);
Expect.equals(28, dt1.minute);
Expect.equals(13, dt1.second);
Expect.equals(752, dt1.millisecond);
Expect.equals(341, dt1.microsecond);
Expect.equals(true, dt1.isUtc);
}

View file

@ -0,0 +1,81 @@
// Copyright (c) 2012, 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 "package:expect/expect.dart";
// Test DateTime constructor with optional arguments.
main() {
var d = new DateTime(2012);
Expect.equals(2012, d.year);
Expect.equals(1, d.month);
Expect.equals(1, d.day);
Expect.equals(0, d.hour);
Expect.equals(0, d.minute);
Expect.equals(0, d.second);
Expect.equals(0, d.millisecond);
d = new DateTime(2012, 1, 28);
Expect.equals(2012, d.year);
Expect.equals(1, d.month);
Expect.equals(28, d.day);
Expect.equals(0, d.hour);
Expect.equals(0, d.minute);
Expect.equals(0, d.second);
Expect.equals(0, d.millisecond);
d = new DateTime(1970, 3);
Expect.equals(1970, d.year);
Expect.equals(3, d.month);
Expect.equals(1, d.day);
Expect.equals(0, d.hour);
Expect.equals(0, d.minute);
Expect.equals(0, d.second);
Expect.equals(0, d.millisecond);
d = new DateTime(1970, 3, 1, 11);
Expect.equals(1970, d.year);
Expect.equals(3, d.month);
Expect.equals(1, d.day);
Expect.equals(11, d.hour);
Expect.equals(0, d.minute);
Expect.equals(0, d.second);
Expect.equals(0, d.millisecond);
d = new DateTime(0, 12, 24, 0, 12);
Expect.equals(0, d.year);
Expect.equals(12, d.month);
Expect.equals(24, d.day);
Expect.equals(0, d.hour);
Expect.equals(12, d.minute);
Expect.equals(0, d.second);
Expect.equals(0, d.millisecond);
d = new DateTime(-1, 2, 2, 3, 0, 0, 4);
Expect.equals(-1, d.year);
Expect.equals(2, d.month);
Expect.equals(2, d.day);
Expect.equals(3, d.hour);
Expect.equals(0, d.minute);
Expect.equals(0, d.second);
Expect.equals(4, d.millisecond);
d = new DateTime(-1, 2, 2, 3, 0, 4);
Expect.equals(-1, d.year);
Expect.equals(2, d.month);
Expect.equals(2, d.day);
Expect.equals(3, d.hour);
Expect.equals(0, d.minute);
Expect.equals(4, d.second);
Expect.equals(0, d.millisecond);
d = new DateTime(2012, 5, 15, 13, 21, 33, 12);
Expect.equals(2012, d.year);
Expect.equals(5, d.month);
Expect.equals(15, d.day);
Expect.equals(13, d.hour);
Expect.equals(21, d.minute);
Expect.equals(33, d.second);
Expect.equals(12, d.millisecond);
}

View file

@ -0,0 +1,31 @@
// Copyright (c) 2012, 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 "package:expect/expect.dart";
// Test DateTime comparison operators.
main() {
var d = new DateTime.fromMillisecondsSinceEpoch(0, isUtc: true);
var d2 = new DateTime.fromMillisecondsSinceEpoch(1, isUtc: true);
Expect.isTrue(d.isBefore(d2));
Expect.isTrue(!d.isAfter(d2));
Expect.isTrue(d2.isAfter(d));
Expect.isTrue(!d2.isBefore(d));
Expect.isFalse(d2.isBefore(d));
Expect.isFalse(!d2.isAfter(d));
Expect.isFalse(d.isAfter(d2));
Expect.isFalse(!d.isBefore(d2));
d = new DateTime.fromMillisecondsSinceEpoch(-1, isUtc: true);
d2 = new DateTime.fromMillisecondsSinceEpoch(0, isUtc: true);
Expect.isTrue(d.isBefore(d2));
Expect.isTrue(!d.isAfter(d2));
Expect.isTrue(d2.isAfter(d));
Expect.isTrue(!d2.isBefore(d));
Expect.isFalse(d2.isBefore(d));
Expect.isFalse(!d2.isAfter(d));
Expect.isFalse(d.isAfter(d2));
Expect.isFalse(!d.isBefore(d2));
}

View file

@ -0,0 +1,50 @@
// Copyright (c) 2012, 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 "package:expect/expect.dart";
// Test DateTime timeZoneName and timeZoneOffset getters.
testUtc() {
var d = DateTime.parse("2012-03-04T03:25:38.123Z");
Expect.equals("UTC", d.timeZoneName);
Expect.equals(0, d.timeZoneOffset.inSeconds);
}
testLocal() {
checkOffset(String name, Duration offset) {
// Timezone abbreviations are not in bijection with their timezones.
// For example AST stands for "Arab Standard Time" (UTC+03), as well as
// "Arabian Standard Time" (UTC+04), or PST stands for Pacific Standard Time
// and Philippine Standard Time.
//
// Hardcode some common timezones.
if (name == "CET") {
Expect.equals(1, offset.inHours);
} else if (name == "CEST") {
Expect.equals(2, offset.inHours);
} else if (name == "GMT") {
Expect.equals(0, offset.inSeconds);
} else if (name == "EST") {
Expect.equals(-5, offset.inHours);
} else if (name == "EDT") {
Expect.equals(-4, offset.inHours);
} else if (name == "PDT") {
Expect.equals(-7, offset.inHours);
}
}
var d = DateTime.parse("2012-01-02T13:45:23");
String name = d.timeZoneName;
checkOffset(name, d.timeZoneOffset);
d = DateTime.parse("2012-07-02T13:45:23");
name = d.timeZoneName;
checkOffset(name, d.timeZoneOffset);
}
main() {
testUtc();
testLocal();
}

View file

@ -0,0 +1,22 @@
// Copyright (c) 2012, 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 "package:expect/expect.dart";
// Make sure the year 0 is correctly printed.
testUtc() {
var d = new DateTime.utc(0, 1, 1);
Expect.equals("0000-01-01 00:00:00.000Z", d.toString());
}
testLocal() {
var d = new DateTime(0, 1, 1);
Expect.equals("0000-01-01 00:00:00.000", d.toString());
}
main() {
testUtc();
testLocal();
}

View file

@ -0,0 +1,42 @@
// Copyright (c) 2013, 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 "package:expect/expect.dart";
main() {
var dt = new DateTime.now();
Expect.isTrue(dt is Comparable);
var dt2 = new DateTime.fromMillisecondsSinceEpoch(100);
var dt3 = new DateTime.fromMillisecondsSinceEpoch(200, isUtc: true);
var dt3b = new DateTime.fromMillisecondsSinceEpoch(200);
var dt4 = new DateTime.fromMillisecondsSinceEpoch(300);
var dt5 = new DateTime.fromMillisecondsSinceEpoch(400, isUtc: true);
var dt5b = new DateTime.fromMillisecondsSinceEpoch(400);
Expect.isTrue(dt2.compareTo(dt2) == 0);
Expect.isTrue(dt3.compareTo(dt3) == 0);
Expect.isTrue(dt3b.compareTo(dt3b) == 0);
Expect.isTrue(dt4.compareTo(dt4) == 0);
Expect.isTrue(dt5.compareTo(dt5) == 0);
Expect.isTrue(dt5b.compareTo(dt5b) == 0);
// Time zones don't have any effect.
Expect.isTrue(dt3.compareTo(dt3b) == 0);
Expect.isTrue(dt5.compareTo(dt5b) == 0);
Expect.isTrue(dt2.compareTo(dt3) < 0);
Expect.isTrue(dt3.compareTo(dt4) < 0);
Expect.isTrue(dt4.compareTo(dt5) < 0);
Expect.isTrue(dt2.compareTo(dt3b) < 0);
Expect.isTrue(dt4.compareTo(dt5b) < 0);
Expect.isTrue(dt3.compareTo(dt2) > 0);
Expect.isTrue(dt4.compareTo(dt3) > 0);
Expect.isTrue(dt5.compareTo(dt4) > 0);
Expect.isTrue(dt3b.compareTo(dt2) > 0);
Expect.isTrue(dt5b.compareTo(dt4) > 0);
}

View file

@ -0,0 +1,99 @@
// Copyright (c) 2012, 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 "package:expect/expect.dart";
// Dart test program for DateTime, extreme values.
bool get supportsMicroseconds =>
new DateTime.fromMicrosecondsSinceEpoch(1).microsecondsSinceEpoch == 1;
// Identical to _maxMillisecondsSinceEpoch in date_time.dart
const int _MAX_MILLISECONDS = 8640000000000000;
void testExtremes() {
var dt =
new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
Expect.equals(275760, dt.year);
Expect.equals(9, dt.month);
Expect.equals(13, dt.day);
Expect.equals(0, dt.hour);
Expect.equals(0, dt.minute);
Expect.equals(0, dt.second);
Expect.equals(0, dt.millisecond);
Expect.equals(0, dt.microsecond);
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
Expect.equals(-271821, dt.year);
Expect.equals(4, dt.month);
Expect.equals(20, dt.day);
Expect.equals(0, dt.hour);
Expect.equals(0, dt.minute);
Expect.equals(0, dt.second);
Expect.equals(0, dt.millisecond);
Expect.equals(0, dt.microsecond);
// Make sure that we can build the extreme dates in local too.
dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
Expect.equals(_MAX_MILLISECONDS, dt.millisecondsSinceEpoch);
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
Expect.equals(-_MAX_MILLISECONDS, dt.millisecondsSinceEpoch);
Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
_MAX_MILLISECONDS + 1,
isUtc: true));
Expect.throws(() => new DateTime.fromMillisecondsSinceEpoch(
-_MAX_MILLISECONDS - 1,
isUtc: true));
Expect.throws(
() => new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS + 1));
Expect.throws(
() => new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS - 1));
dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
Expect.throws(
() => new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 1));
dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
Expect.throws(() =>
new DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 1));
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
Expect.throws(
() => new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, -1));
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
Expect.throws(() =>
new DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, -1));
if (!supportsMicroseconds) return;
dt = new DateTime.fromMicrosecondsSinceEpoch(_MAX_MILLISECONDS * 1000);
dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
Expect.equals(_MAX_MILLISECONDS * 1000, dt.microsecondsSinceEpoch);
dt = new DateTime.fromMicrosecondsSinceEpoch(-_MAX_MILLISECONDS * 1000);
dt = new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute);
Expect.equals(-_MAX_MILLISECONDS * 1000, dt.microsecondsSinceEpoch);
Expect.throws(() => new DateTime.fromMicrosecondsSinceEpoch(
_MAX_MILLISECONDS * 1000 + 1,
isUtc: true));
Expect.throws(() => new DateTime.fromMicrosecondsSinceEpoch(
-_MAX_MILLISECONDS * 1000 - 1,
isUtc: true));
Expect.throws(() =>
new DateTime.fromMicrosecondsSinceEpoch(_MAX_MILLISECONDS * 1000 + 1));
Expect.throws(() =>
new DateTime.fromMicrosecondsSinceEpoch(-_MAX_MILLISECONDS * 1000 - 1));
dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
Expect.throws(() =>
new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, 1));
dt = new DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
Expect.throws(() =>
new DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, 1));
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
Expect.throws(() =>
new DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, -1));
dt = new DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
Expect.throws(() => new DateTime.utc(
dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, -1));
}
void main() {
testExtremes();
}

View file

@ -0,0 +1,107 @@
// Copyright (c) 2019, 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 "package:expect/expect.dart";
// Dart test program for DateTime, far away dates.
// TODO(37442): Find far-away dates with milliseconds-since-epoch values that
// are 'web' integers.
bool get supportsMicroseconds =>
new DateTime.fromMicrosecondsSinceEpoch(1).microsecondsSinceEpoch == 1;
void testFarAwayDates() {
DateTime dt =
new DateTime.fromMillisecondsSinceEpoch(1000000000000001, isUtc: true);
Expect.equals(33658, dt.year);
Expect.equals(9, dt.month);
Expect.equals(27, dt.day);
Expect.equals(1, dt.hour);
Expect.equals(46, dt.minute);
Expect.equals(40, dt.second);
Expect.equals(1, dt.millisecond);
Expect.equals(0, dt.microsecond);
dt = new DateTime.fromMillisecondsSinceEpoch(-1000000000000001, isUtc: true);
Expect.equals(-29719, dt.year);
Expect.equals(4, dt.month);
Expect.equals(5, dt.day);
Expect.equals(22, dt.hour);
Expect.equals(13, dt.minute);
Expect.equals(19, dt.second);
Expect.equals(999, dt.millisecond);
Expect.equals(0, dt.microsecond);
// Same with local zone.
dt = new DateTime.fromMillisecondsSinceEpoch(1000000000000001);
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 hour.
Expect.equals(true, dt.hour >= 0 && dt.hour < 24);
// Timezones can have offsets down to 15 minute.
Expect.equals(true, dt.minute % 15 == 46 % 15);
Expect.equals(40, dt.second);
Expect.equals(1, dt.millisecond);
Expect.equals(0, dt.microsecond);
dt = new DateTime.fromMillisecondsSinceEpoch(-1000000000000001);
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 hour.
Expect.equals(true, dt.hour >= 0 && dt.hour < 24);
// Timezones can have offsets down to 15 minute.
Expect.equals(true, dt.minute % 15 == 13);
Expect.equals(19, dt.second);
Expect.equals(999, dt.millisecond);
Expect.equals(0, dt.microsecond);
if (!supportsMicroseconds) return;
dt =
new DateTime.fromMicrosecondsSinceEpoch(1000000000000000001, isUtc: true);
Expect.equals(33658, dt.year);
Expect.equals(9, dt.month);
Expect.equals(27, dt.day);
Expect.equals(1, dt.hour);
Expect.equals(46, dt.minute);
Expect.equals(40, dt.second);
Expect.equals(0, dt.millisecond);
Expect.equals(1, dt.microsecond);
dt = new DateTime.fromMicrosecondsSinceEpoch(-1000000000000000001,
isUtc: true);
Expect.equals(-29719, dt.year);
Expect.equals(4, dt.month);
Expect.equals(5, dt.day);
Expect.equals(22, dt.hour);
Expect.equals(13, dt.minute);
Expect.equals(19, dt.second);
Expect.equals(999, dt.millisecond);
Expect.equals(999, dt.microsecond);
// Same with local zone.
dt = new DateTime.fromMicrosecondsSinceEpoch(1000000000000000001);
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 hour.
Expect.equals(true, dt.hour >= 0 && dt.hour < 24);
// Timezones can have offsets down to 15 minute.
Expect.equals(true, dt.minute % 15 == 46 % 15);
Expect.equals(40, dt.second);
Expect.equals(0, dt.millisecond);
Expect.equals(1, dt.microsecond);
dt = new DateTime.fromMicrosecondsSinceEpoch(-1000000000000000001);
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 hour.
Expect.equals(true, dt.hour >= 0 && dt.hour < 24);
// Timezones can have offsets down to 15 minute.
Expect.equals(true, dt.minute % 15 == 13);
Expect.equals(19, dt.second);
Expect.equals(999, dt.millisecond);
Expect.equals(999, dt.microsecond);
}
void main() {
testFarAwayDates();
}

View file

@ -0,0 +1,56 @@
// Copyright (c) 2013, 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 "package:expect/expect.dart";
check(DateTime expected, String str) {
DateTime actual = DateTime.parse(str);
Expect.equals(expected, actual); // Only checks if they are at the same time.
Expect.equals(expected.isUtc, actual.isUtc);
}
bool get supportsMicroseconds =>
new DateTime.fromMicrosecondsSinceEpoch(1).microsecondsSinceEpoch == 1;
main() {
check(new DateTime(2012, 02, 27, 13, 27), "2012-02-27 13:27:00");
if (supportsMicroseconds) {
check(new DateTime.utc(2012, 02, 27, 13, 27, 0, 123, 456),
"2012-02-27 13:27:00.1234567891234z");
check(new DateTime.utc(2012, 02, 27, 13, 27, 0, 123, 456),
"2012-02-27 13:27:00,1234567891234z");
} else {
check(new DateTime.utc(2012, 02, 27, 13, 27, 0, 123, 0),
"2012-02-27 13:27:00.1234567891234z");
check(new DateTime.utc(2012, 02, 27, 13, 27, 0, 123, 0),
"2012-02-27 13:27:00,1234567891234z");
}
check(new DateTime(2012, 02, 27, 13, 27), "20120227 13:27:00");
check(new DateTime(2012, 02, 27, 13, 27), "20120227T132700");
check(new DateTime(2012, 02, 27), "20120227");
check(new DateTime(2012, 02, 27), "+20120227");
check(new DateTime.utc(2012, 02, 27, 14), "2012-02-27T14Z");
check(new DateTime.utc(-12345, 1, 1), "-123450101 00:00:00 Z");
check(new DateTime.utc(2012, 02, 27, 14), "2012-02-27T14+00");
check(new DateTime.utc(2012, 02, 27, 14), "2012-02-27T14+0000");
check(new DateTime.utc(2012, 02, 27, 14), "2012-02-27T14+00:00");
check(new DateTime.utc(2012, 02, 27, 14), "2012-02-27T14 +00:00");
check(new DateTime.utc(2015, 02, 14, 13, 0, 0, 0), "2015-02-15T00:00+11");
check(new DateTime.utc(2015, 02, 14, 13, 0, 0, 0), "2015-02-15T00:00:00+11");
check(
new DateTime.utc(2015, 02, 14, 13, 0, 0, 0), "2015-02-15T00:00:00+11:00");
if (supportsMicroseconds) {
check(new DateTime.utc(2015, 02, 15, 0, 0, 0, 500, 500),
"2015-02-15T00:00:00.500500Z");
check(new DateTime.utc(2015, 02, 15, 0, 0, 0, 511, 500),
"2015-02-15T00:00:00.511500Z");
} else {
check(new DateTime.utc(2015, 02, 15, 0, 0, 0, 501),
"2015-02-15T00:00:00.501Z");
check(new DateTime.utc(2015, 02, 15, 0, 0, 0, 512),
"2015-02-15T00:00:00.512Z");
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,11 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.throwsUnsupportedError(() => double.infinity.ceil());
Expect.throwsUnsupportedError(() => double.negativeInfinity.ceil());
Expect.throwsUnsupportedError(() => double.nan.ceil());
}

View file

@ -0,0 +1,80 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.equals(0, 0.0.ceil());
Expect.equals(1, double.minPositive.ceil());
Expect.equals(1, (2.0 * double.minPositive).ceil());
Expect.equals(1, (1.18e-38).ceil());
Expect.equals(1, (1.18e-38 * 2).ceil());
Expect.equals(1, 0.49999999999999994.ceil());
Expect.equals(1, 0.5.ceil());
Expect.equals(1, 0.9999999999999999.ceil());
Expect.equals(1, 1.0.ceil());
Expect.equals(2, 1.000000000000001.ceil());
// The following numbers are on the border of 52 bits.
// For example: 4503599627370499 + 0.5 => 4503599627370500.
Expect.equals(4503599627370496, 4503599627370496.0.ceil());
Expect.equals(4503599627370497, 4503599627370497.0.ceil());
Expect.equals(4503599627370498, 4503599627370498.0.ceil());
Expect.equals(4503599627370499, 4503599627370499.0.ceil());
Expect.equals(9007199254740991, 9007199254740991.0.ceil());
Expect.equals(9007199254740992, 9007199254740992.0.ceil());
Expect.equals(9223372036854775807, double.maxFinite.ceil()); // //# int64: ok
Expect.equals(0, (-double.minPositive).ceil());
Expect.equals(0, (2.0 * -double.minPositive).ceil());
Expect.equals(0, (-1.18e-38).ceil());
Expect.equals(0, (-1.18e-38 * 2).ceil());
Expect.equals(0, (-0.49999999999999994).ceil());
Expect.equals(0, (-0.5).ceil());
Expect.equals(0, (-0.9999999999999999).ceil());
Expect.equals(-1, (-1.0).ceil());
Expect.equals(-1, (-1.000000000000001).ceil());
Expect.equals(-4503599627370496, (-4503599627370496.0).ceil());
Expect.equals(-4503599627370497, (-4503599627370497.0).ceil());
Expect.equals(-4503599627370498, (-4503599627370498.0).ceil());
Expect.equals(-4503599627370499, (-4503599627370499.0).ceil());
Expect.equals(-9007199254740991, (-9007199254740991.0).ceil());
Expect.equals(-9007199254740992, (-9007199254740992.0).ceil());
Expect.equals(-9223372036854775808, (-double.maxFinite).ceil()); // //# int64: ok
Expect.isTrue(0.0.ceil() is int);
Expect.isTrue(double.minPositive.ceil() is int);
Expect.isTrue((2.0 * double.minPositive).ceil() is int);
Expect.isTrue((1.18e-38).ceil() is int);
Expect.isTrue((1.18e-38 * 2).ceil() is int);
Expect.isTrue(0.49999999999999994.ceil() is int);
Expect.isTrue(0.5.ceil() is int);
Expect.isTrue(0.9999999999999999.ceil() is int);
Expect.isTrue(1.0.ceil() is int);
Expect.isTrue(1.000000000000001.ceil() is int);
Expect.isTrue(4503599627370496.0.ceil() is int);
Expect.isTrue(4503599627370497.0.ceil() is int);
Expect.isTrue(4503599627370498.0.ceil() is int);
Expect.isTrue(4503599627370499.0.ceil() is int);
Expect.isTrue(9007199254740991.0.ceil() is int);
Expect.isTrue(9007199254740992.0.ceil() is int);
Expect.isTrue(double.maxFinite.ceil() is int);
Expect.isTrue((-double.minPositive).ceil() is int);
Expect.isTrue((2.0 * -double.minPositive).ceil() is int);
Expect.isTrue((-1.18e-38).ceil() is int);
Expect.isTrue((-1.18e-38 * 2).ceil() is int);
Expect.isTrue((-0.49999999999999994).ceil() is int);
Expect.isTrue((-0.5).ceil() is int);
Expect.isTrue((-0.9999999999999999).ceil() is int);
Expect.isTrue((-1.0).ceil() is int);
Expect.isTrue((-1.000000000000001).ceil() is int);
Expect.isTrue((-4503599627370496.0).ceil() is int);
Expect.isTrue((-4503599627370497.0).ceil() is int);
Expect.isTrue((-4503599627370498.0).ceil() is int);
Expect.isTrue((-4503599627370499.0).ceil() is int);
Expect.isTrue((-9007199254740991.0).ceil() is int);
Expect.isTrue((-9007199254740992.0).ceil() is int);
Expect.isTrue((-double.maxFinite).ceil() is int);
}

View file

@ -0,0 +1,93 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.equals(0.0, 0.0.ceilToDouble());
Expect.equals(1.0, double.minPositive.ceilToDouble());
Expect.equals(1.0, (2.0 * double.minPositive).ceilToDouble());
Expect.equals(1.0, (1.18e-38).ceilToDouble());
Expect.equals(1.0, (1.18e-38 * 2).ceilToDouble());
Expect.equals(1.0, 0.49999999999999994.ceilToDouble());
Expect.equals(1.0, 0.5.ceilToDouble());
Expect.equals(1.0, 0.9999999999999999.ceilToDouble());
Expect.equals(1.0, 1.0.ceilToDouble());
Expect.equals(2.0, 1.000000000000001.ceilToDouble());
// The following numbers are on the border of 52 bits.
// For example: 4503599627370499 + 0.5 => 4503599627370500.
Expect.equals(4503599627370496.0, 4503599627370496.0.ceilToDouble());
Expect.equals(4503599627370497.0, 4503599627370497.0.ceilToDouble());
Expect.equals(4503599627370498.0, 4503599627370498.0.ceilToDouble());
Expect.equals(4503599627370499.0, 4503599627370499.0.ceilToDouble());
Expect.equals(9007199254740991.0, 9007199254740991.0.ceilToDouble());
Expect.equals(9007199254740992.0, 9007199254740992.0.ceilToDouble());
Expect.equals(double.maxFinite, double.maxFinite.ceilToDouble());
Expect.equals(0.0, (-double.minPositive).ceilToDouble());
Expect.equals(0.0, (2.0 * -double.minPositive).ceilToDouble());
Expect.equals(0.0, (-1.18e-38).ceilToDouble());
Expect.equals(0.0, (-1.18e-38 * 2).ceilToDouble());
Expect.equals(0.0, (-0.49999999999999994).ceilToDouble());
Expect.equals(0.0, (-0.5).ceilToDouble());
Expect.equals(0.0, (-0.9999999999999999).ceilToDouble());
Expect.equals(-1.0, (-1.0).ceilToDouble());
Expect.equals(-1.0, (-1.000000000000001).ceilToDouble());
Expect.equals(-4503599627370496.0, (-4503599627370496.0).ceilToDouble());
Expect.equals(-4503599627370497.0, (-4503599627370497.0).ceilToDouble());
Expect.equals(-4503599627370498.0, (-4503599627370498.0).ceilToDouble());
Expect.equals(-4503599627370499.0, (-4503599627370499.0).ceilToDouble());
Expect.equals(-9007199254740991.0, (-9007199254740991.0).ceilToDouble());
Expect.equals(-9007199254740992.0, (-9007199254740992.0).ceilToDouble());
Expect.equals(-double.maxFinite, (-double.maxFinite).ceilToDouble());
Expect.equals(double.infinity, double.infinity.ceilToDouble());
Expect.equals(
double.negativeInfinity, double.negativeInfinity.ceilToDouble());
Expect.isTrue(double.nan.ceilToDouble().isNaN);
Expect.isTrue(0.0.ceilToDouble() is double);
Expect.isTrue(double.minPositive.ceilToDouble() is double);
Expect.isTrue((2.0 * double.minPositive).ceilToDouble() is double);
Expect.isTrue((1.18e-38).ceilToDouble() is double);
Expect.isTrue((1.18e-38 * 2).ceilToDouble() is double);
Expect.isTrue(0.49999999999999994.ceilToDouble() is double);
Expect.isTrue(0.5.ceilToDouble() is double);
Expect.isTrue(0.9999999999999999.ceilToDouble() is double);
Expect.isTrue(1.0.ceilToDouble() is double);
Expect.isTrue(1.000000000000001.ceilToDouble() is double);
Expect.isTrue(4503599627370496.0.ceilToDouble() is double);
Expect.isTrue(4503599627370497.0.ceilToDouble() is double);
Expect.isTrue(4503599627370498.0.ceilToDouble() is double);
Expect.isTrue(4503599627370499.0.ceilToDouble() is double);
Expect.isTrue(9007199254740991.0.ceilToDouble() is double);
Expect.isTrue(9007199254740992.0.ceilToDouble() is double);
Expect.isTrue(double.maxFinite.ceilToDouble() is double);
Expect.isTrue((-double.minPositive).ceilToDouble().isNegative);
Expect.isTrue((2.0 * -double.minPositive).ceilToDouble().isNegative);
Expect.isTrue((-1.18e-38).ceilToDouble().isNegative);
Expect.isTrue((-1.18e-38 * 2).ceilToDouble().isNegative);
Expect.isTrue((-0.49999999999999994).ceilToDouble().isNegative);
Expect.isTrue((-0.5).ceilToDouble().isNegative);
Expect.isTrue((-0.9999999999999999).ceilToDouble().isNegative);
Expect.isTrue((-double.minPositive).ceilToDouble() is double);
Expect.isTrue((2.0 * -double.minPositive).ceilToDouble() is double);
Expect.isTrue((-1.18e-38).ceilToDouble() is double);
Expect.isTrue((-1.18e-38 * 2).ceilToDouble() is double);
Expect.isTrue((-0.49999999999999994).ceilToDouble() is double);
Expect.isTrue((-0.5).ceilToDouble() is double);
Expect.isTrue((-0.9999999999999999).ceilToDouble() is double);
Expect.isTrue((-1.0).ceilToDouble() is double);
Expect.isTrue((-1.000000000000001).ceilToDouble() is double);
Expect.isTrue((-4503599627370496.0).ceilToDouble() is double);
Expect.isTrue((-4503599627370497.0).ceilToDouble() is double);
Expect.isTrue((-4503599627370498.0).ceilToDouble() is double);
Expect.isTrue((-4503599627370499.0).ceilToDouble() is double);
Expect.isTrue((-9007199254740991.0).ceilToDouble() is double);
Expect.isTrue((-9007199254740992.0).ceilToDouble() is double);
Expect.isTrue((-double.maxFinite).ceilToDouble() is double);
}

View file

@ -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.
import "package:expect/expect.dart";
// Dart test program for testing 'compare' on doubles.
void main() {
Expect.equals(0, (0.0).compareTo(0.0));
Expect.equals(0, (1.0).compareTo(1.0));
Expect.equals(0, (-2.0).compareTo(-2.0));
Expect.equals(0, (1e-50).compareTo(1e-50));
Expect.equals(0, (-2e50).compareTo(-2e50));
Expect.equals(0, double.nan.compareTo(double.nan));
Expect.equals(0, double.infinity.compareTo(double.infinity));
Expect.equals(0, double.negativeInfinity.compareTo(double.negativeInfinity));
Expect.equals(0, (-0.0).compareTo(-0.0));
Expect.isTrue((0.0).compareTo(1.0) < 0);
Expect.isTrue((1.0).compareTo(0.0) > 0);
Expect.isTrue((0.0).compareTo(-1.0) > 0);
Expect.isTrue((-1.0).compareTo(0.0) < 0);
Expect.isTrue((0.0).compareTo(1234e11) < 0);
Expect.isTrue((123e-112).compareTo(0.0) > 0);
Expect.isTrue((0.0).compareTo(-123.0e12) > 0);
Expect.isTrue((-1.0e8).compareTo(0.0) < 0);
double maxDouble = 1.7976931348623157e308;
Expect.equals(0, maxDouble.compareTo(maxDouble));
Expect.isTrue(maxDouble.compareTo(double.infinity) < 0);
Expect.isTrue(double.infinity.compareTo(maxDouble) > 0);
double negMaxDouble = -maxDouble;
Expect.equals(0, negMaxDouble.compareTo(negMaxDouble));
Expect.isTrue(double.negativeInfinity.compareTo(negMaxDouble) < 0);
Expect.isTrue(negMaxDouble.compareTo(double.negativeInfinity) > 0);
Expect.isTrue((-0.0).compareTo(0.0) < 0);
Expect.isTrue((0.0).compareTo(-0.0) > 0);
Expect.isTrue(double.nan.compareTo(double.infinity) > 0);
Expect.isTrue(double.nan.compareTo(double.negativeInfinity) > 0);
Expect.isTrue(double.infinity.compareTo(double.nan) < 0);
Expect.isTrue(double.negativeInfinity.compareTo(double.nan) < 0);
Expect.isTrue(maxDouble.compareTo(double.nan) < 0);
Expect.isTrue(negMaxDouble.compareTo(double.nan) < 0);
Expect.isTrue(double.nan.compareTo(maxDouble) > 0);
Expect.isTrue(double.nan.compareTo(negMaxDouble) > 0);
}

View file

@ -0,0 +1,11 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.throwsUnsupportedError(() => double.infinity.floor());
Expect.throwsUnsupportedError(() => double.negativeInfinity.floor());
Expect.throwsUnsupportedError(() => double.nan.floor());
}

View file

@ -0,0 +1,80 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.equals(0, 0.0.floor());
Expect.equals(0, double.minPositive.floor());
Expect.equals(0, (2.0 * double.minPositive).floor());
Expect.equals(0, (1.18e-38).floor());
Expect.equals(0, (1.18e-38 * 2).floor());
Expect.equals(0, 0.49999999999999994.floor());
Expect.equals(0, 0.5.floor());
Expect.equals(0, 0.9999999999999999.floor());
Expect.equals(1, 1.0.floor());
Expect.equals(1, 1.000000000000001.floor());
// The following numbers are on the border of 52 bits.
// For example: 4503599627370499 + 0.5 => 4503599627370500.
Expect.equals(4503599627370496, 4503599627370496.0.floor());
Expect.equals(4503599627370497, 4503599627370497.0.floor());
Expect.equals(4503599627370498, 4503599627370498.0.floor());
Expect.equals(4503599627370499, 4503599627370499.0.floor());
Expect.equals(9007199254740991, 9007199254740991.0.floor());
Expect.equals(9007199254740992, 9007199254740992.0.floor());
Expect.equals(9223372036854775807, double.maxFinite.floor()); // //# int64: ok
Expect.equals(-1, (-double.minPositive).floor());
Expect.equals(-1, (2.0 * -double.minPositive).floor());
Expect.equals(-1, (-1.18e-38).floor());
Expect.equals(-1, (-1.18e-38 * 2).floor());
Expect.equals(-1, (-0.49999999999999994).floor());
Expect.equals(-1, (-0.5).floor());
Expect.equals(-1, (-0.9999999999999999).floor());
Expect.equals(-1, (-1.0).floor());
Expect.equals(-2, (-1.000000000000001).floor());
Expect.equals(-4503599627370496, (-4503599627370496.0).floor());
Expect.equals(-4503599627370497, (-4503599627370497.0).floor());
Expect.equals(-4503599627370498, (-4503599627370498.0).floor());
Expect.equals(-4503599627370499, (-4503599627370499.0).floor());
Expect.equals(-9007199254740991, (-9007199254740991.0).floor());
Expect.equals(-9007199254740992, (-9007199254740992.0).floor());
Expect.equals(-9223372036854775808, (-double.maxFinite).floor()); // //# int64: ok
Expect.isTrue(0.0.floor() is int);
Expect.isTrue(double.minPositive.floor() is int);
Expect.isTrue((2.0 * double.minPositive).floor() is int);
Expect.isTrue((1.18e-38).floor() is int);
Expect.isTrue((1.18e-38 * 2).floor() is int);
Expect.isTrue(0.49999999999999994.floor() is int);
Expect.isTrue(0.5.floor() is int);
Expect.isTrue(0.9999999999999999.floor() is int);
Expect.isTrue(1.0.floor() is int);
Expect.isTrue(1.000000000000001.floor() is int);
Expect.isTrue(4503599627370496.0.floor() is int);
Expect.isTrue(4503599627370497.0.floor() is int);
Expect.isTrue(4503599627370498.0.floor() is int);
Expect.isTrue(4503599627370499.0.floor() is int);
Expect.isTrue(9007199254740991.0.floor() is int);
Expect.isTrue(9007199254740992.0.floor() is int);
Expect.isTrue(double.maxFinite.floor() is int);
Expect.isTrue((-double.minPositive).floor() is int);
Expect.isTrue((2.0 * -double.minPositive).floor() is int);
Expect.isTrue((-1.18e-38).floor() is int);
Expect.isTrue((-1.18e-38 * 2).floor() is int);
Expect.isTrue((-0.49999999999999994).floor() is int);
Expect.isTrue((-0.5).floor() is int);
Expect.isTrue((-0.9999999999999999).floor() is int);
Expect.isTrue((-1.0).floor() is int);
Expect.isTrue((-1.000000000000001).floor() is int);
Expect.isTrue((-4503599627370496.0).floor() is int);
Expect.isTrue((-4503599627370497.0).floor() is int);
Expect.isTrue((-4503599627370498.0).floor() is int);
Expect.isTrue((-4503599627370499.0).floor() is int);
Expect.isTrue((-9007199254740991.0).floor() is int);
Expect.isTrue((-9007199254740992.0).floor() is int);
Expect.isTrue((-double.maxFinite).floor() is int);
}

View file

@ -0,0 +1,85 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.equals(0.0, 0.0.floorToDouble());
Expect.equals(0.0, double.minPositive.floorToDouble());
Expect.equals(0.0, (2.0 * double.minPositive).floorToDouble());
Expect.equals(0.0, (1.18e-38).floorToDouble());
Expect.equals(0.0, (1.18e-38 * 2).floorToDouble());
Expect.equals(0.0, 0.49999999999999994.floorToDouble());
Expect.equals(0.0, 0.5.floorToDouble());
Expect.equals(0.0, 0.9999999999999999.floorToDouble());
Expect.equals(1.0, 1.0.floorToDouble());
Expect.equals(1.0, 1.000000000000001.floorToDouble());
// The following numbers are on the border of 52 bits.
// For example: 4503599627370499 + 0.5 => 4503599627370500.
Expect.equals(4503599627370496.0, 4503599627370496.0.floorToDouble());
Expect.equals(4503599627370497.0, 4503599627370497.0.floorToDouble());
Expect.equals(4503599627370498.0, 4503599627370498.0.floorToDouble());
Expect.equals(4503599627370499.0, 4503599627370499.0.floorToDouble());
Expect.equals(9007199254740991.0, 9007199254740991.0.floorToDouble());
Expect.equals(9007199254740992.0, 9007199254740992.0.floorToDouble());
Expect.equals(double.maxFinite, double.maxFinite.floorToDouble());
Expect.equals(-1.0, (-double.minPositive).floorToDouble());
Expect.equals(-1.0, (2.0 * -double.minPositive).floorToDouble());
Expect.equals(-1.0, (-1.18e-38).floorToDouble());
Expect.equals(-1.0, (-1.18e-38 * 2).floorToDouble());
Expect.equals(-1.0, (-0.49999999999999994).floorToDouble());
Expect.equals(-1.0, (-0.5).floorToDouble());
Expect.equals(-1.0, (-0.9999999999999999).floorToDouble());
Expect.equals(-1.0, (-1.0).floorToDouble());
Expect.equals(-2.0, (-1.000000000000001).floorToDouble());
Expect.equals(-4503599627370496.0, (-4503599627370496.0).floorToDouble());
Expect.equals(-4503599627370497.0, (-4503599627370497.0).floorToDouble());
Expect.equals(-4503599627370498.0, (-4503599627370498.0).floorToDouble());
Expect.equals(-4503599627370499.0, (-4503599627370499.0).floorToDouble());
Expect.equals(-9007199254740991.0, (-9007199254740991.0).floorToDouble());
Expect.equals(-9007199254740992.0, (-9007199254740992.0).floorToDouble());
Expect.equals(-double.maxFinite, (-double.maxFinite).floorToDouble());
Expect.equals(double.infinity, double.infinity.floorToDouble());
Expect.equals(
double.negativeInfinity, double.negativeInfinity.floorToDouble());
Expect.isTrue(double.nan.floorToDouble().isNaN);
Expect.isTrue(0.0.floorToDouble() is double);
Expect.isTrue(double.minPositive.floorToDouble() is double);
Expect.isTrue((2.0 * double.minPositive).floorToDouble() is double);
Expect.isTrue((1.18e-38).floorToDouble() is double);
Expect.isTrue((1.18e-38 * 2).floorToDouble() is double);
Expect.isTrue(0.49999999999999994.floorToDouble() is double);
Expect.isTrue(0.5.floorToDouble() is double);
Expect.isTrue(0.9999999999999999.floorToDouble() is double);
Expect.isTrue(1.0.floorToDouble() is double);
Expect.isTrue(1.000000000000001.floorToDouble() is double);
Expect.isTrue(4503599627370496.0.floorToDouble() is double);
Expect.isTrue(4503599627370497.0.floorToDouble() is double);
Expect.isTrue(4503599627370498.0.floorToDouble() is double);
Expect.isTrue(4503599627370499.0.floorToDouble() is double);
Expect.isTrue(9007199254740991.0.floorToDouble() is double);
Expect.isTrue(9007199254740992.0.floorToDouble() is double);
Expect.isTrue(double.maxFinite.floorToDouble() is double);
Expect.isTrue((-double.minPositive).floorToDouble() is double);
Expect.isTrue((2.0 * -double.minPositive).floorToDouble() is double);
Expect.isTrue((-1.18e-38).floorToDouble() is double);
Expect.isTrue((-1.18e-38 * 2).floorToDouble() is double);
Expect.isTrue((-0.49999999999999994).floorToDouble() is double);
Expect.isTrue((-0.5).floorToDouble() is double);
Expect.isTrue((-0.9999999999999999).floorToDouble() is double);
Expect.isTrue((-1.0).floorToDouble() is double);
Expect.isTrue((-1.000000000000001).floorToDouble() is double);
Expect.isTrue((-4503599627370496.0).floorToDouble() is double);
Expect.isTrue((-4503599627370497.0).floorToDouble() is double);
Expect.isTrue((-4503599627370498.0).floorToDouble() is double);
Expect.isTrue((-4503599627370499.0).floorToDouble() is double);
Expect.isTrue((-9007199254740991.0).floorToDouble() is double);
Expect.isTrue((-9007199254740992.0).floorToDouble() is double);
Expect.isTrue((-double.maxFinite).floorToDouble() is double);
}

View file

@ -0,0 +1,26 @@
// Copyright (c) 2017, 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=--intrinsify
// VMOptions=--no_intrinsify
import 'package:expect/expect.dart';
main() {
for (int x in [0, 1, 0xffff, 0xffffffff, 0x111111111111, 0xffffffffffff]) {
test(x);
test(-x);
}
// Test with ints outside the 53-bit range that are known to have an
// exact double representation.
test(9007199254840856);
test(144115188075954880);
test(936748722493162112);
}
test(int x) {
Expect.equals(x, x.toDouble().toInt(), "bad test argument ($x)");
Expect.equals(x.hashCode, x.toDouble().hashCode);
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,11 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.throwsUnsupportedError(() => double.infinity.round());
Expect.throwsUnsupportedError(() => double.negativeInfinity.round());
Expect.throwsUnsupportedError(() => double.nan.round());
}

View file

@ -0,0 +1,12 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.equals(0, 0.49999999999999994.round());
Expect.equals(0, (-0.49999999999999994).round());
Expect.isTrue(0.49999999999999994.round() is int);
Expect.isTrue((-0.49999999999999994).round() is int);
}

View file

@ -0,0 +1,38 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
// The following numbers are on the border of 52 bits.
// For example: 4503599627370499 + 0.5 => 4503599627370500.
Expect.equals(4503599627370496, 4503599627370496.0.round());
Expect.equals(4503599627370497, 4503599627370497.0.round());
Expect.equals(4503599627370498, 4503599627370498.0.round());
Expect.equals(4503599627370499, 4503599627370499.0.round());
Expect.equals(9007199254740991, 9007199254740991.0.round());
Expect.equals(9007199254740992, 9007199254740992.0.round());
Expect.equals(-4503599627370496, (-4503599627370496.0).round());
Expect.equals(-4503599627370497, (-4503599627370497.0).round());
Expect.equals(-4503599627370498, (-4503599627370498.0).round());
Expect.equals(-4503599627370499, (-4503599627370499.0).round());
Expect.equals(-9007199254740991, (-9007199254740991.0).round());
Expect.equals(-9007199254740992, (-9007199254740992.0).round());
Expect.isTrue(4503599627370496.0.round() is int);
Expect.isTrue(4503599627370497.0.round() is int);
Expect.isTrue(4503599627370498.0.round() is int);
Expect.isTrue(4503599627370499.0.round() is int);
Expect.isTrue(9007199254740991.0.round() is int);
Expect.isTrue(9007199254740992.0.round() is int);
Expect.isTrue((-4503599627370496.0).round() is int);
Expect.isTrue((-4503599627370497.0).round() is int);
Expect.isTrue((-4503599627370498.0).round() is int);
Expect.isTrue((-4503599627370499.0).round() is int);
Expect.isTrue((-9007199254740991.0).round() is int);
Expect.isTrue((-9007199254740992.0).round() is int);
}

View file

@ -0,0 +1,49 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.equals(0, 0.0.round());
Expect.equals(0, double.minPositive.round());
Expect.equals(0, (2.0 * double.minPositive).round());
Expect.equals(0, (1.18e-38).round());
Expect.equals(0, (1.18e-38 * 2).round());
Expect.equals(1, 0.5.round());
Expect.equals(1, 0.9999999999999999.round());
Expect.equals(1, 1.0.round());
Expect.equals(1, 1.000000000000001.round());
Expect.equals(9223372036854775807, double.maxFinite.round()); // //# int64: ok
Expect.equals(0, (-double.minPositive).round());
Expect.equals(0, (2.0 * -double.minPositive).round());
Expect.equals(0, (-1.18e-38).round());
Expect.equals(0, (-1.18e-38 * 2).round());
Expect.equals(-1, (-0.5).round());
Expect.equals(-1, (-0.9999999999999999).round());
Expect.equals(-1, (-1.0).round());
Expect.equals(-1, (-1.000000000000001).round());
Expect.equals(-9223372036854775808, (-double.maxFinite).round()); // //# int64: ok
Expect.isTrue(0.0.round() is int);
Expect.isTrue(double.minPositive.round() is int);
Expect.isTrue((2.0 * double.minPositive).round() is int);
Expect.isTrue((1.18e-38).round() is int);
Expect.isTrue((1.18e-38 * 2).round() is int);
Expect.isTrue(0.5.round() is int);
Expect.isTrue(0.9999999999999999.round() is int);
Expect.isTrue(1.0.round() is int);
Expect.isTrue(1.000000000000001.round() is int);
Expect.isTrue(double.maxFinite.round() is int);
Expect.isTrue((-double.minPositive).round() is int);
Expect.isTrue((2.0 * -double.minPositive).round() is int);
Expect.isTrue((-1.18e-38).round() is int);
Expect.isTrue((-1.18e-38 * 2).round() is int);
Expect.isTrue((-0.5).round() is int);
Expect.isTrue((-0.9999999999999999).round() is int);
Expect.isTrue((-1.0).round() is int);
Expect.isTrue((-1.000000000000001).round() is int);
Expect.isTrue((-double.maxFinite).round() is int);
}

View file

@ -0,0 +1,13 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.equals(0.0, 0.49999999999999994.roundToDouble());
Expect.equals(0.0, (-0.49999999999999994).roundToDouble());
Expect.isTrue(0.49999999999999994.roundToDouble() is double);
Expect.isTrue((-0.49999999999999994).roundToDouble().isNegative);
Expect.isTrue((-0.49999999999999994).roundToDouble() is double);
}

View file

@ -0,0 +1,34 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
// The following numbers are on the border of 52 bits.
// For example: 4503599627370499 + 0.5 => 4503599627370500.
Expect.equals(4503599627370496.0, 4503599627370496.0.roundToDouble());
Expect.equals(4503599627370497.0, 4503599627370497.0.roundToDouble());
Expect.equals(4503599627370498.0, 4503599627370498.0.roundToDouble());
Expect.equals(4503599627370499.0, 4503599627370499.0.roundToDouble());
Expect.equals(9007199254740991.0, 9007199254740991.0.roundToDouble());
Expect.equals(9007199254740992.0, 9007199254740992.0.roundToDouble());
Expect.equals(-4503599627370496.0, (-4503599627370496.0).roundToDouble());
Expect.equals(-4503599627370497.0, (-4503599627370497.0).roundToDouble());
Expect.equals(-4503599627370498.0, (-4503599627370498.0).roundToDouble());
Expect.equals(-4503599627370499.0, (-4503599627370499.0).roundToDouble());
Expect.equals(-9007199254740991.0, (-9007199254740991.0).roundToDouble());
Expect.equals(-9007199254740992.0, (-9007199254740992.0).roundToDouble());
Expect.isTrue(4503599627370496.0.roundToDouble() is double);
Expect.isTrue(4503599627370497.0.roundToDouble() is double);
Expect.isTrue(4503599627370498.0.roundToDouble() is double);
Expect.isTrue(4503599627370499.0.roundToDouble() is double);
Expect.isTrue(9007199254740991.0.roundToDouble() is double);
Expect.isTrue(9007199254740992.0.roundToDouble() is double);
Expect.isTrue((-4503599627370496.0).roundToDouble() is double);
Expect.isTrue((-4503599627370497.0).roundToDouble() is double);
Expect.isTrue((-4503599627370498.0).roundToDouble() is double);
Expect.isTrue((-4503599627370499.0).roundToDouble() is double);
Expect.isTrue((-9007199254740991.0).roundToDouble() is double);
Expect.isTrue((-9007199254740992.0).roundToDouble() is double);
}

View file

@ -0,0 +1,62 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.equals(0.0, 0.0.roundToDouble());
Expect.equals(0.0, double.minPositive.roundToDouble());
Expect.equals(0.0, (2.0 * double.minPositive).roundToDouble());
Expect.equals(0.0, (1.18e-38).roundToDouble());
Expect.equals(0.0, (1.18e-38 * 2).roundToDouble());
Expect.equals(1.0, 0.5.roundToDouble());
Expect.equals(1.0, 0.9999999999999999.roundToDouble());
Expect.equals(1.0, 1.0.roundToDouble());
Expect.equals(1.0, 1.000000000000001.roundToDouble());
Expect.equals(2.0, 1.5.roundToDouble());
Expect.equals(double.maxFinite, double.maxFinite.roundToDouble());
Expect.equals(0.0, (-double.minPositive).roundToDouble());
Expect.equals(0.0, (2.0 * -double.minPositive).roundToDouble());
Expect.equals(0.0, (-1.18e-38).roundToDouble());
Expect.equals(0.0, (-1.18e-38 * 2).roundToDouble());
Expect.equals(-1.0, (-0.5).roundToDouble());
Expect.equals(-1.0, (-0.9999999999999999).roundToDouble());
Expect.equals(-1.0, (-1.0).roundToDouble());
Expect.equals(-1.0, (-1.000000000000001).roundToDouble());
Expect.equals(-2.0, (-1.5).roundToDouble());
Expect.equals(-double.maxFinite, (-double.maxFinite).roundToDouble());
Expect.equals(double.infinity, double.infinity.roundToDouble());
Expect.equals(
double.negativeInfinity, double.negativeInfinity.roundToDouble());
Expect.isTrue(double.nan.roundToDouble().isNaN);
Expect.isTrue(0.0.roundToDouble() is double);
Expect.isTrue(double.minPositive.roundToDouble() is double);
Expect.isTrue((2.0 * double.minPositive).roundToDouble() is double);
Expect.isTrue((1.18e-38).roundToDouble() is double);
Expect.isTrue((1.18e-38 * 2).roundToDouble() is double);
Expect.isTrue(0.5.roundToDouble() is double);
Expect.isTrue(0.9999999999999999.roundToDouble() is double);
Expect.isTrue(1.0.roundToDouble() is double);
Expect.isTrue(1.000000000000001.roundToDouble() is double);
Expect.isTrue(double.maxFinite.roundToDouble() is double);
Expect.isTrue((-double.minPositive).roundToDouble().isNegative);
Expect.isTrue((2.0 * -double.minPositive).roundToDouble().isNegative);
Expect.isTrue((-1.18e-38).roundToDouble().isNegative);
Expect.isTrue((-1.18e-38 * 2).roundToDouble().isNegative);
Expect.isTrue((-double.minPositive).roundToDouble() is double);
Expect.isTrue((2.0 * -double.minPositive).roundToDouble() is double);
Expect.isTrue((-1.18e-38).roundToDouble() is double);
Expect.isTrue((-1.18e-38 * 2).roundToDouble() is double);
Expect.isTrue((-0.5).roundToDouble() is double);
Expect.isTrue((-0.9999999999999999).roundToDouble() is double);
Expect.isTrue((-1.0).roundToDouble() is double);
Expect.isTrue((-1.000000000000001).roundToDouble() is double);
Expect.isTrue((-double.maxFinite).roundToDouble() is double);
}

View file

@ -0,0 +1,11 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.throwsUnsupportedError(() => double.infinity.truncate());
Expect.throwsUnsupportedError(() => double.negativeInfinity.truncate());
Expect.throwsUnsupportedError(() => double.nan.truncate());
}

View file

@ -0,0 +1,88 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.equals(0, 0.0.truncate());
Expect.equals(0, double.minPositive.truncate());
Expect.equals(0, (2.0 * double.minPositive).truncate());
Expect.equals(0, (1.18e-38).truncate());
Expect.equals(0, (1.18e-38 * 2).truncate());
Expect.equals(0, 0.49999999999999994.truncate());
Expect.equals(0, 0.5.truncate());
Expect.equals(0, 0.9999999999999999.truncate());
Expect.equals(1, 1.0.truncate());
Expect.equals(1, 1.000000000000001.truncate());
// The following numbers are on the border of 52 bits.
// For example: 4503599627370499 + 0.5 => 4503599627370500.
Expect.equals(4503599627370496, 4503599627370496.0.truncate());
Expect.equals(4503599627370497, 4503599627370497.0.truncate());
Expect.equals(4503599627370498, 4503599627370498.0.truncate());
Expect.equals(4503599627370499, 4503599627370499.0.truncate());
Expect.equals(9007199254740991, 9007199254740991.0.truncate());
Expect.equals(9007199254740992, 9007199254740992.0.truncate());
Expect.equals(9223372036854775807, double.maxFinite.truncate()); // //# int64: ok
Expect.equals(0, (-double.minPositive).truncate());
Expect.equals(0, (2.0 * -double.minPositive).truncate());
Expect.equals(0, (-1.18e-38).truncate());
Expect.equals(0, (-1.18e-38 * 2).truncate());
Expect.equals(0, (-0.49999999999999994).truncate());
Expect.equals(0, (-0.5).truncate());
Expect.equals(0, (-0.9999999999999999).truncate());
Expect.equals(-1, (-1.0).truncate());
Expect.equals(-1, (-1.000000000000001).truncate());
Expect.equals(-4503599627370496, (-4503599627370496.0).truncate());
Expect.equals(-4503599627370497, (-4503599627370497.0).truncate());
Expect.equals(-4503599627370498, (-4503599627370498.0).truncate());
Expect.equals(-4503599627370499, (-4503599627370499.0).truncate());
Expect.equals(-9007199254740991, (-9007199254740991.0).truncate());
Expect.equals(-9007199254740992, (-9007199254740992.0).truncate());
Expect.equals(-9223372036854775808, (-double.maxFinite).truncate()); // //# int64: ok
Expect.isTrue(0.0.truncate() is int);
Expect.isTrue(double.minPositive.truncate() is int);
Expect.isTrue((2.0 * double.minPositive).truncate() is int);
Expect.isTrue((1.18e-38).truncate() is int);
Expect.isTrue((1.18e-38 * 2).truncate() is int);
Expect.isTrue(0.49999999999999994.truncate() is int);
Expect.isTrue(0.5.truncate() is int);
Expect.isTrue(0.9999999999999999.truncate() is int);
Expect.isTrue(1.0.truncate() is int);
Expect.isTrue(1.000000000000001.truncate() is int);
Expect.isTrue(4503599627370496.0.truncate() is int);
Expect.isTrue(4503599627370497.0.truncate() is int);
Expect.isTrue(4503599627370498.0.truncate() is int);
Expect.isTrue(4503599627370499.0.truncate() is int);
Expect.isTrue(9007199254740991.0.truncate() is int);
Expect.isTrue(9007199254740992.0.truncate() is int);
Expect.isTrue(double.maxFinite.truncate() is int);
Expect.isTrue((-double.minPositive).truncateToDouble().isNegative);
Expect.isTrue((2.0 * -double.minPositive).truncateToDouble().isNegative);
Expect.isTrue((-1.18e-38).truncateToDouble().isNegative);
Expect.isTrue((-1.18e-38 * 2).truncateToDouble().isNegative);
Expect.isTrue((-0.49999999999999994).truncateToDouble().isNegative);
Expect.isTrue((-0.5).truncateToDouble().isNegative);
Expect.isTrue((-0.9999999999999999).truncateToDouble().isNegative);
Expect.isTrue((-double.minPositive).truncate() is int);
Expect.isTrue((2.0 * -double.minPositive).truncate() is int);
Expect.isTrue((-1.18e-38).truncate() is int);
Expect.isTrue((-1.18e-38 * 2).truncate() is int);
Expect.isTrue((-0.49999999999999994).truncate() is int);
Expect.isTrue((-0.5).truncate() is int);
Expect.isTrue((-0.9999999999999999).truncate() is int);
Expect.isTrue((-1.0).truncate() is int);
Expect.isTrue((-1.000000000000001).truncate() is int);
Expect.isTrue((-4503599627370496.0).truncate() is int);
Expect.isTrue((-4503599627370497.0).truncate() is int);
Expect.isTrue((-4503599627370498.0).truncate() is int);
Expect.isTrue((-4503599627370499.0).truncate() is int);
Expect.isTrue((-9007199254740991.0).truncate() is int);
Expect.isTrue((-9007199254740992.0).truncate() is int);
Expect.isTrue((-double.maxFinite).truncate() is int);
}

View file

@ -0,0 +1,85 @@
// Copyright (c) 2013, 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 'package:expect/expect.dart';
main() {
Expect.equals(0.0, 0.0.truncateToDouble());
Expect.equals(0.0, double.minPositive.truncateToDouble());
Expect.equals(0.0, (2.0 * double.minPositive).truncateToDouble());
Expect.equals(0.0, (1.18e-38).truncateToDouble());
Expect.equals(0.0, (1.18e-38 * 2).truncateToDouble());
Expect.equals(0.0, 0.49999999999999994.truncateToDouble());
Expect.equals(0.0, 0.5.truncateToDouble());
Expect.equals(0.0, 0.9999999999999999.truncateToDouble());
Expect.equals(1.0, 1.0.truncateToDouble());
Expect.equals(1.0, 1.000000000000001.truncateToDouble());
// The following numbers are on the border of 52 bits.
// For example: 4503599627370499 + 0.5 => 4503599627370500.
Expect.equals(4503599627370496.0, 4503599627370496.0.truncateToDouble());
Expect.equals(4503599627370497.0, 4503599627370497.0.truncateToDouble());
Expect.equals(4503599627370498.0, 4503599627370498.0.truncateToDouble());
Expect.equals(4503599627370499.0, 4503599627370499.0.truncateToDouble());
Expect.equals(9007199254740991.0, 9007199254740991.0.truncateToDouble());
Expect.equals(9007199254740992.0, 9007199254740992.0.truncateToDouble());
Expect.equals(double.maxFinite, double.maxFinite.truncateToDouble());
Expect.equals(0.0, (-double.minPositive).truncateToDouble());
Expect.equals(0.0, (2.0 * -double.minPositive).truncateToDouble());
Expect.equals(0.0, (-1.18e-38).truncateToDouble());
Expect.equals(0.0, (-1.18e-38 * 2).truncateToDouble());
Expect.equals(0.0, (-0.49999999999999994).truncateToDouble());
Expect.equals(0.0, (-0.5).truncateToDouble());
Expect.equals(0.0, (-0.9999999999999999).truncateToDouble());
Expect.equals(-1.0, (-1.0).truncateToDouble());
Expect.equals(-1.0, (-1.000000000000001).truncateToDouble());
Expect.equals(-4503599627370496.0, (-4503599627370496.0).truncateToDouble());
Expect.equals(-4503599627370497.0, (-4503599627370497.0).truncateToDouble());
Expect.equals(-4503599627370498.0, (-4503599627370498.0).truncateToDouble());
Expect.equals(-4503599627370499.0, (-4503599627370499.0).truncateToDouble());
Expect.equals(-9007199254740991.0, (-9007199254740991.0).truncateToDouble());
Expect.equals(-9007199254740992.0, (-9007199254740992.0).truncateToDouble());
Expect.equals(-double.maxFinite, (-double.maxFinite).truncateToDouble());
Expect.equals(double.infinity, double.infinity.truncateToDouble());
Expect.equals(
double.negativeInfinity, double.negativeInfinity.truncateToDouble());
Expect.isTrue(double.nan.truncateToDouble().isNaN);
Expect.isTrue(0.0.truncateToDouble() is double);
Expect.isTrue(double.minPositive.truncateToDouble() is double);
Expect.isTrue((2.0 * double.minPositive).truncateToDouble() is double);
Expect.isTrue((1.18e-38).truncateToDouble() is double);
Expect.isTrue((1.18e-38 * 2).truncateToDouble() is double);
Expect.isTrue(0.49999999999999994.truncateToDouble() is double);
Expect.isTrue(0.5.truncateToDouble() is double);
Expect.isTrue(0.9999999999999999.truncateToDouble() is double);
Expect.isTrue(1.0.truncateToDouble() is double);
Expect.isTrue(1.000000000000001.truncateToDouble() is double);
Expect.isTrue(4503599627370496.0.truncateToDouble() is double);
Expect.isTrue(4503599627370497.0.truncateToDouble() is double);
Expect.isTrue(4503599627370498.0.truncateToDouble() is double);
Expect.isTrue(4503599627370499.0.truncateToDouble() is double);
Expect.isTrue(9007199254740991.0.truncateToDouble() is double);
Expect.isTrue(9007199254740992.0.truncateToDouble() is double);
Expect.isTrue(double.maxFinite.truncateToDouble() is double);
Expect.isTrue((-double.minPositive).truncateToDouble() is double);
Expect.isTrue((2.0 * -double.minPositive).truncateToDouble() is double);
Expect.isTrue((-1.18e-38).truncateToDouble() is double);
Expect.isTrue((-1.18e-38 * 2).truncateToDouble() is double);
Expect.isTrue((-0.49999999999999994).truncateToDouble() is double);
Expect.isTrue((-0.5).truncateToDouble() is double);
Expect.isTrue((-0.9999999999999999).truncateToDouble() is double);
Expect.isTrue((-1.0).truncateToDouble() is double);
Expect.isTrue((-1.000000000000001).truncateToDouble() is double);
Expect.isTrue((-4503599627370496.0).truncateToDouble() is double);
Expect.isTrue((-4503599627370497.0).truncateToDouble() is double);
Expect.isTrue((-4503599627370498.0).truncateToDouble() is double);
Expect.isTrue((-4503599627370499.0).truncateToDouble() is double);
Expect.isTrue((-9007199254740991.0).truncateToDouble() is double);
Expect.isTrue((-9007199254740992.0).truncateToDouble() is double);
Expect.isTrue((-double.maxFinite).truncateToDouble() is double);
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,21 @@
// Copyright (c) 2013, 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 "package:expect/expect.dart";
import 'dart:math';
main() {
Duration d, d1;
d1 = new Duration(microseconds: pow(2, 53) as int);
d = d1 * 2;
Expect.equals(pow(2, 54), d.inMicroseconds);
d = d1 * 1.5;
Expect.equals(pow(2, 53).toDouble() * 1.5, d.inMicroseconds);
Expect.isTrue(d.inMicroseconds is int);
// Test that we lose precision when multiplying with a double.
d = new Duration(microseconds: (pow(2, 53) as int) + 1) * 1.0;
Expect.equals(0, d.inMicroseconds % 2);
}

View file

@ -0,0 +1,19 @@
// Copyright (c) 2013, 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 "package:expect/expect.dart";
main() {
Duration d, d1;
d1 = new Duration(milliseconds: 1);
d = d1 * 0.005;
Expect.equals(1000 * 0.005, d.inMicroseconds);
d = d1 * 0.0;
Expect.equals(0, d.inMicroseconds);
d = d1 * -0.005;
Expect.equals(1000 * -0.005, d.inMicroseconds);
d = d1 * 0.0015;
Expect.equals((1000 * 0.0015).round(), d.inMicroseconds);
}

View file

@ -0,0 +1,293 @@
// Copyright (c) 2013, 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 "package:expect/expect.dart";
main() {
Duration d;
d = new Duration(days: 1);
Expect.equals(86400000000, d.inMicroseconds);
Expect.equals(86400000, d.inMilliseconds);
Expect.equals(86400, d.inSeconds);
Expect.equals(1440, d.inMinutes);
Expect.equals(24, d.inHours);
Expect.equals(1, d.inDays);
d = const Duration(hours: 1);
Expect.equals(3600000000, d.inMicroseconds);
Expect.equals(3600000, d.inMilliseconds);
Expect.equals(3600, d.inSeconds);
Expect.equals(60, d.inMinutes);
Expect.equals(1, d.inHours);
Expect.equals(0, d.inDays);
d = new Duration(minutes: 1);
Expect.equals(60000000, d.inMicroseconds);
Expect.equals(60000, d.inMilliseconds);
Expect.equals(60, d.inSeconds);
Expect.equals(1, d.inMinutes);
Expect.equals(0, d.inHours);
Expect.equals(0, d.inDays);
d = const Duration(seconds: 1);
Expect.equals(1000000, d.inMicroseconds);
Expect.equals(1000, d.inMilliseconds);
Expect.equals(1, d.inSeconds);
Expect.equals(0, d.inMinutes);
Expect.equals(0, d.inHours);
Expect.equals(0, d.inDays);
d = new Duration(milliseconds: 1);
Expect.equals(1000, d.inMicroseconds);
Expect.equals(1, d.inMilliseconds);
Expect.equals(0, d.inSeconds);
Expect.equals(0, d.inMinutes);
Expect.equals(0, d.inHours);
Expect.equals(0, d.inDays);
d = new Duration(microseconds: 1);
Expect.equals(1, d.inMicroseconds);
Expect.equals(0, d.inMilliseconds);
Expect.equals(0, d.inSeconds);
Expect.equals(0, d.inMinutes);
Expect.equals(0, d.inHours);
Expect.equals(0, d.inDays);
d = const Duration(milliseconds: 1, microseconds: 999);
Expect.equals(1999, d.inMicroseconds);
Expect.equals(1, d.inMilliseconds);
d = const Duration(seconds: 1, milliseconds: 999);
Expect.equals(1999, d.inMilliseconds);
Expect.equals(1, d.inSeconds);
d = new Duration(minutes: 1, seconds: 59);
Expect.equals(119, d.inSeconds);
Expect.equals(1, d.inMinutes);
d = const Duration(hours: 1, minutes: 59);
Expect.equals(119, d.inMinutes);
Expect.equals(1, d.inHours);
d = new Duration(days: 1, hours: 23);
Expect.equals(47, d.inHours);
Expect.equals(1, d.inDays);
d = const Duration(
days: 0,
hours: 23,
minutes: 59,
seconds: 59,
milliseconds: 999,
microseconds: 999);
Expect.equals(0, d.inDays);
d = new Duration(days: -1);
Expect.equals(-86400000000, d.inMicroseconds);
Expect.equals(-86400000, d.inMilliseconds);
Expect.equals(-86400, d.inSeconds);
Expect.equals(-1440, d.inMinutes);
Expect.equals(-24, d.inHours);
Expect.equals(-1, d.inDays);
d = const Duration(hours: -1);
Expect.equals(-3600000000, d.inMicroseconds);
Expect.equals(-3600000, d.inMilliseconds);
Expect.equals(-3600, d.inSeconds);
Expect.equals(-60, d.inMinutes);
Expect.equals(-1, d.inHours);
Expect.equals(0, d.inDays);
d = new Duration(minutes: -1);
Expect.equals(-60000000, d.inMicroseconds);
Expect.equals(-60000, d.inMilliseconds);
Expect.equals(-60, d.inSeconds);
Expect.equals(-1, d.inMinutes);
Expect.equals(0, d.inHours);
Expect.equals(0, d.inDays);
d = const Duration(seconds: -1);
Expect.equals(-1000000, d.inMicroseconds);
Expect.equals(-1000, d.inMilliseconds);
Expect.equals(-1, d.inSeconds);
Expect.equals(0, d.inMinutes);
Expect.equals(0, d.inHours);
Expect.equals(0, d.inDays);
d = new Duration(milliseconds: -1);
Expect.equals(-1000, d.inMicroseconds);
Expect.equals(-1, d.inMilliseconds);
Expect.equals(0, d.inSeconds);
Expect.equals(0, d.inMinutes);
Expect.equals(0, d.inHours);
Expect.equals(0, d.inDays);
d = new Duration(microseconds: -1);
Expect.equals(-1, d.inMicroseconds);
Expect.equals(0, d.inMilliseconds);
Expect.equals(0, d.inSeconds);
Expect.equals(0, d.inMinutes);
Expect.equals(0, d.inHours);
Expect.equals(0, d.inDays);
d = const Duration(days: 1, hours: -24);
Expect.equals(0, d.inMicroseconds);
d = new Duration(hours: 1, minutes: -60);
Expect.equals(0, d.inMicroseconds);
d = const Duration(minutes: 1, seconds: -60);
Expect.equals(0, d.inMicroseconds);
d = new Duration(seconds: 1, milliseconds: -1000);
Expect.equals(0, d.inMicroseconds);
d = new Duration(milliseconds: 1, microseconds: -1000);
Expect.equals(0, d.inMicroseconds);
d = const Duration(hours: 25);
Expect.equals(1, d.inDays);
Expect.equals(25, d.inHours);
Expect.equals(1500, d.inMinutes);
Expect.equals(90000, d.inSeconds);
Expect.equals(90000000, d.inMilliseconds);
Expect.equals(90000000000, d.inMicroseconds);
d = new Duration(minutes: 61);
Expect.equals(0, d.inDays);
Expect.equals(1, d.inHours);
Expect.equals(61, d.inMinutes);
Expect.equals(3660, d.inSeconds);
Expect.equals(3660000, d.inMilliseconds);
Expect.equals(3660000000, d.inMicroseconds);
d = const Duration(seconds: 61);
Expect.equals(0, d.inDays);
Expect.equals(0, d.inHours);
Expect.equals(1, d.inMinutes);
Expect.equals(61, d.inSeconds);
Expect.equals(61000, d.inMilliseconds);
Expect.equals(61000000, d.inMicroseconds);
d = new Duration(milliseconds: 1001);
Expect.equals(0, d.inDays);
Expect.equals(0, d.inHours);
Expect.equals(0, d.inMinutes);
Expect.equals(1, d.inSeconds);
Expect.equals(1001, d.inMilliseconds);
Expect.equals(1001000, d.inMicroseconds);
d = new Duration(microseconds: 1001);
Expect.equals(0, d.inDays);
Expect.equals(0, d.inHours);
Expect.equals(0, d.inMinutes);
Expect.equals(0, d.inSeconds);
Expect.equals(1, d.inMilliseconds);
Expect.equals(1001, d.inMicroseconds);
var d1 = const Duration(milliseconds: 1000);
var d2 = const Duration(seconds: 1);
Expect.identical(d1, d2);
d1 = const Duration(microseconds: 1000);
d2 = const Duration(milliseconds: 1);
Expect.identical(d1, d2);
d1 = new Duration(hours: 1);
d2 = new Duration(hours: -1);
d = d1 + d2;
Expect.equals(0, d.inMicroseconds);
d = d1 - d2;
Expect.equals(3600000000 * 2, d.inMicroseconds);
d2 = new Duration(hours: 1);
d = d1 + d2;
Expect.equals(3600000000 * 2, d.inMicroseconds);
d = d1 - d2;
Expect.equals(0, d.inMicroseconds);
d = d1 * 2;
Expect.equals(3600000000 * 2, d.inMicroseconds);
d = d1 * -1;
Expect.equals(-3600000000, d.inMicroseconds);
d = d1 * 0;
Expect.equals(0, d.inMicroseconds);
d = d1 ~/ 2;
Expect.equals(1800000000, d.inMicroseconds);
d = d1 ~/ 3600000001;
Expect.equals(0, d.inMicroseconds);
d = d1 ~/ -3600000001;
Expect.equals(0, d.inMicroseconds);
d = d1 ~/ 3599999999;
Expect.equals(1, d.inMicroseconds);
d = d1 ~/ -3599999999;
Expect.equals(-1, d.inMicroseconds);
d = d1 ~/ -1;
Expect.equals(-3600000000, d.inMicroseconds);
d = d1 * 0;
Expect.equals(0, d.inMicroseconds);
Expect.throws(() => d1 ~/ 0, (e) => e is IntegerDivisionByZeroException);
d = new Duration(microseconds: 0);
Expect.isTrue(d < new Duration(microseconds: 1));
Expect.isTrue(d <= new Duration(microseconds: 1));
Expect.isTrue(d <= d);
Expect.isTrue(d > new Duration(microseconds: -1));
Expect.isTrue(d >= new Duration(microseconds: -1));
Expect.isTrue(d >= d);
d = const Duration(
days: 1,
hours: 3,
minutes: 17,
seconds: 42,
milliseconds: 823,
microseconds: 127);
Expect.equals("27:17:42.823127", d.toString());
d = const Duration(hours: 1999, minutes: 17, seconds: 42);
Expect.equals("1999:17:42.000000", d.toString());
d = const Duration(
days: -1,
hours: -3,
minutes: -17,
seconds: -42,
milliseconds: -823,
microseconds: -127);
Expect.equals("-27:17:42.823127", d.toString());
d = const Duration(hours: -1999, minutes: -17, seconds: -42);
Expect.equals("-1999:17:42.000000", d.toString());
// Edge conditions for toString of microseconds.
// Regression test for http://dartbug.com/15678
d = const Duration(microseconds: 1);
Expect.equals("0:00:00.000001", d.toString());
d = const Duration(microseconds: 9);
Expect.equals("0:00:00.000009", d.toString());
d = const Duration(microseconds: 10);
Expect.equals("0:00:00.000010", d.toString());
d = const Duration(microseconds: 99);
Expect.equals("0:00:00.000099", d.toString());
d = const Duration(microseconds: 100);
Expect.equals("0:00:00.000100", d.toString());
d = const Duration(microseconds: 999);
Expect.equals("0:00:00.000999", d.toString());
d = const Duration(microseconds: 1000);
Expect.equals("0:00:00.001000", d.toString());
d = const Duration(microseconds: 9999);
Expect.equals("0:00:00.009999", d.toString());
d = const Duration(microseconds: 10000);
Expect.equals("0:00:00.010000", d.toString());
d = const Duration(microseconds: 99999);
Expect.equals("0:00:00.099999", d.toString());
d = const Duration(microseconds: 100000);
Expect.equals("0:00:00.100000", d.toString());
d = const Duration(microseconds: 999999);
Expect.equals("0:00:00.999999", d.toString());
d = const Duration(microseconds: 1000000);
Expect.equals("0:00:01.000000", d.toString());
d1 = const Duration(hours: 1);
d2 = const Duration(hours: -1);
Expect.isFalse(d1.isNegative);
Expect.isTrue(d2.isNegative);
Expect.equals(d1, d1.abs());
Expect.equals(d1, d2.abs());
Expect.equals(d2, -d1);
Expect.equals(d1, -d2);
}

View file

@ -0,0 +1,86 @@
// Copyright (c) 2017, 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 "package:expect/expect.dart";
// Test that noSuchMethod calls behave as expected for dynamic object invocations.
class BaseClass {
final dynamic finalField = "final!";
baz() => "baz!!";
get bla => (() => "bla!!");
}
class ReturnInvocationName extends BaseClass {
var _bar;
ReturnInvocationName(this._bar);
noSuchMethod(Invocation invocation) {
var name = invocation.memberName.toString();
var match = new RegExp(r'Symbol\("([^"]+)"\)').matchAsPrefix(name);
return match != null ? match.group(1) : name;
}
bar() {
return _bar;
}
}
class Foo {}
main() {
dynamic x = new ReturnInvocationName(42);
Expect.equals('final!', x.finalField);
Expect.equals('foo', x.finalField = "foo", 'should call noSuchMethod');
Expect.equals('final!', x.finalField, 'field was not set');
Expect.equals('_prototype', x._prototype);
Expect.equals('_prototype', x._prototype());
Expect.equals('prototype', x.prototype);
Expect.equals('prototype', x.prototype());
Expect.equals('constructor', x.constructor);
Expect.equals('constructor', x.constructor());
Expect.equals('__proto__', x.__proto__);
Expect.equals('__proto__', x.__proto__);
Expect.equals(42, x.bar());
Expect.equals(42, (x.bar)());
Expect.equals('unary-', -x);
Expect.equals('+', x + 42);
Expect.equals('[]', x[4]);
dynamic b = new BaseClass();
Expect.equals('final!', b.finalField);
Expect.throwsNoSuchMethodError(() => b.finalField = "foo");
Expect.equals('final!', b.finalField, 'field was not set');
// Verify that noSuchMethod errors are triggered even when the JS object
// happens to have a matching member name.
dynamic f = new Foo();
Expect.throwsNoSuchMethodError(() => f.prototype);
Expect.throwsNoSuchMethodError(() => f.prototype());
Expect.throwsNoSuchMethodError(() => f.prototype = 42);
Expect.throwsNoSuchMethodError(() => f.constructor);
Expect.throwsNoSuchMethodError(() => f.constructor());
Expect.throwsNoSuchMethodError(() => f.constructor = 42);
Expect.throwsNoSuchMethodError(() => f.__proto__);
// These are valid JS properties but not Dart methods.
Expect.throwsNoSuchMethodError(() => f.toLocaleString);
Expect.throwsNoSuchMethodError(() => f.hasOwnProperty);
f = (int x) {};
// Calls with the wrong number of arguments should be NoSuchMethodErrors.
Expect.throwsNoSuchMethodError(() => f());
Expect.throwsNoSuchMethodError(() => f('hi', '!'));
Expect.throwsNoSuchMethodError(() => f(x: 42));
}