Migrated test block 15 to Dart 2.0.

list_unmodifiable_test required some modifications so callbacks matched
the correct types. list_unmodifiable_test and list_test also needed to
be disabled on dartdevc due to some issue with List<int> not mapping to
JSArray.

BUG=
R=rnystrom@google.com

Review-Url: https://codereview.chromium.org/2992833002 .
This commit is contained in:
Ben Konyi 2017-08-03 11:12:22 -07:00
parent 1c5101e4a6
commit ce197702e4
11 changed files with 32 additions and 1022 deletions

View file

@ -12,7 +12,6 @@ string_case_test/01: Fail # Bug 18061
int_parse_radix_test/01: Pass, Fail # JS implementations disagree on U+0085 being whitespace.
int_parse_radix_test/02: Fail # No bigints.
integer_to_radix_string_test: RuntimeError # issue 22045
list_unmodifiable_test: Pass, RuntimeError # Issue 28712
[ $runtime == safari || $runtime == safarimobilesim ]
double_round3_test: Fail, OK # Runtime rounds 0.49999999999999994 to 1.
@ -41,7 +40,6 @@ regress_r21715_test: RuntimeError # Requires bigint support.
list_as_map_test: Pass, Slow # TODO(kasperl): Please triage.
[ $compiler == dart2js && $runtime == safarimobilesim ]
list_test/01: Fail # Safari bug: Array(-2) seen as dead code.
[ $compiler == dart2analyzer ]
int_parse_radix_bad_handler_test: fail
@ -191,11 +189,6 @@ list_set_all_test: Crash
list_set_range_test: Crash
list_sort_test: RuntimeError
list_sublist_test: Crash
list_test/01: Crash
list_test/none: Crash
list_to_string2_test: Crash
list_to_string_test: Crash
list_unmodifiable_test: Crash
regexp/parentheses_test: Crash
regress_r21715_test: RuntimeError
set_containsAll_test: Crash

View file

@ -79,6 +79,9 @@ compare_to2_test: RuntimeError # Issue 30170
date_time10_test: RuntimeError # Issue 29921
growable_list_test: RuntimeError # Issue 29921
hash_set_test/01: RuntimeError # Issue 29921
list_unmodifiable_test: RuntimeError
list_test/none: RuntimeError
list_test/01: RuntimeError
nan_infinity_test/01: RuntimeError # Issue 29921
null_nosuchmethod_test: RuntimeError # Issue 29921
main_test: RuntimeError # Issue 29921
@ -345,6 +348,7 @@ string_trimlr_test/02: RuntimeError # Uses Unicode 6.2.0 or earlier.
[ $compiler == dart2js && $runtime == safarimobilesim ]
string_trimlr_test/01: Fail
string_trimlr_test/02: RuntimeError # Uses Unicode 6.2.0 or earlier.
list_test/01: Fail # Safari bug: Array(-2) seen as dead code.
[ $compiler == dart2js && $runtime == d8 ]
string_trimlr_test/02: RuntimeError, Pass # Uses Unicode 6.2.0 or earlier, issue 30279.
@ -606,6 +610,11 @@ map_from_iterables_test: Crash
map_from_test: Crash
map_keys2_test: Crash
map_keys_test: Crash
list_test/01: Crash
list_test/none: Crash
list_to_string2_test: Crash
list_to_string_test: Crash
list_unmodifiable_test: Crash
[ $compiler == dart2js && $dart2js_with_kernel && $host_checked ]
stopwatch_test: Crash
@ -792,3 +801,9 @@ regexp/global_test: Skip # Issue 21709
[ $mode == debug ]
regexp/pcre_test: Pass, Slow # Issue 22008
[ $compiler == dart2js && ! $browser ]
package_resource_test: RuntimeError # Issue 26842
[ $compiler == dart2js && ! $dart2js_with_kernel ]
list_unmodifiable_test: Pass, RuntimeError # Issue 28712

View file

@ -214,90 +214,90 @@ class Test<E> {
}
}
createConstList(n) {
List<int> createConstList(int n) {
if (n == 0) return const <int>[];
return const <int>[1, 2, 3];
}
createFixedList(n) {
List<int> createFixedList(int n) {
var result = new List<int>(n);
for (int i = 0; i < n; i++) result[i] = n;
return result;
}
createGrowableList(n) {
List<int> createGrowableList(int n) {
var result = new List<int>()..length = n;
for (int i = 0; i < n; i++) result[i] = n;
return result;
}
createIterable(n) => new Iterable.generate(n);
createConstMapKeys(n) {
Iterable createIterable(int n) => new Iterable.generate(n);
Iterable createConstMapKeys(int n) {
if (n == 0) return const <int, int>{}.keys;
return const <int, int>{0: 0, 1: 1, 2: 2}.keys;
}
createConstMapValues(n) {
Iterable createConstMapValues(int n) {
if (n == 0) return const <int, int>{}.values;
return const <int, int>{0: 0, 1: 1, 2: 2}.values;
}
createMapKeys(n) {
Iterable createMapKeys(int n) {
var map = <int, int>{};
for (int i = 0; i < n; i++) map[i] = i;
return map.keys;
}
createMapValues(n) {
Iterable createMapValues(int n) {
var map = <int, int>{};
for (int i = 0; i < n; i++) map[i] = i;
return map.values;
}
createSplayMapKeys(n) {
Iterable createSplayMapKeys(int n) {
var map = new SplayTreeMap<int, int>();
for (int i = 0; i < n; i++) map[i] = i;
return map.keys;
}
createSplayMapValues(n) {
Iterable createSplayMapValues(int n) {
var map = new SplayTreeMap<int, int>();
for (int i = 0; i < n; i++) map[i] = i;
return map.values;
}
createSet(n) {
Set<int> createSet(int n) {
var set = new Set<int>();
for (int i = 0; i < n; i++) set.add(i);
return set;
}
createSplaySet(n) {
SplayTreeSet<int> createSplaySet(int n) {
var set = new SplayTreeSet<int>();
for (int i = 0; i < n; i++) set.add(i);
return set;
}
createQueue(n) {
Queue<int> createQueue(int n) {
var queue = new Queue<int>();
for (int i = 0; i < n; i++) queue.add(i);
return queue;
}
createListMapKeys(n) {
Iterable createListMapKeys(int n) {
return createGrowableList(n).asMap().keys;
}
createListMapValues(n) {
Iterable createListMapValues(int n) {
return createGrowableList(n).asMap().values;
}
createCodeUnits(n) {
Iterable createCodeUnits(int n) {
var string = new String.fromCharCodes(new Iterable.generate(n));
return string.codeUnits;
}
createTypedList(n) {
Uint8List createTypedList(int n) {
var tl = new Uint8List(n);
for (int i = 0; i < n; i++) tl[i] = i;
return tl;

View file

@ -22,8 +22,6 @@ linked_hash_map_from_iterable_test: Skip
list_contains_argument_order_test: Skip
list_filled_type_argument_test: Skip
list_index_of2_test: Skip
list_test: Skip
list_unmodifiable_test: Skip
regexp/regress-regexp-codeflush_test: Skip
regexp/standalones_test: Skip
string_replace_test: Skip

View file

@ -1,633 +0,0 @@
// 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";
void main() {
// Typed lists - fixed length and can only contain integers.
testTypedList(new Uint8List(4));
testTypedList(new Int8List(4));
testTypedList(new Uint16List(4));
testTypedList(new Int16List(4));
testTypedList(new Uint32List(4));
testTypedList(new Int32List(4));
testTypedList(new Uint8List(4).toList(growable: false));
testTypedList(new Int8List(4).toList(growable: false));
testTypedList(new Uint16List(4).toList(growable: false));
testTypedList(new Int16List(4).toList(growable: false));
testTypedList(new Uint32List(4).toList(growable: false));
testTypedList(new Int32List(4).toList(growable: false));
// Fixed length lists, length 4.
testFixedLengthList(new List(4));
testFixedLengthList(new List(4).toList(growable: false));
testFixedLengthList((new List()..length = 4).toList(growable: false));
// ListBase implementation of List.
testFixedLengthList(new MyFixedList(new List(4)));
testFixedLengthList(new MyFixedList(new List(4)).toList(growable: false));
// Growable lists. Initial length 0.
testGrowableList(new List());
testGrowableList(new List().toList());
testGrowableList(new List(0).toList());
testGrowableList(new List.filled(0, null, growable: true));
testGrowableList([]);
testGrowableList((const []).toList());
testGrowableList(new MyList([]));
testGrowableList(new MyList([]).toList());
testTypedGrowableList(new Uint8List(0).toList());
testTypedGrowableList(new Int8List(0).toList());
testTypedGrowableList(new Uint16List(0).toList());
testTypedGrowableList(new Int16List(0).toList());
testTypedGrowableList(new Uint32List(0).toList());
testTypedGrowableList(new Int32List(0).toList());
testListConstructor();
testErrors();
}
void testErrors() {
// Regression for issue http://dartbug.com/24295
testIndexError(list, index, name) {
try {
list[list.length];
} catch (err, s) {
Expect.isTrue(err is RangeError, "$name[$index]");
Expect.equals(list.length, err.invalidValue, "$name[$index] value");
Expect.equals(list.length - 1, err.end, "$name[$index] end");
Expect.equals(0, err.start, "$name[$index] start");
}
}
testIndex(list, name) {
testIndexError(list, list.length, name); // Just too big.
testIndexError(list, -1, name); // Negative.
testIndexError(list, 0x123456789, name); // > 2^32.
testIndexError(list, -0x123456789, name); // < -2^32.
}
// Slices.
testSliceError(list, start, end, name) {
name = "$name[$start:$end]";
var realError;
try {
RangeError.checkValidRange(start, end, list.length);
} catch (e) {
realError = e;
}
var result;
try {
result = list.sublist(start, end);
} catch (actualError) {
Expect.isNotNull(realError, "$name should not fail");
Expect.isTrue(actualError is RangeError, "$name is-error: $actualError");
Expect.equals(realError.name, actualError.name, "$name name");
Expect.equals(realError.invalidValue, actualError.invalidValue,
"$name[0:l+1] value");
Expect.equals(realError.start, actualError.start, "$name[0:l+1] start");
Expect.equals(realError.end, actualError.end, "$name[0:l+1] end");
return;
}
// Didn't throw.
Expect.isNull(realError, "$name should fail");
Expect.equals(end - start, result.length, "$name result length");
}
testSlice(list, name) {
testSliceError(list, 0, list.length, name); // Should not fail.
testSliceError(list, 0, list.length + 1, name);
testSliceError(list, 0, 0x123456789, name);
testSliceError(list, -1, list.length, name);
testSliceError(list, -0x123456789, list.length, name);
testSliceError(list, list.length + 1, list.length + 1, name);
testSliceError(list, -1, null, name);
if (list.length > 0) {
testSliceError(list, list.length, list.length - 1, name);
}
}
testRangeErrors(list, name) {
testIndex(list, "$name#${list.length} index");
testSlice(list, "$name#${list.length} slice");
}
// Empty lists.
testRangeErrors([], "list");
testRangeErrors(new List(0), "fixed-list");
testRangeErrors(const [], "const-list");
testRangeErrors(new List.unmodifiable([]), "unmodifiable");
testRangeErrors(new Uint8List(0), "typed-list");
testRangeErrors(new Uint8List.view(new Uint8List(0).buffer), "typed-list");
testRangeErrors([1, 2, 3].sublist(1, 1), "sub-list");
// Non-empty lists.
testRangeErrors([1, 2, 3], "list");
testRangeErrors(new List(3), "fixed-list");
testRangeErrors(const [1, 2, 3], "const-list");
testRangeErrors(new List.unmodifiable([1, 2, 3]), "unmodifiable");
testRangeErrors(new Uint8List(3), "typed-list");
testRangeErrors(new Uint8List.view(new Uint8List(3).buffer), "typed-list");
testRangeErrors([1, 2, 3, 4, 5].sublist(1, 3), "sub-list");
}
void testLength(int length, List list) {
Expect.equals(length, list.length);
(length == 0 ? Expect.isTrue : Expect.isFalse)(list.isEmpty);
(length != 0 ? Expect.isTrue : Expect.isFalse)(list.isNotEmpty);
}
void testTypedLengthInvariantOperations(List list) {
// length
Expect.equals(list.length, 4);
// operators [], []=.
for (int i = 0; i < 4; i++) list[i] = 0;
list[0] = 4;
Expect.listEquals([4, 0, 0, 0], list);
list[1] = 7;
Expect.listEquals([4, 7, 0, 0], list);
list[3] = 2;
Expect.listEquals([4, 7, 0, 2], list);
for (int i = 0; i < list.length; i++) {
list[i] = i;
}
// indexOf, lastIndexOf
for (int i = 0; i < 4; i++) {
Expect.equals(i, list[i]);
Expect.equals(i, list.indexOf(i));
Expect.equals(i, list.lastIndexOf(i));
}
// setRange.
list.setRange(0, 4, [3, 2, 1, 0]);
Expect.listEquals([3, 2, 1, 0], list);
list.setRange(1, 4, list);
Expect.listEquals([3, 3, 2, 1], list);
list.setRange(0, 3, list, 1);
Expect.listEquals([3, 2, 1, 1], list);
list.setRange(0, 3, list, 1);
Expect.listEquals([2, 1, 1, 1], list);
list.setRange(2, 4, list, 0);
Expect.listEquals([2, 1, 2, 1], list);
// setAll.
list.setAll(0, [3, 2, 0, 1]);
Expect.listEquals([3, 2, 0, 1], list);
list.setAll(1, [0, 1]);
Expect.listEquals([3, 0, 1, 1], list);
// fillRange.
list.fillRange(1, 3, 7);
Expect.listEquals([3, 7, 7, 1], list);
list.fillRange(0, 0, 9);
Expect.listEquals([3, 7, 7, 1], list);
list.fillRange(4, 4, 9);
Expect.listEquals([3, 7, 7, 1], list);
list.fillRange(0, 4, 9);
Expect.listEquals([9, 9, 9, 9], list);
// sort.
list.setRange(0, 4, [3, 2, 1, 0]);
list.sort();
Expect.listEquals([0, 1, 2, 3], list);
list.setRange(0, 4, [1, 2, 3, 0]);
list.sort();
Expect.listEquals([0, 1, 2, 3], list);
list.setRange(0, 4, [1, 3, 0, 2]);
list.sort((a, b) => b - a); // reverse compare.
Expect.listEquals([3, 2, 1, 0], list);
list.setRange(0, 4, [1, 2, 3, 0]);
list.sort((a, b) => b - a);
Expect.listEquals([3, 2, 1, 0], list);
// Some Iterable methods.
list.setRange(0, 4, [0, 1, 2, 3]);
// map.
testMap(val) {
return val * 2 + 10;
}
List mapped = list.map(testMap).toList();
Expect.equals(mapped.length, list.length);
for (var i = 0; i < list.length; i++) {
Expect.equals(mapped[i], list[i] * 2 + 10);
}
matchAll(val) => true;
matchSome(val) {
return (val == 1 || val == 2);
}
matchSomeFirst(val) {
return val == 0;
}
matchSomeLast(val) {
return val == 3;
}
matchNone(val) => false;
// where.
Iterable filtered = list.where(matchSome);
Expect.equals(filtered.length, 2);
// every
Expect.isTrue(list.every(matchAll));
Expect.isFalse(list.every(matchSome));
Expect.isFalse(list.every(matchNone));
// any
Expect.isTrue(list.any(matchAll));
Expect.isTrue(list.any(matchSome));
Expect.isTrue(list.any(matchSomeFirst));
Expect.isTrue(list.any(matchSomeLast));
Expect.isFalse(list.any(matchNone));
// Argument checking isn't implemented for typed arrays in browsers,
// so it's moved to the method below for now.
}
void testLengthInvariantOperations(List list) {
testTypedLengthInvariantOperations(list);
// Tests that need untyped lists.
list.setAll(0, [0, 1, 2, 3]);
Expect.equals(-1, list.indexOf(100));
Expect.equals(-1, list.lastIndexOf(100));
list[2] = new Yes();
Expect.equals(2, list.indexOf(100));
Expect.equals(2, list.lastIndexOf(100));
list[3] = new Yes();
Expect.equals(2, list.indexOf(100));
Expect.equals(3, list.lastIndexOf(100));
list[2] = 2;
Expect.equals(3, list.indexOf(100));
Expect.equals(3, list.lastIndexOf(100));
list[3] = 3;
Expect.equals(-1, list.indexOf(100));
Expect.equals(-1, list.lastIndexOf(100));
// Argument errors on bad indices. List is still [0, 1, 2, 3].
testArgumentError(action()) {
Expect.throws(action, (e) => e is ArgumentError);
}
// Direct indices (0 <= index < length).
testArgumentError(() => list[-1]);
testArgumentError(() => list[4]);
testArgumentError(() => list[-1] = 99);
testArgumentError(() => list[4] = 99);
testArgumentError(() => list.elementAt(-1));
testArgumentError(() => list.elementAt(4));
// Ranges (0 <= start <= end <= length).
testArgumentError(() => list.sublist(-1, 2));
testArgumentError(() => list.sublist(-1, 5));
testArgumentError(() => list.sublist(2, 5));
testArgumentError(() => list.sublist(4, 2));
testArgumentError(() => list.getRange(-1, 2));
testArgumentError(() => list.getRange(-1, 5));
testArgumentError(() => list.getRange(2, 5));
testArgumentError(() => list.getRange(4, 2));
testArgumentError(() => list.setRange(-1, 2, [1, 2, 3]));
testArgumentError(() => list.setRange(-1, 5, [1, 2, 3, 4, 5, 6]));
testArgumentError(() => list.setRange(2, 5, [1, 2, 3]));
testArgumentError(() => list.setRange(4, 2, [1, 2]));
// for setAll, end is implicitly start + values.length.
testArgumentError(() => list.setAll(-1, []));
testArgumentError(() => list.setAll(5, []));
testArgumentError(() => list.setAll(2, [1, 2, 3]));
testArgumentError(() => list.fillRange(-1, 2));
testArgumentError(() => list.fillRange(-1, 5));
testArgumentError(() => list.fillRange(2, 5));
testArgumentError(() => list.fillRange(4, 2));
}
void testTypedList(List list) {
testTypedLengthInvariantOperations(list);
testCannotChangeLength(list);
}
void testFixedLengthList(List list) {
testLengthInvariantOperations(list);
testCannotChangeLength(list);
}
void testCannotChangeLength(List list) {
isUnsupported(action()) {
Expect.throws(action, (e) => e is UnsupportedError);
}
isUnsupported(() => list.add(0));
isUnsupported(() => list.addAll([0]));
isUnsupported(() => list.removeLast());
isUnsupported(() => list.insert(0, 1));
isUnsupported(() => list.insertAll(0, [1]));
isUnsupported(() => list.clear());
isUnsupported(() => list.remove(1));
isUnsupported(() => list.removeAt(1));
isUnsupported(() => list.removeRange(0, 1));
isUnsupported(() => list.replaceRange(0, 1, []));
}
void testTypedGrowableList(List list) {
testLength(0, list);
// set length.
list.length = 4;
testLength(4, list);
testTypedLengthInvariantOperations(list);
testGrowableListOperations(list);
}
void testGrowableList(List list) {
testLength(0, list);
// set length.
list.length = 4;
testLength(4, list);
testLengthInvariantOperations(list);
testGrowableListOperations(list);
}
void testGrowableListOperations(List list) {
// add, removeLast.
list.clear();
testLength(0, list);
list.add(4);
testLength(1, list);
Expect.equals(4, list.removeLast());
testLength(0, list);
for (int i = 0; i < 100; i++) {
list.add(i);
}
Expect.equals(list.length, 100);
for (int i = 0; i < 100; i++) {
Expect.equals(i, list[i]);
}
Expect.equals(17, list.indexOf(17));
Expect.equals(17, list.lastIndexOf(17));
Expect.equals(-1, list.indexOf(999));
Expect.equals(-1, list.lastIndexOf(999));
Expect.equals(99, list.removeLast());
testLength(99, list);
// remove.
Expect.isTrue(list.remove(4));
testLength(98, list);
Expect.isFalse(list.remove(4));
testLength(98, list);
list.clear();
testLength(0, list);
list.add(4);
list.add(4);
testLength(2, list);
Expect.isTrue(list.remove(4));
testLength(1, list);
Expect.isTrue(list.remove(4));
testLength(0, list);
Expect.isFalse(list.remove(4));
testLength(0, list);
// removeWhere, retainWhere
for (int i = 0; i < 100; i++) {
list.add(i);
}
testLength(100, list);
list.removeWhere((int x) => x.isOdd);
testLength(50, list);
for (int i = 0; i < list.length; i++) {
Expect.isTrue(list[i].isEven);
}
list.retainWhere((int x) => (x % 3) == 0);
testLength(17, list);
for (int i = 0; i < list.length; i++) {
Expect.isTrue((list[i] % 6) == 0);
}
// insert, remove, removeAt
list.clear();
testLength(0, list);
list.insert(0, 0);
Expect.listEquals([0], list);
list.insert(0, 1);
Expect.listEquals([1, 0], list);
list.insert(0, 2);
Expect.listEquals([2, 1, 0], list);
Expect.isTrue(list.remove(1));
Expect.listEquals([2, 0], list);
list.insert(1, 1);
Expect.listEquals([2, 1, 0], list);
list.removeAt(1);
Expect.listEquals([2, 0], list);
list.removeAt(1);
Expect.listEquals([2], list);
// insertAll
list.insertAll(0, [1, 2, 3]);
Expect.listEquals([1, 2, 3, 2], list);
list.insertAll(2, []);
Expect.listEquals([1, 2, 3, 2], list);
list.insertAll(4, [7, 9]);
Expect.listEquals([1, 2, 3, 2, 7, 9], list);
// addAll
list.addAll(list.reversed.toList());
Expect.listEquals([1, 2, 3, 2, 7, 9, 9, 7, 2, 3, 2, 1], list);
list.addAll([]);
Expect.listEquals([1, 2, 3, 2, 7, 9, 9, 7, 2, 3, 2, 1], list);
// replaceRange
list.replaceRange(3, 7, [0, 0]);
Expect.listEquals([1, 2, 3, 0, 0, 7, 2, 3, 2, 1], list);
list.replaceRange(2, 3, [5, 5, 5]);
Expect.listEquals([1, 2, 5, 5, 5, 0, 0, 7, 2, 3, 2, 1], list);
list.replaceRange(2, 4, [6, 6]);
Expect.listEquals([1, 2, 6, 6, 5, 0, 0, 7, 2, 3, 2, 1], list);
list.replaceRange(6, 8, []);
Expect.listEquals([1, 2, 6, 6, 5, 0, 2, 3, 2, 1], list);
// Operations that change the length cause ConcurrentModificationError.
void testConcurrentModification(action()) {
testIterator(int when) {
list.length = 4;
list.setAll(0, [0, 1, 2, 3]);
Expect.throws(() {
for (var element in list) {
if (element == when) action();
}
}, (e) => e is ConcurrentModificationError);
}
testForEach(int when) {
list.length = 4;
list.setAll(0, [0, 1, 2, 3]);
Expect.throws(() {
list.forEach((var element) {
if (element == when) action();
});
}, (e) => e is ConcurrentModificationError);
}
// Test the change at different points of the iteration.
testIterator(0);
testIterator(1);
testIterator(3);
testForEach(0);
testForEach(1);
testForEach(3);
}
testConcurrentModification(() => list.add(5));
testConcurrentModification(() => list.addAll([5, 6]));
testConcurrentModification(() => list.removeLast());
for (int i = 0; i < 4; i++) {
testConcurrentModification(() => list.remove(i));
testConcurrentModification(() => list.removeAt(i));
testConcurrentModification(() => list.removeWhere((x) => x == i));
testConcurrentModification(() => list.retainWhere((x) => x != i));
testConcurrentModification(() => list.insert(i, 5));
testConcurrentModification(() => list.insertAll(i, [5, 6]));
testConcurrentModification(() => list.removeRange(i, i + 1));
testConcurrentModification(() => list.replaceRange(i, i + 1, [5, 6]));
}
// Any operation that doesn't change the length should be safe for iteration.
testSafeConcurrentModification(action()) {
list.length = 4;
list.setAll(0, [0, 1, 2, 3]);
for (var i in list) {
action();
}
list.forEach((e) => action());
}
testSafeConcurrentModification(() {
list.add(5);
list.removeLast();
});
testSafeConcurrentModification(() {
list.add(list[0]);
list.removeAt(0);
});
testSafeConcurrentModification(() {
list.insert(0, list.removeLast());
});
testSafeConcurrentModification(() {
list.replaceRange(1, 3, list.sublist(1, 3).reversed);
});
// Argument errors on bad indices for methods that are only allowed
// on growable lists.
list.length = 4;
list.setAll(0, [0, 1, 2, 3]);
testArgumentError(action()) {
Expect.throws(action, (e) => e is ArgumentError);
}
// Direct indices (0 <= index < length).
testArgumentError(() => list.removeAt(-1));
testArgumentError(() => list.removeAt(4));
// Direct indices including end (0 <= index <= length).
testArgumentError(() => list.insert(-1, 0));
testArgumentError(() => list.insert(5, 0));
testArgumentError(() => list.insertAll(-1, [0]));
testArgumentError(() => list.insertAll(5, [0]));
testArgumentError(() => list.insertAll(-1, [0]));
testArgumentError(() => list.insertAll(5, [0]));
// Ranges (0 <= start <= end <= length).
testArgumentError(() => list.removeRange(-1, 2));
testArgumentError(() => list.removeRange(2, 5));
testArgumentError(() => list.removeRange(-1, 5));
testArgumentError(() => list.removeRange(4, 2));
testArgumentError(() => list.replaceRange(-1, 2, [9]));
testArgumentError(() => list.replaceRange(2, 5, [9]));
testArgumentError(() => list.replaceRange(-1, 5, [9]));
testArgumentError(() => list.replaceRange(4, 2, [9]));
}
class Yes {
operator ==(var other) => true;
int get hashCode => 0;
}
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;
}
}
class MyFixedList<E> extends ListBase<E> {
List<E> _source;
MyFixedList(this._source);
int get length => _source.length;
void set length(int length) {
throw new UnsupportedError("Fixed length!");
}
E operator [](int index) => _source[index];
void operator []=(int index, E value) {
_source[index] = value;
}
}
void testListConstructor() {
// Is fixed-length.
Expect.throws(() {
new List(0).add(4);
});
Expect.throws(() { new List(-2); }); // Not negative. //# 01: ok
// Not null.
Expect.throws(() {
new List(null);
});
Expect.listEquals([4], new List()..add(4));
// Is fixed-length.
Expect.throws(() {
new List.filled(0, 42).add(4);
});
// Not negative.
Expect.throws(() {
new List.filled(-2, 42);
});
// Not null.
Expect.throws(() {
new List.filled(null, 42);
});
}

View file

@ -1,32 +0,0 @@
// 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() {
List list = [1, 2];
list.add(list);
List list2 = new List(4);
list2[0] = 1;
list2[1] = 2;
list2[2] = list2;
list2[3] = list;
Expect.equals("[1, 2, [...]]", list.toString());
Expect.equals("[1, 2, [...], [1, 2, [...]]]", list2.toString());
// Throwing in the middle of a toString does not leave the
// list as being visited.
List list3 = [1, 2, new ThrowOnToString(), 4];
Expect.throws(list3.toString, (e) => e == "Bad!");
list3[2] = 3;
Expect.equals("[1, 2, 3, 4]", list3.toString());
}
class ThrowOnToString {
String toString() {
throw "Bad!";
}
}

View file

@ -1,31 +0,0 @@
// 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:collection';
class MyList extends ListBase {
final list;
MyList(this.list);
get length => list.length;
set length(val) {
list.length = val;
}
operator [](index) => list[index];
operator []=(index, val) => list[index] = val;
}
main() {
Expect.equals("[]", [].toString());
Expect.equals("[1]", [1].toString());
Expect.equals("[1, 2]", [1, 2].toString());
Expect.equals("[]", const [].toString());
Expect.equals("[1]", const [1].toString());
Expect.equals("[1, 2]", const [1, 2].toString());
Expect.equals("[]", new MyList([]).toString());
Expect.equals("[1]", new MyList([1]).toString());
Expect.equals("[1, 2]", new MyList([1, 2]).toString());
}

View file

@ -1,300 +0,0 @@
// 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:collection";
import "dart:typed_data";
main() {
var intTest = new Test<int>();
intTest.run("ConstList", createConstList);
intTest.run("FixedList", createFixedList);
intTest.run("GrowableList", createGrowableList);
intTest.run("ConstMapKeys", createConstMapKeys);
intTest.run("ConstMapValues", createConstMapValues);
intTest.run("MapKeys", createMapKeys);
intTest.run("MapValues", createMapValues);
intTest.run("SplayMapKeys", createSplayMapKeys);
intTest.run("SplayMapValues", createSplayMapValues);
intTest.run("Set", createSet);
intTest.run("SplaySet", createSplaySet);
intTest.run("Queue", createQueue);
intTest.run("ListMapKeys", createListMapKeys);
intTest.run("ListMapValues", createListMapValues);
intTest.run("CodeUnits", createCodeUnits);
intTest.run("TypedList", createTypedList);
new Test<String>().test("strings", ["a", "b", "c"]);
new Test<num>().test("superclass", <int>[1, 2, 3]);
new Test<int>().test("subclass", <num>[1, 2, 3]);
}
class Test<E> {
run(name, Iterable create(int size)) {
test(name, create(0));
test(name, create(1));
test(name, create(3));
}
test(name, iterable) {
testSingle(name, iterable);
testSingle("$name-where", iterable.where((x) => true));
testSingle("$name-map", iterable.map((x) => x));
testSingle("$name-expand", iterable.expand((x) => [x, x]));
testSingle("$name-skip", iterable.skip(1));
testSingle("$name-take", iterable.take(2));
testSingle("$name-skipWhile", iterable.skipWhile((x) => false));
testSingle("$name-takeWhile", iterable.takeWhile((x) => true));
}
testSingle(name, iterable) {
var elements = iterable.toList();
int length = elements.length;
var list = new List<E>.unmodifiable(iterable);
Expect.isTrue(list is List<E>, "$name-type-$E");
Expect.isTrue(list is! List<Test>, "$name-!type-!$E");
checkElements() {
Expect.equals(length, list.length);
for (int i = 0; i < length; i++) {
Expect.identical(elements[i], list[i], "$name-identical-$i");
}
}
checkElements();
throws(funcName, func) {
try {
func();
} catch (e, s) {
Expect.isTrue(e is UnsupportedError, "$name: $funcName threw $e");
return;
}
checkElements();
Expect.fail("$name: $funcName didn't throw");
}
throws("[]=", () {
list[0] = null;
});
throws("length=", () {
list.length = length + 1;
});
throws("length=", () {
list.length = length - 1;
});
throws("setAll", () {
list.setAll(0, []);
});
throws("add", () {
list.add(null);
});
throws("insert", () {
list.insert(0, null);
});
throws("insertAll", () {
list.insertAll(0, []);
});
throws("addAll", () {
list.addAll([]);
});
throws("remove", () {
list.remove(null);
});
throws("removeWhere", () {
list.removeWhere((x) => true);
});
throws("retainWhere", () {
list.retainWhere((x) => false);
});
throws("sort", () {
list.sort();
});
throws("shuffle", () {
list.shuffle();
});
throws("clear", () {
list.clear();
});
throws("removeAt", () {
list.removeAt(0);
});
throws("removeLast", () {
list.removeLast();
});
throws("setRange", () {
list.setRange(0, 1, []);
});
throws("removeRange", () {
list.removeRange(0, 1);
});
throws("replaceRange", () {
list.replaceRange(0, 1, []);
});
throws("fillRange", () {
list.fillRange(0, 1, null);
});
success(opName, op(list)) {
var expect;
try {
expect = op(elements);
} catch (e) {
try {
op(list);
} catch (e2) {
Expect.equals(e.runtimeType, e2.runtimeType);
return;
}
Expect.fail("$name-$opName didn't throw, expected: $e");
}
var actual = op(list);
checkElements();
if (expect is List) {
Expect.listEquals(expect, actual, "$name-$opName");
} else if (expect is Iterable) {
Expect.isTrue(actual is Iterable);
Expect.listEquals(expect.toList(), actual.toList(), "$name-$opName");
} else {
Expect.equals(expect, actual, "$name-$opName");
}
}
success("indexOf", (l) => l.indexOf(null));
success("lastIndexOf", (l) => l.lastIndexOf(null));
success("contains", (l) => l.contains(2));
success("elementAt", (l) => l.elementAt[1]);
success("reversed", (l) => l.reversed);
success("sublist0-1", (l) => l.sublist(0, 1));
success("getRange0-1", (l) => l.getRange(0, 1));
success("asMap-keys", (l) => l.asMap().keys);
success("asMap-values", (l) => l.asMap().values);
success("where", (l) => l.where((x) => true));
success("map", (l) => l.map((x) => x));
success("expand", (l) => l.expand((x) => [x, x]));
success("skip", (l) => l.skip(1));
success("take", (l) => l.take(1));
success("skipWhile", (l) => l.skipWhile((x) => false));
success("takeWhile", (l) => l.takeWhile((x) => true));
success("first", (l) => l.first);
success("last", (l) => l.last);
success("single", (l) => l.single);
success("firstWhere", (l) => l.firstWhere((x) => true));
success("lastWhere", (l) => l.lastWhere((x) => true));
success("singleWhere", (l) => l.singleWhere((x) => true));
success("isEmpty", (l) => l.isEmpty);
success("isNotEmpty", (l) => l.isNotEmpty);
success("join", (l) => l.join("/"));
success("fold", (l) => l.fold("--", (a, b) => "$a/$b"));
success("reduce", (l) => l.reduce((a, b) => a + b));
success("every", (l) => l.every((x) => x == 0));
success("any", (l) => l.any((x) => x == 2));
success("toList", (l) => l.toList());
success("toSet", (l) => l.toSet());
success("toString", (l) => l.toString());
var it = elements.iterator;
list.forEach((v) {
Expect.isTrue(it.moveNext());
Expect.equals(it.current, v);
});
Expect.isFalse(it.moveNext());
if (elements is List<int> && list is List<int>) {
success("String.fromCharCodes", (l) => new String.fromCharCodes(l));
}
}
}
createConstList(n) {
if (n == 0) return const <int>[];
return const <int>[1, 2, 3];
}
createFixedList(n) {
var result = new List<int>(n);
for (int i = 0; i < n; i++) result[i] = n;
return result;
}
createGrowableList(n) {
var result = new List<int>()..length = n;
for (int i = 0; i < n; i++) result[i] = n;
return result;
}
createIterable(n) => new Iterable.generate(n);
createConstMapKeys(n) {
if (n == 0) return const <int, int>{}.keys;
return const <int, int>{0: 0, 1: 1, 2: 2}.keys;
}
createConstMapValues(n) {
if (n == 0) return const <int, int>{}.values;
return const <int, int>{0: 0, 1: 1, 2: 2}.values;
}
createMapKeys(n) {
var map = <int, int>{};
for (int i = 0; i < n; i++) map[i] = i;
return map.keys;
}
createMapValues(n) {
var map = <int, int>{};
for (int i = 0; i < n; i++) map[i] = i;
return map.values;
}
createSplayMapKeys(n) {
var map = new SplayTreeMap<int, int>();
for (int i = 0; i < n; i++) map[i] = i;
return map.keys;
}
createSplayMapValues(n) {
var map = new SplayTreeMap<int, int>();
for (int i = 0; i < n; i++) map[i] = i;
return map.values;
}
createSet(n) {
var set = new Set<int>();
for (int i = 0; i < n; i++) set.add(i);
return set;
}
createSplaySet(n) {
var set = new SplayTreeSet<int>();
for (int i = 0; i < n; i++) set.add(i);
return set;
}
createQueue(n) {
var queue = new Queue<int>();
for (int i = 0; i < n; i++) queue.add(i);
return queue;
}
createListMapKeys(n) {
return createGrowableList(n).asMap().keys;
}
createListMapValues(n) {
return createGrowableList(n).asMap().values;
}
createCodeUnits(n) {
var string = new String.fromCharCodes(new Iterable.generate(n));
return string.codeUnits;
}
createTypedList(n) {
var tl = new Uint8List(n);
for (int i = 0; i < n; i++) tl[i] = i;
return tl;
}