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 map_test; | 5 library map_test; |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 void main() { | 9 void main() { |
10 test(new HashMap()); | 10 test(new HashMap()); |
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
449 if (key.isNaN) count++; | 449 if (key.isNaN) count++; |
450 }); | 450 }); |
451 Expect.equals(2, count); | 451 Expect.equals(2, count); |
452 | 452 |
453 map.clear(); | 453 map.clear(); |
454 Expect.isTrue(map.isEmpty); | 454 Expect.isTrue(map.isEmpty); |
455 } | 455 } |
456 | 456 |
457 void testLength(int length, Map map) { | 457 void testLength(int length, Map map) { |
458 Expect.equals(length, map.length); | 458 Expect.equals(length, map.length); |
459 (length == 0 ? Expect.isTrue : Expect.isFalse)(map.isEmpty); | 459 Expect.equals(length, map.keys.length); |
460 (length != 0 ? Expect.isTrue : Expect.isFalse)(map.isNotEmpty); | 460 Expect.equals(length, map.values.length); |
| 461 // Check being-empty. |
| 462 var ifEmpty = (length == 0) ? Expect.isTrue : Expect.isFalse; |
| 463 var ifNotEmpty = (length != 0) ? Expect.isTrue : Expect.isFalse; |
| 464 ifEmpty(map.isEmpty); |
| 465 ifNotEmpty(map.isNotEmpty); |
| 466 ifEmpty(map.keys.isEmpty); |
| 467 ifNotEmpty(map.keys.isNotEmpty); |
| 468 ifEmpty(map.values.isEmpty); |
| 469 ifNotEmpty(map.values.isNotEmpty); |
| 470 // Test key/value iterators match their isEmpty/isNotEmpty. |
| 471 ifNotEmpty(map.keys.iterator.moveNext()); |
| 472 ifNotEmpty(map.values.iterator.moveNext()); |
| 473 if (length == 0) { |
| 474 for (var k in map.keys) Expect.fail("contains key when iterating: $k"); |
| 475 for (var v in map.values) Expect.fail("contains values when iterating: $v"); |
| 476 } |
461 } | 477 } |
462 | 478 |
463 | 479 |
464 testIdentityMap(Map map) { | 480 testIdentityMap(Map map) { |
465 Expect.isTrue(map.isEmpty); | 481 Expect.isTrue(map.isEmpty); |
466 | 482 |
467 var nan = double.NAN; | 483 var nan = double.NAN; |
468 // TODO(11551): Remove guard when dart2js makes identical(NaN, NaN) true. | 484 // TODO(11551): Remove guard when dart2js makes identical(NaN, NaN) true. |
469 if (identical(nan, nan)) { | 485 if (identical(nan, nan)) { |
470 map[nan] = 42; | 486 map[nan] = 42; |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 map[0] = 0; | 706 map[0] = 0; |
691 map[1] = 1; | 707 map[1] = 1; |
692 map[2] = 2; | 708 map[2] = 2; |
693 Expect.isFalse(map.containsKey("not an int")); | 709 Expect.isFalse(map.containsKey("not an int")); |
694 Expect.isFalse(map.containsKey(1.5)); | 710 Expect.isFalse(map.containsKey(1.5)); |
695 Expect.isNull(map.remove("not an int")); | 711 Expect.isNull(map.remove("not an int")); |
696 Expect.isNull(map.remove(1.5)); | 712 Expect.isNull(map.remove(1.5)); |
697 Expect.isNull(map["not an int"]); | 713 Expect.isNull(map["not an int"]); |
698 Expect.isNull(map[1.5]); | 714 Expect.isNull(map[1.5]); |
699 } | 715 } |
OLD | NEW |