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 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:convert' show JSON; | 9 import 'dart:convert' show JSON; |
10 | 10 |
11 Map newJsonMap() => JSON.decode('{}'); | 11 Map newJsonMap() => JSON.decode('{}'); |
12 Map newJsonMapCustomReviver() => | 12 Map newJsonMapCustomReviver() => |
13 JSON.decode('{}', reviver: (key, value) => value); | 13 JSON.decode('{}', reviver: (key, value) => value); |
14 | 14 |
15 void main() { | 15 void main() { |
16 test(new HashMap()); | 16 test(new HashMap()); |
17 test(new LinkedHashMap()); | 17 test(new LinkedHashMap()); |
18 test(new SplayTreeMap()); | 18 test(new SplayTreeMap()); |
19 test(new SplayTreeMap(Comparable.compare)); | 19 test(new SplayTreeMap(Comparable.compare)); |
20 test(new MapView(new HashMap())); | 20 test(new MapView(new HashMap())); |
21 test(new MapView(new SplayTreeMap())); | 21 test(new MapView(new SplayTreeMap())); |
22 test(new MapBaseMap()); | 22 test(new MapBaseMap()); |
23 test(new MapMixinMap()); | 23 test(new MapMixinMap()); |
24 test(newJsonMap(), false); | |
25 test(newJsonMapCustomReviver(), false); | |
24 testLinkedHashMap(); | 26 testLinkedHashMap(); |
25 testMapLiteral(); | 27 testMapLiteral(); |
26 testNullValue(); | 28 testNullValue(); |
27 testTypes(); | 29 testTypes(); |
28 | 30 |
29 testWeirdStringKeys(new Map()); | 31 testWeirdStringKeys(new Map()); |
30 testWeirdStringKeys(new Map<String, String>()); | 32 testWeirdStringKeys(new Map<String, String>()); |
31 testWeirdStringKeys(new HashMap()); | 33 testWeirdStringKeys(new HashMap()); |
32 testWeirdStringKeys(new HashMap<String, String>()); | 34 testWeirdStringKeys(new HashMap<String, String>()); |
33 testWeirdStringKeys(new LinkedHashMap()); | 35 testWeirdStringKeys(new LinkedHashMap()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 hashCode: myHashCode, | 85 hashCode: myHashCode, |
84 isValidKey: (v) => v is Customer)); | 86 isValidKey: (v) => v is Customer)); |
85 testCustomMap( | 87 testCustomMap( |
86 new HashMap<Customer, dynamic>(equals: myEquals, hashCode: myHashCode)); | 88 new HashMap<Customer, dynamic>(equals: myEquals, hashCode: myHashCode)); |
87 | 89 |
88 testCustomMap(new LinkedHashMap<Customer, dynamic>( | 90 testCustomMap(new LinkedHashMap<Customer, dynamic>( |
89 equals: myEquals, hashCode: myHashCode)); | 91 equals: myEquals, hashCode: myHashCode)); |
90 | 92 |
91 testIterationOrder(new LinkedHashMap()); | 93 testIterationOrder(new LinkedHashMap()); |
92 testIterationOrder(new LinkedHashMap.identity()); | 94 testIterationOrder(new LinkedHashMap.identity()); |
95 testIterationOrder(newJsonMap()); | |
96 testIterationOrder(newJsonMapCustomReviver()); | |
93 | 97 |
94 testOtherKeys(new SplayTreeMap<int, int>()); | 98 testOtherKeys(new SplayTreeMap<int, int>()); |
95 testOtherKeys( | 99 testOtherKeys( |
96 new SplayTreeMap<int, int>((int a, int b) => a - b, (v) => v is int)); | 100 new SplayTreeMap<int, int>((int a, int b) => a - b, (v) => v is int)); |
97 testOtherKeys(new SplayTreeMap((int a, int b) => a - b, (v) => v is int)); | 101 testOtherKeys(new SplayTreeMap((int a, int b) => a - b, (v) => v is int)); |
98 testOtherKeys(new HashMap<int, int>()); | 102 testOtherKeys(new HashMap<int, int>()); |
99 testOtherKeys(new HashMap<int, int>.identity()); | 103 testOtherKeys(new HashMap<int, int>.identity()); |
100 testOtherKeys(new HashMap<int, int>( | 104 testOtherKeys(new HashMap<int, int>( |
101 hashCode: (v) => v.hashCode, isValidKey: (v) => v is int)); | 105 hashCode: (v) => v.hashCode, isValidKey: (v) => v is int)); |
102 testOtherKeys(new HashMap( | 106 testOtherKeys(new HashMap( |
(...skipping 11 matching lines...) Expand all Loading... | |
114 testOtherKeys(new MapBaseMap<int, int>()); | 118 testOtherKeys(new MapBaseMap<int, int>()); |
115 testOtherKeys(new MapMixinMap<int, int>()); | 119 testOtherKeys(new MapMixinMap<int, int>()); |
116 | 120 |
117 testUnmodifiableMap(const {1: 37}); | 121 testUnmodifiableMap(const {1: 37}); |
118 testUnmodifiableMap(new UnmodifiableMapView({1: 37})); | 122 testUnmodifiableMap(new UnmodifiableMapView({1: 37})); |
119 testUnmodifiableMap(new UnmodifiableMapBaseMap([1, 37])); | 123 testUnmodifiableMap(new UnmodifiableMapBaseMap([1, 37])); |
120 | 124 |
121 testFrom(); | 125 testFrom(); |
122 } | 126 } |
123 | 127 |
124 void test(Map map) { | 128 void test(Map map, [bool useIntegerKeys = true]) { |
Bob Nystrom
2017/07/31 21:19:16
Nit, make useIntegerKeys a named parameter.
https
| |
125 testDeletedElement(map); | 129 testDeletedElement(map); |
126 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); | 130 if (useIntegerKeys) { |
131 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8); | |
132 } | |
127 map.clear(); | 133 map.clear(); |
128 testMap(map, "value1", "value2", "value3", "value4", "value5", "value6", | 134 testMap(map, "value1", "value2", "value3", "value4", "value5", "value6", |
129 "value7", "value8"); | 135 "value7", "value8"); |
130 } | 136 } |
131 | 137 |
132 void testLinkedHashMap() { | 138 void testLinkedHashMap() { |
133 LinkedHashMap map = new LinkedHashMap(); | 139 LinkedHashMap map = new LinkedHashMap(); |
134 Expect.equals(false, map.containsKey(1)); | 140 Expect.equals(false, map.containsKey(1)); |
135 map[1] = 1; | 141 map[1] = 1; |
136 map[1] = 2; | 142 map[1] = 2; |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 map.clear(); | 298 map.clear(); |
293 Expect.equals(false, map.containsKey(key1)); | 299 Expect.equals(false, map.containsKey(key1)); |
294 map.putIfAbsent(key1, () => 10); | 300 map.putIfAbsent(key1, () => 10); |
295 Expect.equals(true, map.containsKey(key1)); | 301 Expect.equals(true, map.containsKey(key1)); |
296 Expect.equals(10, map[key1]); | 302 Expect.equals(10, map[key1]); |
297 Expect.equals(10, map.putIfAbsent(key1, () => 11)); | 303 Expect.equals(10, map.putIfAbsent(key1, () => 11)); |
298 | 304 |
299 // Test Map.addAll. | 305 // Test Map.addAll. |
300 map.clear(); | 306 map.clear(); |
301 otherMap.clear(); | 307 otherMap.clear(); |
302 otherMap[99] = 1; | 308 otherMap['99'] = 1; |
303 otherMap[50] = 50; | 309 otherMap['50'] = 50; |
304 otherMap[1] = 99; | 310 otherMap['1'] = 99; |
305 map.addAll(otherMap); | 311 map.addAll(otherMap); |
306 Expect.equals(3, map.length); | 312 Expect.equals(3, map.length); |
307 Expect.equals(1, map[99]); | 313 Expect.equals(1, map['99']); |
308 Expect.equals(50, map[50]); | 314 Expect.equals(50, map['50']); |
309 Expect.equals(99, map[1]); | 315 Expect.equals(99, map['1']); |
310 otherMap[50] = 42; | 316 otherMap['50'] = 42; |
311 map.addAll(new HashMap.from(otherMap)); | 317 map.addAll(new HashMap.from(otherMap)); |
312 Expect.equals(3, map.length); | 318 Expect.equals(3, map.length); |
313 Expect.equals(1, map[99]); | 319 Expect.equals(1, map['99']); |
314 Expect.equals(42, map[50]); | 320 Expect.equals(42, map['50']); |
315 Expect.equals(99, map[1]); | 321 Expect.equals(99, map['1']); |
316 otherMap[99] = 7; | 322 otherMap['99'] = 7; |
317 map.addAll(new SplayTreeMap.from(otherMap)); | 323 map.addAll(new SplayTreeMap.from(otherMap)); |
318 Expect.equals(3, map.length); | 324 Expect.equals(3, map.length); |
319 Expect.equals(7, map[99]); | 325 Expect.equals(7, map['99']); |
320 Expect.equals(42, map[50]); | 326 Expect.equals(42, map['50']); |
321 Expect.equals(99, map[1]); | 327 Expect.equals(99, map['1']); |
322 otherMap.remove(99); | 328 otherMap.remove('99'); |
323 map[99] = 0; | 329 map['99'] = 0; |
324 map.addAll(otherMap); | 330 map.addAll(otherMap); |
325 Expect.equals(3, map.length); | 331 Expect.equals(3, map.length); |
326 Expect.equals(0, map[99]); | 332 Expect.equals(0, map['99']); |
327 Expect.equals(42, map[50]); | 333 Expect.equals(42, map['50']); |
328 Expect.equals(99, map[1]); | 334 Expect.equals(99, map['1']); |
329 map.clear(); | 335 map.clear(); |
330 otherMap.clear(); | 336 otherMap.clear(); |
331 map.addAll(otherMap); | 337 map.addAll(otherMap); |
332 Expect.equals(0, map.length); | 338 Expect.equals(0, map.length); |
333 } | 339 } |
334 | 340 |
335 void testDeletedElement(Map map) { | 341 void testDeletedElement(Map map) { |
336 map.clear(); | 342 map.clear(); |
337 for (int i = 0; i < 100; i++) { | 343 for (int i = 0; i < 100; i++) { |
338 map[1] = 2; | 344 map['1'] = 2; |
339 testLength(1, map); | 345 testLength(1, map); |
340 map.remove(1); | 346 map.remove('1'); |
341 testLength(0, map); | 347 testLength(0, map); |
342 } | 348 } |
343 testLength(0, map); | 349 testLength(0, map); |
344 } | 350 } |
345 | 351 |
346 void testMapLiteral() { | 352 void testMapLiteral() { |
347 Map m = {"a": 1, "b": 2, "c": 3}; | 353 Map m = {"a": 1, "b": 2, "c": 3}; |
348 Expect.equals(3, m.length); | 354 Expect.equals(3, m.length); |
349 int sum = 0; | 355 int sum = 0; |
350 m.forEach((a, b) { | 356 m.forEach((a, b) { |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
776 if (other is! Customer) return false; | 782 if (other is! Customer) return false; |
777 Customer otherCustomer = other; | 783 Customer otherCustomer = other; |
778 return id == otherCustomer.id; | 784 return id == otherCustomer.id; |
779 } | 785 } |
780 } | 786 } |
781 | 787 |
782 int myHashCode(Customer c) => c.secondId; | 788 int myHashCode(Customer c) => c.secondId; |
783 bool myEquals(Customer a, Customer b) => a.secondId == b.secondId; | 789 bool myEquals(Customer a, Customer b) => a.secondId == b.secondId; |
784 | 790 |
785 void testIterationOrder(Map map) { | 791 void testIterationOrder(Map map) { |
786 var order = [0, 6, 4, 2, 7, 9, 7, 1, 2, 5, 3]; | 792 var order = ['0', '6', '4', '2', '7', '9', '7', '1', '2', '5', '3']; |
787 for (int i = 0; i < order.length; i++) map[order[i]] = i; | 793 for (int i = 0; i < order.length; i++) map[order[i]] = i; |
788 Expect.listEquals(map.keys.toList(), [0, 6, 4, 2, 7, 9, 1, 5, 3]); | 794 Expect.listEquals( |
795 map.keys.toList(), ['0', '6', '4', '2', '7', '9', '1', '5', '3']); | |
789 Expect.listEquals(map.values.toList(), [0, 1, 2, 8, 6, 5, 7, 9, 10]); | 796 Expect.listEquals(map.values.toList(), [0, 1, 2, 8, 6, 5, 7, 9, 10]); |
790 } | 797 } |
791 | 798 |
792 void testOtherKeys(Map<int, int> map) { | 799 void testOtherKeys(Map<int, int> map) { |
793 // Test that non-int keys are allowed in containsKey/remove/lookup. | 800 // Test that non-int keys are allowed in containsKey/remove/lookup. |
794 // Custom hash sets and tree sets must be constructed so they don't | 801 // Custom hash sets and tree sets must be constructed so they don't |
795 // use the equality/comparator on incompatible objects. | 802 // use the equality/comparator on incompatible objects. |
796 | 803 |
797 // This should not throw in either checked or unchecked mode. | 804 // This should not throw in either checked or unchecked mode. |
798 map[0] = 0; | |
799 map[1] = 1; | |
800 map[2] = 2; | |
801 Expect.isFalse(map.containsKey("not an int")); | 805 Expect.isFalse(map.containsKey("not an int")); |
802 Expect.isFalse(map.containsKey(1.5)); | 806 Expect.isFalse(map.containsKey(1.5)); |
803 Expect.isNull(map.remove("not an int")); | 807 Expect.isNull(map.remove("not an int")); |
804 Expect.isNull(map.remove(1.5)); | 808 Expect.isNull(map.remove(1.5)); |
805 Expect.isNull(map["not an int"]); | 809 Expect.isNull(map["not an int"]); |
806 Expect.isNull(map[1.5]); | 810 Expect.isNull(map[1.5]); |
807 } | 811 } |
808 | 812 |
809 class Mutable { | 813 class Mutable { |
810 int id; | 814 int id; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
902 final List _values = <V>[]; | 906 final List _values = <V>[]; |
903 UnmodifiableMapBaseMap(List pairs) { | 907 UnmodifiableMapBaseMap(List pairs) { |
904 for (int i = 0; i < pairs.length; i += 2) { | 908 for (int i = 0; i < pairs.length; i += 2) { |
905 _keys.add(pairs[i]); | 909 _keys.add(pairs[i]); |
906 _values.add(pairs[i + 1]); | 910 _values.add(pairs[i + 1]); |
907 } | 911 } |
908 } | 912 } |
909 | 913 |
910 int get _modCount => 0; | 914 int get _modCount => 0; |
911 | 915 |
912 V operator [](K key) { | 916 V operator [](Object key) { |
913 int index = _keys.indexOf(key); | 917 int index = _keys.indexOf(key); |
914 if (index < 0) return null; | 918 if (index < 0) return null; |
915 return _values[index]; | 919 return _values[index]; |
916 } | 920 } |
917 | 921 |
918 Iterable<K> get keys => _keys.skip(0); | 922 Iterable<K> get keys => _keys.skip(0); |
919 } | 923 } |
920 | 924 |
921 abstract class Super implements Comparable {} | 925 abstract class Super implements Comparable {} |
922 | 926 |
923 abstract class Interface implements Comparable {} | 927 abstract class Interface implements Comparable {} |
924 | 928 |
925 class Sub extends Super implements Interface, Comparable { | 929 class Sub extends Super implements Interface, Comparable { |
926 int compareTo(Sub other) => 0; | 930 int compareTo(dynamic other) => 0; |
927 int get hashCode => 0; | 931 int get hashCode => 0; |
928 bool operator ==(other) => other is Sub; | 932 bool operator ==(other) => other is Sub; |
929 } | 933 } |
930 | 934 |
931 expectMap(Map expect, Map actual) { | 935 expectMap(Map expect, Map actual) { |
932 Expect.equals(expect.length, actual.length, "length"); | 936 Expect.equals(expect.length, actual.length, "length"); |
933 for (var key in expect.keys) { | 937 for (var key in expect.keys) { |
934 Expect.isTrue(actual.containsKey(key), "containsKey $key"); | 938 Expect.isTrue(actual.containsKey(key), "containsKey $key"); |
935 Expect.equals(expect[key], actual[key]); | 939 Expect.equals(expect[key], actual[key]); |
936 } | 940 } |
(...skipping 28 matching lines...) Expand all Loading... | |
965 Map<Interface, Interface> interfaceMap = <Interface, Interface>{sub: sub}; | 969 Map<Interface, Interface> interfaceMap = <Interface, Interface>{sub: sub}; |
966 expectMap(superMap, new Map<Super, Super>.from(interfaceMap)); | 970 expectMap(superMap, new Map<Super, Super>.from(interfaceMap)); |
967 expectMap(superMap, new Map<Interface, Interface>.from(superMap)); | 971 expectMap(superMap, new Map<Interface, Interface>.from(superMap)); |
968 expectMap(superMap, new HashMap<Super, Super>.from(interfaceMap)); | 972 expectMap(superMap, new HashMap<Super, Super>.from(interfaceMap)); |
969 expectMap(superMap, new HashMap<Interface, Interface>.from(superMap)); | 973 expectMap(superMap, new HashMap<Interface, Interface>.from(superMap)); |
970 expectMap(superMap, new LinkedHashMap<Super, Super>.from(interfaceMap)); | 974 expectMap(superMap, new LinkedHashMap<Super, Super>.from(interfaceMap)); |
971 expectMap(superMap, new LinkedHashMap<Interface, Interface>.from(superMap)); | 975 expectMap(superMap, new LinkedHashMap<Interface, Interface>.from(superMap)); |
972 expectMap(superMap, new SplayTreeMap<Super, Super>.from(interfaceMap)); | 976 expectMap(superMap, new SplayTreeMap<Super, Super>.from(interfaceMap)); |
973 expectMap(superMap, new SplayTreeMap<Interface, Interface>.from(superMap)); | 977 expectMap(superMap, new SplayTreeMap<Interface, Interface>.from(superMap)); |
974 } | 978 } |
OLD | NEW |