Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: tests/corelib_strong/collection_removes_test.dart

Issue 2977403002: Migrate test block 3 to Dart 2.0. (Closed)
Patch Set: Addressed Bob's nits Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 import 'dart:collection';
6 import "package:expect/expect.dart";
7
8 testRemove(base) {
9 int length = base.length;
10 for (int i = 0; i < length; i++) {
11 Expect.isFalse(base.isEmpty);
12 base.remove(base.first);
13 }
14 Expect.isTrue(base.isEmpty);
15 }
16
17 testRemoveAll(base, Iterable removes) {
18 Set retained = new Set();
19 for (var element in base) {
20 if (!removes.contains(element)) {
21 retained.add(element);
22 }
23 }
24 String name = "$base.removeAll($removes) -> $retained";
25 base.removeAll(removes);
26 for (var value in base) {
27 Expect.isFalse(removes.contains(value), "$name: Found $value");
28 }
29 for (var value in retained) {
30 Expect.isTrue(base.contains(value), "$name: Found $value");
31 }
32 }
33
34 testRetainAll(base, Iterable retains) {
35 Set retained = new Set();
36 for (var element in base) {
37 if (retains.contains(element)) {
38 retained.add(element);
39 }
40 }
41 String name = "$base.retainAll($retains) -> $retained";
42 base.retainAll(retains);
43 for (var value in base) {
44 Expect.isTrue(retains.contains(value), "$name: Found $value");
45 }
46 for (var value in retained) {
47 Expect.isTrue(base.contains(value), "$name: Found $value");
48 }
49 }
50
51 testRemoveWhere(base, bool test(value)) {
52 Set retained = new Set();
53 for (var element in base) {
54 if (!test(element)) {
55 retained.add(element);
56 }
57 }
58 String name = "$base.removeWhere(...) -> $retained";
59 base.removeWhere(test);
60 for (var value in base) {
61 Expect.isFalse(test(value), "$name: Found $value");
62 }
63 for (var value in retained) {
64 Expect.isTrue(base.contains(value), "$name: Found $value");
65 }
66 }
67
68 testRetainWhere(base, bool test(value)) {
69 Set retained = new Set();
70 for (var element in base) {
71 if (test(element)) {
72 retained.add(element);
73 }
74 }
75 String name = "$base.retainWhere(...) -> $retained";
76 base.retainWhere(test);
77 for (var value in base) {
78 Expect.isTrue(test(value), "$name: Found $value");
79 }
80 for (var value in retained) {
81 Expect.isTrue(base.contains(value), "$name: Found $value");
82 }
83 }
84
85 void main() {
86 var collections = [
87 [],
88 [1],
89 [2],
90 [1, 2],
91 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
92 [1, 3, 5, 7, 9],
93 [2, 4, 6, 8, 10]
94 ];
95 for (var base in collections) {
96 for (var delta in collections) {
97 testRemove(base.toList());
98 testRemove(base.toSet());
99
100 var deltaSet = delta.toSet();
101 testRemoveWhere(base.toList(), deltaSet.contains);
102 testRetainWhere(base.toList(), (e) => !deltaSet.contains(e));
103
104 testRemoveAll(base.toSet(), delta);
105 testRemoveAll(base.toSet(), deltaSet);
106 testRetainAll(base.toSet(), delta);
107 testRetainAll(base.toSet(), deltaSet);
108 testRemoveWhere(base.toSet(), deltaSet.contains);
109 testRetainWhere(base.toSet(), (e) => !deltaSet.contains(e));
110
111 // Test the ListBase class's List implementation.
112 testRemoveWhere(new MyList(base.toList()), deltaSet.contains);
113 testRetainWhere(new MyList(base.toList()), (e) => !deltaSet.contains(e));
114 }
115 }
116 }
117
118 class MyList<E> extends ListBase<E> {
119 List<E> _source;
120 MyList(this._source);
121 int get length => _source.length;
122 void set length(int length) {
123 _source.length = length;
124 }
125
126 E operator [](int index) => _source[index];
127 void operator []=(int index, E value) {
128 _source[index] = value;
129 }
130 }
OLDNEW
« no previous file with comments | « tests/corelib_strong/collection_length_test.dart ('k') | tests/corelib_strong/collection_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698