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

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

Issue 656773002: Add concurrent modification check to ListMixin/IterableMixin.reduce. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 2 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) 2014, 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 "package:expect/expect.dart";
6 import 'dart:collection';
7 import 'dart:typed_data';
8
9 class MyList extends ListBase {
10 List list;
11 MyList(this.list);
12
13 get length => list.length;
14 set length(val) { list.length = val; }
15
16 operator [](index) => list[index];
17 operator []=(index, val) => list[index] = val;
18 }
19
20 id(x) => x;
21
22 main() {
23 for (var iterable in [
24 const [1, 2, 3],
25 [1, 2, 3],
26 new List(3)..[0] = 1..[1] = 2..[2] = 3,
27 {1: 1, 2: 2, 3: 3}.keys,
28 {1: 1, 2: 2, 3: 3}.values,
29 new Iterable.generate(3, (x) => x + 1),
30 new List.generate(3, (x) => x + 1),
31 [0, 1, 2, 3].where((x) => x > 0),
32 [0, 1, 2].map((x) => x + 1),
33 [[1, 2], [3]].expand(id),
34 [3, 2, 1].reversed,
35 [0, 1, 2, 3].skip(1),
36 [1, 2, 3, 4].take(3),
37 new Uint8List(3)..[0] = 1..[1] = 2..[2] = 3,
38 (new HashMap()..[1] = 1..[2] = 2..[3] = 3).keys,
39 (new HashMap()..[1] = 1..[2] = 2..[3] = 3).values,
40 (new SplayTreeMap()..[1] = 0..[2] = 0..[3] = 0).keys,
41 (new SplayTreeMap()..[0] = 1..[1] = 2..[2] = 3).values,
42 new HashSet()..add(1)..add(2)..add(3),
43 new LinkedHashSet()..add(1)..add(2)..add(3),
44 new SplayTreeSet()..add(1)..add(2)..add(3),
45 "\x01\x02\x03".codeUnits,
46 "\x01\x02\x03".runes,
47 new MyList([1, 2, 3]),
48 ]) {
49 int callCount = 0;
50 var result = iterable.fold(0, (x, y) { callCount++; return x + y; });
51 Expect.equals(6, result, "${iterable.runtimeType}");
52 Expect.equals(3, callCount);
53 }
54
55 // Empty iterables are allowed.
56 for (var iterable in [
57 const [],
58 [],
59 new List(0),
60 {}.keys,
61 {}.values,
62 new Iterable.generate(0, (x) => x + 1),
63 new List.generate(0, (x) => x + 1),
64 [0, 1, 2, 3].where((x) => false),
65 [].map((x) => x + 1),
66 [[], []].expand(id),
67 [].reversed,
68 [0, 1, 2, 3].skip(4),
69 [1, 2, 3, 4].take(0),
70 new Uint8List(0),
71 (new HashMap()).keys,
72 (new HashMap()).values,
73 (new SplayTreeMap()).keys,
74 (new SplayTreeMap()).values,
75 new HashSet(),
76 new LinkedHashSet(),
77 new SplayTreeSet(),
78 "".codeUnits,
79 "".runes,
80 new MyList([]),
81 ]) {
82 Expect.equals(42, iterable.fold(42, (x, y) => throw "Unreachable"));
83 }
84
85 // Singleton iterables are calling reduce function.
86 for (var iterable in [
87 const [1],
88 [1],
89 new List(1)..[0] = 1,
90 {1: 1}.keys,
91 {1: 1}.values,
92 new Iterable.generate(1, (x) => x + 1),
93 new List.generate(1, (x) => x + 1),
94 [0, 1, 2, 3].where((x) => x == 1),
95 [0].map((x) => x + 1),
96 [[], [1]].expand(id),
97 [1].reversed,
98 [0, 1].skip(1),
99 [1, 2, 3, 4].take(1),
100 new Uint8List(1)..[0] = 1,
101 (new HashMap()..[1] = 0).keys,
102 (new HashMap()..[0] = 1).values,
103 (new SplayTreeMap()..[1] = 0).keys,
104 (new SplayTreeMap()..[0] = 1).values,
105 new HashSet()..add(1),
106 new LinkedHashSet()..add(1),
107 new SplayTreeSet()..add(1),
108 "\x01".codeUnits,
109 "\x01".runes,
110 new MyList([1]),
111 ]) {
112 Expect.equals(43, iterable.fold(42, (x, y) => x + y));
113 }
114
115 // Concurrent modifications not allowed.
116 testModification(base, modify, transform) {
117 var iterable = transform(base);
118 Expect.throws(() {
119 iterable.fold(0, (x, y) {
120 modify(base);
121 return x + y;
122 });
123 }, (e) => e is ConcurrentModificationError);
124 }
125
126 void add4(collection) { collection.add(4); }
127 void put4(map) { map[4] = 4; }
128
129 testModification([1, 2, 3], add4, id);
130 testModification(new HashSet()..add(1)..add(2)..add(3), add4, id);
131 testModification(new LinkedHashSet()..add(1)..add(2)..add(3), add4, id);
132 testModification(new SplayTreeSet()..add(1)..add(2)..add(3), add4, id);
133 testModification(new MyList([1, 2, 3]), add4, id);
134
135 testModification([0, 1, 2, 3], add4, (x) => x.where((x) => x > 0));
136 testModification([0, 1, 2], add4, (x) => x.map((x) => x + 1));
137 testModification([[1, 2], [3]], add4, (x) => x.expand((x) => x));
138 testModification([3, 2, 1], add4, (x) => x.reversed);
139 testModification({1: 1, 2: 2, 3: 3}, put4, (x) => x.keys);
140 testModification({1: 1, 2: 2, 3: 3}, put4, (x) => x.values);
141 var hashMap = new HashMap()..[1] = 1..[2] = 2..[3] = 3;
142 testModification(hashMap, put4, (x) => x.keys);
143 hashMap = new HashMap()..[1] = 1..[2] = 2..[3] = 3;
144 testModification(hashMap, put4, (x) => x.values);
145 var splayMap = new SplayTreeMap()..[1] = 1..[2] = 2..[3] = 3;
146 testModification(splayMap, put4, (x) => x.keys);
147 splayMap = new SplayTreeMap()..[1] = 1..[2] = 2..[3] = 3;
148 testModification(splayMap, put4, (x) => x.values);
149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698