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:test/test.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(8); |
| 33 expect(queue, equals([1, 2, 3, 4, 5, 6, 7, 8])); |
| 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([8, 9]); |
| 47 expect(queue, equals([1, 2, 3, 4, 5, 6, 7, 8, 9])); |
| 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])); |
| 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])); |
| 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(7)); |
| 106 expect(queue, equals([1, 2, 3, 4, 5, 6])); |
| 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(7)); |
| 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 test("throws a RangeError if the index is less than 0", () { |
| 166 var queue = new QueueList.from([1, 2, 3]); |
| 167 expect(() => queue[-1], throwsRangeError); |
| 168 }); |
| 169 |
| 170 test("throws a RangeError if the index is greater than or equal to the " |
| 171 "length", () { |
| 172 var queue = new QueueList.from([1, 2, 3]); |
| 173 expect(() => queue[3], throwsRangeError); |
| 174 }); |
| 175 }); |
| 176 |
| 177 group("[]=", () { |
| 178 test("sets individual entries in the queue", () { |
| 179 var queue = new QueueList.from([1, 2, 3]); |
| 180 queue[0] = "a"; |
| 181 queue[1] = "b"; |
| 182 queue[2] = "c"; |
| 183 expect(queue, equals(["a", "b", "c"])); |
| 184 }); |
| 185 |
| 186 test("sets individual entries in a queue with an internal gap", () { |
| 187 var queue = withInternalGap(); |
| 188 queue[0] = "a"; |
| 189 queue[1] = "b"; |
| 190 queue[2] = "c"; |
| 191 queue[3] = "d"; |
| 192 queue[4] = "e"; |
| 193 queue[5] = "f"; |
| 194 queue[6] = "g"; |
| 195 expect(queue, equals(["a", "b", "c", "d", "e", "f", "g"])); |
| 196 }); |
| 197 |
| 198 test("throws a RangeError if the index is less than 0", () { |
| 199 var queue = new QueueList.from([1, 2, 3]); |
| 200 expect(() { |
| 201 queue[-1] = 0; |
| 202 }, throwsRangeError); |
| 203 }); |
| 204 |
| 205 test("throws a RangeError if the index is greater than or equal to the " |
| 206 "length", () { |
| 207 var queue = new QueueList.from([1, 2, 3]); |
| 208 expect(() { |
| 209 queue[3] = 4; |
| 210 }, throwsRangeError); |
| 211 }); |
| 212 }); |
| 213 |
| 214 group("throws a modification error for", () { |
| 215 var queue; |
| 216 setUp(() { |
| 217 queue = new QueueList.from([1, 2, 3]); |
| 218 }); |
| 219 |
| 220 test("add", () { |
| 221 expect(() => queue.forEach((_) => queue.add(4)), |
| 222 throwsConcurrentModificationError); |
| 223 }); |
| 224 |
| 225 test("addAll", () { |
| 226 expect(() => queue.forEach((_) => queue.addAll([4, 5, 6])), |
| 227 throwsConcurrentModificationError); |
| 228 }); |
| 229 |
| 230 test("addFirst", () { |
| 231 expect(() => queue.forEach((_) => queue.addFirst(0)), |
| 232 throwsConcurrentModificationError); |
| 233 }); |
| 234 |
| 235 test("removeFirst", () { |
| 236 expect(() => queue.forEach((_) => queue.removeFirst()), |
| 237 throwsConcurrentModificationError); |
| 238 }); |
| 239 |
| 240 test("removeLast", () { |
| 241 expect(() => queue.forEach((_) => queue.removeLast()), |
| 242 throwsConcurrentModificationError); |
| 243 }); |
| 244 |
| 245 test("length=", () { |
| 246 expect(() => queue.forEach((_) => queue.length = 1), |
| 247 throwsConcurrentModificationError); |
| 248 }); |
| 249 }); |
| 250 } |
| 251 |
| 252 /// Returns a queue whose internal ring buffer is full enough that adding a new |
| 253 /// element will expand it. |
| 254 QueueList atCapacity() { |
| 255 // Use addAll because [new QueueList.from(List)] won't use the default initial |
| 256 // capacity of 8. |
| 257 return new QueueList()..addAll([1, 2, 3, 4, 5, 6, 7]); |
| 258 } |
| 259 |
| 260 /// Returns a queue whose internal tail has a lower index than its head. |
| 261 QueueList withInternalGap() { |
| 262 var queue = new QueueList.from([null, null, null, null, 1, 2, 3, 4]); |
| 263 for (var i = 0; i < 4; i++) { |
| 264 queue.removeFirst(); |
| 265 } |
| 266 for (var i = 5; i < 8; i++) { |
| 267 queue.addLast(i); |
| 268 } |
| 269 return queue; |
| 270 } |
| 271 |
| 272 /// Returns a matcher that expects that a closure throws a |
| 273 /// [ConcurrentModificationError]. |
| 274 final throwsConcurrentModificationError = throwsA( |
| 275 new isInstanceOf<ConcurrentModificationError>()); |
OLD | NEW |