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

Unified Diff: tests/corelib/hash_set_test.dart

Issue 24267023: Update implementations to use Object.identityHashCode for identity map/set (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Add tests. Fix few bugs now that it can run. Created 7 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/corelib/corelib.status ('k') | tests/corelib/map_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/corelib/hash_set_test.dart
diff --git a/tests/corelib/hash_set_test.dart b/tests/corelib/hash_set_test.dart
index 175cba543a4c974d3de7431b72abb2822f430347..06286100d0bbc48bb82adbb120f10a584fb84d8c 100644
--- a/tests/corelib/hash_set_test.dart
+++ b/tests/corelib/hash_set_test.dart
@@ -9,7 +9,7 @@ library hash_map2_test;
import "package:expect/expect.dart";
import 'dart:collection';
-testSet(Set newSet(), Set newSetFrom(Set from)) {
+testSet(Set newSet(), Set newSetFrom(Iterable from)) {
Set gen(int from, int to) =>
new Set.from(new Iterable.generate(to - from, (n) => n + from));
@@ -220,9 +220,60 @@ testSet(Set newSet(), Set newSetFrom(Set from)) {
}
}
+
+void testIdentitySet(Set create()) {
+ Set set = create();
+ set.add(1);
+ set.add(2);
+ set.add(1); // Integers are identical if equal.
+ Expect.equals(2, set.length);
+ var complex = 4;
+ complex = set.length == 2 ? complex ~/ 4 : 87; // Avoid compile-time constant.
+ Expect.isTrue(set.contains(complex)); // 1 is in set, even if computed.
+ set.clear();
+
+ // All compile time constants are identical to themselves.
+ var constants = [double.INFINITY,
+ double.NAN, -0.0, /// 01: ok
+ 0.0, 42, "", null, false, true, #bif, testIdentitySet];
+ set.addAll(constants);
+ Expect.equals(constants.length, set.length);
+ for (var c in constants) {
+ Expect.isTrue(set.contains(c), "constant: $c");
+ }
+ Expect.isTrue(set.containsAll(constants), "constants: $set");
+ set.clear();
+
+ var m1 = new Mutable(1);
+ var m2 = new Mutable(2);
+ var m3 = new Mutable(3);
+ var m4 = new Mutable(2); // Equal to m2, but not identical.
+ set.addAll([m1, m2, m3, m4]);
+ Expect.equals(4, set.length);
+ Expect.equals(3, m3.hashCode);
+ m3.id = 1;
+ Expect.equals(1, m3.hashCode);
+ // Changing hashCode doesn't affect lookup.
+ Expect.isTrue(set.contains(m3));
+ Expect.isTrue(set.contains(m1));
+ set.remove(m3);
+ Expect.isFalse(set.contains(m3));
+ Expect.isTrue(set.contains(m1));
+}
+
+
void main() {
+ testSet(() => new Set(), (m) => new Set.from(m));
testSet(() => new HashSet(), (m) => new HashSet.from(m));
testSet(() => new LinkedHashSet(), (m) => new LinkedHashSet.from(m));
+ testIdentitySet(() => new Set.identity());
+ testIdentitySet(() => new HashSet.identity());
+ testIdentitySet(() => new LinkedHashSet.identity());
+ testIdentitySet(() => new HashSet(equals: (x, y) => identical(x, y),
+ hashCode: (x) => identityHashCode(x)));
+ testIdentitySet(
+ () => new LinkedHashSet(equals: (x, y) => identical(x, y),
+ hashCode: (x) => identityHashCode(x)));
}
@@ -235,3 +286,10 @@ class BadHashCode {
// Can't make a bad compareTo that isn't invalid.
int compareTo(BadHashCode other) => id - other.id;
}
+
+class Mutable {
+ int id;
+ Mutable(this.id);
+ int get hashCode => id;
+ bool operator==(other) => other is Mutable && id == other.id;
+}
« no previous file with comments | « tests/corelib/corelib.status ('k') | tests/corelib/map_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698