| 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 import "dart:collection"; | 5 import "dart:collection"; |
| 6 import "dart:typed_data"; | 6 import "dart:typed_data"; |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 | 8 |
| 9 void main() { | 9 void main() { |
| 10 // Typed lists - fixed length and can only contain integers. | 10 // Typed lists - fixed length and can only contain integers. |
| 11 testTypedList(new Uint8List(4)); | 11 testTypedList(new Uint8List(4)); |
| 12 testTypedList(new Int8List(4)); | 12 testTypedList(new Int8List(4)); |
| 13 testTypedList(new Uint16List(4)); | 13 testTypedList(new Uint16List(4)); |
| 14 testTypedList(new Int16List(4)); | 14 testTypedList(new Int16List(4)); |
| 15 testTypedList(new Uint32List(4)); | 15 testTypedList(new Uint32List(4)); |
| 16 testTypedList(new Int32List(4)); | 16 testTypedList(new Int32List(4)); |
| 17 testTypedList(new Uint8List(4).toList(growable: false)); |
| 18 testTypedList(new Int8List(4).toList(growable: false)); |
| 19 testTypedList(new Uint16List(4).toList(growable: false)); |
| 20 testTypedList(new Int16List(4).toList(growable: false)); |
| 21 testTypedList(new Uint32List(4).toList(growable: false)); |
| 22 testTypedList(new Int32List(4).toList(growable: false)); |
| 17 | 23 |
| 18 // Fixed length lists, length 4. | 24 // Fixed length lists, length 4. |
| 19 testFixedLengthList(new List(4)); | 25 testFixedLengthList(new List(4)); |
| 20 testFixedLengthList(new List(4).toList(growable: false)); | 26 testFixedLengthList(new List(4).toList(growable: false)); |
| 21 testFixedLengthList((new List()..length = 4).toList(growable: false)); | 27 testFixedLengthList((new List()..length = 4).toList(growable: false)); |
| 22 // ListBase implementation of List. | 28 // ListBase implementation of List. |
| 23 testFixedLengthList(new MyFixedList(new List(4))); | 29 testFixedLengthList(new MyFixedList(new List(4))); |
| 24 testFixedLengthList(new MyFixedList(new List(4)).toList(growable: false)); | 30 testFixedLengthList(new MyFixedList(new List(4)).toList(growable: false)); |
| 25 | 31 |
| 26 testFixedLengthList(new Uint8List(4).toList(growable: false)); | |
| 27 testFixedLengthList(new Int8List(4).toList(growable: false)); | |
| 28 testFixedLengthList(new Uint16List(4).toList(growable: false)); | |
| 29 testFixedLengthList(new Int16List(4).toList(growable: false)); | |
| 30 testFixedLengthList(new Uint32List(4).toList(growable: false)); | |
| 31 testFixedLengthList(new Int32List(4).toList(growable: false)); | |
| 32 | |
| 33 // Growable lists. Initial length 0. | 32 // Growable lists. Initial length 0. |
| 34 testGrowableList(new List()); | 33 testGrowableList(new List()); |
| 35 testGrowableList(new List().toList()); | 34 testGrowableList(new List().toList()); |
| 36 testGrowableList(new List(0).toList()); | 35 testGrowableList(new List(0).toList()); |
| 37 testGrowableList([]); | 36 testGrowableList([]); |
| 38 testGrowableList((const []).toList()); | 37 testGrowableList((const []).toList()); |
| 39 testGrowableList(new MyList([])); | 38 testGrowableList(new MyList([])); |
| 40 testGrowableList(new MyList([]).toList()); | 39 testGrowableList(new MyList([]).toList()); |
| 41 testGrowableList(new Uint8List(0).toList()); | 40 |
| 42 testGrowableList(new Int8List(0).toList()); | 41 testTypedGrowableList(new Uint8List(0).toList()); |
| 43 testGrowableList(new Uint16List(0).toList()); | 42 testTypedGrowableList(new Int8List(0).toList()); |
| 44 testGrowableList(new Int16List(0).toList()); | 43 testTypedGrowableList(new Uint16List(0).toList()); |
| 45 testGrowableList(new Uint32List(0).toList()); | 44 testTypedGrowableList(new Int16List(0).toList()); |
| 46 testGrowableList(new Int32List(0).toList()); | 45 testTypedGrowableList(new Uint32List(0).toList()); |
| 46 testTypedGrowableList(new Int32List(0).toList()); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void testLength(int length, List list) { | 49 void testLength(int length, List list) { |
| 50 Expect.equals(length, list.length); | 50 Expect.equals(length, list.length); |
| 51 (length == 0 ? Expect.isTrue : Expect.isFalse)(list.isEmpty); | 51 (length == 0 ? Expect.isTrue : Expect.isFalse)(list.isEmpty); |
| 52 (length != 0 ? Expect.isTrue : Expect.isFalse)(list.isNotEmpty); | 52 (length != 0 ? Expect.isTrue : Expect.isFalse)(list.isNotEmpty); |
| 53 } | 53 } |
| 54 | 54 |
| 55 void testTypedLengthInvariantOperations(List list) { | 55 void testTypedLengthInvariantOperations(List list) { |
| 56 // length | 56 // length |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 isUnsupported(() => list.removeLast()); | 230 isUnsupported(() => list.removeLast()); |
| 231 isUnsupported(() => list.insert(0, 1)); | 231 isUnsupported(() => list.insert(0, 1)); |
| 232 isUnsupported(() => list.insertAll(0, [1])); | 232 isUnsupported(() => list.insertAll(0, [1])); |
| 233 isUnsupported(() => list.clear()); | 233 isUnsupported(() => list.clear()); |
| 234 isUnsupported(() => list.remove(1)); | 234 isUnsupported(() => list.remove(1)); |
| 235 isUnsupported(() => list.removeAt(1)); | 235 isUnsupported(() => list.removeAt(1)); |
| 236 isUnsupported(() => list.removeRange(0, 1)); | 236 isUnsupported(() => list.removeRange(0, 1)); |
| 237 isUnsupported(() => list.replaceRange(0, 1, [])); | 237 isUnsupported(() => list.replaceRange(0, 1, [])); |
| 238 } | 238 } |
| 239 | 239 |
| 240 void testTypedGrowableList(List list) { |
| 241 testLength(0, list); |
| 242 // set length. |
| 243 list.length = 4; |
| 244 testLength(4, list); |
| 245 |
| 246 testTypedLengthInvariantOperations(list); |
| 247 |
| 248 testGrowableListOperations(list); |
| 249 } |
| 250 |
| 240 void testGrowableList(List list) { | 251 void testGrowableList(List list) { |
| 241 testLength(0, list); | 252 testLength(0, list); |
| 242 // set length. | 253 // set length. |
| 243 list.length = 4; | 254 list.length = 4; |
| 244 testLength(4, list); | 255 testLength(4, list); |
| 245 | 256 |
| 246 testLengthInvariantOperations(list); | 257 testLengthInvariantOperations(list); |
| 247 | 258 |
| 259 testGrowableListOperations(list); |
| 260 } |
| 261 |
| 262 void testGrowableListOperations(List list) { |
| 248 // add, removeLast. | 263 // add, removeLast. |
| 249 list.clear(); | 264 list.clear(); |
| 250 testLength(0, list); | 265 testLength(0, list); |
| 251 list.add(4); | 266 list.add(4); |
| 252 testLength(1, list); | 267 testLength(1, list); |
| 253 Expect.equals(4, list.removeLast()); | 268 Expect.equals(4, list.removeLast()); |
| 254 testLength(0, list); | 269 testLength(0, list); |
| 255 | 270 |
| 256 for (int i = 0; i < 100; i++) { | 271 for (int i = 0; i < 100; i++) { |
| 257 list.add(i); | 272 list.add(i); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 } | 485 } |
| 471 | 486 |
| 472 class MyFixedList<E> extends ListBase<E> { | 487 class MyFixedList<E> extends ListBase<E> { |
| 473 List<E> _source; | 488 List<E> _source; |
| 474 MyFixedList(this._source); | 489 MyFixedList(this._source); |
| 475 int get length => _source.length; | 490 int get length => _source.length; |
| 476 void set length(int length) { throw new UnsupportedError("Fixed length!"); } | 491 void set length(int length) { throw new UnsupportedError("Fixed length!"); } |
| 477 E operator[](int index) => _source[index]; | 492 E operator[](int index) => _source[index]; |
| 478 void operator[]=(int index, E value) { _source[index] = value; } | 493 void operator[]=(int index, E value) { _source[index] = value; } |
| 479 } | 494 } |
| OLD | NEW |