OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Sanity check on the growing behavior of a growable list. |
| 6 |
| 7 import "package:expect/expect.dart"; |
| 8 import "dart:collection" show IterableBase; |
| 9 |
| 10 // Iterable generating numbers in range [0..count). |
| 11 // May perform callback at some point underways. |
| 12 class TestIterableBase<E> extends IterableBase<E> { |
| 13 final int length; |
| 14 final int count; |
| 15 // call [callback] if generating callbackIndex. |
| 16 final int callbackIndex; |
| 17 final Function callback; |
| 18 TestIterableBase(this.length, this.count, |
| 19 this.callbackIndex, this.callback); |
| 20 Iterator<E> get iterator => new CallbackIterator(this); |
| 21 } |
| 22 |
| 23 class TestIterable<E> extends TestIterableBase<E> { |
| 24 TestIterable(count, [callbackIndex = -1, callback]) |
| 25 : super(-1, count, callbackIndex, callback); |
| 26 int get length => throw "SHOULD NOT BE CALLED"; |
| 27 } |
| 28 |
| 29 // Implement Set for private EfficientLength interface. |
| 30 @proxy // Avoid warnings because we don't actually implement Set. |
| 31 class EfficientTestIterable<E> extends TestIterableBase<E> |
| 32 implements Set<E> { |
| 33 EfficientTestIterable(length, count, [callbackIndex = -1, callback]) |
| 34 : super(length, count, callbackIndex, callback); |
| 35 } |
| 36 |
| 37 class CallbackIterator<E> implements Iterator<E> { |
| 38 TestIterableBase _iterable; |
| 39 int _current = null; |
| 40 int _nextIndex = 0; |
| 41 CallbackIterator(this._iterable); |
| 42 bool moveNext() { |
| 43 if (_nextIndex >= _iterable.count) { |
| 44 _current = null; |
| 45 return false; |
| 46 } |
| 47 _current = _nextIndex; |
| 48 _nextIndex++; |
| 49 if (_current == _iterable.callbackIndex) { |
| 50 _iterable.callback(); |
| 51 } |
| 52 return true; |
| 53 } |
| 54 int get current => _current; |
| 55 } |
| 56 |
| 57 |
| 58 void main() { |
| 59 // Without EfficientLength interface |
| 60 { |
| 61 // Change length of list after 20 additions. |
| 62 var l = []; |
| 63 var ci = new TestIterable<E>(257, 200, () { |
| 64 l.add("X"); |
| 65 }); |
| 66 Expect.throws(() { |
| 67 l.addAll(ci); |
| 68 }, (e) => e is ConcurrentModificationError); |
| 69 } |
| 70 |
| 71 { |
| 72 // Change length of list after 20 additions. |
| 73 var l = []; |
| 74 var ci = new TestIterable<E>(257, 200, () { |
| 75 l.length = 0; |
| 76 }); |
| 77 Expect.throws(() { |
| 78 l.addAll(ci); |
| 79 }, (e) => e is ConcurrentModificationError); |
| 80 } |
| 81 |
| 82 // With EfficientLength interface (uses length). |
| 83 { |
| 84 // Change length of list after 20 additions. |
| 85 var l = []; |
| 86 var ci = new EfficientTestIterable<E>(257, 257, 20, () { |
| 87 l.add("X"); |
| 88 }); |
| 89 Expect.throws(() { |
| 90 l.addAll(ci); |
| 91 }, (e) => e is ConcurrentModificationError); |
| 92 } |
| 93 |
| 94 { |
| 95 var l = []; |
| 96 var ci = new EfficientTestIterable<E>(257, 257, 20, () { |
| 97 l.length = 0; |
| 98 }); |
| 99 Expect.throws(() { |
| 100 l.addAll(ci); |
| 101 }, (e) => e is ConcurrentModificationError); |
| 102 } |
| 103 |
| 104 { |
| 105 // Length 50, only 25 elements. |
| 106 var l = []; |
| 107 var ci = new EfficientTestIterable<E>(500, 250); |
| 108 l.addAll(ci); |
| 109 Expect.listEquals(new List.generate(250, (x)=>x), l); |
| 110 } |
| 111 |
| 112 { |
| 113 // Length 25, but 50 elements. |
| 114 var l = []; |
| 115 var ci = new EfficientTestIterable<E>(250, 500); |
| 116 l.addAll(ci); |
| 117 Expect.listEquals(new List.generate(500, (x)=>x), l); |
| 118 } |
| 119 |
| 120 { |
| 121 // Adding to yourself. |
| 122 var l = [1]; |
| 123 Expect.throws(() { l.addAll(l); }, (e) => e is ConcurrentModificationError); |
| 124 } |
| 125 |
| 126 { |
| 127 // Adding to yourself. |
| 128 var l = [1, 2, 3]; |
| 129 Expect.throws(() { l.addAll(l); }, (e) => e is ConcurrentModificationError); |
| 130 } |
| 131 } |
| 132 |
OLD | NEW |