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

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

Issue 1056353004: Add List.unmodifiable constructor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove left-over debug coide. Created 5 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
« sdk/lib/internal/list.dart ('K') | « sdk/lib/internal/list.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
(Empty)
1 // Copyright (c) 2015, 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:expect/expect.dart";
6 import "dart:collection";
7 import "dart:typed_data";
8
9 main() {
10 var intTest = new Test<int>();
11 intTest.run("ConstList", createConstList);
12 intTest.run("FixedList", createFixedList);
13 intTest.run("GrowableList", createGrowableList);
14 intTest.run("ConstMapKeys", createConstMapKeys);
15 intTest.run("ConstMapValues", createConstMapValues);
16 intTest.run("MapKeys", createMapKeys);
17 intTest.run("MapValues", createMapValues);
18 intTest.run("SplayMapKeys", createSplayMapKeys);
19 intTest.run("SplayMapValues", createSplayMapValues);
20 intTest.run("Set", createSet);
21 intTest.run("SplaySet", createSplaySet);
22 intTest.run("Queue", createQueue);
23 intTest.run("ListMapKeys", createListMapKeys);
24 intTest.run("ListMapValues", createListMapValues);
25 intTest.run("CodeUnits", createCodeUnits);
26 intTest.run("TypedList", createTypedList);
27
28 new Test<String>().test("strings", ["a", "b", "c"]);
29
30 new Test<num>().test("superclass", <int>[1, 2, 3]);
31 new Test<int>().test("subclass", <num>[1, 2, 3]);
32 }
33
34 class Test<E> {
35 run(name, Iterable create(int size)) {
36 test(name, create(0));
37 test(name, create(1));
38 test(name, create(3));
39 }
40
41 test(name, iterable) {
42 testSingle(name, iterable);
43 testSingle("$name-where", iterable.where((x) => true));
44 testSingle("$name-map", iterable.map((x) => x));
45 testSingle("$name-expand", iterable.expand((x) => [x, x]));
46 testSingle("$name-skip", iterable.skip(1));
47 testSingle("$name-take", iterable.take(2));
48 testSingle("$name-skipWhile", iterable.skipWhile((x) => false));
49 testSingle("$name-takeWhile", iterable.takeWhile((x) => true));
50 }
51
52 testSingle(name, iterable) {
53 var elements = iterable.toList();
54 int length = elements.length;
55
56 var list = new List<E>.unmodifiable(iterable);
57
58 Expect.isTrue(list is List<E>, "$name-type-$E");
59 Expect.isTrue(list is! List<Test>, "$name-!type-!$E");
60
61 checkElements() {
62 Expect.equals(length, list.length);
63 for (int i = 0; i < length; i++) {
64 Expect.identical(elements[i], list[i], "$name-identical-$i");
65 }
66 }
67
68 checkElements();
69
70 throws(funcName, func) {
71 try {
72 func();
73 } catch (e, s) {
74 Expect.isTrue(e is UnsupportedError, "$name: $funcName threw $e");
75 return;
76 }
77 checkElements();
78 Expect.fail("$name: $funcName didn't throw");
79 }
80
81 throws("[]=", () { list[0] = null; });
82 throws("length=", () { list.length = length + 1; });
83 throws("length=", () { list.length = length - 1; });
84 throws("setAll", () { list.setAll(0, []); });
85 throws("add", () { list.add(null); });
86 throws("insert", () { list.insert(0, null); });
87 throws("insertAll", () { list.insertAll(0, []); });
88 throws("addAll", () { list.addAll([]); });
89 throws("remove", () { list.remove(null); });
90 throws("removeWhere", () { list.removeWhere((x) => true); });
91 throws("retainWhere", () { list.retainWhere((x) => false); });
92 throws("sort", () { list.sort(); });
93 throws("shuffle", () { list.shuffle(); });
94 throws("clear", () { list.clear(); });
95 throws("removeAt", () { list.removeAt(0); });
96 throws("removeLast", () { list.removeLast(); });
97 throws("setRange", () { list.setRange(0, 1, []); });
98 throws("removeRange", () { list.removeRange(0, 1); });
99 throws("replaceRange", () { list.replaceRange(0, 1, []); });
100 throws("fillRange", () { list.fillRange(0, 1, null); });
101
102 success(opName, op(list)) {
103 var expect;
104 try {
105 expect = op(elements);
106 } catch (e) {
107 try {
108 op(list);
109 } catch (e2) {
110 Expect.equals(e.runtimeType, e2.runtimeType);
111 return;
112 }
113 Expect.fail("$name-$opName didn't throw, expected: $e");
114 }
115 var actual = op(list);
116 checkElements();
117 if (expect is List) {
118 Expect.listEquals(expect, actual, "$name-$opName");
119 } else if (expect is Iterable) {
120 Expect.isTrue(actual is Iterable);
121 Expect.listEquals(expect.toList(), actual.toList(), "$name-$opName");
122 } else {
123 Expect.equals(expect, actual, "$name-$opName");
124 }
125 }
126
127 success("indexOf", (l) => l.indexOf(null));
128 success("lastIndexOf", (l) => l.lastIndexOf(null));
129 success("contains", (l) => l.contains(2));
130 success("elementAt", (l) => l.elementAt[1]);
131 success("reversed", (l) => l.reversed);
132 success("sublist0-1", (l) => l.sublist(0, 1));
133 success("getRange0-1", (l) => l.getRange(0, 1));
134 success("asMap-keys", (l) => l.asMap().keys);
135 success("asMap-values", (l) => l.asMap().values);
136 success("where", (l) => l.where((x) => true));
137 success("map", (l) => l.map((x) => x));
138 success("expand", (l) => l.expand((x) => [x, x]));
139 success("skip", (l) => l.skip(1));
140 success("take", (l) => l.take(1));
141 success("skipWhile", (l) => l.skipWhile((x) => false));
142 success("takeWhile", (l) => l.takeWhile((x) => true));
143 success("first", (l) => l.first);
144 success("last", (l) => l.last);
145 success("single", (l) => l.single);
146 success("firstWhere", (l) => l.firstWhere((x) => true));
147 success("lastWhere", (l) => l.lastWhere((x) => true));
148 success("singleWhere", (l) => l.singleWhere((x) => true));
149 success("isEmpty", (l) => l.isEmpty);
150 success("isNotEmpty", (l) => l.isNotEmpty);
151 success("join", (l) => l.join("/"));
152 success("fold", (l) => l.fold("--", (a, b) => "$a/$b"));
153 success("reduce", (l) => l.reduce((a, b) => a + b));
154 success("every", (l) => l.every((x) => x == 0));
155 success("any", (l) => l.any((x) => x == 2));
156 success("toList", (l) => l.toList());
157 success("toSet", (l) => l.toSet());
158 success("toString", (l) => l.toString());
159
160 var it = elements.iterator;
161 list.forEach((v) {
162 Expect.isTrue(it.moveNext());
163 Expect.equals(it.current, v);
164 });
165 Expect.isFalse(it.moveNext());
166
167 if (elements is List<int> && list is List<int>) {
168 success("String.fromCharCodes", (l) => new String.fromCharCodes(l));
169 }
170 }
171 }
172
173 createConstList(n) {
174 if (n == 0) return const <int>[];
175 return const<int>[1, 2, 3];
176 }
177 createFixedList(n) {
178 var result = new List<int>(n);
179 for (int i = 0; i < n; i++) result[i] = n;
180 return result;
181 }
182 createGrowableList(n) {
183 var result = new List<int>()..length = n;
184 for (int i = 0; i < n; i++) result[i] = n;
185 return result;
186 }
187 createIterable(n) => new Iterable.generate(n);
188 createConstMapKeys(n) {
189 if (n == 0) return const <int,int>{}.keys;
190 return const <int,int>{0: 0, 1: 1, 2: 2}.keys;
191 }
192 createConstMapValues(n) {
193 if (n == 0) return const <int,int>{}.values;
194 return const <int,int>{0: 0, 1: 1, 2: 2}.values;
195 }
196 createMapKeys(n) {
197 var map = <int, int>{};
198 for (int i = 0; i < n; i++) map[i] = i;
199 return map.keys;
200 }
201 createMapValues(n) {
202 var map = <int, int>{};
203 for (int i = 0; i < n; i++) map[i] = i;
204 return map.values;
205 }
206 createSplayMapKeys(n) {
207 var map = new SplayTreeMap<int, int>();
208 for (int i = 0; i < n; i++) map[i] = i;
209 return map.keys;
210 }
211 createSplayMapValues(n) {
212 var map = new SplayTreeMap<int, int>();
213 for (int i = 0; i < n; i++) map[i] = i;
214 return map.values;
215 }
216 createSet(n) {
217 var set = new Set<int>();
218 for (int i = 0; i < n; i++) set.add(i);
219 return set;
220 }
221 createSplaySet(n) {
222 var set = new SplayTreeSet<int>();
223 for (int i = 0; i < n; i++) set.add(i);
224 return set;
225 }
226 createQueue(n) {
227 var queue = new Queue<int>();
228 for (int i = 0; i < n; i++) queue.add(i);
229 return queue;
230 }
231 createListMapKeys(n) {
232 return createGrowableList(n).asMap().keys;
233 }
234 createListMapValues(n) {
235 return createGrowableList(n).asMap().values;
236 }
237 createCodeUnits(n) {
238 var string = new String.fromCharCodes(new Iterable.generate(n));
239 return string.codeUnits;
240 }
241 createTypedList(n) {
242 var tl = new Uint8List(n);
243 for (int i = 0; i < n; i++) tl[i] = i;
244 return tl;
245 }
OLDNEW
« sdk/lib/internal/list.dart ('K') | « sdk/lib/internal/list.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698