| 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 | 7 |
| 8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
| 9 import "dart:collection"; | 9 import "dart:collection"; |
| 10 | 10 |
| 11 void testMain(Set create()) { | 11 void testMain(Set create()) { |
| 12 testInts(create); | 12 testInts(create); |
| 13 testStrings(create); | 13 testStrings(create); |
| 14 testInts(() => create().toSet()); |
| 15 testStrings(() => create().toSet()); |
| 14 } | 16 } |
| 15 | 17 |
| 16 void testInts(Set create()) { | 18 void testInts(Set create()) { |
| 17 Set set = create(); | 19 Set set = create(); |
| 18 | 20 |
| 19 testLength(0, set); | 21 testLength(0, set); |
| 20 Expect.isTrue(set.add(1)); | 22 Expect.isTrue(set.add(1)); |
| 21 testLength(1, set); | 23 testLength(1, set); |
| 22 Expect.isTrue(set.contains(1)); | 24 Expect.isTrue(set.contains(1)); |
| 23 | 25 |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 } | 185 } |
| 184 for (int i = 10; i < 20; i++) { | 186 for (int i = 10; i < 20; i++) { |
| 185 Expect.isFalse(set.contains(i)); | 187 Expect.isFalse(set.contains(i)); |
| 186 } | 188 } |
| 187 | 189 |
| 188 // Test Set.clear. | 190 // Test Set.clear. |
| 189 set.clear(); | 191 set.clear(); |
| 190 testLength(0, set); | 192 testLength(0, set); |
| 191 Expect.isTrue(set.add(11)); | 193 Expect.isTrue(set.add(11)); |
| 192 testLength(1, set); | 194 testLength(1, set); |
| 195 |
| 196 // Test Set.toSet. |
| 197 set.add(1); |
| 198 set.add(21); |
| 199 testLength(3, set); |
| 200 var set2 = set.toSet(); |
| 201 testLength(3, set2); |
| 202 Expect.listEquals(set.toList(), set2.toList()); |
| 203 set.add(31); |
| 204 testLength(4, set); |
| 205 testLength(3, set2); |
| 206 |
| 207 set2 = set.toSet()..clear(); |
| 208 testLength(0, set2); |
| 209 Expect.isTrue(set2.add(11)); |
| 210 Expect.isTrue(set2.add(1)); |
| 211 Expect.isTrue(set2.add(21)); |
| 212 Expect.isTrue(set2.add(31)); |
| 213 testLength(4, set2); |
| 214 Expect.listEquals(set.toList(), set2.toList()); |
| 215 |
| 216 set2 = (set.toSet()..clear()).toSet(); // Cloning empty set shouldn't fail. |
| 217 testLength(0, set2); |
| 193 } | 218 } |
| 194 | 219 |
| 195 void testLength(int length, Set set) { | 220 void testLength(int length, Set set) { |
| 196 Expect.equals(length, set.length); | 221 Expect.equals(length, set.length); |
| 197 (length == 0 ? Expect.isTrue : Expect.isFalse)(set.isEmpty); | 222 (length == 0 ? Expect.isTrue : Expect.isFalse)(set.isEmpty); |
| 198 (length != 0 ? Expect.isTrue : Expect.isFalse)(set.isNotEmpty); | 223 (length != 0 ? Expect.isTrue : Expect.isFalse)(set.isNotEmpty); |
| 199 if (length == 0) { | 224 if (length == 0) { |
| 200 for (var e in set) { Expect.fail("contains element when iterated: $e"); } | 225 for (var e in set) { Expect.fail("contains element when iterated: $e"); } |
| 201 } | 226 } |
| 202 (length == 0 ? Expect.isFalse : Expect.isTrue)(set.iterator.moveNext()); | 227 (length == 0 ? Expect.isFalse : Expect.isTrue)(set.iterator.moveNext()); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 bool validKey(Object o) => o is CE; | 377 bool validKey(Object o) => o is CE; |
| 353 final customId = new Map.identity(); | 378 final customId = new Map.identity(); |
| 354 int counter = 0; | 379 int counter = 0; |
| 355 int identityCompare(e1, e2) { | 380 int identityCompare(e1, e2) { |
| 356 if (identical(e1, e2)) return 0; | 381 if (identical(e1, e2)) return 0; |
| 357 int i1 = customId.putIfAbsent(e1, () => ++counter); | 382 int i1 = customId.putIfAbsent(e1, () => ++counter); |
| 358 int i2 = customId.putIfAbsent(e2, () => ++counter); | 383 int i2 = customId.putIfAbsent(e2, () => ++counter); |
| 359 return i1 - i2; | 384 return i1 - i2; |
| 360 } | 385 } |
| 361 | 386 |
| 387 void testIdentity(Set create()) { |
| 388 Set set = create(); |
| 389 var e1 = new CE(0); |
| 390 var e2 = new CE(0); |
| 391 Expect.equals(e1, e2); |
| 392 Expect.isFalse(identical(e1, e2)); |
| 393 |
| 394 testLength(0, set); |
| 395 set.add(e1); |
| 396 testLength(1, set); |
| 397 Expect.isTrue(set.contains(e1)); |
| 398 Expect.isFalse(set.contains(e2)); |
| 399 |
| 400 set.add(e2); |
| 401 testLength(2, set); |
| 402 Expect.isTrue(set.contains(e1)); |
| 403 Expect.isTrue(set.contains(e2)); |
| 404 |
| 405 var set2 = set.toSet(); |
| 406 testLength(2, set2); |
| 407 Expect.isTrue(set2.contains(e1)); |
| 408 Expect.isTrue(set2.contains(e2)); |
| 409 } |
| 410 |
| 362 main() { | 411 main() { |
| 363 testMain(() => new HashSet()); | 412 testMain(() => new HashSet()); |
| 364 testMain(() => new LinkedHashSet()); | 413 testMain(() => new LinkedHashSet()); |
| 414 testMain(() => new HashSet.identity()); |
| 415 testMain(() => new LinkedHashSet.identity()); |
| 365 testMain(() => new HashSet(equals: identical)); | 416 testMain(() => new HashSet(equals: identical)); |
| 366 testMain(() => new LinkedHashSet(equals: identical)); | 417 testMain(() => new LinkedHashSet(equals: identical)); |
| 367 testMain(() => new HashSet(equals: (a, b) => a == b, | 418 testMain(() => new HashSet(equals: (a, b) => a == b, |
| 368 hashCode: (a) => -a.hashCode, | 419 hashCode: (a) => -a.hashCode, |
| 369 isValidKey: (a) => true)); | 420 isValidKey: (a) => true)); |
| 370 testMain(() => new LinkedHashSet( | 421 testMain(() => new LinkedHashSet( |
| 371 equals: (a, b) => a == b, | 422 equals: (a, b) => a == b, |
| 372 hashCode: (a) => -a.hashCode, | 423 hashCode: (a) => -a.hashCode, |
| 373 isValidKey: (a) => true)); | 424 isValidKey: (a) => true)); |
| 374 testMain(() => new SplayTreeSet()); | 425 testMain(() => new SplayTreeSet()); |
| 375 | 426 |
| 427 testIdentity(() => new HashSet.identity()); |
| 428 testIdentity(() => new LinkedHashSet.identity()); |
| 429 testIdentity(() => new HashSet(equals: identical)); |
| 430 testIdentity(() => new LinkedHashSet(equals: identical)); |
| 431 testIdentity(() => new SplayTreeSet(identityCompare)); |
| 432 |
| 376 testTypeAnnotations(new HashSet<int>()); | 433 testTypeAnnotations(new HashSet<int>()); |
| 377 testTypeAnnotations(new LinkedHashSet<int>()); | 434 testTypeAnnotations(new LinkedHashSet<int>()); |
| 378 testTypeAnnotations(new HashSet<int>(equals: identical)); | 435 testTypeAnnotations(new HashSet<int>(equals: identical)); |
| 379 testTypeAnnotations(new LinkedHashSet<int>(equals: identical)); | 436 testTypeAnnotations(new LinkedHashSet<int>(equals: identical)); |
| 380 testTypeAnnotations(new HashSet<int>(equals: (int a, int b) => a == b, | 437 testTypeAnnotations(new HashSet<int>(equals: (int a, int b) => a == b, |
| 381 hashCode: (int a) => a.hashCode, | 438 hashCode: (int a) => a.hashCode, |
| 382 isValidKey: (a) => a is int)); | 439 isValidKey: (a) => a is int)); |
| 383 testTypeAnnotations(new LinkedHashSet<int>(equals: (int a, int b) => a == b, | 440 testTypeAnnotations(new LinkedHashSet<int>(equals: (int a, int b) => a == b, |
| 384 hashCode: (int a) => a.hashCode, | 441 hashCode: (int a) => a.hashCode, |
| 385 isValidKey: (a) => a is int)); | 442 isValidKey: (a) => a is int)); |
| 386 testTypeAnnotations(new SplayTreeSet<int>()); | 443 testTypeAnnotations(new SplayTreeSet<int>()); |
| 387 | 444 |
| 388 testRetainWhere(([equals, hashCode, validKey, comparator]) => | 445 testRetainWhere(([equals, hashCode, validKey, comparator]) => |
| 389 new HashSet(equals: equals, hashCode: hashCode, isValidKey: validKey)); | 446 new HashSet(equals: equals, hashCode: hashCode, isValidKey: validKey)); |
| 390 testRetainWhere(([equals, hashCode, validKey, comparator]) => | 447 testRetainWhere(([equals, hashCode, validKey, comparator]) => |
| 391 new LinkedHashSet(equals: equals, hashCode: hashCode, | 448 new LinkedHashSet(equals: equals, hashCode: hashCode, |
| 392 isValidKey: validKey)); | 449 isValidKey: validKey)); |
| 393 testRetainWhere(([equals, hashCode, validKey, comparator]) => | 450 testRetainWhere(([equals, hashCode, validKey, comparator]) => |
| 394 new SplayTreeSet(comparator, validKey)); | 451 new SplayTreeSet(comparator, validKey)); |
| 395 | 452 |
| 396 testDifferenceIntersection(([equals, hashCode, validKey, comparator]) => | 453 testDifferenceIntersection(([equals, hashCode, validKey, comparator]) => |
| 397 new HashSet(equals: equals, hashCode: hashCode, isValidKey: validKey)); | 454 new HashSet(equals: equals, hashCode: hashCode, isValidKey: validKey)); |
| 398 testDifferenceIntersection(([equals, hashCode, validKey, comparator]) => | 455 testDifferenceIntersection(([equals, hashCode, validKey, comparator]) => |
| 399 new LinkedHashSet(equals: equals, hashCode: hashCode, | 456 new LinkedHashSet(equals: equals, hashCode: hashCode, |
| 400 isValidKey: validKey)); | 457 isValidKey: validKey)); |
| 401 testDifferenceIntersection(([equals, hashCode, validKey, comparator]) => | 458 testDifferenceIntersection(([equals, hashCode, validKey, comparator]) => |
| 402 new SplayTreeSet(comparator, validKey)); | 459 new SplayTreeSet(comparator, validKey)); |
| 403 | |
| 404 } | 460 } |
| OLD | NEW |