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 11 matching lines...) Expand all Loading... |
22 testWeirdStringKeys(new HashMap<String, String>()); | 22 testWeirdStringKeys(new HashMap<String, String>()); |
23 testWeirdStringKeys(new LinkedHashMap()); | 23 testWeirdStringKeys(new LinkedHashMap()); |
24 testWeirdStringKeys(new LinkedHashMap<String, String>()); | 24 testWeirdStringKeys(new LinkedHashMap<String, String>()); |
25 testWeirdStringKeys(new SplayTreeMap()); | 25 testWeirdStringKeys(new SplayTreeMap()); |
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.identity()); |
33 testNumericKeys(new HashMap<num, String>(equals: identical)); | 33 testNumericKeys(new HashMap<num, String>.identity()); |
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)); | 36 testNumericKeys(new LinkedHashMap.identity()); |
37 testNumericKeys(new LinkedHashMap<num, String>(equals: identical)); | 37 testNumericKeys(new LinkedHashMap<num, String>.identity()); |
38 | 38 |
39 testNaNKeys(new Map()); | 39 testNaNKeys(new Map()); |
40 testNaNKeys(new Map<num, String>()); | 40 testNaNKeys(new Map<num, String>()); |
41 testNaNKeys(new HashMap()); | 41 testNaNKeys(new HashMap()); |
42 testNaNKeys(new HashMap<num, String>()); | 42 testNaNKeys(new HashMap<num, String>()); |
43 testNaNKeys(new LinkedHashMap()); | 43 testNaNKeys(new LinkedHashMap()); |
44 testNaNKeys(new LinkedHashMap<num, String>()); | 44 testNaNKeys(new LinkedHashMap<num, String>()); |
45 // 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 |
46 // NaN is not equal to NaN. | 46 // NaN is not equal to NaN. |
47 | 47 |
48 testIdentityMap(new HashMap(equals: identical)); | 48 testIdentityMap(new Map.identity()); |
49 testIdentityMap(new LinkedHashMap(equals: identical)); | 49 testIdentityMap(new HashMap.identity()); |
| 50 testIdentityMap(new LinkedHashMap.identity()); |
| 51 testIdentityMap(new HashMap(equals: identical, |
| 52 hashCode: identityHashCode)); |
| 53 testIdentityMap(new LinkedHashMap(equals: identical, |
| 54 hashCode: identityHashCode)); |
| 55 testIdentityMap(new HashMap(equals: (x, y) => identical(x, y), |
| 56 hashCode: (x) => identityHashCode(x))); |
| 57 testIdentityMap(new LinkedHashMap(equals: (x, y) => identical(x, y), |
| 58 hashCode: (x) => identityHashCode(x))); |
50 | 59 |
51 testCustomMap(new HashMap(equals: myEquals, hashCode: myHashCode, | 60 testCustomMap(new HashMap(equals: myEquals, hashCode: myHashCode, |
52 isValidKey: (v) => v is Customer)); | 61 isValidKey: (v) => v is Customer)); |
53 testCustomMap(new LinkedHashMap(equals: myEquals, hashCode: myHashCode, | 62 testCustomMap(new LinkedHashMap(equals: myEquals, hashCode: myHashCode, |
54 isValidKey: (v) => v is Customer)); | 63 isValidKey: (v) => v is Customer)); |
55 testCustomMap(new HashMap<Customer,dynamic>(equals: myEquals, | 64 testCustomMap(new HashMap<Customer,dynamic>(equals: myEquals, |
56 hashCode: myHashCode)); | 65 hashCode: myHashCode)); |
57 | 66 |
58 testCustomMap(new LinkedHashMap<Customer,dynamic>(equals: myEquals, | 67 testCustomMap(new LinkedHashMap<Customer,dynamic>(equals: myEquals, |
59 hashCode: myHashCode)); | 68 hashCode: myHashCode)); |
60 | 69 |
61 testIterationOrder(new LinkedHashMap()); | 70 testIterationOrder(new LinkedHashMap()); |
62 testIterationOrder(new LinkedHashMap(equals: identical)); | 71 testIterationOrder(new LinkedHashMap.identity()); |
63 | 72 |
64 testOtherKeys(new SplayTreeMap<int, int>()); | 73 testOtherKeys(new SplayTreeMap<int, int>()); |
65 testOtherKeys(new SplayTreeMap<int, int>((int a, int b) => a - b, | 74 testOtherKeys(new SplayTreeMap<int, int>((int a, int b) => a - b, |
66 (v) => v is int)); | 75 (v) => v is int)); |
67 testOtherKeys(new SplayTreeMap((int a, int b) => a - b, | 76 testOtherKeys(new SplayTreeMap((int a, int b) => a - b, |
68 (v) => v is int)); | 77 (v) => v is int)); |
69 testOtherKeys(new HashMap<int, int>()); | 78 testOtherKeys(new HashMap<int, int>()); |
70 testOtherKeys(new HashMap<int, int>(equals: identical)); | 79 testOtherKeys(new HashMap<int, int>.identity()); |
71 testOtherKeys(new HashMap<int, int>(hashCode: (v) => v.hashCode, | 80 testOtherKeys(new HashMap<int, int>(hashCode: (v) => v.hashCode, |
72 isValidKey: (v) => v is int)); | 81 isValidKey: (v) => v is int)); |
73 testOtherKeys(new HashMap(equals: (int x, int y) => x == y, | 82 testOtherKeys(new HashMap(equals: (int x, int y) => x == y, |
74 hashCode: (int v) => v.hashCode, | 83 hashCode: (int v) => v.hashCode, |
75 isValidKey: (v) => v is int)); | 84 isValidKey: (v) => v is int)); |
76 testOtherKeys(new LinkedHashMap<int, int>()); | 85 testOtherKeys(new LinkedHashMap<int, int>()); |
77 testOtherKeys(new LinkedHashMap<int, int>(equals: identical)); | 86 testOtherKeys(new LinkedHashMap<int, int>.identity()); |
78 testOtherKeys(new LinkedHashMap<int, int>(hashCode: (v) => v.hashCode, | 87 testOtherKeys(new LinkedHashMap<int, int>(hashCode: (v) => v.hashCode, |
79 isValidKey: (v) => v is int)); | 88 isValidKey: (v) => v is int)); |
80 testOtherKeys(new LinkedHashMap(equals: (int x, int y) => x == y, | 89 testOtherKeys(new LinkedHashMap(equals: (int x, int y) => x == y, |
81 hashCode: (int v) => v.hashCode, | 90 hashCode: (int v) => v.hashCode, |
82 isValidKey: (v) => v is int)); | 91 isValidKey: (v) => v is int)); |
83 } | 92 } |
84 | 93 |
85 | 94 |
86 void test(Map map) { | 95 void test(Map map) { |
87 testDeletedElement(map); | 96 testDeletedElement(map); |
(...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 testLength(4, map); | 586 testLength(4, map); |
578 | 587 |
579 // Transfer to equality-based map will collapse elements. | 588 // Transfer to equality-based map will collapse elements. |
580 Map eqMap = new HashMap(); | 589 Map eqMap = new HashMap(); |
581 eqMap.addAll(map); | 590 eqMap.addAll(map); |
582 testLength(2, eqMap); | 591 testLength(2, eqMap); |
583 Expect.isTrue(eqMap.containsKey(eq01)); | 592 Expect.isTrue(eqMap.containsKey(eq01)); |
584 Expect.isTrue(eqMap.containsKey(eq02)); | 593 Expect.isTrue(eqMap.containsKey(eq02)); |
585 Expect.isTrue(eqMap.containsKey(eq11)); | 594 Expect.isTrue(eqMap.containsKey(eq11)); |
586 Expect.isTrue(eqMap.containsKey(eq12)); | 595 Expect.isTrue(eqMap.containsKey(eq12)); |
| 596 |
| 597 // Changing objects will not affect identity map. |
| 598 map.clear(); |
| 599 var m1 = new Mutable(1); |
| 600 var m2 = new Mutable(2); |
| 601 var m3 = new Mutable(3); |
| 602 map[m1] = 1; |
| 603 map[m2] = 2; |
| 604 map[m3] = 3; |
| 605 Expect.equals(3, map.length); |
| 606 Expect.isTrue(map.containsKey(m1)); |
| 607 Expect.isTrue(map.containsKey(m2)); |
| 608 Expect.isTrue(map.containsKey(m3)); |
| 609 Expect.notEquals(m1, m3); |
| 610 m3.id = 1; |
| 611 Expect.equals(m1, m3); |
| 612 // Even if keys are equal, they are still not identical. |
| 613 // Even if hashcode of m3 changed, it can still be found. |
| 614 Expect.equals(1, map[m1]); |
| 615 Expect.equals(3, map[m3]); |
587 } | 616 } |
588 | 617 |
589 /** Class of objects that are equal if they hold the same id. */ | 618 /** Class of objects that are equal if they hold the same id. */ |
590 class Equalizer { | 619 class Equalizer { |
591 int id; | 620 int id; |
592 Equalizer(this.id); | 621 Equalizer(this.id); |
593 int get hashCode => id; | 622 int get hashCode => id; |
594 bool operator==(Object other) => | 623 bool operator==(Object other) => |
595 other is Equalizer && id == (other as Equalizer).id; | 624 other is Equalizer && id == (other as Equalizer).id; |
596 } | 625 } |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 map[0] = 0; | 735 map[0] = 0; |
707 map[1] = 1; | 736 map[1] = 1; |
708 map[2] = 2; | 737 map[2] = 2; |
709 Expect.isFalse(map.containsKey("not an int")); | 738 Expect.isFalse(map.containsKey("not an int")); |
710 Expect.isFalse(map.containsKey(1.5)); | 739 Expect.isFalse(map.containsKey(1.5)); |
711 Expect.isNull(map.remove("not an int")); | 740 Expect.isNull(map.remove("not an int")); |
712 Expect.isNull(map.remove(1.5)); | 741 Expect.isNull(map.remove(1.5)); |
713 Expect.isNull(map["not an int"]); | 742 Expect.isNull(map["not an int"]); |
714 Expect.isNull(map[1.5]); | 743 Expect.isNull(map[1.5]); |
715 } | 744 } |
| 745 |
| 746 class Mutable { |
| 747 int id; |
| 748 Mutable(this.id); |
| 749 int get hashCode => id; |
| 750 bool operator==(other) => other is Mutable && other.id == id; |
| 751 } |
OLD | NEW |