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

Side by Side Diff: tests/corelib/list_test.dart

Issue 14425003: Make List.remove return boolean. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « sdk/lib/web_sql/dartium/web_sql_dartium.dart ('k') | tests/corelib/queue_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 import "dart:collection"; 5 import "dart:collection";
6 import "dart:typeddata";
6 import "package:expect/expect.dart"; 7 import "package:expect/expect.dart";
7 8
8 void main() { 9 void main() {
10 // Fixed length lists, length 4.
9 testFixedLengthList(new List(4)); 11 testFixedLengthList(new List(4));
12 testFixedLengthList(new List(4).toList(growable: false));
13 testFixedLengthList((new List()..length = 4).toList(growable: false));
10 // ListBase implementation of List. 14 // ListBase implementation of List.
11 testFixedLengthList(new MyFixedList(new List(4))); 15 testFixedLengthList(new MyFixedList(new List(4)));
16 testFixedLengthList(new MyFixedList(new List(4)).toList(growable: false));
12 17
18
19 testTypedList(new Uint8List(4));
20 testTypedList(new Int8List(4));
21 testTypedList(new Uint16List(4));
22 testTypedList(new Int16List(4));
23 testTypedList(new Uint32List(4));
24 testTypedList(new Int32List(4));
25
26 // Growable lists. Initial length 0.
13 testGrowableList(new List()); 27 testGrowableList(new List());
28 testGrowableList(new List().toList());
29 testGrowableList(new List(0).toList());
14 testGrowableList([]); 30 testGrowableList([]);
31 testGrowableList((const []).toList());
15 testGrowableList(new MyList([])); 32 testGrowableList(new MyList([]));
33 testGrowableList(new MyList([]).toList());
16 } 34 }
17 35
18 void expectValues(list, val1, val2, val3, val4) { 36 void testLength(int length, List list) {
19 Expect.isFalse(list.isEmpty); 37 Expect.equals(length, list.length);
20 Expect.equals(4, list.length); 38 (length == 0 ? Expect.isTrue : Expect.isFalse)(list.isEmpty);
21 Expect.equals(list[0], val1);
22 Expect.equals(list[1], val2);
23 Expect.equals(list[2], val3);
24 Expect.equals(list[3], val4);
25 } 39 }
26 40
27 void testClosures(List list) { 41 void testTypedLengthInvariantOperations(List list) {
28 testMap(val) {return val * 2 + 10; } 42 // length
29 List mapped = list.map(testMap).toList();
30 Expect.equals(mapped.length, list.length);
31 for (var i = 0; i < list.length; i++) {
32 Expect.equals(mapped[i], list[i]*2 + 10);
33 }
34
35 testFilter(val) { return val == 3; }
36 Iterable filtered = list.where(testFilter);
37 Expect.equals(filtered.length, 1);
38
39 testEvery(val) { return val != 11; }
40 bool test = list.every(testEvery);
41 Expect.isTrue(test);
42
43 testSome(val) { return val == 1; }
44 test = list.any(testSome);
45 Expect.isTrue(test);
46
47 testSomeFirst(val) { return val == 0; }
48 test = list.any(testSomeFirst);
49 Expect.isTrue(test);
50
51 testSomeLast(val) { return val == (list.length - 1); }
52 test = list.any(testSomeLast);
53 Expect.isTrue(test);
54 }
55
56 void testFixedLengthList(List list) {
57 Expect.equals(list.length, 4); 43 Expect.equals(list.length, 4);
44 // operators [], []=.
45 for (int i = 0; i < 4; i++) list[i] = 0;
58 list[0] = 4; 46 list[0] = 4;
59 expectValues(list, 4, null, null, null); 47 Expect.listEquals([4, 0, 0, 0], list);
60 String val = "fisk"; 48 list[1] = 7;
61 list[1] = val; 49 Expect.listEquals([4, 7, 0, 0], list);
62 expectValues(list, 4, val, null, null); 50 list[3] = 2;
63 double d = 2.0; 51 Expect.listEquals([4, 7, 0, 2], list);
64 list[3] = d;
65 expectValues(list, 4, val, null, d);
66 52
67 for (int i = 0; i < list.length; i++) { 53 for (int i = 0; i < list.length; i++) {
68 list[i] = i; 54 list[i] = i;
69 } 55 }
70 56
57 // indexOf, lastIndexOf
71 for (int i = 0; i < 4; i++) { 58 for (int i = 0; i < 4; i++) {
72 Expect.equals(i, list[i]); 59 Expect.equals(i, list[i]);
73 Expect.equals(i, list.indexOf(i)); 60 Expect.equals(i, list.indexOf(i));
74 Expect.equals(i, list.lastIndexOf(i)); 61 Expect.equals(i, list.lastIndexOf(i));
75 } 62 }
76 63
64 // setRange.
65 list.setRange(0, 4, [3, 2, 1, 0]);
66 Expect.listEquals([3, 2, 1, 0], list);
67
68 list.setRange(1, 4, list);
69 Expect.listEquals([3, 3, 2, 1], list);
70
71 list.setRange(0, 3, list, 1);
72 Expect.listEquals([3, 2, 1, 1], list);
73 list.setRange(0, 3, list, 1);
74 Expect.listEquals([2, 1, 1, 1], list);
75
76 list.setRange(2, 4, list, 0);
77 Expect.listEquals([2, 1, 2, 1], list);
78
79 // setAll.
80 list.setAll(0, [3, 2, 0, 1]);
81 Expect.listEquals([3, 2, 0, 1], list);
82 list.setAll(1, [0, 1]);
83 Expect.listEquals([3, 0, 1, 1], list);
84
85 // sort.
86 list.setRange(0, 4, [3, 2, 1, 0]);
87 list.sort();
88 Expect.listEquals([0, 1, 2, 3], list);
89 list.setRange(0, 4, [1, 2, 3, 0]);
90 list.sort();
91 Expect.listEquals([0, 1, 2, 3], list);
92 list.setRange(0, 4, [1, 3, 0, 2]);
93 list.sort((a, b) => b - a); // reverse compare.
94 Expect.listEquals([3, 2, 1, 0], list);
95 list.setRange(0, 4, [1, 2, 3, 0]);
96 list.sort((a, b) => b - a);
97 Expect.listEquals([3, 2, 1, 0], list);
98
99 // Some Iterable methods.
100
101 list.setRange(0, 4, [0, 1, 2, 3]);
102 // map.
103 testMap(val) {return val * 2 + 10; }
104 List mapped = list.map(testMap).toList();
105 Expect.equals(mapped.length, list.length);
106 for (var i = 0; i < list.length; i++) {
107 Expect.equals(mapped[i], list[i] * 2 + 10);
108 }
109
110 matchAll(val) => true;
111 matchSome(val) { return (val == 1 || val == 2); }
112 matchSomeFirst(val) { return val == 0; }
113 matchSomeLast(val) { return val == 3; }
114 matchNone(val) => false;
115
116 // where.
117 Iterable filtered = list.where(matchSome);
118 Expect.equals(filtered.length, 2);
119
120 // every
121 Expect.isTrue(list.every(matchAll));
122 Expect.isFalse(list.every(matchSome));
123 Expect.isFalse(list.every(matchNone));
124
125 // any
126 Expect.isTrue(list.any(matchAll));
127 Expect.isTrue(list.any(matchSome));
128 Expect.isTrue(list.any(matchSomeFirst));
129 Expect.isTrue(list.any(matchSomeLast));
130 Expect.isFalse(list.any(matchNone));
131 }
132
133 void testLengthInvariantOperations(List list) {
134 testTypedLengthInvariantOperations(list);
135 // Tests that need untyped lists.
136 list.setAll(0, [0, 1, 2, 3]);
77 Expect.equals(-1, list.indexOf(100)); 137 Expect.equals(-1, list.indexOf(100));
78 Expect.equals(-1, list.lastIndexOf(100)); 138 Expect.equals(-1, list.lastIndexOf(100));
79 list[2] = new Yes(); 139 list[2] = new Yes();
80 Expect.equals(2, list.indexOf(100)); 140 Expect.equals(2, list.indexOf(100));
81 Expect.equals(2, list.lastIndexOf(100)); 141 Expect.equals(2, list.lastIndexOf(100));
82 list[3] = new Yes(); 142 list[3] = new Yes();
83 Expect.equals(2, list.indexOf(100)); 143 Expect.equals(2, list.indexOf(100));
84 Expect.equals(3, list.lastIndexOf(100)); 144 Expect.equals(3, list.lastIndexOf(100));
85 list[2] = 2; 145 list[2] = 2;
86 Expect.equals(3, list.indexOf(100)); 146 Expect.equals(3, list.indexOf(100));
87 Expect.equals(3, list.lastIndexOf(100)); 147 Expect.equals(3, list.lastIndexOf(100));
88 list[3] = 3; 148 list[3] = 3;
89 Expect.equals(-1, list.indexOf(100)); 149 Expect.equals(-1, list.indexOf(100));
90 Expect.equals(-1, list.lastIndexOf(100)); 150 Expect.equals(-1, list.lastIndexOf(100));
91 151
92 testClosures(list); 152 }
93 153
94 Expect.throws(list.clear, (e) => e is UnsupportedError); 154 void testTypedList(List list) {
155 testTypedLengthInvariantOperations(list);
156 testCannotChangeLength(list);
157 }
158
159 void testFixedLengthList(List list) {
160 testLengthInvariantOperations(list);
161 testCannotChangeLength(list);
162 }
163
164 void testCannotChangeLength(List list) {
165 isUnsupported(action()) {
166 Expect.throws(action, (e) => e is UnsupportedError);
167 }
168 isUnsupported(() => list.add(0));
169 isUnsupported(() => list.addAll([0]));
170 isUnsupported(() => list.removeLast());
171 isUnsupported(() => list.insert(0, 1));
172 isUnsupported(() => list.insertAll(0, [1]));
173 isUnsupported(() => list.clear());
174 isUnsupported(() => list.remove(1));
175 isUnsupported(() => list.removeAt(1));
176 isUnsupported(() => list.removeRange(0, 1));
177 isUnsupported(() => list.replaceRange(0, 1, []));
95 } 178 }
96 179
97 void testGrowableList(List list) { 180 void testGrowableList(List list) {
98 Expect.isTrue(list.isEmpty); 181 testLength(0, list);
99 Expect.equals(list.length, 0); 182 // set length.
183 list.length = 4;
184 testLength(4, list);
185
186 testLengthInvariantOperations(list);
187
188 // add, removeLast.
189 list.clear();
190 testLength(0, list);
100 list.add(4); 191 list.add(4);
101 Expect.equals(1, list.length); 192 testLength(1, list);
102 Expect.isTrue(!list.isEmpty); 193 Expect.equals(4, list.removeLast());
103 Expect.equals(list.length, 1); 194 testLength(0, list);
104 Expect.equals(list.length, 1);
105 Expect.equals(list.removeLast(), 4);
106 195
107 for (int i = 0; i < 10; i++) { 196 for (int i = 0; i < 100; i++) {
108 list.add(i); 197 list.add(i);
109 } 198 }
110 199
111 Expect.equals(list.length, 10); 200 Expect.equals(list.length, 100);
112 for (int i = 0; i < 10; i++) { 201 for (int i = 0; i < 100; i++) {
113 Expect.equals(i, list[i]); 202 Expect.equals(i, list[i]);
114 Expect.equals(i, list.indexOf(i));
115 Expect.equals(i, list.lastIndexOf(i));
116 } 203 }
117 204
118 Expect.equals(-1, list.indexOf(100)); 205 Expect.equals(17, list.indexOf(17));
119 Expect.equals(-1, list.lastIndexOf(100)); 206 Expect.equals(17, list.lastIndexOf(17));
120 list[2] = new Yes(); 207 Expect.equals(-1, list.indexOf(999));
121 Expect.equals(2, list.indexOf(100)); 208 Expect.equals(-1, list.lastIndexOf(999));
122 Expect.equals(2, list.lastIndexOf(100));
123 list[3] = new Yes();
124 Expect.equals(2, list.indexOf(100));
125 Expect.equals(3, list.lastIndexOf(100));
126 list[2] = 2;
127 Expect.equals(3, list.indexOf(100));
128 Expect.equals(3, list.lastIndexOf(100));
129 list[3] = 3;
130 Expect.equals(-1, list.indexOf(100));
131 Expect.equals(-1, list.lastIndexOf(100));
132 209
133 testClosures(list); 210 Expect.equals(99, list.removeLast());
211 testLength(99, list);
134 212
135 Expect.equals(9, list.removeLast()); 213 // remove.
214 Expect.isTrue(list.remove(4));
215 testLength(98, list);
216 Expect.isFalse(list.remove(4));
217 testLength(98, list);
136 list.clear(); 218 list.clear();
137 Expect.equals(0, list.length); 219 testLength(0, list);
138 Expect.isTrue(list.isEmpty); 220
139 } 221 list.add(4);
222 list.add(4);
223 testLength(2, list);
224 Expect.isTrue(list.remove(4));
225 testLength(1, list);
226 Expect.isTrue(list.remove(4));
227 testLength(0, list);
228 Expect.isFalse(list.remove(4));
229 testLength(0, list);
230
231 // removeWhere, retainWhere
232 for (int i = 0; i < 100; i++) {
233 list.add(i);
234 }
235 testLength(100, list);
236 list.removeWhere((int x) => x.isOdd);
237 testLength(50, list);
238 for (int i = 0; i < list.length; i++) {
239 Expect.isTrue(list[i].isEven);
240 }
241 list.retainWhere((int x) => (x % 3) == 0);
242 testLength(17, list);
243 for (int i = 0; i < list.length; i++) {
244 Expect.isTrue((list[i] % 6) == 0);
245 }
246
247 // insert, remove, removeAt
248 list.clear();
249 testLength(0, list);
250
251 list.insert(0, 0);
252 Expect.listEquals([0], list);
253
254 list.insert(0, 1);
255 Expect.listEquals([1, 0], list);
256
257 list.insert(0, 2);
258 Expect.listEquals([2, 1, 0], list);
259
260 Expect.isTrue(list.remove(1));
261 Expect.listEquals([2, 0], list);
262
263 list.insert(1, 1);
264 Expect.listEquals([2, 1, 0], list);
265
266 list.removeAt(1);
267 Expect.listEquals([2, 0], list);
268
269 list.removeAt(1);
270 Expect.listEquals([2], list);
271
272 // insertAll
273 list.insertAll(0, [1, 2, 3]);
274 Expect.listEquals([1, 2, 3, 2], list);
275
276 list.insertAll(2, []);
277 Expect.listEquals([1, 2, 3, 2], list);
278
279 list.insertAll(4, [7, 9]);
280 Expect.listEquals([1, 2, 3, 2, 7, 9], list);
281
282 // addAll
283 list.addAll(list.reversed.toList());
284 Expect.listEquals([1, 2, 3, 2, 7, 9, 9, 7, 2, 3, 2, 1], list);
285
286 list.addAll([]);
287 Expect.listEquals([1, 2, 3, 2, 7, 9, 9, 7, 2, 3, 2, 1], list);
288
289 // replaceRange
290 list.replaceRange(3, 7, [0, 0]);
291 Expect.listEquals([1, 2, 3, 0, 0, 7, 2, 3, 2, 1], list);
292
293 list.replaceRange(2, 3, [5, 5, 5]);
294 Expect.listEquals([1, 2, 5, 5, 5, 0, 0, 7, 2, 3, 2, 1], list);
295
296 list.replaceRange(2, 4, [6, 6]);
297 Expect.listEquals([1, 2, 6, 6, 5, 0, 0, 7, 2, 3, 2, 1], list);
298
299 list.replaceRange(6, 8, []);
300 Expect.listEquals([1, 2, 6, 6, 5, 0, 2, 3, 2, 1], list);
301 }
140 302
141 class Yes { 303 class Yes {
142 operator ==(var other) => true; 304 operator ==(var other) => true;
143 } 305 }
144 306
145 class MyList<E> extends ListBase<E> { 307 class MyList<E> extends ListBase<E> {
146 List<E> _source; 308 List<E> _source;
147 MyList(this._source); 309 MyList(this._source);
148 int get length => _source.length; 310 int get length => _source.length;
149 void set length(int length) { _source.length = length; } 311 void set length(int length) { _source.length = length; }
150 E operator[](int index) => _source[index]; 312 E operator[](int index) => _source[index];
151 void operator[]=(int index, E value) { _source[index] = value; } 313 void operator[]=(int index, E value) { _source[index] = value; }
152 } 314 }
153 315
154 class MyFixedList<E> extends ListBase<E> { 316 class MyFixedList<E> extends ListBase<E> {
155 List<E> _source; 317 List<E> _source;
156 MyFixedList(this._source); 318 MyFixedList(this._source);
157 int get length => _source.length; 319 int get length => _source.length;
158 void set length(int length) { throw new UnsupportedError("Fixed length!"); } 320 void set length(int length) { throw new UnsupportedError("Fixed length!"); }
159 E operator[](int index) => _source[index]; 321 E operator[](int index) => _source[index];
160 void operator[]=(int index, E value) { _source[index] = value; } 322 void operator[]=(int index, E value) { _source[index] = value; }
161 } 323 }
OLDNEW
« no previous file with comments | « sdk/lib/web_sql/dartium/web_sql_dartium.dart ('k') | tests/corelib/queue_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698