mirror of
https://github.com/dart-lang/sdk
synced 2024-09-15 23:09:48 +00:00
e769817cdc
Splitting out a failing test to increase coverage of other tests. It is not clear that `a.addAll(a)` should throw if `a` is empty. `a` is not modified by the action. dart2js/DDC do not detect this case and I'm reluctant to add a check for the case where `a` is in fact not modified. The VM test is very specific to an identical argument and cannot detect the case for `a = Mylist([])`. Another option to this proposal is to simply delete the `[]` case. In general, I'd like to move more ConcurrentModificationError tests into assert statements. Change-Id: I1f9559bdb43ea1bae0575413748c9d9d0de58b99 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/170023 Commit-Queue: Stephen Adams <sra@google.com> Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
97 lines
3 KiB
Dart
97 lines
3 KiB
Dart
// 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() {
|
|
// Growable lists. Initial length 0.
|
|
testConcurrentModification(<int>[].toList());
|
|
testConcurrentModification(new List.filled(0, 0, growable: true));
|
|
testConcurrentModification([]);
|
|
testConcurrentModification(new List.from(const []));
|
|
testConcurrentModification(new MyList([]));
|
|
testConcurrentModification(new MyList<int>([]).toList());
|
|
|
|
testConcurrentModification(new Uint8List(0).toList());
|
|
testConcurrentModification(new Int8List(0).toList());
|
|
testConcurrentModification(new Uint16List(0).toList());
|
|
testConcurrentModification(new Int16List(0).toList());
|
|
testConcurrentModification(new Uint32List(0).toList());
|
|
testConcurrentModification(new Int32List(0).toList());
|
|
}
|
|
|
|
void testConcurrentModification(List<int> list) {
|
|
// add, removeLast.
|
|
list.clear();
|
|
list.addAll([1, 2, 3, 2, 7, 9, 9, 7, 2, 3, 2, 1]);
|
|
|
|
// Operations that change the length cause ConcurrentModificationError.
|
|
void testModification(action()) {
|
|
testIterator(int when) {
|
|
list.clear();
|
|
list.addAll([0, 1, 2, 3]);
|
|
Expect.throws(() {
|
|
for (var element in list) {
|
|
if (element == when) action();
|
|
}
|
|
}, (e) => e is ConcurrentModificationError);
|
|
}
|
|
|
|
testForEach(int when) {
|
|
list.clear();
|
|
list.addAll([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);
|
|
}
|
|
|
|
testModification(() => list.add(5));
|
|
testModification(() => list.addAll([5, 6]));
|
|
testModification(() => list.removeLast());
|
|
for (int i = 0; i < 4; i++) {
|
|
testModification(() => list.remove(i));
|
|
testModification(() => list.removeAt(i));
|
|
testModification(() => list.removeWhere((x) => x == i));
|
|
testModification(() => list.retainWhere((x) => x != i));
|
|
testModification(() => list.insert(i, 5));
|
|
testModification(() => list.insertAll(i, [5, 6]));
|
|
testModification(() => list.removeRange(i, i + 1));
|
|
testModification(() => list.replaceRange(i, i + 1, [5, 6]));
|
|
}
|
|
}
|
|
|
|
class MyList<E> extends ListBase<E> {
|
|
// TODO(42496): Use a nullable list because insert() is implemented in terms
|
|
// of length=. Change this back to `E` and remove the `as E` below when that
|
|
// issue is fixed.
|
|
List<E?> _source;
|
|
MyList(this._source);
|
|
int get length => _source.length;
|
|
void set length(int length) {
|
|
_source.length = length;
|
|
}
|
|
|
|
void add(E element) {
|
|
_source.add(element);
|
|
}
|
|
|
|
E operator [](int index) => _source[index] as E;
|
|
void operator []=(int index, E value) {
|
|
_source[index] = value;
|
|
}
|
|
}
|