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) { 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 // Test functionality. |
| 24 for (var iterable in [ |
| 25 const [1, 2, 3], |
| 26 [1, 2, 3], |
| 27 new List(3)..[0] = 1..[1] = 2..[2] = 3, |
| 28 {1: 1, 2: 2, 3: 3}.keys, |
| 29 {1: 1, 2: 2, 3: 3}.values, |
| 30 new Iterable.generate(3, (x) => x + 1), |
| 31 new List.generate(3, (x) => x + 1), |
| 32 [0, 1, 2, 3].where((x) => x > 0), |
| 33 [0, 1, 2].map((x) => x + 1), |
| 34 [[1, 2], [3]].expand(id), |
| 35 [3, 2, 1].reversed, |
| 36 [0, 1, 2, 3].skip(1), |
| 37 [1, 2, 3, 4].take(3), |
| 38 new Uint8List(3)..[0] = 1..[1] = 2..[2] = 3, |
| 39 (new HashMap()..[1] = 1..[2] = 2..[3] = 3).keys, |
| 40 (new HashMap()..[1] = 1..[2] = 2..[3] = 3).values, |
| 41 (new SplayTreeMap()..[1] = 0..[2] = 0..[3] = 0).keys, |
| 42 (new SplayTreeMap()..[0] = 1..[1] = 2..[2] = 3).values, |
| 43 new HashSet()..add(1)..add(2)..add(3), |
| 44 new LinkedHashSet()..add(1)..add(2)..add(3), |
| 45 new SplayTreeSet()..add(1)..add(2)..add(3), |
| 46 "\x01\x02\x03".codeUnits, |
| 47 "\x01\x02\x03".runes, |
| 48 new MyList([1, 2, 3]), |
| 49 ]) { |
| 50 int callCount = 0; |
| 51 var result = iterable.reduce((x, y) { callCount++; return x + y; }); |
| 52 Expect.equals(6, result, "${iterable.runtimeType}"); |
| 53 Expect.equals(2, callCount); |
| 54 } |
| 55 |
| 56 // Empty iterables not allowed. |
| 57 for (var iterable in [ |
| 58 const [], |
| 59 [], |
| 60 new List(0), |
| 61 {}.keys, |
| 62 {}.values, |
| 63 new Iterable.generate(0, (x) => x + 1), |
| 64 new List.generate(0, (x) => x + 1), |
| 65 [0, 1, 2, 3].where((x) => false), |
| 66 [].map((x) => x + 1), |
| 67 [[], []].expand(id), |
| 68 [].reversed, |
| 69 [0, 1, 2, 3].skip(4), |
| 70 [1, 2, 3, 4].take(0), |
| 71 new Uint8List(0), |
| 72 (new HashMap()).keys, |
| 73 (new HashMap()).values, |
| 74 (new SplayTreeMap()).keys, |
| 75 (new SplayTreeMap()).values, |
| 76 new HashSet(), |
| 77 new LinkedHashSet(), |
| 78 new SplayTreeSet(), |
| 79 "".codeUnits, |
| 80 "".runes, |
| 81 new MyList([]), |
| 82 ]) { |
| 83 Expect.throws(() { iterable.reduce((x, y) => throw "Unreachable"); }, |
| 84 (e) => e is StateError); |
| 85 } |
| 86 |
| 87 // Singleton iterables not calling reduce function. |
| 88 for (var iterable in [ |
| 89 const [1], |
| 90 [1], |
| 91 new List(1)..[0] = 1, |
| 92 {1: 1}.keys, |
| 93 {1: 1}.values, |
| 94 new Iterable.generate(1, (x) => x + 1), |
| 95 new List.generate(1, (x) => x + 1), |
| 96 [0, 1, 2, 3].where((x) => x == 1), |
| 97 [0].map((x) => x + 1), |
| 98 [[], [1]].expand(id), |
| 99 [1].reversed, |
| 100 [0, 1].skip(1), |
| 101 [1, 2, 3, 4].take(1), |
| 102 new Uint8List(1)..[0] = 1, |
| 103 (new HashMap()..[1] = 0).keys, |
| 104 (new HashMap()..[0] = 1).values, |
| 105 (new SplayTreeMap()..[1] = 0).keys, |
| 106 (new SplayTreeMap()..[0] = 1).values, |
| 107 new HashSet()..add(1), |
| 108 new LinkedHashSet()..add(1), |
| 109 new SplayTreeSet()..add(1), |
| 110 "\x01".codeUnits, |
| 111 "\x01".runes, |
| 112 new MyList([1]), |
| 113 ]) { |
| 114 Expect.equals(1, iterable.reduce((x, y) => throw "Unreachable")); |
| 115 } |
| 116 |
| 117 // Concurrent modifications not allowed. |
| 118 testModification(base, modify, transform) { |
| 119 var iterable = transform(base); |
| 120 Expect.throws(() { |
| 121 iterable.reduce((x, y) { |
| 122 modify(base); |
| 123 return x + y; |
| 124 }); |
| 125 }, (e) => e is ConcurrentModificationError); |
| 126 } |
| 127 |
| 128 void add4(collection) { collection.add(4); } |
| 129 void put4(map) { map[4] = 4; } |
| 130 |
| 131 testModification([1, 2, 3], add4, id); |
| 132 testModification(new HashSet()..add(1)..add(2)..add(3), add4, id); |
| 133 testModification(new LinkedHashSet()..add(1)..add(2)..add(3), add4, id); |
| 134 testModification(new SplayTreeSet()..add(1)..add(2)..add(3), add4, id); |
| 135 testModification(new MyList([1, 2, 3]), add4, id); |
| 136 |
| 137 testModification([0, 1, 2, 3], add4, (x) => x.where((x) => x > 0)); |
| 138 testModification([0, 1, 2], add4, (x) => x.map((x) => x + 1)); |
| 139 testModification([[1, 2], [3]], add4, (x) => x.expand((x) => x)); |
| 140 testModification([3, 2, 1], add4, (x) => x.reversed); |
| 141 testModification({1: 1, 2: 2, 3: 3}, put4, (x) => x.keys); |
| 142 testModification({1: 1, 2: 2, 3: 3}, put4, (x) => x.values); |
| 143 var hashMap = new HashMap()..[1] = 1..[2] = 2..[3] = 3; |
| 144 testModification(hashMap, put4, (x) => x.keys); |
| 145 hashMap = new HashMap()..[1] = 1..[2] = 2..[3] = 3; |
| 146 testModification(hashMap, put4, (x) => x.values); |
| 147 var splayMap = new SplayTreeMap()..[1] = 1..[2] = 2..[3] = 3; |
| 148 testModification(splayMap, put4, (x) => x.keys); |
| 149 splayMap = new SplayTreeMap()..[1] = 1..[2] = 2..[3] = 3; |
| 150 testModification(splayMap, put4, (x) => x.values); |
| 151 } |
OLD | NEW |