| 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 | 8 |
| 9 void main() { | 9 void main() { |
| 10 testConstructor(); | 10 testConstructor(); |
| 11 | |
| 12 bool checked = false; | |
| 13 assert((checked = true)); | |
| 14 // Concurrent modification checks are only guaranteed in checked mode. | 11 // Concurrent modification checks are only guaranteed in checked mode. |
| 15 if (checked) testConcurrentModification(); | 12 testConcurrentModification(); |
| 16 } | 13 } |
| 17 | 14 |
| 18 // Iterable generating numbers in range [0..count). | 15 // Iterable generating numbers in range [0..count). |
| 19 // May perform callback at some point underways. | 16 // May perform callback at some point underways. |
| 20 class TestIterableBase extends Iterable<int> { | 17 class TestIterableBase extends Iterable<int> { |
| 21 final int length; | 18 final int length; |
| 22 final int count; | 19 final int count; |
| 23 // call [callback] if generating callbackIndex. | 20 // call [callback] if generating callbackIndex. |
| 24 final int callbackIndex; | 21 final int callbackIndex; |
| 25 final Function callback; | 22 final Function callback; |
| 26 TestIterableBase(this.length, this.count, this.callbackIndex, this.callback); | 23 TestIterableBase(this.length, this.count, this.callbackIndex, this.callback); |
| 27 Iterator<int> get iterator => new CallbackIterator(this); | 24 Iterator<int> get iterator => new CallbackIterator(this); |
| 28 } | 25 } |
| 29 | 26 |
| 30 class TestIterable extends TestIterableBase { | 27 class TestIterable extends TestIterableBase { |
| 31 TestIterable(count, [callbackIndex = -1, callback]) | 28 TestIterable(count, [callbackIndex = -1, callback]) |
| 32 : super(-1, count, callbackIndex, callback); | 29 : super(-1, count, callbackIndex, callback); |
| 33 int get length => throw "SHOULD NOT BE CALLED"; | 30 int get length => throw "SHOULD NOT BE CALLED"; |
| 34 } | 31 } |
| 35 | 32 |
| 36 // Implement Set for private EfficientLength interface. | 33 // Implement Set for private EfficientLengthIterable interface. |
| 37 class EfficientTestIterable extends TestIterableBase implements Set<int> { | 34 class EfficientTestIterable extends TestIterableBase implements Set<int> { |
| 38 EfficientTestIterable(length, count, [callbackIndex = -1, callback]) | 35 EfficientTestIterable(length, count, [callbackIndex = -1, callback]) |
| 39 : super(length, count, callbackIndex, callback); | 36 : super(length, count, callbackIndex, callback); |
| 40 // Avoid warnings because we don't actually implement Set. | 37 // Avoid warnings because we don't actually implement Set. |
| 41 noSuchMethod(i) => super.noSuchMethod(i); | 38 noSuchMethod(i) => super.noSuchMethod(i); |
| 42 } | 39 } |
| 43 | 40 |
| 44 class CallbackIterator implements Iterator<int> { | 41 class CallbackIterator implements Iterator<int> { |
| 45 TestIterableBase _iterable; | 42 TestIterableBase _iterable; |
| 46 int _current = null; | 43 int _current = null; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 Expect.throws(() => new List<int>(null), (e) => e is ArgumentError, "null"); | 98 Expect.throws(() => new List<int>(null), (e) => e is ArgumentError, "null"); |
| 102 testThrowsOrTypeError( | 99 testThrowsOrTypeError( |
| 103 () => new List([] as Object), // Cast to avoid warning. | 100 () => new List([] as Object), // Cast to avoid warning. |
| 104 (e) => e is ArgumentError, | 101 (e) => e is ArgumentError, |
| 105 'list'); | 102 'list'); |
| 106 testThrowsOrTypeError( | 103 testThrowsOrTypeError( |
| 107 () => new List([42] as Object), (e) => e is ArgumentError, "list2"); | 104 () => new List([42] as Object), (e) => e is ArgumentError, "list2"); |
| 108 } | 105 } |
| 109 | 106 |
| 110 void testConcurrentModification() { | 107 void testConcurrentModification() { |
| 111 // Without EfficientLength interface | 108 // Without EfficientLengthIterable interface |
| 112 { | 109 { |
| 113 // Change length of list after 200 additions. | 110 // Change length of list after 200 additions. |
| 114 var l = []; | 111 var l = []; |
| 115 var ci = new TestIterable(257, 200, () { | 112 var ci = new TestIterable(257, 200, () { |
| 116 l.add("X"); | 113 l.add("X"); |
| 117 }); | 114 }); |
| 118 Expect.throws(() { | 115 Expect.throws(() { |
| 119 l.addAll(ci); | 116 l.addAll(ci); |
| 120 }, (e) => e is ConcurrentModificationError, "cm1"); | 117 }, (e) => e is ConcurrentModificationError, "cm1"); |
| 121 } | 118 } |
| 122 | 119 |
| 123 { | 120 { |
| 124 // Change length of list after 200 additions. | 121 // Change length of list after 200 additions. |
| 125 var l = []; | 122 var l = []; |
| 126 var ci = new TestIterable(257, 200, () { | 123 var ci = new TestIterable(257, 200, () { |
| 127 l.length = 0; | 124 l.length = 0; |
| 128 }); | 125 }); |
| 129 Expect.throws(() { | 126 Expect.throws(() { |
| 130 l.addAll(ci); | 127 l.addAll(ci); |
| 131 }, (e) => e is ConcurrentModificationError, "cm2"); | 128 }, (e) => e is ConcurrentModificationError, "cm2"); |
| 132 } | 129 } |
| 133 | 130 |
| 134 // With EfficientLength interface (uses length). | 131 // With EfficientLengthIterable interface (uses length). |
| 135 { | 132 { |
| 136 // Change length of list after 20 additions. | 133 // Change length of list after 20 additions. |
| 137 var l = []; | 134 var l = []; |
| 138 var ci = new EfficientTestIterable(257, 257, 20, () { | 135 var ci = new EfficientTestIterable(257, 257, 20, () { |
| 139 l.add("X"); | 136 l.add("X"); |
| 140 }); | 137 }); |
| 141 Expect.throws(() { | 138 Expect.throws(() { |
| 142 l.addAll(ci); | 139 l.addAll(ci); |
| 143 }, (e) => e is ConcurrentModificationError, "cm3"); | 140 }, (e) => e is ConcurrentModificationError, "cm3"); |
| 144 } | 141 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 } | 175 } |
| 179 | 176 |
| 180 { | 177 { |
| 181 // Adding to yourself. | 178 // Adding to yourself. |
| 182 var l = [1, 2, 3]; | 179 var l = [1, 2, 3]; |
| 183 Expect.throws(() { | 180 Expect.throws(() { |
| 184 l.addAll(l); | 181 l.addAll(l); |
| 185 }, (e) => e is ConcurrentModificationError, "cm8"); | 182 }, (e) => e is ConcurrentModificationError, "cm8"); |
| 186 } | 183 } |
| 187 } | 184 } |
| OLD | NEW |