| 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 IterableBase; | 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<E> extends IterableBase<E> { | 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<E> get iterator => new CallbackIterator(this); | 20 Iterator<int> get iterator => new CallbackIterator(this); |
| 21 } | 21 } |
| 22 | 22 |
| 23 class TestIterable<E> extends TestIterableBase<E> { | 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 // Implement Set for private EfficientLength interface. |
| 30 @proxy // Avoid warnings because we don't actually implement Set. | 30 class EfficientTestIterable extends TestIterableBase |
| 31 class EfficientTestIterable<E> extends TestIterableBase<E> | 31 implements Set<int> { |
| 32 implements Set<E> { | |
| 33 EfficientTestIterable(length, count, [callbackIndex = -1, callback]) | 32 EfficientTestIterable(length, count, [callbackIndex = -1, callback]) |
| 34 : super(length, count, callbackIndex, callback); | 33 : super(length, count, callbackIndex, callback); |
| 34 // Avoid warnings because we don't actually implement Set. |
| 35 noSuchMethod(i) => super.noSuchMethod(i); |
| 35 } | 36 } |
| 36 | 37 |
| 37 class CallbackIterator<E> implements Iterator<E> { | 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 EfficientLength 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<E>(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<E>(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 EfficientLength 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<E>(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 } |
| 93 | 94 |
| 94 { | 95 { |
| 95 var l = []; | 96 var l = []; |
| 96 var ci = new EfficientTestIterable<E>(257, 257, 20, () { | 97 var ci = new EfficientTestIterable(257, 257, 20, () { |
| 97 l.length = 0; | 98 l.length = 0; |
| 98 }); | 99 }); |
| 99 Expect.throws(() { | 100 Expect.throws(() { |
| 100 l.addAll(ci); | 101 l.addAll(ci); |
| 101 }, (e) => e is ConcurrentModificationError); | 102 }, (e) => e is ConcurrentModificationError); |
| 102 } | 103 } |
| 103 | 104 |
| 104 { | 105 { |
| 105 // Length 50, only 25 elements. | 106 // Length 50, only 25 elements. |
| 106 var l = []; | 107 var l = []; |
| 107 var ci = new EfficientTestIterable<E>(500, 250); | 108 var ci = new EfficientTestIterable(500, 250); |
| 108 l.addAll(ci); | 109 l.addAll(ci); |
| 109 Expect.listEquals(new List.generate(250, (x)=>x), l); | 110 Expect.listEquals(new List.generate(250, (x)=>x), l); |
| 110 } | 111 } |
| 111 | 112 |
| 112 { | 113 { |
| 113 // Length 25, but 50 elements. | 114 // Length 25, but 50 elements. |
| 114 var l = []; | 115 var l = []; |
| 115 var ci = new EfficientTestIterable<E>(250, 500); | 116 var ci = new EfficientTestIterable(250, 500); |
| 116 l.addAll(ci); | 117 l.addAll(ci); |
| 117 Expect.listEquals(new List.generate(500, (x)=>x), l); | 118 Expect.listEquals(new List.generate(500, (x)=>x), l); |
| 118 } | 119 } |
| 119 | 120 |
| 120 { | 121 { |
| 121 // Adding to yourself. | 122 // Adding to yourself. |
| 122 var l = [1]; | 123 var l = [1]; |
| 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 |