| 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 |
| 106 testAddAll(); | 109 testAddAll(); |
| 110 testLarge(); |
| 107 } | 111 } |
| 108 | 112 |
| 109 static void checkQueue(Queue queue, int expectedSize, int expectedSum) { | 113 void checkQueue(Queue queue, int expectedSize, int expectedSum) { |
| 110 Expect.equals(expectedSize, queue.length); | 114 Expect.equals(expectedSize, queue.length); |
| 111 int sum = 0; | 115 int sum = 0; |
| 112 void sumElements(int value) { | 116 void sumElements(int value) { |
| 113 sum += value; | 117 sum += value; |
| 114 } | 118 } |
| 115 queue.forEach(sumElements); | 119 queue.forEach(sumElements); |
| 116 Expect.equals(expectedSum, sum); | 120 Expect.equals(expectedSum, sum); |
| 117 } | 121 } |
| 118 | 122 |
| 119 static testAddAll() { | 123 void testAddAll() { |
| 120 Set<int> set = new Set<int>.from([1, 2, 4]); | 124 Set<int> set = new Set<int>.from([1, 2, 4]); |
| 121 | 125 |
| 122 Queue<int> queue1 = new Queue<int>.from(set); | 126 Queue queue1 = newQueueFrom(set); |
| 123 Queue<int> queue2 = new Queue<int>(); | 127 Queue queue2 = newQueue(); |
| 124 Queue<int> queue3 = new Queue<int>(); | 128 Queue queue3 = newQueue(); |
| 125 | 129 |
| 126 queue2.addAll(set); | 130 queue2.addAll(set); |
| 127 queue3.addAll(queue1); | 131 queue3.addAll(queue1); |
| 128 | 132 |
| 129 Expect.equals(3, set.length); | 133 Expect.equals(3, set.length); |
| 130 Expect.equals(3, queue1.length); | 134 Expect.equals(3, queue1.length); |
| 131 Expect.equals(3, queue2.length); | 135 Expect.equals(3, queue2.length); |
| 132 Expect.equals(3, queue3.length); | 136 Expect.equals(3, queue3.length); |
| 133 | 137 |
| 134 int sum = 0; | 138 int sum = 0; |
| 135 void f(e) { sum += e; }; | 139 void f(e) { sum += e; }; |
| 136 | 140 |
| 137 set.forEach(f); | 141 set.forEach(f); |
| 138 Expect.equals(7, sum); | 142 Expect.equals(7, sum); |
| 139 sum = 0; | 143 sum = 0; |
| 140 | 144 |
| 141 queue1.forEach(f); | 145 queue1.forEach(f); |
| 142 Expect.equals(7, sum); | 146 Expect.equals(7, sum); |
| 143 sum = 0; | 147 sum = 0; |
| 144 | 148 |
| 145 queue2.forEach(f); | 149 queue2.forEach(f); |
| 146 Expect.equals(7, sum); | 150 Expect.equals(7, sum); |
| 147 sum = 0; | 151 sum = 0; |
| 148 | 152 |
| 149 queue3.forEach(f); | 153 queue3.forEach(f); |
| 150 Expect.equals(7, sum); | 154 Expect.equals(7, sum); |
| 151 sum = 0; | 155 sum = 0; |
| 152 | 156 |
| 153 set = new Set<int>.from([]); | 157 set = new Set<int>.from([]); |
| 154 queue1 = new Queue<int>.from(set); | 158 queue1 = newQueueFrom(set); |
| 155 queue2 = new Queue<int>(); | 159 queue2 = newQueue(); |
| 156 queue3 = new Queue<int>(); | 160 queue3 = newQueue(); |
| 157 | 161 |
| 158 queue2.addAll(set); | 162 queue2.addAll(set); |
| 159 queue3.addAll(queue1); | 163 queue3.addAll(queue1); |
| 160 | 164 |
| 161 Expect.equals(0, set.length); | 165 Expect.equals(0, set.length); |
| 162 Expect.equals(0, queue1.length); | 166 Expect.equals(0, queue1.length); |
| 163 Expect.equals(0, queue2.length); | 167 Expect.equals(0, queue2.length); |
| 164 Expect.equals(0, queue3.length); | 168 Expect.equals(0, queue3.length); |
| 169 } |
| 165 | 170 |
| 171 void testLarge() { |
| 172 int N = 10000; |
| 173 Set set = new Set(); |
| 174 |
| 175 Queue queue = newQueue(); |
| 176 Expect.isTrue(queue.isEmpty); |
| 177 |
| 178 for (int i = 0; i < N; i++) { |
| 179 queue.add(i); |
| 180 set.add(i); |
| 181 } |
| 182 Expect.equals(N, queue.length); |
| 183 Expect.isFalse(queue.isEmpty); |
| 184 |
| 185 Iterable skip1 = queue.skip(1); |
| 186 Iterable take1 = queue.take(1); |
| 187 Iterable mapped = queue.map((e) => -e); |
| 188 |
| 189 for (int i = 0; i < 500; i++) { |
| 190 Expect.equals(i, take1.first); |
| 191 Expect.equals(i, queue.first); |
| 192 Expect.equals(-i, mapped.first); |
| 193 Expect.equals(i + 1, skip1.first); |
| 194 Expect.equals(i, queue.removeFirst()); |
| 195 Expect.equals(i + 1, take1.first); |
| 196 Expect.equals(-i - 1, mapped.first); |
| 197 Expect.equals(N - 1 - i, queue.last); |
| 198 Expect.equals(N - 1 - i, queue.removeLast()); |
| 199 } |
| 200 Expect.equals(N - 1000, queue.length); |
| 201 |
| 202 queue.retainAll(set); |
| 203 Expect.equals(N - 1000, queue.length); |
| 204 |
| 205 queue.remove(N >> 1); |
| 206 Expect.equals(N - 1001, queue.length); |
| 207 |
| 208 queue.removeAll(set); |
| 209 Expect.equals(0, queue.length); |
| 210 Expect.isTrue(queue.isEmpty); |
| 211 |
| 212 queue.addAll(set); |
| 213 Expect.equals(N, queue.length); |
| 214 Expect.isFalse(queue.isEmpty); |
| 215 |
| 216 // Iterate. |
| 217 for (var element in queue) { |
| 218 Expect.isTrue(set.contains(element)); |
| 219 } |
| 220 |
| 221 queue.forEach((element) { Expect.isTrue(set.contains(element)); }); |
| 222 |
| 223 queue.addAll(set); |
| 224 Expect.equals(N * 2, queue.length); |
| 225 Expect.isFalse(queue.isEmpty); |
| 226 |
| 227 queue.clear(); |
| 228 Expect.equals(0, queue.length); |
| 229 Expect.isTrue(queue.isEmpty); |
| 230 } |
| 231 } |
| 232 |
| 233 class ListQueueTest extends QueueTest { |
| 234 Queue newQueue() => new ListQueue(); |
| 235 Queue newQueueFrom(Iterable elements) => new ListQueue.from(elements); |
| 236 } |
| 237 |
| 238 class DoubleLinkedQueueTest extends QueueTest { |
| 239 Queue newQueue() => new DoubleLinkedQueue(); |
| 240 Queue newQueueFrom(Iterable elements) => new DoubleLinkedQueue.from(elements); |
| 241 |
| 242 void testMain() { |
| 243 super.testMain(); |
| 166 testQueueElements(); | 244 testQueueElements(); |
| 167 } | 245 } |
| 168 | 246 |
| 169 static testQueueElements() { | 247 void testQueueElements() { |
| 170 DoubleLinkedQueue<int> queue1 = new DoubleLinkedQueue<int>.from([1, 2, 4]); | 248 DoubleLinkedQueue<int> queue1 = new DoubleLinkedQueue<int>.from([1, 2, 4]); |
| 171 DoubleLinkedQueue<int> queue2 = new DoubleLinkedQueue<int>(); | 249 DoubleLinkedQueue<int> queue2 = new DoubleLinkedQueue<int>(); |
| 172 queue2.addAll(queue1); | 250 queue2.addAll(queue1); |
| 173 | 251 |
| 174 Expect.equals(queue1.length, queue2.length); | 252 Expect.equals(queue1.length, queue2.length); |
| 175 DoubleLinkedQueueEntry<int> entry1 = queue1.firstEntry(); | 253 DoubleLinkedQueueEntry<int> entry1 = queue1.firstEntry(); |
| 176 DoubleLinkedQueueEntry<int> entry2 = queue2.firstEntry(); | 254 DoubleLinkedQueueEntry<int> entry2 = queue2.firstEntry(); |
| 177 while (entry1 != null) { | 255 while (entry1 != null) { |
| 178 Expect.equals(true, !identical(entry1, entry2)); | 256 Expect.equals(true, !identical(entry1, entry2)); |
| 179 entry1 = entry1.nextEntry(); | 257 entry1 = entry1.nextEntry(); |
| 180 entry2 = entry2.nextEntry(); | 258 entry2 = entry2.nextEntry(); |
| 181 } | 259 } |
| 182 Expect.equals(null, entry2); | 260 Expect.equals(null, entry2); |
| 183 } | 261 } |
| 184 } | 262 } |
| 185 | 263 |
| 186 main() { | 264 main() { |
| 187 QueueTest.testMain(); | 265 new DoubleLinkedQueueTest().testMain(); |
| 266 new ListQueueTest().testMain(); |
| 188 } | 267 } |
| 268 |
| OLD | NEW |