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 |
9 void main() { | |
10 testConstructor(); | |
11 | |
12 bool checked = false; | |
13 assert((checked = true)); | |
14 // Concurrent modification checks are only guaranteed in checked mode. | |
15 if (checked) testConcurrentModification(); | |
16 } | |
9 | 17 |
10 // Iterable generating numbers in range [0..count). | 18 // Iterable generating numbers in range [0..count). |
11 // May perform callback at some point underways. | 19 // May perform callback at some point underways. |
12 class TestIterableBase extends IterableBase<int> { | 20 class TestIterableBase extends Iterable<int> { |
13 final int length; | 21 final int length; |
14 final int count; | 22 final int count; |
15 // call [callback] if generating callbackIndex. | 23 // call [callback] if generating callbackIndex. |
16 final int callbackIndex; | 24 final int callbackIndex; |
17 final Function callback; | 25 final Function callback; |
18 TestIterableBase(this.length, this.count, | 26 TestIterableBase(this.length, this.count, |
19 this.callbackIndex, this.callback); | 27 this.callbackIndex, this.callback); |
20 Iterator<int> get iterator => new CallbackIterator(this); | 28 Iterator<int> get iterator => new CallbackIterator(this); |
21 } | 29 } |
22 | 30 |
(...skipping 25 matching lines...) Expand all Loading... | |
48 _current = _nextIndex; | 56 _current = _nextIndex; |
49 _nextIndex++; | 57 _nextIndex++; |
50 if (_current == _iterable.callbackIndex) { | 58 if (_current == _iterable.callbackIndex) { |
51 _iterable.callback(); | 59 _iterable.callback(); |
52 } | 60 } |
53 return true; | 61 return true; |
54 } | 62 } |
55 int get current => _current; | 63 int get current => _current; |
56 } | 64 } |
57 | 65 |
66 void testConstructor() { | |
67 // Constructor can make both growable and fixed-length lists. | |
68 testGrowable(list) { | |
69 Expect.isTrue(list is List<int>); | |
70 Expect.isFalse(list is List<String>); | |
71 int length = list.length; | |
72 list.add(42); | |
73 Expect.equals(list.length, length + 1); | |
74 } | |
58 | 75 |
59 void main() { | 76 testFixedLength(list) { |
77 Expect.isTrue(list is List<int>); | |
78 int length = list.length; | |
79 Expect.throws(() { list.add(42); }, null, "adding to fixed-length list"); | |
80 } | |
81 | |
82 bool checked = false; | |
83 assert((checked = true)); | |
84 testThrowsOrTypeError(fn, test, [name]) { | |
85 Expect.throws(fn, checked ? null : test, | |
Lasse Reichstein Nielsen
2015/07/07 13:08:02
null should be ((e)=>e is TypeError), but dartj2s
herhut
2015/07/08 08:59:58
Yes, this happens early on in handleNewSend in the
Lasse Reichstein Nielsen
2015/07/08 09:03:39
I'm not sure it's necessarily wrong, it's throwing
| |
86 checked ? name : "$name w/ TypeError"); | |
87 } | |
88 testFixedLength(new List<int>(0)); | |
89 testFixedLength(new List<int>(5)); | |
90 testGrowable(new List<int>()); | |
91 testGrowable(new List<int>()..length = 5); | |
92 Expect.throws(() => new List<int>(-1), (e) => e is ArgumentError, "-1"); | |
93 // There must be limits. Fix this test if we ever allow 10^30 elements. | |
94 Expect.throws(() => new List<int>(0x1000000000000000000000000000000), | |
95 (e) => e is ArgumentError, "bignum"); | |
96 Expect.throws(() => new List<int>(null), (e) => e is ArgumentError, "null"); | |
97 testThrowsOrTypeError(() => new List([]), (e) => e is ArgumentError, 'list'); | |
sra1
2015/07/08 09:57:01
Have two versions of each of these:
new List(
| |
98 testThrowsOrTypeError(() => new List([42]), | |
99 (e) => e is ArgumentError, "list2"); | |
Lasse Reichstein Nielsen
2015/07/07 13:08:02
I wanted to check for RangeError here, but it seem
herhut
2015/07/08 08:59:57
See above.
| |
100 } | |
101 | |
102 void testConcurrentModification() { | |
60 // Without EfficientLength interface | 103 // Without EfficientLength interface |
61 { | 104 { |
62 // Change length of list after 20 additions. | 105 // Change length of list after 200 additions. |
63 var l = []; | 106 var l = []; |
64 var ci = new TestIterable(257, 200, () { | 107 var ci = new TestIterable(257, 200, () { |
65 l.add("X"); | 108 l.add("X"); |
66 }); | 109 }); |
67 Expect.throws(() { | 110 Expect.throws(() { |
68 l.addAll(ci); | 111 l.addAll(ci); |
69 }, (e) => e is ConcurrentModificationError); | 112 }, (e) => e is ConcurrentModificationError, "cm1"); |
70 } | 113 } |
71 | 114 |
72 { | 115 { |
73 // Change length of list after 20 additions. | 116 // Change length of list after 200 additions. |
74 var l = []; | 117 var l = []; |
75 var ci = new TestIterable(257, 200, () { | 118 var ci = new TestIterable(257, 200, () { |
76 l.length = 0; | 119 l.length = 0; |
77 }); | 120 }); |
78 Expect.throws(() { | 121 Expect.throws(() { |
79 l.addAll(ci); | 122 l.addAll(ci); |
80 }, (e) => e is ConcurrentModificationError); | 123 }, (e) => e is ConcurrentModificationError, "cm2"); |
81 } | 124 } |
82 | 125 |
83 // With EfficientLength interface (uses length). | 126 // With EfficientLength interface (uses length). |
84 { | 127 { |
85 // Change length of list after 20 additions. | 128 // Change length of list after 20 additions. |
86 var l = []; | 129 var l = []; |
87 var ci = new EfficientTestIterable(257, 257, 20, () { | 130 var ci = new EfficientTestIterable(257, 257, 20, () { |
88 l.add("X"); | 131 l.add("X"); |
89 }); | 132 }); |
90 Expect.throws(() { | 133 Expect.throws(() { |
91 l.addAll(ci); | 134 l.addAll(ci); |
92 }, (e) => e is ConcurrentModificationError); | 135 }, (e) => e is ConcurrentModificationError, "cm3"); |
93 } | 136 } |
94 | 137 |
95 { | 138 { |
96 var l = []; | 139 var l = []; |
97 var ci = new EfficientTestIterable(257, 257, 20, () { | 140 var ci = new EfficientTestIterable(257, 257, 20, () { |
98 l.length = 0; | 141 l.length = 0; |
99 }); | 142 }); |
100 Expect.throws(() { | 143 Expect.throws(() { |
101 l.addAll(ci); | 144 l.addAll(ci); |
102 }, (e) => e is ConcurrentModificationError); | 145 }, (e) => e is ConcurrentModificationError, "cm4"); |
103 } | 146 } |
104 | 147 |
105 { | 148 { |
106 // Length 50, only 25 elements. | 149 // Length 500, only 250 elements. |
107 var l = []; | 150 var l = []; |
108 var ci = new EfficientTestIterable(500, 250); | 151 var ci = new EfficientTestIterable(500, 250); |
109 l.addAll(ci); | 152 l.addAll(ci); |
110 Expect.listEquals(new List.generate(250, (x)=>x), l); | 153 Expect.listEquals(new List.generate(250, (x)=>x), l, "cm5"); |
111 } | 154 } |
112 | 155 |
113 { | 156 { |
114 // Length 25, but 50 elements. | 157 // Length 250, but 500 elements. |
115 var l = []; | 158 var l = []; |
116 var ci = new EfficientTestIterable(250, 500); | 159 var ci = new EfficientTestIterable(250, 500); |
117 l.addAll(ci); | 160 l.addAll(ci); |
118 Expect.listEquals(new List.generate(500, (x)=>x), l); | 161 Expect.listEquals(new List.generate(500, (x)=>x), l, "cm6"); |
119 } | 162 } |
120 | 163 |
121 { | 164 { |
122 // Adding to yourself. | 165 // Adding to yourself. |
123 var l = [1]; | 166 var l = [1]; |
124 Expect.throws(() { l.addAll(l); }, (e) => e is ConcurrentModificationError); | 167 Expect.throws(() { l.addAll(l); }, |
168 (e) => e is ConcurrentModificationError, "cm7"); | |
125 } | 169 } |
126 | 170 |
127 { | 171 { |
128 // Adding to yourself. | 172 // Adding to yourself. |
129 var l = [1, 2, 3]; | 173 var l = [1, 2, 3]; |
130 Expect.throws(() { l.addAll(l); }, (e) => e is ConcurrentModificationError); | 174 Expect.throws(() { l.addAll(l); }, |
175 (e) => e is ConcurrentModificationError, "cm8"); | |
131 } | 176 } |
132 } | 177 } |
133 | 178 |
OLD | NEW |