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

Side by Side Diff: pkg/unmodifiable_collection/test/unmodifiable_collection_test.dart

Issue 17315017: Add unmodifiable_collection package. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 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
OLDNEW
(Empty)
1 // Copyright (c) 2013, 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 "../lib/unmodifiable_collection.dart";
6 import "package:unittest/unittest.dart";
7
8 // Test unmodifiable collection views.
9 // The collections should pass through the operations that are allowed,
10 // an throw on the ones that aren't without affecting the original.
11
12 main() {
13 List list = [];
14 testUnmodifiableList(list, new UnmodifiableListView(list), "empty");
15 list = [42];
16 testUnmodifiableList(list, new UnmodifiableListView(list), "single-42");
17 list = [7];
18 testUnmodifiableList(list, new UnmodifiableListView(list), "single!42");
19 list = [1, 42, 10];
20 testUnmodifiableList(list, new UnmodifiableListView(list), "three-42");
21 list = [1, 7, 10];
22 testUnmodifiableList(list, new UnmodifiableListView(list), "three!42");
23
24 list = [];
25 testNonGrowableList(list, new NonGrowableListView(list), "empty");
26 list = [42];
27 testNonGrowableList(list, new NonGrowableListView(list), "single-42");
28 list = [7];
29 testNonGrowableList(list, new NonGrowableListView(list), "single!42");
30 list = [1, 42, 10];
31 testNonGrowableList(list, new NonGrowableListView(list), "three-42");
32 list = [1, 7, 10];
33 testNonGrowableList(list, new NonGrowableListView(list), "three!42");
34
35 Set aSet = new Set();
36 testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "empty");
37 aSet = new Set.from([42]);
38 testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "single-42");
39 aSet = new Set.from([7]);
40 testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "single!42");
41 aSet = new Set.from([1, 42, 10]);
42 testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "three-42");
43 aSet = new Set.from([1, 7, 10]);
44 testUnmodifiableSet(aSet, new UnmodifiableSetView(aSet), "three!42");
45
46 Map map = new Map();
47 testUnmodifiableMap(map, new UnmodifiableMapView(map), "empty");
48 map = new Map()..[0] = 2;
49 testUnmodifiableMap(map, new UnmodifiableMapView(map), "single-0");
50 map = new Map()..[3] = 2;
51 testUnmodifiableMap(map, new UnmodifiableMapView(map), "single!0");
52 map = new Map()..[0] = 2
53 ..[1] = 1
54 ..[2] = 0;
55 testUnmodifiableMap(map, new UnmodifiableMapView(map), "three-0");
56 map = new Map()..[3] = 2
57 ..[1] = 1
58 ..[2] = 3;
59 testUnmodifiableMap(map, new UnmodifiableMapView(map), "three!0");
60 }
61
62 void testUnmodifiableList(List original, List wrapped, String name) {
63 name = "unmodifiable-list-$name";
64 testIterable(original, wrapped, name);
65 testReadList(original, wrapped, name);
66 testNoWriteList(original, wrapped, name);
67 testNoChangeLengthList(original, wrapped, name);
68 }
69
70 void testNonGrowableList(List original, List wrapped, String name) {
71 name = "nongrowable-list-$name";
72 testIterable(original, wrapped, name);
73 testReadList(original, wrapped, name);
74 testWriteList(original, wrapped, name);
75 testNoChangeLengthList(original, wrapped, name);
76 }
77
78 void testUnmodifiableSet(Set original, Set wrapped, String name) {
79 name = "unmodifiable-set-$name";
80 testIterable(original, wrapped, name);
81 testReadSet(original, wrapped, name);
82 testNoChangeSet(original, wrapped, name);
83 }
84
85 void testUnmodifiableMap(Map original, Map wrapped, name) {
86 name = "unmodifiable-map-$name";
87 testReadMap(original, wrapped, name);
88 testNoChangeMap(original, wrapped, name);
89 }
90
91 void testIterable(Iterable original, Iterable wrapped, String name) {
92 test("$name - any", () {
93 expect(wrapped.any((x) => true), equals(original.any((x) => true)));
94 expect(wrapped.any((x) => false), equals(original.any((x) => false)));
95 });
96
97 test("$name - contains", () {
98 expect(wrapped.contains(0), equals(original.contains(0)));
99 });
100
101 test("$name - elementAt", () {
102 if (original.isEmpty) {
103 expect(() => wrapped.elementAt(0), throws);
104 } else {
105 expect(wrapped.elementAt(0), equals(original.elementAt(0)));
106 }
107 });
108
109 test("$name - every", () {
110 expect(wrapped.every((x) => true), equals(original.every((x) => true)));
111 expect(wrapped.every((x) => false), equals(original.every((x) => false)));
112 });
113
114 test("$name - expand", () {
115 expect(wrapped.expand((x) => [x, x]),
116 equals(original.expand((x) => [x, x])));
117 });
118
119 test("$name - first", () {
120 if (original.isEmpty) {
121 expect(() => wrapped.first, throws);
122 } else {
123 expect(wrapped.first, equals(original.first));
124 }
125 });
126
127 test("$name - firstWhere", () {
128 if (original.isEmpty) {
129 expect(() => wrapped.firstWhere((_) => true), throws);
130 } else {
131 expect(wrapped.firstWhere((_) => true),
132 equals(original.firstWhere((_) => true)));
133 }
134 expect(() => wrapped.firstWhere((_) => false), throws);
135 });
136
137 test("$name - fold", () {
138 expect(wrapped.fold(0, (x, y) => x + y),
139 equals(original.fold(0, (x, y) => x + y)));
140 });
141
142 test("$name - forEach", () {
143 int wrapCtr = 0;
144 int origCtr = 0;
145 wrapped.forEach((x) { wrapCtr += x; });
146 original.forEach((x) { origCtr += x; });
147 expect(wrapCtr, equals(origCtr));
148 });
149
150 test("$name - isEmpty", () {
151 expect(wrapped.isEmpty, equals(original.isEmpty));
152 });
153
154 test("$name - isNotEmpty", () {
155 expect(wrapped.isNotEmpty, equals(original.isNotEmpty));
156 });
157
158 test("$name - iterator", () {
159 Iterator wrapIter = wrapped.iterator;
160 Iterator origIter = original.iterator;
161 while (origIter.moveNext()) {
162 expect(wrapIter.moveNext(), equals(true));
163 expect(wrapIter.current, equals(origIter.current));
164 }
165 expect(wrapIter.moveNext(), equals(false));
166 });
167
168 test("$name - join", () {
169 expect(wrapped.join(""), equals(original.join("")));
170 expect(wrapped.join("-"), equals(original.join("-")));
171 });
172
173 test("$name - last", () {
174 if (original.isEmpty) {
175 expect(() => wrapped.last, throws);
176 } else {
177 expect(wrapped.last, equals(original.last));
178 }
179 });
180
181 test("$name - lastWhere", () {
182 if (original.isEmpty) {
183 expect(() => wrapped.lastWhere((_) => true), throws);
184 } else {
185 expect(wrapped.lastWhere((_) => true),
186 equals(original.lastWhere((_) => true)));
187 }
188 expect(() => wrapped.lastWhere((_) => false), throws);
189 });
190
191 test("$name - length", () {
192 expect(wrapped.length, equals(original.length));
193 });
194
195 test("$name - map", () {
196 expect(wrapped.map((x) => "[$x]"),
197 equals(original.map((x) => "[$x]")));
198 });
199
200 test("$name - reduce", () {
201 if (original.isEmpty) {
202 expect(() => wrapped.reduce((x, y) => x + y), throws);
203 } else {
204 expect(wrapped.reduce((x, y) => x + y),
205 equals(original.reduce((x, y) => x + y)));
206 }
207 });
208
209 test("$name - single", () {
210 if (original.length != 1) {
211 expect(() => wrapped.single, throws);
212 } else {
213 expect(wrapped.single, equals(original.single));
214 }
215 });
216
217 test("$name - singleWhere", () {
218 if (original.length != 1) {
219 expect(() => wrapped.singleWhere((_) => true), throws);
220 } else {
221 expect(wrapped.singleWhere((_) => true),
222 equals(original.singleWhere((_) => true)));
223 }
224 expect(() => wrapped.singleWhere((_) => false), throws);
225 });
226
227 test("$name - skip", () {
228 expect(wrapped.skip(0), equals(original.skip(0)));
229 expect(wrapped.skip(1), equals(original.skip(1)));
230 expect(wrapped.skip(5), equals(original.skip(5)));
231 });
232
233 test("$name - skipWhile", () {
234 expect(wrapped.skipWhile((x) => true), equals(original.skipWhile((x) => true )));
floitsch 2013/06/20 13:07:33 long line.
Lasse Reichstein Nielsen 2013/06/20 14:04:19 Done.
235 expect(wrapped.skipWhile((x) => false), equals(original.skipWhile((x) => fal se)));
236 expect(wrapped.skipWhile((x) => x != 42),
237 equals(original.skipWhile((x) => x != 42)));
238 });
239
240 test("$name - take", () {
241 expect(wrapped.take(0), equals(original.take(0)));
242 expect(wrapped.take(1), equals(original.take(1)));
243 expect(wrapped.take(5), equals(original.take(5)));
244 });
245
246 test("$name - takeWhile", () {
247 expect(wrapped.takeWhile((x) => true),
248 equals(original.takeWhile((x) => true)));
249 expect(wrapped.takeWhile((x) => false),
250 equals(original.takeWhile((x) => false)));
251 expect(wrapped.takeWhile((x) => x != 42),
252 equals(original.takeWhile((x) => x != 42)));
253 });
254
255 test("$name - toList", () {
256 expect(wrapped.toList(), equals(original.toList()));
257 expect(wrapped.toList(growable: false),
258 equals(original.toList(growable: false)));
259 });
260
261 test("$name - toSet", () {
262 expect(wrapped.toSet(), equals(original.toSet()));
263 });
264
265 test("$name - where", () {
266 expect(wrapped.where((x) => true),
267 equals(original.where((x) => true)));
268 expect(wrapped.where((x) => false),
269 equals(original.where((x) => false)));
270 expect(wrapped.where((x) => x != 42),
271 equals(original.where((x) => x != 42)));
272 });
273 }
274
275 void testReadList(List original, List wrapped, String name) {
276 test("$name - length", () {
277 expect(wrapped.length, equals(original.length));
278 });
279
280 test("$name - isEmpty", () {
281 expect(wrapped.isEmpty, equals(original.isEmpty));
282 });
283
284 test("$name - isNotEmpty", () {
285 expect(wrapped.isNotEmpty, equals(original.isNotEmpty));
286 });
287
288 test("$name - []", () {
289 if (original.isEmpty) {
290 expect(() { wrapped[0]; }, throwsRangeError);
291 } else {
292 expect(wrapped[0], equals(original[0]));
293 }
294 });
295
296 test("$name - indexOf", () {
297 expect(wrapped.indexOf(42), equals(original.indexOf(42)));
298 });
299
300 test("$name - lastIndexOf", () {
301 expect(wrapped.lastIndexOf(42), equals(original.lastIndexOf(42)));
302 });
303
304 test("$name - getRange", () {
305 int len = original.length;
306 expect(wrapped.getRange(0, len), equals(original.getRange(0, len)));
307 expect(wrapped.getRange(len >> 1, len),
floitsch 2013/06/20 13:07:33 len ~/ 2
Lasse Reichstein Nielsen 2013/06/20 14:04:19 Done.
308 equals(original.getRange(len >> 1, len)));
309 expect(wrapped.getRange(0, len >> 1),
310 equals(original.getRange(0, len >> 1)));
311 });
312
313 test("$name - sublist", () {
314 int len = original.length;
315 expect(wrapped.sublist(0), equals(original.sublist(0)));
316 expect(wrapped.sublist(len >> 1), equals(original.sublist(len >> 1)));
floitsch 2013/06/20 13:07:33 ditto.
Lasse Reichstein Nielsen 2013/06/20 14:04:19 Done.
317 expect(wrapped.sublist(0, len >> 1),
318 equals(original.sublist(0, len >> 1)));
319 });
320
321 test("$name - asMap", () {
322 expect(wrapped.asMap(), equals(original.asMap()));
323 });
324 }
325
326 void testNoWriteList(List original, List wrapped, String name) {
327 List copy = new List.from(original);
328
329 testThrows(name, thunk) {
330 test(name, () {
331 expect(thunk, throwsUnsupportedError);
332 // No modifications happened.
333 expect(original, equals(copy));
334 });
335 }
336
337 testThrows("$name - []= throws", () { wrapped[0] = 42; });
338
339 testThrows("$name - sort throws", () { wrapped.sort(); });
340
341 testThrows("$name - fillRange throws", () {
342 wrapped.fillRange(0, wrapped.length, 42);
343 });
344
345 testThrows("$name - setRange throws", () {
346 wrapped.setRange(0, wrapped.length,
347 new Iterable.generate(wrapped.length, (i) => i));
348 });
349
350 testThrows("$name - setAll throws", () {
351 wrapped.setAll(0, new Iterable.generate(wrapped.length, (i) => i));
352 });
353 }
354
355 void testWriteList(List original, List wrapped, String name) {
356 List copy = new List.from(original);
357
358 test("$name - []=", () {
359 if (original.isNotEmpty) {
360 int originalFirst = original[0];
361 wrapped[0] = originalFirst + 1;
362 expect(original[0], equals(originalFirst + 1));
363 original[0] = originalFirst;
364 } else {
365 expect(() { wrapped[0] = 42; }, throws);
366 }
367 });
368
369 test("$name - sort", () {
370 List sortCopy = new List.from(original);
371 sortCopy.sort();
372 wrapped.sort();
373 expect(original, equals(sortCopy));
374 original.setAll(0, copy);
375 });
376
377 test("$name - fillRange", () {
378 wrapped.fillRange(0, wrapped.length, 37);
379 for (int i = 0; i < original.length; i++) {
380 expect(original[i], equals(37));
381 }
382 original.setAll(0, copy);
383 });
384
385 test("$name - setRange", () {
386 List reverseList = original.reversed.toList();
387 wrapped.setRange(0, wrapped.length, reverseList);
388 expect(original, equals(reverseList));
389 original.setAll(0, copy);
390 });
391
392 test("$name - setAll", () {
393 List reverseList = original.reversed.toList();
394 wrapped.setAll(0, reverseList);
395 expect(original, equals(reverseList));
396 original.setAll(0, copy);
397 });
398 }
399
400 void testNoChangeLengthList(List original, List wrapped, String name) {
401 List copy = new List.from(original);
402
403 testThrows(name, thunk) {
404 test(name, () {
405 expect(thunk, throwsUnsupportedError);
406 // No modifications happened.
407 expect(original, equals(copy));
408 });
409 }
410
411 testThrows("$name - length= throws", () {
412 wrapped.length = 100;
413 });
414
415 testThrows("$name - add throws", () {
416 wrapped.add(42);
417 });
418
419 testThrows("$name - addAll throws", () {
420 wrapped.addAll([42]);
421 });
422
423 testThrows("$name - insert throws", () {
424 wrapped.insert(0, 42);
425 });
426
427 testThrows("$name - insertAll throws", () {
428 wrapped.insertAll(0, [42]);
429 });
430
431 testThrows("$name - remove throws", () {
432 wrapped.remove(42);
433 });
434
435 testThrows("$name - removeAt throws", () {
436 wrapped.removeAt(0);
437 });
438
439 testThrows("$name - removeLast throws", () {
440 wrapped.removeLast();
441 });
442
443 testThrows("$name - removeWhere throws", () {
444 wrapped.removeWhere((E element) => false);
445 });
446
447 testThrows("$name - retainWhere throws", () {
448 wrapped.retainWhere((E element) => true);
449 });
450
451 testThrows("$name - removeRange throws", () {
452 wrapped.removeRange(0, wrapped.length);
453 });
454
455 testThrows("$name - replaceRange throws", () {
456 wrapped.replaceRange(0, wrapped.length, [42]);
457 });
458
459 testThrows("$name - clear throws", () {
460 wrapped.clear();
461 });
462 }
463
464 void testReadSet(Set original, Set wrapped, String name) {
465 Set copy = new Set.from(original);
466
467 test("$name - containsAll", () {
468 expect(wrapped.containsAll(copy), isTrue);
469 expect(wrapped.containsAll(copy.toList()), isTrue);
470 expect(wrapped.containsAll([]), isTrue);
471 expect(wrapped.containsAll([42]), equals(original.containsAll([42])));
472 });
473
474 test("$name - intersection", () {
475 expect(wrapped.intersection(new Set()), isEmpty);
476 expect(wrapped.intersection(copy), equals(original));
477 expect(wrapped.intersection(new Set.from([42])),
478 new Set.from(original.contains(42) ? [42] : []));
479 });
480
481 test("$name - union", () {
482 expect(wrapped.union(new Set()), equals(original));
483 expect(wrapped.union(copy), equals(original));
484 expect(wrapped.union(new Set.from([42])),
485 equals(original.union(new Set.from([42]))));
486 });
487
488 test("$name - difference", () {
489 expect(wrapped.difference(new Set()), equals(original));
490 expect(wrapped.difference(copy), isEmpty);
491 expect(wrapped.difference(new Set.from([42])),
492 equals(original.difference(new Set.from([42]))));
493 });
494 }
495
496 void testNoChangeSet(Set original, Set wrapped, String name) {
497 Set copy = new Set.from(original);
498
499 testThrows(name, thunk) {
500 test(name, () {
501 expect(thunk, throwsUnsupportedError);
502 // No modifications happened.
503 expect(original, equals(copy));
504 });
505 }
506
507 testThrows("$name - add throws", () {
508 wrapped.add(42);
509 });
510
511 testThrows("$name - addAll throws", () {
512 wrapped.addAll([42]);
513 });
514
515 testThrows("$name - addAll empty throws", () {
516 wrapped.addAll([]);
517 });
518
519 testThrows("$name - remove throws", () {
520 wrapped.remove(42);
521 });
522
523 testThrows("$name - removeAll throws", () {
524 wrapped.removeAll([42]);
525 });
526
527 testThrows("$name - removeAll empty throws", () {
528 wrapped.removeAll([]);
529 });
530
531 testThrows("$name - retainAll throws", () {
532 wrapped.retainAll([42]);
533 });
534
535 testThrows("$name - removeWhere throws", () {
536 wrapped.removeWhere((_) => false);
537 });
538
539 testThrows("$name - retainWhere throws", () {
540 wrapped.retainWhere((_) => true);
541 });
542
543 testThrows("$name - clear throws", () {
544 wrapped.clear();
545 });
546 }
547
548 void testReadMap(Map original, Map wrapped, String name) {
549 test("$name length", () {
550 expect(wrapped.length, equals(original.length));
551 });
552
553 test("$name isEmpty", () {
554 expect(wrapped.isEmpty, equals(original.isEmpty));
555 });
556
557 test("$name isNotEmpty", () {
558 expect(wrapped.isNotEmpty, equals(original.isNotEmpty));
559 });
560
561 test("$name operator[]", () {
562 expect(wrapped[0], equals(original[0]));
563 expect(wrapped[999], equals(original[999]));
564 });
565
566 test("$name containsKey", () {
567 expect(wrapped.containsKey(0), equals(original.containsKey(0)));
568 expect(wrapped.containsKey(999), equals(original.containsKey(999)));
569 });
570
571 test("$name containsValue", () {
572 expect(wrapped.containsValue(0), equals(original.containsValue(0)));
573 expect(wrapped.containsValue(999), equals(original.containsValue(999)));
574 });
575
576 test("$name forEach", () {
577 int origCnt = 0;
578 int wrapCnt = 0;
579 wrapped.forEach((k, v) { wrapCnt += 1 << k + 3 * v; });
580 original.forEach((k, v) { origCnt += 1 << k + 3 * v; });
581 expect(wrapCnt, equals(origCnt));
582 });
583
584 test("$name keys", () {
585 expect(wrapped.keys, equals(original.keys));
586 });
587
588 test("$name values", () {
589 expect(wrapped.values, equals(original.values));
590 });
591 }
592
593 testNoChangeMap(Map original, Map wrapped, String name) {
594 Map copy = new Map.from(original);
595
596 testThrows(name, thunk) {
597 test(name, () {
598 expect(thunk, throwsUnsupportedError);
599 // No modifications happened.
600 expect(original, equals(copy));
601 });
602 }
603
604 testThrows("$name operator[]= throws", () {
605 wrapped[0] = 42;
606 });
607
608 testThrows("$name putIfAbsent throws", () {
609 wrapped.putIfAbsent(0, () => 42);
610 });
611
612 testThrows("$name addAll throws", () {
613 wrapped.addAll(new Map()..[42] = 42);
614 });
615
616 testThrows("$name addAll empty throws", () {
617 wrapped.addAll(new Map());
618 });
619
620 testThrows("$name remove throws", () {
621 wrapped.remove(0);
622 });
623
624 testThrows("$name clear throws", () {
625 wrapped.clear();
626 });
627 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698