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

Side by Side Diff: tests/corelib/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/map_remove_test.dart ('k') | tests/corelib_2/corelib_2.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 library map_test;
6
7 import "package:expect/expect.dart";
8 import 'dart:collection';
9 import 'dart:convert' show JSON;
10
11 Map newJsonMap() => JSON.decode('{}');
12 Map newJsonMapCustomReviver() =>
13 JSON.decode('{}', reviver: (key, value) => value);
14
15 void main() {
16 test(new HashMap());
17 test(new LinkedHashMap());
18 test(new SplayTreeMap());
19 test(new SplayTreeMap(Comparable.compare));
20 test(new MapView(new HashMap()));
21 test(new MapView(new SplayTreeMap()));
22 test(new MapBaseMap());
23 test(new MapMixinMap());
24 testLinkedHashMap();
25 testMapLiteral();
26 testNullValue();
27 testTypes();
28
29 testWeirdStringKeys(new Map());
30 testWeirdStringKeys(new Map<String, String>());
31 testWeirdStringKeys(new HashMap());
32 testWeirdStringKeys(new HashMap<String, String>());
33 testWeirdStringKeys(new LinkedHashMap());
34 testWeirdStringKeys(new LinkedHashMap<String, String>());
35 testWeirdStringKeys(new SplayTreeMap());
36 testWeirdStringKeys(new SplayTreeMap<String, String>());
37 testWeirdStringKeys(new MapBaseMap<String, String>());
38 testWeirdStringKeys(new MapMixinMap<String, String>());
39 testWeirdStringKeys(newJsonMap());
40 testWeirdStringKeys(newJsonMapCustomReviver());
41
42 testNumericKeys(new Map());
43 testNumericKeys(new Map<num, String>());
44 testNumericKeys(new HashMap());
45 testNumericKeys(new HashMap<num, String>());
46 testNumericKeys(new HashMap.identity());
47 testNumericKeys(new HashMap<num, String>.identity());
48 testNumericKeys(new LinkedHashMap());
49 testNumericKeys(new LinkedHashMap<num, String>());
50 testNumericKeys(new LinkedHashMap.identity());
51 testNumericKeys(new LinkedHashMap<num, String>.identity());
52 testNumericKeys(new MapBaseMap<num, String>());
53 testNumericKeys(new MapMixinMap<num, String>());
54
55 testNaNKeys(new Map());
56 testNaNKeys(new Map<num, String>());
57 testNaNKeys(new HashMap());
58 testNaNKeys(new HashMap<num, String>());
59 testNaNKeys(new LinkedHashMap());
60 testNaNKeys(new LinkedHashMap<num, String>());
61 testNaNKeys(new MapBaseMap<num, String>());
62 testNaNKeys(new MapMixinMap<num, String>());
63 // Identity maps fail the NaN-keys tests because the test assumes that
64 // NaN is not equal to NaN.
65
66 testIdentityMap(new Map.identity());
67 testIdentityMap(new HashMap.identity());
68 testIdentityMap(new LinkedHashMap.identity());
69 testIdentityMap(new HashMap(equals: identical, hashCode: identityHashCode));
70 testIdentityMap(
71 new LinkedHashMap(equals: identical, hashCode: identityHashCode));
72 testIdentityMap(new HashMap(
73 equals: (x, y) => identical(x, y), hashCode: (x) => identityHashCode(x)));
74 testIdentityMap(new LinkedHashMap(
75 equals: (x, y) => identical(x, y), hashCode: (x) => identityHashCode(x)));
76
77 testCustomMap(new HashMap(
78 equals: myEquals,
79 hashCode: myHashCode,
80 isValidKey: (v) => v is Customer));
81 testCustomMap(new LinkedHashMap(
82 equals: myEquals,
83 hashCode: myHashCode,
84 isValidKey: (v) => v is Customer));
85 testCustomMap(
86 new HashMap<Customer, dynamic>(equals: myEquals, hashCode: myHashCode));
87
88 testCustomMap(new LinkedHashMap<Customer, dynamic>(
89 equals: myEquals, hashCode: myHashCode));
90
91 testIterationOrder(new LinkedHashMap());
92 testIterationOrder(new LinkedHashMap.identity());
93
94 testOtherKeys(new SplayTreeMap<int, int>());
95 testOtherKeys(
96 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));
98 testOtherKeys(new HashMap<int, int>());
99 testOtherKeys(new HashMap<int, int>.identity());
100 testOtherKeys(new HashMap<int, int>(
101 hashCode: (v) => v.hashCode, isValidKey: (v) => v is int));
102 testOtherKeys(new HashMap(
103 equals: (int x, int y) => x == y,
104 hashCode: (int v) => v.hashCode,
105 isValidKey: (v) => v is int));
106 testOtherKeys(new LinkedHashMap<int, int>());
107 testOtherKeys(new LinkedHashMap<int, int>.identity());
108 testOtherKeys(new LinkedHashMap<int, int>(
109 hashCode: (v) => v.hashCode, isValidKey: (v) => v is int));
110 testOtherKeys(new LinkedHashMap(
111 equals: (int x, int y) => x == y,
112 hashCode: (int v) => v.hashCode,
113 isValidKey: (v) => v is int));
114 testOtherKeys(new MapBaseMap<int, int>());
115 testOtherKeys(new MapMixinMap<int, int>());
116
117 testUnmodifiableMap(const {1: 37});
118 testUnmodifiableMap(new UnmodifiableMapView({1: 37}));
119 testUnmodifiableMap(new UnmodifiableMapBaseMap([1, 37]));
120
121 testFrom();
122 }
123
124 void test(Map map) {
125 testDeletedElement(map);
126 testMap(map, 1, 2, 3, 4, 5, 6, 7, 8);
127 map.clear();
128 testMap(map, "value1", "value2", "value3", "value4", "value5", "value6",
129 "value7", "value8");
130 }
131
132 void testLinkedHashMap() {
133 LinkedHashMap map = new LinkedHashMap();
134 Expect.equals(false, map.containsKey(1));
135 map[1] = 1;
136 map[1] = 2;
137 testLength(1, map);
138 }
139
140 void testMap(Map map, key1, key2, key3, key4, key5, key6, key7, key8) {
141 int value1 = 10;
142 int value2 = 20;
143 int value3 = 30;
144 int value4 = 40;
145 int value5 = 50;
146 int value6 = 60;
147 int value7 = 70;
148 int value8 = 80;
149
150 testLength(0, map);
151
152 map[key1] = value1;
153 Expect.equals(value1, map[key1]);
154 map[key1] = value2;
155 Expect.equals(false, map.containsKey(key2));
156 testLength(1, map);
157
158 map[key1] = value1;
159 Expect.equals(value1, map[key1]);
160 // Add enough entries to make sure the table grows.
161 map[key2] = value2;
162 Expect.equals(value2, map[key2]);
163 testLength(2, map);
164 map[key3] = value3;
165 Expect.equals(value2, map[key2]);
166 Expect.equals(value3, map[key3]);
167 map[key4] = value4;
168 Expect.equals(value3, map[key3]);
169 Expect.equals(value4, map[key4]);
170 map[key5] = value5;
171 Expect.equals(value4, map[key4]);
172 Expect.equals(value5, map[key5]);
173 map[key6] = value6;
174 Expect.equals(value5, map[key5]);
175 Expect.equals(value6, map[key6]);
176 map[key7] = value7;
177 Expect.equals(value6, map[key6]);
178 Expect.equals(value7, map[key7]);
179 map[key8] = value8;
180 Expect.equals(value1, map[key1]);
181 Expect.equals(value2, map[key2]);
182 Expect.equals(value3, map[key3]);
183 Expect.equals(value4, map[key4]);
184 Expect.equals(value5, map[key5]);
185 Expect.equals(value6, map[key6]);
186 Expect.equals(value7, map[key7]);
187 Expect.equals(value8, map[key8]);
188 testLength(8, map);
189
190 map.remove(key4);
191 Expect.equals(false, map.containsKey(key4));
192 testLength(7, map);
193
194 // Test clearing the table.
195 map.clear();
196 testLength(0, map);
197 Expect.equals(false, map.containsKey(key1));
198 Expect.equals(false, map.containsKey(key2));
199 Expect.equals(false, map.containsKey(key3));
200 Expect.equals(false, map.containsKey(key4));
201 Expect.equals(false, map.containsKey(key5));
202 Expect.equals(false, map.containsKey(key6));
203 Expect.equals(false, map.containsKey(key7));
204 Expect.equals(false, map.containsKey(key8));
205
206 // Test adding and removing again.
207 map[key1] = value1;
208 Expect.equals(value1, map[key1]);
209 testLength(1, map);
210 map[key2] = value2;
211 Expect.equals(value2, map[key2]);
212 testLength(2, map);
213 map[key3] = value3;
214 Expect.equals(value3, map[key3]);
215 map.remove(key3);
216 testLength(2, map);
217 map[key4] = value4;
218 Expect.equals(value4, map[key4]);
219 map.remove(key4);
220 testLength(2, map);
221 map[key5] = value5;
222 Expect.equals(value5, map[key5]);
223 map.remove(key5);
224 testLength(2, map);
225 map[key6] = value6;
226 Expect.equals(value6, map[key6]);
227 map.remove(key6);
228 testLength(2, map);
229 map[key7] = value7;
230 Expect.equals(value7, map[key7]);
231 map.remove(key7);
232 testLength(2, map);
233 map[key8] = value8;
234 Expect.equals(value8, map[key8]);
235 map.remove(key8);
236 testLength(2, map);
237
238 Expect.equals(true, map.containsKey(key1));
239 Expect.equals(true, map.containsValue(value1));
240
241 // Test Map.forEach.
242 Map otherMap = new Map();
243 void testForEachMap(key, value) {
244 otherMap[key] = value;
245 }
246
247 map.forEach(testForEachMap);
248 Expect.equals(true, otherMap.containsKey(key1));
249 Expect.equals(true, otherMap.containsKey(key2));
250 Expect.equals(true, otherMap.containsValue(value1));
251 Expect.equals(true, otherMap.containsValue(value2));
252 Expect.equals(2, otherMap.length);
253
254 otherMap.clear();
255 Expect.equals(0, otherMap.length);
256
257 // Test Collection.keys.
258 void testForEachCollection(value) {
259 otherMap[value] = value;
260 }
261
262 Iterable keys = map.keys;
263 keys.forEach(testForEachCollection);
264 Expect.equals(true, otherMap.containsKey(key1));
265 Expect.equals(true, otherMap.containsKey(key2));
266 Expect.equals(true, otherMap.containsValue(key1));
267 Expect.equals(true, otherMap.containsValue(key2));
268 Expect.equals(true, !otherMap.containsKey(value1));
269 Expect.equals(true, !otherMap.containsKey(value2));
270 Expect.equals(true, !otherMap.containsValue(value1));
271 Expect.equals(true, !otherMap.containsValue(value2));
272 Expect.equals(2, otherMap.length);
273 otherMap.clear();
274 Expect.equals(0, otherMap.length);
275
276 // Test Collection.values.
277 Iterable values = map.values;
278 values.forEach(testForEachCollection);
279 Expect.equals(true, !otherMap.containsKey(key1));
280 Expect.equals(true, !otherMap.containsKey(key2));
281 Expect.equals(true, !otherMap.containsValue(key1));
282 Expect.equals(true, !otherMap.containsValue(key2));
283 Expect.equals(true, otherMap.containsKey(value1));
284 Expect.equals(true, otherMap.containsKey(value2));
285 Expect.equals(true, otherMap.containsValue(value1));
286 Expect.equals(true, otherMap.containsValue(value2));
287 Expect.equals(2, otherMap.length);
288 otherMap.clear();
289 Expect.equals(0, otherMap.length);
290
291 // Test Map.putIfAbsent.
292 map.clear();
293 Expect.equals(false, map.containsKey(key1));
294 map.putIfAbsent(key1, () => 10);
295 Expect.equals(true, map.containsKey(key1));
296 Expect.equals(10, map[key1]);
297 Expect.equals(10, map.putIfAbsent(key1, () => 11));
298
299 // Test Map.addAll.
300 map.clear();
301 otherMap.clear();
302 otherMap[99] = 1;
303 otherMap[50] = 50;
304 otherMap[1] = 99;
305 map.addAll(otherMap);
306 Expect.equals(3, map.length);
307 Expect.equals(1, map[99]);
308 Expect.equals(50, map[50]);
309 Expect.equals(99, map[1]);
310 otherMap[50] = 42;
311 map.addAll(new HashMap.from(otherMap));
312 Expect.equals(3, map.length);
313 Expect.equals(1, map[99]);
314 Expect.equals(42, map[50]);
315 Expect.equals(99, map[1]);
316 otherMap[99] = 7;
317 map.addAll(new SplayTreeMap.from(otherMap));
318 Expect.equals(3, map.length);
319 Expect.equals(7, map[99]);
320 Expect.equals(42, map[50]);
321 Expect.equals(99, map[1]);
322 otherMap.remove(99);
323 map[99] = 0;
324 map.addAll(otherMap);
325 Expect.equals(3, map.length);
326 Expect.equals(0, map[99]);
327 Expect.equals(42, map[50]);
328 Expect.equals(99, map[1]);
329 map.clear();
330 otherMap.clear();
331 map.addAll(otherMap);
332 Expect.equals(0, map.length);
333 }
334
335 void testDeletedElement(Map map) {
336 map.clear();
337 for (int i = 0; i < 100; i++) {
338 map[1] = 2;
339 testLength(1, map);
340 map.remove(1);
341 testLength(0, map);
342 }
343 testLength(0, map);
344 }
345
346 void testMapLiteral() {
347 Map m = {"a": 1, "b": 2, "c": 3};
348 Expect.equals(3, m.length);
349 int sum = 0;
350 m.forEach((a, b) {
351 sum += b;
352 });
353 Expect.equals(6, sum);
354
355 List values = m.keys.toList();
356 Expect.equals(3, values.length);
357 String first = values[0];
358 String second = values[1];
359 String third = values[2];
360 String all = "${first}${second}${third}";
361 Expect.equals(3, all.length);
362 Expect.equals(true, all.contains("a", 0));
363 Expect.equals(true, all.contains("b", 0));
364 Expect.equals(true, all.contains("c", 0));
365 }
366
367 void testNullValue() {
368 Map m = {"a": 1, "b": null, "c": 3};
369
370 Expect.equals(null, m["b"]);
371 Expect.equals(true, m.containsKey("b"));
372 Expect.equals(3, m.length);
373
374 m["a"] = null;
375 m["c"] = null;
376 Expect.equals(null, m["a"]);
377 Expect.equals(true, m.containsKey("a"));
378 Expect.equals(null, m["c"]);
379 Expect.equals(true, m.containsKey("c"));
380 Expect.equals(3, m.length);
381
382 m.remove("a");
383 Expect.equals(2, m.length);
384 Expect.equals(null, m["a"]);
385 Expect.equals(false, m.containsKey("a"));
386 }
387
388 void testTypes() {
389 testMap(Map<num, String> map) {
390 Expect.isTrue(map is Map<num, String>);
391 Expect.isTrue(map is! Map<String, dynamic>);
392 Expect.isTrue(map is! Map<dynamic, int>);
393
394 // Use with properly typed keys and values.
395 map[42] = "text1";
396 map[43] = "text2";
397 map[42] = "text3";
398 Expect.equals("text3", map.remove(42));
399 Expect.equals(null, map[42]);
400 map[42] = "text4";
401
402 // Ensure that "containsKey", "containsValue" and "remove"
403 // accepts any object.
404 for (var object in [true, null, new Object()]) {
405 Expect.isFalse(map.containsKey(object));
406 Expect.isFalse(map.containsValue(object));
407 Expect.isNull(map.remove(object));
408 Expect.isNull(map[object]);
409 }
410 }
411
412 testMap(new HashMap<int, String>());
413 testMap(new LinkedHashMap<int, String>());
414 testMap(new SplayTreeMap<int, String>());
415 testMap(new SplayTreeMap<int, String>(Comparable.compare));
416 testMap(new SplayTreeMap<int, String>((int a, int b) => a.compareTo(b)));
417 testMap(new HashMap<num, String>());
418 testMap(new LinkedHashMap<num, String>());
419 testMap(new SplayTreeMap<num, String>());
420 testMap(new SplayTreeMap<num, String>(Comparable.compare));
421 testMap(new SplayTreeMap<num, String>((num a, num b) => a.compareTo(b)));
422 }
423
424 void testWeirdStringKeys(Map map) {
425 // Test weird keys.
426 var weirdKeys = const [
427 'hasOwnProperty',
428 'constructor',
429 'toLocaleString',
430 'propertyIsEnumerable',
431 '__defineGetter__',
432 '__defineSetter__',
433 '__lookupGetter__',
434 '__lookupSetter__',
435 'isPrototypeOf',
436 'toString',
437 'valueOf',
438 '__proto__',
439 '__count__',
440 '__parent__',
441 ''
442 ];
443 Expect.isTrue(map.isEmpty);
444 for (var key in weirdKeys) {
445 Expect.isFalse(map.containsKey(key));
446 Expect.equals(null, map[key]);
447 var value = 'value:$key';
448 map[key] = value;
449 Expect.isTrue(map.containsKey(key));
450 Expect.equals(value, map[key]);
451 Expect.equals(value, map.remove(key));
452 Expect.isFalse(map.containsKey(key));
453 Expect.equals(null, map[key]);
454 }
455 Expect.isTrue(map.isEmpty);
456 }
457
458 void testNumericKeys(Map map) {
459 var numericKeys = const [
460 double.INFINITY,
461 double.NEGATIVE_INFINITY,
462 0,
463 0.0,
464 -0.0
465 ];
466
467 Expect.isTrue(map.isEmpty);
468 for (var key in numericKeys) {
469 Expect.isFalse(map.containsKey(key));
470 Expect.equals(null, map[key]);
471 var value = 'value:$key';
472 map[key] = value;
473 Expect.isTrue(map.containsKey(key));
474 Expect.equals(value, map[key]);
475 Expect.equals(value, map.remove(key));
476 Expect.isFalse(map.containsKey(key));
477 Expect.equals(null, map[key]);
478 }
479 Expect.isTrue(map.isEmpty);
480 }
481
482 void testNaNKeys(Map map) {
483 Expect.isTrue(map.isEmpty);
484 // Test NaN.
485 var nan = double.NAN;
486 Expect.isFalse(map.containsKey(nan));
487 Expect.equals(null, map[nan]);
488
489 map[nan] = 'value:0';
490 Expect.isFalse(map.containsKey(nan));
491 Expect.equals(null, map[nan]);
492 testLength(1, map);
493
494 map[nan] = 'value:1';
495 Expect.isFalse(map.containsKey(nan));
496 Expect.equals(null, map[nan]);
497 testLength(2, map);
498
499 Expect.equals(null, map.remove(nan));
500 testLength(2, map);
501
502 var count = 0;
503 map.forEach((key, value) {
504 if (key.isNaN) count++;
505 });
506 Expect.equals(2, count);
507
508 map.clear();
509 Expect.isTrue(map.isEmpty);
510 }
511
512 void testLength(int length, Map map) {
513 Expect.equals(length, map.length);
514 Expect.equals(length, map.keys.length);
515 Expect.equals(length, map.values.length);
516 // Check being-empty.
517 var ifEmpty = (length == 0) ? Expect.isTrue : Expect.isFalse;
518 var ifNotEmpty = (length != 0) ? Expect.isTrue : Expect.isFalse;
519 ifEmpty(map.isEmpty);
520 ifNotEmpty(map.isNotEmpty);
521 ifEmpty(map.keys.isEmpty);
522 ifNotEmpty(map.keys.isNotEmpty);
523 ifEmpty(map.values.isEmpty);
524 ifNotEmpty(map.values.isNotEmpty);
525 // Test key/value iterators match their isEmpty/isNotEmpty.
526 ifNotEmpty(map.keys.iterator.moveNext());
527 ifNotEmpty(map.values.iterator.moveNext());
528 if (length == 0) {
529 for (var k in map.keys) Expect.fail("contains key when iterating: $k");
530 for (var v in map.values) Expect.fail("contains values when iterating: $v");
531 }
532 }
533
534 testIdentityMap(Map map) {
535 Expect.isTrue(map.isEmpty);
536
537 var nan = double.NAN;
538 // TODO(11551): Remove guard when dart2js makes identical(NaN, NaN) true.
539 if (identical(nan, nan)) {
540 map[nan] = 42;
541 testLength(1, map);
542 Expect.isTrue(map.containsKey(nan));
543 Expect.equals(42, map[nan]);
544 map[nan] = 37;
545 testLength(1, map);
546 Expect.equals(37, map[nan]);
547 Expect.equals(37, map.remove(nan));
548 testLength(0, map);
549 }
550
551 Vampire v1 = const Vampire(1);
552 Vampire v2 = const Vampire(2);
553 Expect.isFalse(v1 == v1);
554 Expect.isFalse(v2 == v2);
555 Expect.isTrue(v2 == v1); // Snob!
556
557 map[v1] = 1;
558 map[v2] = 2;
559 testLength(2, map);
560
561 Expect.isTrue(map.containsKey(v1));
562 Expect.isTrue(map.containsKey(v2));
563
564 Expect.equals(1, map[v1]);
565 Expect.equals(2, map[v2]);
566
567 Expect.equals(1, map.remove(v1));
568 testLength(1, map);
569 Expect.isFalse(map.containsKey(v1));
570 Expect.isTrue(map.containsKey(v2));
571
572 Expect.isNull(map.remove(v1));
573 Expect.equals(2, map.remove(v2));
574 testLength(0, map);
575
576 var eq01 = new Equalizer(0);
577 var eq02 = new Equalizer(0);
578 var eq11 = new Equalizer(1);
579 var eq12 = new Equalizer(1);
580 // Sanity.
581 Expect.equals(eq01, eq02);
582 Expect.equals(eq02, eq01);
583 Expect.equals(eq11, eq12);
584 Expect.equals(eq12, eq11);
585 Expect.notEquals(eq01, eq11);
586 Expect.notEquals(eq01, eq12);
587 Expect.notEquals(eq02, eq11);
588 Expect.notEquals(eq02, eq12);
589 Expect.notEquals(eq11, eq01);
590 Expect.notEquals(eq11, eq02);
591 Expect.notEquals(eq12, eq01);
592 Expect.notEquals(eq12, eq02);
593
594 map[eq01] = 0;
595 map[eq02] = 1;
596 map[eq11] = 2;
597 map[eq12] = 3;
598 testLength(4, map);
599
600 Expect.equals(0, map[eq01]);
601 Expect.equals(1, map[eq02]);
602 Expect.equals(2, map[eq11]);
603 Expect.equals(3, map[eq12]);
604
605 Expect.isTrue(map.containsKey(eq01));
606 Expect.isTrue(map.containsKey(eq02));
607 Expect.isTrue(map.containsKey(eq11));
608 Expect.isTrue(map.containsKey(eq12));
609
610 Expect.equals(1, map.remove(eq02));
611 Expect.equals(3, map.remove(eq12));
612 testLength(2, map);
613 Expect.isTrue(map.containsKey(eq01));
614 Expect.isFalse(map.containsKey(eq02));
615 Expect.isTrue(map.containsKey(eq11));
616 Expect.isFalse(map.containsKey(eq12));
617
618 Expect.equals(0, map[eq01]);
619 Expect.equals(null, map[eq02]);
620 Expect.equals(2, map[eq11]);
621 Expect.equals(null, map[eq12]);
622
623 Expect.equals(0, map.remove(eq01));
624 Expect.equals(2, map.remove(eq11));
625 testLength(0, map);
626
627 map[eq01] = 0;
628 map[eq02] = 1;
629 map[eq11] = 2;
630 map[eq12] = 3;
631 testLength(4, map);
632
633 // Transfer to equality-based map will collapse elements.
634 Map eqMap = new HashMap();
635 eqMap.addAll(map);
636 testLength(2, eqMap);
637 Expect.isTrue(eqMap.containsKey(eq01));
638 Expect.isTrue(eqMap.containsKey(eq02));
639 Expect.isTrue(eqMap.containsKey(eq11));
640 Expect.isTrue(eqMap.containsKey(eq12));
641
642 // Changing objects will not affect identity map.
643 map.clear();
644 var m1 = new Mutable(1);
645 var m2 = new Mutable(2);
646 var m3 = new Mutable(3);
647 map[m1] = 1;
648 map[m2] = 2;
649 map[m3] = 3;
650 Expect.equals(3, map.length);
651 Expect.isTrue(map.containsKey(m1));
652 Expect.isTrue(map.containsKey(m2));
653 Expect.isTrue(map.containsKey(m3));
654 Expect.notEquals(m1, m3);
655 m3.id = 1;
656 Expect.equals(m1, m3);
657 // Even if keys are equal, they are still not identical.
658 // Even if hashcode of m3 changed, it can still be found.
659 Expect.equals(1, map[m1]);
660 Expect.equals(3, map[m3]);
661 }
662
663 /** Class of objects that are equal if they hold the same id. */
664 class Equalizer {
665 int id;
666 Equalizer(this.id);
667 int get hashCode => id;
668 bool operator ==(Object other) =>
669 other is Equalizer && id == (other as Equalizer).id;
670 }
671
672 /**
673 * Objects that are not reflexive.
674 *
675 * They think they are better than their equals.
676 */
677 class Vampire {
678 final int generation;
679 const Vampire(this.generation);
680
681 int get hashCode => generation;
682
683 // The double-fang operator falsely claims that a vampire is equal to
684 // any of its sire's generation.
685 bool operator ==(Object other) =>
686 other is Vampire && generation - 1 == (other as Vampire).generation;
687 }
688
689 void testCustomMap(Map map) {
690 testLength(0, map);
691 var c11 = const Customer(1, 1);
692 var c12 = const Customer(1, 2);
693 var c21 = const Customer(2, 1);
694 var c22 = const Customer(2, 2);
695 // Sanity.
696 Expect.equals(c11, c12);
697 Expect.notEquals(c11, c21);
698 Expect.notEquals(c11, c22);
699 Expect.equals(c21, c22);
700 Expect.notEquals(c21, c11);
701 Expect.notEquals(c21, c12);
702
703 Expect.isTrue(myEquals(c11, c21));
704 Expect.isFalse(myEquals(c11, c12));
705 Expect.isFalse(myEquals(c11, c22));
706 Expect.isTrue(myEquals(c12, c22));
707 Expect.isFalse(myEquals(c12, c11));
708 Expect.isFalse(myEquals(c12, c21));
709
710 map[c11] = 42;
711 testLength(1, map);
712 Expect.isTrue(map.containsKey(c11));
713 Expect.isTrue(map.containsKey(c21));
714 Expect.isFalse(map.containsKey(c12));
715 Expect.isFalse(map.containsKey(c22));
716 Expect.equals(42, map[c11]);
717 Expect.equals(42, map[c21]);
718
719 map[c21] = 37;
720 testLength(1, map);
721 Expect.isTrue(map.containsKey(c11));
722 Expect.isTrue(map.containsKey(c21));
723 Expect.isFalse(map.containsKey(c12));
724 Expect.isFalse(map.containsKey(c22));
725 Expect.equals(37, map[c11]);
726 Expect.equals(37, map[c21]);
727
728 map[c22] = 42;
729 testLength(2, map);
730 Expect.isTrue(map.containsKey(c11));
731 Expect.isTrue(map.containsKey(c21));
732 Expect.isTrue(map.containsKey(c12));
733 Expect.isTrue(map.containsKey(c22));
734 Expect.equals(37, map[c11]);
735 Expect.equals(37, map[c21]);
736 Expect.equals(42, map[c12]);
737 Expect.equals(42, map[c22]);
738
739 Expect.equals(42, map.remove(c12));
740 testLength(1, map);
741 Expect.isTrue(map.containsKey(c11));
742 Expect.isTrue(map.containsKey(c21));
743 Expect.isFalse(map.containsKey(c12));
744 Expect.isFalse(map.containsKey(c22));
745 Expect.equals(37, map[c11]);
746 Expect.equals(37, map[c21]);
747
748 Expect.equals(37, map.remove(c11));
749 testLength(0, map);
750 }
751
752 void testUnmodifiableMap(Map map) {
753 Expect.isTrue(map.containsKey(1));
754 testLength(1, map);
755 Expect.equals(1, map.keys.first);
756 Expect.equals(37, map.values.first);
757
758 Expect.throws(map.clear);
759 Expect.throws(() {
760 map.remove(1);
761 });
762 Expect.throws(() {
763 map[2] = 42;
764 });
765 Expect.throws(() {
766 map.addAll({2: 42});
767 });
768 }
769
770 class Customer {
771 final int id;
772 final int secondId;
773 const Customer(this.id, this.secondId);
774 int get hashCode => id;
775 bool operator ==(Object other) {
776 if (other is! Customer) return false;
777 Customer otherCustomer = other;
778 return id == otherCustomer.id;
779 }
780 }
781
782 int myHashCode(Customer c) => c.secondId;
783 bool myEquals(Customer a, Customer b) => a.secondId == b.secondId;
784
785 void testIterationOrder(Map map) {
786 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;
788 Expect.listEquals(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]);
790 }
791
792 void testOtherKeys(Map<int, int> map) {
793 // 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
795 // use the equality/comparator on incompatible objects.
796
797 // 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"));
802 Expect.isFalse(map.containsKey(1.5));
803 Expect.isNull(map.remove("not an int"));
804 Expect.isNull(map.remove(1.5));
805 Expect.isNull(map["not an int"]);
806 Expect.isNull(map[1.5]);
807 }
808
809 class Mutable {
810 int id;
811 Mutable(this.id);
812 int get hashCode => id;
813 bool operator ==(other) => other is Mutable && other.id == id;
814 }
815
816 // Slow implementation of Map based on MapBase.
817 abstract class MapBaseOperations<K, V> {
818 final List _keys = <K>[];
819 final List _values = <V>[];
820 int _modCount = 0;
821
822 V operator [](Object key) {
823 int index = _keys.indexOf(key);
824 if (index < 0) return null;
825 return _values[index];
826 }
827
828 Iterable<K> get keys => new TestKeyIterable<K>(this);
829
830 void operator []=(K key, V value) {
831 int index = _keys.indexOf(key);
832 if (index >= 0) {
833 _values[index] = value;
834 } else {
835 _modCount++;
836 _keys.add(key);
837 _values.add(value);
838 }
839 }
840
841 V remove(Object key) {
842 int index = _keys.indexOf(key);
843 if (index >= 0) {
844 var result = _values[index];
845 key = _keys.removeLast();
846 var value = _values.removeLast();
847 if (index != _keys.length) {
848 _keys[index] = key;
849 _values[index] = value;
850 }
851 _modCount++;
852 return result;
853 }
854 return null;
855 }
856
857 void clear() {
858 // Clear cannot be based on remove, since remove won't remove keys that
859 // are not equal to themselves. It will fail the testNaNKeys test.
860 _keys.clear();
861 _values.clear();
862 _modCount++;
863 }
864 }
865
866 class MapBaseMap<K, V> = MapBase<K, V> with MapBaseOperations<K, V>;
867 class MapMixinMap<K, V> = MapBaseOperations<K, V> with MapMixin<K, V>;
868
869 class TestKeyIterable<K> extends IterableBase<K> {
870 final _map;
871 TestKeyIterable(this._map);
872 int get length => _map._keys.length;
873 Iterator<K> get iterator => new TestKeyIterator<K>(_map);
874 }
875
876 class TestKeyIterator<K> implements Iterator<K> {
877 final _map;
878 final int _modCount;
879 int _index = 0;
880 var _current;
881 TestKeyIterator(map)
882 : _map = map,
883 _modCount = map._modCount;
884 bool moveNext() {
885 if (_modCount != _map._modCount) {
886 throw new ConcurrentModificationError(_map);
887 }
888 if (_index == _map._keys.length) {
889 _current = null;
890 return false;
891 }
892 _current = _map._keys[_index++];
893 return true;
894 }
895
896 K get current => _current;
897 }
898
899 // Slow implementation of Map based on MapBase.
900 class UnmodifiableMapBaseMap<K, V> extends UnmodifiableMapBase<K, V> {
901 final List _keys = <K>[];
902 final List _values = <V>[];
903 UnmodifiableMapBaseMap(List pairs) {
904 for (int i = 0; i < pairs.length; i += 2) {
905 _keys.add(pairs[i]);
906 _values.add(pairs[i + 1]);
907 }
908 }
909
910 int get _modCount => 0;
911
912 V operator [](K key) {
913 int index = _keys.indexOf(key);
914 if (index < 0) return null;
915 return _values[index];
916 }
917
918 Iterable<K> get keys => _keys.skip(0);
919 }
920
921 abstract class Super implements Comparable {}
922
923 abstract class Interface implements Comparable {}
924
925 class Sub extends Super implements Interface, Comparable {
926 int compareTo(Sub other) => 0;
927 int get hashCode => 0;
928 bool operator ==(other) => other is Sub;
929 }
930
931 expectMap(Map expect, Map actual) {
932 Expect.equals(expect.length, actual.length, "length");
933 for (var key in expect.keys) {
934 Expect.isTrue(actual.containsKey(key), "containsKey $key");
935 Expect.equals(expect[key], actual[key]);
936 }
937 }
938
939 void testFrom() {
940 // Check contents.
941 for (var map in [
942 {},
943 {1: 1},
944 {1: 2, 3: 4, 5: 6, 7: 8}
945 ]) {
946 expectMap(map, new Map.from(map));
947 expectMap(map, new HashMap.from(map));
948 expectMap(map, new LinkedHashMap.from(map));
949 expectMap(map, new SplayTreeMap.from(map));
950 }
951 // Test type combinations allowed.
952 Map<int, int> intMap = <int, int>{1: 2, 3: 4};
953 Map<num, num> numMap = <num, num>{1: 2, 3: 4};
954 expectMap(intMap, new Map<int, int>.from(numMap));
955 expectMap(intMap, new Map<num, num>.from(intMap));
956 expectMap(intMap, new HashMap<int, int>.from(numMap));
957 expectMap(intMap, new HashMap<num, num>.from(intMap));
958 expectMap(intMap, new LinkedHashMap<int, int>.from(numMap));
959 expectMap(intMap, new LinkedHashMap<num, num>.from(intMap));
960 expectMap(intMap, new SplayTreeMap<int, int>.from(numMap));
961 expectMap(intMap, new SplayTreeMap<num, num>.from(intMap));
962
963 var sub = new Sub();
964 Map<Super, Super> superMap = <Super, Super>{sub: sub};
965 Map<Interface, Interface> interfaceMap = <Interface, Interface>{sub: sub};
966 expectMap(superMap, new Map<Super, Super>.from(interfaceMap));
967 expectMap(superMap, new Map<Interface, Interface>.from(superMap));
968 expectMap(superMap, new HashMap<Super, Super>.from(interfaceMap));
969 expectMap(superMap, new HashMap<Interface, Interface>.from(superMap));
970 expectMap(superMap, new LinkedHashMap<Super, Super>.from(interfaceMap));
971 expectMap(superMap, new LinkedHashMap<Interface, Interface>.from(superMap));
972 expectMap(superMap, new SplayTreeMap<Super, Super>.from(interfaceMap));
973 expectMap(superMap, new SplayTreeMap<Interface, Interface>.from(superMap));
974 }
OLDNEW
« no previous file with comments | « tests/corelib/map_remove_test.dart ('k') | tests/corelib_2/corelib_2.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698