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 | |
54 testListConstructor(); | 48 testListConstructor(); |
55 } | 49 } |
56 | 50 |
57 void testLength(int length, List list) { | 51 void testLength(int length, List list) { |
58 Expect.equals(length, list.length); | 52 Expect.equals(length, list.length); |
59 (length == 0 ? Expect.isTrue : Expect.isFalse)(list.isEmpty); | 53 (length == 0 ? Expect.isTrue : Expect.isFalse)(list.isEmpty); |
60 (length != 0 ? Expect.isTrue : Expect.isFalse)(list.isNotEmpty); | 54 (length != 0 ? Expect.isTrue : Expect.isFalse)(list.isNotEmpty); |
61 } | 55 } |
62 | 56 |
63 void testTypedLengthInvariantOperations(List list) { | 57 void testTypedLengthInvariantOperations(List list) { |
64 // length | 58 // length |
65 Expect.equals(list.length, 4); | 59 Expect.equals(list.length, 4); |
66 // operators [], []=. | 60 // operators [], []=. |
67 for (int i = 0; i < 4; i++) list[i] = 0; | 61 for (int i = 0; i < 4; i++) list[i] = 0; |
68 list[0] = 4; | 62 list[0] = 4; |
69 Expect.listEquals([4, 0, 0, 0], list); | 63 Expect.listEquals([4, 0, 0, 0], list); |
70 list[1] = 7; | 64 list[1] = 7; |
71 Expect.listEquals([4, 7, 0, 0], list); | 65 Expect.listEquals([4, 7, 0, 0], list); |
72 list[3] = 2; | 66 list[3] = 2; |
73 Expect.listEquals([4, 7, 0, 2], list); | 67 Expect.listEquals([4, 7, 0, 2], list); |
74 | 68 |
75 for (int i = 0; i < list.length; i++) { | 69 for (int i = 0; i < list.length; i++) { |
76 list[i] = i; | 70 list[i] = i; |
77 } | 71 } |
78 | 72 |
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 | |
96 // indexOf, lastIndexOf | 73 // indexOf, lastIndexOf |
97 for (int i = 0; i < 4; i++) { | 74 for (int i = 0; i < 4; i++) { |
98 Expect.equals(i, list[i]); | 75 Expect.equals(i, list[i]); |
99 Expect.equals(i, list.indexOf(i)); | 76 Expect.equals(i, list.indexOf(i)); |
100 Expect.equals(i, list.lastIndexOf(i)); | 77 Expect.equals(i, list.lastIndexOf(i)); |
101 } | 78 } |
102 | 79 |
103 // setRange. | 80 // setRange. |
104 list.setRange(0, 4, [3, 2, 1, 0]); | 81 list.setRange(0, 4, [3, 2, 1, 0]); |
105 Expect.listEquals([3, 2, 1, 0], list); | 82 Expect.listEquals([3, 2, 1, 0], list); |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 | 497 |
521 void testListConstructor() { | 498 void testListConstructor() { |
522 Expect.throws(() { new List(0).add(4); }); // Is fixed-length. | 499 Expect.throws(() { new List(0).add(4); }); // Is fixed-length. |
523 Expect.throws(() { new List(-2); }); // Not negative. /// 01: ok | 500 Expect.throws(() { new List(-2); }); // Not negative. /// 01: ok |
524 Expect.throws(() { new List(null); }); // Not null. | 501 Expect.throws(() { new List(null); }); // Not null. |
525 Expect.listEquals([4], new List()..add(4)); | 502 Expect.listEquals([4], new List()..add(4)); |
526 Expect.throws(() { new List.filled(0, 42).add(4); }); // Is fixed-length. | 503 Expect.throws(() { new List.filled(0, 42).add(4); }); // Is fixed-length. |
527 Expect.throws(() { new List.filled(-2, 42); }); // Not negative. | 504 Expect.throws(() { new List.filled(-2, 42); }); // Not negative. |
528 Expect.throws(() { new List.filled(null, 42); }); // Not null. | 505 Expect.throws(() { new List.filled(null, 42); }); // Not null. |
529 } | 506 } |
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 |