OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014, 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 import "package:collection/collection.dart"; | |
6 import "package:unittest/unittest.dart"; | |
7 | |
8 void main() { | |
9 group("new QueueList()", () { | |
10 test("creates an empty QueueList", () { | |
11 expect(new QueueList(), isEmpty); | |
12 }); | |
13 | |
14 test("takes an initial capacity", () { | |
15 expect(new QueueList(100), isEmpty); | |
16 }); | |
17 }); | |
18 | |
19 test("new QueueList.from() copies the contents of an iterable", () { | |
20 expect(new QueueList.from([1, 2, 3].skip(1)), equals([2, 3])); | |
21 }); | |
22 | |
23 group("add()", () { | |
24 test("adds an element to the end of the queue", () { | |
25 var queue = new QueueList.from([1, 2, 3]); | |
26 queue.add(4); | |
27 expect(queue, equals([1, 2, 3, 4])); | |
28 }); | |
29 | |
30 test("expands a full queue", () { | |
31 var queue = atCapacity(); | |
32 queue.add(9); | |
33 expect(queue, equals([1, 2, 3, 4, 5, 6, 7, 8, 9])); | |
34 }); | |
35 }); | |
36 | |
37 group("addAll()", () { | |
38 test("adds elements to the end of the queue", () { | |
39 var queue = new QueueList.from([1, 2, 3]); | |
40 queue.addAll([4, 5, 6]); | |
41 expect(queue, equals([1, 2, 3, 4, 5, 6])); | |
42 }); | |
43 | |
44 test("expands a full queue", () { | |
45 var queue = atCapacity(); | |
46 queue.addAll([9, 10]); | |
47 expect(queue, equals([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); | |
48 }); | |
49 }); | |
50 | |
51 group("addFirst()", () { | |
52 test("adds an element to the beginning of the queue", () { | |
53 var queue = new QueueList.from([1, 2, 3]); | |
54 queue.addFirst(0); | |
55 expect(queue, equals([0, 1, 2, 3])); | |
56 }); | |
57 | |
58 test("expands a full queue", () { | |
59 var queue = atCapacity(); | |
60 queue.addFirst(0); | |
61 expect(queue, equals([0, 1, 2, 3, 4, 5, 6, 7, 8])); | |
62 }); | |
63 }); | |
64 | |
65 group("removeFirst()", () { | |
66 test("removes an element from the beginning of the queue", () { | |
67 var queue = new QueueList.from([1, 2, 3]); | |
68 expect(queue.removeFirst(), equals(1)); | |
69 expect(queue, equals([2, 3])); | |
70 }); | |
71 | |
72 test("removes an element from the beginning of a queue with an internal " | |
73 "gap", () { | |
74 var queue = withInternalGap(); | |
75 expect(queue.removeFirst(), equals(1)); | |
76 expect(queue, equals([2, 3, 4, 5, 6, 7])); | |
77 }); | |
78 | |
79 test("removes an element from the beginning of a queue at capacity", () { | |
80 var queue = atCapacity(); | |
81 expect(queue.removeFirst(), equals(1)); | |
82 expect(queue, equals([2, 3, 4, 5, 6, 7, 8])); | |
83 }); | |
84 | |
85 test("throws a StateError for an empty queue", () { | |
86 expect(new QueueList().removeFirst, throwsStateError); | |
87 }); | |
88 }); | |
89 | |
90 group("removeLast()", () { | |
91 test("removes an element from the end of the queue", () { | |
92 var queue = new QueueList.from([1, 2, 3]); | |
93 expect(queue.removeLast(), equals(3)); | |
94 expect(queue, equals([1, 2])); | |
95 }); | |
96 | |
97 test("removes an element from the end of a queue with an internal gap", () { | |
98 var queue = withInternalGap(); | |
99 expect(queue.removeLast(), equals(7)); | |
100 expect(queue, equals([1, 2, 3, 4, 5, 6])); | |
101 }); | |
102 | |
103 test("removes an element from the end of a queue at capacity", () { | |
104 var queue = atCapacity(); | |
105 expect(queue.removeLast(), equals(8)); | |
106 expect(queue, equals([1, 2, 3, 4, 5, 6, 7])); | |
107 }); | |
108 | |
109 test("throws a StateError for an empty queue", () { | |
110 expect(new QueueList().removeLast, throwsStateError); | |
111 }); | |
112 }); | |
113 | |
114 group("length", () { | |
115 test("returns the length of a queue", () { | |
116 expect(new QueueList.from([1, 2, 3]).length, equals(3)); | |
117 }); | |
118 | |
119 test("returns the length of a queue with an internal gap", () { | |
120 expect(withInternalGap().length, equals(7)); | |
121 }); | |
122 | |
123 test("returns the length of a queue at capacity", () { | |
124 expect(atCapacity().length, equals(8)); | |
125 }); | |
126 }); | |
127 | |
128 group("length=", () { | |
129 test("shrinks a larger queue", () { | |
130 var queue = new QueueList.from([1, 2, 3]); | |
131 queue.length = 1; | |
132 expect(queue, equals([1])); | |
133 }); | |
134 | |
135 test("grows a smaller queue", () { | |
136 var queue = new QueueList.from([1, 2, 3]); | |
137 queue.length = 5; | |
138 expect(queue, equals([1, 2, 3, null, null])); | |
139 }); | |
140 | |
141 test("throws a RangeError if length is less than 0", () { | |
142 expect(() => new QueueList().length = -1, throwsRangeError); | |
143 }); | |
144 }); | |
145 | |
146 group("[]", () { | |
147 test("returns individual entries in the queue", () { | |
148 var queue = new QueueList.from([1, 2, 3]); | |
149 expect(queue[0], equals(1)); | |
150 expect(queue[1], equals(2)); | |
151 expect(queue[2], equals(3)); | |
152 }); | |
153 | |
154 test("returns individual entries in a queue with an internal gap", () { | |
155 var queue = withInternalGap(); | |
156 expect(queue[0], equals(1)); | |
157 expect(queue[1], equals(2)); | |
158 expect(queue[2], equals(3)); | |
159 expect(queue[3], equals(4)); | |
160 expect(queue[4], equals(5)); | |
161 expect(queue[5], equals(6)); | |
162 expect(queue[6], equals(7)); | |
163 }); | |
164 }); | |
165 | |
166 group("[]=", () { | |
167 test("sets individual entries in the queue", () { | |
168 var queue = new QueueList.from([1, 2, 3]); | |
169 queue[0] = "a"; | |
170 queue[1] = "b"; | |
171 queue[2] = "c"; | |
172 expect(queue, equals(["a", "b", "c"])); | |
173 }); | |
174 | |
175 test("sets individual entries in a queue with an internal gap", () { | |
176 var queue = withInternalGap(); | |
177 queue[0] = "a"; | |
178 queue[1] = "b"; | |
179 queue[2] = "c"; | |
180 queue[3] = "d"; | |
181 queue[4] = "e"; | |
182 queue[5] = "f"; | |
183 queue[6] = "g"; | |
184 expect(queue, equals(["a", "b", "c", "d", "e", "f", "g"])); | |
185 }); | |
186 }); | |
187 | |
188 group("throws a modification error for", () { | |
189 var queue; | |
190 setUp(() { | |
191 queue = new QueueList.from([1, 2, 3]); | |
192 }); | |
193 | |
194 test("add", () { | |
195 expect(() => queue.forEach((_) => queue.add(4)), | |
196 throwsConcurrentModificationError); | |
197 }); | |
198 | |
199 test("addAll", () { | |
200 expect(() => queue.forEach((_) => queue.addAll([4, 5, 6])), | |
201 throwsConcurrentModificationError); | |
202 }); | |
203 | |
204 test("addFirst", () { | |
205 expect(() => queue.forEach((_) => queue.addFirst(0)), | |
206 throwsConcurrentModificationError); | |
207 }); | |
208 | |
209 test("removeFirst", () { | |
210 expect(() => queue.forEach((_) => queue.removeFirst()), | |
211 throwsConcurrentModificationError); | |
212 }); | |
213 | |
214 test("removeLast", () { | |
215 expect(() => queue.forEach((_) => queue.removeLast()), | |
216 throwsConcurrentModificationError); | |
217 }); | |
218 | |
219 test("length=", () { | |
220 expect(() => queue.forEach((_) => queue.length = 1), | |
221 throwsConcurrentModificationError); | |
222 }); | |
223 }); | |
224 } | |
225 | |
226 /// Returns a queue whose internal ring buffer is full. | |
227 QueueList atCapacity() { | |
228 // Use addAll because [new QueueList.from(List)] won't use the default initial | |
229 // capacity of 8. | |
Lasse Reichstein Nielsen
2014/10/29 07:04:44
The buffer grows when the 8th element is added, so
nweiz
2014/10/29 20:49:36
Done.
| |
230 return new QueueList()..addAll([1, 2, 3, 4, 5, 6, 7, 8]); | |
231 } | |
232 | |
233 /// Returns a queue whose internal tail has a lower index than its head. | |
234 QueueList withInternalGap() { | |
235 var queue = new QueueList.from([null, null, null, null, 1, 2, 3, 4]); | |
236 for (var i = 0; i < 4; i++) { | |
237 queue.removeFirst(); | |
238 } | |
239 for (var i = 5; i < 8; i++) { | |
240 queue.addLast(i); | |
241 } | |
242 return queue; | |
243 } | |
244 | |
245 /// Returns a matcher that expects that a closure throws a | |
246 /// [ConcurrentModificationError]. | |
247 final throwsConcurrentModificationError = throwsA( | |
248 new isInstanceOf<ConcurrentModificationError>( | |
249 "ConcurrentModificationError")); | |
Lasse Reichstein Nielsen
2014/10/29 07:04:44
Consider copying some tests from tests/corelib/lis
nweiz
2014/10/29 20:49:36
I think we can rely on the implementation of ListM
| |
OLD | NEW |