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. |
(...skipping 27 matching lines...) Expand all Loading... |
38 testGrowableList(new MyList([])); | 38 testGrowableList(new MyList([])); |
39 testGrowableList(new MyList([]).toList()); | 39 testGrowableList(new MyList([]).toList()); |
40 | 40 |
41 testTypedGrowableList(new Uint8List(0).toList()); | 41 testTypedGrowableList(new Uint8List(0).toList()); |
42 testTypedGrowableList(new Int8List(0).toList()); | 42 testTypedGrowableList(new Int8List(0).toList()); |
43 testTypedGrowableList(new Uint16List(0).toList()); | 43 testTypedGrowableList(new Uint16List(0).toList()); |
44 testTypedGrowableList(new Int16List(0).toList()); | 44 testTypedGrowableList(new Int16List(0).toList()); |
45 testTypedGrowableList(new Uint32List(0).toList()); | 45 testTypedGrowableList(new Uint32List(0).toList()); |
46 testTypedGrowableList(new Int32List(0).toList()); | 46 testTypedGrowableList(new Int32List(0).toList()); |
47 | 47 |
| 48 // Lists based on ListBase and ListMixin. |
| 49 testGrowableList(new BaseList()); |
| 50 testGrowableList(new BaseList().toList()); |
| 51 testGrowableList(new MixinList()); |
| 52 testGrowableList(new MixinList().toList()); |
| 53 |
48 testListConstructor(); | 54 testListConstructor(); |
49 } | 55 } |
50 | 56 |
51 void testLength(int length, List list) { | 57 void testLength(int length, List list) { |
52 Expect.equals(length, list.length); | 58 Expect.equals(length, list.length); |
53 (length == 0 ? Expect.isTrue : Expect.isFalse)(list.isEmpty); | 59 (length == 0 ? Expect.isTrue : Expect.isFalse)(list.isEmpty); |
54 (length != 0 ? Expect.isTrue : Expect.isFalse)(list.isNotEmpty); | 60 (length != 0 ? Expect.isTrue : Expect.isFalse)(list.isNotEmpty); |
55 } | 61 } |
56 | 62 |
57 void testTypedLengthInvariantOperations(List list) { | 63 void testTypedLengthInvariantOperations(List list) { |
58 // length | 64 // length |
59 Expect.equals(list.length, 4); | 65 Expect.equals(list.length, 4); |
60 // operators [], []=. | 66 // operators [], []=. |
61 for (int i = 0; i < 4; i++) list[i] = 0; | 67 for (int i = 0; i < 4; i++) list[i] = 0; |
62 list[0] = 4; | 68 list[0] = 4; |
63 Expect.listEquals([4, 0, 0, 0], list); | 69 Expect.listEquals([4, 0, 0, 0], list); |
64 list[1] = 7; | 70 list[1] = 7; |
65 Expect.listEquals([4, 7, 0, 0], list); | 71 Expect.listEquals([4, 7, 0, 0], list); |
66 list[3] = 2; | 72 list[3] = 2; |
67 Expect.listEquals([4, 7, 0, 2], list); | 73 Expect.listEquals([4, 7, 0, 2], list); |
68 | 74 |
69 for (int i = 0; i < list.length; i++) { | 75 for (int i = 0; i < list.length; i++) { |
70 list[i] = i; | 76 list[i] = i; |
71 } | 77 } |
72 | 78 |
| 79 // last= |
| 80 Expect.listEquals([0, 1, 2, 3], list); |
| 81 list.last = 47; |
| 82 Expect.listEquals([0, 1, 2, 47], list); |
| 83 list.last -= 5; |
| 84 Expect.listEquals([0, 1, 2, 42], list); |
| 85 list.last++; |
| 86 Expect.listEquals([0, 1, 2, 43], list); |
| 87 ++list.last; |
| 88 Expect.listEquals([0, 1, 2, 44], list); |
| 89 list.last--; |
| 90 Expect.listEquals([0, 1, 2, 43], list); |
| 91 --list.last; |
| 92 Expect.listEquals([0, 1, 2, 42], list); |
| 93 list.last = 3; |
| 94 Expect.listEquals([0, 1, 2, 3], list); |
| 95 |
73 // indexOf, lastIndexOf | 96 // indexOf, lastIndexOf |
74 for (int i = 0; i < 4; i++) { | 97 for (int i = 0; i < 4; i++) { |
75 Expect.equals(i, list[i]); | 98 Expect.equals(i, list[i]); |
76 Expect.equals(i, list.indexOf(i)); | 99 Expect.equals(i, list.indexOf(i)); |
77 Expect.equals(i, list.lastIndexOf(i)); | 100 Expect.equals(i, list.lastIndexOf(i)); |
78 } | 101 } |
79 | 102 |
80 // setRange. | 103 // setRange. |
81 list.setRange(0, 4, [3, 2, 1, 0]); | 104 list.setRange(0, 4, [3, 2, 1, 0]); |
82 Expect.listEquals([3, 2, 1, 0], list); | 105 Expect.listEquals([3, 2, 1, 0], list); |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
497 | 520 |
498 void testListConstructor() { | 521 void testListConstructor() { |
499 Expect.throws(() { new List(0).add(4); }); // Is fixed-length. | 522 Expect.throws(() { new List(0).add(4); }); // Is fixed-length. |
500 Expect.throws(() { new List(-2); }); // Not negative. /// 01: ok | 523 Expect.throws(() { new List(-2); }); // Not negative. /// 01: ok |
501 Expect.throws(() { new List(null); }); // Not null. | 524 Expect.throws(() { new List(null); }); // Not null. |
502 Expect.listEquals([4], new List()..add(4)); | 525 Expect.listEquals([4], new List()..add(4)); |
503 Expect.throws(() { new List.filled(0, 42).add(4); }); // Is fixed-length. | 526 Expect.throws(() { new List.filled(0, 42).add(4); }); // Is fixed-length. |
504 Expect.throws(() { new List.filled(-2, 42); }); // Not negative. | 527 Expect.throws(() { new List.filled(-2, 42); }); // Not negative. |
505 Expect.throws(() { new List.filled(null, 42); }); // Not null. | 528 Expect.throws(() { new List.filled(null, 42); }); // Not null. |
506 } | 529 } |
| 530 |
| 531 abstract class ListImpl<E>{ |
| 532 List _base = []; |
| 533 int get length => _base.length; |
| 534 void set length(int length) { _base.length = length; } |
| 535 E operator[](int index) => _base[index]; |
| 536 void operator[]=(int index, E value) { _base[index] = value; } |
| 537 } |
| 538 |
| 539 class BaseList<E> = ListBase<E> with ListImpl<E>; |
| 540 |
| 541 class MixinList<E> = ListImpl<E> with ListMixin<E>; |
OLD | NEW |