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

Unified Diff: tests/corelib/set_test.dart

Issue 24104003: Reapply "Convert HashSet, LinkedHashSet to factory methods and custom implementations." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix bugs and add tests. 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
Index: tests/corelib/set_test.dart
diff --git a/tests/corelib/set_test.dart b/tests/corelib/set_test.dart
index f08583554288900e82cf9ffcfa4f0a2c947554ca..a0baf6da045a05c775373a0e9931c6a069017771 100644
--- a/tests/corelib/set_test.dart
+++ b/tests/corelib/set_test.dart
@@ -9,7 +9,13 @@ import 'package:expect/expect.dart';
import "dart:collection";
void testMain(Set create()) {
+ testInts(create);
+ testStrings(create);
+}
+
+void testInts(Set create()) {
Set set = create();
+
testLength(0, set);
set.add(1);
testLength(1, set);
@@ -186,9 +192,74 @@ void testLength(int length, Set set) {
Expect.equals(length, set.length);
(length == 0 ? Expect.isTrue : Expect.isFalse)(set.isEmpty);
(length != 0 ? Expect.isTrue : Expect.isFalse)(set.isNotEmpty);
+ if (length == 0) {
+ for (var e in set) { Expect.fail("contains element when iterated: $e"); }
+ }
+ (length == 0 ? Expect.isFalse : Expect.isTrue)(set.iterator.moveNext());
+}
+
+void testStrings(Set create()) {
+ var set = create();
+ var strings = ["foo", "bar", "baz", "qux", "fisk", "hest", "svin", "pigvar"];
+ set.addAll(strings);
+ testLength(8, set);
+ set.removeAll(strings.where((x) => x.length == 3));
+ testLength(4, set);
+ set.add("bar");
+ set.add("qux");
+ testLength(6, set);
+ set.addAll(strings);
+ testLength(8, set);
+ set.removeWhere((x) => x.length != 3);
+ testLength(4, set);
+ set.retainWhere((x) => x[1] == "a");
+ testLength(2, set);
+ Expect.isTrue(set.containsAll(["baz", "bar"]));
+
+ set = set.union(strings.where((x) => x.length != 3).toSet());
+ testLength(6, set);
+ set = set.intersection(["qux", "baz", "fisk", "egern"].toSet());
+ testLength(2, set);
+ Expect.isTrue(set.containsAll(["baz", "fisk"]));
+}
+
+void testTypeAnnotations(Set<int> set) {
+ set.add(0);
+ set.add(999);
+ set.add(0x800000000);
+ set.add(0x20000000000000);
+ Expect.isFalse(set.contains("not an it"));
+ Expect.isFalse(set.remove("not an it"));
+ Expect.isFalse(set.containsAll(["Not an int", "Also no an int"]));
+
+ testLength(4, set);
+ set.removeAll(["Not an int", 999, "Also no an int"]);
+ testLength(3, set);
+ set.retainAll(["Not an int", 0, "Also no an int"]);
+ testLength(1, set);
}
main() {
- testMain(() => new Set());
testMain(() => new HashSet());
+ testMain(() => new LinkedHashSet());
+ testMain(() => new HashSet(equals: identical));
+ testMain(() => new LinkedHashSet(equals: identical));
+ testMain(() => new HashSet(equals: (a, b) => a == b,
+ hashCode: (a) => -a.hashCode,
+ isValidKey: (a) => true));
+ testMain(() => new LinkedHashSet(
+ equals: (a, b) => a == b,
+ hashCode: (a) => -a.hashCode,
+ isValidKey: (a) => true));
+
+ testTypeAnnotations(new HashSet<int>());
+ testTypeAnnotations(new LinkedHashSet<int>());
+ testTypeAnnotations(new HashSet<int>(equals: identical));
+ testTypeAnnotations(new LinkedHashSet<int>(equals: identical));
+ testTypeAnnotations(new HashSet<int>(equals: (int a, int b) => a == b,
+ hashCode: (int a) => a.hashCode,
+ isValidKey: (a) => a is int));
+ testTypeAnnotations(new LinkedHashSet<int>(equals: (int a, int b) => a == b,
+ hashCode: (int a) => a.hashCode,
+ isValidKey: (a) => a is int));
}

Powered by Google App Engine
This is Rietveld 408576698