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'; |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 Expect.equals(2, set.length); | 211 Expect.equals(2, set.length); |
212 Expect.isTrue(set.contains(1)); | 212 Expect.isTrue(set.contains(1)); |
213 Expect.isFalse(set.contains(2)); | 213 Expect.isFalse(set.contains(2)); |
214 Expect.isTrue(set.contains(3)); | 214 Expect.isTrue(set.contains(3)); |
215 set.retainWhere((each) => each == 3); | 215 set.retainWhere((each) => each == 3); |
216 Expect.equals(1, set.length); | 216 Expect.equals(1, set.length); |
217 Expect.isFalse(set.contains(1)); | 217 Expect.isFalse(set.contains(1)); |
218 Expect.isFalse(set.contains(2)); | 218 Expect.isFalse(set.contains(2)); |
219 Expect.isTrue(set.contains(3)); | 219 Expect.isTrue(set.contains(3)); |
220 } | 220 } |
| 221 |
| 222 { // Test lookup |
| 223 Set set = newSet(); |
| 224 var m1a = new Mutable(1); |
| 225 var m1b = new Mutable(1); |
| 226 var m2a = new Mutable(2); |
| 227 var m2b = new Mutable(2); |
| 228 Expect.isNull(set.lookup(m1a)); |
| 229 Expect.isNull(set.lookup(m1b)); |
| 230 set.add(m1a); |
| 231 Expect.identical(m1a, set.lookup(m1a)); |
| 232 Expect.identical(m1a, set.lookup(m1b)); |
| 233 |
| 234 Expect.isNull(set.lookup(m2a)); |
| 235 Expect.isNull(set.lookup(m2b)); |
| 236 set.add(m2a); |
| 237 Expect.identical(m2a, set.lookup(m2a)); |
| 238 Expect.identical(m2a, set.lookup(m2b)); |
| 239 |
| 240 set.add(m2b); // Adding doesn't change element. |
| 241 Expect.identical(m2a, set.lookup(m2a)); |
| 242 Expect.identical(m2a, set.lookup(m2b)); |
| 243 |
| 244 set.remove(m1a); |
| 245 set.add(m1b); |
| 246 Expect.identical(m1b, set.lookup(m1a)); |
| 247 Expect.identical(m1b, set.lookup(m1b)); |
| 248 |
| 249 set.add(1); |
| 250 Expect.identical(1, set.lookup(1.0)); |
| 251 set.add(-0.0); |
| 252 Expect.identical(-0.0, set.lookup(0.0)); |
| 253 } |
221 } | 254 } |
222 | 255 |
223 | 256 |
224 void testIdentitySet(Set create()) { | 257 void testIdentitySet(Set create()) { |
225 Set set = create(); | 258 Set set = create(); |
226 set.add(1); | 259 set.add(1); |
227 set.add(2); | 260 set.add(2); |
228 set.add(1); // Integers are identical if equal. | 261 set.add(1); // Integers are identical if equal. |
229 Expect.equals(2, set.length); | 262 Expect.equals(2, set.length); |
230 var complex = 4; | 263 var complex = 4; |
(...skipping 21 matching lines...) Expand all Loading... |
252 Expect.equals(4, set.length); | 285 Expect.equals(4, set.length); |
253 Expect.equals(3, m3.hashCode); | 286 Expect.equals(3, m3.hashCode); |
254 m3.id = 1; | 287 m3.id = 1; |
255 Expect.equals(1, m3.hashCode); | 288 Expect.equals(1, m3.hashCode); |
256 // Changing hashCode doesn't affect lookup. | 289 // Changing hashCode doesn't affect lookup. |
257 Expect.isTrue(set.contains(m3)); | 290 Expect.isTrue(set.contains(m3)); |
258 Expect.isTrue(set.contains(m1)); | 291 Expect.isTrue(set.contains(m1)); |
259 set.remove(m3); | 292 set.remove(m3); |
260 Expect.isFalse(set.contains(m3)); | 293 Expect.isFalse(set.contains(m3)); |
261 Expect.isTrue(set.contains(m1)); | 294 Expect.isTrue(set.contains(m1)); |
| 295 |
| 296 Expect.identical(m1, set.lookup(m1)); |
| 297 Expect.identical(null, set.lookup(m3)); |
262 } | 298 } |
263 | 299 |
264 | 300 |
265 void main() { | 301 void main() { |
266 testSet(() => new Set(), (m) => new Set.from(m)); | 302 testSet(() => new Set(), (m) => new Set.from(m)); |
267 testSet(() => new HashSet(), (m) => new HashSet.from(m)); | 303 testSet(() => new HashSet(), (m) => new HashSet.from(m)); |
268 testSet(() => new LinkedHashSet(), (m) => new LinkedHashSet.from(m)); | 304 testSet(() => new LinkedHashSet(), (m) => new LinkedHashSet.from(m)); |
269 testIdentitySet(() => new Set.identity()); | 305 testIdentitySet(() => new Set.identity()); |
270 testIdentitySet(() => new HashSet.identity()); | 306 testIdentitySet(() => new HashSet.identity()); |
271 testIdentitySet(() => new LinkedHashSet.identity()); | 307 testIdentitySet(() => new LinkedHashSet.identity()); |
(...skipping 14 matching lines...) Expand all Loading... |
286 // Can't make a bad compareTo that isn't invalid. | 322 // Can't make a bad compareTo that isn't invalid. |
287 int compareTo(BadHashCode other) => id - other.id; | 323 int compareTo(BadHashCode other) => id - other.id; |
288 } | 324 } |
289 | 325 |
290 class Mutable { | 326 class Mutable { |
291 int id; | 327 int id; |
292 Mutable(this.id); | 328 Mutable(this.id); |
293 int get hashCode => id; | 329 int get hashCode => id; |
294 bool operator==(other) => other is Mutable && id == other.id; | 330 bool operator==(other) => other is Mutable && id == other.id; |
295 } | 331 } |
OLD | NEW |