Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Side by Side Diff: tests/corelib/list_test.dart

Issue 14048002: Make the analyzer happy about collections. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Reupload. Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/corelib/collection_removes_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "package:expect/expect.dart"; 6 import "package:expect/expect.dart";
6 7
7 class ListTest { 8 void main() {
9 testFixedLengthList(new List(4));
10 // ListBase implementation of List.
11 testFixedLengthList(new MyFixedList(new List(4)));
8 12
9 static testMain() { 13 testGrowableList(new List());
10 testList(); 14 testGrowableList([]);
11 testExpandableList(); 15 testGrowableList(new MyList([]));
16 }
17
18 void expectValues(list, val1, val2, val3, val4) {
19 Expect.isFalse(list.isEmpty);
20 Expect.equals(4, list.length);
21 Expect.equals(list[0], val1);
22 Expect.equals(list[1], val2);
23 Expect.equals(list[2], val3);
24 Expect.equals(list[3], val4);
25 }
26
27 void testClosures(List list) {
28 testMap(val) {return val * 2 + 10; }
29 List mapped = list.map(testMap).toList();
30 Expect.equals(mapped.length, list.length);
31 for (var i = 0; i < list.length; i++) {
32 Expect.equals(mapped[i], list[i]*2 + 10);
12 } 33 }
13 34
14 static void expectValues(list, val1, val2, val3, val4) { 35 testFilter(val) { return val == 3; }
15 Expect.equals(true, list.length == 4); 36 Iterable filtered = list.where(testFilter);
16 Expect.equals(true, list.length == 4); 37 Expect.equals(filtered.length, 1);
17 Expect.equals(true, !list.isEmpty); 38
18 Expect.equals(list[0], val1); 39 testEvery(val) { return val != 11; }
19 Expect.equals(list[1], val2); 40 bool test = list.every(testEvery);
20 Expect.equals(list[2], val3); 41 Expect.isTrue(test);
21 Expect.equals(list[3], val4); 42
43 testSome(val) { return val == 1; }
44 test = list.any(testSome);
45 Expect.isTrue(test);
46
47 testSomeFirst(val) { return val == 0; }
48 test = list.any(testSomeFirst);
49 Expect.isTrue(test);
50
51 testSomeLast(val) { return val == (list.length - 1); }
52 test = list.any(testSomeLast);
53 Expect.isTrue(test);
54 }
55
56 void testFixedLengthList(List list) {
57 Expect.equals(list.length, 4);
58 list[0] = 4;
59 expectValues(list, 4, null, null, null);
60 String val = "fisk";
61 list[1] = val;
62 expectValues(list, 4, val, null, null);
63 double d = 2.0;
64 list[3] = d;
65 expectValues(list, 4, val, null, d);
66
67 for (int i = 0; i < list.length; i++) {
68 list[i] = i;
22 } 69 }
23 70
24 static void testClosures(List list) { 71 for (int i = 0; i < 4; i++) {
25 testMap(val) {return val * 2 + 10; } 72 Expect.equals(i, list[i]);
26 List mapped = list.map(testMap).toList(); 73 Expect.equals(i, list.indexOf(i));
27 Expect.equals(mapped.length, list.length); 74 Expect.equals(i, list.lastIndexOf(i));
28 for (var i = 0; i < list.length; i++) {
29 Expect.equals(mapped[i], list[i]*2 + 10);
30 }
31
32 testFilter(val) { return val == 3; }
33 Iterable filtered = list.where(testFilter);
34 Expect.equals(filtered.length, 1);
35
36 testEvery(val) { return val != 11; }
37 bool test = list.every(testEvery);
38 Expect.equals(true, test);
39
40 testSome(val) { return val == 1; }
41 test = list.any(testSome);
42 Expect.equals(true, test);
43
44 testSomeFirst(val) { return val == 0; }
45 test = list.any(testSomeFirst);
46 Expect.equals(true, test);
47
48 testSomeLast(val) { return val == (list.length - 1); }
49 test = list.any(testSomeLast);
50 Expect.equals(true, test);
51 } 75 }
52 76
53 static void testList() { 77 Expect.equals(-1, list.indexOf(100));
54 List list = new List(4); 78 Expect.equals(-1, list.lastIndexOf(100));
55 Expect.equals(list.length, 4); 79 list[2] = new Yes();
56 list[0] = 4; 80 Expect.equals(2, list.indexOf(100));
57 expectValues(list, 4, null, null, null); 81 Expect.equals(2, list.lastIndexOf(100));
58 String val = "fisk"; 82 list[3] = new Yes();
59 list[1] = val; 83 Expect.equals(2, list.indexOf(100));
60 expectValues(list, 4, val, null, null); 84 Expect.equals(3, list.lastIndexOf(100));
61 double d = 2.0; 85 list[2] = 2;
62 list[3] = d; 86 Expect.equals(3, list.indexOf(100));
63 expectValues(list, 4, val, null, d); 87 Expect.equals(3, list.lastIndexOf(100));
88 list[3] = 3;
89 Expect.equals(-1, list.indexOf(100));
90 Expect.equals(-1, list.lastIndexOf(100));
64 91
65 for (int i = 0; i < list.length; i++) { 92 testClosures(list);
66 list[i] = i;
67 }
68 93
69 for (int i = 0; i < 4; i++) { 94 Expect.throws(list.clear, (e) => e is UnsupportedError);
70 Expect.equals(i, list[i]); 95 }
71 Expect.equals(i, list.indexOf(i));
72 Expect.equals(i, list.lastIndexOf(i));
73 }
74 96
75 Expect.equals(-1, list.indexOf(100)); 97 void testGrowableList(List list) {
76 Expect.equals(-1, list.lastIndexOf(100)); 98 Expect.isTrue(list.isEmpty);
77 list[2] = new Yes(); 99 Expect.equals(list.length, 0);
78 Expect.equals(2, list.indexOf(100)); 100 list.add(4);
79 Expect.equals(2, list.lastIndexOf(100)); 101 Expect.equals(1, list.length);
80 list[3] = new Yes(); 102 Expect.isTrue(!list.isEmpty);
81 Expect.equals(2, list.indexOf(100)); 103 Expect.equals(list.length, 1);
82 Expect.equals(3, list.lastIndexOf(100)); 104 Expect.equals(list.length, 1);
83 list[2] = 2; 105 Expect.equals(list.removeLast(), 4);
84 Expect.equals(3, list.indexOf(100));
85 Expect.equals(3, list.lastIndexOf(100));
86 list[3] = 3;
87 Expect.equals(-1, list.indexOf(100));
88 Expect.equals(-1, list.lastIndexOf(100));
89 106
90 testClosures(list); 107 for (int i = 0; i < 10; i++) {
91 108 list.add(i);
92 var exception = null;
93 try {
94 list.clear();
95 } on UnsupportedError catch (e) {
96 exception = e;
97 }
98 Expect.equals(true, exception != null);
99 } 109 }
100 110
101 static void testExpandableList() { 111 Expect.equals(list.length, 10);
102 List list = new List(); 112 for (int i = 0; i < 10; i++) {
103 Expect.equals(true, list.isEmpty); 113 Expect.equals(i, list[i]);
104 Expect.equals(list.length, 0); 114 Expect.equals(i, list.indexOf(i));
105 list.add(4); 115 Expect.equals(i, list.lastIndexOf(i));
106 Expect.equals(1, list.length); 116 }
107 Expect.equals(true, !list.isEmpty);
108 Expect.equals(list.length, 1);
109 Expect.equals(list.length, 1);
110 Expect.equals(list.removeLast(), 4);
111 117
112 for (int i = 0; i < 10; i++) { 118 Expect.equals(-1, list.indexOf(100));
113 list.add(i); 119 Expect.equals(-1, list.lastIndexOf(100));
114 } 120 list[2] = new Yes();
121 Expect.equals(2, list.indexOf(100));
122 Expect.equals(2, list.lastIndexOf(100));
123 list[3] = new Yes();
124 Expect.equals(2, list.indexOf(100));
125 Expect.equals(3, list.lastIndexOf(100));
126 list[2] = 2;
127 Expect.equals(3, list.indexOf(100));
128 Expect.equals(3, list.lastIndexOf(100));
129 list[3] = 3;
130 Expect.equals(-1, list.indexOf(100));
131 Expect.equals(-1, list.lastIndexOf(100));
115 132
116 Expect.equals(list.length, 10); 133 testClosures(list);
117 for (int i = 0; i < 10; i++) {
118 Expect.equals(i, list[i]);
119 Expect.equals(i, list.indexOf(i));
120 Expect.equals(i, list.lastIndexOf(i));
121 }
122 134
123 Expect.equals(-1, list.indexOf(100)); 135 Expect.equals(9, list.removeLast());
124 Expect.equals(-1, list.lastIndexOf(100)); 136 list.clear();
125 list[2] = new Yes(); 137 Expect.equals(0, list.length);
126 Expect.equals(2, list.indexOf(100)); 138 Expect.isTrue(list.isEmpty);
127 Expect.equals(2, list.lastIndexOf(100));
128 list[3] = new Yes();
129 Expect.equals(2, list.indexOf(100));
130 Expect.equals(3, list.lastIndexOf(100));
131 list[2] = 2;
132 Expect.equals(3, list.indexOf(100));
133 Expect.equals(3, list.lastIndexOf(100));
134 list[3] = 3;
135 Expect.equals(-1, list.indexOf(100));
136 Expect.equals(-1, list.lastIndexOf(100));
137
138 testClosures(list);
139
140 Expect.equals(list.removeLast(), 9);
141 list.clear();
142 Expect.equals(list.length, 0);
143 Expect.equals(list.length, 0);
144 Expect.equals(true, list.isEmpty);
145 }
146 } 139 }
147 140
148 class Yes { 141 class Yes {
149 operator ==(var other) => true; 142 operator ==(var other) => true;
150 } 143 }
151 144
152 main() { 145 class MyList<E> extends ListBase<E> {
153 ListTest.testMain(); 146 List<E> _source;
147 MyList(this._source);
148 int get length => _source.length;
149 void set length(int length) { _source.length = length; }
150 E operator[](int index) => _source[index];
151 void operator[]=(int index, E value) { _source[index] = value; }
154 } 152 }
153
154 class MyFixedList<E> extends ListBase<E> {
155 List<E> _source;
156 MyFixedList(this._source);
157 int get length => _source.length;
158 void set length(int length) { throw new UnsupportedError("Fixed length!"); }
159 E operator[](int index) => _source[index];
160 void operator[]=(int index, E value) { _source[index] = value; }
161 }
OLDNEW
« no previous file with comments | « tests/corelib/collection_removes_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698