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 import 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
8 import "dart:collection"; | 8 import "dart:collection"; |
9 | 9 |
10 void testMain(Set create()) { | 10 void testMain(Set create()) { |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 Expect.isFalse(set.remove("not an it")); | 262 Expect.isFalse(set.remove("not an it")); |
263 Expect.isFalse(set.containsAll(["Not an int", "Also no an int"])); | 263 Expect.isFalse(set.containsAll(["Not an int", "Also no an int"])); |
264 | 264 |
265 testLength(4, set); | 265 testLength(4, set); |
266 set.removeAll(["Not an int", 999, "Also no an int"]); | 266 set.removeAll(["Not an int", 999, "Also no an int"]); |
267 testLength(3, set); | 267 testLength(3, set); |
268 set.retainAll(["Not an int", 0, "Also no an int"]); | 268 set.retainAll(["Not an int", 0, "Also no an int"]); |
269 testLength(1, set); | 269 testLength(1, set); |
270 } | 270 } |
271 | 271 |
272 void testRetainWhere(Set create([equals, hashCode, validKey, compare])) { | 272 void testRetainWhere( |
| 273 Set<CE> create( |
| 274 [CEEq equals, CEHash hashCode, ValidKey validKey, CECompare compare])) { |
273 // The retainWhere method must not collapse the argument Iterable | 275 // The retainWhere method must not collapse the argument Iterable |
274 // in a way that doesn't match the equality of the set. | 276 // in a way that doesn't match the equality of the set. |
275 // It must not throw away equal elements that are different in the | 277 // It must not throw away equal elements that are different in the |
276 // equality of the set. | 278 // equality of the set. |
277 // It must not consider objects to be not there if they are equal | 279 // It must not consider objects to be not there if they are equal |
278 // in the equality of the set. | 280 // in the equality of the set. |
279 | 281 |
280 // If set equality is natural equality, using different but equal objects | 282 // If set equality is natural equality, using different but equal objects |
281 // must work. Can't use an identity set internally (as was done at some point | 283 // must work. Can't use an identity set internally (as was done at some point |
282 // during development). | 284 // during development). |
283 Set set = create(); | 285 var set = create(); |
284 set.addAll([new CE(0), new CE(1), new CE(2)]); | 286 set.addAll([new CE(0), new CE(1), new CE(2)]); |
285 Expect.equals(3, set.length); // All different. | 287 Expect.equals(3, set.length); // All different. |
286 set.retainAll([new CE(0), new CE(2)]); | 288 set.retainAll([new CE(0), new CE(2)]); |
287 Expect.equals(2, set.length); | 289 Expect.equals(2, set.length); |
288 Expect.isTrue(set.contains(new CE(0))); | 290 Expect.isTrue(set.contains(new CE(0))); |
289 Expect.isTrue(set.contains(new CE(2))); | 291 Expect.isTrue(set.contains(new CE(2))); |
290 | 292 |
291 // If equality of set is identity, we can't internally use a non-identity | 293 // If equality of set is identity, we can't internally use a non-identity |
292 // based set because it might throw away equal objects that are not identical. | 294 // based set because it might throw away equal objects that are not identical. |
293 var elems = [new CE(0), new CE(1), new CE(2), new CE(0)]; | 295 var elems = [new CE(0), new CE(1), new CE(2), new CE(0)]; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 360 |
359 intersection = set1.intersection(set3); | 361 intersection = set1.intersection(set3); |
360 testLength(0, intersection); | 362 testLength(0, intersection); |
361 } | 363 } |
362 | 364 |
363 // Objects that are equal based on data. | 365 // Objects that are equal based on data. |
364 class CE implements Comparable<CE> { | 366 class CE implements Comparable<CE> { |
365 final int id; | 367 final int id; |
366 const CE(this.id); | 368 const CE(this.id); |
367 int get hashCode => id; | 369 int get hashCode => id; |
368 bool operator ==(Object other) => other is CE && id == (other as CE).id; | 370 bool operator ==(Object other) => other is CE && id == other.id; |
369 int compareTo(CE other) => id - other.id; | 371 int compareTo(CE other) => id - other.id; |
370 String toString() => "CE($id)"; | 372 String toString() => "CE($id)"; |
371 } | 373 } |
372 | 374 |
373 typedef int CECompare(CE e1, CE e2); | 375 typedef int CECompare(CE e1, CE e2); |
| 376 typedef int CEHash(CE e1); |
| 377 typedef bool CEEq(CE e1, CE e2); |
| 378 typedef bool ValidKey(Object o); |
374 // Equality of Id objects based on id modulo value. | 379 // Equality of Id objects based on id modulo value. |
375 Function customEq(int mod) => (CE e1, CE e2) => ((e1.id - e2.id) % mod) == 0; | 380 CEEq customEq(int mod) => (CE e1, CE e2) => ((e1.id - e2.id) % mod) == 0; |
376 Function customHash(int mod) => (CE e) => e.id % mod; | 381 CEHash customHash(int mod) => (CE e) => e.id % mod; |
377 CECompare customCompare(int mod) => | 382 CECompare customCompare(int mod) => |
378 (CE e1, CE e2) => (e1.id % mod) - (e2.id % mod); | 383 (CE e1, CE e2) => (e1.id % mod) - (e2.id % mod); |
379 bool validKey(Object o) => o is CE; | 384 bool validKey(Object o) => o is CE; |
380 final customId = new Map<dynamic, dynamic>.identity(); | 385 final customId = new Map<dynamic, dynamic>.identity(); |
381 int counter = 0; | 386 int counter = 0; |
382 int identityCompare(e1, e2) { | 387 int identityCompare(e1, e2) { |
383 if (identical(e1, e2)) return 0; | 388 if (identical(e1, e2)) return 0; |
384 int i1 = customId.putIfAbsent(e1, () => ++counter); | 389 int i1 = customId.putIfAbsent(e1, () => ++counter); |
385 int i2 = customId.putIfAbsent(e2, () => ++counter); | 390 int i2 = customId.putIfAbsent(e2, () => ++counter); |
386 return i1 - i2; | 391 return i1 - i2; |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 | 536 |
532 testCESetFrom( | 537 testCESetFrom( |
533 (x) => new SplayTreeSet<CE>.from(x, customCompare(20), validKey)); | 538 (x) => new SplayTreeSet<CE>.from(x, customCompare(20), validKey)); |
534 testCESetFrom((x) => new SplayTreeSet<CE>.from(x, identityCompare)); | 539 testCESetFrom((x) => new SplayTreeSet<CE>.from(x, identityCompare)); |
535 | 540 |
536 testASetFrom((x) => new Set<A>.from(x)); | 541 testASetFrom((x) => new Set<A>.from(x)); |
537 testASetFrom((x) => new HashSet<A>.from(x)); | 542 testASetFrom((x) => new HashSet<A>.from(x)); |
538 testASetFrom((x) => new LinkedHashSet<A>.from(x)); | 543 testASetFrom((x) => new LinkedHashSet<A>.from(x)); |
539 testASetFrom((x) => new SplayTreeSet<A>.from(x, identityCompare)); | 544 testASetFrom((x) => new SplayTreeSet<A>.from(x, identityCompare)); |
540 } | 545 } |
OLD | NEW |