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)); | |
38 | 36 |
39 testNaNKeys(new Map()); | 37 testNaNKeys(new Map()); |
40 testNaNKeys(new Map<num, String>()); | 38 testNaNKeys(new Map<num, String>()); |
41 testNaNKeys(new HashMap()); | 39 testNaNKeys(new HashMap()); |
42 testNaNKeys(new HashMap<num, String>()); | 40 testNaNKeys(new HashMap<num, String>()); |
43 testNaNKeys(new LinkedHashMap()); | 41 testNaNKeys(new LinkedHashMap()); |
44 testNaNKeys(new LinkedHashMap<num, String>()); | 42 testNaNKeys(new LinkedHashMap<num, String>()); |
45 // Identity maps fail the NaN-keys tests because the test assumes that | 43 // Identity maps fail the NaN-keys tests because the test assumes that |
46 // NaN is not equal to NaN. | 44 // NaN is not equal to NaN. |
47 | 45 |
48 testIdentityMap(new HashMap(equals: identical)); | 46 testIdentityMap(new HashMap(equals: identical)); |
49 testIdentityMap(new LinkedHashMap(equals: identical)); | |
50 | 47 |
51 testCustomMap(new HashMap(equals: myEquals, hashCode: myHashCode, | 48 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)); | |
60 | 49 |
61 testIterationOrder(new LinkedHashMap()); | 50 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)); | |
83 } | 51 } |
84 | 52 |
85 | 53 |
86 void test(Map map) { | 54 void test(Map map) { |
87 testDeletedElement(map); | 55 testDeletedElement(map); |
88 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); | 56 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); |
89 map.clear(); | 57 map.clear(); |
90 testMap(map, "value1", "value2", "value3", "value4", "value5", | 58 testMap(map, "value1", "value2", "value3", "value4", "value5", |
91 "value6", "value7", "value8"); | 59 "value6", "value7", "value8"); |
92 } | 60 } |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
667 bool operator==(Object other) { | 635 bool operator==(Object other) { |
668 if (other is! Customer) return false; | 636 if (other is! Customer) return false; |
669 Customer otherCustomer = other; | 637 Customer otherCustomer = other; |
670 return id == otherCustomer.id; | 638 return id == otherCustomer.id; |
671 } | 639 } |
672 } | 640 } |
673 | 641 |
674 int myHashCode(Customer c) => c.secondId; | 642 int myHashCode(Customer c) => c.secondId; |
675 bool myEquals(Customer a, Customer b) => a.secondId == b.secondId; | 643 bool myEquals(Customer a, Customer b) => a.secondId == b.secondId; |
676 | 644 |
677 void testIterationOrder(Map map) { | 645 testIterationOrder(Map map) { |
678 var order = [0, 6, 4, 2, 7, 9, 7, 1, 2, 5, 3]; | 646 var order = [0, 6, 4, 2, 7, 9, 7, 1, 2, 5, 3]; |
679 for (int i = 0; i < order.length; i++) map[order[i]] = i; | 647 for (int i = 0; i < order.length; i++) map[order[i]] = i; |
680 Expect.listEquals(map.keys.toList(), [0, 6, 4, 2, 7, 9, 1, 5, 3]); | 648 Expect.listEquals(map.keys.toList(), [0, 6, 4, 2, 7, 9, 1, 5, 3]); |
681 Expect.listEquals(map.values.toList(), [0, 1, 2, 8, 6, 5, 7, 9, 10]); | 649 Expect.listEquals(map.values.toList(), [0, 1, 2, 8, 6, 5, 7, 9, 10]); |
682 } | 650 } |
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 |