Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // Tests of hash set behavior, with focus in iteration and concurrent | 5 // Tests of hash set behavior, with focus in iteration and concurrent |
| 6 // modification errors. | 6 // modification errors. |
| 7 | 7 |
| 8 library hash_map2_test; | 8 library hash_map2_test; |
| 9 import "package:expect/expect.dart"; | 9 import "package:expect/expect.dart"; |
| 10 import 'dart:collection'; | 10 import 'dart:collection'; |
| 11 | 11 |
| 12 testSet(Set newSet(), Set newSetFrom(Set from)) { | 12 testSet(Set newSet(), Set newSetFrom(Set from)) { |
| 13 | |
| 13 Set gen(int from, int to) => | 14 Set gen(int from, int to) => |
| 14 new Set.from(new Iterable.generate(to - from, (n) => n + from)); | 15 new Set.from(new Iterable.generate(to - from, (n) => n + from)); |
| 15 | 16 |
| 16 bool odd(int n) => (n & 1) == 1; | 17 bool odd(int n) => (n & 1) == 1; |
| 17 bool even(int n) => (n & 1) == 0; | 18 bool even(int n) => (n & 1) == 0; |
| 18 | 19 |
| 19 { // Test growing to largish capacity. | 20 { // Test growing to largish capacity. |
| 20 Set set = newSet(); | 21 Set set = newSet(); |
| 21 | 22 |
| 22 for (int i = 0; i < 256; i++) { | 23 for (int i = 0; i < 256; i++) { |
| 23 set.add(i); | 24 set.add(i); |
| 24 } | 25 } |
| 26 | |
| 27 print(set.runtimeType); | |
|
floitsch
2013/09/11 11:23:17
debug prints.
Lasse Reichstein Nielsen
2013/09/11 12:25:43
Done.
| |
| 25 set.addAll(gen(256, 512)); | 28 set.addAll(gen(256, 512)); |
| 29 print(set.length); | |
| 26 set.addAll(newSetFrom(gen(512, 1000))); | 30 set.addAll(newSetFrom(gen(512, 1000))); |
| 31 print(set.length); | |
| 27 Expect.equals(1000, set.length); | 32 Expect.equals(1000, set.length); |
| 28 | 33 |
| 29 // Remove half. | 34 // Remove half. |
| 30 for (int i = 0; i < 1000; i += 2) set.remove(i); | 35 for (int i = 0; i < 1000; i += 2) set.remove(i); |
| 31 Expect.equals(500, set.length); | 36 Expect.equals(500, set.length); |
| 32 Expect.isFalse(set.any(even)); | 37 Expect.isFalse(set.any(even)); |
| 33 Expect.isTrue(set.every(odd)); | 38 Expect.isTrue(set.every(odd)); |
| 34 | 39 |
| 35 // Re-add all. | 40 // Re-add all. |
| 36 set.addAll(gen(0, 1000)); | 41 set.addAll(gen(0, 1000)); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 Expect.equals(2, set.length); | 214 Expect.equals(2, set.length); |
| 210 Expect.isTrue(set.contains(1)); | 215 Expect.isTrue(set.contains(1)); |
| 211 Expect.isFalse(set.contains(2)); | 216 Expect.isFalse(set.contains(2)); |
| 212 Expect.isTrue(set.contains(3)); | 217 Expect.isTrue(set.contains(3)); |
| 213 set.retainWhere((each) => each == 3); | 218 set.retainWhere((each) => each == 3); |
| 214 Expect.equals(1, set.length); | 219 Expect.equals(1, set.length); |
| 215 Expect.isFalse(set.contains(1)); | 220 Expect.isFalse(set.contains(1)); |
| 216 Expect.isFalse(set.contains(2)); | 221 Expect.isFalse(set.contains(2)); |
| 217 Expect.isTrue(set.contains(3)); | 222 Expect.isTrue(set.contains(3)); |
| 218 } | 223 } |
| 224 | |
| 219 } | 225 } |
| 220 | 226 |
| 221 void main() { | 227 void main() { |
| 222 testSet(() => new HashSet(), (m) => new HashSet.from(m)); | 228 testSet(() => new HashSet(), (m) => new HashSet.from(m)); |
| 223 testSet(() => new LinkedHashSet(), (m) => new LinkedHashSet.from(m)); | 229 testSet(() => new LinkedHashSet(), (m) => new LinkedHashSet.from(m)); |
| 224 } | 230 } |
| 225 | 231 |
| 226 | 232 |
| 227 class BadHashCode { | 233 class BadHashCode { |
| 228 static int idCounter = 0; | 234 static int idCounter = 0; |
| 229 final int id; | 235 final int id; |
| 230 BadHashCode() : id = idCounter++; | 236 BadHashCode() : id = idCounter++; |
| 231 int get hashCode => 42; | 237 int get hashCode => 42; |
| 232 // operator == is identity. | 238 // operator == is identity. |
| 233 // Can't make a bad compareTo that isn't invalid. | 239 // Can't make a bad compareTo that isn't invalid. |
| 234 int compareTo(BadHashCode other) => id - other.id; | 240 int compareTo(BadHashCode other) => id - other.id; |
| 235 } | 241 } |
| OLD | NEW |