Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: tests/corelib_2/map_test.dart

Issue 2990903002: Migrated test block 16 to Dart 2.0. (Closed)
Patch Set: Changed useIntegerKeys to a named parameter Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/corelib_2/map_remove_test.dart ('k') | tests/corelib_strong/corelib_strong.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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()); 24 test(newJsonMap(), useIntegerKeys: false);
25 test(newJsonMapCustomReviver()); 25 test(newJsonMapCustomReviver(), useIntegerKeys: false);
26 testLinkedHashMap(); 26 testLinkedHashMap();
27 testMapLiteral(); 27 testMapLiteral();
28 testNullValue(); 28 testNullValue();
29 testTypes(); 29 testTypes();
30 30
31 testWeirdStringKeys(new Map()); 31 testWeirdStringKeys(new Map());
32 testWeirdStringKeys(new Map<String, String>()); 32 testWeirdStringKeys(new Map<String, String>());
33 testWeirdStringKeys(new HashMap()); 33 testWeirdStringKeys(new HashMap());
34 testWeirdStringKeys(new HashMap<String, String>()); 34 testWeirdStringKeys(new HashMap<String, String>());
35 testWeirdStringKeys(new LinkedHashMap()); 35 testWeirdStringKeys(new LinkedHashMap());
(...skipping 10 matching lines...) Expand all
46 testNumericKeys(new HashMap()); 46 testNumericKeys(new HashMap());
47 testNumericKeys(new HashMap<num, String>()); 47 testNumericKeys(new HashMap<num, String>());
48 testNumericKeys(new HashMap.identity()); 48 testNumericKeys(new HashMap.identity());
49 testNumericKeys(new HashMap<num, String>.identity()); 49 testNumericKeys(new HashMap<num, String>.identity());
50 testNumericKeys(new LinkedHashMap()); 50 testNumericKeys(new LinkedHashMap());
51 testNumericKeys(new LinkedHashMap<num, String>()); 51 testNumericKeys(new LinkedHashMap<num, String>());
52 testNumericKeys(new LinkedHashMap.identity()); 52 testNumericKeys(new LinkedHashMap.identity());
53 testNumericKeys(new LinkedHashMap<num, String>.identity()); 53 testNumericKeys(new LinkedHashMap<num, String>.identity());
54 testNumericKeys(new MapBaseMap<num, String>()); 54 testNumericKeys(new MapBaseMap<num, String>());
55 testNumericKeys(new MapMixinMap<num, String>()); 55 testNumericKeys(new MapMixinMap<num, String>());
56 testNumericKeys(newJsonMap());
57 testNumericKeys(newJsonMapCustomReviver());
58 56
59 testNaNKeys(new Map()); 57 testNaNKeys(new Map());
60 testNaNKeys(new Map<num, String>()); 58 testNaNKeys(new Map<num, String>());
61 testNaNKeys(new HashMap()); 59 testNaNKeys(new HashMap());
62 testNaNKeys(new HashMap<num, String>()); 60 testNaNKeys(new HashMap<num, String>());
63 testNaNKeys(new LinkedHashMap()); 61 testNaNKeys(new LinkedHashMap());
64 testNaNKeys(new LinkedHashMap<num, String>()); 62 testNaNKeys(new LinkedHashMap<num, String>());
65 testNaNKeys(new MapBaseMap<num, String>()); 63 testNaNKeys(new MapBaseMap<num, String>());
66 testNaNKeys(new MapMixinMap<num, String>()); 64 testNaNKeys(new MapMixinMap<num, String>());
67 testNaNKeys(newJsonMap());
68 testNaNKeys(newJsonMapCustomReviver());
69 // Identity maps fail the NaN-keys tests because the test assumes that 65 // Identity maps fail the NaN-keys tests because the test assumes that
70 // NaN is not equal to NaN. 66 // NaN is not equal to NaN.
71 67
72 testIdentityMap(new Map.identity()); 68 testIdentityMap(new Map.identity());
73 testIdentityMap(new HashMap.identity()); 69 testIdentityMap(new HashMap.identity());
74 testIdentityMap(new LinkedHashMap.identity()); 70 testIdentityMap(new LinkedHashMap.identity());
75 testIdentityMap(new HashMap(equals: identical, hashCode: identityHashCode)); 71 testIdentityMap(new HashMap(equals: identical, hashCode: identityHashCode));
76 testIdentityMap( 72 testIdentityMap(
77 new LinkedHashMap(equals: identical, hashCode: identityHashCode)); 73 new LinkedHashMap(equals: identical, hashCode: identityHashCode));
78 testIdentityMap(new HashMap( 74 testIdentityMap(new HashMap(
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 testOtherKeys(new LinkedHashMap<int, int>()); 110 testOtherKeys(new LinkedHashMap<int, int>());
115 testOtherKeys(new LinkedHashMap<int, int>.identity()); 111 testOtherKeys(new LinkedHashMap<int, int>.identity());
116 testOtherKeys(new LinkedHashMap<int, int>( 112 testOtherKeys(new LinkedHashMap<int, int>(
117 hashCode: (v) => v.hashCode, isValidKey: (v) => v is int)); 113 hashCode: (v) => v.hashCode, isValidKey: (v) => v is int));
118 testOtherKeys(new LinkedHashMap( 114 testOtherKeys(new LinkedHashMap(
119 equals: (int x, int y) => x == y, 115 equals: (int x, int y) => x == y,
120 hashCode: (int v) => v.hashCode, 116 hashCode: (int v) => v.hashCode,
121 isValidKey: (v) => v is int)); 117 isValidKey: (v) => v is int));
122 testOtherKeys(new MapBaseMap<int, int>()); 118 testOtherKeys(new MapBaseMap<int, int>());
123 testOtherKeys(new MapMixinMap<int, int>()); 119 testOtherKeys(new MapMixinMap<int, int>());
124 testOtherKeys(newJsonMap());
125 testOtherKeys(newJsonMapCustomReviver());
126 120
127 testUnmodifiableMap(const {1: 37}); 121 testUnmodifiableMap(const {1: 37});
128 testUnmodifiableMap(new UnmodifiableMapView({1: 37})); 122 testUnmodifiableMap(new UnmodifiableMapView({1: 37}));
129 testUnmodifiableMap(new UnmodifiableMapBaseMap([1, 37])); 123 testUnmodifiableMap(new UnmodifiableMapBaseMap([1, 37]));
130 124
131 testFrom(); 125 testFrom();
132 } 126 }
133 127
134 void test(Map map) { 128 void test(Map map, {bool useIntegerKeys = true}) {
135 testDeletedElement(map); 129 testDeletedElement(map);
136 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 }
137 map.clear(); 133 map.clear();
138 testMap(map, "value1", "value2", "value3", "value4", "value5", "value6", 134 testMap(map, "value1", "value2", "value3", "value4", "value5", "value6",
139 "value7", "value8"); 135 "value7", "value8");
140 } 136 }
141 137
142 void testLinkedHashMap() { 138 void testLinkedHashMap() {
143 LinkedHashMap map = new LinkedHashMap(); 139 LinkedHashMap map = new LinkedHashMap();
144 Expect.equals(false, map.containsKey(1)); 140 Expect.equals(false, map.containsKey(1));
145 map[1] = 1; 141 map[1] = 1;
146 map[1] = 2; 142 map[1] = 2;
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 map.clear(); 298 map.clear();
303 Expect.equals(false, map.containsKey(key1)); 299 Expect.equals(false, map.containsKey(key1));
304 map.putIfAbsent(key1, () => 10); 300 map.putIfAbsent(key1, () => 10);
305 Expect.equals(true, map.containsKey(key1)); 301 Expect.equals(true, map.containsKey(key1));
306 Expect.equals(10, map[key1]); 302 Expect.equals(10, map[key1]);
307 Expect.equals(10, map.putIfAbsent(key1, () => 11)); 303 Expect.equals(10, map.putIfAbsent(key1, () => 11));
308 304
309 // Test Map.addAll. 305 // Test Map.addAll.
310 map.clear(); 306 map.clear();
311 otherMap.clear(); 307 otherMap.clear();
312 otherMap[99] = 1; 308 otherMap['99'] = 1;
313 otherMap[50] = 50; 309 otherMap['50'] = 50;
314 otherMap[1] = 99; 310 otherMap['1'] = 99;
315 map.addAll(otherMap); 311 map.addAll(otherMap);
316 Expect.equals(3, map.length); 312 Expect.equals(3, map.length);
317 Expect.equals(1, map[99]); 313 Expect.equals(1, map['99']);
318 Expect.equals(50, map[50]); 314 Expect.equals(50, map['50']);
319 Expect.equals(99, map[1]); 315 Expect.equals(99, map['1']);
320 otherMap[50] = 42; 316 otherMap['50'] = 42;
321 map.addAll(new HashMap.from(otherMap)); 317 map.addAll(new HashMap.from(otherMap));
322 Expect.equals(3, map.length); 318 Expect.equals(3, map.length);
323 Expect.equals(1, map[99]); 319 Expect.equals(1, map['99']);
324 Expect.equals(42, map[50]); 320 Expect.equals(42, map['50']);
325 Expect.equals(99, map[1]); 321 Expect.equals(99, map['1']);
326 otherMap[99] = 7; 322 otherMap['99'] = 7;
327 map.addAll(new SplayTreeMap.from(otherMap)); 323 map.addAll(new SplayTreeMap.from(otherMap));
328 Expect.equals(3, map.length); 324 Expect.equals(3, map.length);
329 Expect.equals(7, map[99]); 325 Expect.equals(7, map['99']);
330 Expect.equals(42, map[50]); 326 Expect.equals(42, map['50']);
331 Expect.equals(99, map[1]); 327 Expect.equals(99, map['1']);
332 otherMap.remove(99); 328 otherMap.remove('99');
333 map[99] = 0; 329 map['99'] = 0;
334 map.addAll(otherMap); 330 map.addAll(otherMap);
335 Expect.equals(3, map.length); 331 Expect.equals(3, map.length);
336 Expect.equals(0, map[99]); 332 Expect.equals(0, map['99']);
337 Expect.equals(42, map[50]); 333 Expect.equals(42, map['50']);
338 Expect.equals(99, map[1]); 334 Expect.equals(99, map['1']);
339 map.clear(); 335 map.clear();
340 otherMap.clear(); 336 otherMap.clear();
341 map.addAll(otherMap); 337 map.addAll(otherMap);
342 Expect.equals(0, map.length); 338 Expect.equals(0, map.length);
343 } 339 }
344 340
345 void testDeletedElement(Map map) { 341 void testDeletedElement(Map map) {
346 map.clear(); 342 map.clear();
347 for (int i = 0; i < 100; i++) { 343 for (int i = 0; i < 100; i++) {
348 map[1] = 2; 344 map['1'] = 2;
349 testLength(1, map); 345 testLength(1, map);
350 map.remove(1); 346 map.remove('1');
351 testLength(0, map); 347 testLength(0, map);
352 } 348 }
353 testLength(0, map); 349 testLength(0, map);
354 } 350 }
355 351
356 void testMapLiteral() { 352 void testMapLiteral() {
357 Map m = {"a": 1, "b": 2, "c": 3}; 353 Map m = {"a": 1, "b": 2, "c": 3};
358 Expect.equals(3, m.length); 354 Expect.equals(3, m.length);
359 int sum = 0; 355 int sum = 0;
360 m.forEach((a, b) { 356 m.forEach((a, b) {
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after
786 if (other is! Customer) return false; 782 if (other is! Customer) return false;
787 Customer otherCustomer = other; 783 Customer otherCustomer = other;
788 return id == otherCustomer.id; 784 return id == otherCustomer.id;
789 } 785 }
790 } 786 }
791 787
792 int myHashCode(Customer c) => c.secondId; 788 int myHashCode(Customer c) => c.secondId;
793 bool myEquals(Customer a, Customer b) => a.secondId == b.secondId; 789 bool myEquals(Customer a, Customer b) => a.secondId == b.secondId;
794 790
795 void testIterationOrder(Map map) { 791 void testIterationOrder(Map map) {
796 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'];
797 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;
798 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']);
799 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]);
800 } 797 }
801 798
802 void testOtherKeys(Map<int, int> map) { 799 void testOtherKeys(Map<int, int> map) {
803 // Test that non-int keys are allowed in containsKey/remove/lookup. 800 // Test that non-int keys are allowed in containsKey/remove/lookup.
804 // 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
805 // use the equality/comparator on incompatible objects. 802 // use the equality/comparator on incompatible objects.
806 803
807 // This should not throw in either checked or unchecked mode. 804 // This should not throw in either checked or unchecked mode.
808 map[0] = 0;
809 map[1] = 1;
810 map[2] = 2;
811 Expect.isFalse(map.containsKey("not an int")); 805 Expect.isFalse(map.containsKey("not an int"));
812 Expect.isFalse(map.containsKey(1.5)); 806 Expect.isFalse(map.containsKey(1.5));
813 Expect.isNull(map.remove("not an int")); 807 Expect.isNull(map.remove("not an int"));
814 Expect.isNull(map.remove(1.5)); 808 Expect.isNull(map.remove(1.5));
815 Expect.isNull(map["not an int"]); 809 Expect.isNull(map["not an int"]);
816 Expect.isNull(map[1.5]); 810 Expect.isNull(map[1.5]);
817 } 811 }
818 812
819 class Mutable { 813 class Mutable {
820 int id; 814 int id;
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
912 final List _values = <V>[]; 906 final List _values = <V>[];
913 UnmodifiableMapBaseMap(List pairs) { 907 UnmodifiableMapBaseMap(List pairs) {
914 for (int i = 0; i < pairs.length; i += 2) { 908 for (int i = 0; i < pairs.length; i += 2) {
915 _keys.add(pairs[i]); 909 _keys.add(pairs[i]);
916 _values.add(pairs[i + 1]); 910 _values.add(pairs[i + 1]);
917 } 911 }
918 } 912 }
919 913
920 int get _modCount => 0; 914 int get _modCount => 0;
921 915
922 V operator [](K key) { 916 V operator [](Object key) {
923 int index = _keys.indexOf(key); 917 int index = _keys.indexOf(key);
924 if (index < 0) return null; 918 if (index < 0) return null;
925 return _values[index]; 919 return _values[index];
926 } 920 }
927 921
928 Iterable<K> get keys => _keys.skip(0); 922 Iterable<K> get keys => _keys.skip(0);
929 } 923 }
930 924
931 abstract class Super implements Comparable {} 925 abstract class Super implements Comparable {}
932 926
933 abstract class Interface implements Comparable {} 927 abstract class Interface implements Comparable {}
934 928
935 class Sub extends Super implements Interface, Comparable { 929 class Sub extends Super implements Interface, Comparable {
936 int compareTo(Sub other) => 0; 930 int compareTo(dynamic other) => 0;
937 int get hashCode => 0; 931 int get hashCode => 0;
938 bool operator ==(other) => other is Sub; 932 bool operator ==(other) => other is Sub;
939 } 933 }
940 934
941 expectMap(Map expect, Map actual) { 935 expectMap(Map expect, Map actual) {
942 Expect.equals(expect.length, actual.length, "length"); 936 Expect.equals(expect.length, actual.length, "length");
943 for (var key in expect.keys) { 937 for (var key in expect.keys) {
944 Expect.isTrue(actual.containsKey(key), "containsKey $key"); 938 Expect.isTrue(actual.containsKey(key), "containsKey $key");
945 Expect.equals(expect[key], actual[key]); 939 Expect.equals(expect[key], actual[key]);
946 } 940 }
(...skipping 28 matching lines...) Expand all
975 Map<Interface, Interface> interfaceMap = <Interface, Interface>{sub: sub}; 969 Map<Interface, Interface> interfaceMap = <Interface, Interface>{sub: sub};
976 expectMap(superMap, new Map<Super, Super>.from(interfaceMap)); 970 expectMap(superMap, new Map<Super, Super>.from(interfaceMap));
977 expectMap(superMap, new Map<Interface, Interface>.from(superMap)); 971 expectMap(superMap, new Map<Interface, Interface>.from(superMap));
978 expectMap(superMap, new HashMap<Super, Super>.from(interfaceMap)); 972 expectMap(superMap, new HashMap<Super, Super>.from(interfaceMap));
979 expectMap(superMap, new HashMap<Interface, Interface>.from(superMap)); 973 expectMap(superMap, new HashMap<Interface, Interface>.from(superMap));
980 expectMap(superMap, new LinkedHashMap<Super, Super>.from(interfaceMap)); 974 expectMap(superMap, new LinkedHashMap<Super, Super>.from(interfaceMap));
981 expectMap(superMap, new LinkedHashMap<Interface, Interface>.from(superMap)); 975 expectMap(superMap, new LinkedHashMap<Interface, Interface>.from(superMap));
982 expectMap(superMap, new SplayTreeMap<Super, Super>.from(interfaceMap)); 976 expectMap(superMap, new SplayTreeMap<Super, Super>.from(interfaceMap));
983 expectMap(superMap, new SplayTreeMap<Interface, Interface>.from(superMap)); 977 expectMap(superMap, new SplayTreeMap<Interface, Interface>.from(superMap));
984 } 978 }
OLDNEW
« no previous file with comments | « tests/corelib_2/map_remove_test.dart ('k') | tests/corelib_strong/corelib_strong.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698