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); |
| 13 testStrings(create); |
| 14 } |
| 15 |
| 16 void testInts(Set create()) { |
12 Set set = create(); | 17 Set set = create(); |
| 18 |
13 testLength(0, set); | 19 testLength(0, set); |
14 set.add(1); | 20 set.add(1); |
15 testLength(1, set); | 21 testLength(1, set); |
16 Expect.isTrue(set.contains(1)); | 22 Expect.isTrue(set.contains(1)); |
17 | 23 |
18 set.add(1); | 24 set.add(1); |
19 testLength(1, set); | 25 testLength(1, set); |
20 Expect.isTrue(set.contains(1)); | 26 Expect.isTrue(set.contains(1)); |
21 | 27 |
22 set.remove(1); | 28 set.remove(1); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 set.clear(); | 185 set.clear(); |
180 testLength(0, set); | 186 testLength(0, set); |
181 set.add(11); | 187 set.add(11); |
182 testLength(1, set); | 188 testLength(1, set); |
183 } | 189 } |
184 | 190 |
185 void testLength(int length, Set set) { | 191 void testLength(int length, Set set) { |
186 Expect.equals(length, set.length); | 192 Expect.equals(length, set.length); |
187 (length == 0 ? Expect.isTrue : Expect.isFalse)(set.isEmpty); | 193 (length == 0 ? Expect.isTrue : Expect.isFalse)(set.isEmpty); |
188 (length != 0 ? Expect.isTrue : Expect.isFalse)(set.isNotEmpty); | 194 (length != 0 ? Expect.isTrue : Expect.isFalse)(set.isNotEmpty); |
| 195 if (length == 0) { |
| 196 for (var e in set) { Expect.fail("contains element when iterated: $e"); } |
| 197 } |
| 198 (length == 0 ? Expect.isFalse : Expect.isTrue)(set.iterator.moveNext()); |
| 199 } |
| 200 |
| 201 void testStrings(Set create()) { |
| 202 var set = create(); |
| 203 var strings = ["foo", "bar", "baz", "qux", "fisk", "hest", "svin", "pigvar"]; |
| 204 set.addAll(strings); |
| 205 testLength(8, set); |
| 206 set.removeAll(strings.where((x) => x.length == 3)); |
| 207 testLength(4, set); |
| 208 set.add("bar"); |
| 209 set.add("qux"); |
| 210 testLength(6, set); |
| 211 set.addAll(strings); |
| 212 testLength(8, set); |
| 213 set.removeWhere((x) => x.length != 3); |
| 214 testLength(4, set); |
| 215 set.retainWhere((x) => x[1] == "a"); |
| 216 testLength(2, set); |
| 217 Expect.isTrue(set.containsAll(["baz", "bar"])); |
| 218 |
| 219 set = set.union(strings.where((x) => x.length != 3).toSet()); |
| 220 testLength(6, set); |
| 221 set = set.intersection(["qux", "baz", "fisk", "egern"].toSet()); |
| 222 testLength(2, set); |
| 223 Expect.isTrue(set.containsAll(["baz", "fisk"])); |
| 224 } |
| 225 |
| 226 void testTypeAnnotations(Set<int> set) { |
| 227 set.add(0); |
| 228 set.add(999); |
| 229 set.add(0x800000000); |
| 230 set.add(0x20000000000000); |
| 231 Expect.isFalse(set.contains("not an it")); |
| 232 Expect.isFalse(set.remove("not an it")); |
| 233 Expect.isFalse(set.containsAll(["Not an int", "Also no an int"])); |
| 234 |
| 235 testLength(4, set); |
| 236 set.removeAll(["Not an int", 999, "Also no an int"]); |
| 237 testLength(3, set); |
| 238 set.retainAll(["Not an int", 0, "Also no an int"]); |
| 239 testLength(1, set); |
189 } | 240 } |
190 | 241 |
191 main() { | 242 main() { |
192 testMain(() => new Set()); | |
193 testMain(() => new HashSet()); | 243 testMain(() => new HashSet()); |
| 244 testMain(() => new LinkedHashSet()); |
| 245 testMain(() => new HashSet(equals: identical)); |
| 246 testMain(() => new LinkedHashSet(equals: identical)); |
| 247 testMain(() => new HashSet(equals: (a, b) => a == b, |
| 248 hashCode: (a) => -a.hashCode, |
| 249 isValidKey: (a) => true)); |
| 250 testMain(() => new LinkedHashSet( |
| 251 equals: (a, b) => a == b, |
| 252 hashCode: (a) => -a.hashCode, |
| 253 isValidKey: (a) => true)); |
| 254 |
| 255 testTypeAnnotations(new HashSet<int>()); |
| 256 testTypeAnnotations(new LinkedHashSet<int>()); |
| 257 testTypeAnnotations(new HashSet<int>(equals: identical)); |
| 258 testTypeAnnotations(new LinkedHashSet<int>(equals: identical)); |
| 259 testTypeAnnotations(new HashSet<int>(equals: (int a, int b) => a == b, |
| 260 hashCode: (int a) => a.hashCode, |
| 261 isValidKey: (a) => a is int)); |
| 262 testTypeAnnotations(new LinkedHashSet<int>(equals: (int a, int b) => a == b, |
| 263 hashCode: (int a) => a.hashCode, |
| 264 isValidKey: (a) => a is int)); |
194 } | 265 } |
OLD | NEW |