OLD | NEW |
| (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) { | |
15 list.length = val; | |
16 } | |
17 | |
18 operator [](index) => list[index]; | |
19 operator []=(index, val) => list[index] = val; | |
20 } | |
21 | |
22 id(x) => x; | |
23 | |
24 main() { | |
25 for (var iterable in [ | |
26 const [1, 2, 3], | |
27 [1, 2, 3], | |
28 new List(3) | |
29 ..[0] = 1 | |
30 ..[1] = 2 | |
31 ..[2] = 3, | |
32 {1: 1, 2: 2, 3: 3}.keys, | |
33 {1: 1, 2: 2, 3: 3}.values, | |
34 new Iterable.generate(3, (x) => x + 1), | |
35 new List.generate(3, (x) => x + 1), | |
36 [0, 1, 2, 3].where((x) => x > 0), | |
37 [0, 1, 2].map((x) => x + 1), | |
38 [ | |
39 [1, 2], | |
40 [3] | |
41 ].expand(id), | |
42 [3, 2, 1].reversed, | |
43 [0, 1, 2, 3].skip(1), | |
44 [1, 2, 3, 4].take(3), | |
45 new Uint8List(3) | |
46 ..[0] = 1 | |
47 ..[1] = 2 | |
48 ..[2] = 3, | |
49 (new HashMap() | |
50 ..[1] = 1 | |
51 ..[2] = 2 | |
52 ..[3] = 3) | |
53 .keys, | |
54 (new HashMap() | |
55 ..[1] = 1 | |
56 ..[2] = 2 | |
57 ..[3] = 3) | |
58 .values, | |
59 (new SplayTreeMap() | |
60 ..[1] = 0 | |
61 ..[2] = 0 | |
62 ..[3] = 0) | |
63 .keys, | |
64 (new SplayTreeMap() | |
65 ..[0] = 1 | |
66 ..[1] = 2 | |
67 ..[2] = 3) | |
68 .values, | |
69 new HashSet()..add(1)..add(2)..add(3), | |
70 new LinkedHashSet()..add(1)..add(2)..add(3), | |
71 new SplayTreeSet()..add(1)..add(2)..add(3), | |
72 "\x01\x02\x03".codeUnits, | |
73 "\x01\x02\x03".runes, | |
74 new MyList([1, 2, 3]), | |
75 ]) { | |
76 int callCount = 0; | |
77 var result = iterable.fold(0, (x, y) { | |
78 callCount++; | |
79 return x + y; | |
80 }); | |
81 Expect.equals(6, result, "${iterable.runtimeType}"); | |
82 Expect.equals(3, callCount); | |
83 } | |
84 | |
85 // Empty iterables are allowed. | |
86 for (var iterable in [ | |
87 const [], | |
88 [], | |
89 new List(0), | |
90 {}.keys, | |
91 {}.values, | |
92 new Iterable.generate(0, (x) => x + 1), | |
93 new List.generate(0, (x) => x + 1), | |
94 [0, 1, 2, 3].where((x) => false), | |
95 [].map((x) => x + 1), | |
96 [[], []].expand(id), | |
97 [].reversed, | |
98 [0, 1, 2, 3].skip(4), | |
99 [1, 2, 3, 4].take(0), | |
100 new Uint8List(0), | |
101 (new HashMap()).keys, | |
102 (new HashMap()).values, | |
103 (new SplayTreeMap()).keys, | |
104 (new SplayTreeMap()).values, | |
105 new HashSet(), | |
106 new LinkedHashSet(), | |
107 new SplayTreeSet(), | |
108 "".codeUnits, | |
109 "".runes, | |
110 new MyList([]), | |
111 ]) { | |
112 Expect.equals(42, iterable.fold(42, (x, y) => throw "Unreachable")); | |
113 } | |
114 | |
115 // Singleton iterables are calling reduce function. | |
116 for (var iterable in [ | |
117 const [1], | |
118 [1], | |
119 new List(1)..[0] = 1, | |
120 {1: 1}.keys, | |
121 {1: 1}.values, | |
122 new Iterable.generate(1, (x) => x + 1), | |
123 new List.generate(1, (x) => x + 1), | |
124 [0, 1, 2, 3].where((x) => x == 1), | |
125 [0].map((x) => x + 1), | |
126 [ | |
127 [], | |
128 [1] | |
129 ].expand(id), | |
130 [1].reversed, | |
131 [0, 1].skip(1), | |
132 [1, 2, 3, 4].take(1), | |
133 new Uint8List(1)..[0] = 1, | |
134 (new HashMap()..[1] = 0).keys, | |
135 (new HashMap()..[0] = 1).values, | |
136 (new SplayTreeMap()..[1] = 0).keys, | |
137 (new SplayTreeMap()..[0] = 1).values, | |
138 new HashSet()..add(1), | |
139 new LinkedHashSet()..add(1), | |
140 new SplayTreeSet()..add(1), | |
141 "\x01".codeUnits, | |
142 "\x01".runes, | |
143 new MyList([1]), | |
144 ]) { | |
145 Expect.equals(43, iterable.fold(42, (x, y) => x + y)); | |
146 } | |
147 | |
148 // Concurrent modifications not allowed. | |
149 testModification(base, modify, transform) { | |
150 var iterable = transform(base); | |
151 Expect.throws(() { | |
152 iterable.fold(0, (x, y) { | |
153 modify(base); | |
154 return x + y; | |
155 }); | |
156 }, (e) => e is ConcurrentModificationError); | |
157 } | |
158 | |
159 void add4(collection) { | |
160 collection.add(4); | |
161 } | |
162 | |
163 void put4(map) { | |
164 map[4] = 4; | |
165 } | |
166 | |
167 testModification([1, 2, 3], add4, id); | |
168 testModification(new HashSet()..add(1)..add(2)..add(3), add4, id); | |
169 testModification(new LinkedHashSet()..add(1)..add(2)..add(3), add4, id); | |
170 testModification(new SplayTreeSet()..add(1)..add(2)..add(3), add4, id); | |
171 testModification(new MyList([1, 2, 3]), add4, id); | |
172 | |
173 testModification([0, 1, 2, 3], add4, (x) => x.where((x) => x > 0)); | |
174 testModification([0, 1, 2], add4, (x) => x.map((x) => x + 1)); | |
175 testModification([ | |
176 [1, 2], | |
177 [3] | |
178 ], add4, (x) => x.expand((x) => x)); | |
179 testModification([3, 2, 1], add4, (x) => x.reversed); | |
180 testModification({1: 1, 2: 2, 3: 3}, put4, (x) => x.keys); | |
181 testModification({1: 1, 2: 2, 3: 3}, put4, (x) => x.values); | |
182 var hashMap = new HashMap() | |
183 ..[1] = 1 | |
184 ..[2] = 2 | |
185 ..[3] = 3; | |
186 testModification(hashMap, put4, (x) => x.keys); | |
187 hashMap = new HashMap() | |
188 ..[1] = 1 | |
189 ..[2] = 2 | |
190 ..[3] = 3; | |
191 testModification(hashMap, put4, (x) => x.values); | |
192 var splayMap = new SplayTreeMap() | |
193 ..[1] = 1 | |
194 ..[2] = 2 | |
195 ..[3] = 3; | |
196 testModification(splayMap, put4, (x) => x.keys); | |
197 splayMap = new SplayTreeMap() | |
198 ..[1] = 1 | |
199 ..[2] = 2 | |
200 ..[3] = 3; | |
201 testModification(splayMap, put4, (x) => x.values); | |
202 } | |
OLD | NEW |