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 library set_test; | 5 library set_test; |
6 | 6 |
7 | 7 |
8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
9 import "dart:collection"; | 9 import "dart:collection"; |
10 | 10 |
11 void testMain(Set create()) { | 11 void testMain(Set create()) { |
12 testInts(create); | 12 testInts(create); |
13 testStrings(create); | 13 testStrings(create); |
14 } | 14 } |
15 | 15 |
16 void testInts(Set create()) { | 16 void testInts(Set create()) { |
17 Set set = create(); | 17 Set set = create(); |
18 | 18 |
19 testLength(0, set); | 19 testLength(0, set); |
20 set.add(1); | 20 Expect.isTrue(set.add(1)); |
21 testLength(1, set); | 21 testLength(1, set); |
22 Expect.isTrue(set.contains(1)); | 22 Expect.isTrue(set.contains(1)); |
23 | 23 |
24 set.add(1); | 24 Expect.isFalse(set.add(1)); |
25 testLength(1, set); | 25 testLength(1, set); |
26 Expect.isTrue(set.contains(1)); | 26 Expect.isTrue(set.contains(1)); |
27 | 27 |
28 set.remove(1); | 28 Expect.isTrue(set.remove(1)); |
29 testLength(0, set); | 29 testLength(0, set); |
30 Expect.isFalse(set.contains(1)); | 30 Expect.isFalse(set.contains(1)); |
31 | 31 |
| 32 Expect.isFalse(set.remove(1)); |
| 33 testLength(0, set); |
| 34 Expect.isFalse(set.contains(1)); |
| 35 |
32 for (int i = 0; i < 10; i++) { | 36 for (int i = 0; i < 10; i++) { |
33 set.add(i); | 37 set.add(i); |
34 } | 38 } |
35 | 39 |
36 testLength(10, set); | 40 testLength(10, set); |
37 for (int i = 0; i < 10; i++) { | 41 for (int i = 0; i < 10; i++) { |
38 Expect.isTrue(set.contains(i)); | 42 Expect.isTrue(set.contains(i)); |
39 } | 43 } |
40 | 44 |
41 testLength(10, set); | 45 testLength(10, set); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 for (int i = 0; i < 10; i++) { | 181 for (int i = 0; i < 10; i++) { |
178 Expect.isTrue(set.contains(i)); | 182 Expect.isTrue(set.contains(i)); |
179 } | 183 } |
180 for (int i = 10; i < 20; i++) { | 184 for (int i = 10; i < 20; i++) { |
181 Expect.isFalse(set.contains(i)); | 185 Expect.isFalse(set.contains(i)); |
182 } | 186 } |
183 | 187 |
184 // Test Set.clear. | 188 // Test Set.clear. |
185 set.clear(); | 189 set.clear(); |
186 testLength(0, set); | 190 testLength(0, set); |
187 set.add(11); | 191 Expect.isTrue(set.add(11)); |
188 testLength(1, set); | 192 testLength(1, set); |
189 } | 193 } |
190 | 194 |
191 void testLength(int length, Set set) { | 195 void testLength(int length, Set set) { |
192 Expect.equals(length, set.length); | 196 Expect.equals(length, set.length); |
193 (length == 0 ? Expect.isTrue : Expect.isFalse)(set.isEmpty); | 197 (length == 0 ? Expect.isTrue : Expect.isFalse)(set.isEmpty); |
194 (length != 0 ? Expect.isTrue : Expect.isFalse)(set.isNotEmpty); | 198 (length != 0 ? Expect.isTrue : Expect.isFalse)(set.isNotEmpty); |
195 if (length == 0) { | 199 if (length == 0) { |
196 for (var e in set) { Expect.fail("contains element when iterated: $e"); } | 200 for (var e in set) { Expect.fail("contains element when iterated: $e"); } |
197 } | 201 } |
198 (length == 0 ? Expect.isFalse : Expect.isTrue)(set.iterator.moveNext()); | 202 (length == 0 ? Expect.isFalse : Expect.isTrue)(set.iterator.moveNext()); |
199 } | 203 } |
200 | 204 |
201 void testStrings(Set create()) { | 205 void testStrings(Set create()) { |
202 var set = create(); | 206 var set = create(); |
203 var strings = ["foo", "bar", "baz", "qux", "fisk", "hest", "svin", "pigvar"]; | 207 var strings = ["foo", "bar", "baz", "qux", "fisk", "hest", "svin", "pigvar"]; |
204 set.addAll(strings); | 208 set.addAll(strings); |
205 testLength(8, set); | 209 testLength(8, set); |
206 set.removeAll(strings.where((x) => x.length == 3)); | 210 set.removeAll(strings.where((x) => x.length == 3)); |
207 testLength(4, set); | 211 testLength(4, set); |
208 set.add("bar"); | 212 Expect.isTrue(set.add("bar")); |
209 set.add("qux"); | 213 Expect.isTrue(set.add("qux")); |
210 testLength(6, set); | 214 testLength(6, set); |
211 set.addAll(strings); | 215 set.addAll(strings); |
212 testLength(8, set); | 216 testLength(8, set); |
213 set.removeWhere((x) => x.length != 3); | 217 set.removeWhere((x) => x.length != 3); |
214 testLength(4, set); | 218 testLength(4, set); |
215 set.retainWhere((x) => x[1] == "a"); | 219 set.retainWhere((x) => x[1] == "a"); |
216 testLength(2, set); | 220 testLength(2, set); |
217 Expect.isTrue(set.containsAll(["baz", "bar"])); | 221 Expect.isTrue(set.containsAll(["baz", "bar"])); |
218 | 222 |
219 set = set.union(strings.where((x) => x.length != 3).toSet()); | 223 set = set.union(strings.where((x) => x.length != 3).toSet()); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 testTypeAnnotations(new LinkedHashSet<int>(equals: (int a, int b) => a == b, | 331 testTypeAnnotations(new LinkedHashSet<int>(equals: (int a, int b) => a == b, |
328 hashCode: (int a) => a.hashCode, | 332 hashCode: (int a) => a.hashCode, |
329 isValidKey: (a) => a is int)); | 333 isValidKey: (a) => a is int)); |
330 | 334 |
331 testRetainWhere(([equals, hashCode, validKey]) => | 335 testRetainWhere(([equals, hashCode, validKey]) => |
332 new HashSet(equals: equals, hashCode: hashCode, isValidKey: validKey)); | 336 new HashSet(equals: equals, hashCode: hashCode, isValidKey: validKey)); |
333 testRetainWhere(([equals, hashCode, validKey]) => | 337 testRetainWhere(([equals, hashCode, validKey]) => |
334 new LinkedHashSet(equals: equals, hashCode: hashCode, | 338 new LinkedHashSet(equals: equals, hashCode: hashCode, |
335 isValidKey: validKey)); | 339 isValidKey: validKey)); |
336 } | 340 } |
OLD | NEW |