OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 "package:expect/expect.dart"; | 5 import "package:expect/expect.dart"; |
6 import 'dart:collection'; | 6 import 'dart:collection'; |
7 import 'dart:typed_data'; | 7 import 'dart:typed_data'; |
8 | 8 |
9 class MyList extends ListBase { | 9 class MyList extends ListBase { |
10 List list; | 10 List list; |
11 MyList(this.list); | 11 MyList(this.list); |
12 | 12 |
13 get length => list.length; | 13 get length => list.length; |
14 set length(val) { | 14 set length(val) { |
15 list.length = val; | 15 list.length = val; |
16 } | 16 } |
17 | 17 |
18 operator [](index) => list[index]; | 18 operator [](index) => list[index]; |
19 operator []=(index, val) => list[index] = val; | 19 operator []=(index, val) => list[index] = val; |
20 } | 20 } |
21 | 21 |
22 id(x) => x; | 22 id (x) => x; |
23 | 23 |
24 main() { | 24 main() { |
25 for (var iterable in [ | 25 for (dynamic iterable in [ |
26 const [1, 2, 3], | 26 const [1, 2, 3], |
27 [1, 2, 3], | 27 [1, 2, 3], |
28 new List(3) | 28 new List(3) |
29 ..[0] = 1 | 29 ..[0] = 1 |
30 ..[1] = 2 | 30 ..[1] = 2 |
31 ..[2] = 3, | 31 ..[2] = 3, |
32 {1: 1, 2: 2, 3: 3}.keys, | 32 {1: 1, 2: 2, 3: 3}.keys, |
33 {1: 1, 2: 2, 3: 3}.values, | 33 {1: 1, 2: 2, 3: 3}.values, |
34 new Iterable.generate(3, (x) => x + 1), | 34 new Iterable.generate(3, (x) => x + 1), |
35 new List.generate(3, (x) => x + 1), | 35 new List.generate(3, (x) => x + 1), |
36 [0, 1, 2, 3].where((x) => x > 0), | 36 [0, 1, 2, 3].where((x) => x > 0), |
37 [0, 1, 2].map((x) => x + 1), | 37 [0, 1, 2].map((x) => x + 1), |
38 [ | 38 [ //# 01: ok |
39 [1, 2], | 39 [1, 2], //# 01: ok |
40 [3] | 40 [3] //# 01: ok |
41 ].expand(id), | 41 ].expand(id), //# 01: ok |
42 [3, 2, 1].reversed, | 42 [3, 2, 1].reversed, |
43 [0, 1, 2, 3].skip(1), | 43 [0, 1, 2, 3].skip(1), |
44 [1, 2, 3, 4].take(3), | 44 [1, 2, 3, 4].take(3), |
45 new Uint8List(3) | 45 new Uint8List(3) |
46 ..[0] = 1 | 46 ..[0] = 1 |
47 ..[1] = 2 | 47 ..[1] = 2 |
48 ..[2] = 3, | 48 ..[2] = 3, |
49 (new HashMap() | 49 (new HashMap() |
50 ..[1] = 1 | 50 ..[1] = 1 |
51 ..[2] = 2 | 51 ..[2] = 2 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 for (var iterable in [ | 86 for (var iterable in [ |
87 const [], | 87 const [], |
88 [], | 88 [], |
89 new List(0), | 89 new List(0), |
90 {}.keys, | 90 {}.keys, |
91 {}.values, | 91 {}.values, |
92 new Iterable.generate(0, (x) => x + 1), | 92 new Iterable.generate(0, (x) => x + 1), |
93 new List.generate(0, (x) => x + 1), | 93 new List.generate(0, (x) => x + 1), |
94 [0, 1, 2, 3].where((x) => false), | 94 [0, 1, 2, 3].where((x) => false), |
95 [].map((x) => x + 1), | 95 [].map((x) => x + 1), |
96 [[], []].expand(id), | 96 [[], []].expand(id), //# 01: ok |
97 [].reversed, | 97 [].reversed, |
98 [0, 1, 2, 3].skip(4), | 98 [0, 1, 2, 3].skip(4), |
99 [1, 2, 3, 4].take(0), | 99 [1, 2, 3, 4].take(0), |
100 new Uint8List(0), | 100 new Uint8List(0), |
101 (new HashMap()).keys, | 101 (new HashMap()).keys, |
102 (new HashMap()).values, | 102 (new HashMap()).values, |
103 (new SplayTreeMap()).keys, | 103 (new SplayTreeMap()).keys, |
104 (new SplayTreeMap()).values, | 104 (new SplayTreeMap()).values, |
105 new HashSet(), | 105 new HashSet(), |
106 new LinkedHashSet(), | 106 new LinkedHashSet(), |
107 new SplayTreeSet(), | 107 new SplayTreeSet(), |
108 "".codeUnits, | 108 "".codeUnits, |
109 "".runes, | 109 "".runes, |
110 new MyList([]), | 110 new MyList([]), |
111 ]) { | 111 ]) { |
112 Expect.equals(42, iterable.fold(42, (x, y) => throw "Unreachable")); | 112 Expect.equals(42, iterable.fold(42, (x, y) => throw "Unreachable")); |
113 } | 113 } |
114 | 114 |
115 // Singleton iterables are calling reduce function. | 115 // Singleton iterables are calling reduce function. |
116 for (var iterable in [ | 116 for (dynamic iterable in [ |
117 const [1], | 117 const [1], |
118 [1], | 118 [1], |
119 new List(1)..[0] = 1, | 119 new List(1)..[0] = 1, |
120 {1: 1}.keys, | 120 {1: 1}.keys, |
121 {1: 1}.values, | 121 {1: 1}.values, |
122 new Iterable.generate(1, (x) => x + 1), | 122 new Iterable.generate(1, (x) => x + 1), |
123 new List.generate(1, (x) => x + 1), | 123 new List.generate(1, (x) => x + 1), |
124 [0, 1, 2, 3].where((x) => x == 1), | 124 [0, 1, 2, 3].where((x) => x == 1), |
125 [0].map((x) => x + 1), | 125 [0].map((x) => x + 1), |
126 [ | 126 [ //# 01: ok |
127 [], | 127 [], //# 01: ok |
128 [1] | 128 [1] //# 01: ok |
129 ].expand(id), | 129 ].expand(id), //# 01: ok |
130 [1].reversed, | 130 [1].reversed, |
131 [0, 1].skip(1), | 131 [0, 1].skip(1), |
132 [1, 2, 3, 4].take(1), | 132 [1, 2, 3, 4].take(1), |
133 new Uint8List(1)..[0] = 1, | 133 new Uint8List(1)..[0] = 1, |
134 (new HashMap()..[1] = 0).keys, | 134 (new HashMap()..[1] = 0).keys, |
135 (new HashMap()..[0] = 1).values, | 135 (new HashMap()..[0] = 1).values, //# 02: ok |
136 (new SplayTreeMap()..[1] = 0).keys, | 136 (new SplayTreeMap()..[1] = 0).keys, |
137 (new SplayTreeMap()..[0] = 1).values, | 137 (new SplayTreeMap()..[0] = 1).values, //# 02: ok |
138 new HashSet()..add(1), | 138 new HashSet()..add(1), |
139 new LinkedHashSet()..add(1), | 139 new LinkedHashSet()..add(1), |
140 new SplayTreeSet()..add(1), | 140 new SplayTreeSet()..add(1), |
141 "\x01".codeUnits, | 141 "\x01".codeUnits, |
142 "\x01".runes, | 142 "\x01".runes, |
143 new MyList([1]), | 143 new MyList([1]), |
144 ]) { | 144 ]) { |
145 Expect.equals(43, iterable.fold(42, (x, y) => x + y)); | 145 Expect.equals(43, iterable.fold(42, (x, y) => x + y)); |
146 } | 146 } |
147 | 147 |
148 // Concurrent modifications not allowed. | 148 // Concurrent modifications not allowed. |
149 testModification(base, modify, transform) { | 149 testModification(base, modify, transform) { |
150 var iterable = transform(base); | 150 var iterable = transform(base); |
151 Expect.throws(() { | 151 Expect.throws(() { |
152 iterable.fold(0, (x, y) { | 152 iterable.fold(0, (x, y) { |
153 modify(base); | 153 modify(base); |
154 return x + y; | 154 return x + y; |
155 }); | 155 }); |
156 }, (e) => e is ConcurrentModificationError); | 156 }, (e) => e is ConcurrentModificationError); |
157 } | 157 } |
158 | 158 |
159 void add4(collection) { | 159 void add4(collection) { |
160 collection.add(4); | 160 collection.add(4); |
161 } | 161 } |
162 | 162 |
163 void put4(map) { | 163 void put4(map) { |
164 map[4] = 4; | 164 map[4] = 4; |
165 } | 165 } |
166 | 166 |
167 testModification([1, 2, 3], add4, id); | 167 testModification([1, 2, 3], add4, id); //# 02: ok |
168 testModification(new HashSet()..add(1)..add(2)..add(3), add4, id); | 168 testModification(new HashSet()..add(1)..add(2)..add(3), add4, id); //# 02: ok |
169 testModification(new LinkedHashSet()..add(1)..add(2)..add(3), add4, id); | 169 testModification(new LinkedHashSet()..add(1)..add(2)..add(3), add4, id); //# 0
2: ok |
170 testModification(new SplayTreeSet()..add(1)..add(2)..add(3), add4, id); | 170 testModification(new SplayTreeSet()..add(1)..add(2)..add(3), add4, id); //# 02
: ok |
171 testModification(new MyList([1, 2, 3]), add4, id); | 171 testModification(new MyList([1, 2, 3]), add4, id); //# 02: ok |
172 | 172 |
173 testModification([0, 1, 2, 3], add4, (x) => x.where((x) => x > 0)); | 173 testModification([0, 1, 2, 3], add4, (x) => x.where((x) => x > 0)); //# 02: ok |
174 testModification([0, 1, 2], add4, (x) => x.map((x) => x + 1)); | 174 testModification([0, 1, 2], add4, (x) => x.map((x) => x + 1)); //# 02: ok |
175 testModification([ | 175 testModification([ //# 02: ok |
176 [1, 2], | 176 [1, 2], //# 02: ok |
177 [3] | 177 [3] //# 02: ok |
178 ], add4, (x) => x.expand((x) => x)); | 178 ], add4, (x) => x.expand((x) => x)); //# 02: ok |
179 testModification([3, 2, 1], add4, (x) => x.reversed); | 179 testModification([3, 2, 1], add4, (x) => x.reversed); //# 02: ok |
180 testModification({1: 1, 2: 2, 3: 3}, put4, (x) => x.keys); | 180 testModification({1: 1, 2: 2, 3: 3}, put4, (x) => x.keys); |
181 testModification({1: 1, 2: 2, 3: 3}, put4, (x) => x.values); | 181 testModification({1: 1, 2: 2, 3: 3}, put4, (x) => x.values); |
182 var hashMap = new HashMap() | 182 var hashMap = new HashMap() |
183 ..[1] = 1 | 183 ..[1] = 1 |
184 ..[2] = 2 | 184 ..[2] = 2 |
185 ..[3] = 3; | 185 ..[3] = 3; |
186 testModification(hashMap, put4, (x) => x.keys); | 186 testModification(hashMap, put4, (x) => x.keys); |
187 hashMap = new HashMap() | 187 hashMap = new HashMap() |
188 ..[1] = 1 | 188 ..[1] = 1 |
189 ..[2] = 2 | 189 ..[2] = 2 |
190 ..[3] = 3; | 190 ..[3] = 3; |
191 testModification(hashMap, put4, (x) => x.values); | 191 testModification(hashMap, put4, (x) => x.values); |
192 var splayMap = new SplayTreeMap() | 192 var splayMap = new SplayTreeMap() |
193 ..[1] = 1 | 193 ..[1] = 1 |
194 ..[2] = 2 | 194 ..[2] = 2 |
195 ..[3] = 3; | 195 ..[3] = 3; |
196 testModification(splayMap, put4, (x) => x.keys); | 196 testModification(splayMap, put4, (x) => x.keys); |
197 splayMap = new SplayTreeMap() | 197 splayMap = new SplayTreeMap() |
198 ..[1] = 1 | 198 ..[1] = 1 |
199 ..[2] = 2 | 199 ..[2] = 2 |
200 ..[3] = 3; | 200 ..[3] = 3; |
201 testModification(splayMap, put4, (x) => x.values); | 201 testModification(splayMap, put4, (x) => x.values); |
202 } | 202 } |
OLD | NEW |