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 15 matching lines...) Expand all Loading... |
26 testWeirdStringKeys(new SplayTreeMap<String, String>()); | 26 testWeirdStringKeys(new SplayTreeMap<String, String>()); |
27 | 27 |
28 testNumericKeys(new Map()); | 28 testNumericKeys(new Map()); |
29 testNumericKeys(new Map<num, String>()); | 29 testNumericKeys(new Map<num, String>()); |
30 testNumericKeys(new HashMap()); | 30 testNumericKeys(new HashMap()); |
31 testNumericKeys(new HashMap<num, String>()); | 31 testNumericKeys(new HashMap<num, String>()); |
32 testNumericKeys(new HashMap(equals: identical)); | 32 testNumericKeys(new HashMap(equals: identical)); |
33 testNumericKeys(new HashMap<num, String>(equals: identical)); | 33 testNumericKeys(new HashMap<num, String>(equals: identical)); |
34 testNumericKeys(new LinkedHashMap()); | 34 testNumericKeys(new LinkedHashMap()); |
35 testNumericKeys(new LinkedHashMap<num, String>()); | 35 testNumericKeys(new LinkedHashMap<num, String>()); |
| 36 testNumericKeys(new LinkedHashMap(equals: identical)); |
| 37 testNumericKeys(new LinkedHashMap<num, String>(equals: identical)); |
36 | 38 |
37 testNaNKeys(new Map()); | 39 testNaNKeys(new Map()); |
38 testNaNKeys(new Map<num, String>()); | 40 testNaNKeys(new Map<num, String>()); |
39 testNaNKeys(new HashMap()); | 41 testNaNKeys(new HashMap()); |
40 testNaNKeys(new HashMap<num, String>()); | 42 testNaNKeys(new HashMap<num, String>()); |
41 testNaNKeys(new LinkedHashMap()); | 43 testNaNKeys(new LinkedHashMap()); |
42 testNaNKeys(new LinkedHashMap<num, String>()); | 44 testNaNKeys(new LinkedHashMap<num, String>()); |
43 // Identity maps fail the NaN-keys tests because the test assumes that | 45 // Identity maps fail the NaN-keys tests because the test assumes that |
44 // NaN is not equal to NaN. | 46 // NaN is not equal to NaN. |
45 | 47 |
46 testIdentityMap(new HashMap(equals: identical)); | 48 testIdentityMap(new HashMap(equals: identical)); |
| 49 testIdentityMap(new LinkedHashMap(equals: identical)); |
47 | 50 |
48 testCustomMap(new HashMap(equals: myEquals, hashCode: myHashCode)); | 51 testCustomMap(new HashMap(equals: myEquals, hashCode: myHashCode, |
| 52 isValidKey: (v) => v is Customer)); |
| 53 testCustomMap(new LinkedHashMap(equals: myEquals, hashCode: myHashCode, |
| 54 isValidKey: (v) => v is Customer)); |
| 55 testCustomMap(new HashMap<Customer,dynamic>(equals: myEquals, |
| 56 hashCode: myHashCode)); |
| 57 |
| 58 testCustomMap(new LinkedHashMap<Customer,dynamic>(equals: myEquals, |
| 59 hashCode: myHashCode)); |
49 | 60 |
50 testIterationOrder(new LinkedHashMap()); | 61 testIterationOrder(new LinkedHashMap()); |
| 62 testIterationOrder(new LinkedHashMap(equals: identical)); |
| 63 |
| 64 testOtherKeys(new SplayTreeMap<int, int>()); |
| 65 testOtherKeys(new SplayTreeMap<int, int>((int a, int b) => a - b, |
| 66 (v) => v is int)); |
| 67 testOtherKeys(new SplayTreeMap((int a, int b) => a - b, |
| 68 (v) => v is int)); |
| 69 testOtherKeys(new HashMap<int, int>()); |
| 70 testOtherKeys(new HashMap<int, int>(equals: identical)); |
| 71 testOtherKeys(new HashMap<int, int>(hashCode: (v) => v.hashCode, |
| 72 isValidKey: (v) => v is int)); |
| 73 testOtherKeys(new HashMap(equals: (int x, int y) => x == y, |
| 74 hashCode: (int v) => v.hashCode, |
| 75 isValidKey: (v) => v is int)); |
| 76 testOtherKeys(new LinkedHashMap<int, int>()); |
| 77 testOtherKeys(new LinkedHashMap<int, int>(equals: identical)); |
| 78 testOtherKeys(new LinkedHashMap<int, int>(hashCode: (v) => v.hashCode, |
| 79 isValidKey: (v) => v is int)); |
| 80 testOtherKeys(new LinkedHashMap(equals: (int x, int y) => x == y, |
| 81 hashCode: (int v) => v.hashCode, |
| 82 isValidKey: (v) => v is int)); |
51 } | 83 } |
52 | 84 |
53 | 85 |
54 void test(Map map) { | 86 void test(Map map) { |
55 testDeletedElement(map); | 87 testDeletedElement(map); |
56 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); | 88 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); |
57 map.clear(); | 89 map.clear(); |
58 testMap(map, "value1", "value2", "value3", "value4", "value5", | 90 testMap(map, "value1", "value2", "value3", "value4", "value5", |
59 "value6", "value7", "value8"); | 91 "value6", "value7", "value8"); |
60 } | 92 } |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 bool operator==(Object other) { | 667 bool operator==(Object other) { |
636 if (other is! Customer) return false; | 668 if (other is! Customer) return false; |
637 Customer otherCustomer = other; | 669 Customer otherCustomer = other; |
638 return id == otherCustomer.id; | 670 return id == otherCustomer.id; |
639 } | 671 } |
640 } | 672 } |
641 | 673 |
642 int myHashCode(Customer c) => c.secondId; | 674 int myHashCode(Customer c) => c.secondId; |
643 bool myEquals(Customer a, Customer b) => a.secondId == b.secondId; | 675 bool myEquals(Customer a, Customer b) => a.secondId == b.secondId; |
644 | 676 |
645 testIterationOrder(Map map) { | 677 void testIterationOrder(Map map) { |
646 var order = [0, 6, 4, 2, 7, 9, 7, 1, 2, 5, 3]; | 678 var order = [0, 6, 4, 2, 7, 9, 7, 1, 2, 5, 3]; |
647 for (int i = 0; i < order.length; i++) map[order[i]] = i; | 679 for (int i = 0; i < order.length; i++) map[order[i]] = i; |
648 Expect.listEquals(map.keys.toList(), [0, 6, 4, 2, 7, 9, 1, 5, 3]); | 680 Expect.listEquals(map.keys.toList(), [0, 6, 4, 2, 7, 9, 1, 5, 3]); |
649 Expect.listEquals(map.values.toList(), [0, 1, 2, 8, 6, 5, 7, 9, 10]); | 681 Expect.listEquals(map.values.toList(), [0, 1, 2, 8, 6, 5, 7, 9, 10]); |
650 } | 682 } |
| 683 |
| 684 void testOtherKeys(Map<int, int> map) { |
| 685 // Test that non-int keys are allowed in containsKey/remove/lookup. |
| 686 // Custom hash sets and tree sets must be constructed so they don't |
| 687 // use the equality/comparator on incompatible objects. |
| 688 |
| 689 // This should not throw in either checked or unchecked mode. |
| 690 map[0] = 0; |
| 691 map[1] = 1; |
| 692 map[2] = 2; |
| 693 Expect.isFalse(map.containsKey("not an int")); |
| 694 Expect.isFalse(map.containsKey(1.5)); |
| 695 Expect.isNull(map.remove("not an int")); |
| 696 Expect.isNull(map.remove(1.5)); |
| 697 Expect.isNull(map["not an int"]); |
| 698 Expect.isNull(map[1.5]); |
| 699 } |
OLD | NEW |