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 library queue.test; | 5 library queue.test; |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 abstract class QueueTest { | 10 abstract class QueueTest { |
11 | |
12 Queue newQueue(); | 11 Queue newQueue(); |
13 Queue newQueueFrom(Iterable iterable); | 12 Queue newQueueFrom(Iterable iterable); |
14 | 13 |
15 void testMain() { | 14 void testMain() { |
16 Queue queue = newQueue(); | 15 Queue queue = newQueue(); |
17 checkQueue(queue, 0, 0); | 16 checkQueue(queue, 0, 0); |
18 | 17 |
19 queue.addFirst(1); | 18 queue.addFirst(1); |
20 checkQueue(queue, 1, 1); | 19 checkQueue(queue, 1, 1); |
21 | 20 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 return (value is int); | 62 return (value is int); |
64 } | 63 } |
65 | 64 |
66 Expect.equals(true, queue.every(isInstanceOfInt)); | 65 Expect.equals(true, queue.every(isInstanceOfInt)); |
67 | 66 |
68 Expect.equals(false, queue.every(is10)); | 67 Expect.equals(false, queue.every(is10)); |
69 | 68 |
70 bool is1(int value) { | 69 bool is1(int value) { |
71 return (value == 1); | 70 return (value == 1); |
72 } | 71 } |
| 72 |
73 Expect.equals(false, queue.any(is1)); | 73 Expect.equals(false, queue.any(is1)); |
74 | 74 |
75 queue.clear(); | 75 queue.clear(); |
76 Expect.equals(0, queue.length); | 76 Expect.equals(0, queue.length); |
77 | 77 |
78 var exception = null; | 78 var exception = null; |
79 try { | 79 try { |
80 queue.removeFirst(); | 80 queue.removeFirst(); |
81 } on StateError catch (e) { | 81 } on StateError catch (e) { |
82 exception = e; | 82 exception = e; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 testLarge(); | 122 testLarge(); |
123 testFromListToList(); | 123 testFromListToList(); |
124 } | 124 } |
125 | 125 |
126 void checkQueue(Queue queue, int expectedSize, int expectedSum) { | 126 void checkQueue(Queue queue, int expectedSize, int expectedSum) { |
127 testLength(expectedSize, queue); | 127 testLength(expectedSize, queue); |
128 int sum = 0; | 128 int sum = 0; |
129 void sumElements(int value) { | 129 void sumElements(int value) { |
130 sum += value; | 130 sum += value; |
131 } | 131 } |
| 132 |
132 queue.forEach(sumElements); | 133 queue.forEach(sumElements); |
133 Expect.equals(expectedSum, sum); | 134 Expect.equals(expectedSum, sum); |
134 } | 135 } |
135 | 136 |
136 testLength(int length, Queue queue) { | 137 testLength(int length, Queue queue) { |
137 Expect.equals(length, queue.length); | 138 Expect.equals(length, queue.length); |
138 ((length == 0) ? Expect.isTrue : Expect.isFalse)(queue.isEmpty); | 139 ((length == 0) ? Expect.isTrue : Expect.isFalse)(queue.isEmpty); |
139 ((length != 0) ? Expect.isTrue : Expect.isFalse)(queue.isNotEmpty); | 140 ((length != 0) ? Expect.isTrue : Expect.isFalse)(queue.isNotEmpty); |
140 } | 141 } |
141 | 142 |
142 void testAddAll() { | 143 void testAddAll() { |
143 Set<int> set = new Set<int>.from([1, 2, 4]); | 144 Set<int> set = new Set<int>.from([1, 2, 4]); |
144 Expect.equals(3, set.length); | 145 Expect.equals(3, set.length); |
145 | 146 |
146 Queue queue1 = newQueueFrom(set); | 147 Queue queue1 = newQueueFrom(set); |
147 Queue queue2 = newQueue(); | 148 Queue queue2 = newQueue(); |
148 Queue queue3 = newQueue(); | 149 Queue queue3 = newQueue(); |
149 testLength(3, queue1); | 150 testLength(3, queue1); |
150 testLength(0, queue2); | 151 testLength(0, queue2); |
151 testLength(0, queue3); | 152 testLength(0, queue3); |
152 | 153 |
153 queue2.addAll(set); | 154 queue2.addAll(set); |
154 testLength(3, queue2); | 155 testLength(3, queue2); |
155 | 156 |
156 queue3.addAll(queue1); | 157 queue3.addAll(queue1); |
157 testLength(3, queue3); | 158 testLength(3, queue3); |
158 | 159 |
159 int sum = 0; | 160 int sum = 0; |
160 void f(e) { sum += e; }; | 161 void f(e) { |
| 162 sum += e; |
| 163 } |
| 164 |
| 165 ; |
161 | 166 |
162 set.forEach(f); | 167 set.forEach(f); |
163 Expect.equals(7, sum); | 168 Expect.equals(7, sum); |
164 sum = 0; | 169 sum = 0; |
165 | 170 |
166 queue1.forEach(f); | 171 queue1.forEach(f); |
167 Expect.equals(7, sum); | 172 Expect.equals(7, sum); |
168 sum = 0; | 173 sum = 0; |
169 | 174 |
170 queue2.forEach(f); | 175 queue2.forEach(f); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 | 260 |
256 for (int i = 0; i < N; i++) { | 261 for (int i = 0; i < N; i++) { |
257 queue.add(i); | 262 queue.add(i); |
258 set.add(i); | 263 set.add(i); |
259 } | 264 } |
260 Expect.equals(N, queue.length); | 265 Expect.equals(N, queue.length); |
261 Expect.isFalse(queue.isEmpty); | 266 Expect.isFalse(queue.isEmpty); |
262 | 267 |
263 Expect.equals(0, queue.elementAt(0)); | 268 Expect.equals(0, queue.elementAt(0)); |
264 Expect.equals(N - 1, queue.elementAt(N - 1)); | 269 Expect.equals(N - 1, queue.elementAt(N - 1)); |
265 Expect.throws(() { queue.elementAt(-1); }); | 270 Expect.throws(() { |
266 Expect.throws(() { queue.elementAt(N); }); | 271 queue.elementAt(-1); |
| 272 }); |
| 273 Expect.throws(() { |
| 274 queue.elementAt(N); |
| 275 }); |
267 | 276 |
268 Iterable skip1 = queue.skip(1); | 277 Iterable skip1 = queue.skip(1); |
269 Iterable take1 = queue.take(1); | 278 Iterable take1 = queue.take(1); |
270 Iterable mapped = queue.map((e) => -e); | 279 Iterable mapped = queue.map((e) => -e); |
271 | 280 |
272 for (int i = 0; i < 500; i++) { | 281 for (int i = 0; i < 500; i++) { |
273 Expect.equals(i, take1.first); | 282 Expect.equals(i, take1.first); |
274 Expect.equals(i, queue.first); | 283 Expect.equals(i, queue.first); |
275 Expect.equals(-i, mapped.first); | 284 Expect.equals(-i, mapped.first); |
276 Expect.equals(i + 1, skip1.first); | 285 Expect.equals(i + 1, skip1.first); |
(...skipping 14 matching lines...) Expand all Loading... |
291 | 300 |
292 queue.addAll(set); | 301 queue.addAll(set); |
293 Expect.equals(N, queue.length); | 302 Expect.equals(N, queue.length); |
294 Expect.isFalse(queue.isEmpty); | 303 Expect.isFalse(queue.isEmpty); |
295 | 304 |
296 // Iterate. | 305 // Iterate. |
297 for (var element in queue) { | 306 for (var element in queue) { |
298 Expect.isTrue(set.contains(element)); | 307 Expect.isTrue(set.contains(element)); |
299 } | 308 } |
300 | 309 |
301 queue.forEach((element) { Expect.isTrue(set.contains(element)); }); | 310 queue.forEach((element) { |
| 311 Expect.isTrue(set.contains(element)); |
| 312 }); |
302 | 313 |
303 queue.addAll(set); | 314 queue.addAll(set); |
304 Expect.equals(N * 2, queue.length); | 315 Expect.equals(N * 2, queue.length); |
305 Expect.isFalse(queue.isEmpty); | 316 Expect.isFalse(queue.isEmpty); |
306 | 317 |
307 queue.clear(); | 318 queue.clear(); |
308 Expect.equals(0, queue.length); | 319 Expect.equals(0, queue.length); |
309 Expect.isTrue(queue.isEmpty); | 320 Expect.isTrue(queue.isEmpty); |
310 } | 321 } |
311 | 322 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 Expect.equals(87, prev.element); | 474 Expect.equals(87, prev.element); |
464 | 475 |
465 Expect.equals(37, next.remove()); | 476 Expect.equals(37, next.remove()); |
466 Expect.equals(87, prev.element); | 477 Expect.equals(87, prev.element); |
467 Expect.equals(null, prev.nextEntry()); | 478 Expect.equals(null, prev.nextEntry()); |
468 Expect.equals(null, prev.previousEntry()); | 479 Expect.equals(null, prev.previousEntry()); |
469 | 480 |
470 Expect.equals(87, prev.remove()); | 481 Expect.equals(87, prev.remove()); |
471 } | 482 } |
472 | 483 |
473 | |
474 main() { | 484 main() { |
475 new DoubleLinkedQueueTest().testMain(); | 485 new DoubleLinkedQueueTest().testMain(); |
476 new ListQueueTest().testMain(); | 486 new ListQueueTest().testMain(); |
477 linkEntryTest(); | 487 linkEntryTest(); |
478 } | 488 } |
OLD | NEW |