OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Sanity check on the growing behavior of a growable list. | 5 // Sanity check on the growing behavior of a growable list. |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import "dart:collection" show EfficientLengthIterable; | 8 import "dart:collection" show IterableBase; |
9 | 9 |
10 // Iterable generating numbers in range [0..count). | 10 // Iterable generating numbers in range [0..count). |
11 // May perform callback at some point underways. | 11 // May perform callback at some point underways. |
12 class TestIterableBase extends Iterable<int> { | 12 class TestIterableBase extends IterableBase<int> { |
13 final int length; | 13 final int length; |
14 final int count; | 14 final int count; |
15 // call [callback] if generating callbackIndex. | 15 // call [callback] if generating callbackIndex. |
16 final int callbackIndex; | 16 final int callbackIndex; |
17 final Function callback; | 17 final Function callback; |
18 TestIterableBase(this.length, this.count, | 18 TestIterableBase(this.length, this.count, |
19 this.callbackIndex, this.callback); | 19 this.callbackIndex, this.callback); |
20 Iterator<int> get iterator => new CallbackIterator(this); | 20 Iterator<int> get iterator => new CallbackIterator(this); |
21 } | 21 } |
22 | 22 |
23 class TestIterable extends TestIterableBase { | 23 class TestIterable extends TestIterableBase { |
24 TestIterable(count, [callbackIndex = -1, callback]) | 24 TestIterable(count, [callbackIndex = -1, callback]) |
25 : super(-1, count, callbackIndex, callback); | 25 : super(-1, count, callbackIndex, callback); |
26 int get length => throw "SHOULD NOT BE CALLED"; | 26 int get length => throw "SHOULD NOT BE CALLED"; |
27 } | 27 } |
28 | 28 |
| 29 // Implement Set for private EfficientLength interface. |
29 class EfficientTestIterable extends TestIterableBase | 30 class EfficientTestIterable extends TestIterableBase |
30 implements EfficientLengthIterable<int> { | 31 implements Set<int> { |
31 EfficientTestIterable(length, count, [callbackIndex = -1, callback]) | 32 EfficientTestIterable(length, count, [callbackIndex = -1, callback]) |
32 : super(length, count, callbackIndex, callback); | 33 : super(length, count, callbackIndex, callback); |
33 // Avoid warnings because we don't actually implement Set. | 34 // Avoid warnings because we don't actually implement Set. |
34 noSuchMethod(i) => super.noSuchMethod(i); | 35 noSuchMethod(i) => super.noSuchMethod(i); |
35 } | 36 } |
36 | 37 |
37 class CallbackIterator implements Iterator<int> { | 38 class CallbackIterator implements Iterator<int> { |
38 TestIterableBase _iterable; | 39 TestIterableBase _iterable; |
39 int _current = null; | 40 int _current = null; |
40 int _nextIndex = 0; | 41 int _nextIndex = 0; |
41 CallbackIterator(this._iterable); | 42 CallbackIterator(this._iterable); |
42 bool moveNext() { | 43 bool moveNext() { |
43 if (_nextIndex >= _iterable.count) { | 44 if (_nextIndex >= _iterable.count) { |
44 _current = null; | 45 _current = null; |
45 return false; | 46 return false; |
46 } | 47 } |
47 _current = _nextIndex; | 48 _current = _nextIndex; |
48 _nextIndex++; | 49 _nextIndex++; |
49 if (_current == _iterable.callbackIndex) { | 50 if (_current == _iterable.callbackIndex) { |
50 _iterable.callback(); | 51 _iterable.callback(); |
51 } | 52 } |
52 return true; | 53 return true; |
53 } | 54 } |
54 int get current => _current; | 55 int get current => _current; |
55 } | 56 } |
56 | 57 |
57 | 58 |
58 void main() { | 59 void main() { |
59 // Without EfficientLengthIterable interface | 60 // Without EfficientLength interface |
60 { | 61 { |
61 // Change length of list after 20 additions. | 62 // Change length of list after 20 additions. |
62 var l = []; | 63 var l = []; |
63 var ci = new TestIterable(257, 200, () { | 64 var ci = new TestIterable(257, 200, () { |
64 l.add("X"); | 65 l.add("X"); |
65 }); | 66 }); |
66 Expect.throws(() { | 67 Expect.throws(() { |
67 l.addAll(ci); | 68 l.addAll(ci); |
68 }, (e) => e is ConcurrentModificationError); | 69 }, (e) => e is ConcurrentModificationError); |
69 } | 70 } |
70 | 71 |
71 { | 72 { |
72 // Change length of list after 20 additions. | 73 // Change length of list after 20 additions. |
73 var l = []; | 74 var l = []; |
74 var ci = new TestIterable(257, 200, () { | 75 var ci = new TestIterable(257, 200, () { |
75 l.length = 0; | 76 l.length = 0; |
76 }); | 77 }); |
77 Expect.throws(() { | 78 Expect.throws(() { |
78 l.addAll(ci); | 79 l.addAll(ci); |
79 }, (e) => e is ConcurrentModificationError); | 80 }, (e) => e is ConcurrentModificationError); |
80 } | 81 } |
81 | 82 |
82 // With EfficientLengthIterable interface (uses length). | 83 // With EfficientLength interface (uses length). |
83 { | 84 { |
84 // Change length of list after 20 additions. | 85 // Change length of list after 20 additions. |
85 var l = []; | 86 var l = []; |
86 var ci = new EfficientTestIterable(257, 257, 20, () { | 87 var ci = new EfficientTestIterable(257, 257, 20, () { |
87 l.add("X"); | 88 l.add("X"); |
88 }); | 89 }); |
89 Expect.throws(() { | 90 Expect.throws(() { |
90 l.addAll(ci); | 91 l.addAll(ci); |
91 }, (e) => e is ConcurrentModificationError); | 92 }, (e) => e is ConcurrentModificationError); |
92 } | 93 } |
(...skipping 30 matching lines...) Expand all Loading... |
123 Expect.throws(() { l.addAll(l); }, (e) => e is ConcurrentModificationError); | 124 Expect.throws(() { l.addAll(l); }, (e) => e is ConcurrentModificationError); |
124 } | 125 } |
125 | 126 |
126 { | 127 { |
127 // Adding to yourself. | 128 // Adding to yourself. |
128 var l = [1, 2, 3]; | 129 var l = [1, 2, 3]; |
129 Expect.throws(() { l.addAll(l); }, (e) => e is ConcurrentModificationError); | 130 Expect.throws(() { l.addAll(l); }, (e) => e is ConcurrentModificationError); |
130 } | 131 } |
131 } | 132 } |
132 | 133 |
OLD | NEW |