| 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 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 class QueueTest { | 9 abstract class QueueTest { |
| 10 | 10 |
| 11 static testMain() { | 11 Queue newQueue([int capacity]); |
| 12 Queue queue = new Queue(); | 12 Queue newQueueFrom(Iterable iterable); |
| 13 |
| 14 void testMain() { |
| 15 Queue queue = newQueue(); |
| 13 checkQueue(queue, 0, 0); | 16 checkQueue(queue, 0, 0); |
| 14 | 17 |
| 15 queue.addFirst(1); | 18 queue.addFirst(1); |
| 16 checkQueue(queue, 1, 1); | 19 checkQueue(queue, 1, 1); |
| 17 | 20 |
| 18 queue.addLast(10); | 21 queue.addLast(10); |
| 19 checkQueue(queue, 2, 11); | 22 checkQueue(queue, 2, 11); |
| 20 | 23 |
| 21 Expect.equals(10, queue.removeLast()); | 24 Expect.equals(10, queue.removeLast()); |
| 22 checkQueue(queue, 1, 1); | 25 checkQueue(queue, 1, 1); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 36 checkQueue(queue, 3, 1110); | 39 checkQueue(queue, 3, 1110); |
| 37 | 40 |
| 38 int mapTest(int value) { | 41 int mapTest(int value) { |
| 39 return value ~/ 10; | 42 return value ~/ 10; |
| 40 } | 43 } |
| 41 | 44 |
| 42 bool is10(int value) { | 45 bool is10(int value) { |
| 43 return (value == 10); | 46 return (value == 10); |
| 44 } | 47 } |
| 45 | 48 |
| 46 Queue mapped = new Queue.from(queue.map(mapTest)); | 49 Queue mapped = newQueueFrom(queue.map(mapTest)); |
| 47 checkQueue(mapped, 3, 111); | 50 checkQueue(mapped, 3, 111); |
| 48 checkQueue(queue, 3, 1110); | 51 checkQueue(queue, 3, 1110); |
| 49 Expect.equals(1, mapped.removeFirst()); | 52 Expect.equals(1, mapped.removeFirst()); |
| 50 Expect.equals(100, mapped.removeLast()); | 53 Expect.equals(100, mapped.removeLast()); |
| 51 Expect.equals(10, mapped.removeFirst()); | 54 Expect.equals(10, mapped.removeFirst()); |
| 52 | 55 |
| 53 Queue other = new Queue.from(queue.where(is10)); | 56 Queue other = newQueueFrom(queue.where(is10)); |
| 54 checkQueue(other, 1, 10); | 57 checkQueue(other, 1, 10); |
| 55 | 58 |
| 56 Expect.equals(true, queue.any(is10)); | 59 Expect.equals(true, queue.any(is10)); |
| 57 | 60 |
| 58 bool isInstanceOfInt(int value) { | 61 bool isInstanceOfInt(int value) { |
| 59 return (value is int); | 62 return (value is int); |
| 60 } | 63 } |
| 61 | 64 |
| 62 Expect.equals(true, queue.every(isInstanceOfInt)); | 65 Expect.equals(true, queue.every(isInstanceOfInt)); |
| 63 | 66 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 93 queue.addFirst(2); | 96 queue.addFirst(2); |
| 94 Expect.equals(2, queue.first); | 97 Expect.equals(2, queue.first); |
| 95 Expect.equals(1, queue.last); | 98 Expect.equals(1, queue.last); |
| 96 | 99 |
| 97 queue.addLast(3); | 100 queue.addLast(3); |
| 98 Expect.equals(3, queue.last); | 101 Expect.equals(3, queue.last); |
| 99 bool isGreaterThanOne(int value) { | 102 bool isGreaterThanOne(int value) { |
| 100 return (value > 1); | 103 return (value > 1); |
| 101 } | 104 } |
| 102 | 105 |
| 103 other = new Queue.from(queue.where(isGreaterThanOne)); | 106 other = newQueueFrom(queue.where(isGreaterThanOne)); |
| 104 checkQueue(other, 2, 5); | 107 checkQueue(other, 2, 5); |
| 105 | 108 |
| 109 // Cycle through values without ever having large element count. |
| 110 queue = newQueue(); |
| 111 queue.add(0); |
| 112 for (int i = 0; i < 255; i++) { |
| 113 queue.add(i + 1); |
| 114 Expect.equals(i, queue.removeFirst()); |
| 115 } |
| 116 Expect.equals(255, queue.removeFirst()); |
| 117 Expect.isTrue(queue.isEmpty); |
| 118 |
| 106 testAddAll(); | 119 testAddAll(); |
| 120 testLarge(); |
| 107 } | 121 } |
| 108 | 122 |
| 109 static void checkQueue(Queue queue, int expectedSize, int expectedSum) { | 123 void checkQueue(Queue queue, int expectedSize, int expectedSum) { |
| 110 Expect.equals(expectedSize, queue.length); | 124 Expect.equals(expectedSize, queue.length); |
| 111 int sum = 0; | 125 int sum = 0; |
| 112 void sumElements(int value) { | 126 void sumElements(int value) { |
| 113 sum += value; | 127 sum += value; |
| 114 } | 128 } |
| 115 queue.forEach(sumElements); | 129 queue.forEach(sumElements); |
| 116 Expect.equals(expectedSum, sum); | 130 Expect.equals(expectedSum, sum); |
| 117 } | 131 } |
| 118 | 132 |
| 119 static testAddAll() { | 133 void testAddAll() { |
| 120 Set<int> set = new Set<int>.from([1, 2, 4]); | 134 Set<int> set = new Set<int>.from([1, 2, 4]); |
| 121 | 135 |
| 122 Queue<int> queue1 = new Queue<int>.from(set); | 136 Queue queue1 = newQueueFrom(set); |
| 123 Queue<int> queue2 = new Queue<int>(); | 137 Queue queue2 = newQueue(); |
| 124 Queue<int> queue3 = new Queue<int>(); | 138 Queue queue3 = newQueue(); |
| 125 | 139 |
| 126 queue2.addAll(set); | 140 queue2.addAll(set); |
| 127 queue3.addAll(queue1); | 141 queue3.addAll(queue1); |
| 128 | 142 |
| 129 Expect.equals(3, set.length); | 143 Expect.equals(3, set.length); |
| 130 Expect.equals(3, queue1.length); | 144 Expect.equals(3, queue1.length); |
| 131 Expect.equals(3, queue2.length); | 145 Expect.equals(3, queue2.length); |
| 132 Expect.equals(3, queue3.length); | 146 Expect.equals(3, queue3.length); |
| 133 | 147 |
| 134 int sum = 0; | 148 int sum = 0; |
| 135 void f(e) { sum += e; }; | 149 void f(e) { sum += e; }; |
| 136 | 150 |
| 137 set.forEach(f); | 151 set.forEach(f); |
| 138 Expect.equals(7, sum); | 152 Expect.equals(7, sum); |
| 139 sum = 0; | 153 sum = 0; |
| 140 | 154 |
| 141 queue1.forEach(f); | 155 queue1.forEach(f); |
| 142 Expect.equals(7, sum); | 156 Expect.equals(7, sum); |
| 143 sum = 0; | 157 sum = 0; |
| 144 | 158 |
| 145 queue2.forEach(f); | 159 queue2.forEach(f); |
| 146 Expect.equals(7, sum); | 160 Expect.equals(7, sum); |
| 147 sum = 0; | 161 sum = 0; |
| 148 | 162 |
| 149 queue3.forEach(f); | 163 queue3.forEach(f); |
| 150 Expect.equals(7, sum); | 164 Expect.equals(7, sum); |
| 151 sum = 0; | 165 sum = 0; |
| 152 | 166 |
| 153 set = new Set<int>.from([]); | 167 set = new Set<int>.from([]); |
| 154 queue1 = new Queue<int>.from(set); | 168 queue1 = newQueueFrom(set); |
| 155 queue2 = new Queue<int>(); | 169 queue2 = newQueue(); |
| 156 queue3 = new Queue<int>(); | 170 queue3 = newQueue(); |
| 157 | 171 |
| 158 queue2.addAll(set); | 172 queue2.addAll(set); |
| 159 queue3.addAll(queue1); | 173 queue3.addAll(queue1); |
| 160 | 174 |
| 161 Expect.equals(0, set.length); | 175 Expect.equals(0, set.length); |
| 162 Expect.equals(0, queue1.length); | 176 Expect.equals(0, queue1.length); |
| 163 Expect.equals(0, queue2.length); | 177 Expect.equals(0, queue2.length); |
| 164 Expect.equals(0, queue3.length); | 178 Expect.equals(0, queue3.length); |
| 179 } |
| 165 | 180 |
| 181 void testLarge() { |
| 182 int N = 10000; |
| 183 Set set = new Set(); |
| 184 |
| 185 Queue queue = newQueue(); |
| 186 Expect.isTrue(queue.isEmpty); |
| 187 |
| 188 for (int i = 0; i < N; i++) { |
| 189 queue.add(i); |
| 190 set.add(i); |
| 191 } |
| 192 Expect.equals(N, queue.length); |
| 193 Expect.isFalse(queue.isEmpty); |
| 194 |
| 195 Iterable skip1 = queue.skip(1); |
| 196 Iterable take1 = queue.take(1); |
| 197 Iterable mapped = queue.map((e) => -e); |
| 198 |
| 199 for (int i = 0; i < 500; i++) { |
| 200 Expect.equals(i, take1.first); |
| 201 Expect.equals(i, queue.first); |
| 202 Expect.equals(-i, mapped.first); |
| 203 Expect.equals(i + 1, skip1.first); |
| 204 Expect.equals(i, queue.removeFirst()); |
| 205 Expect.equals(i + 1, take1.first); |
| 206 Expect.equals(-i - 1, mapped.first); |
| 207 Expect.equals(N - 1 - i, queue.last); |
| 208 Expect.equals(N - 1 - i, queue.removeLast()); |
| 209 } |
| 210 Expect.equals(N - 1000, queue.length); |
| 211 |
| 212 queue.retainAll(set); |
| 213 Expect.equals(N - 1000, queue.length); |
| 214 |
| 215 queue.remove(N >> 1); |
| 216 Expect.equals(N - 1001, queue.length); |
| 217 |
| 218 queue.removeAll(set); |
| 219 Expect.equals(0, queue.length); |
| 220 Expect.isTrue(queue.isEmpty); |
| 221 |
| 222 queue.addAll(set); |
| 223 Expect.equals(N, queue.length); |
| 224 Expect.isFalse(queue.isEmpty); |
| 225 |
| 226 // Iterate. |
| 227 for (var element in queue) { |
| 228 Expect.isTrue(set.contains(element)); |
| 229 } |
| 230 |
| 231 queue.forEach((element) { Expect.isTrue(set.contains(element)); }); |
| 232 |
| 233 queue.addAll(set); |
| 234 Expect.equals(N * 2, queue.length); |
| 235 Expect.isFalse(queue.isEmpty); |
| 236 |
| 237 queue.clear(); |
| 238 Expect.equals(0, queue.length); |
| 239 Expect.isTrue(queue.isEmpty); |
| 240 } |
| 241 } |
| 242 |
| 243 class ListQueueTest extends QueueTest { |
| 244 Queue newQueue() => new ListQueue(); |
| 245 Queue newQueueFrom(Iterable elements) => new ListQueue.from(elements); |
| 246 } |
| 247 |
| 248 class DoubleLinkedQueueTest extends QueueTest { |
| 249 Queue newQueue() => new DoubleLinkedQueue(); |
| 250 Queue newQueueFrom(Iterable elements) => new DoubleLinkedQueue.from(elements); |
| 251 |
| 252 void testMain() { |
| 253 super.testMain(); |
| 166 testQueueElements(); | 254 testQueueElements(); |
| 167 } | 255 } |
| 168 | 256 |
| 169 static testQueueElements() { | 257 void testQueueElements() { |
| 170 DoubleLinkedQueue<int> queue1 = new DoubleLinkedQueue<int>.from([1, 2, 4]); | 258 DoubleLinkedQueue<int> queue1 = new DoubleLinkedQueue<int>.from([1, 2, 4]); |
| 171 DoubleLinkedQueue<int> queue2 = new DoubleLinkedQueue<int>(); | 259 DoubleLinkedQueue<int> queue2 = new DoubleLinkedQueue<int>(); |
| 172 queue2.addAll(queue1); | 260 queue2.addAll(queue1); |
| 173 | 261 |
| 174 Expect.equals(queue1.length, queue2.length); | 262 Expect.equals(queue1.length, queue2.length); |
| 175 DoubleLinkedQueueEntry<int> entry1 = queue1.firstEntry(); | 263 DoubleLinkedQueueEntry<int> entry1 = queue1.firstEntry(); |
| 176 DoubleLinkedQueueEntry<int> entry2 = queue2.firstEntry(); | 264 DoubleLinkedQueueEntry<int> entry2 = queue2.firstEntry(); |
| 177 while (entry1 != null) { | 265 while (entry1 != null) { |
| 178 Expect.equals(true, !identical(entry1, entry2)); | 266 Expect.equals(true, !identical(entry1, entry2)); |
| 179 entry1 = entry1.nextEntry(); | 267 entry1 = entry1.nextEntry(); |
| 180 entry2 = entry2.nextEntry(); | 268 entry2 = entry2.nextEntry(); |
| 181 } | 269 } |
| 182 Expect.equals(null, entry2); | 270 Expect.equals(null, entry2); |
| 183 } | 271 } |
| 184 } | 272 } |
| 185 | 273 |
| 274 void trickyTest() { |
| 275 Queue q = new ListQueue(); |
| 276 |
| 277 for (int i = 0; i < 255; i++) { |
| 278 q.add(i); |
| 279 } |
| 280 for (int i = 0; i < 128; i++) { |
| 281 Expect.equals(i, q.removeFirst()); |
| 282 } |
| 283 q.add(255); |
| 284 for (int i = 0; i < 127; i++) { |
| 285 q.add(i); |
| 286 } |
| 287 |
| 288 Expect.equals(255, q.length); |
| 289 |
| 290 // Remove element at end of internal buffer. |
| 291 q.removeMatching((v) => v == 255); |
| 292 // Remove element at beginning of internal buffer. |
| 293 q.removeMatching((v) => v == 0); |
| 294 // Remove element at both ends of internal buffer. |
| 295 q.removeMatching((v) => v == 254 || v == 1); |
| 296 |
| 297 Expect.equals(251, q.length); |
| 298 |
| 299 Iterable i255 = new Iterable.generate(255, (x) => x); |
| 300 |
| 301 q = new ListQueue(); |
| 302 q.addAll(i255); |
| 303 Expect.listEquals(i255.toList(), q.toList()); |
| 304 |
| 305 q = new ListQueue(); |
| 306 q.addAll(i255.toList()); |
| 307 Expect.listEquals(i255.toList(), q.toList()); |
| 308 |
| 309 q = new ListQueue.from(i255); |
| 310 for (int i = 0; i < 128; i++) q.removeFirst(); |
| 311 q.add(256); |
| 312 q.add(0); |
| 313 q.addAll(i255.toList()); |
| 314 Expect.equals(129 + 255, q.length); |
| 315 |
| 316 // Test addAll that requires the queue to grow. |
| 317 q = new ListQueue(); |
| 318 q.addAll(i255.take(35)); |
| 319 q.addAll(i255.skip(35).take(96)); |
| 320 q.addAll(i255.skip(35 + 96)); |
| 321 Expect.listEquals(i255.toList(), q.toList()); |
| 322 } |
| 323 |
| 186 main() { | 324 main() { |
| 187 QueueTest.testMain(); | 325 new DoubleLinkedQueueTest().testMain(); |
| 326 new ListQueueTest().testMain(); |
| 327 trickyTest(); |
| 188 } | 328 } |
| OLD | NEW |