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