[corelib] Add Set.unmodifiable backed by an UnmodifiableSetView

UnmodifiableSetView does not have backend specific implementations
(simillar to UnmodifiableMapView, unlike UnmodifiableListView).

Closes https://github.com/dart-lang/sdk/issues/36901

Change-Id: I041bb6dc95d6a67a395ca75581ffe8e5933acdc6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164103
Commit-Queue: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Kevin Moore <kevmoo@google.com>
Reviewed-by: Stephen Adams <sra@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
This commit is contained in:
Oleksii Khomchenko 2020-12-10 11:00:27 +00:00 committed by commit-bot@chromium.org
parent 80161c3081
commit 64226729c1
8 changed files with 639 additions and 37 deletions

View file

@ -14,6 +14,16 @@ opted out of null safety by adding `// @dart=2.9` to the beginning of the file.
### Core libraries
#### `dart:collection`
* Added `UnmodifiableSetView` class, which allows users to guarantee that
methods that could change underlying `Set` instance can not be invoked.
#### `dart:core`
* Added `unmodifiable` constructor to class `Set`, which allows users to create
unmodifiable `Set` instances.
#### `dart:io`
* `HttpRequest` will now correctly follow HTTP 308 redirects

View file

@ -1,11 +1,11 @@
ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_THROWS_EXCEPTION|lib/core/core.dart|3679|5|94|Const constructors can't throw exceptions.
ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_THROWS_EXCEPTION|lib/core/core.dart|7878|5|97|Const constructors can't throw exceptions.
ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_THROWS_EXCEPTION|lib/core/core.dart|7887|5|97|Const constructors can't throw exceptions.
ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_THROWS_EXCEPTION|lib/core/core.dart|893|5|95|Const constructors can't throw exceptions.
ERROR|COMPILE_TIME_ERROR|CONST_CONSTRUCTOR_THROWS_EXCEPTION|lib/core/core.dart|926|5|94|Const constructors can't throw exceptions.
ERROR|COMPILE_TIME_ERROR|INVALID_ASSIGNMENT|lib/_internal/js_dev_runtime/private/interceptors.dart|1358|18|27|A value of type 'double' can't be assigned to a variable of type 'int'.
ERROR|COMPILE_TIME_ERROR|RETURN_OF_INVALID_TYPE|lib/_internal/js_dev_runtime/private/interceptors.dart|1225|14|38|A value of type 'double' can't be returned from method '%' because it has a return type of 'JSNumber'.
ERROR|COMPILE_TIME_ERROR|RETURN_OF_INVALID_TYPE|lib/_internal/js_dev_runtime/private/interceptors.dart|1227|14|38|A value of type 'double' can't be returned from method '%' because it has a return type of 'JSNumber'.
ERROR|SYNTACTIC_ERROR|CONST_FACTORY|lib/core/core.dart|3677|3|5|Only redirecting factory constructors can be declared to be 'const'.
ERROR|SYNTACTIC_ERROR|CONST_FACTORY|lib/core/core.dart|7876|3|5|Only redirecting factory constructors can be declared to be 'const'.
ERROR|SYNTACTIC_ERROR|CONST_FACTORY|lib/core/core.dart|7885|3|5|Only redirecting factory constructors can be declared to be 'const'.
ERROR|SYNTACTIC_ERROR|CONST_FACTORY|lib/core/core.dart|891|3|5|Only redirecting factory constructors can be declared to be 'const'.
ERROR|SYNTACTIC_ERROR|CONST_FACTORY|lib/core/core.dart|924|3|5|Only redirecting factory constructors can be declared to be 'const'.

View file

@ -340,8 +340,38 @@ abstract class _SetBase<E> with SetMixin<E> {
Set<E> toSet() => _newSet()..addAll(this);
}
abstract class _UnmodifiableSetMixin<E> implements Set<E> {
static Never _throwUnmodifiable() {
throw UnsupportedError("Cannot change an unmodifiable set");
}
/// This operation is not supported by an unmodifiable set.
bool add(E value) => _throwUnmodifiable();
/// This operation is not supported by an unmodifiable set.
void clear() => _throwUnmodifiable();
/// This operation is not supported by an unmodifiable set.
void addAll(Iterable<E> elements) => _throwUnmodifiable();
/// This operation is not supported by an unmodifiable set.
void removeAll(Iterable<Object?> elements) => _throwUnmodifiable();
/// This operation is not supported by an unmodifiable set.
void retainAll(Iterable<Object?> elements) => _throwUnmodifiable();
/// This operation is not supported by an unmodifiable set.
void removeWhere(bool test(E element)) => _throwUnmodifiable();
/// This operation is not supported by an unmodifiable set.
void retainWhere(bool test(E element)) => _throwUnmodifiable();
/// This operation is not supported by an unmodifiable set.
bool remove(Object? value) => _throwUnmodifiable();
}
/// Class used to implement const sets.
class _UnmodifiableSet<E> extends _SetBase<E> {
class _UnmodifiableSet<E> extends _SetBase<E> with _UnmodifiableSetMixin<E> {
final Map<E, Null> _map;
const _UnmodifiableSet(this._map);
@ -364,38 +394,25 @@ class _UnmodifiableSet<E> extends _SetBase<E> {
}
return null;
}
// Mutating methods throw.
bool add(E value) {
throw UnsupportedError("Cannot change unmodifiable set");
}
void clear() {
throw UnsupportedError("Cannot change unmodifiable set");
}
void addAll(Iterable<E> elements) {
throw UnsupportedError("Cannot change unmodifiable set");
}
void removeAll(Iterable<Object?> elements) {
throw UnsupportedError("Cannot change unmodifiable set");
}
void retainAll(Iterable<Object?> elements) {
throw UnsupportedError("Cannot change unmodifiable set");
}
void removeWhere(bool test(E element)) {
throw UnsupportedError("Cannot change unmodifiable set");
}
void retainWhere(bool test(E element)) {
throw UnsupportedError("Cannot change unmodifiable set");
}
bool remove(Object? value) {
throw UnsupportedError("Cannot change unmodifiable set");
}
}
/// An unmodifiable [Set] view of another [Set].
///
/// Methods that could change the set, such as [add] and [remove],
/// must not be called.
class UnmodifiableSetView<E> extends SetBase<E> with _UnmodifiableSetMixin<E> {
final Set<E> _source;
/// Creates an [UnmodifiableSetView] of [source].
UnmodifiableSetView(Set<E> source) : _source = source;
bool contains(Object? element) => _source.contains(element);
E? lookup(Object? element) => _source.lookup(element);
int get length => _source.length;
Iterator<E> get iterator => _source.iterator;
Set<E> toSet() => _source.toSet();
}

View file

@ -90,6 +90,15 @@ abstract class Set<E> extends EfficientLengthIterable<E> {
*/
factory Set.of(Iterable<E> elements) = LinkedHashSet<E>.of;
/**
* Creates an unmodifiable [Set] from [elements].
*
* The new set behaves like the result of [Set.of],
* except that the set returned by this constructor is not modifiable.
*/
factory Set.unmodifiable(Iterable<E> elements) =>
UnmodifiableSetView<E>(<E>{...elements});
/**
* Adapts [source] to be a `Set<T>`.
*

View file

@ -482,6 +482,66 @@ void testASetFrom(setFrom) {
Expect.isTrue(aSet.length == 1);
}
void testUnmodifiable(Set source) {
var unmodifiable = Set.unmodifiable(source);
Expect.throwsUnsupportedError(() {
unmodifiable.add(3);
}, "add");
Expect.throwsUnsupportedError(() {
unmodifiable.addAll({1, 2, 3});
}, "addAll");
Expect.throwsUnsupportedError(() {
unmodifiable.addAll(<int>{});
}, "addAll empty");
Expect.throwsUnsupportedError(() {
unmodifiable.remove(3);
}, "remove");
Expect.throwsUnsupportedError(() {
unmodifiable.removeAll({1, 2, 3});
}, "removeAll");
Expect.throwsUnsupportedError(() {
unmodifiable.removeAll(<int>{});
}, "removeAll empty");
Expect.throwsUnsupportedError(() {
unmodifiable.retainAll({1, 2, 3});
}, "retainAll");
Expect.throwsUnsupportedError(() {
unmodifiable.retainAll(<int>{});
}, "retainAll empty");
Expect.throwsUnsupportedError(() {
unmodifiable.removeWhere((_) => true);
}, "removeWhere");
Expect.throwsUnsupportedError(() {
unmodifiable.retainWhere((_) => false);
}, "retainWhere");
Expect.throwsUnsupportedError(() {
unmodifiable.clear();
}, "clear");
}
void testUnmodifiableSetIsNotUpdatedIfSourceSetIsUpdated() {
var modifiable = {1};
var unmodifiable = Set.unmodifiable(modifiable);
modifiable.add(2);
Expect.notEquals(modifiable.length, unmodifiable.length);
Expect.setEquals({2}, modifiable.difference(unmodifiable));
modifiable.removeAll({1, 2});
Expect.setEquals({1}, unmodifiable.difference(modifiable));
Expect.setEquals({1}, unmodifiable);
}
main() {
testMain(() => new HashSet());
testMain(() => new LinkedHashSet());
@ -553,4 +613,7 @@ main() {
testASetFrom((x) => new HashSet<A>.from(x));
testASetFrom((x) => new LinkedHashSet<A>.from(x));
testASetFrom((x) => new SplayTreeSet<A>.from(x, identityCompare));
testUnmodifiable({1});
testUnmodifiableSetIsNotUpdatedIfSourceSetIsUpdated();
}

View file

@ -0,0 +1,220 @@
// Copyright (c) 2020, 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";
main() {
testIterableApi();
testUnmodifiableSetApi();
testMutatingApisThrow();
testChangesInOriginalSetAreObservedInUnmodifiableView();
}
void testIterableApi() {
Set<int> original = {1, 2, 3};
Set<int> copy = {...original};
UnmodifiableSetView<int> wrapped = new UnmodifiableSetView(original);
Expect.equals(wrapped.any((_) => true), original.any((_) => true));
Expect.equals(wrapped.any((_) => false), original.any((_) => false));
Expect.equals(wrapped.contains(0), original.contains(0));
Expect.equals(wrapped.elementAt(0), original.elementAt(0));
Expect.equals(wrapped.every((_) => true), original.every((_) => true));
Expect.equals(wrapped.every((_) => false), original.every((_) => false));
Expect.setEquals(
wrapped.expand((x) => [x, x]), original.expand((x) => [x, x]));
Expect.equals(wrapped.first, original.first);
Expect.equals(
wrapped.firstWhere((_) => true), original.firstWhere((_) => true));
Expect.throwsStateError(() {
wrapped.firstWhere((_) => false);
}, "firstWhere");
Expect.equals(wrapped.fold<int>(0, (x, y) => x + y),
original.fold<int>(0, (x, y) => x + y));
testForeach(wrapped, original);
Expect.equals(wrapped.isEmpty, original.isEmpty);
Expect.equals(wrapped.isNotEmpty, original.isNotEmpty);
testIterator(wrapped, original);
Expect.equals(wrapped.join(""), original.join(""));
Expect.equals(wrapped.join("-"), original.join("-"));
Expect.equals(wrapped.last, original.last);
Expect.equals(
wrapped.lastWhere((_) => true), original.lastWhere((_) => true));
Expect.throwsStateError(() {
wrapped.lastWhere((_) => false);
}, "lastWhere");
Expect.equals(wrapped.length, original.length);
Expect.setEquals(wrapped.map((x) => "[$x]"), original.map((x) => "[$x]"));
Expect.equals(
wrapped.reduce((x, y) => x + y), original.reduce((x, y) => x + y));
Expect.throwsStateError(() {
wrapped.single;
}, "single");
Expect.throwsStateError(() {
wrapped.singleWhere((_) => true);
}, "singleWhere true");
Expect.throwsStateError(() {
wrapped.singleWhere((_) => false);
}, "singleWhere false");
Expect.setEquals(wrapped.skip(0), original.skip(0));
Expect.setEquals(wrapped.skip(1), original.skip(1));
Expect.setEquals(
wrapped.skipWhile((_) => true), original.skipWhile((_) => true));
Expect.setEquals(
wrapped.skipWhile((_) => false), original.skipWhile((_) => false));
Expect.setEquals(wrapped.take(0), original.take(0));
Expect.setEquals(wrapped.take(1), original.take(1));
Expect.setEquals(
wrapped.takeWhile((_) => true), original.takeWhile((_) => true));
Expect.setEquals(
wrapped.takeWhile((_) => false), original.takeWhile((_) => false));
var toListResult = wrapped.toList();
Expect.listEquals(original.toList(), toListResult);
toListResult.add(4);
Expect.listEquals([1, 2, 3, 4], toListResult);
toListResult[3] = 5;
Expect.listEquals([1, 2, 3, 5], toListResult);
// wrapped and original are intact
Expect.setEquals(copy, wrapped);
Expect.setEquals(copy, original);
var toSetResult = wrapped.toSet();
Expect.setEquals(original.toSet(), toSetResult);
toSetResult.add(4);
Expect.setEquals({1, 2, 3, 4}, toSetResult);
// wrapped and original are intact
Expect.setEquals(copy, wrapped);
Expect.setEquals(copy, original);
Expect.setEquals(wrapped.where((_) => true), original.where((_) => true));
Expect.setEquals(wrapped.where((_) => false), original.where((_) => false));
}
void testUnmodifiableSetApi() {
Set<int> original = {1, 2, 3};
Set<int> copy = {...original};
UnmodifiableSetView<int> wrapped = new UnmodifiableSetView(original);
Expect.isTrue(wrapped.containsAll(copy));
Expect.isTrue(wrapped.containsAll(copy.toList()));
Expect.isTrue(wrapped.containsAll([]));
Expect.isTrue(wrapped.intersection({}).isEmpty);
Expect.setEquals(wrapped.intersection(copy), original);
Expect.setEquals(wrapped.union({}), original);
Expect.setEquals(wrapped.union(copy), original);
Expect.setEquals(wrapped.difference({}), original);
Expect.isTrue(wrapped.difference(copy).isEmpty);
}
void testMutatingApisThrow() {
UnmodifiableSetView<int> s = new UnmodifiableSetView({1, 2, 3});
Expect.throwsUnsupportedError(() {
s.add(3);
}, "add");
Expect.throwsUnsupportedError(() {
s.addAll({1, 2, 3});
}, "addAll");
Expect.throwsUnsupportedError(() {
s.addAll(<int>{});
}, "addAll empty");
Expect.throwsUnsupportedError(() {
s.remove(3);
}, "remove");
Expect.throwsUnsupportedError(() {
s.removeAll({1, 2, 3});
}, "removeAll");
Expect.throwsUnsupportedError(() {
s.removeAll(<int>{});
}, "removeAll empty");
Expect.throwsUnsupportedError(() {
s.retainAll({1, 2, 3});
}, "retainAll");
Expect.throwsUnsupportedError(() {
s.retainAll(<int>{});
}, "retainAll empty");
Expect.throwsUnsupportedError(() {
s.removeWhere((_) => true);
}, "removeWhere");
Expect.throwsUnsupportedError(() {
s.retainWhere((_) => false);
}, "retainWhere");
Expect.throwsUnsupportedError(() {
s.clear();
}, "clear");
}
void testChangesInOriginalSetAreObservedInUnmodifiableView() {
Set<int> original = {1, 2, 3};
Set<int> copy = {...original};
UnmodifiableSetView<int> wrapped = new UnmodifiableSetView(original);
original.add(4);
Expect.setEquals(original, wrapped);
Expect.setEquals({4}, wrapped.difference(copy));
}
void testForeach(Set<int> wrapped, Set<int> original) {
var wrapCtr = 0;
var origCtr = 0;
wrapped.forEach((x) {
wrapCtr += x;
});
original.forEach((x) {
origCtr += x;
});
Expect.equals(wrapCtr, origCtr);
}
void testIterator(Set<int> wrapped, Set<int> original) {
Iterator wrapIter = wrapped.iterator;
Iterator origIter = original.iterator;
while (origIter.moveNext()) {
Expect.isTrue(wrapIter.moveNext());
Expect.equals(wrapIter.current, origIter.current);
}
Expect.isFalse(wrapIter.moveNext());
}

View file

@ -482,6 +482,66 @@ void testASetFrom(setFrom) {
Expect.isTrue(aSet.length == 1);
}
void testUnmodifiable(Set source) {
var unmodifiable = Set.unmodifiable(source);
Expect.throwsUnsupportedError(() {
unmodifiable.add(3);
}, "add");
Expect.throwsUnsupportedError(() {
unmodifiable.addAll({1, 2, 3});
}, "addAll");
Expect.throwsUnsupportedError(() {
unmodifiable.addAll(<int>{});
}, "addAll empty");
Expect.throwsUnsupportedError(() {
unmodifiable.remove(3);
}, "remove");
Expect.throwsUnsupportedError(() {
unmodifiable.removeAll({1, 2, 3});
}, "removeAll");
Expect.throwsUnsupportedError(() {
unmodifiable.removeAll(<int>{});
}, "removeAll empty");
Expect.throwsUnsupportedError(() {
unmodifiable.retainAll({1, 2, 3});
}, "retainAll");
Expect.throwsUnsupportedError(() {
unmodifiable.retainAll(<int>{});
}, "retainAll empty");
Expect.throwsUnsupportedError(() {
unmodifiable.removeWhere((_) => true);
}, "removeWhere");
Expect.throwsUnsupportedError(() {
unmodifiable.retainWhere((_) => false);
}, "retainWhere");
Expect.throwsUnsupportedError(() {
unmodifiable.clear();
}, "clear");
}
void testUnmodifiableSetIsNotUpdatedIfSourceSetIsUpdated() {
var modifiable = {1};
var unmodifiable = Set.unmodifiable(modifiable);
modifiable.add(2);
Expect.notEquals(modifiable.length, unmodifiable.length);
Expect.setEquals({2}, modifiable.difference(unmodifiable));
modifiable.removeAll({1, 2});
Expect.setEquals({1}, unmodifiable.difference(modifiable));
Expect.setEquals({1}, unmodifiable);
}
main() {
testMain(() => new HashSet());
testMain(() => new LinkedHashSet());
@ -553,4 +613,7 @@ main() {
testASetFrom((x) => new HashSet<A>.from(x));
testASetFrom((x) => new LinkedHashSet<A>.from(x));
testASetFrom((x) => new SplayTreeSet<A>.from(x, identityCompare));
testUnmodifiable({1});
testUnmodifiableSetIsNotUpdatedIfSourceSetIsUpdated();
}

View file

@ -0,0 +1,220 @@
// Copyright (c) 2020, 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";
main() {
testIterableApi();
testUnmodifiableSetApi();
testMutatingApisThrow();
testChangesInOriginalSetAreObservedInUnmodifiableView();
}
void testIterableApi() {
Set<int> original = {1, 2, 3};
Set<int> copy = {...original};
UnmodifiableSetView<int> wrapped = new UnmodifiableSetView(original);
Expect.equals(wrapped.any((_) => true), original.any((_) => true));
Expect.equals(wrapped.any((_) => false), original.any((_) => false));
Expect.equals(wrapped.contains(0), original.contains(0));
Expect.equals(wrapped.elementAt(0), original.elementAt(0));
Expect.equals(wrapped.every((_) => true), original.every((_) => true));
Expect.equals(wrapped.every((_) => false), original.every((_) => false));
Expect.setEquals(
wrapped.expand((x) => [x, x]), original.expand((x) => [x, x]));
Expect.equals(wrapped.first, original.first);
Expect.equals(
wrapped.firstWhere((_) => true), original.firstWhere((_) => true));
Expect.throwsStateError(() {
wrapped.firstWhere((_) => false);
}, "firstWhere");
Expect.equals(wrapped.fold<int>(0, (x, y) => x + y),
original.fold<int>(0, (x, y) => x + y));
testForeach(wrapped, original);
Expect.equals(wrapped.isEmpty, original.isEmpty);
Expect.equals(wrapped.isNotEmpty, original.isNotEmpty);
testIterator(wrapped, original);
Expect.equals(wrapped.join(""), original.join(""));
Expect.equals(wrapped.join("-"), original.join("-"));
Expect.equals(wrapped.last, original.last);
Expect.equals(
wrapped.lastWhere((_) => true), original.lastWhere((_) => true));
Expect.throwsStateError(() {
wrapped.lastWhere((_) => false);
}, "lastWhere");
Expect.equals(wrapped.length, original.length);
Expect.setEquals(wrapped.map((x) => "[$x]"), original.map((x) => "[$x]"));
Expect.equals(
wrapped.reduce((x, y) => x + y), original.reduce((x, y) => x + y));
Expect.throwsStateError(() {
wrapped.single;
}, "single");
Expect.throwsStateError(() {
wrapped.singleWhere((_) => true);
}, "singleWhere true");
Expect.throwsStateError(() {
wrapped.singleWhere((_) => false);
}, "singleWhere false");
Expect.setEquals(wrapped.skip(0), original.skip(0));
Expect.setEquals(wrapped.skip(1), original.skip(1));
Expect.setEquals(
wrapped.skipWhile((_) => true), original.skipWhile((_) => true));
Expect.setEquals(
wrapped.skipWhile((_) => false), original.skipWhile((_) => false));
Expect.setEquals(wrapped.take(0), original.take(0));
Expect.setEquals(wrapped.take(1), original.take(1));
Expect.setEquals(
wrapped.takeWhile((_) => true), original.takeWhile((_) => true));
Expect.setEquals(
wrapped.takeWhile((_) => false), original.takeWhile((_) => false));
var toListResult = wrapped.toList();
Expect.listEquals(original.toList(), toListResult);
toListResult.add(4);
Expect.listEquals([1, 2, 3, 4], toListResult);
toListResult[3] = 5;
Expect.listEquals([1, 2, 3, 5], toListResult);
// wrapped and original are intact
Expect.setEquals(copy, wrapped);
Expect.setEquals(copy, original);
var toSetResult = wrapped.toSet();
Expect.setEquals(original.toSet(), toSetResult);
toSetResult.add(4);
Expect.setEquals({1, 2, 3, 4}, toSetResult);
// wrapped and original are intact
Expect.setEquals(copy, wrapped);
Expect.setEquals(copy, original);
Expect.setEquals(wrapped.where((_) => true), original.where((_) => true));
Expect.setEquals(wrapped.where((_) => false), original.where((_) => false));
}
void testUnmodifiableSetApi() {
Set<int> original = {1, 2, 3};
Set<int> copy = {...original};
UnmodifiableSetView<int> wrapped = new UnmodifiableSetView(original);
Expect.isTrue(wrapped.containsAll(copy));
Expect.isTrue(wrapped.containsAll(copy.toList()));
Expect.isTrue(wrapped.containsAll([]));
Expect.isTrue(wrapped.intersection({}).isEmpty);
Expect.setEquals(wrapped.intersection(copy), original);
Expect.setEquals(wrapped.union({}), original);
Expect.setEquals(wrapped.union(copy), original);
Expect.setEquals(wrapped.difference({}), original);
Expect.isTrue(wrapped.difference(copy).isEmpty);
}
void testMutatingApisThrow() {
UnmodifiableSetView<int> s = new UnmodifiableSetView({1, 2, 3});
Expect.throwsUnsupportedError(() {
s.add(3);
}, "add");
Expect.throwsUnsupportedError(() {
s.addAll({1, 2, 3});
}, "addAll");
Expect.throwsUnsupportedError(() {
s.addAll(<int>{});
}, "addAll empty");
Expect.throwsUnsupportedError(() {
s.remove(3);
}, "remove");
Expect.throwsUnsupportedError(() {
s.removeAll({1, 2, 3});
}, "removeAll");
Expect.throwsUnsupportedError(() {
s.removeAll(<int>{});
}, "removeAll empty");
Expect.throwsUnsupportedError(() {
s.retainAll({1, 2, 3});
}, "retainAll");
Expect.throwsUnsupportedError(() {
s.retainAll(<int>{});
}, "retainAll empty");
Expect.throwsUnsupportedError(() {
s.removeWhere((_) => true);
}, "removeWhere");
Expect.throwsUnsupportedError(() {
s.retainWhere((_) => false);
}, "retainWhere");
Expect.throwsUnsupportedError(() {
s.clear();
}, "clear");
}
void testChangesInOriginalSetAreObservedInUnmodifiableView() {
Set<int> original = {1, 2, 3};
Set<int> copy = {...original};
UnmodifiableSetView<int> wrapped = new UnmodifiableSetView(original);
original.add(4);
Expect.setEquals(original, wrapped);
Expect.setEquals({4}, wrapped.difference(copy));
}
void testForeach(Set<int> wrapped, Set<int> original) {
var wrapCtr = 0;
var origCtr = 0;
wrapped.forEach((x) {
wrapCtr += x;
});
original.forEach((x) {
origCtr += x;
});
Expect.equals(wrapCtr, origCtr);
}
void testIterator(Set<int> wrapped, Set<int> original) {
Iterator wrapIter = wrapped.iterator;
Iterator origIter = original.iterator;
while (origIter.moveNext()) {
Expect.isTrue(wrapIter.moveNext());
Expect.equals(wrapIter.current, origIter.current);
}
Expect.isFalse(wrapIter.moveNext());
}